1 #include "clar_libgit2.h"
6 static git_repository
*g_repo
= NULL
;
8 #define CRLF_NUM_TEST_OBJECTS 9
10 static const char *g_crlf_raw
[CRLF_NUM_TEST_OBJECTS
] = {
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!"
22 static off64_t g_crlf_raw_len
[CRLF_NUM_TEST_OBJECTS
] = {
23 -1, -1, -1, -1, -1, 17, -1, -1, 12
26 static git_oid g_crlf_oids
[CRLF_NUM_TEST_OBJECTS
];
28 static git_buf g_crlf_filtered
[CRLF_NUM_TEST_OBJECTS
] = {
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 }
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 },
52 void test_object_blob_filter__initialize(void)
56 g_repo
= cl_git_sandbox_init("empty_standard_repo");
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
]);
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
]));
67 void test_object_blob_filter__cleanup(void)
69 cl_git_sandbox_cleanup();
72 void test_object_blob_filter__unfiltered(void)
77 for (i
= 0; i
< CRLF_NUM_TEST_OBJECTS
; i
++) {
78 size_t raw_len
= (size_t)g_crlf_raw_len
[i
];
80 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &g_crlf_oids
[i
]));
82 cl_assert_equal_sz(raw_len
, (size_t)git_blob_rawsize(blob
));
84 0, memcmp(g_crlf_raw
[i
], git_blob_rawcontent(blob
), raw_len
));
90 void test_object_blob_filter__stats(void)
94 git_buf buf
= GIT_BUF_INIT
;
95 git_buf_text_stats stats
;
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);
102 0, memcmp(&g_crlf_filtered_stats
[i
], &stats
, sizeof(stats
)));
106 git_buf_dispose(&buf
);
109 void test_object_blob_filter__to_odb(void)
111 git_filter_list
*fl
= NULL
;
115 git_buf out
= GIT_BUF_INIT
, zeroed
;
117 cl_git_pass(git_repository_config(&cfg
, g_repo
));
120 git_attr_cache_flush(g_repo
);
121 cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");
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
);
127 for (i
= 0; i
< CRLF_NUM_TEST_OBJECTS
; i
++) {
128 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &g_crlf_oids
[i
]));
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
);
134 0, memcmp(out
.ptr
, g_crlf_filtered
[i
].ptr
, out
.size
));
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
);
141 0, memcmp(zeroed
.ptr
, g_crlf_filtered
[i
].ptr
, zeroed
.size
));
142 git_buf_dispose(&zeroed
);
147 git_filter_list_free(fl
);
148 git_buf_dispose(&out
);
149 git_config_free(cfg
);