diff options
author | Ben Straub <bs@github.com> | 2013-11-14 14:05:52 -0800 |
---|---|---|
committer | Ben Straub <bs@github.com> | 2013-11-14 14:05:52 -0800 |
commit | 1782038144ef3413831801bb9c2f3038a84ac6f4 (patch) | |
tree | f074cc30890a20f5418c10fae1815ca516588a27 /tests/pack | |
parent | 7b947bf5cc59eefa83c28eb5f5fd8434207ebb8b (diff) | |
download | libgit2-1782038144ef3413831801bb9c2f3038a84ac6f4.tar.gz |
Rename tests-clar to tests
Diffstat (limited to 'tests/pack')
-rw-r--r-- | tests/pack/indexer.c | 126 | ||||
-rw-r--r-- | tests/pack/packbuilder.c | 204 |
2 files changed, 330 insertions, 0 deletions
diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c new file mode 100644 index 000000000..07963a9e7 --- /dev/null +++ b/tests/pack/indexer.c @@ -0,0 +1,126 @@ +#include "clar_libgit2.h" +#include <git2.h> +#include "fileops.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. + */ +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 +}; +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 + */ +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 +}; +unsigned int thin_pack_len = 78; + +unsigned char base_obj[] = { 07, 076 }; +unsigned int base_obj_len = 2; + +void test_pack_indexer__out_of_order(void) +{ + git_indexer *idx; + git_transfer_progress stats; + + cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, 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__fix_thin(void) +{ + git_indexer *idx; + git_transfer_progress stats; + 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_OBJ_BLOB)); + git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18"); + cl_assert(!git_oid_cmp(&id, &should_id)); + + cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, 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); + + git_oid_fromstr(&should_id, "11f0f69b334728fdd8bc86b80499f22f29d85b15"); + cl_assert(!git_oid_cmp(git_indexer_hash(idx), &should_id)); + + 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-11f0f69b334728fdd8bc86b80499f22f29d85b15.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, 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); + } +} diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c new file mode 100644 index 000000000..54d1e8022 --- /dev/null +++ b/tests/pack/packbuilder.c @@ -0,0 +1,204 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "pack.h" +#include "hash.h" +#include "iterator.h" +#include "vector.h" +#include "posix.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; + +void test_pack_packbuilder__initialize(void) +{ + _repo = cl_git_sandbox_init("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; +} + +void test_pack_packbuilder__cleanup(void) +{ + git_oid *o; + unsigned int i; + + 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_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_OBJ_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_transfer_progress *stats = (git_transfer_progress *)payload; + + return git_indexer_append(_indexer, ptr, len, stats); +} + +void test_pack_packbuilder__create_pack(void) +{ + git_transfer_progress stats; + git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; + git_hash_ctx ctx; + git_oid hash; + char hex[41]; hex[40] = '\0'; + + seed_packbuilder(); + + cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL, NULL)); + cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats)); + cl_git_pass(git_indexer_commit(_indexer, &stats)); + + git_oid_fmt(hex, git_indexer_hash(_indexer)); + git_buf_printf(&path, "pack-%s.pack", hex); + + /* + * 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-clar/resources/testrepo.git + * $ git rev-list --objects HEAD | \ + * git pack-objects -q --no-reuse-delta --threads=1 pack + * $ sha1sum git-80e61eb315239ef3c53033e37fee43b744d57122.pack + * 5d410bdf97cf896f9007681b92868471d636954b + * + */ + + cl_git_pass(git_futils_readbuffer(&buf, git_buf_cstr(&path))); + + cl_git_pass(git_hash_ctx_init(&ctx)); + 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_buf_free(&path); + git_buf_free(&buf); + + git_oid_fmt(hex, &hash); + + cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b"); +} + +void test_pack_packbuilder__get_hash(void) +{ + char hex[41]; hex[40] = '\0'; + + seed_packbuilder(); + + git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL); + git_oid_fmt(hex, git_packbuilder_hash(_packbuilder)); + + cl_assert_equal_s(hex, "80e61eb315239ef3c53033e37fee43b744d57122"); +} + +static void test_write_pack_permission(mode_t given, mode_t expected) +{ + struct stat statbuf; + mode_t mask, os_mask; + + seed_packbuilder(); + + 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-80e61eb315239ef3c53033e37fee43b744d57122.idx", &statbuf)); + cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask); + + cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.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); +} + +static git_transfer_progress stats; +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, NULL)); + cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); + cl_git_pass(git_indexer_commit(idx, &stats)); + git_indexer_free(idx); +} |