| 1 | package errors_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 12 | "github.com/google/go-cmp/cmp" |
| 13 | ) |
| 14 | |
| 15 | func TestErrors(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | |
| 18 | tmp := t.TempDir() |
| 19 | |
| 20 | dest := filepath.Join(tmp, "dest") |
| 21 | |
| 22 | // We configure an rsync module with a non-existant path to trigger an |
| 23 | // error. Removing read permission from a file is not sufficient because |
| 24 | // that does not actually trigger an error! See TestNoReadPermission. |
| 25 | nonExistant := filepath.Join(tmp, "non/existant") |
| 26 | |
| 27 | // start a server to sync from |
| 28 | srv := rsynctest.New(t, rsynctest.InteropModule(nonExistant)) |
| 29 | |
| 30 | // sync into dest dir |
| 31 | var buf bytes.Buffer |
| 32 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 33 | // "--debug=all4", |
| 34 | "--archive", |
| 35 | "-v", "-v", "-v", "-v", |
| 36 | "--port="+srv.Port, |
| 37 | "rsync://localhost/interop/", // copy contents of interop |
| 38 | //source+"/", // sync from local directory |
| 39 | filepath.Base(dest)) // directly into dest (relative to rsync.Dir) |
| 40 | rsync.Dir = filepath.Dir(dest) |
| 41 | rsync.Stdout = &buf |
| 42 | rsync.Stderr = &buf |
| 43 | if err := rsync.Run(); err == nil { |
| 44 | t.Fatalf("rsync unexpectedly did not return with an error exit code, output:\n%s", buf.String()) |
| 45 | } |
| 46 | |
| 47 | output := buf.String() |
| 48 | t.Logf("output:\n%s\n(end of output)", output) |
| 49 | if want := "module path is not accessible"; !strings.Contains(output, want) { |
| 50 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestNoSuchModule(t *testing.T) { |
| 55 | t.Parallel() |
| 56 | |
| 57 | tmp := t.TempDir() |
| 58 | |
| 59 | dest := filepath.Join(tmp, "dest") |
| 60 | |
| 61 | // start a server to sync from |
| 62 | srv := rsynctest.New(t, nil) |
| 63 | |
| 64 | // sync into dest dir |
| 65 | var buf bytes.Buffer |
| 66 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 67 | // "--debug=all4", |
| 68 | "--archive", |
| 69 | "-v", "-v", "-v", "-v", |
| 70 | "--port="+srv.Port, |
| 71 | "rsync://localhost/requesting-nonsense/", // copy contents of interop |
| 72 | //source+"/", // sync from local directory |
| 73 | filepath.Base(dest)) // directly into dest (relative to rsync.Dir) |
| 74 | rsync.Dir = filepath.Dir(dest) |
| 75 | rsync.Stdout = &buf |
| 76 | rsync.Stderr = &buf |
| 77 | if err := rsync.Run(); err == nil { |
| 78 | t.Fatalf("rsync unexpectedly did not return with an error exit code, output:\n%s", buf.String()) |
| 79 | } |
| 80 | |
| 81 | output := buf.String() |
| 82 | if want := "Unknown module"; !strings.Contains(output, want) { |
| 83 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestNoReadPermission(t *testing.T) { |
| 88 | t.Parallel() |
| 89 | |
| 90 | tmp := t.TempDir() |
| 91 | source := filepath.Join(tmp, "source") |
| 92 | dest := filepath.Join(tmp, "dest") |
| 93 | |
| 94 | // create files in source to be copied |
| 95 | if err := os.MkdirAll(source, 0755); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | dummy := filepath.Join(source, "dummy") |
| 99 | if err := os.WriteFile(dummy, []byte("dummy"), 0644); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | other := filepath.Join(source, "other") |
| 103 | want := []byte("other file contents") |
| 104 | if err := os.WriteFile(other, want, 0644); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | |
| 108 | // Remove read permission to trigger an error for one of the requested files. |
| 109 | if err := os.Chmod(dummy, 0); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | |
| 113 | // start a server to sync from |
| 114 | srv := rsynctest.New(t, rsynctest.InteropModule(source)) |
| 115 | |
| 116 | // sync into dest dir |
| 117 | var buf bytes.Buffer |
| 118 | rsync := exec.Command(rsynctest.AnyRsync(t), |
| 119 | // "--debug=all4", |
| 120 | "--archive", |
| 121 | "-v", "-v", "-v", "-v", |
| 122 | "--port="+srv.Port, |
| 123 | "rsync://localhost/interop/", // copy contents of interop |
| 124 | filepath.Base(dest)) // directly into dest (relative to rsync.Dir) |
| 125 | rsync.Dir = filepath.Dir(dest) |
| 126 | rsync.Stdout = &buf |
| 127 | rsync.Stderr = &buf |
| 128 | if err := rsync.Run(); err != nil { |
| 129 | t.Fatalf("%v: %v", rsync.Args, err) |
| 130 | } |
| 131 | |
| 132 | if os.Getuid() > 0 { |
| 133 | // uid 0 can read the file despite chmod(0), so skip this check: |
| 134 | |
| 135 | if _, err := os.ReadFile(filepath.Join(dest, "dummy")); err == nil { |
| 136 | t.Fatalf("dummy file unexpectedly created in the destination") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | got, err := os.ReadFile(filepath.Join(dest, "other")) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | if diff := cmp.Diff(want, got); diff != "" { |
| 145 | t.Fatalf("unexpected file contents: diff (-want +got):\n%s", diff) |
| 146 | } |
| 147 | } |