|
3 | 3 | #include "sysdir.h" |
4 | 4 | #include <ctype.h> |
5 | 5 |
|
| 6 | +static git_buf config_path = GIT_BUF_INIT; |
| 7 | + |
| 8 | +void test_repo_open__initialize(void) |
| 9 | +{ |
| 10 | + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &config_path)); |
| 11 | +} |
6 | 12 |
|
7 | 13 | void test_repo_open__cleanup(void) |
8 | 14 | { |
9 | 15 | cl_git_sandbox_cleanup(); |
10 | 16 | cl_fixture_cleanup("empty_standard_repo"); |
| 17 | + cl_fixture_cleanup("__global_config"); |
11 | 18 |
|
12 | 19 | if (git_path_isdir("alternate")) |
13 | 20 | git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES); |
14 | 21 |
|
15 | 22 | git_path__set_owner(GIT_PATH_MOCK_OWNER_NONE); |
| 23 | + |
| 24 | + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr)); |
| 25 | + git_buf_dispose(&config_path); |
16 | 26 | } |
17 | 27 |
|
18 | 28 | void test_repo_open__bare_empty_repo(void) |
@@ -480,11 +490,103 @@ void test_repo_open__validates_dir_ownership(void) |
480 | 490 | void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void) |
481 | 491 | { |
482 | 492 | git_repository *repo; |
| 493 | + git_buf config_path = GIT_BUF_INIT, |
| 494 | + config_filename = GIT_BUF_INIT, |
| 495 | + config_data = GIT_BUF_INIT; |
483 | 496 |
|
484 | 497 | cl_fixture_sandbox("empty_standard_repo"); |
485 | 498 | cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git")); |
486 | 499 |
|
487 | 500 | git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER); |
488 | 501 | cl_git_fail(git_repository_open(&repo, "empty_standard_repo")); |
489 | 502 |
|
| 503 | + /* Add safe.directory options to the global configuration */ |
| 504 | + git_buf_joinpath(&config_path, clar_sandbox_path(), "__global_config"); |
| 505 | + cl_must_pass(p_mkdir(config_path.ptr, 0777)); |
| 506 | + git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr); |
| 507 | + |
| 508 | + git_buf_joinpath(&config_filename, config_path.ptr, ".gitconfig"); |
| 509 | + |
| 510 | + git_buf_printf(&config_data, |
| 511 | + "[foo]\n" \ |
| 512 | + "\tbar = Foobar\n" \ |
| 513 | + "\tbaz = Baz!\n" \ |
| 514 | + "[safe]\n" \ |
| 515 | + "\tdirectory = /non/existent/path\n" \ |
| 516 | + "\tdirectory = /\n" \ |
| 517 | + "\tdirectory = c:\\\\temp\n" \ |
| 518 | + "\tdirectory = %s/%s\n" \ |
| 519 | + "\tdirectory = /tmp\n" \ |
| 520 | + "[bar]\n" \ |
| 521 | + "\tfoo = barfoo\n", |
| 522 | + clar_sandbox_path(), "empty_standard_repo"); |
| 523 | + cl_git_rewritefile(config_filename.ptr, config_data.ptr); |
| 524 | + |
| 525 | + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); |
| 526 | + git_repository_free(repo); |
| 527 | + |
| 528 | + git_buf_dispose(&config_path); |
| 529 | + git_buf_dispose(&config_filename); |
| 530 | + git_buf_dispose(&config_data); |
| 531 | +} |
| 532 | + |
| 533 | +void test_repo_open__can_reset_safe_directory_list(void) |
| 534 | +{ |
| 535 | + git_repository *repo; |
| 536 | + git_buf config_path = GIT_BUF_INIT, |
| 537 | + config_filename = GIT_BUF_INIT, |
| 538 | + config_data = GIT_BUF_INIT; |
| 539 | + |
| 540 | + cl_fixture_sandbox("empty_standard_repo"); |
| 541 | + cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git")); |
| 542 | + |
| 543 | + git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER); |
| 544 | + cl_git_fail(git_repository_open(&repo, "empty_standard_repo")); |
| 545 | + |
| 546 | + /* Add safe.directory options to the global configuration */ |
| 547 | + git_buf_joinpath(&config_path, clar_sandbox_path(), "__global_config"); |
| 548 | + cl_must_pass(p_mkdir(config_path.ptr, 0777)); |
| 549 | + git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr); |
| 550 | + |
| 551 | + git_buf_joinpath(&config_filename, config_path.ptr, ".gitconfig"); |
| 552 | + |
| 553 | + /* The blank resets our sandbox directory and opening fails */ |
| 554 | + |
| 555 | + git_buf_printf(&config_data, |
| 556 | + "[foo]\n" \ |
| 557 | + "\tbar = Foobar\n" \ |
| 558 | + "\tbaz = Baz!\n" \ |
| 559 | + "[safe]\n" \ |
| 560 | + "\tdirectory = %s/%s\n" \ |
| 561 | + "\tdirectory = \n" \ |
| 562 | + "\tdirectory = /tmp\n" \ |
| 563 | + "[bar]\n" \ |
| 564 | + "\tfoo = barfoo\n", |
| 565 | + clar_sandbox_path(), "empty_standard_repo"); |
| 566 | + cl_git_rewritefile(config_filename.ptr, config_data.ptr); |
| 567 | + |
| 568 | + cl_git_fail(git_repository_open(&repo, "empty_standard_repo")); |
| 569 | + |
| 570 | + /* The blank resets tmp and allows subsequent declarations to succeed */ |
| 571 | + |
| 572 | + git_buf_clear(&config_data); |
| 573 | + git_buf_printf(&config_data, |
| 574 | + "[foo]\n" \ |
| 575 | + "\tbar = Foobar\n" \ |
| 576 | + "\tbaz = Baz!\n" \ |
| 577 | + "[safe]\n" \ |
| 578 | + "\tdirectory = /tmp\n" \ |
| 579 | + "\tdirectory = \n" \ |
| 580 | + "\tdirectory = %s/%s\n" \ |
| 581 | + "[bar]\n" \ |
| 582 | + "\tfoo = barfoo\n", |
| 583 | + clar_sandbox_path(), "empty_standard_repo"); |
| 584 | + cl_git_rewritefile(config_filename.ptr, config_data.ptr); |
| 585 | + |
| 586 | + cl_git_pass(git_repository_open(&repo, "empty_standard_repo")); |
| 587 | + git_repository_free(repo); |
| 588 | + |
| 589 | + git_buf_dispose(&config_path); |
| 590 | + git_buf_dispose(&config_filename); |
| 591 | + git_buf_dispose(&config_data); |
490 | 592 | } |
0 commit comments