filter.c 3.99 KB
Newer Older
1 2 3
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
4
#include "buf_text.h"
5 6

static git_repository *g_repo = NULL;
Russell Belfer committed
7 8 9 10

#define CRLF_NUM_TEST_OBJECTS	9

static const char *g_crlf_raw[CRLF_NUM_TEST_OBJECTS] = {
11 12 13 14 15
	"",
	"foo\nbar\n",
	"foo\rbar\r",
	"foo\r\nbar\r\n",
	"foo\nbar\rboth\r\nreversed\n\ragain\nproblems\r",
16 17
	"123\n\000\001\002\003\004abc\255\254\253\r\n",
	"\xEF\xBB\xBFThis is UTF-8\n",
crazymaster committed
18
	"\xEF\xBB\xBF\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\r\n\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\r\n",
19
	"\xFE\xFF\x00T\x00h\x00i\x00s\x00!"
20
};
Russell Belfer committed
21 22 23

static git_off_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
	-1, -1, -1, -1, -1, 17, -1, -1, 12
24
};
Russell Belfer committed
25 26 27 28

static git_oid g_crlf_oids[CRLF_NUM_TEST_OBJECTS];

static git_buf g_crlf_filtered[CRLF_NUM_TEST_OBJECTS] = {
29 30 31 32 33
	{ "", 0, 0 },
	{ "foo\nbar\n", 0, 8 },
	{ "foo\rbar\r", 0, 8 },
	{ "foo\nbar\n", 0, 8 },
	{ "foo\nbar\rboth\nreversed\n\ragain\nproblems\r", 0, 38 },
34 35
	{ "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 },
	{ "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 },
crazymaster committed
36
	{ "\xEF\xBB\xBF\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\n\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\n", 0, 29 },
37
	{ "\xFE\xFF\x00T\x00h\x00i\x00s\x00!", 0, 12 }
38 39
};

Russell Belfer committed
40 41 42 43 44 45 46 47 48 49 50 51
static git_buf_text_stats g_crlf_filtered_stats[CRLF_NUM_TEST_OBJECTS] = {
	{ 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 0, 0, 2, 0, 6, 0 },
	{ 0, 0, 2, 0, 0, 6, 0 },
	{ 0, 0, 2, 2, 2, 6, 0 },
	{ 0, 0, 4, 4, 1, 31, 0 },
	{ 0, 1, 1, 2, 1, 9, 5 },
	{ GIT_BOM_UTF8, 0, 0, 1, 0, 16, 0 },
	{ GIT_BOM_UTF8, 0, 2, 2, 2, 27, 0 },
	{ GIT_BOM_UTF16_BE, 5, 0, 0, 0, 7, 5 },
};

52 53 54 55
void test_object_blob_filter__initialize(void)
{
	int i;

Russell Belfer committed
56
	g_repo = cl_git_sandbox_init("empty_standard_repo");
57

Russell Belfer committed
58 59 60
	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
		if (g_crlf_raw_len[i] < 0)
			g_crlf_raw_len[i] = strlen(g_crlf_raw[i]);
61

Russell Belfer committed
62 63
		cl_git_pass(git_blob_create_frombuffer(
			&g_crlf_oids[i], g_repo, g_crlf_raw[i], (size_t)g_crlf_raw_len[i]));
64 65 66 67 68
	}
}

void test_object_blob_filter__cleanup(void)
{
Russell Belfer committed
69
	cl_git_sandbox_cleanup();
70 71 72 73 74 75 76
}

void test_object_blob_filter__unfiltered(void)
{
	int i;
	git_blob *blob;

Russell Belfer committed
77 78 79 80 81 82 83 84 85
	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
		size_t raw_len = (size_t)g_crlf_raw_len[i];

		cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));

		cl_assert_equal_sz(raw_len, (size_t)git_blob_rawsize(blob));
		cl_assert_equal_i(
			0, memcmp(g_crlf_raw[i], git_blob_rawcontent(blob), raw_len));

86 87 88 89 90 91 92 93 94
		git_blob_free(blob);
	}
}

void test_object_blob_filter__stats(void)
{
	int i;
	git_blob *blob;
	git_buf buf = GIT_BUF_INIT;
95
	git_buf_text_stats stats;
96

Russell Belfer committed
97 98
	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
		cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
99
		cl_git_pass(git_blob__getbuf(&buf, blob));
100
		git_buf_text_gather_stats(&stats, &buf, false);
Russell Belfer committed
101 102
		cl_assert_equal_i(
			0, memcmp(&g_crlf_filtered_stats[i], &stats, sizeof(stats)));
103 104 105 106 107 108 109 110
		git_blob_free(blob);
	}

	git_buf_free(&buf);
}

void test_object_blob_filter__to_odb(void)
{
111
	git_filter_list *fl = NULL;
112 113 114
	git_config *cfg;
	int i;
	git_blob *blob;
115
	git_buf out = GIT_BUF_INIT, zeroed;
116 117 118 119 120 121 122

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_assert(cfg);

	git_attr_cache_flush(g_repo);
	cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");

Russell Belfer committed
123
	cl_git_pass(git_filter_list_load(
124
		&fl, g_repo, NULL, "filename.txt", GIT_FILTER_TO_ODB, 0));
125
	cl_assert(fl != NULL);
126

Russell Belfer committed
127 128
	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
		cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
129

130
		/* try once with allocated blob */
131
		cl_git_pass(git_filter_list_apply_to_blob(&out, fl, blob));
Russell Belfer committed
132 133 134
		cl_assert_equal_sz(g_crlf_filtered[i].size, out.size);
		cl_assert_equal_i(
			0, memcmp(out.ptr, g_crlf_filtered[i].ptr, out.size));
135

136 137 138 139 140 141 142 143
		/* try again with zeroed blob */
		memset(&zeroed, 0, sizeof(zeroed));
		cl_git_pass(git_filter_list_apply_to_blob(&zeroed, fl, blob));
		cl_assert_equal_sz(g_crlf_filtered[i].size, zeroed.size);
		cl_assert_equal_i(
			0, memcmp(zeroed.ptr, g_crlf_filtered[i].ptr, zeroed.size));
		git_buf_free(&zeroed);

144 145 146
		git_blob_free(blob);
	}

147
	git_filter_list_free(fl);
148
	git_buf_free(&out);
149 150
	git_config_free(cfg);
}