]> git.proxmox.com Git - libgit2.git/blob - tests/core/buffer.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / core / buffer.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "git2/sys/hashsig.h"
4 #include "futils.h"
5
6 #define TESTSTR "Have you seen that? Have you seeeen that??"
7 const char *test_string = TESTSTR;
8 const char *test_string_x2 = TESTSTR TESTSTR;
9
10 #define TESTSTR_4096 REP1024("1234")
11 #define TESTSTR_8192 REP1024("12341234")
12 const char *test_4096 = TESTSTR_4096;
13 const char *test_8192 = TESTSTR_8192;
14
15 /* test basic data concatenation */
16 void test_core_buffer__0(void)
17 {
18 git_buf buf = GIT_BUF_INIT;
19
20 cl_assert(buf.size == 0);
21
22 git_buf_puts(&buf, test_string);
23 cl_assert(git_buf_oom(&buf) == 0);
24 cl_assert_equal_s(test_string, git_buf_cstr(&buf));
25
26 git_buf_puts(&buf, test_string);
27 cl_assert(git_buf_oom(&buf) == 0);
28 cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf));
29
30 git_buf_dispose(&buf);
31 }
32
33 /* test git_buf_printf */
34 void test_core_buffer__1(void)
35 {
36 git_buf buf = GIT_BUF_INIT;
37
38 git_buf_printf(&buf, "%s %s %d ", "shoop", "da", 23);
39 cl_assert(git_buf_oom(&buf) == 0);
40 cl_assert_equal_s("shoop da 23 ", git_buf_cstr(&buf));
41
42 git_buf_printf(&buf, "%s %d", "woop", 42);
43 cl_assert(git_buf_oom(&buf) == 0);
44 cl_assert_equal_s("shoop da 23 woop 42", git_buf_cstr(&buf));
45
46 git_buf_dispose(&buf);
47 }
48
49 /* more thorough test of concatenation options */
50 void test_core_buffer__2(void)
51 {
52 git_buf buf = GIT_BUF_INIT;
53 int i;
54 char data[128];
55
56 cl_assert(buf.size == 0);
57
58 /* this must be safe to do */
59 git_buf_dispose(&buf);
60 cl_assert(buf.size == 0);
61 cl_assert(buf.asize == 0);
62
63 /* empty buffer should be empty string */
64 cl_assert_equal_s("", git_buf_cstr(&buf));
65 cl_assert(buf.size == 0);
66 /* cl_assert(buf.asize == 0); -- should not assume what git_buf does */
67
68 /* free should set us back to the beginning */
69 git_buf_dispose(&buf);
70 cl_assert(buf.size == 0);
71 cl_assert(buf.asize == 0);
72
73 /* add letter */
74 git_buf_putc(&buf, '+');
75 cl_assert(git_buf_oom(&buf) == 0);
76 cl_assert_equal_s("+", git_buf_cstr(&buf));
77
78 /* add letter again */
79 git_buf_putc(&buf, '+');
80 cl_assert(git_buf_oom(&buf) == 0);
81 cl_assert_equal_s("++", git_buf_cstr(&buf));
82
83 /* let's try that a few times */
84 for (i = 0; i < 16; ++i) {
85 git_buf_putc(&buf, '+');
86 cl_assert(git_buf_oom(&buf) == 0);
87 }
88 cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf));
89
90 git_buf_dispose(&buf);
91
92 /* add data */
93 git_buf_put(&buf, "xo", 2);
94 cl_assert(git_buf_oom(&buf) == 0);
95 cl_assert_equal_s("xo", git_buf_cstr(&buf));
96
97 /* add letter again */
98 git_buf_put(&buf, "xo", 2);
99 cl_assert(git_buf_oom(&buf) == 0);
100 cl_assert_equal_s("xoxo", git_buf_cstr(&buf));
101
102 /* let's try that a few times */
103 for (i = 0; i < 16; ++i) {
104 git_buf_put(&buf, "xo", 2);
105 cl_assert(git_buf_oom(&buf) == 0);
106 }
107 cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo",
108 git_buf_cstr(&buf));
109
110 git_buf_dispose(&buf);
111
112 /* set to string */
113 git_buf_sets(&buf, test_string);
114 cl_assert(git_buf_oom(&buf) == 0);
115 cl_assert_equal_s(test_string, git_buf_cstr(&buf));
116
117 /* append string */
118 git_buf_puts(&buf, test_string);
119 cl_assert(git_buf_oom(&buf) == 0);
120 cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf));
121
122 /* set to string again (should overwrite - not append) */
123 git_buf_sets(&buf, test_string);
124 cl_assert(git_buf_oom(&buf) == 0);
125 cl_assert_equal_s(test_string, git_buf_cstr(&buf));
126
127 /* test clear */
128 git_buf_clear(&buf);
129 cl_assert_equal_s("", git_buf_cstr(&buf));
130
131 git_buf_dispose(&buf);
132
133 /* test extracting data into buffer */
134 git_buf_puts(&buf, REP4("0123456789"));
135 cl_assert(git_buf_oom(&buf) == 0);
136
137 git_buf_copy_cstr(data, sizeof(data), &buf);
138 cl_assert_equal_s(REP4("0123456789"), data);
139 git_buf_copy_cstr(data, 11, &buf);
140 cl_assert_equal_s("0123456789", data);
141 git_buf_copy_cstr(data, 3, &buf);
142 cl_assert_equal_s("01", data);
143 git_buf_copy_cstr(data, 1, &buf);
144 cl_assert_equal_s("", data);
145
146 git_buf_copy_cstr(data, sizeof(data), &buf);
147 cl_assert_equal_s(REP4("0123456789"), data);
148
149 git_buf_sets(&buf, REP256("x"));
150 git_buf_copy_cstr(data, sizeof(data), &buf);
151 /* since sizeof(data) == 128, only 127 bytes should be copied */
152 cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x")
153 REP16("x") "xxxxxxxxxxxxxxx", data);
154
155 git_buf_dispose(&buf);
156
157 git_buf_copy_cstr(data, sizeof(data), &buf);
158 cl_assert_equal_s("", data);
159 }
160
161 /* let's do some tests with larger buffers to push our limits */
162 void test_core_buffer__3(void)
163 {
164 git_buf buf = GIT_BUF_INIT;
165
166 /* set to string */
167 git_buf_set(&buf, test_4096, 4096);
168 cl_assert(git_buf_oom(&buf) == 0);
169 cl_assert_equal_s(test_4096, git_buf_cstr(&buf));
170
171 /* append string */
172 git_buf_puts(&buf, test_4096);
173 cl_assert(git_buf_oom(&buf) == 0);
174 cl_assert_equal_s(test_8192, git_buf_cstr(&buf));
175
176 /* set to string again (should overwrite - not append) */
177 git_buf_set(&buf, test_4096, 4096);
178 cl_assert(git_buf_oom(&buf) == 0);
179 cl_assert_equal_s(test_4096, git_buf_cstr(&buf));
180
181 git_buf_dispose(&buf);
182 }
183
184 /* let's try some producer/consumer tests */
185 void test_core_buffer__4(void)
186 {
187 git_buf buf = GIT_BUF_INIT;
188 int i;
189
190 for (i = 0; i < 10; ++i) {
191 git_buf_puts(&buf, "1234"); /* add 4 */
192 cl_assert(git_buf_oom(&buf) == 0);
193 git_buf_consume(&buf, buf.ptr + 2); /* eat the first two */
194 cl_assert(strlen(git_buf_cstr(&buf)) == (size_t)((i + 1) * 2));
195 }
196 /* we have appended 1234 10x and removed the first 20 letters */
197 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
198
199 git_buf_consume(&buf, NULL);
200 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
201
202 git_buf_consume(&buf, "invalid pointer");
203 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
204
205 git_buf_consume(&buf, buf.ptr);
206 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
207
208 git_buf_consume(&buf, buf.ptr + 1);
209 cl_assert_equal_s("2341234123412341234", git_buf_cstr(&buf));
210
211 git_buf_consume(&buf, buf.ptr + buf.size);
212 cl_assert_equal_s("", git_buf_cstr(&buf));
213
214 git_buf_dispose(&buf);
215 }
216
217
218 static void
219 check_buf_append(
220 const char* data_a,
221 const char* data_b,
222 const char* expected_data,
223 size_t expected_size,
224 size_t expected_asize)
225 {
226 git_buf tgt = GIT_BUF_INIT;
227
228 git_buf_sets(&tgt, data_a);
229 cl_assert(git_buf_oom(&tgt) == 0);
230 git_buf_puts(&tgt, data_b);
231 cl_assert(git_buf_oom(&tgt) == 0);
232 cl_assert_equal_s(expected_data, git_buf_cstr(&tgt));
233 cl_assert_equal_i(tgt.size, expected_size);
234 if (expected_asize > 0)
235 cl_assert_equal_i(tgt.asize, expected_asize);
236
237 git_buf_dispose(&tgt);
238 }
239
240 static void
241 check_buf_append_abc(
242 const char* buf_a,
243 const char* buf_b,
244 const char* buf_c,
245 const char* expected_ab,
246 const char* expected_abc,
247 const char* expected_abca,
248 const char* expected_abcab,
249 const char* expected_abcabc)
250 {
251 git_buf buf = GIT_BUF_INIT;
252
253 git_buf_sets(&buf, buf_a);
254 cl_assert(git_buf_oom(&buf) == 0);
255 cl_assert_equal_s(buf_a, git_buf_cstr(&buf));
256
257 git_buf_puts(&buf, buf_b);
258 cl_assert(git_buf_oom(&buf) == 0);
259 cl_assert_equal_s(expected_ab, git_buf_cstr(&buf));
260
261 git_buf_puts(&buf, buf_c);
262 cl_assert(git_buf_oom(&buf) == 0);
263 cl_assert_equal_s(expected_abc, git_buf_cstr(&buf));
264
265 git_buf_puts(&buf, buf_a);
266 cl_assert(git_buf_oom(&buf) == 0);
267 cl_assert_equal_s(expected_abca, git_buf_cstr(&buf));
268
269 git_buf_puts(&buf, buf_b);
270 cl_assert(git_buf_oom(&buf) == 0);
271 cl_assert_equal_s(expected_abcab, git_buf_cstr(&buf));
272
273 git_buf_puts(&buf, buf_c);
274 cl_assert(git_buf_oom(&buf) == 0);
275 cl_assert_equal_s(expected_abcabc, git_buf_cstr(&buf));
276
277 git_buf_dispose(&buf);
278 }
279
280 /* more variations on append tests */
281 void test_core_buffer__5(void)
282 {
283 check_buf_append("", "", "", 0, 0);
284 check_buf_append("a", "", "a", 1, 0);
285 check_buf_append("", "a", "a", 1, 8);
286 check_buf_append("", "a", "a", 1, 8);
287 check_buf_append("a", "b", "ab", 2, 8);
288 check_buf_append("", "abcdefgh", "abcdefgh", 8, 16);
289 check_buf_append("abcdefgh", "", "abcdefgh", 8, 16);
290
291 /* buffer with starting asize will grow to:
292 * 1 -> 2, 2 -> 3, 3 -> 5, 4 -> 6, 5 -> 8, 6 -> 9,
293 * 7 -> 11, 8 -> 12, 9 -> 14, 10 -> 15, 11 -> 17, 12 -> 18,
294 * 13 -> 20, 14 -> 21, 15 -> 23, 16 -> 24, 17 -> 26, 18 -> 27,
295 * 19 -> 29, 20 -> 30, 21 -> 32, 22 -> 33, 23 -> 35, 24 -> 36,
296 * ...
297 * follow sequence until value > target size,
298 * then round up to nearest multiple of 8.
299 */
300
301 check_buf_append("abcdefgh", "/", "abcdefgh/", 9, 16);
302 check_buf_append("abcdefgh", "ijklmno", "abcdefghijklmno", 15, 16);
303 check_buf_append("abcdefgh", "ijklmnop", "abcdefghijklmnop", 16, 24);
304 check_buf_append("0123456789", "0123456789",
305 "01234567890123456789", 20, 24);
306 check_buf_append(REP16("x"), REP16("o"),
307 REP16("x") REP16("o"), 32, 40);
308
309 check_buf_append(test_4096, "", test_4096, 4096, 4104);
310 check_buf_append(test_4096, test_4096, test_8192, 8192, 8200);
311
312 /* check sequences of appends */
313 check_buf_append_abc("a", "b", "c",
314 "ab", "abc", "abca", "abcab", "abcabc");
315 check_buf_append_abc("a1", "b2", "c3",
316 "a1b2", "a1b2c3", "a1b2c3a1",
317 "a1b2c3a1b2", "a1b2c3a1b2c3");
318 check_buf_append_abc("a1/", "b2/", "c3/",
319 "a1/b2/", "a1/b2/c3/", "a1/b2/c3/a1/",
320 "a1/b2/c3/a1/b2/", "a1/b2/c3/a1/b2/c3/");
321 }
322
323 /* test swap */
324 void test_core_buffer__6(void)
325 {
326 git_buf a = GIT_BUF_INIT;
327 git_buf b = GIT_BUF_INIT;
328
329 git_buf_sets(&a, "foo");
330 cl_assert(git_buf_oom(&a) == 0);
331 git_buf_sets(&b, "bar");
332 cl_assert(git_buf_oom(&b) == 0);
333
334 cl_assert_equal_s("foo", git_buf_cstr(&a));
335 cl_assert_equal_s("bar", git_buf_cstr(&b));
336
337 git_buf_swap(&a, &b);
338
339 cl_assert_equal_s("bar", git_buf_cstr(&a));
340 cl_assert_equal_s("foo", git_buf_cstr(&b));
341
342 git_buf_dispose(&a);
343 git_buf_dispose(&b);
344 }
345
346
347 /* test detach/attach data */
348 void test_core_buffer__7(void)
349 {
350 const char *fun = "This is fun";
351 git_buf a = GIT_BUF_INIT;
352 char *b = NULL;
353
354 git_buf_sets(&a, "foo");
355 cl_assert(git_buf_oom(&a) == 0);
356 cl_assert_equal_s("foo", git_buf_cstr(&a));
357
358 b = git_buf_detach(&a);
359
360 cl_assert_equal_s("foo", b);
361 cl_assert_equal_s("", a.ptr);
362 git__free(b);
363
364 b = git_buf_detach(&a);
365
366 cl_assert_equal_s(NULL, b);
367 cl_assert_equal_s("", a.ptr);
368
369 git_buf_dispose(&a);
370
371 b = git__strdup(fun);
372 git_buf_attach(&a, b, 0);
373
374 cl_assert_equal_s(fun, a.ptr);
375 cl_assert(a.size == strlen(fun));
376 cl_assert(a.asize == strlen(fun) + 1);
377
378 git_buf_dispose(&a);
379
380 b = git__strdup(fun);
381 git_buf_attach(&a, b, strlen(fun) + 1);
382
383 cl_assert_equal_s(fun, a.ptr);
384 cl_assert(a.size == strlen(fun));
385 cl_assert(a.asize == strlen(fun) + 1);
386
387 git_buf_dispose(&a);
388 }
389
390
391 static void
392 check_joinbuf_2(
393 const char *a,
394 const char *b,
395 const char *expected)
396 {
397 char sep = '/';
398 git_buf buf = GIT_BUF_INIT;
399
400 git_buf_join(&buf, sep, a, b);
401 cl_assert(git_buf_oom(&buf) == 0);
402 cl_assert_equal_s(expected, git_buf_cstr(&buf));
403 git_buf_dispose(&buf);
404 }
405
406 static void
407 check_joinbuf_overlapped(
408 const char *oldval,
409 int ofs_a,
410 const char *b,
411 const char *expected)
412 {
413 char sep = '/';
414 git_buf buf = GIT_BUF_INIT;
415
416 git_buf_sets(&buf, oldval);
417 git_buf_join(&buf, sep, buf.ptr + ofs_a, b);
418 cl_assert(git_buf_oom(&buf) == 0);
419 cl_assert_equal_s(expected, git_buf_cstr(&buf));
420 git_buf_dispose(&buf);
421 }
422
423 static void
424 check_joinbuf_n_2(
425 const char *a,
426 const char *b,
427 const char *expected)
428 {
429 char sep = '/';
430 git_buf buf = GIT_BUF_INIT;
431
432 git_buf_sets(&buf, a);
433 cl_assert(git_buf_oom(&buf) == 0);
434
435 git_buf_join_n(&buf, sep, 1, b);
436 cl_assert(git_buf_oom(&buf) == 0);
437 cl_assert_equal_s(expected, git_buf_cstr(&buf));
438
439 git_buf_dispose(&buf);
440 }
441
442 static void
443 check_joinbuf_n_4(
444 const char *a,
445 const char *b,
446 const char *c,
447 const char *d,
448 const char *expected)
449 {
450 char sep = ';';
451 git_buf buf = GIT_BUF_INIT;
452 git_buf_join_n(&buf, sep, 4, a, b, c, d);
453 cl_assert(git_buf_oom(&buf) == 0);
454 cl_assert_equal_s(expected, git_buf_cstr(&buf));
455 git_buf_dispose(&buf);
456 }
457
458 /* test join */
459 void test_core_buffer__8(void)
460 {
461 git_buf a = GIT_BUF_INIT;
462
463 git_buf_join_n(&a, '/', 1, "foo");
464 cl_assert(git_buf_oom(&a) == 0);
465 cl_assert_equal_s("foo", git_buf_cstr(&a));
466
467 git_buf_join_n(&a, '/', 1, "bar");
468 cl_assert(git_buf_oom(&a) == 0);
469 cl_assert_equal_s("foo/bar", git_buf_cstr(&a));
470
471 git_buf_join_n(&a, '/', 1, "baz");
472 cl_assert(git_buf_oom(&a) == 0);
473 cl_assert_equal_s("foo/bar/baz", git_buf_cstr(&a));
474
475 git_buf_dispose(&a);
476
477 check_joinbuf_2(NULL, "", "");
478 check_joinbuf_2(NULL, "a", "a");
479 check_joinbuf_2(NULL, "/a", "/a");
480 check_joinbuf_2("", "", "");
481 check_joinbuf_2("", "a", "a");
482 check_joinbuf_2("", "/a", "/a");
483 check_joinbuf_2("a", "", "a/");
484 check_joinbuf_2("a", "/", "a/");
485 check_joinbuf_2("a", "b", "a/b");
486 check_joinbuf_2("/", "a", "/a");
487 check_joinbuf_2("/", "", "/");
488 check_joinbuf_2("/a", "/b", "/a/b");
489 check_joinbuf_2("/a", "/b/", "/a/b/");
490 check_joinbuf_2("/a/", "b/", "/a/b/");
491 check_joinbuf_2("/a/", "/b/", "/a/b/");
492 check_joinbuf_2("/a/", "//b/", "/a/b/");
493 check_joinbuf_2("/abcd", "/defg", "/abcd/defg");
494 check_joinbuf_2("/abcd", "/defg/", "/abcd/defg/");
495 check_joinbuf_2("/abcd/", "defg/", "/abcd/defg/");
496 check_joinbuf_2("/abcd/", "/defg/", "/abcd/defg/");
497
498 check_joinbuf_overlapped("abcd", 0, "efg", "abcd/efg");
499 check_joinbuf_overlapped("abcd", 1, "efg", "bcd/efg");
500 check_joinbuf_overlapped("abcd", 2, "efg", "cd/efg");
501 check_joinbuf_overlapped("abcd", 3, "efg", "d/efg");
502 check_joinbuf_overlapped("abcd", 4, "efg", "efg");
503 check_joinbuf_overlapped("abc/", 2, "efg", "c/efg");
504 check_joinbuf_overlapped("abc/", 3, "efg", "/efg");
505 check_joinbuf_overlapped("abc/", 4, "efg", "efg");
506 check_joinbuf_overlapped("abcd", 3, "", "d/");
507 check_joinbuf_overlapped("abcd", 4, "", "");
508 check_joinbuf_overlapped("abc/", 2, "", "c/");
509 check_joinbuf_overlapped("abc/", 3, "", "/");
510 check_joinbuf_overlapped("abc/", 4, "", "");
511
512 check_joinbuf_n_2("", "", "");
513 check_joinbuf_n_2("", "a", "a");
514 check_joinbuf_n_2("", "/a", "/a");
515 check_joinbuf_n_2("a", "", "a/");
516 check_joinbuf_n_2("a", "/", "a/");
517 check_joinbuf_n_2("a", "b", "a/b");
518 check_joinbuf_n_2("/", "a", "/a");
519 check_joinbuf_n_2("/", "", "/");
520 check_joinbuf_n_2("/a", "/b", "/a/b");
521 check_joinbuf_n_2("/a", "/b/", "/a/b/");
522 check_joinbuf_n_2("/a/", "b/", "/a/b/");
523 check_joinbuf_n_2("/a/", "/b/", "/a/b/");
524 check_joinbuf_n_2("/abcd", "/defg", "/abcd/defg");
525 check_joinbuf_n_2("/abcd", "/defg/", "/abcd/defg/");
526 check_joinbuf_n_2("/abcd/", "defg/", "/abcd/defg/");
527 check_joinbuf_n_2("/abcd/", "/defg/", "/abcd/defg/");
528
529 check_joinbuf_n_4("", "", "", "", "");
530 check_joinbuf_n_4("", "a", "", "", "a;");
531 check_joinbuf_n_4("a", "", "", "", "a;");
532 check_joinbuf_n_4("", "", "", "a", "a");
533 check_joinbuf_n_4("a", "b", "", ";c;d;", "a;b;c;d;");
534 check_joinbuf_n_4("a", "b", "", ";c;d", "a;b;c;d");
535 check_joinbuf_n_4("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop");
536 check_joinbuf_n_4("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;");
537 check_joinbuf_n_4(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;");
538 }
539
540 void test_core_buffer__9(void)
541 {
542 git_buf buf = GIT_BUF_INIT;
543
544 /* just some exhaustive tests of various separator placement */
545 char *a[] = { "", "-", "a-", "-a", "-a-" };
546 char *b[] = { "", "-", "b-", "-b", "-b-" };
547 char sep[] = { 0, '-', '/' };
548 char *expect_null[] = { "", "-", "a-", "-a", "-a-",
549 "-", "--", "a--", "-a-", "-a--",
550 "b-", "-b-", "a-b-", "-ab-", "-a-b-",
551 "-b", "--b", "a--b", "-a-b", "-a--b",
552 "-b-", "--b-", "a--b-", "-a-b-", "-a--b-" };
553 char *expect_dash[] = { "", "-", "a-", "-a-", "-a-",
554 "-", "-", "a-", "-a-", "-a-",
555 "b-", "-b-", "a-b-", "-a-b-", "-a-b-",
556 "-b", "-b", "a-b", "-a-b", "-a-b",
557 "-b-", "-b-", "a-b-", "-a-b-", "-a-b-" };
558 char *expect_slas[] = { "", "-/", "a-/", "-a/", "-a-/",
559 "-", "-/-", "a-/-", "-a/-", "-a-/-",
560 "b-", "-/b-", "a-/b-", "-a/b-", "-a-/b-",
561 "-b", "-/-b", "a-/-b", "-a/-b", "-a-/-b",
562 "-b-", "-/-b-", "a-/-b-", "-a/-b-", "-a-/-b-" };
563 char **expect_values[] = { expect_null, expect_dash, expect_slas };
564 char separator, **expect;
565 unsigned int s, i, j;
566
567 for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {
568 separator = sep[s];
569 expect = expect_values[s];
570
571 for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {
572 for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {
573 git_buf_join(&buf, separator, a[i], b[j]);
574 cl_assert_equal_s(*expect, buf.ptr);
575 expect++;
576 }
577 }
578 }
579
580 git_buf_dispose(&buf);
581 }
582
583 void test_core_buffer__10(void)
584 {
585 git_buf a = GIT_BUF_INIT;
586
587 cl_git_pass(git_buf_join_n(&a, '/', 1, "test"));
588 cl_assert_equal_s(a.ptr, "test");
589 cl_git_pass(git_buf_join_n(&a, '/', 1, "string"));
590 cl_assert_equal_s(a.ptr, "test/string");
591 git_buf_clear(&a);
592 cl_git_pass(git_buf_join_n(&a, '/', 3, "test", "string", "join"));
593 cl_assert_equal_s(a.ptr, "test/string/join");
594 cl_git_pass(git_buf_join_n(&a, '/', 2, a.ptr, "more"));
595 cl_assert_equal_s(a.ptr, "test/string/join/test/string/join/more");
596
597 git_buf_dispose(&a);
598 }
599
600 void test_core_buffer__join3(void)
601 {
602 git_buf a = GIT_BUF_INIT;
603
604 cl_git_pass(git_buf_join3(&a, '/', "test", "string", "join"));
605 cl_assert_equal_s("test/string/join", a.ptr);
606 cl_git_pass(git_buf_join3(&a, '/', "test/", "string", "join"));
607 cl_assert_equal_s("test/string/join", a.ptr);
608 cl_git_pass(git_buf_join3(&a, '/', "test/", "/string", "join"));
609 cl_assert_equal_s("test/string/join", a.ptr);
610 cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "join"));
611 cl_assert_equal_s("test/string/join", a.ptr);
612 cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "/join"));
613 cl_assert_equal_s("test/string/join", a.ptr);
614
615 cl_git_pass(git_buf_join3(&a, '/', "", "string", "join"));
616 cl_assert_equal_s("string/join", a.ptr);
617 cl_git_pass(git_buf_join3(&a, '/', "", "string/", "join"));
618 cl_assert_equal_s("string/join", a.ptr);
619 cl_git_pass(git_buf_join3(&a, '/', "", "string/", "/join"));
620 cl_assert_equal_s("string/join", a.ptr);
621
622 cl_git_pass(git_buf_join3(&a, '/', "string", "", "join"));
623 cl_assert_equal_s("string/join", a.ptr);
624 cl_git_pass(git_buf_join3(&a, '/', "string/", "", "join"));
625 cl_assert_equal_s("string/join", a.ptr);
626 cl_git_pass(git_buf_join3(&a, '/', "string/", "", "/join"));
627 cl_assert_equal_s("string/join", a.ptr);
628
629 git_buf_dispose(&a);
630 }
631
632 void test_core_buffer__11(void)
633 {
634 git_buf a = GIT_BUF_INIT;
635 char *t1[] = { "nothing", "in", "common" };
636 char *t2[] = { "something", "something else", "some other" };
637 char *t3[] = { "something", "some fun", "no fun" };
638 char *t4[] = { "happy", "happier", "happiest" };
639 char *t5[] = { "happiest", "happier", "happy" };
640 char *t6[] = { "no", "nope", "" };
641 char *t7[] = { "", "doesn't matter" };
642
643 cl_git_pass(git_buf_common_prefix(&a, t1, 3));
644 cl_assert_equal_s(a.ptr, "");
645
646 cl_git_pass(git_buf_common_prefix(&a, t2, 3));
647 cl_assert_equal_s(a.ptr, "some");
648
649 cl_git_pass(git_buf_common_prefix(&a, t3, 3));
650 cl_assert_equal_s(a.ptr, "");
651
652 cl_git_pass(git_buf_common_prefix(&a, t4, 3));
653 cl_assert_equal_s(a.ptr, "happ");
654
655 cl_git_pass(git_buf_common_prefix(&a, t5, 3));
656 cl_assert_equal_s(a.ptr, "happ");
657
658 cl_git_pass(git_buf_common_prefix(&a, t6, 3));
659 cl_assert_equal_s(a.ptr, "");
660
661 cl_git_pass(git_buf_common_prefix(&a, t7, 3));
662 cl_assert_equal_s(a.ptr, "");
663
664 git_buf_dispose(&a);
665 }
666
667 void test_core_buffer__rfind_variants(void)
668 {
669 git_buf a = GIT_BUF_INIT;
670 ssize_t len;
671
672 cl_git_pass(git_buf_sets(&a, "/this/is/it/"));
673
674 len = (ssize_t)git_buf_len(&a);
675
676 cl_assert(git_buf_rfind(&a, '/') == len - 1);
677 cl_assert(git_buf_rfind_next(&a, '/') == len - 4);
678
679 cl_assert(git_buf_rfind(&a, 'i') == len - 3);
680 cl_assert(git_buf_rfind_next(&a, 'i') == len - 3);
681
682 cl_assert(git_buf_rfind(&a, 'h') == 2);
683 cl_assert(git_buf_rfind_next(&a, 'h') == 2);
684
685 cl_assert(git_buf_rfind(&a, 'q') == -1);
686 cl_assert(git_buf_rfind_next(&a, 'q') == -1);
687
688 git_buf_dispose(&a);
689 }
690
691 void test_core_buffer__puts_escaped(void)
692 {
693 git_buf a = GIT_BUF_INIT;
694
695 git_buf_clear(&a);
696 cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "", ""));
697 cl_assert_equal_s("this is a test", a.ptr);
698
699 git_buf_clear(&a);
700 cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "t", "\\"));
701 cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);
702
703 git_buf_clear(&a);
704 cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "i ", "__"));
705 cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr);
706
707 git_buf_clear(&a);
708 cl_git_pass(git_buf_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
709 cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr);
710
711 git_buf_dispose(&a);
712 }
713
714 static void assert_unescape(char *expected, char *to_unescape) {
715 git_buf buf = GIT_BUF_INIT;
716
717 cl_git_pass(git_buf_sets(&buf, to_unescape));
718 git_buf_unescape(&buf);
719 cl_assert_equal_s(expected, buf.ptr);
720 cl_assert_equal_sz(strlen(expected), buf.size);
721
722 git_buf_dispose(&buf);
723 }
724
725 void test_core_buffer__unescape(void)
726 {
727 assert_unescape("Escaped\\", "Es\\ca\\ped\\");
728 assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\");
729 assert_unescape("\\", "\\");
730 assert_unescape("\\", "\\\\");
731 assert_unescape("", "");
732 }
733
734 void test_core_buffer__encode_base64(void)
735 {
736 git_buf buf = GIT_BUF_INIT;
737
738 /* t h i s
739 * 0x 74 68 69 73
740 * 0b 01110100 01101000 01101001 01110011
741 * 0b 011101 000110 100001 101001 011100 110000
742 * 0x 1d 06 21 29 1c 30
743 * d G h p c w
744 */
745 cl_git_pass(git_buf_encode_base64(&buf, "this", 4));
746 cl_assert_equal_s("dGhpcw==", buf.ptr);
747
748 git_buf_clear(&buf);
749 cl_git_pass(git_buf_encode_base64(&buf, "this!", 5));
750 cl_assert_equal_s("dGhpcyE=", buf.ptr);
751
752 git_buf_clear(&buf);
753 cl_git_pass(git_buf_encode_base64(&buf, "this!\n", 6));
754 cl_assert_equal_s("dGhpcyEK", buf.ptr);
755
756 git_buf_dispose(&buf);
757 }
758
759 void test_core_buffer__decode_base64(void)
760 {
761 git_buf buf = GIT_BUF_INIT;
762
763 cl_git_pass(git_buf_decode_base64(&buf, "dGhpcw==", 8));
764 cl_assert_equal_s("this", buf.ptr);
765
766 git_buf_clear(&buf);
767 cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyE=", 8));
768 cl_assert_equal_s("this!", buf.ptr);
769
770 git_buf_clear(&buf);
771 cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyEK", 8));
772 cl_assert_equal_s("this!\n", buf.ptr);
773
774 cl_git_fail(git_buf_decode_base64(&buf, "This is not a valid base64 string!!!", 36));
775 cl_assert_equal_s("this!\n", buf.ptr);
776
777 git_buf_dispose(&buf);
778 }
779
780 void test_core_buffer__encode_base85(void)
781 {
782 git_buf buf = GIT_BUF_INIT;
783
784 cl_git_pass(git_buf_encode_base85(&buf, "this", 4));
785 cl_assert_equal_s("bZBXF", buf.ptr);
786 git_buf_clear(&buf);
787
788 cl_git_pass(git_buf_encode_base85(&buf, "two rnds", 8));
789 cl_assert_equal_s("ba!tca&BaE", buf.ptr);
790 git_buf_clear(&buf);
791
792 cl_git_pass(git_buf_encode_base85(&buf, "this is base 85 encoded",
793 strlen("this is base 85 encoded")));
794 cl_assert_equal_s("bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", buf.ptr);
795 git_buf_clear(&buf);
796
797 git_buf_dispose(&buf);
798 }
799
800 void test_core_buffer__decode_base85(void)
801 {
802 git_buf buf = GIT_BUF_INIT;
803
804 cl_git_pass(git_buf_decode_base85(&buf, "bZBXF", 5, 4));
805 cl_assert_equal_sz(4, buf.size);
806 cl_assert_equal_s("this", buf.ptr);
807 git_buf_clear(&buf);
808
809 cl_git_pass(git_buf_decode_base85(&buf, "ba!tca&BaE", 10, 8));
810 cl_assert_equal_sz(8, buf.size);
811 cl_assert_equal_s("two rnds", buf.ptr);
812 git_buf_clear(&buf);
813
814 cl_git_pass(git_buf_decode_base85(&buf, "bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", 30, 23));
815 cl_assert_equal_sz(23, buf.size);
816 cl_assert_equal_s("this is base 85 encoded", buf.ptr);
817 git_buf_clear(&buf);
818
819 git_buf_dispose(&buf);
820 }
821
822 void test_core_buffer__decode_base85_fails_gracefully(void)
823 {
824 git_buf buf = GIT_BUF_INIT;
825
826 git_buf_puts(&buf, "foobar");
827
828 cl_git_fail(git_buf_decode_base85(&buf, "invalid charsZZ", 15, 42));
829 cl_git_fail(git_buf_decode_base85(&buf, "invalidchars__ ", 15, 42));
830 cl_git_fail(git_buf_decode_base85(&buf, "overflowZZ~~~~~", 15, 42));
831 cl_git_fail(git_buf_decode_base85(&buf, "truncated", 9, 42));
832 cl_assert_equal_sz(6, buf.size);
833 cl_assert_equal_s("foobar", buf.ptr);
834
835 git_buf_dispose(&buf);
836 }
837
838 void test_core_buffer__classify_with_utf8(void)
839 {
840 char *data0 = "Simple text\n";
841 size_t data0len = 12;
842 char *data1 = "Is that UTF-8 data I see…\nYep!\n";
843 size_t data1len = 31;
844 char *data2 = "Internal NUL!!!\000\n\nI see you!\n";
845 size_t data2len = 29;
846 char *data3 = "\xef\xbb\xbfThis is UTF-8 with a BOM.\n";
847 size_t data3len = 20;
848 git_buf b;
849
850 b.ptr = data0; b.size = b.asize = data0len;
851 cl_assert(!git_buf_is_binary(&b));
852 cl_assert(!git_buf_contains_nul(&b));
853
854 b.ptr = data1; b.size = b.asize = data1len;
855 cl_assert(!git_buf_is_binary(&b));
856 cl_assert(!git_buf_contains_nul(&b));
857
858 b.ptr = data2; b.size = b.asize = data2len;
859 cl_assert(git_buf_is_binary(&b));
860 cl_assert(git_buf_contains_nul(&b));
861
862 b.ptr = data3; b.size = b.asize = data3len;
863 cl_assert(!git_buf_is_binary(&b));
864 cl_assert(!git_buf_contains_nul(&b));
865 }
866
867 #define SIMILARITY_TEST_DATA_1 \
868 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
869 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
870 "020\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
871 "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
872 "040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
873
874 void test_core_buffer__similarity_metric(void)
875 {
876 git_hashsig *a, *b;
877 git_buf buf = GIT_BUF_INIT;
878 int sim;
879
880 /* in the first case, we compare data to itself and expect 100% match */
881
882 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
883 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
884 cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
885
886 cl_assert_equal_i(100, git_hashsig_compare(a, b));
887
888 git_hashsig_free(a);
889 git_hashsig_free(b);
890
891 /* if we change just a single byte, how much does that change magnify? */
892
893 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
894 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
895 cl_git_pass(git_buf_sets(&buf,
896 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
897 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
898 "x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
899 "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
900 "040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
901 ));
902 cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
903
904 sim = git_hashsig_compare(a, b);
905
906 cl_assert_in_range(95, sim, 100); /* expect >95% similarity */
907
908 git_hashsig_free(a);
909 git_hashsig_free(b);
910
911 /* let's try comparing data to a superset of itself */
912
913 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
914 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
915 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1
916 "050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n"));
917 cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
918
919 sim = git_hashsig_compare(a, b);
920 /* 20% lines added ~= 10% lines changed */
921
922 cl_assert_in_range(85, sim, 95); /* expect similarity around 90% */
923
924 git_hashsig_free(a);
925 git_hashsig_free(b);
926
927 /* what if we keep about half the original data and add half new */
928
929 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
930 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
931 cl_git_pass(git_buf_sets(&buf,
932 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
933 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
934 "020x\n021\n022\n023\n024\n" \
935 "x25\nx26\nx27\nx28\nx29\n" \
936 "x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \
937 "x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n"
938 ));
939 cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
940
941 sim = git_hashsig_compare(a, b);
942 /* 50% lines changed */
943
944 cl_assert_in_range(40, sim, 60); /* expect in the 40-60% similarity range */
945
946 git_hashsig_free(a);
947 git_hashsig_free(b);
948
949 /* lastly, let's check that we can hash file content as well */
950
951 cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
952 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
953
954 cl_git_pass(git_futils_mkdir("scratch", 0755, GIT_MKDIR_PATH));
955 cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
956 cl_git_pass(git_hashsig_create_fromfile(
957 &b, "scratch/testdata", GIT_HASHSIG_NORMAL));
958
959 cl_assert_equal_i(100, git_hashsig_compare(a, b));
960
961 git_hashsig_free(a);
962 git_hashsig_free(b);
963
964 git_buf_dispose(&buf);
965 git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES);
966 }
967
968
969 void test_core_buffer__similarity_metric_whitespace(void)
970 {
971 git_hashsig *a, *b;
972 git_buf buf = GIT_BUF_INIT;
973 int sim, i, j;
974 git_hashsig_option_t opt;
975 const char *tabbed =
976 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
977 " separator = sep[s];\n"
978 " expect = expect_values[s];\n"
979 "\n"
980 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
981 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
982 " git_buf_join(&buf, separator, a[i], b[j]);\n"
983 " cl_assert_equal_s(*expect, buf.ptr);\n"
984 " expect++;\n"
985 " }\n"
986 " }\n"
987 " }\n";
988 const char *spaced =
989 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
990 " separator = sep[s];\n"
991 " expect = expect_values[s];\n"
992 "\n"
993 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
994 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
995 " git_buf_join(&buf, separator, a[i], b[j]);\n"
996 " cl_assert_equal_s(*expect, buf.ptr);\n"
997 " expect++;\n"
998 " }\n"
999 " }\n"
1000 " }\n";
1001 const char *crlf_spaced2 =
1002 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
1003 " separator = sep[s];\r\n"
1004 " expect = expect_values[s];\r\n"
1005 "\r\n"
1006 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
1007 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
1008 " git_buf_join(&buf, separator, a[i], b[j]);\r\n"
1009 " cl_assert_equal_s(*expect, buf.ptr);\r\n"
1010 " expect++;\r\n"
1011 " }\r\n"
1012 " }\r\n"
1013 " }\r\n";
1014 const char *text[3] = { tabbed, spaced, crlf_spaced2 };
1015
1016 /* let's try variations of our own code with whitespace changes */
1017
1018 for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) {
1019 for (i = 0; i < 3; ++i) {
1020 for (j = 0; j < 3; ++j) {
1021 cl_git_pass(git_buf_sets(&buf, text[i]));
1022 cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
1023
1024 cl_git_pass(git_buf_sets(&buf, text[j]));
1025 cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
1026
1027 sim = git_hashsig_compare(a, b);
1028
1029 if (opt == GIT_HASHSIG_NORMAL) {
1030 if (i == j)
1031 cl_assert_equal_i(100, sim);
1032 else
1033 cl_assert_in_range(0, sim, 30); /* pretty different */
1034 } else {
1035 cl_assert_equal_i(100, sim);
1036 }
1037
1038 git_hashsig_free(a);
1039 git_hashsig_free(b);
1040 }
1041 }
1042 }
1043
1044 git_buf_dispose(&buf);
1045 }
1046
1047 #include "../filter/crlf.h"
1048
1049 #define check_buf(expected,buf) do { \
1050 cl_assert_equal_s(expected, buf.ptr); \
1051 cl_assert_equal_sz(strlen(expected), buf.size); } while (0)
1052
1053 void test_core_buffer__lf_and_crlf_conversions(void)
1054 {
1055 git_buf src = GIT_BUF_INIT, tgt = GIT_BUF_INIT;
1056
1057 /* LF source */
1058
1059 git_buf_sets(&src, "lf\nlf\nlf\nlf\n");
1060
1061 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1062 check_buf("lf\r\nlf\r\nlf\r\nlf\r\n", tgt);
1063
1064 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1065 check_buf(src.ptr, tgt);
1066
1067 git_buf_sets(&src, "\nlf\nlf\nlf\nlf\nlf");
1068
1069 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1070 check_buf("\r\nlf\r\nlf\r\nlf\r\nlf\r\nlf", tgt);
1071
1072 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1073 check_buf(src.ptr, tgt);
1074
1075 /* CRLF source */
1076
1077 git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
1078
1079 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1080 check_buf("crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n", tgt);
1081
1082 git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
1083
1084 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1085 check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt);
1086
1087 git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
1088
1089 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1090 check_buf("\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf", tgt);
1091
1092 git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
1093
1094 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1095 check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt);
1096
1097 /* CRLF in LF text */
1098
1099 git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
1100
1101 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1102 check_buf("\r\nlf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\ncrlf\r\n", tgt);
1103
1104 git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
1105
1106 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1107 check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt);
1108
1109 /* LF in CRLF text */
1110
1111 git_buf_sets(&src, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf");
1112
1113 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1114 check_buf("\r\ncrlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf", tgt);
1115
1116 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1117 check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt);
1118
1119 /* bare CR test */
1120
1121 git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
1122
1123 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1124 check_buf("\rcrlf\r\nlf\r\nlf\r\ncr\rcrlf\r\nlf\r\ncr\r", tgt);
1125
1126 git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
1127
1128 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1129 check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt);
1130
1131 git_buf_sets(&src, "\rcr\r");
1132 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1133 check_buf(src.ptr, tgt);
1134 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1135 check_buf("\rcr\r", tgt);
1136
1137 git_buf_dispose(&src);
1138 git_buf_dispose(&tgt);
1139
1140 /* blob correspondence tests */
1141
1142 git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
1143 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1144 check_buf(ALL_CRLF_TEXT_AS_CRLF, tgt);
1145 git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
1146 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1147 check_buf(ALL_CRLF_TEXT_AS_LF, tgt);
1148 git_buf_dispose(&src);
1149 git_buf_dispose(&tgt);
1150
1151 git_buf_sets(&src, ALL_LF_TEXT_RAW);
1152 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1153 check_buf(ALL_LF_TEXT_AS_CRLF, tgt);
1154 git_buf_sets(&src, ALL_LF_TEXT_RAW);
1155 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1156 check_buf(ALL_LF_TEXT_AS_LF, tgt);
1157 git_buf_dispose(&src);
1158 git_buf_dispose(&tgt);
1159
1160 git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
1161 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1162 check_buf(MORE_CRLF_TEXT_AS_CRLF, tgt);
1163 git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
1164 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1165 check_buf(MORE_CRLF_TEXT_AS_LF, tgt);
1166 git_buf_dispose(&src);
1167 git_buf_dispose(&tgt);
1168
1169 git_buf_sets(&src, MORE_LF_TEXT_RAW);
1170 cl_git_pass(git_buf_lf_to_crlf(&tgt, &src));
1171 check_buf(MORE_LF_TEXT_AS_CRLF, tgt);
1172 git_buf_sets(&src, MORE_LF_TEXT_RAW);
1173 cl_git_pass(git_buf_crlf_to_lf(&tgt, &src));
1174 check_buf(MORE_LF_TEXT_AS_LF, tgt);
1175 git_buf_dispose(&src);
1176 git_buf_dispose(&tgt);
1177 }
1178
1179 void test_core_buffer__dont_grow_borrowed(void)
1180 {
1181 const char *somestring = "blah blah";
1182 git_buf buf = GIT_BUF_INIT;
1183
1184 git_buf_attach_notowned(&buf, somestring, strlen(somestring) + 1);
1185 cl_assert_equal_p(somestring, buf.ptr);
1186 cl_assert_equal_i(0, buf.asize);
1187 cl_assert_equal_i(strlen(somestring) + 1, buf.size);
1188
1189 cl_git_fail_with(GIT_EINVALID, git_buf_grow(&buf, 1024));
1190 }
1191
1192 void test_core_buffer__dont_hit_infinite_loop_when_resizing(void)
1193 {
1194 git_buf buf = GIT_BUF_INIT;
1195
1196 cl_git_pass(git_buf_puts(&buf, "foobar"));
1197 /*
1198 * We do not care whether this succeeds or fails, which
1199 * would depend on platform-specific allocation
1200 * semantics. We only want to know that the function
1201 * actually returns.
1202 */
1203 (void)git_buf_try_grow(&buf, SIZE_MAX, true);
1204
1205 git_buf_dispose(&buf);
1206 }
1207
1208 void test_core_buffer__avoid_printing_into_oom_buffer(void)
1209 {
1210 git_buf buf = GIT_BUF_INIT;
1211
1212 /* Emulate OOM situation with a previous allocation */
1213 buf.asize = 8;
1214 buf.ptr = git_buf__oom;
1215
1216 /*
1217 * Print the same string again. As the buffer still has
1218 * an `asize` of 8 due to the previous print,
1219 * `ENSURE_SIZE` would not try to reallocate the array at
1220 * all. As it didn't explicitly check for `git_buf__oom`
1221 * in earlier versions, this would've resulted in it
1222 * returning successfully and thus `git_buf_puts` would
1223 * just print into the `git_buf__oom` array.
1224 */
1225 cl_git_fail(git_buf_puts(&buf, "foobar"));
1226 }