1 #include "clar_libgit2.h"
5 static git_repository
*g_repo
= NULL
;
7 #define CRLF_NUM_TEST_OBJECTS 9
9 static const char *g_crlf_raw
[CRLF_NUM_TEST_OBJECTS
] = {
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!"
21 static off64_t g_crlf_raw_len
[CRLF_NUM_TEST_OBJECTS
] = {
22 -1, -1, -1, -1, -1, 17, -1, -1, 12
25 static git_oid g_crlf_oids
[CRLF_NUM_TEST_OBJECTS
];
27 static git_buf g_crlf_filtered
[CRLF_NUM_TEST_OBJECTS
] = {
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 }
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 },
51 void test_object_blob_filter__initialize(void)
55 g_repo
= cl_git_sandbox_init("empty_standard_repo");
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
]);
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
]));
66 void test_object_blob_filter__cleanup(void)
68 cl_git_sandbox_cleanup();
71 void test_object_blob_filter__unfiltered(void)
76 for (i
= 0; i
< CRLF_NUM_TEST_OBJECTS
; i
++) {
77 size_t raw_len
= (size_t)g_crlf_raw_len
[i
];
79 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &g_crlf_oids
[i
]));
81 cl_assert_equal_sz(raw_len
, (size_t)git_blob_rawsize(blob
));
83 0, memcmp(g_crlf_raw
[i
], git_blob_rawcontent(blob
), raw_len
));
89 void test_object_blob_filter__stats(void)
93 git_buf buf
= GIT_BUF_INIT
;
94 git_buf_text_stats stats
;
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);
101 0, memcmp(&g_crlf_filtered_stats
[i
], &stats
, sizeof(stats
)));
105 git_buf_dispose(&buf
);
108 void test_object_blob_filter__to_odb(void)
110 git_filter_list
*fl
= NULL
;
114 git_buf out
= GIT_BUF_INIT
, zeroed
;
116 cl_git_pass(git_repository_config(&cfg
, g_repo
));
119 git_attr_cache_flush(g_repo
);
120 cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");
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
);
126 for (i
= 0; i
< CRLF_NUM_TEST_OBJECTS
; i
++) {
127 cl_git_pass(git_blob_lookup(&blob
, g_repo
, &g_crlf_oids
[i
]));
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
);
133 0, memcmp(out
.ptr
, g_crlf_filtered
[i
].ptr
, out
.size
));
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
);
140 0, memcmp(zeroed
.ptr
, g_crlf_filtered
[i
].ptr
, zeroed
.size
));
141 git_buf_dispose(&zeroed
);
146 git_filter_list_free(fl
);
147 git_buf_dispose(&out
);
148 git_config_free(cfg
);