| 1 | package receiver_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/gokrazy/rsync/internal/testlogger" |
| 13 | ) |
| 14 | |
| 15 | func TestDaemonReceiverSync(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | |
| 18 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 19 | |
| 20 | tmp := t.TempDir() |
| 21 | source := filepath.Join(tmp, "source") |
| 22 | dest := filepath.Join(tmp, "dest") |
| 23 | destLarge := filepath.Join(dest, "large-data-file") |
| 24 | |
| 25 | headPattern := []byte{0x11} |
| 26 | bodyPattern := []byte{0xbb} |
| 27 | endPattern := []byte{0xee} |
| 28 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 29 | |
| 30 | // start a server which receives data |
| 31 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 32 | |
| 33 | rsync := exec.Command(rsyncBin, |
| 34 | // "--debug=all4", |
| 35 | "--archive", |
| 36 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 37 | // will start listing individual chunk matches. |
| 38 | "-v", "-v", "-v", // "-v", |
| 39 | "--port="+srv.Port, |
| 40 | filepath.Base(source)+"/", // copy contents of source (relative to rsync.Dir) |
| 41 | "rsync://localhost/interop/") |
| 42 | rsync.Dir = filepath.Dir(source) |
| 43 | rsync.Env = append(os.Environ(), |
| 44 | // Ensure rsync does not localize decimal separators and fractional |
| 45 | // points based on the current locale: |
| 46 | "LANG=C.UTF-8") |
| 47 | rsync.Stdout = testlogger.New(t) |
| 48 | rsync.Stderr = testlogger.New(t) |
| 49 | if err := rsync.Run(); err != nil { |
| 50 | t.Fatalf("%v: %v", rsync.Args, err) |
| 51 | } |
| 52 | |
| 53 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // like TestDaemonReceiverSync, but specifying a destination path that is a subdirectory |
| 59 | // within the module. |
| 60 | func TestDaemonReceiverSyncSubdir(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | |
| 63 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 64 | |
| 65 | tests := []struct { |
| 66 | name string |
| 67 | destPath string |
| 68 | rsyncTarget string |
| 69 | }{ |
| 70 | { |
| 71 | name: "simple subdir", |
| 72 | destPath: "destsubdir", |
| 73 | rsyncTarget: "rsync://localhost/interop/destsubdir/", |
| 74 | }, |
| 75 | { |
| 76 | name: "nested subdir", |
| 77 | destPath: "subdir/destsubdir", |
| 78 | rsyncTarget: "rsync://localhost/interop/subdir/destsubdir/", |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | for _, tt := range tests { |
| 83 | t.Run(tt.name, func(t *testing.T) { |
| 84 | t.Parallel() |
| 85 | |
| 86 | tmp := t.TempDir() |
| 87 | source := filepath.Join(tmp, "source") |
| 88 | dest := filepath.Join(tmp, "dest") |
| 89 | destLarge := filepath.Join(dest, tt.destPath, "large-data-file") |
| 90 | |
| 91 | headPattern := []byte{0x11} |
| 92 | bodyPattern := []byte{0xbb} |
| 93 | endPattern := []byte{0xee} |
| 94 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 95 | |
| 96 | // start a server which receives data |
| 97 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 98 | |
| 99 | rsync := exec.Command(rsyncBin, |
| 100 | // "--debug=all4", |
| 101 | "--archive", |
| 102 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 103 | // will start listing individual chunk matches. |
| 104 | "-v", "-v", "-v", // "-v", |
| 105 | "--port="+srv.Port, |
| 106 | filepath.Base(source)+"/", // copy contents of source (relative to rsync.Dir) |
| 107 | tt.rsyncTarget) |
| 108 | rsync.Dir = filepath.Dir(source) |
| 109 | rsync.Env = append(os.Environ(), |
| 110 | // Ensure rsync does not localize decimal separators and fractional |
| 111 | // points based on the current locale: |
| 112 | "LANG=C.UTF-8") |
| 113 | rsync.Stdout = testlogger.New(t) |
| 114 | rsync.Stderr = testlogger.New(t) |
| 115 | if err := rsync.Run(); err != nil { |
| 116 | t.Fatalf("%v: %v", rsync.Args, err) |
| 117 | } |
| 118 | |
| 119 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // like TestDaemonReceiverSync, but specifying a destination path that is a subdirectory |
| 127 | // within the module. |
| 128 | func TestDaemonReceiverSyncSubdirTraversal(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | |
| 131 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 132 | |
| 133 | tests := []struct { |
| 134 | name string |
| 135 | destPath string |
| 136 | rsyncTarget string |
| 137 | }{ |
| 138 | { |
| 139 | name: "simple subdir", |
| 140 | destPath: "destsubdir", |
| 141 | rsyncTarget: "rsync://localhost/interop/../", |
| 142 | }, |
| 143 | { |
| 144 | name: "nested subdir", |
| 145 | destPath: "subdir/destsubdir", |
| 146 | rsyncTarget: "rsync://localhost/interop/subdir/../../", |
| 147 | }, |
| 148 | } |
| 149 | |
| 150 | for _, tt := range tests { |
| 151 | t.Run(tt.name, func(t *testing.T) { |
| 152 | t.Parallel() |
| 153 | |
| 154 | tmp := t.TempDir() |
| 155 | source := filepath.Join(tmp, "source") |
| 156 | dest := filepath.Join(tmp, "dest") |
| 157 | destLarge := filepath.Join(dest, tt.destPath, "large-data-file") |
| 158 | |
| 159 | headPattern := []byte{0x11} |
| 160 | bodyPattern := []byte{0xbb} |
| 161 | endPattern := []byte{0xee} |
| 162 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 163 | |
| 164 | // start a server which receives data |
| 165 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 166 | |
| 167 | rsync := exec.Command(rsyncBin, |
| 168 | // "--debug=all4", |
| 169 | "--archive", |
| 170 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 171 | // will start listing individual chunk matches. |
| 172 | "-v", "-v", "-v", // "-v", |
| 173 | "--port="+srv.Port, |
| 174 | filepath.Base(source)+"/", // copy contents of source (relative to rsync.Dir) |
| 175 | tt.rsyncTarget) |
| 176 | rsync.Dir = filepath.Dir(source) |
| 177 | rsync.Env = append(os.Environ(), |
| 178 | // Ensure rsync does not localize decimal separators and fractional |
| 179 | // points based on the current locale: |
| 180 | "LANG=C.UTF-8") |
| 181 | var buf bytes.Buffer |
| 182 | rsync.Stdout = &buf |
| 183 | rsync.Stderr = &buf |
| 184 | if err := rsync.Run(); err != nil { |
| 185 | if strings.Contains(buf.String(), "path escapes from parent") { |
| 186 | return |
| 187 | } |
| 188 | t.Fatalf("%v: %v", rsync.Args, err) |
| 189 | } |
| 190 | |
| 191 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | }) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func TestDaemonReceiverDelete(t *testing.T) { |
| 199 | t.Parallel() |
| 200 | |
| 201 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 202 | |
| 203 | tmp := t.TempDir() |
| 204 | source := filepath.Join(tmp, "source") |
| 205 | dest := filepath.Join(tmp, "dest") |
| 206 | destLarge := filepath.Join(dest, "large-data-file") |
| 207 | |
| 208 | headPattern := []byte{0x11} |
| 209 | bodyPattern := []byte{0xbb} |
| 210 | endPattern := []byte{0xee} |
| 211 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 212 | |
| 213 | // start a server which receives data |
| 214 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 215 | |
| 216 | run := func() { |
| 217 | rsync := exec.Command(rsyncBin, |
| 218 | // "--debug=all4", |
| 219 | "--archive", |
| 220 | "--delete", |
| 221 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 222 | // will start listing individual chunk matches. |
| 223 | "-v", "-v", "-v", // "-v", |
| 224 | "--port="+srv.Port, |
| 225 | filepath.Base(source)+"/", // copy contents of source (relative to rsync.Dir) |
| 226 | "rsync://localhost/interop/") |
| 227 | rsync.Dir = filepath.Dir(source) |
| 228 | rsync.Env = append(os.Environ(), |
| 229 | // Ensure rsync does not localize decimal separators and fractional |
| 230 | // points based on the current locale: |
| 231 | "LANG=C.UTF-8") |
| 232 | rsync.Stdout = testlogger.New(t) |
| 233 | rsync.Stderr = testlogger.New(t) |
| 234 | if err := rsync.Run(); err != nil { |
| 235 | t.Fatalf("%v: %v", rsync.Args, err) |
| 236 | } |
| 237 | } |
| 238 | run() |
| 239 | |
| 240 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | |
| 244 | // Add more files to the destination, which should be deleted: |
| 245 | extra := filepath.Join(dest, "extrafile") |
| 246 | if err := os.WriteFile(extra, []byte("deleteme"), 0644); err != nil { |
| 247 | t.Fatal(err) |
| 248 | } |
| 249 | run() |
| 250 | if _, err := os.Stat(extra); !os.IsNotExist(err) { |
| 251 | t.Errorf("expected %s to be deleted, but it still exists", extra) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | func TestDaemonReceiverSyncHardLinks(t *testing.T) { |
| 256 | t.Parallel() |
| 257 | |
| 258 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 259 | |
| 260 | tmp := t.TempDir() |
| 261 | source := filepath.Join(tmp, "source") |
| 262 | dest := filepath.Join(tmp, "dest") |
| 263 | destLarge := filepath.Join(dest, "large-data-file") |
| 264 | |
| 265 | headPattern := []byte{0x11} |
| 266 | bodyPattern := []byte{0xbb} |
| 267 | endPattern := []byte{0xee} |
| 268 | rsynctest.WriteLargeDataFile(t, source, headPattern, bodyPattern, endPattern) |
| 269 | |
| 270 | // start a server which receives data |
| 271 | srv := rsynctest.New(t, rsynctest.WritableInteropModule(dest)) |
| 272 | |
| 273 | rsync := exec.Command(rsyncBin, |
| 274 | // "--debug=all4", |
| 275 | "--archive", |
| 276 | "--hard-links", |
| 277 | // A verbosity level of 3 is enough, any higher than that and rsync |
| 278 | // will start listing individual chunk matches. |
| 279 | "-v", "-v", "-v", // "-v", |
| 280 | "--port="+srv.Port, |
| 281 | filepath.Base(source)+"/", // copy contents of source (relative to rsync.Dir) |
| 282 | "rsync://localhost/interop/") |
| 283 | rsync.Dir = filepath.Dir(source) |
| 284 | rsync.Env = append(os.Environ(), |
| 285 | // Ensure rsync does not localize decimal separators and fractional |
| 286 | // points based on the current locale: |
| 287 | "LANG=C.UTF-8") |
| 288 | var buf bytes.Buffer |
| 289 | rsync.Stdout = &buf |
| 290 | rsync.Stderr = &buf |
| 291 | if err := rsync.Run(); err != nil { |
| 292 | if strings.Contains(buf.String(), "gokr-rsync [receiver]: support for hard links not yet implemented") { |
| 293 | return |
| 294 | } |
| 295 | t.Fatalf("%v: %v", rsync.Args, err) |
| 296 | } |
| 297 | |
| 298 | if err := rsynctest.DataFileMatches(destLarge, headPattern, bodyPattern, endPattern); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | } |