diff options
Diffstat (limited to 'tests-clar/core')
-rw-r--r-- | tests-clar/core/buffer.c | 389 | ||||
-rw-r--r-- | tests-clar/core/copy.c | 126 | ||||
-rw-r--r-- | tests-clar/core/env.c | 303 | ||||
-rw-r--r-- | tests-clar/core/errors.c | 29 | ||||
-rw-r--r-- | tests-clar/core/filebuf.c | 4 | ||||
-rw-r--r-- | tests-clar/core/mkdir.c | 182 | ||||
-rw-r--r-- | tests-clar/core/oid.c | 12 | ||||
-rw-r--r-- | tests-clar/core/opts.c | 30 | ||||
-rw-r--r-- | tests-clar/core/path.c | 62 | ||||
-rw-r--r-- | tests-clar/core/pool.c | 50 | ||||
-rw-r--r-- | tests-clar/core/rmdir.c | 42 | ||||
-rw-r--r-- | tests-clar/core/stat.c | 97 | ||||
-rw-r--r-- | tests-clar/core/vector.c | 84 |
13 files changed, 1393 insertions, 17 deletions
diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c index 6a718f459..3d8221e04 100644 --- a/tests-clar/core/buffer.c +++ b/tests-clar/core/buffer.c @@ -1,5 +1,8 @@ #include "clar_libgit2.h" #include "buffer.h" +#include "buf_text.h" +#include "hashsig.h" +#include "fileops.h" #define TESTSTR "Have you seen that? Have you seeeen that??" const char *test_string = TESTSTR; @@ -456,6 +459,9 @@ void test_core_buffer__8(void) git_buf_free(&a); + check_joinbuf_2(NULL, "", ""); + check_joinbuf_2(NULL, "a", "a"); + check_joinbuf_2(NULL, "/a", "/a"); check_joinbuf_2("", "", ""); check_joinbuf_2("", "a", "a"); check_joinbuf_2("", "/a", "/a"); @@ -576,38 +582,407 @@ void test_core_buffer__11(void) t.strings = t1; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t2; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "some"); t.strings = t3; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t4; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "happ"); t.strings = t5; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, "happ"); t.strings = t6; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); t.strings = t7; t.count = 3; - cl_git_pass(git_buf_common_prefix(&a, &t)); + cl_git_pass(git_buf_text_common_prefix(&a, &t)); cl_assert_equal_s(a.ptr, ""); git_buf_free(&a); } + +void test_core_buffer__rfind_variants(void) +{ + git_buf a = GIT_BUF_INIT; + ssize_t len; + + cl_git_pass(git_buf_sets(&a, "/this/is/it/")); + + len = (ssize_t)git_buf_len(&a); + + cl_assert(git_buf_rfind(&a, '/') == len - 1); + cl_assert(git_buf_rfind_next(&a, '/') == len - 4); + + cl_assert(git_buf_rfind(&a, 'i') == len - 3); + cl_assert(git_buf_rfind_next(&a, 'i') == len - 3); + + cl_assert(git_buf_rfind(&a, 'h') == 2); + cl_assert(git_buf_rfind_next(&a, 'h') == 2); + + cl_assert(git_buf_rfind(&a, 'q') == -1); + cl_assert(git_buf_rfind_next(&a, 'q') == -1); + + git_buf_free(&a); +} + +void test_core_buffer__puts_escaped(void) +{ + git_buf a = GIT_BUF_INIT; + + git_buf_clear(&a); + cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "", "")); + cl_assert_equal_s("this is a test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "t", "\\")); + cl_assert_equal_s("\\this is a \\tes\\t", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "i ", "__")); + cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_text_puts_escape_regex(&a, "^match\\s*[A-Z]+.*")); + cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr); + + git_buf_free(&a); +} + +static void assert_unescape(char *expected, char *to_unescape) { + git_buf buf = GIT_BUF_INIT; + + cl_git_pass(git_buf_sets(&buf, to_unescape)); + git_buf_text_unescape(&buf); + cl_assert_equal_s(expected, buf.ptr); + cl_assert_equal_sz(strlen(expected), buf.size); + + git_buf_free(&buf); +} + +void test_core_buffer__unescape(void) +{ + assert_unescape("Escaped\\", "Es\\ca\\ped\\"); + assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\"); + assert_unescape("\\", "\\"); + assert_unescape("\\", "\\\\"); + assert_unescape("", ""); +} + +void test_core_buffer__base64(void) +{ + git_buf buf = GIT_BUF_INIT; + + /* t h i s + * 0x 74 68 69 73 + * 0b 01110100 01101000 01101001 01110011 + * 0b 011101 000110 100001 101001 011100 110000 + * 0x 1d 06 21 29 1c 30 + * d G h p c w + */ + cl_git_pass(git_buf_put_base64(&buf, "this", 4)); + cl_assert_equal_s("dGhpcw==", buf.ptr); + + git_buf_clear(&buf); + cl_git_pass(git_buf_put_base64(&buf, "this!", 5)); + cl_assert_equal_s("dGhpcyE=", buf.ptr); + + git_buf_clear(&buf); + cl_git_pass(git_buf_put_base64(&buf, "this!\n", 6)); + cl_assert_equal_s("dGhpcyEK", buf.ptr); + + git_buf_free(&buf); +} + +void test_core_buffer__classify_with_utf8(void) +{ + char *data0 = "Simple text\n"; + size_t data0len = 12; + char *data1 = "Is that UTF-8 data I seeā¦\nYep!\n"; + size_t data1len = 31; + char *data2 = "Internal NUL!!!\000\n\nI see you!\n"; + size_t data2len = 29; + git_buf b; + + b.ptr = data0; b.size = b.asize = data0len; + cl_assert(!git_buf_text_is_binary(&b)); + cl_assert(!git_buf_text_contains_nul(&b)); + + b.ptr = data1; b.size = b.asize = data1len; + cl_assert(git_buf_text_is_binary(&b)); + cl_assert(!git_buf_text_contains_nul(&b)); + + b.ptr = data2; b.size = b.asize = data2len; + cl_assert(git_buf_text_is_binary(&b)); + cl_assert(git_buf_text_contains_nul(&b)); +} + +#define SIMILARITY_TEST_DATA_1 \ + "test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" \ + "is this enough?\nthere has to be enough data to fill the hash array!\n" \ + "Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" \ + "Let's make sure we've got plenty to go with here.\n smile \n" + +void test_core_buffer__similarity_metric(void) +{ + git_hashsig *a, *b; + git_buf buf = GIT_BUF_INIT; + int sim; + + /* in the first case, we compare data to itself and expect 100% match */ + + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + + cl_assert_equal_i(100, git_hashsig_compare(a, b)); + + git_hashsig_free(a); + git_hashsig_free(b); + + /* if we change just a single byte, how much does that change magnify? */ + + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + cl_git_pass(git_buf_sets(&buf, + "Test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" + "is this enough?\nthere has to be enough data to fill the hash array!\n" + "Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" + "Let's make sure we've got plenty to go with here.\n smile \n")); + cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + + sim = git_hashsig_compare(a, b); + + cl_assert(95 < sim && sim < 100); /* expect >95% similarity */ + + git_hashsig_free(a); + git_hashsig_free(b); + + /* let's try comparing data to a superset of itself */ + + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1 + "and if I add some more, it should still be pretty similar, yes?\n")); + cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + + sim = git_hashsig_compare(a, b); + + cl_assert(70 < sim && sim < 80); /* expect in the 70-80% similarity range */ + + git_hashsig_free(a); + git_hashsig_free(b); + + /* what if we keep about half the original data and add half new */ + + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + cl_git_pass(git_buf_sets(&buf, + "test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" + "is this enough?\nthere has to be enough data to fill the hash array!\n" + "okay, that's half the original\nwhat else can we add?\nmore data\n" + "one more line will complete this\nshort\nlines\ndon't\nmatter\n")); + cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + + sim = git_hashsig_compare(a, b); + + cl_assert(40 < sim && sim < 60); /* expect in the 40-60% similarity range */ + + git_hashsig_free(a); + git_hashsig_free(b); + + /* lastly, let's check that we can hash file content as well */ + + cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1)); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL)); + + cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH)); + cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1); + cl_git_pass(git_hashsig_create_fromfile( + &b, "scratch/testdata", GIT_HASHSIG_NORMAL)); + + cl_assert_equal_i(100, git_hashsig_compare(a, b)); + + git_hashsig_free(a); + git_hashsig_free(b); + + git_buf_free(&buf); + git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES); +} + + +void test_core_buffer__similarity_metric_whitespace(void) +{ + git_hashsig *a, *b; + git_buf buf = GIT_BUF_INIT; + int sim, i, j; + git_hashsig_option_t opt; + const char *tabbed = + " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n" + " separator = sep[s];\n" + " expect = expect_values[s];\n" + "\n" + " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n" + " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n" + " git_buf_join(&buf, separator, a[i], b[j]);\n" + " cl_assert_equal_s(*expect, buf.ptr);\n" + " expect++;\n" + " }\n" + " }\n" + " }\n"; + const char *spaced = + " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n" + " separator = sep[s];\n" + " expect = expect_values[s];\n" + "\n" + " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n" + " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n" + " git_buf_join(&buf, separator, a[i], b[j]);\n" + " cl_assert_equal_s(*expect, buf.ptr);\n" + " expect++;\n" + " }\n" + " }\n" + " }\n"; + const char *crlf_spaced2 = + " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n" + " separator = sep[s];\r\n" + " expect = expect_values[s];\r\n" + "\r\n" + " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n" + " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n" + " git_buf_join(&buf, separator, a[i], b[j]);\r\n" + " cl_assert_equal_s(*expect, buf.ptr);\r\n" + " expect++;\r\n" + " }\r\n" + " }\r\n" + " }\r\n"; + const char *text[3] = { tabbed, spaced, crlf_spaced2 }; + + /* let's try variations of our own code with whitespace changes */ + + for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) { + for (i = 0; i < 3; ++i) { + for (j = 0; j < 3; ++j) { + cl_git_pass(git_buf_sets(&buf, text[i])); + cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt)); + + cl_git_pass(git_buf_sets(&buf, text[j])); + cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt)); + + sim = git_hashsig_compare(a, b); + + if (opt == GIT_HASHSIG_NORMAL) { + if (i == j) + cl_assert_equal_i(100, sim); + else + cl_assert(sim < 30); /* expect pretty different */ + } else { + cl_assert_equal_i(100, sim); + } + + git_hashsig_free(a); + git_hashsig_free(b); + } + } + } + + git_buf_free(&buf); +} + +#define check_buf(expected,buf) do { \ + cl_assert_equal_s(expected, buf.ptr); \ + cl_assert_equal_sz(strlen(expected), buf.size); } while (0) + +void test_core_buffer__lf_and_crlf_conversions(void) +{ + git_buf src = GIT_BUF_INIT, tgt = GIT_BUF_INIT; + + /* LF source */ + + git_buf_sets(&src, "lf\nlf\nlf\nlf\n"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("lf\r\nlf\r\nlf\r\nlf\r\n", tgt); + + cl_assert_equal_i(GIT_ENOTFOUND, git_buf_text_crlf_to_lf(&tgt, &src)); + /* no conversion needed if all LFs already */ + + git_buf_sets(&src, "\nlf\nlf\nlf\nlf\nlf"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("\r\nlf\r\nlf\r\nlf\r\nlf\r\nlf", tgt); + + cl_assert_equal_i(GIT_ENOTFOUND, git_buf_text_crlf_to_lf(&tgt, &src)); + /* no conversion needed if all LFs already */ + + /* CRLF source */ + + git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n", tgt); + check_buf(src.ptr, tgt); + + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt); + + git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf", tgt); + check_buf(src.ptr, tgt); + + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt); + + /* CRLF in LF text */ + + git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("\r\nlf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\ncrlf\r\n", tgt); + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt); + + /* LF in CRLF text */ + + git_buf_sets(&src, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("\r\ncrlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf", tgt); + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt); + + /* bare CR test */ + + git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r"); + + cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src)); + check_buf("\rcrlf\r\nlf\r\nlf\r\ncr\rcrlf\r\nlf\r\ncr\r", tgt); + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt); + + git_buf_sets(&src, "\rcr\r"); + cl_assert_equal_i(GIT_ENOTFOUND, git_buf_text_lf_to_crlf(&tgt, &src)); + cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src)); + check_buf("\rcr\r", tgt); + + git_buf_free(&src); + git_buf_free(&tgt); +} diff --git a/tests-clar/core/copy.c b/tests-clar/core/copy.c new file mode 100644 index 000000000..c0c59c056 --- /dev/null +++ b/tests-clar/core/copy.c @@ -0,0 +1,126 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "path.h" +#include "posix.h" + +void test_core_copy__file(void) +{ + struct stat st; + const char *content = "This is some stuff to copy\n"; + + cl_git_mkfile("copy_me", content); + + cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664)); + + cl_git_pass(git_path_lstat("copy_me_two", &st)); + cl_assert(S_ISREG(st.st_mode)); + cl_assert(strlen(content) == (size_t)st.st_size); + + cl_git_pass(p_unlink("copy_me_two")); + cl_git_pass(p_unlink("copy_me")); +} + +void test_core_copy__file_in_dir(void) +{ + struct stat st; + const char *content = "This is some other stuff to copy\n"; + + cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", NULL, 0775, GIT_MKDIR_PATH)); + cl_git_mkfile("an_dir/in_a_dir/copy_me", content); + cl_assert(git_path_isdir("an_dir")); + + cl_git_pass(git_futils_mkpath2file + ("an_dir/second_dir/and_more/copy_me_two", 0775)); + + cl_git_pass(git_futils_cp + ("an_dir/in_a_dir/copy_me", + "an_dir/second_dir/and_more/copy_me_two", + 0664)); + + cl_git_pass(git_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st)); + cl_assert(S_ISREG(st.st_mode)); + cl_assert(strlen(content) == (size_t)st.st_size); + + cl_git_pass(git_futils_rmdir_r("an_dir", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_assert(!git_path_isdir("an_dir")); +} + +void test_core_copy__tree(void) +{ + struct stat st; + const char *content = "File content\n"; + + cl_git_pass(git_futils_mkdir("src/b", NULL, 0775, GIT_MKDIR_PATH)); + cl_git_pass(git_futils_mkdir("src/c/d", NULL, 0775, GIT_MKDIR_PATH)); + cl_git_pass(git_futils_mkdir("src/c/e", NULL, 0775, GIT_MKDIR_PATH)); + + cl_git_mkfile("src/f1", content); + cl_git_mkfile("src/b/f2", content); + cl_git_mkfile("src/c/f3", content); + cl_git_mkfile("src/c/d/f4", content); + cl_git_mkfile("src/c/d/.f5", content); + +#ifndef GIT_WIN32 + cl_assert(p_symlink("../../b/f2", "src/c/d/l1") == 0); +#endif + + cl_assert(git_path_isdir("src")); + cl_assert(git_path_isdir("src/b")); + cl_assert(git_path_isdir("src/c/d")); + cl_assert(git_path_isfile("src/c/d/f4")); + + /* copy with no empty dirs, yes links, no dotfiles, no overwrite */ + + cl_git_pass( + git_futils_cp_r("src", "t1", GIT_CPDIR_COPY_SYMLINKS, 0) ); + + cl_assert(git_path_isdir("t1")); + cl_assert(git_path_isdir("t1/b")); + cl_assert(git_path_isdir("t1/c")); + cl_assert(git_path_isdir("t1/c/d")); + cl_assert(!git_path_isdir("t1/c/e")); + + cl_assert(git_path_isfile("t1/f1")); + cl_assert(git_path_isfile("t1/b/f2")); + cl_assert(git_path_isfile("t1/c/f3")); + cl_assert(git_path_isfile("t1/c/d/f4")); + cl_assert(!git_path_isfile("t1/c/d/.f5")); + + cl_git_pass(git_path_lstat("t1/c/f3", &st)); + cl_assert(S_ISREG(st.st_mode)); + cl_assert(strlen(content) == (size_t)st.st_size); + +#ifndef GIT_WIN32 + cl_git_pass(git_path_lstat("t1/c/d/l1", &st)); + cl_assert(S_ISLNK(st.st_mode)); +#endif + + cl_git_pass(git_futils_rmdir_r("t1", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_assert(!git_path_isdir("t1")); + + /* copy with empty dirs, no links, yes dotfiles, no overwrite */ + + cl_git_pass( + git_futils_cp_r("src", "t2", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_COPY_DOTFILES, 0) ); + + cl_assert(git_path_isdir("t2")); + cl_assert(git_path_isdir("t2/b")); + cl_assert(git_path_isdir("t2/c")); + cl_assert(git_path_isdir("t2/c/d")); + cl_assert(git_path_isdir("t2/c/e")); + + cl_assert(git_path_isfile("t2/f1")); + cl_assert(git_path_isfile("t2/b/f2")); + cl_assert(git_path_isfile("t2/c/f3")); + cl_assert(git_path_isfile("t2/c/d/f4")); + cl_assert(git_path_isfile("t2/c/d/.f5")); + +#ifndef GIT_WIN32 + cl_git_fail(git_path_lstat("t2/c/d/l1", &st)); +#endif + + cl_git_pass(git_futils_rmdir_r("t2", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_assert(!git_path_isdir("t2")); + + cl_git_pass(git_futils_rmdir_r("src", NULL, GIT_RMDIR_REMOVE_FILES)); +} diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c new file mode 100644 index 000000000..0fa6472d7 --- /dev/null +++ b/tests-clar/core/env.c @@ -0,0 +1,303 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "path.h" + +#ifdef GIT_WIN32 +#define NUM_VARS 5 +static const char *env_vars[NUM_VARS] = { + "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES" +}; +#else +#define NUM_VARS 1 +static const char *env_vars[NUM_VARS] = { "HOME" }; +#endif + +static char *env_save[NUM_VARS]; + +static char *home_values[] = { + "fake_home", + "f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */ + "f\xc4\x80ke_\xc4\xa4ome", /* latin extended */ + "f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad", /* having fun with greek */ + "fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */ + "f\xe1\x9cx80ke_\xe1\x9c\x91ome", /* tagalog characters */ + "\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */ + "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */ + NULL +}; + +void test_core_env__initialize(void) +{ + int i; + for (i = 0; i < NUM_VARS; ++i) { + const char *original = cl_getenv(env_vars[i]); +#ifdef GIT_WIN32 + env_save[i] = (char *)original; +#else + env_save[i] = original ? git__strdup(original) : NULL; +#endif + } +} + +static void reset_global_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL)); +} + +static void reset_system_search_path(void) +{ + cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL)); +} + +void test_core_env__cleanup(void) +{ + int i; + char **val; + + for (i = 0; i < NUM_VARS; ++i) { + cl_setenv(env_vars[i], env_save[i]); + git__free(env_save[i]); + env_save[i] = NULL; + } + + /* these will probably have already been cleaned up, but if a test + * fails, then it's probably good to try and clear out these dirs + */ + for (val = home_values; *val != NULL; val++) { + if (**val != '\0') + (void)p_rmdir(*val); + } + + /* reset search paths to default */ + reset_global_search_path(); + reset_system_search_path(); +} + +static void setenv_and_check(const char *name, const char *value) +{ + char *check; + + cl_git_pass(cl_setenv(name, value)); + + check = cl_getenv(name); + cl_assert_equal_s(value, check); +#ifdef GIT_WIN32 + git__free(check); +#endif +} + +void test_core_env__0(void) +{ + git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; + char testfile[16], tidx = '0'; + char **val; + const char *testname = "testfile"; + size_t testlen = strlen(testname); + + strncpy(testfile, testname, sizeof(testfile)); + cl_assert_equal_s(testname, testfile); + + for (val = home_values; *val != NULL; val++) { + + /* if we can't make the directory, let's just assume + * we are on a filesystem that doesn't support the + * characters in question and skip this test... + */ + if (p_mkdir(*val, 0777) != 0) { + *val = ""; /* mark as not created */ + continue; + } + + cl_git_pass(git_path_prettify(&path, *val, NULL)); + + /* vary testfile name in each directory so accidentally leaving + * an environment variable set from a previous iteration won't + * accidentally make this test pass... + */ + testfile[testlen] = tidx++; + cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); + cl_git_mkfile(path.ptr, "find me"); + git_buf_rtruncate_at_char(&path, '/'); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + setenv_and_check("HOME", path.ptr); + reset_global_search_path(); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + + cl_setenv("HOME", env_save[0]); + reset_global_search_path(); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + +#ifdef GIT_WIN32 + setenv_and_check("HOMEDRIVE", NULL); + setenv_and_check("HOMEPATH", NULL); + setenv_and_check("USERPROFILE", path.ptr); + reset_global_search_path(); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + + { + int root = git_path_root(path.ptr); + char old; + + if (root >= 0) { + setenv_and_check("USERPROFILE", NULL); + reset_global_search_path(); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + old = path.ptr[root]; + path.ptr[root] = '\0'; + setenv_and_check("HOMEDRIVE", path.ptr); + path.ptr[root] = old; + setenv_and_check("HOMEPATH", &path.ptr[root]); + reset_global_search_path(); + + cl_git_pass(git_futils_find_global_file(&found, testfile)); + } + } +#endif + + (void)p_rmdir(*val); + } + + git_buf_free(&path); + git_buf_free(&found); +} + + +void test_core_env__1(void) +{ + git_buf path = GIT_BUF_INIT; + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + + cl_git_pass(cl_setenv("HOME", "doesnotexist")); +#ifdef GIT_WIN32 + cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist")); + cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist")); +#endif + reset_global_search_path(); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + + cl_git_pass(cl_setenv("HOME", NULL)); +#ifdef GIT_WIN32 + cl_git_pass(cl_setenv("HOMEPATH", NULL)); + cl_git_pass(cl_setenv("USERPROFILE", NULL)); +#endif + reset_global_search_path(); + reset_system_search_path(); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile")); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); + +#ifdef GIT_WIN32 + cl_git_pass(cl_setenv("PROGRAMFILES", NULL)); + reset_system_search_path(); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile")); +#endif + + git_buf_free(&path); +} + +static void check_global_searchpath( + const char *path, int position, const char *file, git_buf *temp) +{ + char out[GIT_PATH_MAX]; + + /* build and set new path */ + if (position < 0) + cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH")); + else if (position > 0) + cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path)); + else + cl_git_pass(git_buf_sets(temp, path)); + + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr)); + + /* get path and make sure $PATH expansion worked */ + cl_git_pass(git_libgit2_opts( + GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out))); + + if (position < 0) + cl_assert(git__prefixcmp(out, path) == 0); + else if (position > 0) + cl_assert(git__suffixcmp(out, path) == 0); + else + cl_assert_equal_s(out, path); + + /* find file using new path */ + cl_git_pass(git_futils_find_global_file(temp, file)); + + /* reset path and confirm file not found */ + cl_git_pass(git_libgit2_opts( + GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(temp, file)); +} + +void test_core_env__2(void) +{ + git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT; + char testfile[16], tidx = '0'; + char **val; + const char *testname = "alternate"; + size_t testlen = strlen(testname); + + strncpy(testfile, testname, sizeof(testfile)); + cl_assert_equal_s(testname, testfile); + + for (val = home_values; *val != NULL; val++) { + + /* if we can't make the directory, let's just assume + * we are on a filesystem that doesn't support the + * characters in question and skip this test... + */ + if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) { + *val = ""; /* mark as not created */ + continue; + } + + cl_git_pass(git_path_prettify(&path, *val, NULL)); + + /* vary testfile name so any sloppiness is resetting variables or + * deleting files won't accidentally make a test pass. + */ + testfile[testlen] = tidx++; + cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); + cl_git_mkfile(path.ptr, "find me"); + git_buf_rtruncate_at_char(&path, '/'); + + /* default should be NOTFOUND */ + cl_assert_equal_i( + GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile)); + + /* try plain, append $PATH, and prepend $PATH */ + check_global_searchpath(path.ptr, 0, testfile, &found); + check_global_searchpath(path.ptr, -1, testfile, &found); + check_global_searchpath(path.ptr, 1, testfile, &found); + + /* cleanup */ + cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile)); + (void)p_unlink(path.ptr); + (void)p_rmdir(*val); + } + + git_buf_free(&path); + git_buf_free(&found); +} diff --git a/tests-clar/core/errors.c b/tests-clar/core/errors.c index 0be3e7aca..512a4134d 100644 --- a/tests-clar/core/errors.c +++ b/tests-clar/core/errors.c @@ -1,4 +1,31 @@ #include "clar_libgit2.h" + +void test_core_errors__public_api(void) +{ + char *str_in_error; + + giterr_clear(); + cl_assert(giterr_last() == NULL); + + giterr_set_oom(); + + cl_assert(giterr_last() != NULL); + cl_assert(giterr_last()->klass == GITERR_NOMEMORY); + str_in_error = strstr(giterr_last()->message, "memory"); + cl_assert(str_in_error != NULL); + + giterr_clear(); + + giterr_set_str(GITERR_REPOSITORY, "This is a test"); + + cl_assert(giterr_last() != NULL); + str_in_error = strstr(giterr_last()->message, "This is a test"); + cl_assert(str_in_error != NULL); + + giterr_clear(); + cl_assert(giterr_last() == NULL); +} + #include "common.h" #include "util.h" #include "posix.h" @@ -31,7 +58,7 @@ void test_core_errors__new_school(void) do { struct stat st; memset(&st, 0, sizeof(st)); - assert(p_lstat("this_file_does_not_exist", &st) < 0); + cl_assert(p_lstat("this_file_does_not_exist", &st) < 0); GIT_UNUSED(st); } while (false); giterr_set(GITERR_OS, "stat failed"); /* internal fn */ diff --git a/tests-clar/core/filebuf.c b/tests-clar/core/filebuf.c index eab8a26eb..4451c01c7 100644 --- a/tests-clar/core/filebuf.c +++ b/tests-clar/core/filebuf.c @@ -8,7 +8,7 @@ void test_core_filebuf__0(void) int fd; char test[] = "test", testlock[] = "test.lock"; - fd = p_creat(testlock, 0744); + fd = p_creat(testlock, 0744); //-V536 cl_must_pass(fd); cl_must_pass(p_close(fd)); @@ -27,7 +27,7 @@ void test_core_filebuf__1(void) int fd; char test[] = "test"; - fd = p_creat(test, 0666); + fd = p_creat(test, 0666); //-V536 cl_must_pass(fd); cl_must_pass(p_write(fd, "libgit2 rocks\n", 14)); cl_must_pass(p_close(fd)); diff --git a/tests-clar/core/mkdir.c b/tests-clar/core/mkdir.c new file mode 100644 index 000000000..1e50b4336 --- /dev/null +++ b/tests-clar/core/mkdir.c @@ -0,0 +1,182 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "path.h" +#include "posix.h" + +static void cleanup_basic_dirs(void *ref) +{ + GIT_UNUSED(ref); + git_futils_rmdir_r("d0", NULL, GIT_RMDIR_EMPTY_HIERARCHY); + git_futils_rmdir_r("d1", NULL, GIT_RMDIR_EMPTY_HIERARCHY); + git_futils_rmdir_r("d2", NULL, GIT_RMDIR_EMPTY_HIERARCHY); + git_futils_rmdir_r("d3", NULL, GIT_RMDIR_EMPTY_HIERARCHY); + git_futils_rmdir_r("d4", NULL, GIT_RMDIR_EMPTY_HIERARCHY); +} + +void test_core_mkdir__basic(void) +{ + cl_set_cleanup(cleanup_basic_dirs, NULL); + + /* make a directory */ + cl_assert(!git_path_isdir("d0")); + cl_git_pass(git_futils_mkdir("d0", NULL, 0755, 0)); + cl_assert(git_path_isdir("d0")); + + /* make a path */ + cl_assert(!git_path_isdir("d1")); + cl_git_pass(git_futils_mkdir("d1/d1.1/d1.2", NULL, 0755, GIT_MKDIR_PATH)); + cl_assert(git_path_isdir("d1")); + cl_assert(git_path_isdir("d1/d1.1")); + cl_assert(git_path_isdir("d1/d1.1/d1.2")); + + /* make a dir exclusively */ + cl_assert(!git_path_isdir("d2")); + cl_git_pass(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL)); + cl_assert(git_path_isdir("d2")); + + /* make exclusive failure */ + cl_git_fail(git_futils_mkdir("d2", NULL, 0755, GIT_MKDIR_EXCL)); + + /* make a path exclusively */ + cl_assert(!git_path_isdir("d3")); + cl_git_pass(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + cl_assert(git_path_isdir("d3")); + cl_assert(git_path_isdir("d3/d3.1/d3.2")); + + /* make exclusive path failure */ + cl_git_fail(git_futils_mkdir("d3/d3.1/d3.2", NULL, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + /* ??? Should EXCL only apply to the last item in the path? */ + + /* path with trailing slash? */ + cl_assert(!git_path_isdir("d4")); + cl_git_pass(git_futils_mkdir("d4/d4.1/", NULL, 0755, GIT_MKDIR_PATH)); + cl_assert(git_path_isdir("d4/d4.1")); +} + +static void cleanup_basedir(void *ref) +{ + GIT_UNUSED(ref); + git_futils_rmdir_r("base", NULL, GIT_RMDIR_EMPTY_HIERARCHY); +} + +void test_core_mkdir__with_base(void) +{ +#define BASEDIR "base/dir/here" + + cl_set_cleanup(cleanup_basedir, NULL); + + cl_git_pass(git_futils_mkdir(BASEDIR, NULL, 0755, GIT_MKDIR_PATH)); + + cl_git_pass(git_futils_mkdir("a", BASEDIR, 0755, 0)); + cl_assert(git_path_isdir(BASEDIR "/a")); + + cl_git_pass(git_futils_mkdir("b/b1/b2", BASEDIR, 0755, GIT_MKDIR_PATH)); + cl_assert(git_path_isdir(BASEDIR "/b/b1/b2")); + + /* exclusive with existing base */ + cl_git_pass(git_futils_mkdir("c/c1/c2", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + + /* fail: exclusive with duplicated suffix */ + cl_git_fail(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + + /* fail: exclusive with any duplicated component */ + cl_git_fail(git_futils_mkdir("c/cz/cz", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + + /* success: exclusive without path */ + cl_git_pass(git_futils_mkdir("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_EXCL)); + + /* path with shorter base and existing dirs */ + cl_git_pass(git_futils_mkdir("dir/here/d/", "base", 0755, GIT_MKDIR_PATH)); + cl_assert(git_path_isdir("base/dir/here/d")); + + /* fail: path with shorter base and existing dirs */ + cl_git_fail(git_futils_mkdir("dir/here/e/", "base", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL)); + + /* fail: base with missing components */ + cl_git_fail(git_futils_mkdir("f/", "base/missing", 0755, GIT_MKDIR_PATH)); + + /* success: shift missing component to path */ + cl_git_pass(git_futils_mkdir("missing/f/", "base/", 0755, GIT_MKDIR_PATH)); +} + +static void cleanup_chmod_root(void *ref) +{ + mode_t *mode = ref; + + if (*mode != 0) { + (void)p_umask(*mode); + git__free(mode); + } + + git_futils_rmdir_r("r", NULL, GIT_RMDIR_EMPTY_HIERARCHY); +} + +static void check_mode(mode_t expected, mode_t actual) +{ +#ifdef GIT_WIN32 + /* chmod on Win32 doesn't support exec bit, not group/world bits */ + cl_assert((expected & 0600) == (actual & 0777)); +#else + cl_assert(expected == (actual & 0777)); +#endif +} + +void test_core_mkdir__chmods(void) +{ + struct stat st; + mode_t *old = git__malloc(sizeof(mode_t)); + *old = p_umask(022); + + cl_set_cleanup(cleanup_chmod_root, old); + + cl_git_pass(git_futils_mkdir("r", NULL, 0777, 0)); + + cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH)); + + cl_git_pass(git_path_lstat("r/mode", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode/is", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode/is/important", &st)); + check_mode(0755, st.st_mode); + + cl_git_pass(git_futils_mkdir("mode2/is2/important2", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD)); + + cl_git_pass(git_path_lstat("r/mode2", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode2/is2", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode2/is2/important2", &st)); + check_mode(0777, st.st_mode); + + cl_git_pass(git_futils_mkdir("mode3/is3/important3", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH)); + + cl_git_pass(git_path_lstat("r/mode3", &st)); + check_mode(0777, st.st_mode); + cl_git_pass(git_path_lstat("r/mode3/is3", &st)); + check_mode(0777, st.st_mode); + cl_git_pass(git_path_lstat("r/mode3/is3/important3", &st)); + check_mode(0777, st.st_mode); + + /* test that we chmod existing dir */ + + cl_git_pass(git_futils_mkdir("mode/is/important", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD)); + + cl_git_pass(git_path_lstat("r/mode", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode/is", &st)); + check_mode(0755, st.st_mode); + cl_git_pass(git_path_lstat("r/mode/is/important", &st)); + check_mode(0777, st.st_mode); + + /* test that we chmod even existing dirs if CHMOD_PATH is set */ + + cl_git_pass(git_futils_mkdir("mode2/is2/important2.1", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH)); + + cl_git_pass(git_path_lstat("r/mode2", &st)); + check_mode(0777, st.st_mode); + cl_git_pass(git_path_lstat("r/mode2/is2", &st)); + check_mode(0777, st.st_mode); + cl_git_pass(git_path_lstat("r/mode2/is2/important2.1", &st)); + check_mode(0777, st.st_mode); +} diff --git a/tests-clar/core/oid.c b/tests-clar/core/oid.c index c89713955..08791cce6 100644 --- a/tests-clar/core/oid.c +++ b/tests-clar/core/oid.c @@ -1,11 +1,17 @@ #include "clar_libgit2.h" static git_oid id; +static git_oid idp; +static git_oid idm; const char *str_oid = "ae90f12eea699729ed24555e40b9fd669da12a12"; +const char *str_oid_p = "ae90f12eea699729ed"; +const char *str_oid_m = "ae90f12eea699729ed24555e40b9fd669da12a12THIS IS EXTRA TEXT THAT SHOULD GET IGNORED"; void test_core_oid__initialize(void) { cl_git_pass(git_oid_fromstr(&id, str_oid)); + cl_git_pass(git_oid_fromstrp(&idp, str_oid_p)); + cl_git_fail(git_oid_fromstrp(&idm, str_oid_m)); } void test_core_oid__streq(void) @@ -15,4 +21,10 @@ void test_core_oid__streq(void) cl_assert(git_oid_streq(&id, "deadbeef") == -1); cl_assert(git_oid_streq(&id, "I'm not an oid.... :)") == -1); + + cl_assert(git_oid_streq(&idp, "ae90f12eea699729ed0000000000000000000000") == 0); + cl_assert(git_oid_streq(&idp, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef") == -1); + + cl_assert(git_oid_streq(&idp, "deadbeef") == -1); + cl_assert(git_oid_streq(&idp, "I'm not an oid.... :)") == -1); } diff --git a/tests-clar/core/opts.c b/tests-clar/core/opts.c new file mode 100644 index 000000000..907339d51 --- /dev/null +++ b/tests-clar/core/opts.c @@ -0,0 +1,30 @@ +#include "clar_libgit2.h" +#include "cache.h" + +void test_core_opts__readwrite(void) +{ + size_t old_val = 0; + size_t new_val = 0; + + git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &old_val); + git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, (size_t)1234); + git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &new_val); + + cl_assert(new_val == 1234); + + git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, old_val); + git_libgit2_opts(GIT_OPT_GET_MWINDOW_SIZE, &new_val); + + cl_assert(new_val == old_val); + + git_libgit2_opts(GIT_OPT_GET_ODB_CACHE_SIZE, &old_val); + + cl_assert(old_val == GIT_DEFAULT_CACHE_SIZE); + + git_libgit2_opts(GIT_OPT_SET_ODB_CACHE_SIZE, (size_t)GIT_DEFAULT_CACHE_SIZE*2); + git_libgit2_opts(GIT_OPT_GET_ODB_CACHE_SIZE, &new_val); + + cl_assert(new_val == (GIT_DEFAULT_CACHE_SIZE*2)); + + git_libgit2_opts(GIT_OPT_GET_ODB_CACHE_SIZE, &old_val); +} diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c index d826612ac..407770baa 100644 --- a/tests-clar/core/path.c +++ b/tests-clar/core/path.c @@ -87,6 +87,15 @@ void test_core_path__00_dirname(void) check_dirname(".git/", "."); check_dirname(REP16("/abc"), REP15("/abc")); + +#ifdef GIT_WIN32 + check_dirname("C:/path/", "C:/"); + check_dirname("C:/path", "C:/"); + check_dirname("//computername/path/", "//computername/"); + check_dirname("//computername/path", "//computername/"); + check_dirname("//computername/sub/path/", "//computername/sub"); + check_dirname("//computername/sub/path", "//computername/sub"); +#endif } /* get the base name of a path */ @@ -315,7 +324,7 @@ static void check_fromurl(const char *expected_result, const char *input, int sh git_buf_free(&buf); } -#ifdef _MSC_VER +#ifdef GIT_WIN32 #define ABS_PATH_MARKER "" #else #define ABS_PATH_MARKER "/" @@ -418,3 +427,54 @@ void test_core_path__13_cannot_prettify_a_non_existing_file(void) git_buf_free(&p); } + +void test_core_path__14_apply_relative(void) +{ + git_buf p = GIT_BUF_INIT; + + cl_git_pass(git_buf_sets(&p, "/this/is/a/base")); + + cl_git_pass(git_path_apply_relative(&p, "../test")); + cl_assert_equal_s("/this/is/a/test", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../the/./end")); + cl_assert_equal_s("/this/is/the/end", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string")); + cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../..")); + cl_assert_equal_s("/this/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../")); + cl_assert_equal_s("/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../..")); + cl_assert_equal_s("/", p.ptr); + + + cl_git_pass(git_buf_sets(&p, "d:/another/test")); + + cl_git_pass(git_path_apply_relative(&p, "../../../../..")); + cl_assert_equal_s("d:/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/.")); + cl_assert_equal_s("d:/from/here/and/back/", p.ptr); + + + cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git")); + + cl_git_pass(git_path_apply_relative(&p, "../another.git")); + cl_assert_equal_s("https://my.url.com/another.git", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch")); + cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "..")); + cl_assert_equal_s("https://my.url.com/full/path/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../")); + cl_assert_equal_s("https://", p.ptr); + + git_buf_free(&p); +} diff --git a/tests-clar/core/pool.c b/tests-clar/core/pool.c index 5ed97366f..c42bb6da0 100644 --- a/tests-clar/core/pool.c +++ b/tests-clar/core/pool.c @@ -83,3 +83,53 @@ void test_core_pool__2(void) git_pool_clear(&p); } + +void test_core_pool__free_list(void) +{ + int i; + git_pool p; + void *ptr, *ptrs[50]; + + cl_git_pass(git_pool_init(&p, 100, 100)); + + for (i = 0; i < 10; ++i) { + ptr = git_pool_malloc(&p, 1); + cl_assert(ptr != NULL); + } + cl_assert_equal_i(10, (int)p.items); + + for (i = 0; i < 50; ++i) { + ptrs[i] = git_pool_malloc(&p, 1); + cl_assert(ptrs[i] != NULL); + } + cl_assert_equal_i(60, (int)p.items); + + git_pool_free(&p, ptr); + cl_assert_equal_i(60, (int)p.items); + + git_pool_free_array(&p, 50, ptrs); + cl_assert_equal_i(60, (int)p.items); + + for (i = 0; i < 50; ++i) { + ptrs[i] = git_pool_malloc(&p, 1); + cl_assert(ptrs[i] != NULL); + } + cl_assert_equal_i(60, (int)p.items); + + for (i = 0; i < 111; ++i) { + ptr = git_pool_malloc(&p, 1); + cl_assert(ptr != NULL); + } + cl_assert_equal_i(170, (int)p.items); + + git_pool_free_array(&p, 50, ptrs); + cl_assert_equal_i(170, (int)p.items); + + for (i = 0; i < 50; ++i) { + ptrs[i] = git_pool_malloc(&p, 1); + cl_assert(ptrs[i] != NULL); + } + cl_assert_equal_i(170, (int)p.items); + + git_pool_clear(&p); +} diff --git a/tests-clar/core/rmdir.c b/tests-clar/core/rmdir.c index 530f1f908..f0b0bfa42 100644 --- a/tests-clar/core/rmdir.c +++ b/tests-clar/core/rmdir.c @@ -30,7 +30,7 @@ void test_core_rmdir__initialize(void) /* make sure empty dir can be deleted recusively */ void test_core_rmdir__delete_recursive(void) { - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_EMPTY_HIERARCHY)); + cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); } /* make sure non-empty dir cannot be deleted recusively */ @@ -42,15 +42,15 @@ void test_core_rmdir__fail_to_delete_non_empty_dir(void) cl_git_mkfile(git_buf_cstr(&file), "dummy"); - cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_EMPTY_HIERARCHY)); + cl_git_fail(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); cl_must_pass(p_unlink(file.ptr)); - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_EMPTY_HIERARCHY)); + cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); git_buf_free(&file); } -void test_core_rmdir__can_skip__non_empty_dir(void) +void test_core_rmdir__can_skip_non_empty_dir(void) { git_buf file = GIT_BUF_INIT; @@ -58,11 +58,41 @@ void test_core_rmdir__can_skip__non_empty_dir(void) cl_git_mkfile(git_buf_cstr(&file), "dummy"); - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_ONLY_EMPTY_DIRS)); + cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_SKIP_NONEMPTY)); cl_assert(git_path_exists(git_buf_cstr(&file)) == true); - cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, GIT_DIRREMOVAL_FILES_AND_DIRS)); + cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_REMOVE_FILES)); cl_assert(git_path_exists(empty_tmp_dir) == false); git_buf_free(&file); } + +void test_core_rmdir__can_remove_empty_parents(void) +{ + git_buf file = GIT_BUF_INIT; + + cl_git_pass( + git_buf_joinpath(&file, empty_tmp_dir, "/one/two_two/three/file.txt")); + cl_git_mkfile(git_buf_cstr(&file), "dummy"); + cl_assert(git_path_isfile(git_buf_cstr(&file))); + + cl_git_pass(git_futils_rmdir_r("one/two_two/three/file.txt", empty_tmp_dir, + GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_EMPTY_PARENTS)); + + cl_assert(!git_path_exists(git_buf_cstr(&file))); + + git_buf_rtruncate_at_char(&file, '/'); /* three (only contained file.txt) */ + cl_assert(!git_path_exists(git_buf_cstr(&file))); + + git_buf_rtruncate_at_char(&file, '/'); /* two_two (only contained three) */ + cl_assert(!git_path_exists(git_buf_cstr(&file))); + + git_buf_rtruncate_at_char(&file, '/'); /* one (contained two_one also) */ + cl_assert(git_path_exists(git_buf_cstr(&file))); + + cl_assert(git_path_exists(empty_tmp_dir) == true); + + git_buf_free(&file); + + cl_git_pass(git_futils_rmdir_r(empty_tmp_dir, NULL, GIT_RMDIR_EMPTY_HIERARCHY)); +} diff --git a/tests-clar/core/stat.c b/tests-clar/core/stat.c new file mode 100644 index 000000000..2e4abfb79 --- /dev/null +++ b/tests-clar/core/stat.c @@ -0,0 +1,97 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "path.h" +#include "posix.h" + +void test_core_stat__initialize(void) +{ + cl_git_pass(git_futils_mkdir("root/d1/d2", NULL, 0755, GIT_MKDIR_PATH)); + cl_git_mkfile("root/file", "whatever\n"); + cl_git_mkfile("root/d1/file", "whatever\n"); +} + +void test_core_stat__cleanup(void) +{ + git_futils_rmdir_r("root", NULL, GIT_RMDIR_REMOVE_FILES); +} + +#define cl_assert_error(val) \ + do { err = errno; cl_assert_equal_i((val), err); } while (0) + +void test_core_stat__0(void) +{ + struct stat st; + int err; + + cl_assert_equal_i(0, p_lstat("root", &st)); + cl_assert(S_ISDIR(st.st_mode)); + cl_assert_error(0); + + cl_assert_equal_i(0, p_lstat("root/", &st)); + cl_assert(S_ISDIR(st.st_mode)); + cl_assert_error(0); + + cl_assert_equal_i(0, p_lstat("root/file", &st)); + cl_assert(S_ISREG(st.st_mode)); + cl_assert_error(0); + + cl_assert_equal_i(0, p_lstat("root/d1", &st)); + cl_assert(S_ISDIR(st.st_mode)); + cl_assert_error(0); + + cl_assert_equal_i(0, p_lstat("root/d1/", &st)); + cl_assert(S_ISDIR(st.st_mode)); + cl_assert_error(0); + + cl_assert_equal_i(0, p_lstat("root/d1/file", &st)); + cl_assert(S_ISREG(st.st_mode)); + cl_assert_error(0); + + cl_assert(p_lstat("root/missing", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat("root/missing/but/could/be/created", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat_posixly("root/missing/but/could/be/created", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat("root/d1/missing", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat("root/d1/missing/deeper/path", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat_posixly("root/d1/missing/deeper/path", &st) < 0); + cl_assert_error(ENOENT); + + cl_assert(p_lstat_posixly("root/d1/file/deeper/path", &st) < 0); + cl_assert_error(ENOTDIR); + + cl_assert(p_lstat("root/file/invalid", &st) < 0); +#ifdef GIT_WIN32 + cl_assert_error(ENOENT); +#else + cl_assert_error(ENOTDIR); +#endif + + cl_assert(p_lstat_posixly("root/file/invalid", &st) < 0); + cl_assert_error(ENOTDIR); + + cl_assert(p_lstat("root/file/invalid/deeper_path", &st) < 0); +#ifdef GIT_WIN32 + cl_assert_error(ENOENT); +#else + cl_assert_error(ENOTDIR); +#endif + + cl_assert(p_lstat_posixly("root/file/invalid/deeper_path", &st) < 0); + cl_assert_error(ENOTDIR); + + cl_assert(p_lstat_posixly("root/d1/file/extra", &st) < 0); + cl_assert_error(ENOTDIR); + + cl_assert(p_lstat_posixly("root/d1/file/further/invalid/items", &st) < 0); + cl_assert_error(ENOTDIR); +} + diff --git a/tests-clar/core/vector.c b/tests-clar/core/vector.c index ef3d6c36d..c9e43a149 100644 --- a/tests-clar/core/vector.c +++ b/tests-clar/core/vector.c @@ -189,3 +189,87 @@ void test_core_vector__5(void) git_vector_free(&x); } + +static int remove_ones(const git_vector *v, size_t idx) +{ + return (git_vector_get(v, idx) == (void *)0x001); +} + +/* Test removal based on callback */ +void test_core_vector__remove_matching(void) +{ + git_vector x; + size_t i; + void *compare; + + git_vector_init(&x, 1, NULL); + git_vector_insert(&x, (void*) 0x001); + + cl_assert(x.length == 1); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 0); + + git_vector_insert(&x, (void*) 0x001); + git_vector_insert(&x, (void*) 0x001); + git_vector_insert(&x, (void*) 0x001); + + cl_assert(x.length == 3); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 0); + + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x001); + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x001); + + cl_assert(x.length == 4); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 2); + + git_vector_foreach(&x, i, compare) { + cl_assert(compare != (void *)0x001); + } + + git_vector_clear(&x); + + git_vector_insert(&x, (void*) 0x001); + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x001); + + cl_assert(x.length == 4); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 2); + + git_vector_foreach(&x, i, compare) { + cl_assert(compare != (void *)0x001); + } + + git_vector_clear(&x); + + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x001); + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x001); + + cl_assert(x.length == 4); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 2); + + git_vector_foreach(&x, i, compare) { + cl_assert(compare != (void *)0x001); + } + + git_vector_clear(&x); + + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x003); + git_vector_insert(&x, (void*) 0x002); + git_vector_insert(&x, (void*) 0x003); + + cl_assert(x.length == 4); + git_vector_remove_matching(&x, remove_ones); + cl_assert(x.length == 4); + + git_vector_free(&x); +} |