| 1 | package sender_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "log" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "os/user" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strconv" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 16 | "github.com/gokrazy/rsync/internal/testlogger" |
| 17 | "github.com/google/go-cmp/cmp" |
| 18 | ) |
| 19 | |
| 20 | func TestMain(m *testing.M) { |
| 21 | if err := rsynctest.CommandMain(m); err != nil { |
| 22 | log.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func setUid(t *testing.T, fn string) (uid, gid int, verify bool) { |
| 27 | if os.Getuid() != 0 { |
| 28 | return 0, 0, false |
| 29 | } |
| 30 | |
| 31 | u, err := user.Lookup("nobody") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | |
| 36 | uid64, err := strconv.ParseInt(u.Uid, 0, 64) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | uid = int(uid64) |
| 41 | |
| 42 | gid64, err := strconv.ParseInt(u.Gid, 0, 64) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | gid = int(gid64) |
| 47 | |
| 48 | if err := os.Chown(fn, uid, gid); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | return uid, gid, true |
| 53 | } |
| 54 | |
| 55 | func TestSender(t *testing.T) { |
| 56 | t.Parallel() |
| 57 | |
| 58 | tmp := t.TempDir() |
| 59 | source := filepath.Join(tmp, "source") |
| 60 | dest := filepath.Join(tmp, "dest") |
| 61 | |
| 62 | if err := os.MkdirAll(source, 0755); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | hello := filepath.Join(source, "hello") |
| 66 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | if err := os.Symlink("hello", filepath.Join(source, "hey")); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | |
| 84 | no := filepath.Join(source, "no") |
| 85 | if err := os.WriteFile(no, []byte("no"), 0666); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | uid, gid, verifyUid := setUid(t, no) |
| 89 | |
| 90 | devices := filepath.Join(source, "devices") |
| 91 | if os.Getuid() == 0 { |
| 92 | rsynctest.CreateDummyDeviceFiles(t, devices) |
| 93 | } |
| 94 | |
| 95 | // start a server to sync to |
| 96 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 97 | |
| 98 | args := []string{ |
| 99 | "gokr-rsync", |
| 100 | "-aH", |
| 101 | source + "/", |
| 102 | "rsync://localhost:" + srv.Port + "/interop/", |
| 103 | } |
| 104 | firstStats := rsynctest.Run(t, args...) |
| 105 | |
| 106 | { |
| 107 | want := []byte("world") |
| 108 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if diff := cmp.Diff(want, got); diff != "" { |
| 113 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 114 | } |
| 115 | } |
| 116 | { |
| 117 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | want := "hello" |
| 122 | if got != want { |
| 123 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 124 | } |
| 125 | } |
| 126 | if verifyUid { |
| 127 | st, err := os.Stat(filepath.Join(dest, "no")) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | gotUID, gotGID := rsynctest.StatUidGid(t, st) |
| 132 | if got, want := gotUID, uid; got != want { |
| 133 | t.Errorf("unexpected uid: got %d, want %d", got, want) |
| 134 | } |
| 135 | if got, want := gotGID, gid; got != want { |
| 136 | t.Errorf("unexpected gid: got %d, want %d", got, want) |
| 137 | } |
| 138 | } |
| 139 | if os.Getuid() == 0 { |
| 140 | rsynctest.VerifyDummyDeviceFiles(t, devices, filepath.Join(dest, "devices")) |
| 141 | } |
| 142 | |
| 143 | incrementalStats := rsynctest.Run(t, args...) |
| 144 | if incrementalStats.Written >= firstStats.Written { |
| 145 | t.Fatalf("incremental run unexpectedly not more efficient than first run: incremental wrote %d bytes, first wrote %d bytes", incrementalStats.Written, firstStats.Written) |
| 146 | } |
| 147 | |
| 148 | // Make a change that is invisible with our current settings: |
| 149 | // change the file contents without changing size and mtime. |
| 150 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | // Replace the dest symlink to see if it will be restored |
| 160 | rsynctest.ReplaceSymlink(t, "wrong", filepath.Join(dest, "hey")) |
| 161 | |
| 162 | rsynctest.Run(t, args...) |
| 163 | |
| 164 | { |
| 165 | want := []byte("world") |
| 166 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 167 | if err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | if diff := cmp.Diff(want, got); diff != "" { |
| 171 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 172 | } |
| 173 | } |
| 174 | { |
| 175 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 176 | if err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | want := "hello" |
| 180 | if got != want { |
| 181 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // like TestSender, but without a trailing slash, i.e. do not copy directory |
| 187 | // contents, but the directory itself. |
| 188 | func TestSenderNoSlash(t *testing.T) { |
| 189 | t.Parallel() |
| 190 | |
| 191 | tmp := t.TempDir() |
| 192 | source := filepath.Join(tmp, "source") |
| 193 | dest := filepath.Join(tmp, "dest") |
| 194 | |
| 195 | if err := os.MkdirAll(source, 0755); err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | hello := filepath.Join(source, "hello") |
| 199 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 200 | t.Fatal(err) |
| 201 | } |
| 202 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | |
| 213 | if err := os.Symlink("hello", filepath.Join(source, "hey")); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | |
| 217 | no := filepath.Join(source, "no") |
| 218 | if err := os.WriteFile(no, []byte("no"), 0666); err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | uid, gid, verifyUid := setUid(t, no) |
| 222 | |
| 223 | devices := filepath.Join(source, "devices") |
| 224 | if os.Getuid() == 0 { |
| 225 | rsynctest.CreateDummyDeviceFiles(t, devices) |
| 226 | } |
| 227 | |
| 228 | // start a server to sync to |
| 229 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 230 | |
| 231 | args := []string{ |
| 232 | "gokr-rsync", |
| 233 | "-aH", |
| 234 | source, |
| 235 | "rsync://localhost:" + srv.Port + "/interop/", |
| 236 | } |
| 237 | firstStats := rsynctest.Run(t, args...) |
| 238 | |
| 239 | dest = filepath.Join(dest, "source") |
| 240 | |
| 241 | { |
| 242 | want := []byte("world") |
| 243 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 244 | if err != nil { |
| 245 | t.Fatal(err) |
| 246 | } |
| 247 | if diff := cmp.Diff(want, got); diff != "" { |
| 248 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 249 | } |
| 250 | } |
| 251 | { |
| 252 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 253 | if err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | want := "hello" |
| 257 | if got != want { |
| 258 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 259 | } |
| 260 | } |
| 261 | if verifyUid { |
| 262 | st, err := os.Stat(filepath.Join(dest, "no")) |
| 263 | if err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | gotUID, gotGID := rsynctest.StatUidGid(t, st) |
| 267 | if got, want := gotUID, uid; got != want { |
| 268 | t.Errorf("unexpected uid: got %d, want %d", got, want) |
| 269 | } |
| 270 | if got, want := gotGID, gid; got != want { |
| 271 | t.Errorf("unexpected gid: got %d, want %d", got, want) |
| 272 | } |
| 273 | } |
| 274 | if os.Getuid() == 0 { |
| 275 | rsynctest.VerifyDummyDeviceFiles(t, devices, filepath.Join(dest, "devices")) |
| 276 | } |
| 277 | |
| 278 | incrementalStats := rsynctest.Run(t, args...) |
| 279 | if incrementalStats.Written >= firstStats.Written { |
| 280 | t.Fatalf("incremental run unexpectedly not more efficient than first run: incremental wrote %d bytes, first wrote %d bytes", incrementalStats.Written, firstStats.Written) |
| 281 | } |
| 282 | |
| 283 | // Make a change that is invisible with our current settings: |
| 284 | // change the file contents without changing size and mtime. |
| 285 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 286 | t.Fatal(err) |
| 287 | } |
| 288 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 289 | t.Fatal(err) |
| 290 | } |
| 291 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 292 | t.Fatal(err) |
| 293 | } |
| 294 | // Replace the dest symlink to see if it will be restored |
| 295 | rsynctest.ReplaceSymlink(t, "wrong", filepath.Join(dest, "hey")) |
| 296 | |
| 297 | rsynctest.Run(t, args...) |
| 298 | |
| 299 | { |
| 300 | want := []byte("world") |
| 301 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 302 | if err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | if diff := cmp.Diff(want, got); diff != "" { |
| 306 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 307 | } |
| 308 | } |
| 309 | { |
| 310 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 311 | if err != nil { |
| 312 | t.Fatal(err) |
| 313 | } |
| 314 | want := "hello" |
| 315 | if got != want { |
| 316 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // like TestSender, but using a relative path |
| 322 | func TestSenderRelative(t *testing.T) { |
| 323 | tmp := t.TempDir() |
| 324 | t.Chdir(tmp) |
| 325 | source := filepath.Join(tmp, "source") |
| 326 | dest := filepath.Join(tmp, "dest") |
| 327 | |
| 328 | if err := os.MkdirAll(source, 0755); err != nil { |
| 329 | t.Fatal(err) |
| 330 | } |
| 331 | hello := filepath.Join(source, "hello") |
| 332 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 333 | t.Fatal(err) |
| 334 | } |
| 335 | |
| 336 | // start a server to sync to |
| 337 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 338 | |
| 339 | args := []string{ |
| 340 | "gokr-rsync", |
| 341 | "-aH", |
| 342 | "source", |
| 343 | "rsync://localhost:" + srv.Port + "/interop/", |
| 344 | } |
| 345 | rsynctest.Run(t, args...) |
| 346 | |
| 347 | dest = filepath.Join(dest, "source") |
| 348 | |
| 349 | { |
| 350 | want := []byte("world") |
| 351 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 352 | if err != nil { |
| 353 | t.Fatal(err) |
| 354 | } |
| 355 | if diff := cmp.Diff(want, got); diff != "" { |
| 356 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // TestSenderNamedSourceParentUnreadable is a regression test for |
| 362 | // https://github.com/gokrazy/rsync/issues/66: syncing a named directory (no |
| 363 | // trailing slash) must not require access to the source’s parent directory. |
| 364 | // |
| 365 | // The sender allowlists the source itself for Landlock, but not its parent, so |
| 366 | // opening the parent (as the pre-fix code did to obtain the source basename) |
| 367 | // is denied. We reproduce that portably — without Landlock — by removing read |
| 368 | // access from the parent while keeping search (execute) access, which likewise |
| 369 | // makes os.OpenRoot(parent) fail but os.OpenRoot(source) succeed. |
| 370 | func TestSenderNamedSourceParentUnreadable(t *testing.T) { |
| 371 | if runtime.GOOS == "windows" { |
| 372 | t.Skip("POSIX directory permissions do not gate reads on Windows") |
| 373 | } |
| 374 | if os.Getuid() == 0 { |
| 375 | t.Skip("root bypasses directory permission checks, so the parent would still be readable") |
| 376 | } |
| 377 | t.Parallel() |
| 378 | |
| 379 | tmp := t.TempDir() |
| 380 | parent := filepath.Join(tmp, "parent") |
| 381 | source := filepath.Join(parent, "source") |
| 382 | dest := filepath.Join(tmp, "dest") |
| 383 | |
| 384 | if err := os.MkdirAll(source, 0755); err != nil { |
| 385 | t.Fatal(err) |
| 386 | } |
| 387 | if err := os.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil { |
| 388 | t.Fatal(err) |
| 389 | } |
| 390 | |
| 391 | // Remove read (but keep search) on the parent, then restore it before the |
| 392 | // t.TempDir() cleanup so RemoveAll can recurse back in. |
| 393 | if err := os.Chmod(parent, 0o111); err != nil { |
| 394 | t.Fatal(err) |
| 395 | } |
| 396 | t.Cleanup(func() { os.Chmod(parent, 0o755) }) |
| 397 | |
| 398 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 399 | |
| 400 | // Named directory: no trailing slash, so the source directory itself is |
| 401 | // copied and the receiver creates dest/source/hello. |
| 402 | // |
| 403 | // --gokr.dont_restrict: the unreadable parent (not Landlock) provides |
| 404 | // the constraint under test; skipping the Landlock ruleset stays within |
| 405 | // the kernel's limit of 16 stacked rulesets per process (all tests in |
| 406 | // this package share one process). |
| 407 | rsynctest.Run(t, "gokr-rsync", "-aH", "--gokr.dont_restrict", source, "rsync://localhost:"+srv.Port+"/interop/") |
| 408 | |
| 409 | want := []byte("world") |
| 410 | got, err := os.ReadFile(filepath.Join(dest, "source", "hello")) |
| 411 | if err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | if diff := cmp.Diff(want, got); diff != "" { |
| 415 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // TestSenderNamedFileParentUnreadable is the single-file variant of |
| 420 | // TestSenderNamedSourceParentUnreadable: syncing a named file must not |
| 421 | // require access to the file's parent directory either (issue #66; the |
| 422 | // client allowlists the file itself as a read-only Landlock rule). |
| 423 | func TestSenderNamedFileParentUnreadable(t *testing.T) { |
| 424 | if runtime.GOOS == "windows" { |
| 425 | t.Skip("POSIX directory permissions do not gate reads on Windows") |
| 426 | } |
| 427 | if os.Getuid() == 0 { |
| 428 | t.Skip("root bypasses directory permission checks, so the parent would still be readable") |
| 429 | } |
| 430 | t.Parallel() |
| 431 | |
| 432 | tmp := t.TempDir() |
| 433 | parent := filepath.Join(tmp, "parent") |
| 434 | hello := filepath.Join(parent, "hello") |
| 435 | dest := filepath.Join(tmp, "dest") |
| 436 | |
| 437 | if err := os.MkdirAll(parent, 0755); err != nil { |
| 438 | t.Fatal(err) |
| 439 | } |
| 440 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 441 | t.Fatal(err) |
| 442 | } |
| 443 | |
| 444 | // Remove read (but keep search) on the parent, then restore it before the |
| 445 | // t.TempDir() cleanup so RemoveAll can recurse back in. |
| 446 | if err := os.Chmod(parent, 0o111); err != nil { |
| 447 | t.Fatal(err) |
| 448 | } |
| 449 | t.Cleanup(func() { os.Chmod(parent, 0o755) }) |
| 450 | |
| 451 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 452 | |
| 453 | // --gokr.dont_restrict: see TestSenderNamedSourceParentUnreadable. |
| 454 | rsynctest.Run(t, "gokr-rsync", "-aH", "--gokr.dont_restrict", hello, "rsync://localhost:"+srv.Port+"/interop/") |
| 455 | |
| 456 | want := []byte("world") |
| 457 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 458 | if err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | if diff := cmp.Diff(want, got); diff != "" { |
| 462 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | func TestSenderTraversal(t *testing.T) { |
| 467 | tmp := t.TempDir() |
| 468 | source := filepath.Join(tmp, "source") |
| 469 | dest := filepath.Join(tmp, "dest") |
| 470 | |
| 471 | if err := os.MkdirAll(source, 0755); err != nil { |
| 472 | t.Fatal(err) |
| 473 | } |
| 474 | if err := os.WriteFile(filepath.Join(source, "hello.txt"), []byte("hi"), 0644); err != nil { |
| 475 | t.Fatal(err) |
| 476 | } |
| 477 | if err := os.WriteFile(filepath.Join(tmp, "passwd"), []byte("secret"), 0644); err != nil { |
| 478 | t.Fatal(err) |
| 479 | } |
| 480 | |
| 481 | if err := os.MkdirAll(dest, 0755); err != nil { |
| 482 | t.Fatal(err) |
| 483 | } |
| 484 | |
| 485 | // start a server to sync from |
| 486 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 487 | |
| 488 | args := []string{ |
| 489 | "gokr-rsync", |
| 490 | "-aH", |
| 491 | "rsync://localhost:" + srv.Port + "/interop/../", |
| 492 | dest + "/", |
| 493 | } |
| 494 | rsynctest.Run(t, args...) |
| 495 | |
| 496 | passwd := filepath.Join(dest, "passwd") |
| 497 | |
| 498 | got, err := os.ReadFile(passwd) |
| 499 | if err == nil { |
| 500 | t.Fatalf("unexpectedly synced /etc/passwd: %s", string(got)) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // like TestSender, but both source and dest are local directories |
| 505 | func TestSenderBothLocal(t *testing.T) { |
| 506 | t.Parallel() |
| 507 | |
| 508 | tmp := t.TempDir() |
| 509 | source := filepath.Join(tmp, "source") |
| 510 | dest := filepath.Join(tmp, "dest") |
| 511 | |
| 512 | if err := os.MkdirAll(source, 0755); err != nil { |
| 513 | t.Fatal(err) |
| 514 | } |
| 515 | hello := filepath.Join(source, "hello") |
| 516 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 517 | t.Fatal(err) |
| 518 | } |
| 519 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 520 | if err != nil { |
| 521 | t.Fatal(err) |
| 522 | } |
| 523 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 524 | t.Fatal(err) |
| 525 | } |
| 526 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 527 | t.Fatal(err) |
| 528 | } |
| 529 | |
| 530 | if err := os.Symlink("hello", filepath.Join(source, "hey")); err != nil { |
| 531 | t.Fatal(err) |
| 532 | } |
| 533 | |
| 534 | no := filepath.Join(source, "no") |
| 535 | if err := os.WriteFile(no, []byte("no"), 0666); err != nil { |
| 536 | t.Fatal(err) |
| 537 | } |
| 538 | uid, gid, verifyUid := setUid(t, no) |
| 539 | |
| 540 | devices := filepath.Join(source, "devices") |
| 541 | if os.Getuid() == 0 { |
| 542 | rsynctest.CreateDummyDeviceFiles(t, devices) |
| 543 | } |
| 544 | |
| 545 | args := []string{ |
| 546 | "gokr-rsync", |
| 547 | "-aH", |
| 548 | source, |
| 549 | dest, |
| 550 | } |
| 551 | firstStats := rsynctest.Run(t, args...) |
| 552 | |
| 553 | dest = filepath.Join(dest, "source") |
| 554 | |
| 555 | { |
| 556 | want := []byte("world") |
| 557 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 558 | if err != nil { |
| 559 | t.Fatal(err) |
| 560 | } |
| 561 | if diff := cmp.Diff(want, got); diff != "" { |
| 562 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 563 | } |
| 564 | } |
| 565 | { |
| 566 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 567 | if err != nil { |
| 568 | t.Fatal(err) |
| 569 | } |
| 570 | want := "hello" |
| 571 | if got != want { |
| 572 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 573 | } |
| 574 | } |
| 575 | if verifyUid { |
| 576 | st, err := os.Stat(filepath.Join(dest, "no")) |
| 577 | if err != nil { |
| 578 | t.Fatal(err) |
| 579 | } |
| 580 | gotUID, gotGID := rsynctest.StatUidGid(t, st) |
| 581 | if got, want := gotUID, uid; got != want { |
| 582 | t.Errorf("unexpected uid: got %d, want %d", got, want) |
| 583 | } |
| 584 | if got, want := gotGID, gid; got != want { |
| 585 | t.Errorf("unexpected gid: got %d, want %d", got, want) |
| 586 | } |
| 587 | } |
| 588 | if os.Getuid() == 0 { |
| 589 | rsynctest.VerifyDummyDeviceFiles(t, devices, filepath.Join(dest, "devices")) |
| 590 | } |
| 591 | |
| 592 | incrementalStats := rsynctest.Run(t, args...) |
| 593 | if incrementalStats.Written >= firstStats.Written { |
| 594 | t.Fatalf("incremental run unexpectedly not more efficient than first run: incremental wrote %d bytes, first wrote %d bytes", incrementalStats.Written, firstStats.Written) |
| 595 | } |
| 596 | |
| 597 | // Make a change that is invisible with our current settings: |
| 598 | // change the file contents without changing size and mtime. |
| 599 | if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil { |
| 600 | t.Fatal(err) |
| 601 | } |
| 602 | if err := os.Chtimes(hello, mtime, mtime); err != nil { |
| 603 | t.Fatal(err) |
| 604 | } |
| 605 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 606 | t.Fatal(err) |
| 607 | } |
| 608 | // Replace the dest symlink to see if it will be restored |
| 609 | rsynctest.ReplaceSymlink(t, "wrong", filepath.Join(dest, "hey")) |
| 610 | |
| 611 | rsynctest.Run(t, args...) |
| 612 | |
| 613 | { |
| 614 | want := []byte("world") |
| 615 | got, err := os.ReadFile(filepath.Join(dest, "hello")) |
| 616 | if err != nil { |
| 617 | t.Fatal(err) |
| 618 | } |
| 619 | if diff := cmp.Diff(want, got); diff != "" { |
| 620 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 621 | } |
| 622 | } |
| 623 | { |
| 624 | got, err := os.Readlink(filepath.Join(dest, "hey")) |
| 625 | if err != nil { |
| 626 | t.Fatal(err) |
| 627 | } |
| 628 | want := "hello" |
| 629 | if got != want { |
| 630 | t.Fatalf("unexpected link target: got %q, want %q", got, want) |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // like TestSender, but the source is a single regular file, not a directory |
| 636 | func TestSenderBothLocalFile(t *testing.T) { |
| 637 | t.Parallel() |
| 638 | |
| 639 | tmp := t.TempDir() |
| 640 | source := filepath.Join(tmp, "source") |
| 641 | dest := filepath.Join(tmp, "dest") |
| 642 | |
| 643 | if err := os.WriteFile(source, []byte("hey"), 0644); err != nil { |
| 644 | t.Fatal(err) |
| 645 | } |
| 646 | mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z") |
| 647 | if err != nil { |
| 648 | t.Fatal(err) |
| 649 | } |
| 650 | if err := os.Chtimes(source, mtime, mtime); err != nil { |
| 651 | t.Fatal(err) |
| 652 | } |
| 653 | |
| 654 | args := []string{ |
| 655 | "gokr-rsync", |
| 656 | "-avH", |
| 657 | source, |
| 658 | dest, |
| 659 | } |
| 660 | rsynctest.Run(t, args...) |
| 661 | |
| 662 | { |
| 663 | want := []byte("hey") |
| 664 | got, err := os.ReadFile(filepath.Join(dest, "source")) |
| 665 | if err != nil { |
| 666 | t.Fatal(err) |
| 667 | } |
| 668 | if diff := cmp.Diff(want, got); diff != "" { |
| 669 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // like TestSenderBothLocalFile, but with an invocation that once caused a hang |
| 675 | // (see issue #43). |
| 676 | func TestSenderBothLocalHang(t *testing.T) { |
| 677 | t.Parallel() |
| 678 | |
| 679 | tmp := t.TempDir() |
| 680 | source := filepath.Join(tmp, "source") |
| 681 | dest := filepath.Join(tmp, "dest") |
| 682 | |
| 683 | if err := os.MkdirAll(source, 0755); err != nil { |
| 684 | t.Fatal(err) |
| 685 | } |
| 686 | |
| 687 | hello := filepath.Join(source, "hello.txt") |
| 688 | |
| 689 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 690 | t.Fatal(err) |
| 691 | } |
| 692 | |
| 693 | args := []string{ |
| 694 | "gokr-rsync", |
| 695 | "-rv", |
| 696 | source, |
| 697 | dest, |
| 698 | } |
| 699 | rsynctest.Run(t, args...) |
| 700 | |
| 701 | { |
| 702 | want := []byte("world") |
| 703 | got, err := os.ReadFile(filepath.Join(dest, "source", "hello.txt")) |
| 704 | if err != nil { |
| 705 | t.Fatal(err) |
| 706 | } |
| 707 | if diff := cmp.Diff(want, got); diff != "" { |
| 708 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // like TestSenderBothLocalFile, but with an invocation that used to fail |
| 714 | // with error message "file has changed mid-transfer" (issue #53). |
| 715 | func TestSenderPartial257K(t *testing.T) { |
| 716 | t.Parallel() |
| 717 | |
| 718 | tmp := t.TempDir() |
| 719 | source := filepath.Join(tmp, "source") |
| 720 | dest := filepath.Join(tmp, "dest") |
| 721 | want := make([]byte, 1024*257+1) |
| 722 | |
| 723 | if err := os.MkdirAll(source, 0755); err != nil { |
| 724 | t.Fatal(err) |
| 725 | } |
| 726 | |
| 727 | hello := filepath.Join(source, "hello.txt") |
| 728 | |
| 729 | if err := os.WriteFile(hello, want, 0644); err != nil { |
| 730 | t.Fatal(err) |
| 731 | } |
| 732 | |
| 733 | destFile := filepath.Join(dest, "source", "hello.txt") |
| 734 | if err := os.MkdirAll(filepath.Dir(destFile), 0755); err != nil { |
| 735 | t.Fatal(err) |
| 736 | } |
| 737 | if err := os.WriteFile(destFile, []byte{0}, 0644); err != nil { |
| 738 | t.Fatal(err) |
| 739 | } |
| 740 | |
| 741 | args := []string{ |
| 742 | "gokr-rsync", |
| 743 | "-rv", |
| 744 | source, |
| 745 | dest, |
| 746 | } |
| 747 | rsynctest.Run(t, args...) |
| 748 | |
| 749 | { |
| 750 | got, err := os.ReadFile(destFile) |
| 751 | if err != nil { |
| 752 | t.Fatal(err) |
| 753 | } |
| 754 | if diff := cmp.Diff(want, got); diff != "" { |
| 755 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | func TestReceiverCommandDryRun(t *testing.T) { |
| 761 | t.Parallel() |
| 762 | |
| 763 | if runtime.GOOS == "windows" { |
| 764 | t.Skip("stdin not supported on Windows") |
| 765 | } |
| 766 | |
| 767 | tmp := t.TempDir() |
| 768 | source := filepath.Join(tmp, "source") |
| 769 | dest := filepath.Join(tmp, "dest") |
| 770 | |
| 771 | if err := os.MkdirAll(source, 0755); err != nil { |
| 772 | t.Fatal(err) |
| 773 | } |
| 774 | hello := filepath.Join(source, "hello") |
| 775 | if err := os.WriteFile(hello, []byte("world"), 0644); err != nil { |
| 776 | t.Fatal(err) |
| 777 | } |
| 778 | if err := os.MkdirAll(dest, 0755); err != nil { |
| 779 | t.Fatal(err) |
| 780 | } |
| 781 | hello = filepath.Join(dest, "hello") |
| 782 | if err := os.WriteFile(hello, []byte("moon"), 0644); err != nil { |
| 783 | t.Fatal(err) |
| 784 | } |
| 785 | |
| 786 | exe, err := os.Executable() |
| 787 | if err != nil { |
| 788 | t.Fatal(err) |
| 789 | } |
| 790 | var buf bytes.Buffer |
| 791 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 792 | "--dry-run", |
| 793 | "-e", `"`+exe+`"`, |
| 794 | "-a", |
| 795 | filepath.Base(source)+"/", |
| 796 | "localhost:"+dest+"/") |
| 797 | rsync.Dir = filepath.Dir(source) |
| 798 | rsync.Stdout = &buf |
| 799 | rsync.Stderr = testlogger.New(t) |
| 800 | t.Logf("%v", rsync.Args) |
| 801 | if err := rsync.Run(); err != nil { |
| 802 | t.Fatalf("rsync error, output:\n%s", buf.String()) |
| 803 | } |
| 804 | } |