Initial commit

This commit is contained in:
jiapengyu
2026-07-27 13:37:48 +08:00
commit ecba71e2a5
39123 changed files with 5989154 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
#include "clar_libgit2.h"
#include "buffer.h"
static const char *test_string = "Have you seen that? Have you seeeen that??";
void test_buf_basic__resize(void)
{
git_buf buf1 = GIT_BUF_INIT;
git_buf_puts(&buf1, test_string);
cl_assert(git_buf_oom(&buf1) == 0);
cl_assert_equal_s(git_buf_cstr(&buf1), test_string);
git_buf_puts(&buf1, test_string);
cl_assert(strlen(git_buf_cstr(&buf1)) == strlen(test_string) * 2);
git_buf_dispose(&buf1);
}
void test_buf_basic__resize_incremental(void)
{
git_buf buf1 = GIT_BUF_INIT;
/* Presently, asking for 6 bytes will round up to 8. */
cl_git_pass(git_buf_puts(&buf1, "Hello"));
cl_assert_equal_i(5, buf1.size);
cl_assert_equal_i(8, buf1.asize);
/* Ensure an additional byte does not realloc. */
cl_git_pass(git_buf_grow_by(&buf1, 1));
cl_assert_equal_i(5, buf1.size);
cl_assert_equal_i(8, buf1.asize);
/* But requesting many does. */
cl_git_pass(git_buf_grow_by(&buf1, 16));
cl_assert_equal_i(5, buf1.size);
cl_assert(buf1.asize > 8);
git_buf_dispose(&buf1);
}
void test_buf_basic__printf(void)
{
git_buf buf2 = GIT_BUF_INIT;
git_buf_printf(&buf2, "%s %s %d ", "shoop", "da", 23);
cl_assert(git_buf_oom(&buf2) == 0);
cl_assert_equal_s(git_buf_cstr(&buf2), "shoop da 23 ");
git_buf_printf(&buf2, "%s %d", "woop", 42);
cl_assert(git_buf_oom(&buf2) == 0);
cl_assert_equal_s(git_buf_cstr(&buf2), "shoop da 23 woop 42");
git_buf_dispose(&buf2);
}
+59
View File
@@ -0,0 +1,59 @@
#include "clar_libgit2.h"
#include "buffer.h"
/* Override default allocators with ones that will fail predictably. */
static git_allocator std_alloc;
static git_allocator oom_alloc;
static void *oom_malloc(size_t n, const char *file, int line)
{
/* Reject any allocation of more than 100 bytes */
return (n > 100) ? NULL : std_alloc.gmalloc(n, file, line);
}
static void *oom_realloc(void *p, size_t n, const char *file, int line)
{
/* Reject any allocation of more than 100 bytes */
return (n > 100) ? NULL : std_alloc.grealloc(p, n, file, line);
}
void test_buf_oom__initialize(void)
{
git_stdalloc_init_allocator(&std_alloc);
git_stdalloc_init_allocator(&oom_alloc);
oom_alloc.gmalloc = oom_malloc;
oom_alloc.grealloc = oom_realloc;
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &oom_alloc));
}
void test_buf_oom__cleanup(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, NULL));
}
void test_buf_oom__grow(void)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_grow(&buf, 42));
cl_assert(!git_buf_oom(&buf));
cl_assert(git_buf_grow(&buf, 101) == -1);
cl_assert(git_buf_oom(&buf));
git_buf_dispose(&buf);
}
void test_buf_oom__grow_by(void)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_grow_by(&buf, 42));
cl_assert(!git_buf_oom(&buf));
cl_assert(git_buf_grow_by(&buf, 101) == -1);
cl_assert(git_buf_oom(&buf));
}
+49
View File
@@ -0,0 +1,49 @@
#include "clar_libgit2.h"
#include "buffer.h"
static void expect_decode_pass(const char *expected, const char *encoded)
{
git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT;
/*
* ensure that we only read the given length of the input buffer
* by putting garbage at the end. this will ensure that we do
* not, eg, rely on nul-termination or walk off the end of the buf.
*/
cl_git_pass(git_buf_puts(&in, encoded));
cl_git_pass(git_buf_PUTS(&in, "TRAILER"));
cl_git_pass(git_buf_decode_percent(&out, in.ptr, strlen(encoded)));
cl_assert_equal_s(expected, git_buf_cstr(&out));
cl_assert_equal_i(strlen(expected), git_buf_len(&out));
git_buf_dispose(&in);
git_buf_dispose(&out);
}
void test_buf_percent__decode_succeeds(void)
{
expect_decode_pass("", "");
expect_decode_pass(" ", "%20");
expect_decode_pass("a", "a");
expect_decode_pass(" a", "%20a");
expect_decode_pass("a ", "a%20");
expect_decode_pass("github.com", "github.com");
expect_decode_pass("github.com", "githu%62.com");
expect_decode_pass("github.com", "github%2ecom");
expect_decode_pass("foo bar baz", "foo%20bar%20baz");
expect_decode_pass("foo bar baz", "foo%20bar%20baz");
expect_decode_pass("foo bar ", "foo%20bar%20");
}
void test_buf_percent__ignores_invalid(void)
{
expect_decode_pass("githu%%.com", "githu%%.com");
expect_decode_pass("github.co%2", "github.co%2");
expect_decode_pass("github%2.com", "github%2.com");
expect_decode_pass("githu%2z.com", "githu%2z.com");
expect_decode_pass("github.co%9z", "github.co%9z");
expect_decode_pass("github.co%2", "github.co%2");
expect_decode_pass("github.co%", "github.co%");
}
+88
View File
@@ -0,0 +1,88 @@
#include "clar_libgit2.h"
#include "buffer.h"
static void expect_quote_pass(const char *expected, const char *str)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&buf, str));
cl_git_pass(git_buf_quote(&buf));
cl_assert_equal_s(expected, git_buf_cstr(&buf));
cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
git_buf_dispose(&buf);
}
void test_buf_quote__quote_succeeds(void)
{
expect_quote_pass("", "");
expect_quote_pass("foo", "foo");
expect_quote_pass("foo/bar/baz.c", "foo/bar/baz.c");
expect_quote_pass("foo bar", "foo bar");
expect_quote_pass("\"\\\"leading quote\"", "\"leading quote");
expect_quote_pass("\"slash\\\\y\"", "slash\\y");
expect_quote_pass("\"foo\\r\\nbar\"", "foo\r\nbar");
expect_quote_pass("\"foo\\177bar\"", "foo\177bar");
expect_quote_pass("\"foo\\001bar\"", "foo\001bar");
expect_quote_pass("\"foo\\377bar\"", "foo\377bar");
}
static void expect_unquote_pass(const char *expected, const char *quoted)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&buf, quoted));
cl_git_pass(git_buf_unquote(&buf));
cl_assert_equal_s(expected, git_buf_cstr(&buf));
cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
git_buf_dispose(&buf);
}
static void expect_unquote_fail(const char *quoted)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&buf, quoted));
cl_git_fail(git_buf_unquote(&buf));
git_buf_dispose(&buf);
}
void test_buf_quote__unquote_succeeds(void)
{
expect_unquote_pass("", "\"\"");
expect_unquote_pass(" ", "\" \"");
expect_unquote_pass("foo", "\"foo\"");
expect_unquote_pass("foo bar", "\"foo bar\"");
expect_unquote_pass("foo\"bar", "\"foo\\\"bar\"");
expect_unquote_pass("foo\\bar", "\"foo\\\\bar\"");
expect_unquote_pass("foo\tbar", "\"foo\\tbar\"");
expect_unquote_pass("\vfoo\tbar\n", "\"\\vfoo\\tbar\\n\"");
expect_unquote_pass("foo\nbar", "\"foo\\012bar\"");
expect_unquote_pass("foo\r\nbar", "\"foo\\015\\012bar\"");
expect_unquote_pass("foo\r\nbar", "\"\\146\\157\\157\\015\\012\\142\\141\\162\"");
expect_unquote_pass("newline: \n", "\"newline: \\012\"");
expect_unquote_pass("0xff: \377", "\"0xff: \\377\"");
}
void test_buf_quote__unquote_fails(void)
{
expect_unquote_fail("no quotes at all");
expect_unquote_fail("\"no trailing quote");
expect_unquote_fail("no leading quote\"");
expect_unquote_fail("\"invalid \\z escape char\"");
expect_unquote_fail("\"\\q invalid escape char\"");
expect_unquote_fail("\"invalid escape char \\p\"");
expect_unquote_fail("\"invalid \\1 escape char \"");
expect_unquote_fail("\"invalid \\14 escape char \"");
expect_unquote_fail("\"invalid \\280 escape char\"");
expect_unquote_fail("\"invalid \\378 escape char\"");
expect_unquote_fail("\"invalid \\380 escape char\"");
expect_unquote_fail("\"invalid \\411 escape char\"");
expect_unquote_fail("\"truncated escape char \\\"");
expect_unquote_fail("\"truncated escape char \\0\"");
expect_unquote_fail("\"truncated escape char \\01\"");
}
+93
View File
@@ -0,0 +1,93 @@
#include "clar_libgit2.h"
#include "buffer.h"
static git_buf _buf;
void test_buf_splice__initialize(void) {
git_buf_init(&_buf, 16);
}
void test_buf_splice__cleanup(void) {
git_buf_dispose(&_buf);
}
void test_buf_splice__preprend(void)
{
git_buf_sets(&_buf, "world!");
cl_git_pass(git_buf_splice(&_buf, 0, 0, "Hello Dolly", strlen("Hello ")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__append(void)
{
git_buf_sets(&_buf, "Hello");
cl_git_pass(git_buf_splice(&_buf, git_buf_len(&_buf), 0, " world!", strlen(" world!")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__insert_at(void)
{
git_buf_sets(&_buf, "Hell world!");
cl_git_pass(git_buf_splice(&_buf, strlen("Hell"), 0, "o", strlen("o")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__remove_at(void)
{
git_buf_sets(&_buf, "Hello world of warcraft!");
cl_git_pass(git_buf_splice(&_buf, strlen("Hello world"), strlen(" of warcraft"), "", 0));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__replace(void)
{
git_buf_sets(&_buf, "Hell0 w0rld!");
cl_git_pass(git_buf_splice(&_buf, strlen("Hell"), strlen("0 w0"), "o wo", strlen("o wo")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__replace_with_longer(void)
{
git_buf_sets(&_buf, "Hello you!");
cl_git_pass(git_buf_splice(&_buf, strlen("Hello "), strlen("you"), "world", strlen("world")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__replace_with_shorter(void)
{
git_buf_sets(&_buf, "Brave new world!");
cl_git_pass(git_buf_splice(&_buf, 0, strlen("Brave new"), "Hello", strlen("Hello")));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__truncate(void)
{
git_buf_sets(&_buf, "Hello world!!");
cl_git_pass(git_buf_splice(&_buf, strlen("Hello world!"), strlen("!"), "", 0));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}
void test_buf_splice__dont_do_anything(void)
{
git_buf_sets(&_buf, "Hello world!");
cl_git_pass(git_buf_splice(&_buf, 3, 0, "Hello", 0));
cl_assert_equal_s("Hello world!", git_buf_cstr(&_buf));
}