]> git.proxmox.com Git - libgit2.git/blob - tests/object/blob/filter.c
0f0f4845f392e82edb1ddc268678f69f1b8806d7
[libgit2.git] / tests / object / blob / filter.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "blob.h"
4 #include "buf_text.h"
5
6 static git_repository *g_repo = NULL;
7
8 #define CRLF_NUM_TEST_OBJECTS 9
9
10 static const char *g_crlf_raw[CRLF_NUM_TEST_OBJECTS] = {
11 "",
12 "foo\nbar\n",
13 "foo\rbar\r",
14 "foo\r\nbar\r\n",
15 "foo\nbar\rboth\r\nreversed\n\ragain\nproblems\r",
16 "123\n\000\001\002\003\004abc\255\254\253\r\n",
17 "\xEF\xBB\xBFThis is UTF-8\n",
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 };
21
22 static off64_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
23 -1, -1, -1, -1, -1, 17, -1, -1, 12
24 };
25
26 static git_oid g_crlf_oids[CRLF_NUM_TEST_OBJECTS];
27
28 static git_buf g_crlf_filtered[CRLF_NUM_TEST_OBJECTS] = {
29 { "", 0, 0 },
30 { "foo\nbar\n", 0, 8 },
31 { "foo\rbar\r", 0, 8 },
32 { "foo\nbar\n", 0, 8 },
33 { "foo\nbar\rboth\nreversed\n\ragain\nproblems\r", 0, 38 },
34 { "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 },
35 { "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 },
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
40 static git_buf_text_stats g_crlf_filtered_stats[CRLF_NUM_TEST_OBJECTS] = {
41 { 0, 0, 0, 0, 0, 0, 0 },
42 { 0, 0, 0, 2, 0, 6, 0 },
43 { 0, 0, 2, 0, 0, 6, 0 },
44 { 0, 0, 2, 2, 2, 6, 0 },
45 { 0, 0, 4, 4, 1, 31, 0 },
46 { 0, 1, 1, 2, 1, 9, 5 },
47 { GIT_BOM_UTF8, 0, 0, 1, 0, 16, 0 },
48 { GIT_BOM_UTF8, 0, 2, 2, 2, 27, 0 },
49 { GIT_BOM_UTF16_BE, 5, 0, 0, 0, 7, 5 },
50 };
51
52 void test_object_blob_filter__initialize(void)
53 {
54 int i;
55
56 g_repo = cl_git_sandbox_init("empty_standard_repo");
57
58 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
59 if (g_crlf_raw_len[i] < 0)
60 g_crlf_raw_len[i] = strlen(g_crlf_raw[i]);
61
62 cl_git_pass(git_blob_create_from_buffer(
63 &g_crlf_oids[i], g_repo, g_crlf_raw[i], (size_t)g_crlf_raw_len[i]));
64 }
65 }
66
67 void test_object_blob_filter__cleanup(void)
68 {
69 cl_git_sandbox_cleanup();
70 }
71
72 void test_object_blob_filter__unfiltered(void)
73 {
74 int i;
75 git_blob *blob;
76
77 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
78 size_t raw_len = (size_t)g_crlf_raw_len[i];
79
80 cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
81
82 cl_assert_equal_sz(raw_len, (size_t)git_blob_rawsize(blob));
83 cl_assert_equal_i(
84 0, memcmp(g_crlf_raw[i], git_blob_rawcontent(blob), raw_len));
85
86 git_blob_free(blob);
87 }
88 }
89
90 void test_object_blob_filter__stats(void)
91 {
92 int i;
93 git_blob *blob;
94 git_buf buf = GIT_BUF_INIT;
95 git_buf_text_stats stats;
96
97 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
98 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);
101 cl_assert_equal_i(
102 0, memcmp(&g_crlf_filtered_stats[i], &stats, sizeof(stats)));
103 git_blob_free(blob);
104 }
105
106 git_buf_dispose(&buf);
107 }
108
109 void test_object_blob_filter__to_odb(void)
110 {
111 git_filter_list *fl = NULL;
112 git_config *cfg;
113 int i;
114 git_blob *blob;
115 git_buf out = GIT_BUF_INIT, zeroed;
116
117 cl_git_pass(git_repository_config(&cfg, g_repo));
118 cl_assert(cfg);
119
120 git_attr_cache_flush(g_repo);
121 cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");
122
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
127 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
128 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));
132 cl_assert_equal_sz(g_crlf_filtered[i].size, out.size);
133 cl_assert_equal_i(
134 0, memcmp(out.ptr, g_crlf_filtered[i].ptr, out.size));
135
136 /* try again with zeroed blob */
137 memset(&zeroed, 0, sizeof(zeroed));
138 cl_git_pass(git_filter_list_apply_to_blob(&zeroed, fl, blob));
139 cl_assert_equal_sz(g_crlf_filtered[i].size, zeroed.size);
140 cl_assert_equal_i(
141 0, memcmp(zeroed.ptr, g_crlf_filtered[i].ptr, zeroed.size));
142 git_buf_dispose(&zeroed);
143
144 git_blob_free(blob);
145 }
146
147 git_filter_list_free(fl);
148 git_buf_dispose(&out);
149 git_config_free(cfg);
150 }