/* * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, * as published by the Free Software Foundation. * * In addition to the permissions in the GNU General Public License, * the authors give you unlimited permission to link the compiled * version of this file into combinations with other programs, * and to distribute those combinations without any restriction * coming from the use of this file. (The General Public License * restrictions do apply in other respects; for example, they cover * modification of the file, and distribution when not linked into * a combined executable.) * * This file is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "test_lib.h" #include "fileops.h" static char *odb_dir = "test-objects"; #include "t03-data.h" static int make_odb_dir(void) { if (gitfo_mkdir(odb_dir, 0755) < 0) { int err = errno; fprintf(stderr, "can't make directory \"%s\"", odb_dir); if (err == EEXIST) fprintf(stderr, " (already exists)"); fprintf(stderr, "\n"); return -1; } return 0; } static int check_object_files(object_data *d) { if (gitfo_exists(d->dir) < 0) return -1; if (gitfo_exists(d->file) < 0) return -1; return 0; } static int cmp_objects(git_rawobj *o1, git_rawobj *o2) { if (o1->type != o2->type) return -1; if (o1->len != o2->len) return -1; if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0)) return -1; return 0; } static int remove_object_files(object_data *d) { if (gitfo_unlink(d->file) < 0) { fprintf(stderr, "can't delete object file \"%s\"\n", d->file); return -1; } if ((gitfo_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) { fprintf(stderr, "can't remove directory \"%s\"\n", d->dir); return -1; } if (gitfo_rmdir(odb_dir) < 0) { fprintf(stderr, "can't remove directory \"%s\"\n", odb_dir); return -1; } return 0; } BEGIN_TEST("write", write_commit) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, commit.id)); must_pass(git_odb_write(&id2, db, &commit_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&commit)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &commit_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&commit)); END_TEST BEGIN_TEST("write", write_tree) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, tree.id)); must_pass(git_odb_write(&id2, db, &tree_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&tree)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &tree_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&tree)); END_TEST BEGIN_TEST("write", write_tag) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, tag.id)); must_pass(git_odb_write(&id2, db, &tag_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&tag)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &tag_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&tag)); END_TEST BEGIN_TEST("write", write_zero) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, zero.id)); must_pass(git_odb_write(&id2, db, &zero_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&zero)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &zero_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&zero)); END_TEST BEGIN_TEST("write", write_one) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, one.id)); must_pass(git_odb_write(&id2, db, &one_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&one)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &one_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&one)); END_TEST BEGIN_TEST("write", write_two) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, two.id)); must_pass(git_odb_write(&id2, db, &two_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&two)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &two_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&two)); END_TEST BEGIN_TEST("write", write_some) git_odb *db; git_oid id1, id2; git_rawobj obj; must_pass(make_odb_dir()); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id1, some.id)); must_pass(git_odb_write(&id2, db, &some_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); must_pass(check_object_files(&some)); must_pass(git_odb_read(&obj, db, &id1)); must_pass(cmp_objects(&obj, &some_obj)); git_rawobj_close(&obj); git_odb_close(db); must_pass(remove_object_files(&some)); END_TEST git_testsuite *libgit2_suite_objwrite(void) { git_testsuite *suite = git_testsuite_new("Object Write"); ADD_TEST(suite, "write", write_commit); ADD_TEST(suite, "write", write_tree); ADD_TEST(suite, "write", write_tag); ADD_TEST(suite, "write", write_zero); ADD_TEST(suite, "write", write_one); ADD_TEST(suite, "write", write_two); ADD_TEST(suite, "write", write_some); return suite; }