| 1 | package receiver_test |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "log" |
| 6 | "os" |
| 7 | "os/user" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/gokrazy/rsync/internal/rsyncdconfig" |
| 16 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 17 | "github.com/gokrazy/rsync/rsyncd" |
| 18 | "github.com/google/go-cmp/cmp" |
| 19 | ) |
| 20 | |
| 21 | func TestMain(m *testing.M) { |
| 22 | if err := rsynctest.CommandMain(m); err != nil { |
| 23 | log.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func setUid(t *testing.T, fn string) (uid, gid int, verify bool) { |
| 28 | if os.Getuid() != 0 { |
| 29 | return 0, 0, false |
| 30 | } |
| 31 | |
| 32 | u, err := user.Lookup("nobody") |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | |
| 37 | uid64, err := strconv.ParseInt(u.Uid, 0, 64) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | uid = int(uid64) |
| 42 | |
| 43 | gid64, err := strconv.ParseInt(u.Gid, 0, 64) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | gid = int(gid64) |
| 48 | |
| 49 | if err := os.Chown(fn, uid, gid); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | |
| 53 | return uid, gid, true |
| 54 | } |
| 55 | |
| 56 | func TestReceiver(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | |
| 59 | tmp := t.TempDir() |
| 60 | source := filepath.Join(tmp, "source") |
| 61 | dest := filepath.Join(tmp, "dest") |
| 62 | |
| 63 | if err := os.MkdirAll(source, 0755); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | sourcesub := filepath.Join(source, "subdir") |
| 67 | if err := os.MkdirAll(sourcesub, 0755); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | sourcesubempty := filepath.Join(source, "subdirempty") |
| 71 | if err := os.MkdirAll(sourcesubempty, 0755); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | hello := filepath.Join(source, "hello") |
| 75 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | hellosub := filepath.Join(sourcesub, "hello") |
| 79 | if err := os.WriteFile(hellosub, []byte("space"), 0644); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | if err := os.Chtimes(hellosub, mtime, mtime); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if err := os.Chtimes(sourcesubempty, mtime, mtime); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | |
| 99 | if err := os.Symlink("hello", filepath.Join(source, "hey")); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | |
| 103 | no := filepath.Join(source, "no") |
| 104 | if err := os.WriteFile(no, []byte("no"), 0666); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | uid, gid, verifyUid := setUid(t, no) |
| 108 | |
| 109 | devices := filepath.Join(source, "devices") |
| 110 | if os.Getuid() == 0 { |
| 111 | rsynctest.CreateDummyDeviceFiles(t, devices) |
| 112 | } |
| 113 | |
| 114 | // start a server to sync from |
| 115 | srv := rsynctest.NewInMemory(t, rsyncd.Module{ |
| 116 | Name: "interop", |
| 117 | Path: source, |
| 118 | }) |
| 119 | args := []string{"-aH"} |
| 120 | firstStats := srv.RunClient(t, args, "./", []string{dest}) |
| 121 | |
| 122 | { |
| 123 | want := []byte("world") |
| 124 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | if diff := cmp.Diff(want, got); diff != "" { |
| 129 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 130 | } |
| 131 | } |
| 132 | { |
| 133 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | want := "hello" |
| 138 | if got != want { |
| 139 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 140 | } |
| 141 | } |
| 142 | if verifyUid { |
| 143 | st, err := os.Stat(filepath.Join(dest, "no")) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | gotUID, gotGID := rsynctest.StatUidGid(t, st) |
| 148 | if got, want := gotUID, uid; got != want { |
| 149 | t.Errorf("unexpected uid: got %d, want %d", got, want) |
| 150 | } |
| 151 | if got, want := gotGID, gid; got != want { |
| 152 | t.Errorf("unexpected gid: got %d, want %d", got, want) |
| 153 | } |
| 154 | } |
| 155 | { |
| 156 | st, err := os.Stat(filepath.Join(dest, "subdir", "hello")) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | if got, want := st.ModTime(), mtime; !got.Equal(want) { |
| 161 | t.Errorf("dest/subdir/hello has unexpected mod time: got %v, want %v", got, want) |
| 162 | } |
| 163 | } |
| 164 | { |
| 165 | st, err := os.Stat(filepath.Join(dest, "subdirempty")) |
| 166 | if err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | if got, want := st.ModTime(), mtime; !got.Equal(want) { |
| 170 | t.Errorf("dest/subdirempty has unexpected mod time: got %v, want %v", got, want) |
| 171 | } |
| 172 | } |
| 173 | if os.Getuid() == 0 { |
| 174 | rsynctest.VerifyDummyDeviceFiles(t, devices, filepath.Join(dest, "devices")) |
| 175 | } |
| 176 | |
| 177 | incrementalStats := srv.RunClient(t, args, "./", []string{dest}) |
| 178 | if incrementalStats.Written >= firstStats.Written { |
| 179 | t.Fatalf("incremental run unexpectedly not more efficient than first run: incremental wrote %d bytes, first wrote %d bytes", incrementalStats.Written, firstStats.Written) |
| 180 | } |
| 181 | |
| 182 | // Make a change that is invisible with our current settings: |
| 183 | // change the file contents without changing size and mtime. |
| 184 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | // Replace the dest symlink to see if it will be restored |
| 194 | rsynctest.ReplaceSymlink(t, "wrong", filepath.Join(dest, "hey")) |
| 195 | |
| 196 | srv.RunClient(t, args, "./", []string{dest}) |
| 197 | |
| 198 | { |
| 199 | want := []byte("world") |
| 200 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 201 | if err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | if diff := cmp.Diff(want, got); diff != "" { |
| 205 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 206 | } |
| 207 | } |
| 208 | { |
| 209 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | want := "hello" |
| 214 | if got != want { |
| 215 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestReceiverSync(t *testing.T) { |
| 221 | t.Parallel() |
| 222 | |
| 223 | tmp := t.TempDir() |
| 224 | source := filepath.Join(tmp, "source") |
| 225 | dest := filepath.Join(tmp, "dest") |
| 226 | destLarge := filepath.Join(dest, "large-data-file") |
| 227 | |
| 228 | headPattern := []byte{0x11} |
| 229 | bodyPattern := []byte{0xbb} |
| 230 | endPattern := []byte{0xee} |
| 231 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 232 | |
| 233 | // start a server to sync from |
| 234 | srv := rsynctest.NewInMemory(t, rsyncd.Module{ |
| 235 | Name: "interop", |
| 236 | Path: source, |
| 237 | }) |
| 238 | args := []string{"-aH"} |
| 239 | firstStats := srv.RunClient(t, args, "./", []string{dest}) |
| 240 | t.Logf("firstStats: %+v", firstStats) |
| 241 | // receiver_test.go:211: firstStats: &{Read:91 Written:3146087 Size:3149824} |
| 242 | |
| 243 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 244 | t.Fatal(err) |
| 245 | } |
| 246 | |
| 247 | // Change the middle of the large data file: |
| 248 | bodyPattern = []byte{0x66} |
| 249 | // modify the large data file |
| 250 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 251 | |
| 252 | incrementalStats := srv.RunClient(t, args, "./", []string{dest}) |
| 253 | t.Logf("incrementalStats: %+v", incrementalStats) |
| 254 | if got, want := incrementalStats.Written, int64(2*1024*1024); got >= want { |
| 255 | t.Fatalf("rsync unexpectedly transferred more data than needed: got %d, want < %d", got, want) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | func TestReceiverSyncDelete(t *testing.T) { |
| 260 | t.Parallel() |
| 261 | |
| 262 | tmp := t.TempDir() |
| 263 | source := filepath.Join(tmp, "source") |
| 264 | dest := filepath.Join(tmp, "dest") |
| 265 | destLarge := filepath.Join(dest, "large-data-file") |
| 266 | |
| 267 | headPattern := []byte{0x11} |
| 268 | bodyPattern := []byte{0xbb} |
| 269 | endPattern := []byte{0xee} |
| 270 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 271 | |
| 272 | // start a server to sync from |
| 273 | srv := rsynctest.NewInMemory(t, rsyncd.Module{ |
| 274 | Name: "interop", |
| 275 | Path: source, |
| 276 | }) |
| 277 | args := []string{"-aH", "--delete"} |
| 278 | firstStats := srv.RunClient(t, args, "./", []string{dest}) |
| 279 | t.Logf("firstStats: %+v", firstStats) |
| 280 | // receiver_test.go:211: firstStats: &{Read:91 Written:3146087 Size:3149824} |
| 281 | |
| 282 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | |
| 286 | // Add more files to the destination, which should be deleted: |
| 287 | extra := filepath.Join(dest, "extrafile") |
| 288 | if err := os.WriteFile(extra, []byte("deleteme"), 0644); err != nil { |
| 289 | t.Fatal(err) |
| 290 | } |
| 291 | extraDir := filepath.Join(dest, "extradir") |
| 292 | if err := os.MkdirAll(extraDir, 0755); err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | extra2 := filepath.Join(extraDir, "blocker") |
| 296 | if err := os.WriteFile(extra2, []byte("deleteme"), 0644); err != nil { |
| 297 | t.Fatal(err) |
| 298 | } |
| 299 | |
| 300 | srv.RunClient(t, args, "./", []string{dest}) |
| 301 | for _, gone := range []string{extra, extraDir, extra2} { |
| 302 | if _, err := os.Stat(gone); !os.IsNotExist(err) { |
| 303 | t.Errorf("expected %s to be deleted, but it still exists", gone) |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | func TestReceiverAlwaysChecksum(t *testing.T) { |
| 309 | t.Parallel() |
| 310 | |
| 311 | tmp := t.TempDir() |
| 312 | source := filepath.Join(tmp, "source") |
| 313 | dest := filepath.Join(tmp, "dest") |
| 314 | if err := os.MkdirAll(source, 0755); err != nil { |
| 315 | t.Fatal(err) |
| 316 | } |
| 317 | hello := filepath.Join(source, "hello.txt") |
| 318 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 322 | if err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 326 | t.Fatal(err) |
| 327 | } |
| 328 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 329 | t.Fatal(err) |
| 330 | } |
| 331 | |
| 332 | // start a server to sync from |
| 333 | srv := rsynctest.NewInMemory(t, rsyncd.Module{ |
| 334 | Name: "interop", |
| 335 | Path: source, |
| 336 | }) |
| 337 | args := []string{"-aH"} |
| 338 | srv.RunClient(t, args, "./", []string{dest}) |
| 339 | |
| 340 | desthello := filepath.Join(dest, "hello.txt") |
| 341 | b, err := os.ReadFile(desthello) |
| 342 | if err != nil { |
| 343 | t.Fatal(err) |
| 344 | } |
| 345 | if got, want := strings.TrimSpace(string(b)), "world"; got != want { |
| 346 | t.Fatalf("hello.txt: unexpected contents: got %q, want %q", got, want) |
| 347 | } |
| 348 | |
| 349 | // Make a change that is invisible with our current settings: |
| 350 | // change the file contents without changing size and mtime. |
| 351 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 352 | t.Fatal(err) |
| 353 | } |
| 354 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 355 | t.Fatal(err) |
| 356 | } |
| 357 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 358 | t.Fatal(err) |
| 359 | } |
| 360 | |
| 361 | args = append(args, "--checksum") |
| 362 | srv.RunClient(t, args, "./", []string{dest}) |
| 363 | |
| 364 | b, err = os.ReadFile(desthello) |
| 365 | if err != nil { |
| 366 | t.Fatal(err) |
| 367 | } |
| 368 | if got, want := strings.TrimSpace(string(b)), "moon!"; got != want { |
| 369 | t.Fatalf("hello.txt: unexpected contents: got %q, want %q", got, want) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | func TestReceiverReadOnlyDir(t *testing.T) { |
| 374 | t.Parallel() |
| 375 | |
| 376 | tmp := t.TempDir() |
| 377 | source := filepath.Join(tmp, "source") |
| 378 | dest := filepath.Join(tmp, "dest") |
| 379 | if err := os.MkdirAll(source, 0755); err != nil { |
| 380 | t.Fatal(err) |
| 381 | } |
| 382 | hello := filepath.Join(source, "hello.txt") |
| 383 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 384 | t.Fatal(err) |
| 385 | } |
| 386 | if err := os.Chmod(source, 0o555); err != nil { |
| 387 | t.Fatal(err) |
| 388 | } |
| 389 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 390 | if err != nil { |
| 391 | t.Fatal(err) |
| 392 | } |
| 393 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 394 | t.Fatal(err) |
| 395 | } |
| 396 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 397 | t.Fatal(err) |
| 398 | } |
| 399 | |
| 400 | // start a server to sync from |
| 401 | srv := rsynctest.NewInMemory(t, rsyncd.Module{ |
| 402 | Name: "interop", |
| 403 | Path: source, |
| 404 | }) |
| 405 | args := []string{"-aH"} |
| 406 | srv.RunClient(t, args, "./", []string{dest}) |
| 407 | |
| 408 | desthello := filepath.Join(dest, "hello.txt") |
| 409 | b, err := os.ReadFile(desthello) |
| 410 | if err != nil { |
| 411 | t.Fatal(err) |
| 412 | } |
| 413 | if got, want := strings.TrimSpace(string(b)), "world"; got != want { |
| 414 | t.Fatalf("hello.txt: unexpected contents: got %q, want %q", got, want) |
| 415 | } |
| 416 | st, err := os.Stat(dest) |
| 417 | if err != nil { |
| 418 | t.Fatal(err) |
| 419 | } |
| 420 | if got, want := st.Mode()&os.ModePerm, fs.FileMode(0o555); got != want { |
| 421 | t.Fatalf("dest: unexpected permission: got %v, want %v", got, want) |
| 422 | } |
| 423 | |
| 424 | // Make a change that is invisible with our current settings: |
| 425 | // change the file contents without changing size and mtime. |
| 426 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 427 | t.Fatal(err) |
| 428 | } |
| 429 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 430 | t.Fatal(err) |
| 431 | } |
| 432 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 433 | t.Fatal(err) |
| 434 | } |
| 435 | |
| 436 | args = append(args, "--checksum") |
| 437 | srv.RunClient(t, args, "./", []string{dest}) |
| 438 | |
| 439 | b, err = os.ReadFile(desthello) |
| 440 | if err != nil { |
| 441 | t.Fatal(err) |
| 442 | } |
| 443 | if got, want := strings.TrimSpace(string(b)), "moon!"; got != want { |
| 444 | t.Fatalf("hello.txt: unexpected contents: got %q, want %q", got, want) |
| 445 | } |
| 446 | |
| 447 | st, err = os.Stat(dest) |
| 448 | if err != nil { |
| 449 | t.Fatal(err) |
| 450 | } |
| 451 | if got, want := st.Mode()&os.ModePerm, fs.FileMode(0o555); got != want { |
| 452 | t.Fatalf("dest: unexpected permission: got %v, want %v", got, want) |
| 453 | } |
| 454 | |
| 455 | // Restore write permission so that t.TempDir() cleanup succeeds |
| 456 | if err := os.Chmod(dest, 0755); err != nil { |
| 457 | t.Fatal(err) |
| 458 | } |
| 459 | if err := os.Chmod(source, 0755); err != nil { |
| 460 | t.Fatal(err) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | func TestReceiverSSH(t *testing.T) { |
| 465 | if runtime.GOOS == "windows" { |
| 466 | t.Skip("ssh transport test relies on a POSIX ssh client and /dev/null") |
| 467 | } |
| 468 | |
| 469 | tmp := t.TempDir() |
| 470 | source := filepath.Join(tmp, "source") |
| 471 | dest := filepath.Join(tmp, "dest") |
| 472 | |
| 473 | if err := os.MkdirAll(source, 0755); err != nil { |
| 474 | t.Fatal(err) |
| 475 | } |
| 476 | if err := os.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil { |
| 477 | t.Fatal(err) |
| 478 | } |
| 479 | |
| 480 | // start a server to sync from |
| 481 | srv := rsynctest.New(t, |
| 482 | rsynctest.InteropModule(source), |
| 483 | rsynctest.Listeners([]rsyncdconfig.Listener{ |
| 484 | { |
| 485 | AnonSSH: "localhost:0", |
| 486 | HostKeyPath: filepath.Join(tmp, "ssh_host_ed25519_key"), |
| 487 | }, |
| 488 | })) |
| 489 | |
| 490 | // ensure the user running the tests (root when doing the privileged run!) |
| 491 | // has an SSH private key: |
| 492 | privKeyPath := filepath.Join(tmp, "ssh_private_key") |
| 493 | if err := genKey(privKeyPath); err != nil { |
| 494 | t.Fatal(err) |
| 495 | } |
| 496 | |
| 497 | // Ensure SSH_* environment variables (like SSH_ASKPASS or |
| 498 | // SSH_AUTH_SOCK) do not leak into the test, otherwise tests |
| 499 | // might be interrupted by a text UI password prompt. |
| 500 | t.Setenv("SSH_ASKPASS", "") |
| 501 | t.Setenv("SSH_AUTH_SOCK", "") |
| 502 | // sync into dest dir |
| 503 | rsynctest.Run(t, "gokr-rsync", |
| 504 | "-aH", |
| 505 | "--dry-run", |
| 506 | "-e", "ssh -vv -o IdentityFile="+privKeyPath+" -o StrictHostKeyChecking=no -o CheckHostIP=no -o UserKnownHostsFile=/dev/null -p "+srv.Port, |
| 507 | "rsync://localhost/interop/", |
| 508 | dest) |
| 509 | } |
| 510 | |
| 511 | func TestReceiverCommand(t *testing.T) { |
| 512 | t.Parallel() |
| 513 | |
| 514 | tmp := t.TempDir() |
| 515 | source := filepath.Join(tmp, "source") |
| 516 | dest := filepath.Join(tmp, "dest") |
| 517 | |
| 518 | if err := os.MkdirAll(source, 0755); err != nil { |
| 519 | t.Fatal(err) |
| 520 | } |
| 521 | if err := os.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil { |
| 522 | t.Fatal(err) |
| 523 | } |
| 524 | |
| 525 | // sync into dest dir |
| 526 | rsynctest.Run(t, "gokr-rsync", |
| 527 | "-aH", |
| 528 | "--dry-run", |
| 529 | "-e", `"`+os.Args[0]+`"`, |
| 530 | "localhost:"+source+"/", |
| 531 | dest) |
| 532 | } |
| 533 | |
| 534 | // TestReceiverSymlinkTraversal passes by default but is useful to simulate |
| 535 | // a symlink race TOCTOU attack by modifying rsyncd/rsyncd.go. |
| 536 | func TestReceiverSymlinkTraversal(t *testing.T) { |
| 537 | t.Parallel() |
| 538 | |
| 539 | tmp := t.TempDir() |
| 540 | if err := os.WriteFile(filepath.Join(tmp, "passwd"), []byte("secret"), 0644); err != nil { |
| 541 | t.Fatal(err) |
| 542 | } |
| 543 | source := filepath.Join(tmp, "source") |
| 544 | dest := filepath.Join(tmp, "dest") |
| 545 | |
| 546 | if err := os.MkdirAll(source, 0755); err != nil { |
| 547 | t.Fatal(err) |
| 548 | } |
| 549 | hello := filepath.Join(source, "passwd") |
| 550 | if err := os.WriteFile(hello, []byte("benign"), 0644); err != nil { |
| 551 | t.Fatal(err) |
| 552 | } |
| 553 | subdir := filepath.Join(source, "dir") |
| 554 | if err := os.MkdirAll(subdir, 0755); err != nil { |
| 555 | t.Fatal(err) |
| 556 | } |
| 557 | subhello := filepath.Join(subdir, "passwd") |
| 558 | if err := os.WriteFile(subhello, []byte("benign"), 0644); err != nil { |
| 559 | t.Fatal(err) |
| 560 | } |
| 561 | |
| 562 | // start a server to sync from |
| 563 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 564 | |
| 565 | rsynctest.Run(t, "gokr-rsync", |
| 566 | "-aH", |
| 567 | "rsync://localhost:"+srv.Port+"/interop/", |
| 568 | dest) |
| 569 | |
| 570 | { |
| 571 | want := []byte("benign") |
| 572 | got, err := os.ReadFile(filepath.Join(dest, "passwd")) |
| 573 | if err != nil { |
| 574 | t.Fatal(err) |
| 575 | } |
| 576 | if diff := cmp.Diff(want, got); diff != "" { |
| 577 | t.Errorf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | { |
| 582 | want := []byte("benign") |
| 583 | got, err := os.ReadFile(filepath.Join(dest, "dir", "passwd")) |
| 584 | if err != nil { |
| 585 | t.Fatal(err) |
| 586 | } |
| 587 | if diff := cmp.Diff(want, got); diff != "" { |
| 588 | t.Errorf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 589 | } |
| 590 | } |
| 591 | } |