diff options
| author | Carlos Martín Nieto <cmn@elego.de> | 2011-03-31 15:29:13 +0200 |
|---|---|---|
| committer | Carlos Martín Nieto <cmn@elego.de> | 2011-03-31 15:29:13 +0200 |
| commit | f026f2b9ee5f0aeced5c366c890c4a29eee2a1c7 (patch) | |
| tree | c26b59992df7ebe645cb9485a4eb70c41e127816 /tests | |
| parent | 11d0e70578baf47fb1cb565e0336e18d417e5da6 (diff) | |
| parent | a796d24cf697b0b51aa0ca7ef887e980f0d9fb7a (diff) | |
| download | libgit2-f026f2b9ee5f0aeced5c366c890c4a29eee2a1c7.tar.gz | |
Merge upstream/development
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
Diffstat (limited to 'tests')
32 files changed, 802 insertions, 521 deletions
diff --git a/tests/resources/empty_bare.git/HEAD b/tests/resources/empty_bare.git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests/resources/empty_bare.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests/resources/empty_bare.git/config b/tests/resources/empty_bare.git/config new file mode 100644 index 000000000..90e16477b --- /dev/null +++ b/tests/resources/empty_bare.git/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = false + bare = true + symlinks = false + ignorecase = true + hideDotFiles = dotGitOnly diff --git a/tests/resources/empty_bare.git/description b/tests/resources/empty_bare.git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests/resources/empty_bare.git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests/resources/empty_bare.git/info/exclude b/tests/resources/empty_bare.git/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/tests/resources/empty_bare.git/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests/resources/empty_bare.git/objects/info/dummy-marker.txt b/tests/resources/empty_bare.git/objects/info/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_bare.git/objects/info/dummy-marker.txt diff --git a/tests/resources/empty_bare.git/objects/pack/dummy-marker.txt b/tests/resources/empty_bare.git/objects/pack/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_bare.git/objects/pack/dummy-marker.txt diff --git a/tests/resources/empty_bare.git/refs/heads/dummy-marker.txt b/tests/resources/empty_bare.git/refs/heads/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_bare.git/refs/heads/dummy-marker.txt diff --git a/tests/resources/empty_bare.git/refs/tags/dummy-marker.txt b/tests/resources/empty_bare.git/refs/tags/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_bare.git/refs/tags/dummy-marker.txt diff --git a/tests/resources/empty_standard_repo/.gitted/HEAD b/tests/resources/empty_standard_repo/.gitted/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests/resources/empty_standard_repo/.gitted/config b/tests/resources/empty_standard_repo/.gitted/config new file mode 100644 index 000000000..78387c50b --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/config @@ -0,0 +1,8 @@ +[core] + repositoryformatversion = 0 + filemode = false + bare = false + logallrefupdates = true + symlinks = false + ignorecase = true + hideDotFiles = dotGitOnly diff --git a/tests/resources/empty_standard_repo/.gitted/description b/tests/resources/empty_standard_repo/.gitted/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests/resources/empty_standard_repo/.gitted/info/exclude b/tests/resources/empty_standard_repo/.gitted/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests/resources/empty_standard_repo/.gitted/objects/info/dummy-marker.txt b/tests/resources/empty_standard_repo/.gitted/objects/info/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/objects/info/dummy-marker.txt diff --git a/tests/resources/empty_standard_repo/.gitted/objects/pack/dummy-marker.txt b/tests/resources/empty_standard_repo/.gitted/objects/pack/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/objects/pack/dummy-marker.txt diff --git a/tests/resources/empty_standard_repo/.gitted/refs/heads/dummy-marker.txt b/tests/resources/empty_standard_repo/.gitted/refs/heads/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/refs/heads/dummy-marker.txt diff --git a/tests/resources/empty_standard_repo/.gitted/refs/tags/dummy-marker.txt b/tests/resources/empty_standard_repo/.gitted/refs/tags/dummy-marker.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/tests/resources/empty_standard_repo/.gitted/refs/tags/dummy-marker.txt diff --git a/tests/t00-core.c b/tests/t00-core.c index 1f6c06a95..4cb111428 100644 --- a/tests/t00-core.c +++ b/tests/t00-core.c @@ -27,17 +27,6 @@ #include "vector.h" #include "fileops.h" -BEGIN_TEST(refcnt0, "increment refcount twice, decrement twice") - git_refcnt p; - - gitrc_init(&p, 0); - gitrc_inc(&p); - gitrc_inc(&p); - must_be_true(!gitrc_dec(&p)); - must_be_true(gitrc_dec(&p)); - gitrc_free(&p); -END_TEST - BEGIN_TEST(string0, "compare prefixes") must_be_true(git__prefixcmp("", "") == 0); must_be_true(git__prefixcmp("a", "") == 0); @@ -162,187 +151,204 @@ BEGIN_TEST(path2, "get the latest component in a path") #undef TOPDIR_TEST END_TEST -typedef int (normalize_path)(char *, const char *); +typedef int (normalize_path)(char *, size_t, const char *); -static int ensure_normalized(const char *input_path, const char *expected_path, normalize_path normalizer) +/* Assert flags */ +#define CWD_AS_PREFIX 1 +#define PATH_AS_SUFFIX 2 +#define ROOTED_PATH 4 + +static int ensure_normalized(const char *input_path, const char *expected_path, normalize_path normalizer, int assert_flags) { int error = GIT_SUCCESS; char buffer_out[GIT_PATH_MAX]; + char current_workdir[GIT_PATH_MAX]; - error = normalizer(buffer_out, input_path); + error = gitfo_getcwd(current_workdir, sizeof(current_workdir)); + if (error < GIT_SUCCESS) + return error; + + error = normalizer(buffer_out, sizeof(buffer_out), input_path); if (error < GIT_SUCCESS) return error; if (expected_path == NULL) return error; - if (strcmp(buffer_out, expected_path)) - error = GIT_ERROR; + if ((assert_flags & PATH_AS_SUFFIX) != 0) + if (git__suffixcmp(buffer_out, expected_path)) + return GIT_ERROR; + + if ((assert_flags & CWD_AS_PREFIX) != 0) + if (git__prefixcmp(buffer_out, current_workdir)) + return GIT_ERROR; + + if ((assert_flags & ROOTED_PATH) != 0) { + error = strcmp(expected_path, buffer_out); + } return error; } -static int ensure_dir_path_normalized(const char *input_path, const char *expected_path) +static int ensure_dir_path_normalized(const char *input_path, const char *expected_path, int assert_flags) { - return ensure_normalized(input_path, expected_path, gitfo_prettify_dir_path); + return ensure_normalized(input_path, expected_path, gitfo_prettify_dir_path, assert_flags); } -static int ensure_file_path_normalized(const char *input_path, const char *expected_path) +static int ensure_file_path_normalized(const char *input_path, const char *expected_path, int assert_flags) { - return ensure_normalized(input_path, expected_path, gitfo_prettify_file_path); + return ensure_normalized(input_path, expected_path, gitfo_prettify_file_path, assert_flags); } BEGIN_TEST(path3, "prettify and validate a path to a file") - must_pass(ensure_file_path_normalized("a", "a")); - must_pass(ensure_file_path_normalized("./testrepo.git", "testrepo.git")); - must_pass(ensure_file_path_normalized("./.git", ".git")); - must_pass(ensure_file_path_normalized("./git.", "git.")); - must_fail(ensure_file_path_normalized("git./", NULL)); - must_fail(ensure_file_path_normalized("", NULL)); - must_fail(ensure_file_path_normalized(".", NULL)); - must_fail(ensure_file_path_normalized("./", NULL)); - must_fail(ensure_file_path_normalized("./.", NULL)); - must_fail(ensure_file_path_normalized("./..", NULL)); - must_fail(ensure_file_path_normalized("../.", NULL)); - must_fail(ensure_file_path_normalized("./.././/", NULL)); - must_fail(ensure_file_path_normalized("dir/..", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/../..", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/..///..", NULL)); - must_fail(ensure_file_path_normalized("dir/sub///../..", NULL)); - must_fail(ensure_file_path_normalized("dir/sub///..///..", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/../../..", NULL)); - must_pass(ensure_file_path_normalized("dir", "dir")); - must_fail(ensure_file_path_normalized("dir//", NULL)); - must_pass(ensure_file_path_normalized("./dir", "dir")); - must_fail(ensure_file_path_normalized("dir/.", NULL)); - must_fail(ensure_file_path_normalized("dir///./", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/..", NULL)); - must_fail(ensure_file_path_normalized("dir//sub/..",NULL)); - must_fail(ensure_file_path_normalized("dir//sub/../", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/../", NULL)); - must_fail(ensure_file_path_normalized("dir/sub/../.", NULL)); - must_fail(ensure_file_path_normalized("dir/s1/../s2/", NULL)); - must_fail(ensure_file_path_normalized("d1/s1///s2/..//../s3/", NULL)); - must_pass(ensure_file_path_normalized("d1/s1//../s2/../../d2", "d2")); - must_fail(ensure_file_path_normalized("dir/sub/../", NULL)); - must_fail(ensure_file_path_normalized("....", NULL)); - must_fail(ensure_file_path_normalized("...", NULL)); - must_fail(ensure_file_path_normalized("./...", NULL)); - must_fail(ensure_file_path_normalized("d1/...", NULL)); - must_fail(ensure_file_path_normalized("d1/.../", NULL)); - must_fail(ensure_file_path_normalized("d1/.../d2", NULL)); + must_pass(ensure_file_path_normalized("a", "a", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_file_path_normalized("./testrepo.git", "testrepo.git", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_file_path_normalized("./.git", ".git", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_file_path_normalized("./git.", "git.", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_fail(ensure_file_path_normalized("git./", NULL, 0)); + must_fail(ensure_file_path_normalized("", NULL, 0)); + must_fail(ensure_file_path_normalized(".", NULL, 0)); + must_fail(ensure_file_path_normalized("./", NULL, 0)); + must_fail(ensure_file_path_normalized("./.", NULL, 0)); + must_fail(ensure_file_path_normalized("./..", NULL, 0)); + must_fail(ensure_file_path_normalized("../.", NULL, 0)); + must_fail(ensure_file_path_normalized("./.././/", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/../..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/..///..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub///../..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub///..///..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/../../..", NULL, 0)); + must_pass(ensure_file_path_normalized("dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_fail(ensure_file_path_normalized("dir//", NULL, 0)); + must_pass(ensure_file_path_normalized("./dir", "dir", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_fail(ensure_file_path_normalized("dir/.", NULL, 0)); + must_fail(ensure_file_path_normalized("dir///./", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/..", NULL, 0)); + must_fail(ensure_file_path_normalized("dir//sub/..",NULL, 0)); + must_fail(ensure_file_path_normalized("dir//sub/../", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/sub/../.", NULL, 0)); + must_fail(ensure_file_path_normalized("dir/s1/../s2/", NULL, 0)); + must_fail(ensure_file_path_normalized("d1/s1///s2/..//../s3/", NULL, 0)); + must_pass(ensure_file_path_normalized("d1/s1//../s2/../../d2", "d2", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_fail(ensure_file_path_normalized("dir/sub/../", NULL, 0)); + must_pass(ensure_file_path_normalized("../../a/../../b/c/d/../../e", "b/e", PATH_AS_SUFFIX)); + must_fail(ensure_file_path_normalized("....", NULL, 0)); + must_fail(ensure_file_path_normalized("...", NULL, 0)); + must_fail(ensure_file_path_normalized("./...", NULL, 0)); + must_fail(ensure_file_path_normalized("d1/...", NULL, 0)); + must_fail(ensure_file_path_normalized("d1/.../", NULL, 0)); + must_fail(ensure_file_path_normalized("d1/.../d2", NULL, 0)); - must_pass(ensure_file_path_normalized("/a", "/a")); - must_pass(ensure_file_path_normalized("/./testrepo.git", "/testrepo.git")); - must_pass(ensure_file_path_normalized("/./.git", "/.git")); - must_pass(ensure_file_path_normalized("/./git.", "/git.")); - must_fail(ensure_file_path_normalized("/git./", NULL)); - must_fail(ensure_file_path_normalized("/", NULL)); - must_fail(ensure_file_path_normalized("/.", NULL)); - must_fail(ensure_file_path_normalized("/./", NULL)); - must_fail(ensure_file_path_normalized("/./.", NULL)); - must_fail(ensure_file_path_normalized("/./..", NULL)); - must_fail(ensure_file_path_normalized("/../.", NULL)); - must_fail(ensure_file_path_normalized("/./.././/", NULL)); - must_fail(ensure_file_path_normalized("/dir/..", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/../..", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/..///..", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub///../..", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub///..///..", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/../../..", NULL)); - must_pass(ensure_file_path_normalized("/dir", "/dir")); - must_fail(ensure_file_path_normalized("/dir//", NULL)); - must_pass(ensure_file_path_normalized("/./dir", "/dir")); - must_fail(ensure_file_path_normalized("/dir/.", NULL)); - must_fail(ensure_file_path_normalized("/dir///./", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/..", NULL)); - must_fail(ensure_file_path_normalized("/dir//sub/..",NULL)); - must_fail(ensure_file_path_normalized("/dir//sub/../", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/../", NULL)); - must_fail(ensure_file_path_normalized("/dir/sub/../.", NULL)); - must_fail(ensure_file_path_normalized("/dir/s1/../s2/", NULL)); - must_fail(ensure_file_path_normalized("/d1/s1///s2/..//../s3/", NULL)); - must_pass(ensure_file_path_normalized("/d1/s1//../s2/../../d2", "/d2")); - must_fail(ensure_file_path_normalized("/dir/sub/../", NULL)); - must_fail(ensure_file_path_normalized("/....", NULL)); - must_fail(ensure_file_path_normalized("/...", NULL)); - must_fail(ensure_file_path_normalized("/./...", NULL)); - must_fail(ensure_file_path_normalized("/d1/...", NULL)); - must_fail(ensure_file_path_normalized("/d1/.../", NULL)); - must_fail(ensure_file_path_normalized("/d1/.../d2", NULL)); + must_pass(ensure_file_path_normalized("/a", "/a", ROOTED_PATH)); + must_pass(ensure_file_path_normalized("/./testrepo.git", "/testrepo.git", ROOTED_PATH)); + must_pass(ensure_file_path_normalized("/./.git", "/.git", ROOTED_PATH)); + must_pass(ensure_file_path_normalized("/./git.", "/git.", ROOTED_PATH)); + must_fail(ensure_file_path_normalized("/git./", NULL, 0)); + must_fail(ensure_file_path_normalized("/", NULL, 0)); + must_fail(ensure_file_path_normalized("/.", NULL, 0)); + must_fail(ensure_file_path_normalized("/./", NULL, 0)); + must_fail(ensure_file_path_normalized("/./.", NULL, 0)); + must_fail(ensure_file_path_normalized("/./..", NULL, 0)); + must_fail(ensure_file_path_normalized("/../.", NULL, 0)); + must_fail(ensure_file_path_normalized("/./.././/", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/../..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/..///..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub///../..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub///..///..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/../../..", NULL, 0)); + must_pass(ensure_file_path_normalized("/dir", "/dir", 0)); + must_fail(ensure_file_path_normalized("/dir//", NULL, 0)); + must_pass(ensure_file_path_normalized("/./dir", "/dir", 0)); + must_fail(ensure_file_path_normalized("/dir/.", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir///./", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/..", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir//sub/..",NULL, 0)); + must_fail(ensure_file_path_normalized("/dir//sub/../", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/sub/../.", NULL, 0)); + must_fail(ensure_file_path_normalized("/dir/s1/../s2/", NULL, 0)); + must_fail(ensure_file_path_normalized("/d1/s1///s2/..//../s3/", NULL, 0)); + must_pass(ensure_file_path_normalized("/d1/s1//../s2/../../d2", "/d2", 0)); + must_fail(ensure_file_path_normalized("/dir/sub/../", NULL, 0)); + must_fail(ensure_file_path_normalized("/....", NULL, 0)); + must_fail(ensure_file_path_normalized("/...", NULL, 0)); + must_fail(ensure_file_path_normalized("/./...", NULL, 0)); + must_fail(ensure_file_path_normalized("/d1/...", NULL, 0)); + must_fail(ensure_file_path_normalized("/d1/.../", NULL, 0)); + must_fail(ensure_file_path_normalized("/d1/.../d2", NULL, 0)); END_TEST BEGIN_TEST(path4, "validate and prettify a path to a folder") - must_pass(ensure_dir_path_normalized("./testrepo.git", "testrepo.git/")); - must_pass(ensure_dir_path_normalized("./.git", ".git/")); - must_pass(ensure_dir_path_normalized("./git.", "git./")); - must_pass(ensure_dir_path_normalized("git./", "git./")); - must_pass(ensure_dir_path_normalized("", "")); - must_pass(ensure_dir_path_normalized(".", "")); - must_pass(ensure_dir_path_normalized("./", "")); - must_pass(ensure_dir_path_normalized("./.", "")); - must_fail(ensure_dir_path_normalized("./..", NULL)); - must_fail(ensure_dir_path_normalized("../.", NULL)); - must_fail(ensure_dir_path_normalized("./.././/", NULL)); - must_pass(ensure_dir_path_normalized("dir/..", "")); - must_pass(ensure_dir_path_normalized("dir/sub/../..", "")); - must_pass(ensure_dir_path_normalized("dir/sub/..///..", "")); - must_pass(ensure_dir_path_normalized("dir/sub///../..", "")); - must_pass(ensure_dir_path_normalized("dir/sub///..///..", "")); - must_fail(ensure_dir_path_normalized("dir/sub/../../..", NULL)); - must_pass(ensure_dir_path_normalized("dir", "dir/")); - must_pass(ensure_dir_path_normalized("dir//", "dir/")); - must_pass(ensure_dir_path_normalized("./dir", "dir/")); - must_pass(ensure_dir_path_normalized("dir/.", "dir/")); - must_pass(ensure_dir_path_normalized("dir///./", "dir/")); - must_pass(ensure_dir_path_normalized("dir/sub/..", "dir/")); - must_pass(ensure_dir_path_normalized("dir//sub/..", "dir/")); - must_pass(ensure_dir_path_normalized("dir//sub/../", "dir/")); - must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/")); - must_pass(ensure_dir_path_normalized("dir/sub/../.", "dir/")); - must_pass(ensure_dir_path_normalized("dir/s1/../s2/", "dir/s2/")); - must_pass(ensure_dir_path_normalized("d1/s1///s2/..//../s3/", "d1/s3/")); - must_pass(ensure_dir_path_normalized("d1/s1//../s2/../../d2", "d2/")); - must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/")); - must_fail(ensure_dir_path_normalized("....", NULL)); - must_fail(ensure_dir_path_normalized("...", NULL)); - must_fail(ensure_dir_path_normalized("./...", NULL)); - must_fail(ensure_dir_path_normalized("d1/...", NULL)); - must_fail(ensure_dir_path_normalized("d1/.../", NULL)); - must_fail(ensure_dir_path_normalized("d1/.../d2", NULL)); - - must_pass(ensure_dir_path_normalized("/./testrepo.git", "/testrepo.git/")); - must_pass(ensure_dir_path_normalized("/./.git", "/.git/")); - must_pass(ensure_dir_path_normalized("/./git.", "/git./")); - must_pass(ensure_dir_path_normalized("/git./", "/git./")); - must_pass(ensure_dir_path_normalized("/", "/")); - must_pass(ensure_dir_path_normalized("//", "/")); - must_pass(ensure_dir_path_normalized("///", "/")); - must_pass(ensure_dir_path_normalized("/.", "/")); - must_pass(ensure_dir_path_normalized("/./", "/")); - must_fail(ensure_dir_path_normalized("/./..", NULL)); - must_fail(ensure_dir_path_normalized("/../.", NULL)); - must_fail(ensure_dir_path_normalized("/./.././/", NULL)); - must_pass(ensure_dir_path_normalized("/dir/..", "/")); - must_pass(ensure_dir_path_normalized("/dir/sub/../..", "/")); - must_fail(ensure_dir_path_normalized("/dir/sub/../../..", NULL)); - must_pass(ensure_dir_path_normalized("/dir", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir//", "/dir/")); - must_pass(ensure_dir_path_normalized("/./dir", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir/.", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir///./", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir//sub/..", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir/sub/../", "/dir/")); - must_pass(ensure_dir_path_normalized("//dir/sub/../.", "/dir/")); - must_pass(ensure_dir_path_normalized("/dir/s1/../s2/", "/dir/s2/")); - must_pass(ensure_dir_path_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/")); - must_pass(ensure_dir_path_normalized("/d1/s1//../s2/../../d2", "/d2/")); - must_fail(ensure_dir_path_normalized("/....", NULL)); - must_fail(ensure_dir_path_normalized("/...", NULL)); - must_fail(ensure_dir_path_normalized("/./...", NULL)); - must_fail(ensure_dir_path_normalized("/d1/...", NULL)); - must_fail(ensure_dir_path_normalized("/d1/.../", NULL)); - must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL)); + must_pass(ensure_dir_path_normalized("./testrepo.git", "testrepo.git/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("./.git", ".git/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("./git.", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("git./", "git./", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized(".", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("./", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("./.", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub///../..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub///..///..", "", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir//", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("./dir", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir///./", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir//sub/..", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir//sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/../.", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/s1/../s2/", "dir/s2/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("d1/s1///s2/..//../s3/", "d1/s3/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("d1/s1//../s2/../../d2", "d2/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("dir/sub/../", "dir/", CWD_AS_PREFIX | PATH_AS_SUFFIX)); + must_pass(ensure_dir_path_normalized("../../a/../../b/c/d/../../e", "b/e/", PATH_AS_SUFFIX)); + must_fail(ensure_dir_path_normalized("....", NULL, 0)); + must_fail(ensure_dir_path_normalized("...", NULL, 0)); + must_fail(ensure_dir_path_normalized("./...", NULL, 0)); + must_fail(ensure_dir_path_normalized("d1/...", NULL, 0)); + must_fail(ensure_dir_path_normalized("d1/.../", NULL, 0)); + must_fail(ensure_dir_path_normalized("d1/.../d2", NULL, 0)); + + must_pass(ensure_dir_path_normalized("/./testrepo.git", "/testrepo.git/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/./.git", "/.git/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/./git.", "/git./", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/git./", "/git./", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/", "/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("//", "/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("///", "/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/.", "/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/./", "/", ROOTED_PATH)); + must_fail(ensure_dir_path_normalized("/./..", NULL, 0)); + must_fail(ensure_dir_path_normalized("/../.", NULL, 0)); + must_fail(ensure_dir_path_normalized("/./.././/", NULL, 0)); + must_pass(ensure_dir_path_normalized("/dir/..", "/", 0)); + must_pass(ensure_dir_path_normalized("/dir/sub/../..", "/", 0)); + must_fail(ensure_dir_path_normalized("/dir/sub/../../..", NULL, 0)); + must_pass(ensure_dir_path_normalized("/dir", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir//", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/./dir", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir/.", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir///./", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir//sub/..", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir/sub/../", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("//dir/sub/../.", "/dir/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/dir/s1/../s2/", "/dir/s2/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/", ROOTED_PATH)); + must_pass(ensure_dir_path_normalized("/d1/s1//../s2/../../d2", "/d2/", ROOTED_PATH)); + must_fail(ensure_dir_path_normalized("/....", NULL, 0)); + must_fail(ensure_dir_path_normalized("/...", NULL, 0)); + must_fail(ensure_dir_path_normalized("/./...", NULL, 0)); + must_fail(ensure_dir_path_normalized("/d1/...", NULL, 0)); + must_fail(ensure_dir_path_normalized("/d1/.../", NULL, 0)); + must_fail(ensure_dir_path_normalized("/d1/.../d2", NULL, 0)); END_TEST static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path) @@ -383,6 +389,37 @@ BEGIN_TEST(path6, "properly join path components for more than one path") must_pass(ensure_joinpath_n("a", "b", "", "/c/d", "a/b/c/d")); END_TEST +static int count_number_of_path_segments(const char *path) +{ + int number = 0; + char *current = (char *)path; + + while (*current) + { + if (*current++ == '/') + number++; + } + + assert (number > 0); + + return --number; +} + +BEGIN_TEST(path7, "prevent a path which escapes the root directory from being prettified") + char current_workdir[GIT_PATH_MAX]; + char prettified[GIT_PATH_MAX]; + int i = 0, number_to_escape; + + must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir))); + + number_to_escape = count_number_of_path_segments(current_workdir); + + for (i = 0; i < number_to_escape + 1; i++) + git__joinpath(current_workdir, current_workdir, "../"); + + must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir)); +END_TEST + typedef struct name_data { int count; /* return count */ char *name; /* filename */ @@ -626,8 +663,6 @@ END_TEST BEGIN_SUITE(core) - ADD_TEST(refcnt0); - ADD_TEST(string0); ADD_TEST(string1); @@ -641,6 +676,7 @@ BEGIN_SUITE(core) ADD_TEST(path4); ADD_TEST(path5); ADD_TEST(path6); + ADD_TEST(path7); ADD_TEST(dirent0); ADD_TEST(dirent1); diff --git a/tests/t01-rawobj.c b/tests/t01-rawobj.c index c2123cd3c..5db9a79fc 100644 --- a/tests/t01-rawobj.c +++ b/tests/t01-rawobj.c @@ -23,10 +23,17 @@ * Boston, MA 02110-1301, USA. */ #include "test_lib.h" -#include "t01-data.h" +#include "odb.h" #include "hash.h" +#include "t01-data.h" + +static int hash_object(git_oid *oid, git_rawobj *obj) +{ + return git_odb_hash(oid, obj->data, obj->len, obj->type); +} + BEGIN_TEST(oid0, "validate size of oid objects") git_oid out; must_be_true(20 == GIT_OID_RAWSZ); @@ -329,7 +336,7 @@ BEGIN_TEST(oid17, "stress test for the git_oid_shorten object") git_oid oid; size_t i, j; - int min_len, found_collision; + int min_len = 0, found_collision; os = git_oid_shorten_new(0); must_be_true(os != NULL); @@ -497,28 +504,28 @@ BEGIN_TEST(objhash0, "hash junk data") /* invalid types: */ junk_obj.data = some_data; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); junk_obj.type = GIT_OBJ__EXT1; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); junk_obj.type = GIT_OBJ__EXT2; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); junk_obj.type = GIT_OBJ_OFS_DELTA; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); junk_obj.type = GIT_OBJ_REF_DELTA; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); /* data can be NULL only if len is zero: */ junk_obj.type = GIT_OBJ_BLOB; junk_obj.data = NULL; - must_pass(git_rawobj_hash(&id, &junk_obj)); + must_pass(hash_object(&id, &junk_obj)); must_be_true(git_oid_cmp(&id, &id_zero) == 0); junk_obj.len = 1; - must_fail(git_rawobj_hash(&id, &junk_obj)); + must_fail(hash_object(&id, &junk_obj)); END_TEST BEGIN_TEST(objhash1, "hash a commit object") @@ -526,7 +533,7 @@ BEGIN_TEST(objhash1, "hash a commit object") must_pass(git_oid_mkstr(&id1, commit_id)); - must_pass(git_rawobj_hash(&id2, &commit_obj)); + must_pass(hash_object(&id2, &commit_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -536,7 +543,7 @@ BEGIN_TEST(objhash2, "hash a tree object") must_pass(git_oid_mkstr(&id1, tree_id)); - must_pass(git_rawobj_hash(&id2, &tree_obj)); + must_pass(hash_object(&id2, &tree_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -546,7 +553,7 @@ BEGIN_TEST(objhash3, "hash a tag object") must_pass(git_oid_mkstr(&id1, tag_id)); - must_pass(git_rawobj_hash(&id2, &tag_obj)); + must_pass(hash_object(&id2, &tag_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -556,7 +563,7 @@ BEGIN_TEST(objhash4, "hash a zero-length object") must_pass(git_oid_mkstr(&id1, zero_id)); - must_pass(git_rawobj_hash(&id2, &zero_obj)); + must_pass(hash_object(&id2, &zero_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -566,7 +573,7 @@ BEGIN_TEST(objhash5, "hash an one-byte long object") must_pass(git_oid_mkstr(&id1, one_id)); - must_pass(git_rawobj_hash(&id2, &one_obj)); + must_pass(hash_object(&id2, &one_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -576,7 +583,7 @@ BEGIN_TEST(objhash6, "hash a two-byte long object") must_pass(git_oid_mkstr(&id1, two_id)); - must_pass(git_rawobj_hash(&id2, &two_obj)); + must_pass(hash_object(&id2, &two_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST @@ -586,7 +593,7 @@ BEGIN_TEST(objhash7, "hash an object several bytes long") must_pass(git_oid_mkstr(&id1, some_id)); - must_pass(git_rawobj_hash(&id2, &some_obj)); + must_pass(hash_object(&id2, &some_obj)); must_be_true(git_oid_cmp(&id1, &id2) == 0); END_TEST diff --git a/tests/t02-objread.c b/tests/t02-objread.c index 2a9d130c4..85b03b026 100644 --- a/tests/t02-objread.c +++ b/tests/t02-objread.c @@ -24,6 +24,7 @@ */ #include "test_lib.h" #include "test_helpers.h" +#include "odb.h" #include "t02-data.h" #include "t02-oids.h" @@ -50,16 +51,16 @@ END_TEST BEGIN_TEST(readloose0, "read a loose commit") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &commit)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, commit.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &commit)); + must_pass(cmp_objects((git_rawobj *)&obj->raw, &commit)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &commit)); END_TEST @@ -67,16 +68,16 @@ END_TEST BEGIN_TEST(readloose1, "read a loose tree") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &tree)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, tree.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &tree)); + must_pass(cmp_objects((git_rawobj *)&obj->raw, &tree)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &tree)); END_TEST @@ -84,16 +85,16 @@ END_TEST BEGIN_TEST(readloose2, "read a loose tag") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &tag)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, tag.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &tag)); + must_pass(cmp_objects((git_rawobj *)&obj->raw, &tag)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &tag)); END_TEST @@ -101,16 +102,16 @@ END_TEST BEGIN_TEST(readloose3, "read a loose zero-bytes object") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &zero)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, zero.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &zero)); + must_pass(cmp_objects((git_rawobj *)&obj->raw, &zero)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &zero)); END_TEST @@ -118,16 +119,16 @@ END_TEST BEGIN_TEST(readloose4, "read a one-byte long loose object") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &one)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, one.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &one)); + must_pass(cmp_objects(&obj->raw, &one)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &one)); END_TEST @@ -135,16 +136,16 @@ END_TEST BEGIN_TEST(readloose5, "read a two-bytes long loose object") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &two)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, two.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &two)); + must_pass(cmp_objects(&obj->raw, &two)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &two)); END_TEST @@ -152,16 +153,16 @@ END_TEST BEGIN_TEST(readloose6, "read a loose object which is several bytes long") git_odb *db; git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(write_object_files(odb_dir, &some)); must_pass(git_odb_open(&db, odb_dir)); must_pass(git_oid_mkstr(&id, some.id)); must_pass(git_odb_read(&obj, db, &id)); - must_pass(cmp_objects(&obj, &some)); + must_pass(cmp_objects(&obj->raw, &some)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(odb_dir, &some)); END_TEST @@ -174,13 +175,13 @@ BEGIN_TEST(readpack0, "read several packed objects") for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) { git_oid id; - git_rawobj obj; + git_odb_object *obj; must_pass(git_oid_mkstr(&id, packed_objects[i])); must_be_true(git_odb_exists(db, &id) == 1); must_pass(git_odb_read(&obj, db, &id)); - git_rawobj_close(&obj); + git_odb_object_close(obj); } git_odb_close(db); @@ -194,17 +195,19 @@ BEGIN_TEST(readheader0, "read only the header of several packed objects") for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) { git_oid id; - git_rawobj obj, header; + git_odb_object *obj; + size_t len; + git_otype type; must_pass(git_oid_mkstr(&id, packed_objects[i])); must_pass(git_odb_read(&obj, db, &id)); - must_pass(git_odb_read_header(&header, db, &id)); + must_pass(git_odb_read_header(&len, &type, db, &id)); - must_be_true(obj.len == header.len); - must_be_true(obj.type == header.type); + must_be_true(obj->raw.len == len); + must_be_true(obj->raw.type == type); - git_rawobj_close(&obj); + git_odb_object_close(obj); } git_odb_close(db); @@ -218,19 +221,21 @@ BEGIN_TEST(readheader1, "read only the header of several loose objects") for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) { git_oid id; - git_rawobj obj, header; + git_odb_object *obj; + size_t len; + git_otype type; must_pass(git_oid_mkstr(&id, loose_objects[i])); must_be_true(git_odb_exists(db, &id) == 1); must_pass(git_odb_read(&obj, db, &id)); - must_pass(git_odb_read_header(&header, db, &id)); + must_pass(git_odb_read_header(&len, &type, db, &id)); - must_be_true(obj.len == header.len); - must_be_true(obj.type == header.type); + must_be_true(obj->raw.len == len); + must_be_true(obj->raw.type == type); - git_rawobj_close(&obj); + git_odb_object_close(obj); } git_odb_close(db); diff --git a/tests/t03-objwrite.c b/tests/t03-objwrite.c index 10c6c7f1a..773887397 100644 --- a/tests/t03-objwrite.c +++ b/tests/t03-objwrite.c @@ -24,6 +24,7 @@ */ #include "test_lib.h" #include "fileops.h" +#include "odb.h" static char *odb_dir = "test-objects"; #include "t03-data.h" @@ -80,23 +81,39 @@ static int remove_object_files(object_data *d) return 0; } +static int streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw) +{ + git_odb_stream *stream; + int error; + + if ((error = git_odb_open_wstream(&stream, odb, raw->len, raw->type)) < GIT_SUCCESS) + return error; + + stream->write(stream, raw->data, raw->len); + + error = stream->finalize_write(oid, stream); + stream->free(stream); + + return error; +} + BEGIN_TEST(write0, "write loose commit object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &commit_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&commit)); END_TEST @@ -104,20 +121,20 @@ END_TEST BEGIN_TEST(write1, "write loose tree object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &tree_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&tree)); END_TEST @@ -125,20 +142,20 @@ END_TEST BEGIN_TEST(write2, "write loose tag object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &tag_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&tag)); END_TEST @@ -146,20 +163,20 @@ END_TEST BEGIN_TEST(write3, "write zero-length object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &zero_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&zero)); END_TEST @@ -167,20 +184,20 @@ END_TEST BEGIN_TEST(write4, "write one-byte long object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &one_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&one)); END_TEST @@ -188,20 +205,20 @@ END_TEST BEGIN_TEST(write5, "write two-byte long object") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &two_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&two)); END_TEST @@ -209,20 +226,20 @@ END_TEST BEGIN_TEST(write6, "write an object which is several bytes long") git_odb *db; git_oid id1, id2; - git_rawobj obj; + git_odb_object *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_pass(streaming_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)); + must_pass(cmp_objects(&obj->raw, &some_obj)); - git_rawobj_close(&obj); + git_odb_object_close(obj); git_odb_close(db); must_pass(remove_object_files(&some)); END_TEST diff --git a/tests/t04-commit.c b/tests/t04-commit.c index 8e62759a8..e92842435 100644 --- a/tests/t04-commit.c +++ b/tests/t04-commit.c @@ -366,7 +366,7 @@ BEGIN_TEST(details0, "query the details on a parsed commit") const git_signature *author, *committer; const char *message, *message_short; - time_t commit_time; + git_time_t commit_time; unsigned int parents, p; git_commit *parent; @@ -390,11 +390,11 @@ BEGIN_TEST(details0, "query the details on a parsed commit") must_be_true(commit_time > 0); must_be_true(parents <= 2); for (p = 0;p < parents;p++) { - parent = git_commit_parent(commit, p); + must_pass(git_commit_parent(&parent, commit, p)); must_be_true(parent != NULL); must_be_true(git_commit_author(parent) != NULL); // is it really a commit? } - must_be_true(git_commit_parent(commit, parents) == NULL); + must_fail(git_commit_parent(&parent, commit, parents)); } git_repository_free(repo); @@ -407,39 +407,42 @@ This is a commit created in memory and it will be written back to disk\n" static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; + BEGIN_TEST(write0, "write a new commit object from memory to disk") git_repository *repo; - git_commit *commit, *parent; - git_tree *tree; - git_oid id; + git_commit *commit; + git_oid tree_id, parent_id, commit_id; const git_signature *author, *committer; /* char hex_oid[41]; */ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - /* Create commit in memory */ - must_pass(git_commit_new(&commit, repo)); - - /* Add new parent */ - git_oid_mkstr(&id, commit_ids[4]); - must_pass(git_commit_lookup(&parent, repo, &id)); - git_commit_add_parent(commit, parent); + git_oid_mkstr(&tree_id, tree_oid); + git_oid_mkstr(&parent_id, commit_ids[4]); - /* Set other attributes */ + /* create signatures */ committer = git_signature_new(COMMITTER_NAME, COMMITTER_EMAIL, 123456789, 60); must_be_true(committer != NULL); author = git_signature_new(COMMITTER_NAME, COMMITTER_EMAIL, 987654321, 90); must_be_true(author != NULL); - git_commit_set_committer(commit, committer); - git_commit_set_author(commit, author); - git_commit_set_message(commit, COMMIT_MESSAGE); + must_pass(git_commit_create_v( + &commit_id, /* out id */ + repo, + NULL, /* do not update the HEAD */ + author, + committer, + COMMIT_MESSAGE, + &tree_id, + 1, &parent_id)); git_signature_free((git_signature *)committer); git_signature_free((git_signature *)author); + must_pass(git_commit_lookup(&commit, repo, &commit_id)); + /* Check attributes were set correctly */ author = git_commit_author(commit); must_be_true(author != NULL); @@ -457,47 +460,6 @@ BEGIN_TEST(write0, "write a new commit object from memory to disk") must_be_true(strcmp(git_commit_message(commit), COMMIT_MESSAGE) == 0); - /* add new tree */ - git_oid_mkstr(&id, tree_oid); - must_pass(git_tree_lookup(&tree, repo, &id)); - - git_commit_set_tree(commit, tree); - - /* Test it has no OID */ - must_be_true(git_commit_id(commit) == NULL); - - /* Write to disk */ - must_pass(git_object_write((git_object *)commit)); - - must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit)); - - git_repository_free(repo); -END_TEST - -BEGIN_TEST(write1, "load a commit object, modify it and write it back") - git_repository *repo; - git_oid id; - git_commit *commit, *parent; - const char *message; - /* char hex_oid[41]; */ - - must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - - git_oid_mkstr(&id, commit_ids[0]); - - must_pass(git_commit_lookup(&commit, repo, &id)); - - message = git_commit_message(commit); - - git_commit_set_message(commit, "This is a new test message. Cool!\n"); - - git_oid_mkstr(&id, commit_ids[4]); - must_pass(git_commit_lookup(&parent, repo, &id)); - - git_commit_add_parent(commit, parent); - - must_pass(git_object_write((git_object *)commit)); - must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit)); git_repository_free(repo); @@ -509,6 +471,7 @@ BEGIN_SUITE(commit) ADD_TEST(parse1); ADD_TEST(parse2); ADD_TEST(details0); + ADD_TEST(write0); - ADD_TEST(write1); + //ADD_TEST(write1); END_SUITE diff --git a/tests/t05-revwalk.c b/tests/t05-revwalk.c index fd009fac1..cfcf01066 100644 --- a/tests/t05-revwalk.c +++ b/tests/t05-revwalk.c @@ -25,8 +25,6 @@ #include "test_lib.h" #include "test_helpers.h" -#include "revwalk.h" - /* $ git log --oneline --graph --decorate * a4a7dce (HEAD, br2) Merge branch 'master' into br2 @@ -70,12 +68,12 @@ static const int commit_sorting_time_reverse[][6] = { static const int result_bytes = 24; -static int get_commit_index(git_commit *commit) +static int get_commit_index(git_oid *raw_oid) { int i; char oid[40]; - git_oid_fmt(oid, &commit->object.id); + git_oid_fmt(oid, raw_oid); for (i = 0; i < commit_count; ++i) if (memcmp(oid, commit_ids[i], 40) == 0) @@ -84,23 +82,31 @@ static int get_commit_index(git_commit *commit) return -1; } -static int test_walk(git_revwalk *walk, git_commit *start_from, +static int test_walk(git_revwalk *walk, const git_oid *root, int flags, const int possible_results[][6], int results_count) { - git_commit *commit = NULL; + git_oid oid; int i; int result_array[commit_count]; git_revwalk_sorting(walk, flags); - git_revwalk_push(walk, start_from); + git_revwalk_push(walk, root); for (i = 0; i < commit_count; ++i) result_array[i] = -1; i = 0; - while (git_revwalk_next(&commit, walk) == GIT_SUCCESS) - result_array[i++] = get_commit_index(commit); + + while (git_revwalk_next(&oid, walk) == GIT_SUCCESS) { + result_array[i++] = get_commit_index(&oid); + /*{ + char str[41]; + git_oid_fmt(str, &oid); + str[40] = 0; + printf(" %d) %s\n", i, str); + }*/ + } for (i = 0; i < results_count; ++i) if (memcmp(possible_results[i], @@ -114,103 +120,21 @@ BEGIN_TEST(walk0, "do a simple walk on a repo with different sorting modes") git_oid id; git_repository *repo; git_revwalk *walk; - git_commit *head = NULL; must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - must_pass(git_revwalk_new(&walk, repo)); git_oid_mkstr(&id, commit_head); - must_pass(git_commit_lookup(&head, repo, &id)); - - must_pass(test_walk(walk, head, - GIT_SORT_TIME, - commit_sorting_time, 1)); - - must_pass(test_walk(walk, head, - GIT_SORT_TOPOLOGICAL, - commit_sorting_topo, 2)); - - must_pass(test_walk(walk, head, - GIT_SORT_TIME | GIT_SORT_REVERSE, - commit_sorting_time_reverse, 1)); - - must_pass(test_walk(walk, head, - GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, - commit_sorting_topo_reverse, 2)); - + must_pass(test_walk(walk, &id, GIT_SORT_TIME, commit_sorting_time, 1)); + must_pass(test_walk(walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2)); + must_pass(test_walk(walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1)); + must_pass(test_walk(walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2)); git_revwalk_free(walk); git_repository_free(repo); END_TEST -BEGIN_TEST(list0, "check that a commit list is properly sorted by time") - - git_revwalk_list list; - git_revwalk_listnode *n; - int i, t; - time_t previous_time; - -#define TEST_SORTED() \ - previous_time = INT_MAX;\ - for (n = list.head; n != NULL; n = n->next) {\ - must_be_true(n->walk_commit->commit_object->committer->when.time <= previous_time);\ - previous_time = n->walk_commit->commit_object->committer->when.time;\ - } - -#define CLEAR_LIST() \ - for (n = list.head; n != NULL; n = n->next) {\ - git_signature_free(n->walk_commit->commit_object->committer);\ - free(n->walk_commit->commit_object);\ - free(n->walk_commit);\ - }\ - git_revwalk_list_clear(&list); - - memset(&list, 0x0, sizeof(git_revwalk_list)); - srand((unsigned int)time(NULL)); - - for (t = 0; t < 20; ++t) { - const int test_size = rand() % 500 + 500; - - /* Purely random sorting test */ - for (i = 0; i < test_size; ++i) { - git_commit *c = git__malloc(sizeof(git_commit)); - git_revwalk_commit *rc = git__malloc(sizeof(git_revwalk_commit)); - - c->committer = git_signature_new("", "", (time_t)rand(), 0); - rc->commit_object = c; - - git_revwalk_list_push_back(&list, rc); - } - - git_revwalk_list_timesort(&list); - TEST_SORTED(); - CLEAR_LIST(); - } - - /* Try to sort list with all dates equal. */ - for (i = 0; i < 200; ++i) { - git_commit *c = git__malloc(sizeof(git_commit)); - git_revwalk_commit *rc = git__malloc(sizeof(git_revwalk_commit)); - - c->committer = git_signature_new("", "", 0, 0); - rc->commit_object = c; - - git_revwalk_list_push_back(&list, rc); - } - - git_revwalk_list_timesort(&list); - TEST_SORTED(); - CLEAR_LIST(); - - /* Try to sort empty list */ - git_revwalk_list_timesort(&list); - TEST_SORTED(); - -END_TEST - BEGIN_SUITE(revwalk) ADD_TEST(walk0); - ADD_TEST(list0); END_SUITE diff --git a/tests/t06-index.c b/tests/t06-index.c index 19b4da5c2..93ca2c04e 100644 --- a/tests/t06-index.c +++ b/tests/t06-index.c @@ -34,7 +34,7 @@ struct test_entry { unsigned int index; char path[128]; git_off_t file_size; - time_t mtime; + git_time_t mtime; }; struct test_entry TEST_ENTRIES[] = { diff --git a/tests/t08-tag.c b/tests/t08-tag.c index 0afdf719d..70eeb28a6 100644 --- a/tests/t08-tag.c +++ b/tests/t08-tag.c @@ -48,12 +48,12 @@ BEGIN_TEST(read0, "read and parse a tag from the repository") must_be_true(strcmp(git_tag_name(tag1), "test") == 0); must_be_true(git_tag_type(tag1) == GIT_OBJ_TAG); - tag2 = (git_tag *)git_tag_target(tag1); + must_pass(git_tag_target((git_object **)&tag2, tag1)); must_be_true(tag2 != NULL); must_be_true(git_oid_cmp(&id2, git_tag_id(tag2)) == 0); - commit = (git_commit *)git_tag_target(tag2); + must_pass(git_tag_target((git_object **)&commit, tag2)); must_be_true(commit != NULL); must_be_true(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0); @@ -61,27 +61,62 @@ BEGIN_TEST(read0, "read and parse a tag from the repository") git_repository_free(repo); END_TEST -BEGIN_TEST(write0, "write back a tag to the repository") - git_oid id; + +#define TAGGER_NAME "Vicent Marti" +#define TAGGER_EMAIL "vicent@github.com" +#define TAGGER_MESSAGE "This is my tag.\n\nThere are many tags, but this one is mine\n" + +BEGIN_TEST(write0, "write a tag to the repository and read it again") git_repository *repo; git_tag *tag; + git_oid target_id, tag_id; + const git_signature *tagger; + git_reference *ref_tag; + /* char hex_oid[41]; */ must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - git_oid_mkstr(&id, tag1_id); + git_oid_mkstr(&target_id, tagged_commit); + + /* create signatures */ + tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60); + must_be_true(tagger != NULL); + + must_pass(git_tag_create( + &tag_id, /* out id */ + repo, + "the-tag", /* do not update the HEAD */ + &target_id, + GIT_OBJ_COMMIT, + tagger, + TAGGER_MESSAGE)); - must_pass(git_tag_lookup(&tag, repo, &id)); + git_signature_free((git_signature *)tagger); - git_tag_set_name(tag, "This is a different tag LOL"); + must_pass(git_tag_lookup(&tag, repo, &tag_id)); + + /* Check attributes were set correctly */ + tagger = git_tag_tagger(tag); + must_be_true(tagger != NULL); + must_be_true(strcmp(tagger->name, TAGGER_NAME) == 0); + must_be_true(strcmp(tagger->email, TAGGER_EMAIL) == 0); + must_be_true(tagger->when.time == 123456789); + must_be_true(tagger->when.offset == 60); + + must_be_true(strcmp(git_tag_message(tag), TAGGER_MESSAGE) == 0); + + must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/the-tag")); + must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0); + must_pass(git_reference_delete(ref_tag)); - must_pass(git_object_write((git_object *)tag)); must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag)); git_repository_free(repo); + END_TEST BEGIN_SUITE(tag) ADD_TEST(read0); - ADD_TEST(write0); + ADD_TEST(write0); END_SUITE diff --git a/tests/t09-tree.c b/tests/t09-tree.c index 6bc2a84bd..6c1b2e643 100644 --- a/tests/t09-tree.c +++ b/tests/t09-tree.c @@ -66,98 +66,24 @@ BEGIN_TEST(read1, "read a tree from the repository") must_be_true(git_tree_entrycount(tree) == 3); + /* GH-86: git_object_lookup() should also check the type if the object comes from the cache */ + must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_TREE) == 0); + must_be_true(git_object_lookup(&obj, repo, &id, GIT_OBJ_BLOB) == GIT_EINVALIDTYPE); + entry = git_tree_entry_byname(tree, "README"); must_be_true(entry != NULL); must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0); - must_pass(git_tree_entry_2object(&obj, entry)); - - git_repository_free(repo); -END_TEST - -BEGIN_TEST(write0, "add a new entry to a tree and write it back to disk") - const unsigned int entry_count = 128; - - git_repository *repo; - git_tree *tree; - unsigned int i; - git_oid entry_id; - - must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - must_pass(git_tree_new(&tree, repo)); - - git_oid_mkstr(&entry_id, tree_oid); - for (i = 0; i < entry_count; ++i) { - char filename[32]; - git_tree_entry *ent = NULL; - - sprintf(filename, "file%d.txt", i); - must_pass(git_tree_add_entry(&ent, tree, &entry_id, filename, 040000)); - must_be_true(ent != NULL); - } - - must_be_true(git_tree_entrycount(tree) == entry_count); - must_pass(git_object_write((git_object *)tree)); - must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree)); + must_pass(git_tree_entry_2object(&obj, repo, entry)); git_repository_free(repo); END_TEST -BEGIN_TEST(write1, "add several entries in-memory and validate that they exist; write back to disk") - git_oid id; - git_repository *repo; - git_tree *tree; - git_tree_entry *entry; - unsigned int i; - /* char hex_oid[41]; */ - - must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); - - git_oid_mkstr(&id, tree_oid); - - must_pass(git_tree_lookup(&tree, repo, &id)); - - must_be_true(git_tree_entrycount(tree) == 3); - - /* check there is NP if we don't want the - * created entry back */ - git_tree_add_entry(NULL, tree, &id, "zzz_test_entry.dat", 0); - git_tree_add_entry(NULL, tree, &id, "01_test_entry.txt", 0); - - must_be_true(git_tree_entrycount(tree) == 5); - - entry = git_tree_entry_byindex(tree, 0); - must_be_true(strcmp(git_tree_entry_name(entry), "01_test_entry.txt") == 0); - - entry = git_tree_entry_byindex(tree, 4); - must_be_true(strcmp(git_tree_entry_name(entry), "zzz_test_entry.dat") == 0); - - must_pass(git_tree_remove_entry_byname(tree, "README")); - must_be_true(git_tree_entrycount(tree) == 4); - - for (i = 0; i < git_tree_entrycount(tree); ++i) { - entry = git_tree_entry_byindex(tree, i); - must_be_true(strcmp(git_tree_entry_name(entry), "README") != 0); - } - - must_pass(git_object_write((git_object *)tree)); - -/* - git_oid_fmt(hex_oid, git_tree_id(tree)); - hex_oid[40] = 0; - printf("TREE New SHA1: %s\n", hex_oid); -*/ - - must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree)); - git_repository_free(repo); -END_TEST - - BEGIN_SUITE(tree) ADD_TEST(read0); ADD_TEST(read1); - ADD_TEST(write0); - ADD_TEST(write1); +// ADD_TEST(write0); /* TODO THREADSAFE */ +// ADD_TEST(write1); END_SUITE diff --git a/tests/t10-refs.c b/tests/t10-refs.c index abe364133..413811c9d 100644 --- a/tests/t10-refs.c +++ b/tests/t10-refs.c @@ -295,6 +295,118 @@ BEGIN_TEST(create2, "create a new OID reference") must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */ END_TEST +static const char *ref_name = "refs/heads/other"; +static const char *ref_master_name = "refs/heads/master"; +static const char *ref_branch_name = "refs/heads/branch"; +static const char *ref_test_name = "refs/heads/test"; +BEGIN_TEST(overwrite0, "Overwrite an existing symbolic reference") + git_reference *ref, *branch_ref; + git_repository *repo; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + + /* The target needds to exist and we need to check the name has changed */ + must_pass(git_reference_create_symbolic(&branch_ref, repo, ref_branch_name, ref_master_name)); + must_pass(git_reference_create_symbolic(&ref, repo, ref_name, ref_branch_name)); + /* Ensure it points to the right place*/ + must_pass(git_reference_lookup(&ref, repo, ref_name)); + must_be_true(git_reference_type(ref) & GIT_REF_SYMBOLIC); + must_be_true(!strcmp(git_reference_target(ref), ref_branch_name)); + + /* Ensure we can't create it unless we force it to */ + must_fail(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name)); + must_pass(git_reference_create_symbolic_f(&ref, repo, ref_name, ref_master_name)); + + /* Ensure it points to the right place */ + must_pass(git_reference_lookup(&ref, repo, ref_name)); + must_be_true(git_reference_type(ref) & GIT_REF_SYMBOLIC); + must_be_true(!strcmp(git_reference_target(ref), ref_master_name)); + + must_pass(git_reference_delete(ref)); + must_pass(git_reference_delete(branch_ref)); + git_repository_free(repo); +END_TEST + +BEGIN_TEST(overwrite1, "Overwrite an existing object id reference") + git_reference *ref; + git_repository *repo; + git_oid id; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + + must_pass(git_reference_lookup(&ref, repo, ref_master_name)); + must_be_true(ref->type & GIT_REF_OID); + git_oid_cpy(&id, git_reference_oid(ref)); + + /* Create it */ + must_pass(git_reference_create_oid(&ref, repo, ref_name, &id)); + + must_pass(git_reference_lookup(&ref, repo, ref_test_name)); + must_be_true(ref->type & GIT_REF_OID); + git_oid_cpy(&id, git_reference_oid(ref)); + + /* Ensure we can't overwrite unless we force it */ + must_fail(git_reference_create_oid(&ref, repo, ref_name, &id)); + must_pass(git_reference_create_oid_f(&ref, repo, ref_name, &id)); + + /* Ensure it has been overwritten */ + must_pass(git_reference_lookup(&ref, repo, ref_name)); + must_be_true(!git_oid_cmp(&id, git_reference_oid(ref))); + + git_reference_delete(ref); + git_repository_free(repo); +END_TEST + +BEGIN_TEST(overwrite2, "Overwrite an existing object id reference with a symbolic one") + git_reference *ref; + git_repository *repo; + git_oid id; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + + must_pass(git_reference_lookup(&ref, repo, ref_master_name)); + must_be_true(ref->type & GIT_REF_OID); + git_oid_cpy(&id, git_reference_oid(ref)); + + must_pass(git_reference_create_oid(&ref, repo, ref_name, &id)); + must_fail(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name)); + must_pass(git_reference_create_symbolic_f(&ref, repo, ref_name, ref_master_name)); + + /* Ensure it points to the right place */ + must_pass(git_reference_lookup(&ref, repo, ref_name)); + must_be_true(git_reference_type(ref) & GIT_REF_SYMBOLIC); + must_be_true(!strcmp(git_reference_target(ref), ref_master_name)); + + git_reference_delete(ref); + git_repository_free(repo); +END_TEST + +BEGIN_TEST(overwrite3, "Overwrite an existing symbolic reference with an object id one") + git_reference *ref; + git_repository *repo; + git_oid id; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + + must_pass(git_reference_lookup(&ref, repo, ref_master_name)); + must_be_true(ref->type & GIT_REF_OID); + git_oid_cpy(&id, git_reference_oid(ref)); + + /* Create the symbolic ref */ + must_pass(git_reference_create_symbolic(&ref, repo, ref_name, ref_master_name)); + /* It shouldn't overwrite unless we tell it to */ + must_fail(git_reference_create_oid(&ref, repo, ref_name, &id)); + must_pass(git_reference_create_oid_f(&ref, repo, ref_name, &id)); + + /* Ensure it points to the right place */ + must_pass(git_reference_lookup(&ref, repo, ref_name)); + must_be_true(git_reference_type(ref) & GIT_REF_OID); + must_be_true(!git_oid_cmp(git_reference_oid(ref), &id)); + + git_reference_delete(ref); + git_repository_free(repo); +END_TEST + BEGIN_TEST(pack0, "create a packfile for an empty folder") git_repository *repo; char temp_path[GIT_PATH_MAX]; @@ -500,6 +612,25 @@ BEGIN_TEST(rename4, "can not rename a reference with an invalid name") close_temp_repo(repo); END_TEST +BEGIN_TEST(rename5, "can force-rename a reference with the name of an existing reference") + git_reference *looked_up_ref; + git_repository *repo; + + must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); + + /* An existing reference... */ + must_pass(git_reference_lookup(&looked_up_ref, repo, packed_head_name)); + + /* Can not be renamed to the name of another existing reference. */ + must_pass(git_reference_rename_f(looked_up_ref, packed_test_head_name)); + + /* Check we actually renamed it */ + must_pass(git_reference_lookup(&looked_up_ref, repo, packed_test_head_name)); + must_be_true(!strcmp(looked_up_ref->name, packed_test_head_name)); + + close_temp_repo(repo); +END_TEST + BEGIN_TEST(delete0, "deleting a ref which is both packed and loose should remove both tracks in the filesystem") git_reference *looked_up_ref, *another_looked_up_ref; git_repository *repo; @@ -560,7 +691,8 @@ BEGIN_TEST(normalize0, "normalize a direct (OID) reference name") must_fail(ensure_refname_normalized(OID_REF, "refs/heads/a/", NULL)); must_fail(ensure_refname_normalized(OID_REF, "refs/heads/a.", NULL)); must_fail(ensure_refname_normalized(OID_REF, "refs/heads/a.lock", NULL)); - must_fail(ensure_refname_normalized(OID_REF, "refs/dummy/a", NULL)); + must_pass(ensure_refname_normalized(OID_REF, "refs/dummy/a", NULL)); + must_pass(ensure_refname_normalized(OID_REF, "refs/stash", NULL)); must_pass(ensure_refname_normalized(OID_REF, "refs/tags/a", "refs/tags/a")); must_pass(ensure_refname_normalized(OID_REF, "refs/heads/a/b", "refs/heads/a/b")); must_pass(ensure_refname_normalized(OID_REF, "refs/heads/a./b", "refs/heads/a./b")); @@ -710,6 +842,40 @@ BEGIN_TEST(normalize2, "tests borrowed from JGit") must_fail(ensure_refname_normalized(SYM_REF, "refs/heads/master@{1.hour.ago}", NULL)); END_TEST +BEGIN_TEST(list0, "try to list all the references in our test repo") + git_repository *repo; + git_strarray ref_list; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + must_pass(git_reference_listall(&ref_list, repo, GIT_REF_LISTALL)); + + /*{ + unsigned short i; + for (i = 0; i < ref_list.count; ++i) + printf("# %s\n", ref_list.strings[i]); + }*/ + + /* We have exactly 7 refs in total if we include the packed ones: + * there is a reference that exists both in the packfile and as + * loose, but we only list it once */ + must_be_true(ref_list.count == 7); + + git_strarray_free(&ref_list); + git_repository_free(repo); +END_TEST + +BEGIN_TEST(list1, "try to list only the symbolic references") + git_repository *repo; + git_strarray ref_list; + + must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); + must_pass(git_reference_listall(&ref_list, repo, GIT_REF_SYMBOLIC)); + must_be_true(ref_list.count == 0); /* no symrefs in the test repo */ + + git_strarray_free(&ref_list); + git_repository_free(repo); +END_TEST + BEGIN_SUITE(refs) ADD_TEST(readtag0); @@ -727,6 +893,11 @@ BEGIN_SUITE(refs) ADD_TEST(create1); ADD_TEST(create2); + ADD_TEST(overwrite0); + ADD_TEST(overwrite1); + ADD_TEST(overwrite2); + ADD_TEST(overwrite3); + ADD_TEST(normalize0); ADD_TEST(normalize1); ADD_TEST(normalize2); @@ -739,6 +910,9 @@ BEGIN_SUITE(refs) ADD_TEST(rename2); ADD_TEST(rename3); ADD_TEST(rename4); + ADD_TEST(rename5); ADD_TEST(delete0); + ADD_TEST(list0); + ADD_TEST(list1); END_SUITE diff --git a/tests/t11-sqlite.c b/tests/t11-sqlite.c index 9e9d1b786..61ecf98ac 100644 --- a/tests/t11-sqlite.c +++ b/tests/t11-sqlite.c @@ -23,25 +23,30 @@ * Boston, MA 02110-1301, USA. */ #include "test_lib.h" -#include "t03-data.h" +#include "odb.h" +#ifdef GIT2_SQLITE_BACKEND +#include "t03-data.h" #include "fileops.h" #include "git2/odb_backend.h" -static int cmp_objects(git_rawobj *o1, git_rawobj *o2) + +static int cmp_objects(git_odb_object *odb_obj, git_rawobj *raw) { - if (o1->type != o2->type) + if (raw->type != git_odb_object_type(odb_obj)) return -1; - if (o1->len != o2->len) + + if (raw->len != git_odb_object_size(odb_obj)) return -1; - if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0)) + + if ((raw->len > 0) && (memcmp(raw->data, git_odb_object_data(odb_obj), raw->len) != 0)) return -1; + return 0; } static git_odb *open_sqlite_odb(void) { -#ifdef GIT2_SQLITE_BACKEND git_odb *odb; git_odb_backend *sqlite; @@ -55,23 +60,20 @@ static git_odb *open_sqlite_odb(void) return NULL; return odb; -#else - return NULL; -#endif } #define TEST_WRITE(PTR) {\ git_odb *db; \ git_oid id1, id2; \ - git_rawobj obj; \ + git_odb_object *obj; \ db = open_sqlite_odb(); \ must_be_true(db != NULL); \ must_pass(git_oid_mkstr(&id1, PTR.id)); \ - must_pass(git_odb_write(&id2, db, &PTR##_obj)); \ + must_pass(git_odb_write(&id2, db, PTR##_obj.data, PTR##_obj.len, PTR##_obj.type)); \ must_be_true(git_oid_cmp(&id1, &id2) == 0); \ must_pass(git_odb_read(&obj, db, &id1)); \ - must_pass(cmp_objects(&obj, &PTR##_obj)); \ - git_rawobj_close(&obj); \ + must_pass(cmp_objects(obj, &PTR##_obj)); \ + git_odb_object_close(obj); \ git_odb_close(db); \ } @@ -105,7 +107,6 @@ END_TEST BEGIN_SUITE(sqlite) -#ifdef GIT2_SQLITE_BACKEND ADD_TEST(sqlite0); ADD_TEST(sqlite1); ADD_TEST(sqlite2); @@ -113,5 +114,13 @@ BEGIN_SUITE(sqlite) ADD_TEST(sqlite4); ADD_TEST(sqlite5); ADD_TEST(sqlite6); -#endif END_SUITE + +#else /* no sqlite builtin */ +BEGIN_SUITE(sqlite) + /* empty */ +END_SUITE +#endif + + + diff --git a/tests/t12-repo.c b/tests/t12-repo.c index a9a93d147..adf20cfd7 100644 --- a/tests/t12-repo.c +++ b/tests/t12-repo.c @@ -113,22 +113,18 @@ static int ensure_repository_init( return GIT_ERROR; if (repo->path_workdir != NULL || expected_working_directory != NULL) { - if (strcmp(repo->path_workdir, expected_working_directory) != 0) - //return GIT_ERROR; + if (git__suffixcmp(repo->path_workdir, expected_working_directory) != 0) goto cleanup; } - if (strcmp(repo->path_odb, path_odb) != 0) - //return GIT_ERROR; + if (git__suffixcmp(repo->path_odb, path_odb) != 0) goto cleanup; - if (strcmp(repo->path_repository, expected_path_repository) != 0) - //return GIT_ERROR; + if (git__suffixcmp(repo->path_repository, expected_path_repository) != 0) goto cleanup; if (repo->path_index != NULL || expected_path_index != NULL) { - if (strcmp(repo->path_index, expected_path_index) != 0) - //return GIT_ERROR; + if (git__suffixcmp(repo->path_index, expected_path_index) != 0) goto cleanup; } @@ -162,11 +158,100 @@ BEGIN_TEST(init1, "initialize a bare repo") must_pass(ensure_repository_init(TEMP_REPO_FOLDER_NS, BARE_REPOSITORY, NULL, path_repository, NULL)); END_TEST +BEGIN_TEST(init2, "Initialize and open a bare repo with a relative path escaping out of the current working directory") + char path_repository[GIT_PATH_MAX]; + char current_workdir[GIT_PATH_MAX]; + const int mode = 0755; /* or 0777 ? */ + git_repository* repo; + + must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir))); + + git__joinpath(path_repository, TEMP_REPO_FOLDER, "a/b/c/"); + must_pass(gitfo_mkdir_recurs(path_repository, mode)); + + must_pass(chdir(path_repository)); + + must_pass(git_repository_init(&repo, "../d/e.git", 1)); + must_pass(git__suffixcmp(repo->path_repository, "/a/b/d/e.git/")); + + git_repository_free(repo); + + must_pass(git_repository_open(&repo, "../d/e.git")); + + git_repository_free(repo); + + must_pass(chdir(current_workdir)); + rmdir_recurs(TEMP_REPO_FOLDER); +END_TEST + +#define EMPTY_BARE_REPOSITORY_NAME "empty_bare.git" +#define EMPTY_BARE_REPOSITORY_FOLDER TEST_RESOURCES "/" EMPTY_BARE_REPOSITORY_NAME "/" + +BEGIN_TEST(open0, "Open a bare repository that has just been initialized by git") + git_repository *repo; + + must_pass(copydir_recurs(EMPTY_BARE_REPOSITORY_FOLDER, TEMP_REPO_FOLDER)); + must_pass(remove_placeholders(TEMP_REPO_FOLDER, "dummy-marker.txt")); + + must_pass(git_repository_open(&repo, TEMP_REPO_FOLDER)); + + git_repository_free(repo); + must_pass(rmdir_recurs(TEMP_REPO_FOLDER)); +END_TEST + +#define SOURCE_EMPTY_REPOSITORY_NAME "empty_standard_repo/.gitted" +#define EMPTY_REPOSITORY_NAME "empty_standard_repo/.git" +#define EMPTY_REPOSITORY_FOLDER TEST_RESOURCES "/" SOURCE_EMPTY_REPOSITORY_NAME "/" +#define DEST_REPOSITORY_FOLDER TEMP_REPO_FOLDER DOT_GIT "/" + +BEGIN_TEST(open1, "Open a standard repository that has just been initialized by git") + git_repository *repo; + + must_pass(copydir_recurs(EMPTY_REPOSITORY_FOLDER, DEST_REPOSITORY_FOLDER)); + must_pass(remove_placeholders(DEST_REPOSITORY_FOLDER, "dummy-marker.txt")); + + must_pass(git_repository_open(&repo, DEST_REPOSITORY_FOLDER)); + + git_repository_free(repo); + must_pass(rmdir_recurs(TEMP_REPO_FOLDER)); +END_TEST + + +BEGIN_TEST(open2, "Open a bare repository with a relative path escaping out of the current working directory") + char new_current_workdir[GIT_PATH_MAX]; + char current_workdir[GIT_PATH_MAX]; + char path_repository[GIT_PATH_MAX]; + + const int mode = 0755; /* or 0777 ? */ + git_repository* repo; + + /* Setup the repository to open */ + must_pass(gitfo_getcwd(current_workdir, sizeof(current_workdir))); + strcpy(path_repository, current_workdir); + git__joinpath_n(path_repository, 3, path_repository, TEMP_REPO_FOLDER, "a/d/e.git"); + must_pass(copydir_recurs(REPOSITORY_FOLDER, path_repository)); + + /* Change the current working directory */ + git__joinpath(new_current_workdir, TEMP_REPO_FOLDER, "a/b/c/"); + must_pass(gitfo_mkdir_recurs(new_current_workdir, mode)); + must_pass(chdir(new_current_workdir)); + + must_pass(git_repository_open(&repo, "../../d/e.git")); + + git_repository_free(repo); + + must_pass(chdir(current_workdir)); + rmdir_recurs(TEMP_REPO_FOLDER); +END_TEST BEGIN_SUITE(repository) ADD_TEST(odb0); ADD_TEST(odb1); ADD_TEST(init0); ADD_TEST(init1); + ADD_TEST(init2); + ADD_TEST(open0); + ADD_TEST(open1); + ADD_TEST(open2); END_SUITE diff --git a/tests/t13-threads.c b/tests/t13-threads.c new file mode 100644 index 000000000..3888b70ce --- /dev/null +++ b/tests/t13-threads.c @@ -0,0 +1,41 @@ +/* + * 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 "test_helpers.h" +#include "cache.h" + + +typedef struct { + git_cached_obj cached; + unsigned int __dummy; +} ttest_obj; + +BEGIN_TEST(cache0, "run several threads polling the cache at the same time") + +END_TEST + +BEGIN_SUITE(threads) + ADD_TEST(cache0); +END_SUITE diff --git a/tests/test_helpers.c b/tests/test_helpers.c index 588461135..760de238b 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -141,7 +141,7 @@ int copy_file(const char *src, const char *dst) if (gitfo_read_file(&source_buf, src) < GIT_SUCCESS) return GIT_ENOTFOUND; - dst_fd = gitfo_creat(dst, 0644); + dst_fd = gitfo_creat_force(dst, 0644); if (dst_fd < 0) goto cleanup; @@ -211,18 +211,13 @@ typedef struct { static int copy_filesystem_element_recurs(void *_data, char *source) { - const int mode = 0755; /* or 0777 ? */ copydir_data *data = (copydir_data *)_data; data->dst[data->dst_len] = 0; git__joinpath(data->dst, data->dst, source + data->src_len); - if (gitfo_isdir(source) == GIT_SUCCESS) { - if (gitfo_mkdir(data->dst, mode) < GIT_SUCCESS) - return GIT_EOSERR; - + if (gitfo_isdir(source) == GIT_SUCCESS) return gitfo_dirent(source, GIT_PATH_MAX, copy_filesystem_element_recurs, _data); - } return copy_file(source, data->dst); } @@ -261,3 +256,31 @@ void close_temp_repo(git_repository *repo) git_repository_free(repo); rmdir_recurs(TEMP_REPO_FOLDER); } + +static int remove_placeholders_recurs(void *filename, char *path) +{ + char passed_filename[GIT_PATH_MAX]; + char *data = (char *)filename; + + if (!gitfo_isdir(path)) + return gitfo_dirent(path, GIT_PATH_MAX, remove_placeholders_recurs, data); + + if (git__basename_r(passed_filename, sizeof(passed_filename), path) < GIT_SUCCESS) + return GIT_EINVALIDPATH; + + if (!strcmp(data, passed_filename)) + return gitfo_unlink(path); + + return GIT_SUCCESS; +} + +int remove_placeholders(char *directory_path, char *filename) +{ + char buffer[GIT_PATH_MAX]; + + if (gitfo_isdir(directory_path)) + return GIT_EINVALIDPATH; + + strcpy(buffer, directory_path); + return remove_placeholders_recurs(filename, buffer); +} diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 97b81ab40..19c8ae55c 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -29,6 +29,8 @@ #include "test_lib.h" #include <git2.h> +#include "odb.h" + #define TEST_REPOSITORY_NAME "testrepo.git" #define REPOSITORY_FOLDER TEST_RESOURCES "/" TEST_REPOSITORY_NAME "/" #define ODB_FOLDER (REPOSITORY_FOLDER "objects/") @@ -65,6 +67,7 @@ extern int cmp_files(const char *a, const char *b); extern int copy_file(const char *source, const char *dest); extern int rmdir_recurs(const char *directory_path); extern int copydir_recurs(const char *source_directory_path, const char *destination_directory_path); +extern int remove_placeholders(char *directory_path, char *filename); extern int open_temp_repo(git_repository **repo, const char *path); extern void close_temp_repo(git_repository *repo); diff --git a/tests/test_main.c b/tests/test_main.c index 9308b8d45..f2a623a48 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -42,6 +42,7 @@ DECLARE_SUITE(tree); DECLARE_SUITE(refs); DECLARE_SUITE(sqlite); DECLARE_SUITE(repository); +DECLARE_SUITE(threads); static libgit2_suite suite_methods[]= { SUITE_NAME(core), @@ -57,6 +58,7 @@ static libgit2_suite suite_methods[]= { SUITE_NAME(refs), SUITE_NAME(sqlite), SUITE_NAME(repository), + SUITE_NAME(threads), }; #define GIT_SUITE_COUNT (ARRAY_SIZE(suite_methods)) |
