]> git.proxmox.com Git - libgit2.git/blame - tests/object/blob/filter.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / object / blob / filter.c
CommitLineData
ce49c7a8
RB
1#include "clar_libgit2.h"
2#include "posix.h"
3#include "blob.h"
3658e81e 4#include "buf_text.h"
ce49c7a8
RB
5
6static git_repository *g_repo = NULL;
b47349b8
RB
7
8#define CRLF_NUM_TEST_OBJECTS 9
9
10static const char *g_crlf_raw[CRLF_NUM_TEST_OBJECTS] = {
ce49c7a8
RB
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",
7bf87ab6
RB
16 "123\n\000\001\002\003\004abc\255\254\253\r\n",
17 "\xEF\xBB\xBFThis is UTF-8\n",
d0b25d9d 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",
7bf87ab6 19 "\xFE\xFF\x00T\x00h\x00i\x00s\x00!"
ce49c7a8 20};
b47349b8 21
22a2d3d5 22static off64_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
b47349b8 23 -1, -1, -1, -1, -1, 17, -1, -1, 12
ce49c7a8 24};
b47349b8
RB
25
26static git_oid g_crlf_oids[CRLF_NUM_TEST_OBJECTS];
27
28static git_buf g_crlf_filtered[CRLF_NUM_TEST_OBJECTS] = {
ce49c7a8
RB
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 },
7bf87ab6
RB
34 { "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 },
35 { "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 },
d0b25d9d 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 },
7bf87ab6 37 { "\xFE\xFF\x00T\x00h\x00i\x00s\x00!", 0, 12 }
ce49c7a8
RB
38};
39
b47349b8
RB
40static 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
ce49c7a8
RB
52void test_object_blob_filter__initialize(void)
53{
54 int i;
55
b47349b8 56 g_repo = cl_git_sandbox_init("empty_standard_repo");
ce49c7a8 57
b47349b8
RB
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]);
ce49c7a8 61
22a2d3d5 62 cl_git_pass(git_blob_create_from_buffer(
b47349b8 63 &g_crlf_oids[i], g_repo, g_crlf_raw[i], (size_t)g_crlf_raw_len[i]));
ce49c7a8
RB
64 }
65}
66
67void test_object_blob_filter__cleanup(void)
68{
b47349b8 69 cl_git_sandbox_cleanup();
ce49c7a8
RB
70}
71
72void test_object_blob_filter__unfiltered(void)
73{
74 int i;
75 git_blob *blob;
76
b47349b8
RB
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
ce49c7a8
RB
86 git_blob_free(blob);
87 }
88}
89
90void test_object_blob_filter__stats(void)
91{
92 int i;
93 git_blob *blob;
94 git_buf buf = GIT_BUF_INIT;
7bf87ab6 95 git_buf_text_stats stats;
ce49c7a8 96
b47349b8
RB
97 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
98 cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
ce49c7a8 99 cl_git_pass(git_blob__getbuf(&buf, blob));
7bf87ab6 100 git_buf_text_gather_stats(&stats, &buf, false);
b47349b8
RB
101 cl_assert_equal_i(
102 0, memcmp(&g_crlf_filtered_stats[i], &stats, sizeof(stats)));
ce49c7a8
RB
103 git_blob_free(blob);
104 }
105
ac3d33df 106 git_buf_dispose(&buf);
ce49c7a8
RB
107}
108
109void test_object_blob_filter__to_odb(void)
110{
85d54812 111 git_filter_list *fl = NULL;
ce49c7a8
RB
112 git_config *cfg;
113 int i;
114 git_blob *blob;
1e4976cb 115 git_buf out = GIT_BUF_INIT, zeroed;
ce49c7a8
RB
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
4b11f25a 123 cl_git_pass(git_filter_list_load(
5269008c 124 &fl, g_repo, NULL, "filename.txt", GIT_FILTER_TO_ODB, 0));
85d54812 125 cl_assert(fl != NULL);
ce49c7a8 126
b47349b8
RB
127 for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
128 cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
ce49c7a8 129
1e4976cb 130 /* try once with allocated blob */
2a7d224f 131 cl_git_pass(git_filter_list_apply_to_blob(&out, fl, blob));
b47349b8 132 cl_assert_equal_sz(g_crlf_filtered[i].size, out.size);
b47349b8
RB
133 cl_assert_equal_i(
134 0, memcmp(out.ptr, g_crlf_filtered[i].ptr, out.size));
ce49c7a8 135
1e4976cb
RB
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));
ac3d33df 142 git_buf_dispose(&zeroed);
1e4976cb 143
ce49c7a8
RB
144 git_blob_free(blob);
145 }
146
85d54812 147 git_filter_list_free(fl);
ac3d33df 148 git_buf_dispose(&out);
ce49c7a8
RB
149 git_config_free(cfg);
150}