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