| 1 | #!/bin/sh |
| 2 | |
| 3 | # NOTES |
| 4 | # ===== |
| 5 | # |
| 6 | # 1. You will come across - at first glance weird - invocations of `touch |
| 7 | # -d` in this script. These are necessitated by the fact that rsync 3.4.1 |
| 8 | # does not seem to pick up sub-second diffs in mtime when comparing the |
| 9 | # source and target when called with the `-t` flag. By setting the mtime to |
| 10 | # more than a second in difference explicitly, we bypass this [unexpected |
| 11 | # rsync behavior][0]. |
| 12 | # |
| 13 | # [0]: https://github.com/RsyncProject/rsync/issues/785 |
| 14 | # |
| 15 | # 2. With a set REMOTE environment variable, this script generates a |
| 16 | # REMOTE_PATH variable for every test. This can be used to run rsync over an |
| 17 | # ssh connection. If configured for localhost, the target files will appear |
| 18 | # on the same machine such that diffoscope can compare them. This mechanism |
| 19 | # is used when local-tests is executed in the remote-tests script. |
| 20 | |
| 21 | set -u |
| 22 | # set -e # commented out because of https://github.com/kward/shunit2/issues/174 |
| 23 | |
| 24 | DIFF_CMD=${DIFF_CMD:-"diffoscope --no-progress"} |
| 25 | RSYNC_BIN=${RSYNC_BIN:-"/usr/bin/rsync"} |
| 26 | |
| 27 | setUp() { |
| 28 | TEST_DIR="$SHUNIT_TMPDIR"/$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c10) |
| 29 | mkdir -p "$TEST_DIR" |
| 30 | cd "$TEST_DIR" || exit 1 # lacking -e, handle https://www.shellcheck.net/wiki/SC2164 |
| 31 | TARGET_DIR="${REMOTE:-}""${REMOTE:+:}""$(pwd)" |
| 32 | } |
| 33 | |
| 34 | tearDown() { |
| 35 | rm -rf ../"$TEST_DIR" |
| 36 | } |
| 37 | |
| 38 | testSimpleFileCopy() { |
| 39 | echo foo > src |
| 40 | |
| 41 | $RSYNC_BIN src "$TARGET_DIR"/target |
| 42 | |
| 43 | $DIFF_CMD src target |
| 44 | assertTrue "Failed simple file copy" "$?" |
| 45 | } |
| 46 | |
| 47 | testSimpleFileCopyWithPermAndMtimePreservation() { |
| 48 | echo foo > src |
| 49 | chmod 666 src |
| 50 | |
| 51 | $RSYNC_BIN --times --perms src "$TARGET_DIR"/target |
| 52 | |
| 53 | $DIFF_CMD --exclude-directory-metadata=no src target |
| 54 | assertTrue "Failed file copy with mtime and perm preservation" $? |
| 55 | } |
| 56 | |
| 57 | testMtimeSyncForFileIfTargetNewer() { |
| 58 | echo foo > src |
| 59 | cp src target |
| 60 | touch -d "next second" target |
| 61 | |
| 62 | $RSYNC_BIN --times src "$TARGET_DIR"/target |
| 63 | |
| 64 | $DIFF_CMD --exclude-directory-metadata=no src target |
| 65 | assertTrue "Failed mtime sync for newer target" $? |
| 66 | } |
| 67 | |
| 68 | testMtimeSyncForFileIfTargetNewerUpdate(){ |
| 69 | echo foo > src |
| 70 | cp src target |
| 71 | touch -d "next minute" target |
| 72 | |
| 73 | $RSYNC_BIN --times --update src "$TARGET_DIR"/target |
| 74 | |
| 75 | $DIFF_CMD --exclude-directory-metadata=no src target > /dev/null 2>&1 |
| 76 | status=$? |
| 77 | assertFalse 'Failed mtime sync for newer target with -u flag' $status |
| 78 | } |
| 79 | |
| 80 | testChecksumFileComparison() { |
| 81 | # this test handles a case described in |
| 82 | # |
| 83 | # https://rachelbythebay.com/w/2025/05/31/sync/ |
| 84 | # |
| 85 | # by default, rsync uses a metadata check on size and mtime to determine |
| 86 | # if there are diffs that need syncing. Now, it can happen, that the |
| 87 | # file contents change but size and mtime do not. In this case rsync |
| 88 | # will fail to pick up the diff and not sync the files, unless it is |
| 89 | # forced to compute the checksum of the files with -c. |
| 90 | |
| 91 | echo "ABCDEFGHIJKLMNOP" > src.txt |
| 92 | touch -d "@1750044444" src.txt |
| 93 | $RSYNC_BIN --times src.txt "$TARGET_DIR"/target.txt |
| 94 | sed -i 'y/ABC/XYZ/' src.txt |
| 95 | touch -d "@1750044444" src.txt |
| 96 | $RSYNC_BIN src.txt "$TARGET_DIR"/target.txt |
| 97 | |
| 98 | $DIFF_CMD src.txt target.txt > /dev/null 2>&1 |
| 99 | assertFalse "Rsync unexpectedly synced file of same size and mtime" "$?" |
| 100 | |
| 101 | $RSYNC_BIN --checksum src.txt "$TARGET_DIR"/target.txt |
| 102 | |
| 103 | $DIFF_CMD src.txt target.txt |
| 104 | assertTrue "Rsync failed checksum based sync" "$?" |
| 105 | } |
| 106 | |
| 107 | testAppendOnShorterFile() { |
| 108 | echo foo > src |
| 109 | cp src target |
| 110 | echo bar >> src |
| 111 | |
| 112 | $RSYNC_BIN --append src "$TARGET_DIR"/target |
| 113 | |
| 114 | $DIFF_CMD src target |
| 115 | assertTrue $? |
| 116 | } |
| 117 | |
| 118 | testAppendOnLargerFile() { |
| 119 | |
| 120 | echo foo > src |
| 121 | cp src target |
| 122 | echo bar >> src |
| 123 | echo barbar >> target |
| 124 | |
| 125 | $RSYNC_BIN --append src "$TARGET_DIR"/target |
| 126 | |
| 127 | $DIFF_CMD src target --text diff.txt |
| 128 | cat << EOF > ./expected_diff.txt |
| 129 | --- src |
| 130 | +++ target |
| 131 | @@ -1,2 +1,2 @@ |
| 132 | foo |
| 133 | -bar |
| 134 | +barbar |
| 135 | EOF |
| 136 | $DIFF_CMD expected_diff.txt diff.txt |
| 137 | assertTrue "Failed --append on larger target" $? |
| 138 | } |
| 139 | |
| 140 | testAppendWithDifferentHistory(){ |
| 141 | echo foo > src |
| 142 | echo foo > target |
| 143 | echo bar >> src |
| 144 | echo goo >> target |
| 145 | echo boo >> src |
| 146 | |
| 147 | $RSYNC_BIN --append src "$TARGET_DIR"/target |
| 148 | |
| 149 | $DIFF_CMD src target --text diff.txt |
| 150 | cat << EOF > ./expected_diff.txt |
| 151 | --- src |
| 152 | +++ target |
| 153 | @@ -1,3 +1,3 @@ |
| 154 | foo |
| 155 | -bar |
| 156 | +goo |
| 157 | boo |
| 158 | EOF |
| 159 | $DIFF_CMD expected_diff.txt diff.txt |
| 160 | assertTrue "Failed --append with different history" $? |
| 161 | } |
| 162 | |
| 163 | testAppendVerify(){ |
| 164 | printf "foo\nbar\nboo" > src |
| 165 | printf "foo\ngoo" > target |
| 166 | |
| 167 | $RSYNC_BIN --append-verify src "$TARGET_DIR"/target |
| 168 | |
| 169 | $DIFF_CMD src target |
| 170 | assertTrue "Did not unify history with --append-verify" $? |
| 171 | } |
| 172 | |
| 173 | testMkPath(){ |
| 174 | mkdir -p src/abc |
| 175 | touch src/abc/x.c |
| 176 | touch src/abc/y.c |
| 177 | touch src/main.py |
| 178 | |
| 179 | $RSYNC_BIN --mkpath src/abc/*.c "$TARGET_DIR"/target/src/abc/ |
| 180 | |
| 181 | $DIFF_CMD --exclude-directory-metadata=yes src target/src --text diff.txt |
| 182 | cat << EOF > ./expected_diff.txt |
| 183 | --- src |
| 184 | +++ target/src |
| 185 | ├── file list |
| 186 | │ @@ -1,2 +1 @@ |
| 187 | │ -abc |
| 188 | │ -main.py |
| 189 | │ +abc |
| 190 | EOF |
| 191 | $DIFF_CMD expected_diff.txt diff.txt |
| 192 | assertTrue "Failed creating dir path on destination" $? |
| 193 | } |
| 194 | |
| 195 | testSymlinkSync() { |
| 196 | |
| 197 | mkdir src |
| 198 | touch src/abc.c |
| 199 | touch file_outside_src.txt |
| 200 | cd src || exit 1 # lacking -e, handle https://www.shellcheck.net/wiki/SC2164 |
| 201 | ln -s abc.c link_to_abc |
| 202 | ln -s ../file_outside_src.txt link_to_file_outside |
| 203 | cd .. || exit 1 # lacking -e, handle https://www.shellcheck.net/wiki/SC2164 |
| 204 | mkdir target |
| 205 | |
| 206 | # sync without --links option |
| 207 | $RSYNC_BIN src/* "$TARGET_DIR"/target |
| 208 | |
| 209 | $DIFF_CMD --exclude-directory-metadata=yes src target --text diff.txt |
| 210 | cat << EOF > expected_diff.txt |
| 211 | --- src |
| 212 | +++ target |
| 213 | ├── file list |
| 214 | │ @@ -1,3 +1 @@ |
| 215 | │ -abc.c |
| 216 | │ -link_to_abc |
| 217 | │ -link_to_file_outside |
| 218 | │ +abc.c |
| 219 | EOF |
| 220 | $DIFF_CMD --exclude-directory-metadata=yes diff.txt expected_diff.txt |
| 221 | assertTrue "Transferred symlink without --links option" $? |
| 222 | |
| 223 | # sync with --safe-links option |
| 224 | $RSYNC_BIN --links --safe-links src/* "$TARGET_DIR"/target |
| 225 | |
| 226 | $DIFF_CMD --exclude-directory-metadata=yes src target --text diff.txt |
| 227 | cat << EOF > expected_diff.txt |
| 228 | --- src |
| 229 | +++ target |
| 230 | ├── file list |
| 231 | │ @@ -1,3 +1,2 @@ |
| 232 | │ abc.c |
| 233 | │ -link_to_abc |
| 234 | │ -link_to_file_outside |
| 235 | │ +link_to_abc |
| 236 | EOF |
| 237 | $DIFF_CMD --exclude-directory-metadata=yes diff.txt expected_diff.txt |
| 238 | assertTrue "Unexpected diff for --safe-links sync" $? |
| 239 | |
| 240 | # sync with links option |
| 241 | $RSYNC_BIN --links src/* "$TARGET_DIR"/target |
| 242 | |
| 243 | $DIFF_CMD --exclude-directory-metadata=yes src target |
| 244 | assertTrue "Unexpected diff for --links sync" $? |
| 245 | } |
| 246 | |
| 247 | testMultiLevelDirSyncWithTimesAndPerm() { |
| 248 | mkdir -p src/abc/ |
| 249 | echo "echo foo" > src/abc/one.sh |
| 250 | chmod u+x src/abc/one.sh |
| 251 | touch -d "last hour" src |
| 252 | touch -d "last minute" src/abc |
| 253 | |
| 254 | $RSYNC_BIN --recursive --times --perms src/ "$TARGET_DIR"/target |
| 255 | |
| 256 | $DIFF_CMD --exclude-directory-metadata=no src target |
| 257 | assertTrue $? |
| 258 | } |
| 259 | |
| 260 | testMultiLevelDirSyncWithDelete() { |
| 261 | mkdir -p src/abc/ |
| 262 | echo foo > src/abc/one.c |
| 263 | $RSYNC_BIN --recursive src/ "$TARGET_DIR"/target |
| 264 | echo foofoo > target/abc/two.c |
| 265 | |
| 266 | $RSYNC_BIN --recursive --del src/ "$TARGET_DIR"/target |
| 267 | |
| 268 | $DIFF_CMD --exclude-directory-metadata=yes src target |
| 269 | assertTrue $? |
| 270 | } |
| 271 | |
| 272 | testMultiLevelDirArchive() { |
| 273 | mkdir -p src/abc |
| 274 | mkdir -p src/xyz |
| 275 | echo "echo foo" > src/abc/one.sh |
| 276 | chmod u+x src/abc/one.sh |
| 277 | echo foofoofoo > src/xyz/three.c |
| 278 | touch -d "yesterday" src |
| 279 | touch -d "last minute" src/abc |
| 280 | touch -d "last hour" src/xyz |
| 281 | |
| 282 | $RSYNC_BIN --archive src/ "$TARGET_DIR"/target |
| 283 | |
| 284 | $DIFF_CMD --exclude-directory-metadata=no src target |
| 285 | assertTrue $? |
| 286 | } |
| 287 | |
| 288 | testDirSyncWithExcludePattern() { |
| 289 | mkdir -p src/reports |
| 290 | touch src/company.ledger |
| 291 | touch src/reports/2024.pdf |
| 292 | touch src/reports/2023.pdf |
| 293 | touch src/reports/2023.md |
| 294 | touch src/reports/2024.md |
| 295 | |
| 296 | $RSYNC_BIN --recursive --exclude="*.pdf" src/ "$TARGET_DIR"/target |
| 297 | $DIFF_CMD --exclude-directory-metadata=yes src target --text diff.txt |
| 298 | cat << EOF > expected_diff.txt |
| 299 | --- src |
| 300 | +++ target |
| 301 | │ --- src/reports |
| 302 | ├── +++ target/reports |
| 303 | │ ├── file list |
| 304 | │ │ @@ -1,4 +1,2 @@ |
| 305 | │ │ 2023.md |
| 306 | │ │ +2024.md |
| 307 | │ │ -2023.pdf |
| 308 | │ │ -2024.md |
| 309 | │ │ -2024.pdf |
| 310 | EOF |
| 311 | |
| 312 | $DIFF_CMD diff.txt expected_diff.txt |
| 313 | assertTrue "Failed --exclude test" $? |
| 314 | } |
| 315 | |
| 316 | # TODO: test case for extended filesystem attributes (xattr) |
| 317 | # TODO: test case for access control lists (acl) |
| 318 | # TODO: test case with (compressed) tarballs |
| 319 | # TODO: test case with target/ <- note trailing slash |
| 320 | |
| 321 | # (partially) requiring root rights: |
| 322 | # TODO: test case for --group |
| 323 | # TODO: test case for --owner |
| 324 | # TODO: test case for --devices |
| 325 | # TODO: test case for --specials |
| 326 | |
| 327 | # shellcheck disable=SC1091 |
| 328 | . shunit2 |