| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test protocol v2 server commands' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success 'setup to generate files with expected content' ' |
| 11 | printf "agent=git/%s" "$(git version | cut -d" " -f3)" >agent_capability && |
| 12 | |
| 13 | test_oid_cache <<-EOF && |
| 14 | wrong_algo sha1:sha256 |
| 15 | wrong_algo sha256:sha1 |
| 16 | EOF |
| 17 | |
| 18 | if test_have_prereq WINDOWS |
| 19 | then |
| 20 | printf "agent=FAKE\n" >agent_capability |
| 21 | else |
| 22 | printf -- "-%s\n" $(uname -s | test_redact_non_printables) >>agent_capability |
| 23 | fi && |
| 24 | cat >expect.base <<-EOF && |
| 25 | version 2 |
| 26 | $(cat agent_capability) |
| 27 | ls-refs=unborn |
| 28 | fetch=shallow wait-for-done |
| 29 | server-option |
| 30 | object-format=$(test_oid algo) |
| 31 | EOF |
| 32 | cat >expect.trailer <<-EOF |
| 33 | 0000 |
| 34 | EOF |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'test capability advertisement' ' |
| 38 | cat expect.base expect.trailer >expect && |
| 39 | |
| 40 | if test_have_prereq WINDOWS |
| 41 | then |
| 42 | GIT_USER_AGENT=FAKE && export GIT_USER_AGENT |
| 43 | fi && |
| 44 | GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \ |
| 45 | --advertise-capabilities >out && |
| 46 | test-tool pkt-line unpack <out >actual && |
| 47 | test_cmp expect actual |
| 48 | ' |
| 49 | |
| 50 | test_expect_success 'stateless-rpc flag does not list capabilities' ' |
| 51 | # Empty request |
| 52 | test-tool pkt-line pack >in <<-EOF && |
| 53 | 0000 |
| 54 | EOF |
| 55 | test-tool serve-v2 --stateless-rpc >out <in && |
| 56 | test_must_be_empty out && |
| 57 | |
| 58 | # EOF |
| 59 | test-tool serve-v2 --stateless-rpc >out && |
| 60 | test_must_be_empty out |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'request invalid capability' ' |
| 64 | test-tool pkt-line pack >in <<-EOF && |
| 65 | foobar |
| 66 | 0000 |
| 67 | EOF |
| 68 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 69 | test_grep "unknown capability" err |
| 70 | ' |
| 71 | |
| 72 | test_expect_success 'request with no command' ' |
| 73 | test-tool pkt-line pack >in <<-EOF && |
| 74 | agent=git/test |
| 75 | object-format=$(test_oid algo) |
| 76 | 0000 |
| 77 | EOF |
| 78 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 79 | test_grep "no command requested" err |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'request invalid command' ' |
| 83 | test-tool pkt-line pack >in <<-EOF && |
| 84 | command=foo |
| 85 | object-format=$(test_oid algo) |
| 86 | agent=git/test |
| 87 | 0000 |
| 88 | EOF |
| 89 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 90 | test_grep "invalid command" err |
| 91 | ' |
| 92 | |
| 93 | test_expect_success 'request capability as command' ' |
| 94 | test-tool pkt-line pack >in <<-EOF && |
| 95 | command=agent |
| 96 | object-format=$(test_oid algo) |
| 97 | 0000 |
| 98 | EOF |
| 99 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 100 | grep invalid.command.*agent err |
| 101 | ' |
| 102 | |
| 103 | test_expect_success 'request command as capability' ' |
| 104 | test-tool pkt-line pack >in <<-EOF && |
| 105 | command=ls-refs |
| 106 | object-format=$(test_oid algo) |
| 107 | fetch |
| 108 | 0000 |
| 109 | EOF |
| 110 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 111 | grep unknown.capability err |
| 112 | ' |
| 113 | |
| 114 | test_expect_success 'requested command is command=value' ' |
| 115 | test-tool pkt-line pack >in <<-EOF && |
| 116 | command=ls-refs=whatever |
| 117 | object-format=$(test_oid algo) |
| 118 | 0000 |
| 119 | EOF |
| 120 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 121 | grep invalid.command.*ls-refs=whatever err |
| 122 | ' |
| 123 | |
| 124 | test_expect_success 'wrong object-format' ' |
| 125 | test-tool pkt-line pack >in <<-EOF && |
| 126 | command=fetch |
| 127 | agent=git/test |
| 128 | object-format=$(test_oid wrong_algo) |
| 129 | 0000 |
| 130 | EOF |
| 131 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 132 | test_grep "mismatched object format" err |
| 133 | ' |
| 134 | |
| 135 | # Test the basics of ls-refs |
| 136 | # |
| 137 | test_expect_success 'setup some refs and tags' ' |
| 138 | test_commit one && |
| 139 | git branch dev main && |
| 140 | test_commit two && |
| 141 | git symbolic-ref refs/heads/release refs/heads/main && |
| 142 | git tag -a -m "annotated tag" annotated-tag |
| 143 | ' |
| 144 | |
| 145 | test_expect_success 'basics of ls-refs' ' |
| 146 | test-tool pkt-line pack >in <<-EOF && |
| 147 | command=ls-refs |
| 148 | object-format=$(test_oid algo) |
| 149 | 0000 |
| 150 | EOF |
| 151 | |
| 152 | cat >expect <<-EOF && |
| 153 | $(git rev-parse HEAD) HEAD |
| 154 | $(git rev-parse refs/heads/dev) refs/heads/dev |
| 155 | $(git rev-parse refs/heads/main) refs/heads/main |
| 156 | $(git rev-parse refs/heads/release) refs/heads/release |
| 157 | $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag |
| 158 | $(git rev-parse refs/tags/one) refs/tags/one |
| 159 | $(git rev-parse refs/tags/two) refs/tags/two |
| 160 | 0000 |
| 161 | EOF |
| 162 | |
| 163 | test-tool serve-v2 --stateless-rpc <in >out && |
| 164 | test-tool pkt-line unpack <out >actual && |
| 165 | test_cmp expect actual |
| 166 | ' |
| 167 | |
| 168 | test_expect_success 'ls-refs complains about unknown options' ' |
| 169 | test-tool pkt-line pack >in <<-EOF && |
| 170 | command=ls-refs |
| 171 | object-format=$(test_oid algo) |
| 172 | 0001 |
| 173 | no-such-arg |
| 174 | 0000 |
| 175 | EOF |
| 176 | |
| 177 | test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in && |
| 178 | grep unexpected.line.*no-such-arg err |
| 179 | ' |
| 180 | |
| 181 | test_expect_success 'basic ref-prefixes' ' |
| 182 | test-tool pkt-line pack >in <<-EOF && |
| 183 | command=ls-refs |
| 184 | object-format=$(test_oid algo) |
| 185 | 0001 |
| 186 | ref-prefix refs/heads/main |
| 187 | ref-prefix refs/tags/one |
| 188 | 0000 |
| 189 | EOF |
| 190 | |
| 191 | cat >expect <<-EOF && |
| 192 | $(git rev-parse refs/heads/main) refs/heads/main |
| 193 | $(git rev-parse refs/tags/one) refs/tags/one |
| 194 | 0000 |
| 195 | EOF |
| 196 | |
| 197 | test-tool serve-v2 --stateless-rpc <in >out && |
| 198 | test-tool pkt-line unpack <out >actual && |
| 199 | test_cmp expect actual |
| 200 | ' |
| 201 | |
| 202 | test_expect_success 'refs/heads prefix' ' |
| 203 | test-tool pkt-line pack >in <<-EOF && |
| 204 | command=ls-refs |
| 205 | object-format=$(test_oid algo) |
| 206 | 0001 |
| 207 | ref-prefix refs/heads/ |
| 208 | 0000 |
| 209 | EOF |
| 210 | |
| 211 | cat >expect <<-EOF && |
| 212 | $(git rev-parse refs/heads/dev) refs/heads/dev |
| 213 | $(git rev-parse refs/heads/main) refs/heads/main |
| 214 | $(git rev-parse refs/heads/release) refs/heads/release |
| 215 | 0000 |
| 216 | EOF |
| 217 | |
| 218 | test-tool serve-v2 --stateless-rpc <in >out && |
| 219 | test-tool pkt-line unpack <out >actual && |
| 220 | test_cmp expect actual |
| 221 | ' |
| 222 | |
| 223 | test_expect_success 'ignore very large set of prefixes' ' |
| 224 | # generate a large number of ref-prefixes that we expect |
| 225 | # to match nothing; the value here exceeds TOO_MANY_PREFIXES |
| 226 | # from ls-refs.c. |
| 227 | { |
| 228 | echo command=ls-refs && |
| 229 | echo object-format=$(test_oid algo) && |
| 230 | echo 0001 && |
| 231 | awk "{ |
| 232 | for (i = 1; i <= 65536; i++) |
| 233 | print \"ref-prefix refs/heads/\", \$i |
| 234 | }" && |
| 235 | echo 0000 |
| 236 | } | |
| 237 | test-tool pkt-line pack >in && |
| 238 | |
| 239 | # and then confirm that we see unmatched prefixes anyway (i.e., |
| 240 | # that the prefix was not applied). |
| 241 | cat >expect <<-EOF && |
| 242 | $(git rev-parse HEAD) HEAD |
| 243 | $(git rev-parse refs/heads/dev) refs/heads/dev |
| 244 | $(git rev-parse refs/heads/main) refs/heads/main |
| 245 | $(git rev-parse refs/heads/release) refs/heads/release |
| 246 | $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag |
| 247 | $(git rev-parse refs/tags/one) refs/tags/one |
| 248 | $(git rev-parse refs/tags/two) refs/tags/two |
| 249 | 0000 |
| 250 | EOF |
| 251 | |
| 252 | test-tool serve-v2 --stateless-rpc <in >out && |
| 253 | test-tool pkt-line unpack <out >actual && |
| 254 | test_cmp expect actual |
| 255 | ' |
| 256 | |
| 257 | test_expect_success 'peel parameter' ' |
| 258 | test-tool pkt-line pack >in <<-EOF && |
| 259 | command=ls-refs |
| 260 | object-format=$(test_oid algo) |
| 261 | 0001 |
| 262 | peel |
| 263 | ref-prefix refs/tags/ |
| 264 | 0000 |
| 265 | EOF |
| 266 | |
| 267 | cat >expect <<-EOF && |
| 268 | $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{}) |
| 269 | $(git rev-parse refs/tags/one) refs/tags/one |
| 270 | $(git rev-parse refs/tags/two) refs/tags/two |
| 271 | 0000 |
| 272 | EOF |
| 273 | |
| 274 | test-tool serve-v2 --stateless-rpc <in >out && |
| 275 | test-tool pkt-line unpack <out >actual && |
| 276 | test_cmp expect actual |
| 277 | ' |
| 278 | |
| 279 | test_expect_success 'symrefs parameter' ' |
| 280 | test-tool pkt-line pack >in <<-EOF && |
| 281 | command=ls-refs |
| 282 | object-format=$(test_oid algo) |
| 283 | 0001 |
| 284 | symrefs |
| 285 | ref-prefix refs/heads/ |
| 286 | 0000 |
| 287 | EOF |
| 288 | |
| 289 | cat >expect <<-EOF && |
| 290 | $(git rev-parse refs/heads/dev) refs/heads/dev |
| 291 | $(git rev-parse refs/heads/main) refs/heads/main |
| 292 | $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main |
| 293 | 0000 |
| 294 | EOF |
| 295 | |
| 296 | test-tool serve-v2 --stateless-rpc <in >out && |
| 297 | test-tool pkt-line unpack <out >actual && |
| 298 | test_cmp expect actual |
| 299 | ' |
| 300 | |
| 301 | test_expect_success 'sending server-options' ' |
| 302 | test-tool pkt-line pack >in <<-EOF && |
| 303 | command=ls-refs |
| 304 | object-format=$(test_oid algo) |
| 305 | server-option=hello |
| 306 | server-option=world |
| 307 | 0001 |
| 308 | ref-prefix HEAD |
| 309 | 0000 |
| 310 | EOF |
| 311 | |
| 312 | cat >expect <<-EOF && |
| 313 | $(git rev-parse HEAD) HEAD |
| 314 | 0000 |
| 315 | EOF |
| 316 | |
| 317 | test-tool serve-v2 --stateless-rpc <in >out && |
| 318 | test-tool pkt-line unpack <out >actual && |
| 319 | test_cmp expect actual |
| 320 | ' |
| 321 | |
| 322 | test_expect_success 'unexpected lines are not allowed in fetch request' ' |
| 323 | git init server && |
| 324 | |
| 325 | test-tool pkt-line pack >in <<-EOF && |
| 326 | command=fetch |
| 327 | object-format=$(test_oid algo) |
| 328 | 0001 |
| 329 | this-is-not-a-command |
| 330 | 0000 |
| 331 | EOF |
| 332 | |
| 333 | ( |
| 334 | cd server && |
| 335 | test_must_fail test-tool serve-v2 --stateless-rpc |
| 336 | ) <in >/dev/null 2>err && |
| 337 | grep "unexpected line: .this-is-not-a-command." err |
| 338 | ' |
| 339 | |
| 340 | # Test the basics of object-info |
| 341 | # |
| 342 | test_expect_success 'basics of object-info' ' |
| 343 | test_config transfer.advertiseObjectInfo true && |
| 344 | |
| 345 | test-tool pkt-line pack >in <<-EOF && |
| 346 | command=object-info |
| 347 | object-format=$(test_oid algo) |
| 348 | 0001 |
| 349 | size |
| 350 | oid $(git rev-parse two:two.t) |
| 351 | oid $(git rev-parse two:two.t) |
| 352 | 0000 |
| 353 | EOF |
| 354 | |
| 355 | cat >expect <<-EOF && |
| 356 | size |
| 357 | $(git rev-parse two:two.t) $(wc -c <two.t | xargs) |
| 358 | $(git rev-parse two:two.t) $(wc -c <two.t | xargs) |
| 359 | 0000 |
| 360 | EOF |
| 361 | |
| 362 | test-tool serve-v2 --stateless-rpc <in >out && |
| 363 | test-tool pkt-line unpack <out >actual && |
| 364 | test_cmp expect actual |
| 365 | ' |
| 366 | |
| 367 | test_expect_success 'test capability advertisement with uploadpack.advertiseBundleURIs' ' |
| 368 | test_config uploadpack.advertiseBundleURIs true && |
| 369 | |
| 370 | cat >expect.extra <<-EOF && |
| 371 | bundle-uri |
| 372 | EOF |
| 373 | cat expect.base \ |
| 374 | expect.extra \ |
| 375 | expect.trailer >expect && |
| 376 | |
| 377 | if test_have_prereq WINDOWS |
| 378 | then |
| 379 | GIT_USER_AGENT=FAKE && export GIT_USER_AGENT |
| 380 | fi && |
| 381 | GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \ |
| 382 | --advertise-capabilities >out && |
| 383 | test-tool pkt-line unpack <out >actual && |
| 384 | test_cmp expect actual |
| 385 | ' |
| 386 | |
| 387 | test_expect_success 'basics of bundle-uri: dies if not enabled' ' |
| 388 | test-tool pkt-line pack >in <<-EOF && |
| 389 | command=bundle-uri |
| 390 | 0000 |
| 391 | EOF |
| 392 | |
| 393 | cat >err.expect <<-\EOF && |
| 394 | fatal: invalid command '"'"'bundle-uri'"'"' |
| 395 | EOF |
| 396 | |
| 397 | cat >expect <<-\EOF && |
| 398 | ERR serve: invalid command '"'"'bundle-uri'"'"' |
| 399 | EOF |
| 400 | |
| 401 | test_must_fail test-tool serve-v2 --stateless-rpc <in >out 2>err.actual && |
| 402 | test_cmp err.expect err.actual && |
| 403 | test_must_be_empty out |
| 404 | ' |
| 405 | |
| 406 | test_expect_success 'object-info missing from capabilities when disabled' ' |
| 407 | test_config transfer.advertiseObjectInfo false && |
| 408 | |
| 409 | GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \ |
| 410 | --advertise-capabilities >out && |
| 411 | test-tool pkt-line unpack <out >actual && |
| 412 | |
| 413 | ! grep object.info actual |
| 414 | ' |
| 415 | |
| 416 | test_expect_success 'object-info commands rejected when disabled' ' |
| 417 | test_config transfer.advertiseObjectInfo false && |
| 418 | |
| 419 | test-tool pkt-line pack >in <<-EOF && |
| 420 | command=object-info |
| 421 | EOF |
| 422 | |
| 423 | test_must_fail test-tool serve-v2 --stateless-rpc <in 2>err && |
| 424 | grep invalid.command err |
| 425 | ' |
| 426 | |
| 427 | test_done |