2 #include "clar_libgit2.h"
5 #include "win32/path_w32.h"
9 static void test_utf8_to_utf16(const char *utf8_in
, const wchar_t *utf16_expected
)
11 git_win32_path path_utf16
;
14 cl_assert((path_utf16len
= git_win32_path_from_utf8(path_utf16
, utf8_in
)) >= 0);
15 cl_assert_equal_wcs(utf16_expected
, path_utf16
);
16 cl_assert_equal_i(wcslen(utf16_expected
), path_utf16len
);
19 static void test_utf8_to_utf16_relative(const char* utf8_in
, const wchar_t* utf16_expected
)
21 git_win32_path path_utf16
;
24 cl_assert((path_utf16len
= git_win32_path_relative_from_utf8(path_utf16
, utf8_in
)) >= 0);
25 cl_assert_equal_wcs(utf16_expected
, path_utf16
);
26 cl_assert_equal_i(wcslen(utf16_expected
), path_utf16len
);
30 void test_path_win32__utf8_to_utf16(void)
33 test_utf8_to_utf16("C:\\", L
"\\\\?\\C:\\");
34 test_utf8_to_utf16("c:\\", L
"\\\\?\\c:\\");
35 test_utf8_to_utf16("C:/", L
"\\\\?\\C:\\");
36 test_utf8_to_utf16("c:/", L
"\\\\?\\c:\\");
40 void test_path_win32__removes_trailing_slash(void)
43 test_utf8_to_utf16("C:\\Foo\\", L
"\\\\?\\C:\\Foo");
44 test_utf8_to_utf16("C:\\Foo\\\\", L
"\\\\?\\C:\\Foo");
45 test_utf8_to_utf16("C:\\Foo\\\\", L
"\\\\?\\C:\\Foo");
46 test_utf8_to_utf16("C:/Foo/", L
"\\\\?\\C:\\Foo");
47 test_utf8_to_utf16("C:/Foo///", L
"\\\\?\\C:\\Foo");
51 void test_path_win32__squashes_multiple_slashes(void)
54 test_utf8_to_utf16("C:\\\\Foo\\Bar\\\\Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
55 test_utf8_to_utf16("C://Foo/Bar///Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
59 void test_path_win32__unc(void)
62 test_utf8_to_utf16("\\\\server\\c$\\unc\\path", L
"\\\\?\\UNC\\server\\c$\\unc\\path");
63 test_utf8_to_utf16("//server/git/style/unc/path", L
"\\\\?\\UNC\\server\\git\\style\\unc\\path");
67 void test_path_win32__honors_max_path(void)
70 git_win32_path path_utf16
;
72 test_utf8_to_utf16("C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk",
73 L
"\\\\?\\C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk");
75 cl_check_fail(git_win32_path_from_utf8(path_utf16
, "C:\\This path is 4097 chars and exceeds our maximum path length on Windows which is limited to 4096 characters\\alas\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij01"));
80 void test_path_win32__dot_and_dotdot(void)
83 test_utf8_to_utf16("C:\\Foo\\..\\Foobar", L
"\\\\?\\C:\\Foobar");
84 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar", L
"\\\\?\\C:\\Foo\\Foobar");
85 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar\\..", L
"\\\\?\\C:\\Foo");
86 test_utf8_to_utf16("C:\\Foobar\\..", L
"\\\\?\\C:\\");
87 test_utf8_to_utf16("C:/Foo/Bar/../Foobar", L
"\\\\?\\C:\\Foo\\Foobar");
88 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/../Asdf/", L
"\\\\?\\C:\\Foo\\Asdf");
89 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/..", L
"\\\\?\\C:\\Foo");
90 test_utf8_to_utf16("C:/Foo/..", L
"\\\\?\\C:\\");
92 test_utf8_to_utf16("C:\\Foo\\Bar\\.\\Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
93 test_utf8_to_utf16("C:\\.\\Foo\\.\\Bar\\.\\Foobar\\.\\", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
94 test_utf8_to_utf16("C:/Foo/Bar/./Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
95 test_utf8_to_utf16("C:/Foo/../Bar/./Foobar/../", L
"\\\\?\\C:\\Bar");
97 test_utf8_to_utf16("C:\\Foo\\..\\..\\Bar", L
"\\\\?\\C:\\Bar");
101 void test_path_win32__absolute_from_no_drive_letter(void)
104 test_utf8_to_utf16("\\Foo", L
"\\\\?\\C:\\Foo");
105 test_utf8_to_utf16("\\Foo\\Bar", L
"\\\\?\\C:\\Foo\\Bar");
106 test_utf8_to_utf16("/Foo/Bar", L
"\\\\?\\C:\\Foo\\Bar");
110 void test_path_win32__absolute_from_relative(void)
113 char cwd_backup
[MAX_PATH
];
115 cl_must_pass(p_getcwd(cwd_backup
, MAX_PATH
));
116 cl_must_pass(p_chdir("C:/"));
118 test_utf8_to_utf16("Foo", L
"\\\\?\\C:\\Foo");
119 test_utf8_to_utf16("..\\..\\Foo", L
"\\\\?\\C:\\Foo");
120 test_utf8_to_utf16("Foo\\..", L
"\\\\?\\C:\\");
121 test_utf8_to_utf16("Foo\\..\\..", L
"\\\\?\\C:\\");
122 test_utf8_to_utf16("", L
"\\\\?\\C:\\");
124 cl_must_pass(p_chdir("C:/Windows"));
126 test_utf8_to_utf16("Foo", L
"\\\\?\\C:\\Windows\\Foo");
127 test_utf8_to_utf16("Foo\\Bar", L
"\\\\?\\C:\\Windows\\Foo\\Bar");
128 test_utf8_to_utf16("..\\Foo", L
"\\\\?\\C:\\Foo");
129 test_utf8_to_utf16("Foo\\..\\Bar", L
"\\\\?\\C:\\Windows\\Bar");
130 test_utf8_to_utf16("", L
"\\\\?\\C:\\Windows");
132 cl_must_pass(p_chdir(cwd_backup
));
136 void test_path_win32__keeps_relative(void)
139 /* Relative paths stay relative */
140 test_utf8_to_utf16_relative("Foo", L
"Foo");
141 test_utf8_to_utf16_relative("..\\..\\Foo", L
"..\\..\\Foo");
142 test_utf8_to_utf16_relative("Foo\\..", L
"Foo\\..");
143 test_utf8_to_utf16_relative("Foo\\..\\..", L
"Foo\\..\\..");
144 test_utf8_to_utf16_relative("Foo\\Bar", L
"Foo\\Bar");
145 test_utf8_to_utf16_relative("Foo\\..\\Bar", L
"Foo\\..\\Bar");
146 test_utf8_to_utf16_relative("../../Foo", L
"..\\..\\Foo");
147 test_utf8_to_utf16_relative("Foo/..", L
"Foo\\..");
148 test_utf8_to_utf16_relative("Foo/../..", L
"Foo\\..\\..");
149 test_utf8_to_utf16_relative("Foo/Bar", L
"Foo\\Bar");
150 test_utf8_to_utf16_relative("Foo/../Bar", L
"Foo\\..\\Bar");
151 test_utf8_to_utf16_relative("Foo/../Bar/", L
"Foo\\..\\Bar\\");
152 test_utf8_to_utf16_relative("", L
"");
154 /* Absolute paths are canonicalized */
155 test_utf8_to_utf16_relative("\\Foo", L
"\\\\?\\C:\\Foo");
156 test_utf8_to_utf16_relative("/Foo/Bar/", L
"\\\\?\\C:\\Foo\\Bar");
157 test_utf8_to_utf16_relative("\\\\server\\c$\\unc\\path", L
"\\\\?\\UNC\\server\\c$\\unc\\path");
162 static void test_canonicalize(const wchar_t *in
, const wchar_t *expected
)
164 git_win32_path canonical
;
166 cl_assert(wcslen(in
) < MAX_PATH
);
167 wcscpy(canonical
, in
);
169 cl_must_pass(git_win32_path_canonicalize(canonical
));
170 cl_assert_equal_wcs(expected
, canonical
);
174 static void test_remove_namespace(const wchar_t *in
, const wchar_t *expected
)
177 git_win32_path canonical
;
179 cl_assert(wcslen(in
) < MAX_PATH
);
180 wcscpy(canonical
, in
);
182 git_win32_path_remove_namespace(canonical
, wcslen(in
));
183 cl_assert_equal_wcs(expected
, canonical
);
186 GIT_UNUSED(expected
);
190 void test_path_win32__remove_namespace(void)
192 test_remove_namespace(L
"\\\\?\\C:\\Temp\\Foo", L
"C:\\Temp\\Foo");
193 test_remove_namespace(L
"\\\\?\\C:\\", L
"C:\\");
194 test_remove_namespace(L
"\\\\?\\", L
"");
196 test_remove_namespace(L
"\\??\\C:\\Temp\\Foo", L
"C:\\Temp\\Foo");
197 test_remove_namespace(L
"\\??\\C:\\", L
"C:\\");
198 test_remove_namespace(L
"\\??\\", L
"");
200 test_remove_namespace(L
"\\\\?\\UNC\\server\\C$\\folder", L
"\\\\server\\C$\\folder");
201 test_remove_namespace(L
"\\\\?\\UNC\\server\\C$\\folder", L
"\\\\server\\C$\\folder");
202 test_remove_namespace(L
"\\\\?\\UNC\\server\\C$", L
"\\\\server\\C$");
203 test_remove_namespace(L
"\\\\?\\UNC\\server\\", L
"\\\\server");
204 test_remove_namespace(L
"\\\\?\\UNC\\server", L
"\\\\server");
206 test_remove_namespace(L
"\\??\\UNC\\server\\C$\\folder", L
"\\\\server\\C$\\folder");
207 test_remove_namespace(L
"\\??\\UNC\\server\\C$\\folder", L
"\\\\server\\C$\\folder");
208 test_remove_namespace(L
"\\??\\UNC\\server\\C$", L
"\\\\server\\C$");
209 test_remove_namespace(L
"\\??\\UNC\\server\\", L
"\\\\server");
210 test_remove_namespace(L
"\\??\\UNC\\server", L
"\\\\server");
212 test_remove_namespace(L
"\\\\server\\C$\\folder", L
"\\\\server\\C$\\folder");
213 test_remove_namespace(L
"\\\\server\\C$", L
"\\\\server\\C$");
214 test_remove_namespace(L
"\\\\server\\", L
"\\\\server");
215 test_remove_namespace(L
"\\\\server", L
"\\\\server");
217 test_remove_namespace(L
"C:\\Foo\\Bar", L
"C:\\Foo\\Bar");
218 test_remove_namespace(L
"C:\\", L
"C:\\");
219 test_remove_namespace(L
"", L
"");
223 void test_path_win32__canonicalize(void)
226 test_canonicalize(L
"C:\\Foo\\Bar", L
"C:\\Foo\\Bar");
227 test_canonicalize(L
"C:\\Foo\\", L
"C:\\Foo");
228 test_canonicalize(L
"C:\\Foo\\\\", L
"C:\\Foo");
229 test_canonicalize(L
"C:\\Foo\\..\\Bar", L
"C:\\Bar");
230 test_canonicalize(L
"C:\\Foo\\..\\..\\Bar", L
"C:\\Bar");
231 test_canonicalize(L
"C:\\Foo\\..\\..\\..\\..\\", L
"C:\\");
232 test_canonicalize(L
"C:/Foo/Bar", L
"C:\\Foo\\Bar");
233 test_canonicalize(L
"C:/", L
"C:\\");
235 test_canonicalize(L
"\\\\?\\C:\\Foo\\Bar", L
"\\\\?\\C:\\Foo\\Bar");
236 test_canonicalize(L
"\\\\?\\C:\\Foo\\Bar\\", L
"\\\\?\\C:\\Foo\\Bar");
237 test_canonicalize(L
"\\\\?\\C:\\\\Foo\\.\\Bar\\\\..\\", L
"\\\\?\\C:\\Foo");
238 test_canonicalize(L
"\\\\?\\C:\\\\", L
"\\\\?\\C:\\");
239 test_canonicalize(L
"//?/C:/", L
"\\\\?\\C:\\");
240 test_canonicalize(L
"//?/C:/../../Foo/", L
"\\\\?\\C:\\Foo");
241 test_canonicalize(L
"//?/C:/Foo/../../", L
"\\\\?\\C:\\");
243 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder", L
"\\\\?\\UNC\\server\\C$\\folder");
244 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\", L
"\\\\?\\UNC\\server\\C$\\folder");
245 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\", L
"\\\\?\\UNC\\server\\C$\\folder");
246 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\..\\..\\..\\..\\share\\", L
"\\\\?\\UNC\\server\\share");
248 test_canonicalize(L
"\\\\server\\share", L
"\\\\server\\share");
249 test_canonicalize(L
"\\\\server\\share\\", L
"\\\\server\\share");
250 test_canonicalize(L
"\\\\server\\share\\\\foo\\\\bar", L
"\\\\server\\share\\foo\\bar");
251 test_canonicalize(L
"\\\\server\\\\share\\\\foo\\\\bar", L
"\\\\server\\share\\foo\\bar");
252 test_canonicalize(L
"\\\\server\\share\\..\\foo", L
"\\\\server\\foo");
253 test_canonicalize(L
"\\\\server\\..\\..\\share\\.\\foo", L
"\\\\server\\share\\foo");
257 void test_path_win32__8dot3_name(void)
262 if (!cl_sandbox_supports_8dot3())
265 /* Some guaranteed short names */
266 cl_assert_equal_s("PROGRA~1", (shortname
= git_win32_path_8dot3_name("C:\\Program Files")));
267 git__free(shortname
);
269 cl_assert_equal_s("WINDOWS", (shortname
= git_win32_path_8dot3_name("C:\\WINDOWS")));
270 git__free(shortname
);
272 /* Create some predictable short names */
273 cl_must_pass(p_mkdir(".foo", 0777));
274 cl_assert_equal_s("FOO~1", (shortname
= git_win32_path_8dot3_name(".foo")));
275 git__free(shortname
);
277 cl_git_write2file("bar~1", "foobar\n", 7, O_RDWR
|O_CREAT
, 0666);
278 cl_must_pass(p_mkdir(".bar", 0777));
279 cl_assert_equal_s("BAR~2", (shortname
= git_win32_path_8dot3_name(".bar")));
280 git__free(shortname
);