diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-16 23:29:22 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-22 22:07:45 -0500 |
| commit | 3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb (patch) | |
| tree | fd6368a72944571c51627b40c592e7d58e0036e1 /tests/libgit2/pack | |
| parent | 91ba089663f5efc3bd4ba14a5099372cf5ce57a6 (diff) | |
| download | libgit2-3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb.tar.gz | |
refactor: `tests` is now `tests/libgit2`
Like we want to separate libgit2 and utility source code, we want to
separate libgit2 and utility tests. Start by moving all the tests into
libgit2.
Diffstat (limited to 'tests/libgit2/pack')
| -rw-r--r-- | tests/libgit2/pack/filelimit.c | 136 | ||||
| -rw-r--r-- | tests/libgit2/pack/indexer.c | 320 | ||||
| -rw-r--r-- | tests/libgit2/pack/midx.c | 111 | ||||
| -rw-r--r-- | tests/libgit2/pack/packbuilder.c | 276 | ||||
| -rw-r--r-- | tests/libgit2/pack/sharing.c | 42 | ||||
| -rw-r--r-- | tests/libgit2/pack/threadsafety.c | 62 |
6 files changed, 947 insertions, 0 deletions
diff --git a/tests/libgit2/pack/filelimit.c b/tests/libgit2/pack/filelimit.c new file mode 100644 index 000000000..fa08485fb --- /dev/null +++ b/tests/libgit2/pack/filelimit.c @@ -0,0 +1,136 @@ +#include "clar_libgit2.h" +#include "mwindow.h" + +#include <git2.h> +#include "git2/sys/commit.h" +#include "git2/sys/mempack.h" + +static size_t expected_open_mwindow_files = 0; +static size_t original_mwindow_file_limit = 0; + +extern git_mutex git__mwindow_mutex; +extern git_mwindow_ctl git_mwindow__mem_ctl; + +void test_pack_filelimit__initialize_tiny(void) +{ + expected_open_mwindow_files = 1; + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, expected_open_mwindow_files)); +} + +void test_pack_filelimit__initialize_medium(void) +{ + expected_open_mwindow_files = 10; + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, expected_open_mwindow_files)); +} + +void test_pack_filelimit__initialize_unlimited(void) +{ + expected_open_mwindow_files = 15; + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, 0)); +} + +void test_pack_filelimit__cleanup(void) +{ + git_str path = GIT_STR_INIT; + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, original_mwindow_file_limit)); + + cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "repo.git")); + cl_fixture_cleanup(path.ptr); + git_str_dispose(&path); +} + +/* + * Create a packfile with one commit, one tree, and two blobs. The first blob + * (README.md) has the same content in all commits, but the second one + * (file.txt) has a different content in each commit. + */ +static void create_packfile_commit( + git_repository *repo, + git_oid *out_commit_id, + git_oid *parent_id, + size_t commit_index, + size_t commit_count) +{ + git_str file_contents = GIT_STR_INIT; + git_treebuilder *treebuilder; + git_packbuilder *packbuilder; + git_signature *s; + git_oid oid, tree_id, commit_id; + const git_oid *parents[] = { parent_id }; + size_t parent_count = parent_id ? 1 : 0; + + cl_git_pass(git_treebuilder_new(&treebuilder, repo, NULL)); + + cl_git_pass(git_blob_create_from_buffer(&oid, repo, "", 0)); + cl_git_pass(git_treebuilder_insert(NULL, treebuilder, "README.md", &oid, 0100644)); + + cl_git_pass(git_str_printf(&file_contents, "Commit %zd/%zd", commit_index, commit_count)); + cl_git_pass(git_blob_create_from_buffer(&oid, repo, file_contents.ptr, file_contents.size)); + cl_git_pass(git_treebuilder_insert(NULL, treebuilder, "file.txt", &oid, 0100644)); + + cl_git_pass(git_treebuilder_write(&tree_id, treebuilder)); + cl_git_pass(git_signature_now(&s, "alice", "alice@example.com")); + cl_git_pass(git_commit_create_from_ids(&commit_id, repo, "refs/heads/master", s, s, + NULL, file_contents.ptr, &tree_id, parent_count, parents)); + + cl_git_pass(git_packbuilder_new(&packbuilder, repo)); + cl_git_pass(git_packbuilder_insert_commit(packbuilder, &commit_id)); + cl_git_pass(git_packbuilder_write(packbuilder, NULL, 0, NULL, NULL)); + + cl_git_pass(git_oid_cpy(out_commit_id, &commit_id)); + + git_str_dispose(&file_contents); + git_treebuilder_free(treebuilder); + git_packbuilder_free(packbuilder); + git_signature_free(s); +} + +void test_pack_filelimit__open_repo_with_multiple_packfiles(void) +{ + git_str path = GIT_STR_INIT; + git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; + git_repository *repo; + git_revwalk *walk; + git_oid id, *parent_id = NULL; + size_t i; + const size_t commit_count = 16; + unsigned int open_windows; + + /* + * Create a repository and populate it with 16 commits, each in its own + * packfile. + */ + cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "repo.git")); + cl_git_pass(git_repository_init(&repo, path.ptr, true)); + for (i = 0; i < commit_count; ++i) { + create_packfile_commit(repo, &id, parent_id, i + 1, commit_count); + parent_id = &id; + } + + cl_git_pass(git_revwalk_new(&walk, repo)); + cl_git_pass(git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL)); + cl_git_pass(git_revwalk_push_ref(walk, "refs/heads/master")); + + /* Walking the repository requires eventually opening each of the packfiles. */ + i = 0; + while (git_revwalk_next(&id, walk) == 0) + ++i; + cl_assert_equal_i(commit_count, i); + + cl_git_pass(git_mutex_lock(&git__mwindow_mutex)); + /* + * Adding an assert while holding a lock will cause the whole process to + * deadlock. Copy the value and do the assert after releasing the lock. + */ + open_windows = ctl->open_windows; + cl_git_pass(git_mutex_unlock(&git__mwindow_mutex)); + + cl_assert_equal_i(expected_open_mwindow_files, open_windows); + + git_str_dispose(&path); + git_revwalk_free(walk); + git_repository_free(repo); +} diff --git a/tests/libgit2/pack/indexer.c b/tests/libgit2/pack/indexer.c new file mode 100644 index 000000000..ec48ffd98 --- /dev/null +++ b/tests/libgit2/pack/indexer.c @@ -0,0 +1,320 @@ +#include "clar_libgit2.h" +#include <git2.h> +#include "futils.h" +#include "hash.h" +#include "iterator.h" +#include "vector.h" +#include "posix.h" + + +/* + * This is a packfile with three objects. The second is a delta which + * depends on the third, which is also a delta. + */ +static const unsigned char out_of_order_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, + 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76, + 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, + 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, + 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01, + 0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c, + 0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62, + 0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4, + 0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92, + 0x6f, 0xae, 0x66, 0x75 +}; +static const unsigned int out_of_order_pack_len = 112; + +/* + * Packfile with two objects. The second is a delta against an object + * which is not in the packfile + */ +static const unsigned char thin_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, + 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76, + 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, + 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, + 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x42, 0x52, + 0x3a, 0x6f, 0x39, 0xd1, 0xfe, 0x66, 0x68, 0x6b, 0xa5, 0xe5, 0xe2, 0x97, + 0xac, 0x94, 0x6c, 0x76, 0x0b, 0x04 +}; +static const unsigned int thin_pack_len = 78; + +/* + * Packfile with one object. It references an object which is not in the + * packfile and has a corrupt length (states the deltified stream is 1 byte + * long, where it is actually 6). + */ +static const unsigned char corrupt_thin_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, + 0x71, 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, + 0x10, 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, + 0x62, 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x07, + 0x67, 0x03, 0xc5, 0x40, 0x99, 0x49, 0xb1, 0x3b, 0x7d, 0xae, 0x9b, 0x0e, + 0xdd, 0xde, 0xc6, 0x76, 0x43, 0x24, 0x64 +}; +static const unsigned int corrupt_thin_pack_len = 67; + +/* + * Packfile with a missing trailer. + */ +static const unsigned char missing_trailer_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x50, 0xf4, 0x3b, +}; +static const unsigned int missing_trailer_pack_len = 12; + +/* + * Packfile that causes the packfile stream to open in a way in which it leaks + * the stream reader. + */ +static const unsigned char leaky_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, + 0xf4, 0xbd, 0x51, 0x51, 0x51, 0x51, 0x51, 0x72, 0x65, 0x41, 0x4b, 0x63, + 0x5f, 0x64, 0x65, 0x70, 0x74, 0x68, 0xbd, 0x41, 0x4b +}; +static const unsigned int leaky_pack_len = 33; + +/* + * Packfile with a three objects. The first one is a tree referencing two blobs, + * the second object is one of those blobs. The second blob is missing. + */ +unsigned char incomplete_pack[] = { + 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, + 0xae, 0x03, 0x78, 0x9c, 0x33, 0x34, 0x30, 0x30, 0x33, 0x31, 0x51, 0x48, + 0x4a, 0x2c, 0x62, 0x08, 0x17, 0x3b, 0x15, 0xd9, 0x7e, 0xfa, 0x67, 0x6d, + 0xf6, 0x56, 0x4f, 0x85, 0x7d, 0xcb, 0xd6, 0xde, 0x53, 0xd1, 0x6d, 0x7f, + 0x66, 0x08, 0x91, 0x4e, 0xcb, 0xcf, 0x67, 0x50, 0xad, 0x39, 0x9a, 0xa2, + 0xb3, 0x71, 0x41, 0xc8, 0x87, 0x9e, 0x13, 0xf6, 0xba, 0x53, 0xec, 0xc2, + 0xfe, 0xda, 0xed, 0x9b, 0x09, 0x00, 0xe8, 0xc8, 0x19, 0xab, 0x34, 0x78, + 0x9c, 0x4b, 0x4a, 0x2c, 0xe2, 0x02, 0x00, 0x03, 0x9d, 0x01, 0x40, 0x4b, + 0x72, 0xa2, 0x6f, 0xb6, 0x88, 0x2d, 0x6c, 0xa5, 0x07, 0xb2, 0xa5, 0x45, + 0xe8, 0xdb, 0xe6, 0x53, 0xb3, 0x52, 0xe2 +}; +unsigned int incomplete_pack_len = 115; + +static const unsigned char base_obj[] = { 07, 076 }; +static const unsigned int base_obj_len = 2; + +void test_pack_indexer__out_of_order(void) +{ + git_indexer *idx = 0; + git_indexer_progress stats = { 0 }; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + cl_git_pass(git_indexer_append( + idx, out_of_order_pack, out_of_order_pack_len, &stats)); + cl_git_pass(git_indexer_commit(idx, &stats)); + + cl_assert_equal_i(stats.total_objects, 3); + cl_assert_equal_i(stats.received_objects, 3); + cl_assert_equal_i(stats.indexed_objects, 3); + + git_indexer_free(idx); +} + +void test_pack_indexer__missing_trailer(void) +{ + git_indexer *idx = 0; + git_indexer_progress stats = { 0 }; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + cl_git_pass(git_indexer_append( + idx, missing_trailer_pack, missing_trailer_pack_len, &stats)); + cl_git_fail(git_indexer_commit(idx, &stats)); + + cl_assert(git_error_last() != NULL); + cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_INDEXER); + + git_indexer_free(idx); +} + +void test_pack_indexer__leaky(void) +{ + git_indexer *idx = 0; + git_indexer_progress stats = { 0 }; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + cl_git_pass(git_indexer_append( + idx, leaky_pack, leaky_pack_len, &stats)); + cl_git_fail(git_indexer_commit(idx, &stats)); + + cl_assert(git_error_last() != NULL); + cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_INDEXER); + + git_indexer_free(idx); +} + +void test_pack_indexer__fix_thin(void) +{ + git_indexer *idx = NULL; + git_indexer_progress stats = { 0 }; + git_repository *repo; + git_odb *odb; + git_oid id, should_id; + + cl_git_pass(git_repository_init(&repo, "thin.git", true)); + cl_git_pass(git_repository_odb(&odb, repo)); + + /* Store the missing base into your ODB so the indexer can fix the pack */ + cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB)); + git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18"); + cl_assert_equal_oid(&should_id, &id); + + cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); + cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats)); + cl_git_pass(git_indexer_commit(idx, &stats)); + + cl_assert_equal_i(stats.total_objects, 2); + cl_assert_equal_i(stats.received_objects, 2); + cl_assert_equal_i(stats.indexed_objects, 2); + cl_assert_equal_i(stats.local_objects, 1); + + cl_assert_equal_s("fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13", git_indexer_name(idx)); + + git_indexer_free(idx); + git_odb_free(odb); + git_repository_free(repo); + + /* + * The pack's name/hash only tells us what objects there are, + * so we need to go through the packfile again in order to + * figure out whether we calculated the trailer correctly. + */ + { + unsigned char buffer[128]; + int fd; + ssize_t read; + struct stat st; + const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack"; + + fd = p_open(name, O_RDONLY); + cl_assert(fd != -1); + + cl_git_pass(p_stat(name, &st)); + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + read = p_read(fd, buffer, sizeof(buffer)); + cl_assert(read != -1); + p_close(fd); + + cl_git_pass(git_indexer_append(idx, buffer, read, &stats)); + cl_git_pass(git_indexer_commit(idx, &stats)); + + cl_assert_equal_i(stats.total_objects, 3); + cl_assert_equal_i(stats.received_objects, 3); + cl_assert_equal_i(stats.indexed_objects, 3); + cl_assert_equal_i(stats.local_objects, 0); + + git_indexer_free(idx); + } +} + +void test_pack_indexer__corrupt_length(void) +{ + git_indexer *idx = NULL; + git_indexer_progress stats = { 0 }; + git_repository *repo; + git_odb *odb; + git_oid id, should_id; + + cl_git_pass(git_repository_init(&repo, "thin.git", true)); + cl_git_pass(git_repository_odb(&odb, repo)); + + /* Store the missing base into your ODB so the indexer can fix the pack */ + cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB)); + git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18"); + cl_assert_equal_oid(&should_id, &id); + + cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); + cl_git_pass(git_indexer_append( + idx, corrupt_thin_pack, corrupt_thin_pack_len, &stats)); + cl_git_fail(git_indexer_commit(idx, &stats)); + + cl_assert(git_error_last() != NULL); + cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_ZLIB); + + git_indexer_free(idx); + git_odb_free(odb); + git_repository_free(repo); +} + +void test_pack_indexer__incomplete_pack_fails_with_strict(void) +{ + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; + git_indexer *idx = 0; + git_indexer_progress stats = { 0 }; + + opts.verify = 1; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); + cl_git_pass(git_indexer_append( + idx, incomplete_pack, incomplete_pack_len, &stats)); + cl_git_fail(git_indexer_commit(idx, &stats)); + + cl_assert_equal_i(stats.total_objects, 2); + cl_assert_equal_i(stats.received_objects, 2); + cl_assert_equal_i(stats.indexed_objects, 2); + + git_indexer_free(idx); +} + +void test_pack_indexer__out_of_order_with_connectivity_checks(void) +{ + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; + git_indexer *idx = 0; + git_indexer_progress stats = { 0 }; + + opts.verify = 1; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); + cl_git_pass(git_indexer_append( + idx, out_of_order_pack, out_of_order_pack_len, &stats)); + cl_git_pass(git_indexer_commit(idx, &stats)); + + cl_assert_equal_i(stats.total_objects, 3); + cl_assert_equal_i(stats.received_objects, 3); + cl_assert_equal_i(stats.indexed_objects, 3); + + git_indexer_free(idx); +} + +static int find_tmp_file_recurs(void *opaque, git_str *path) +{ + int error = 0; + git_str *first_tmp_file = opaque; + struct stat st; + + if ((error = p_lstat_posixly(path->ptr, &st)) < 0) + return error; + + if (S_ISDIR(st.st_mode)) + return git_fs_path_direach(path, 0, find_tmp_file_recurs, opaque); + + /* This is the template that's used in git_futils_mktmp. */ + if (strstr(git_str_cstr(path), "_git2_") != NULL) + return git_str_sets(first_tmp_file, git_str_cstr(path)); + + return 0; +} + +void test_pack_indexer__no_tmp_files(void) +{ + git_indexer *idx = NULL; + git_str path = GIT_STR_INIT; + git_str first_tmp_file = GIT_STR_INIT; + + /* Precondition: there are no temporary files. */ + cl_git_pass(git_str_sets(&path, clar_sandbox_path())); + cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); + git_str_dispose(&path); + cl_assert(git_str_len(&first_tmp_file) == 0); + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + git_indexer_free(idx); + + cl_git_pass(git_str_sets(&path, clar_sandbox_path())); + cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); + git_str_dispose(&path); + cl_assert(git_str_len(&first_tmp_file) == 0); + git_str_dispose(&first_tmp_file); +} diff --git a/tests/libgit2/pack/midx.c b/tests/libgit2/pack/midx.c new file mode 100644 index 000000000..9dd949363 --- /dev/null +++ b/tests/libgit2/pack/midx.c @@ -0,0 +1,111 @@ +#include "clar_libgit2.h" + +#include <git2.h> +#include <git2/sys/midx.h> + +#include "futils.h" +#include "midx.h" + +void test_pack_midx__parse(void) +{ + git_repository *repo; + struct git_midx_file *idx; + struct git_midx_entry e; + git_oid id; + git_str midx_path = GIT_STR_INIT; + + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index")); + cl_git_pass(git_midx_open(&idx, git_str_cstr(&midx_path))); + cl_assert_equal_i(git_midx_needs_refresh(idx, git_str_cstr(&midx_path)), 0); + + cl_git_pass(git_oid_fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5")); + cl_git_pass(git_midx_entry_find(&e, idx, &id, GIT_OID_HEXSZ)); + cl_assert_equal_oid(&e.sha1, &id); + cl_assert_equal_s( + (const char *)git_vector_get(&idx->packfile_names, e.pack_index), + "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx"); + + git_midx_free(idx); + git_repository_free(repo); + git_str_dispose(&midx_path); +} + +void test_pack_midx__lookup(void) +{ + git_repository *repo; + git_commit *commit; + git_oid id; + + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + + cl_git_pass(git_oid_fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5")); + cl_git_pass(git_commit_lookup_prefix(&commit, repo, &id, GIT_OID_HEXSZ)); + cl_assert_equal_s(git_commit_message(commit), "packed commit one\n"); + + git_commit_free(commit); + git_repository_free(repo); +} + +void test_pack_midx__writer(void) +{ + git_repository *repo; + git_midx_writer *w = NULL; + git_buf midx = GIT_BUF_INIT; + git_str expected_midx = GIT_STR_INIT, path = GIT_STR_INIT; + + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); + + cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack")); + cl_git_pass(git_midx_writer_new(&w, git_str_cstr(&path))); + + cl_git_pass(git_midx_writer_add(w, "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx")); + cl_git_pass(git_midx_writer_add(w, "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.idx")); + cl_git_pass(git_midx_writer_add(w, "pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")); + + cl_git_pass(git_midx_writer_dump(&midx, w)); + cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack/multi-pack-index")); + cl_git_pass(git_futils_readbuffer(&expected_midx, git_str_cstr(&path))); + + cl_assert_equal_i(midx.size, git_str_len(&expected_midx)); + cl_assert_equal_strn(midx.ptr, git_str_cstr(&expected_midx), midx.size); + + git_buf_dispose(&midx); + git_str_dispose(&expected_midx); + git_str_dispose(&path); + git_midx_writer_free(w); + git_repository_free(repo); +} + +void test_pack_midx__odb_create(void) +{ + git_repository *repo; + git_odb *odb; + git_clone_options opts = GIT_CLONE_OPTIONS_INIT; + git_str midx = GIT_STR_INIT, expected_midx = GIT_STR_INIT, midx_path = GIT_STR_INIT; + struct stat st; + + opts.bare = true; + opts.local = GIT_CLONE_LOCAL; + cl_git_pass(git_clone(&repo, cl_fixture("testrepo/.gitted"), "./clone.git", &opts)); + cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index")); + cl_git_fail(p_stat(git_str_cstr(&midx_path), &st)); + + cl_git_pass(git_repository_odb(&odb, repo)); + cl_git_pass(git_odb_write_multi_pack_index(odb)); + git_odb_free(odb); + + cl_git_pass(p_stat(git_str_cstr(&midx_path), &st)); + + cl_git_pass(git_futils_readbuffer(&expected_midx, cl_fixture("testrepo.git/objects/pack/multi-pack-index"))); + cl_git_pass(git_futils_readbuffer(&midx, git_str_cstr(&midx_path))); + cl_assert_equal_i(git_str_len(&midx), git_str_len(&expected_midx)); + cl_assert_equal_strn(git_str_cstr(&midx), git_str_cstr(&expected_midx), git_str_len(&midx)); + + git_repository_free(repo); + git_str_dispose(&midx); + git_str_dispose(&midx_path); + git_str_dispose(&expected_midx); + + cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES)); +} diff --git a/tests/libgit2/pack/packbuilder.c b/tests/libgit2/pack/packbuilder.c new file mode 100644 index 000000000..f23579817 --- /dev/null +++ b/tests/libgit2/pack/packbuilder.c @@ -0,0 +1,276 @@ +#include "clar_libgit2.h" +#include "futils.h" +#include "pack.h" +#include "hash.h" +#include "iterator.h" +#include "vector.h" +#include "posix.h" +#include "hash.h" + +static git_repository *_repo; +static git_revwalk *_revwalker; +static git_packbuilder *_packbuilder; +static git_indexer *_indexer; +static git_vector _commits; +static int _commits_is_initialized; +static git_indexer_progress _stats; + +extern bool git_disable_pack_keep_file_checks; + +void test_pack_packbuilder__initialize(void) +{ + _repo = cl_git_sandbox_init("testrepo.git"); + cl_git_pass(p_chdir("testrepo.git")); + cl_git_pass(git_revwalk_new(&_revwalker, _repo)); + cl_git_pass(git_packbuilder_new(&_packbuilder, _repo)); + cl_git_pass(git_vector_init(&_commits, 0, NULL)); + _commits_is_initialized = 1; + memset(&_stats, 0, sizeof(_stats)); + p_fsync__cnt = 0; +} + +void test_pack_packbuilder__cleanup(void) +{ + git_oid *o; + unsigned int i; + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0)); + cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, false)); + + if (_commits_is_initialized) { + _commits_is_initialized = 0; + git_vector_foreach(&_commits, i, o) { + git__free(o); + } + git_vector_free(&_commits); + } + + git_packbuilder_free(_packbuilder); + _packbuilder = NULL; + + git_revwalk_free(_revwalker); + _revwalker = NULL; + + git_indexer_free(_indexer); + _indexer = NULL; + + cl_git_pass(p_chdir("..")); + cl_git_sandbox_cleanup(); + _repo = NULL; +} + +static void seed_packbuilder(void) +{ + git_oid oid, *o; + unsigned int i; + + git_revwalk_sorting(_revwalker, GIT_SORT_TIME); + cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD")); + + while (git_revwalk_next(&oid, _revwalker) == 0) { + o = git__malloc(GIT_OID_RAWSZ); + cl_assert(o != NULL); + git_oid_cpy(o, &oid); + cl_git_pass(git_vector_insert(&_commits, o)); + } + + git_vector_foreach(&_commits, i, o) { + cl_git_pass(git_packbuilder_insert(_packbuilder, o, NULL)); + } + + git_vector_foreach(&_commits, i, o) { + git_object *obj; + cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJECT_COMMIT)); + cl_git_pass(git_packbuilder_insert_tree(_packbuilder, + git_commit_tree_id((git_commit *)obj))); + git_object_free(obj); + } +} + +static int feed_indexer(void *ptr, size_t len, void *payload) +{ + git_indexer_progress *stats = (git_indexer_progress *)payload; + + return git_indexer_append(_indexer, ptr, len, stats); +} + +void test_pack_packbuilder__create_pack(void) +{ + git_indexer_progress stats; + git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; + git_hash_ctx ctx; + unsigned char hash[GIT_HASH_SHA1_SIZE]; + char hex[(GIT_HASH_SHA1_SIZE * 2) + 1]; + + seed_packbuilder(); + + cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL)); + cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats)); + cl_git_pass(git_indexer_commit(_indexer, &stats)); + + git_str_printf(&path, "pack-%s.pack", git_indexer_name(_indexer)); + + /* + * By default, packfiles are created with only one thread. + * Therefore we can predict the object ordering and make sure + * we create exactly the same pack as git.git does when *not* + * reusing existing deltas (as libgit2). + * + * $ cd tests/resources/testrepo.git + * $ git rev-list --objects HEAD | \ + * git pack-objects -q --no-reuse-delta --threads=1 pack + * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack + * 5d410bdf97cf896f9007681b92868471d636954b + * + */ + + cl_git_pass(git_futils_readbuffer(&buf, git_str_cstr(&path))); + + cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1)); + cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size)); + cl_git_pass(git_hash_final(hash, &ctx)); + git_hash_ctx_cleanup(&ctx); + + git_str_dispose(&path); + git_str_dispose(&buf); + + git_hash_fmt(hex, hash, GIT_HASH_SHA1_SIZE); + cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b"); +} + +void test_pack_packbuilder__get_name(void) +{ + seed_packbuilder(); + + cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL)); + cl_assert_equal_s("7f5fa362c664d68ba7221259be1cbd187434b2f0", git_packbuilder_name(_packbuilder)); +} + +void test_pack_packbuilder__write_default_path(void) +{ + seed_packbuilder(); + + cl_git_pass(git_packbuilder_write(_packbuilder, NULL, 0, NULL, NULL)); + cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx")); + cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack")); +} + +static void test_write_pack_permission(mode_t given, mode_t expected) +{ + struct stat statbuf; + mode_t mask, os_mask; + + seed_packbuilder(); + + cl_git_pass(git_packbuilder_write(_packbuilder, ".", given, NULL, NULL)); + + /* Windows does not return group/user bits from stat, + * files are never executable. + */ +#ifdef GIT_WIN32 + os_mask = 0600; +#else + os_mask = 0777; +#endif + + mask = p_umask(0); + p_umask(mask); + + cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf)); + cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask); + + cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf)); + cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask); +} + +void test_pack_packbuilder__permissions_standard(void) +{ + test_write_pack_permission(0, GIT_PACK_FILE_MODE); +} + +void test_pack_packbuilder__permissions_readonly(void) +{ + test_write_pack_permission(0444, 0444); +} + +void test_pack_packbuilder__permissions_readwrite(void) +{ + test_write_pack_permission(0666, 0666); +} + +void test_pack_packbuilder__does_not_fsync_by_default(void) +{ + seed_packbuilder(); + cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); + cl_assert_equal_sz(0, p_fsync__cnt); +} + +/* We fsync the packfile and index. On non-Windows, we also fsync + * the parent directories. + */ +#ifdef GIT_WIN32 +static int expected_fsyncs = 2; +#else +static int expected_fsyncs = 4; +#endif + +void test_pack_packbuilder__fsync_global_setting(void) +{ + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1)); + p_fsync__cnt = 0; + seed_packbuilder(); + cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); + cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt); +} + +void test_pack_packbuilder__fsync_repo_setting(void) +{ + cl_repo_set_bool(_repo, "core.fsyncObjectFiles", true); + p_fsync__cnt = 0; + seed_packbuilder(); + cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); + cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt); +} + +static int foreach_cb(void *buf, size_t len, void *payload) +{ + git_indexer *idx = (git_indexer *) payload; + cl_git_pass(git_indexer_append(idx, buf, len, &_stats)); + return 0; +} + +void test_pack_packbuilder__foreach(void) +{ + git_indexer *idx; + + seed_packbuilder(); + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); + cl_git_pass(git_indexer_commit(idx, &_stats)); + git_indexer_free(idx); +} + +static int foreach_cancel_cb(void *buf, size_t len, void *payload) +{ + git_indexer *idx = (git_indexer *)payload; + cl_git_pass(git_indexer_append(idx, buf, len, &_stats)); + return (_stats.total_objects > 2) ? -1111 : 0; +} + +void test_pack_packbuilder__foreach_with_cancel(void) +{ + git_indexer *idx; + + seed_packbuilder(); + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); + cl_git_fail_with( + git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111); + git_indexer_free(idx); +} + +void test_pack_packbuilder__keep_file_check(void) +{ + assert(!git_disable_pack_keep_file_checks); + cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, true)); + assert(git_disable_pack_keep_file_checks); +} diff --git a/tests/libgit2/pack/sharing.c b/tests/libgit2/pack/sharing.c new file mode 100644 index 000000000..eaf7686b7 --- /dev/null +++ b/tests/libgit2/pack/sharing.c @@ -0,0 +1,42 @@ +#include "clar_libgit2.h" +#include <git2.h> +#include "strmap.h" +#include "mwindow.h" +#include "pack.h" + +extern git_strmap *git__pack_cache; + +void test_pack_sharing__open_two_repos(void) +{ + git_repository *repo1, *repo2; + git_object *obj1, *obj2; + git_oid id; + size_t pos; + void *data; + int error; + + cl_git_pass(git_repository_open(&repo1, cl_fixture("testrepo.git"))); + cl_git_pass(git_repository_open(&repo2, cl_fixture("testrepo.git"))); + + git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + + cl_git_pass(git_object_lookup(&obj1, repo1, &id, GIT_OBJECT_ANY)); + cl_git_pass(git_object_lookup(&obj2, repo2, &id, GIT_OBJECT_ANY)); + + pos = 0; + while ((error = git_strmap_iterate(&data, git__pack_cache, &pos, NULL)) == 0) { + struct git_pack_file *pack = (struct git_pack_file *) data; + + cl_assert_equal_i(2, pack->refcount.val); + } + + cl_assert_equal_i(3, git_strmap_size(git__pack_cache)); + + git_object_free(obj1); + git_object_free(obj2); + git_repository_free(repo1); + git_repository_free(repo2); + + /* we don't want to keep the packs open after the repos go away */ + cl_assert_equal_i(0, git_strmap_size(git__pack_cache)); +} diff --git a/tests/libgit2/pack/threadsafety.c b/tests/libgit2/pack/threadsafety.c new file mode 100644 index 000000000..fd6a61fbd --- /dev/null +++ b/tests/libgit2/pack/threadsafety.c @@ -0,0 +1,62 @@ +#include "clar_libgit2.h" +#include "pool.h" + +#include <git2.h> +#include "git2/sys/commit.h" +#include "git2/sys/mempack.h" + +static size_t original_mwindow_file_limit = 0; + +void test_pack_threadsafety__initialize(void) +{ + size_t open_mwindow_files = 1; + + cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, open_mwindow_files)); +} + +void test_pack_threadsafety__cleanup(void) +{ + cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, original_mwindow_file_limit)); +} + +#ifdef GIT_THREADS +static void *get_status(void *arg) +{ + const char *repo_path = (const char *)arg; + git_repository *repo; + git_status_list *status; + + cl_git_pass(git_repository_open(&repo, repo_path)); + cl_git_pass(git_status_list_new(&status, repo, NULL)); + git_status_list_free(status); + git_repository_free(repo); + + return NULL; +} +#endif + +void test_pack_threadsafety__open_repo_in_multiple_threads(void) +{ +#ifdef GIT_THREADS + const char *repo_path = cl_fixture("../.."); + git_repository *repo; + git_thread threads[8]; + size_t i; + + /* If we can't open the libgit2 repo or if it isn't a full repo + * with proper history, just skip this test */ + if (git_repository_open(&repo, repo_path) < 0) + cl_skip(); + if (git_repository_is_shallow(repo)) + cl_skip(); + git_repository_free(repo); + + for (i = 0; i < ARRAY_SIZE(threads); i++) + git_thread_create(&threads[i], get_status, (void *)repo_path); + for (i = 0; i < ARRAY_SIZE(threads); i++) + git_thread_join(&threads[i], NULL); +#else + cl_skip(); +#endif +} |
