1 #include "clar_libgit2.h"
2 #include "path.h"
3 
4 #ifdef GIT_USE_ICONV
5 static git_path_iconv_t ic;
6 static char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
7 static char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
8 #endif
9 
test_core_iconv__initialize(void)10 void test_core_iconv__initialize(void)
11 {
12 #ifdef GIT_USE_ICONV
13 	cl_git_pass(git_path_iconv_init_precompose(&ic));
14 #endif
15 }
16 
test_core_iconv__cleanup(void)17 void test_core_iconv__cleanup(void)
18 {
19 #ifdef GIT_USE_ICONV
20 	git_path_iconv_clear(&ic);
21 #endif
22 }
23 
test_core_iconv__unchanged(void)24 void test_core_iconv__unchanged(void)
25 {
26 #ifdef GIT_USE_ICONV
27 	const char *data = "Ascii data", *original = data;
28 	size_t datalen = strlen(data);
29 
30 	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
31 	GIT_UNUSED(datalen);
32 
33 	/* There are no high bits set, so this should leave data untouched */
34 	cl_assert(data == original);
35 #endif
36 }
37 
test_core_iconv__decomposed_to_precomposed(void)38 void test_core_iconv__decomposed_to_precomposed(void)
39 {
40 #ifdef GIT_USE_ICONV
41 	const char *data = nfd;
42 	size_t datalen, nfdlen = strlen(nfd);
43 
44 	datalen = nfdlen;
45 	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
46 	GIT_UNUSED(datalen);
47 
48 	/* The decomposed nfd string should be transformed to the nfc form
49 	 * (on platforms where iconv is enabled, of course).
50 	 */
51 	cl_assert_equal_s(nfc, data);
52 
53 	/* should be able to do it multiple times with the same git_path_iconv_t */
54 	data = nfd; datalen = nfdlen;
55 	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
56 	cl_assert_equal_s(nfc, data);
57 
58 	data = nfd; datalen = nfdlen;
59 	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
60 	cl_assert_equal_s(nfc, data);
61 #endif
62 }
63 
test_core_iconv__precomposed_is_unmodified(void)64 void test_core_iconv__precomposed_is_unmodified(void)
65 {
66 #ifdef GIT_USE_ICONV
67 	const char *data = nfc;
68 	size_t datalen = strlen(nfc);
69 
70 	cl_git_pass(git_path_iconv(&ic, &data, &datalen));
71 	GIT_UNUSED(datalen);
72 
73 	/* data is already in precomposed form, so even though some bytes have
74 	 * the high-bit set, the iconv transform should result in no change.
75 	 */
76 	cl_assert_equal_s(nfc, data);
77 #endif
78 }
79