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