2 #include "clar_libgit2.h"
6 #include "win32/path_w32.h"
8 static bool is_8dot3_disabled(void)
10 wchar_t shortPath
[MAX_PATH
];
11 wchar_t *dest
= L
"longer_than_8dot3";
12 FILE *fp
= _wfopen(dest
, L
"w");
15 cl_assert(GetShortPathNameW(dest
, shortPath
, MAX_PATH
) > 0);
17 return !wcscmp(dest
, shortPath
);
21 void test_utf8_to_utf16(const char *utf8_in
, const wchar_t *utf16_expected
)
24 git_win32_path path_utf16
;
27 cl_assert((path_utf16len
= git_win32_path_from_utf8(path_utf16
, utf8_in
)) >= 0);
28 cl_assert_equal_wcs(utf16_expected
, path_utf16
);
29 cl_assert_equal_i(wcslen(utf16_expected
), path_utf16len
);
32 GIT_UNUSED(utf16_expected
);
36 void test_path_win32__utf8_to_utf16(void)
39 test_utf8_to_utf16("C:\\", L
"\\\\?\\C:\\");
40 test_utf8_to_utf16("c:\\", L
"\\\\?\\c:\\");
41 test_utf8_to_utf16("C:/", L
"\\\\?\\C:\\");
42 test_utf8_to_utf16("c:/", L
"\\\\?\\c:\\");
46 void test_path_win32__removes_trailing_slash(void)
49 test_utf8_to_utf16("C:\\Foo\\", L
"\\\\?\\C:\\Foo");
50 test_utf8_to_utf16("C:\\Foo\\\\", L
"\\\\?\\C:\\Foo");
51 test_utf8_to_utf16("C:\\Foo\\\\", L
"\\\\?\\C:\\Foo");
52 test_utf8_to_utf16("C:/Foo/", L
"\\\\?\\C:\\Foo");
53 test_utf8_to_utf16("C:/Foo///", L
"\\\\?\\C:\\Foo");
57 void test_path_win32__squashes_multiple_slashes(void)
60 test_utf8_to_utf16("C:\\\\Foo\\Bar\\\\Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
61 test_utf8_to_utf16("C://Foo/Bar///Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
65 void test_path_win32__unc(void)
68 test_utf8_to_utf16("\\\\server\\c$\\unc\\path", L
"\\\\?\\UNC\\server\\c$\\unc\\path");
69 test_utf8_to_utf16("//server/git/style/unc/path", L
"\\\\?\\UNC\\server\\git\\style\\unc\\path");
73 void test_path_win32__honors_max_path(void)
76 git_win32_path path_utf16
;
78 test_utf8_to_utf16("C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
79 L
"\\\\?\\C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
80 test_utf8_to_utf16("\\\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
81 L
"\\\\?\\UNC\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
83 cl_check_fail(git_win32_path_from_utf8(path_utf16
, "C:\\This path is 260 chars and is sadly too long for windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
84 cl_check_fail(git_win32_path_from_utf8(path_utf16
, "\\\\unc\\paths are also bound by 260 character restrictions\\including the server name portion\\bcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
88 void test_path_win32__dot_and_dotdot(void)
91 test_utf8_to_utf16("C:\\Foo\\..\\Foobar", L
"\\\\?\\C:\\Foobar");
92 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar", L
"\\\\?\\C:\\Foo\\Foobar");
93 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar\\..", L
"\\\\?\\C:\\Foo");
94 test_utf8_to_utf16("C:\\Foobar\\..", L
"\\\\?\\C:\\");
95 test_utf8_to_utf16("C:/Foo/Bar/../Foobar", L
"\\\\?\\C:\\Foo\\Foobar");
96 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/../Asdf/", L
"\\\\?\\C:\\Foo\\Asdf");
97 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/..", L
"\\\\?\\C:\\Foo");
98 test_utf8_to_utf16("C:/Foo/..", L
"\\\\?\\C:\\");
100 test_utf8_to_utf16("C:\\Foo\\Bar\\.\\Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
101 test_utf8_to_utf16("C:\\.\\Foo\\.\\Bar\\.\\Foobar\\.\\", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
102 test_utf8_to_utf16("C:/Foo/Bar/./Foobar", L
"\\\\?\\C:\\Foo\\Bar\\Foobar");
103 test_utf8_to_utf16("C:/Foo/../Bar/./Foobar/../", L
"\\\\?\\C:\\Bar");
105 test_utf8_to_utf16("C:\\Foo\\..\\..\\Bar", L
"\\\\?\\C:\\Bar");
109 void test_path_win32__absolute_from_no_drive_letter(void)
112 test_utf8_to_utf16("\\Foo", L
"\\\\?\\C:\\Foo");
113 test_utf8_to_utf16("\\Foo\\Bar", L
"\\\\?\\C:\\Foo\\Bar");
114 test_utf8_to_utf16("/Foo/Bar", L
"\\\\?\\C:\\Foo\\Bar");
118 void test_path_win32__absolute_from_relative(void)
121 char cwd_backup
[MAX_PATH
];
123 cl_must_pass(p_getcwd(cwd_backup
, MAX_PATH
));
124 cl_must_pass(p_chdir("C:/"));
126 test_utf8_to_utf16("Foo", L
"\\\\?\\C:\\Foo");
127 test_utf8_to_utf16("..\\..\\Foo", L
"\\\\?\\C:\\Foo");
128 test_utf8_to_utf16("Foo\\..", L
"\\\\?\\C:\\");
129 test_utf8_to_utf16("Foo\\..\\..", L
"\\\\?\\C:\\");
130 test_utf8_to_utf16("", L
"\\\\?\\C:\\");
132 cl_must_pass(p_chdir("C:/Windows"));
134 test_utf8_to_utf16("Foo", L
"\\\\?\\C:\\Windows\\Foo");
135 test_utf8_to_utf16("Foo\\Bar", L
"\\\\?\\C:\\Windows\\Foo\\Bar");
136 test_utf8_to_utf16("..\\Foo", L
"\\\\?\\C:\\Foo");
137 test_utf8_to_utf16("Foo\\..\\Bar", L
"\\\\?\\C:\\Windows\\Bar");
138 test_utf8_to_utf16("", L
"\\\\?\\C:\\Windows");
140 cl_must_pass(p_chdir(cwd_backup
));
144 void test_canonicalize(const wchar_t *in
, const wchar_t *expected
)
147 git_win32_path canonical
;
149 cl_assert(wcslen(in
) < MAX_PATH
);
150 wcscpy(canonical
, in
);
152 cl_must_pass(git_win32_path_canonicalize(canonical
));
153 cl_assert_equal_wcs(expected
, canonical
);
156 GIT_UNUSED(expected
);
160 void test_path_win32__canonicalize(void)
163 test_canonicalize(L
"C:\\Foo\\Bar", L
"C:\\Foo\\Bar");
164 test_canonicalize(L
"C:\\Foo\\", L
"C:\\Foo");
165 test_canonicalize(L
"C:\\Foo\\\\", L
"C:\\Foo");
166 test_canonicalize(L
"C:\\Foo\\..\\Bar", L
"C:\\Bar");
167 test_canonicalize(L
"C:\\Foo\\..\\..\\Bar", L
"C:\\Bar");
168 test_canonicalize(L
"C:\\Foo\\..\\..\\..\\..\\", L
"C:\\");
169 test_canonicalize(L
"C:/Foo/Bar", L
"C:\\Foo\\Bar");
170 test_canonicalize(L
"C:/", L
"C:\\");
172 test_canonicalize(L
"Foo\\\\Bar\\\\Asdf\\\\", L
"Foo\\Bar\\Asdf");
173 test_canonicalize(L
"Foo\\\\Bar\\\\..\\\\Asdf\\", L
"Foo\\Asdf");
174 test_canonicalize(L
"Foo\\\\Bar\\\\.\\\\Asdf\\", L
"Foo\\Bar\\Asdf");
175 test_canonicalize(L
"Foo\\\\..\\Bar\\\\.\\\\Asdf\\", L
"Bar\\Asdf");
176 test_canonicalize(L
"\\", L
"");
177 test_canonicalize(L
"", L
"");
178 test_canonicalize(L
"Foo\\..\\..\\..\\..", L
"");
179 test_canonicalize(L
"..\\..\\..\\..", L
"");
180 test_canonicalize(L
"\\..\\..\\..\\..", L
"");
182 test_canonicalize(L
"\\\\?\\C:\\Foo\\Bar", L
"\\\\?\\C:\\Foo\\Bar");
183 test_canonicalize(L
"\\\\?\\C:\\Foo\\Bar\\", L
"\\\\?\\C:\\Foo\\Bar");
184 test_canonicalize(L
"\\\\?\\C:\\\\Foo\\.\\Bar\\\\..\\", L
"\\\\?\\C:\\Foo");
185 test_canonicalize(L
"\\\\?\\C:\\\\", L
"\\\\?\\C:\\");
186 test_canonicalize(L
"//?/C:/", L
"\\\\?\\C:\\");
187 test_canonicalize(L
"//?/C:/../../Foo/", L
"\\\\?\\C:\\Foo");
188 test_canonicalize(L
"//?/C:/Foo/../../", L
"\\\\?\\C:\\");
190 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder", L
"\\\\?\\UNC\\server\\C$\\folder");
191 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\", L
"\\\\?\\UNC\\server\\C$\\folder");
192 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\", L
"\\\\?\\UNC\\server\\C$\\folder");
193 test_canonicalize(L
"\\\\?\\UNC\\server\\C$\\folder\\..\\..\\..\\..\\share\\", L
"\\\\?\\UNC\\server\\share");
195 test_canonicalize(L
"\\\\server\\share", L
"\\\\server\\share");
196 test_canonicalize(L
"\\\\server\\share\\", L
"\\\\server\\share");
197 test_canonicalize(L
"\\\\server\\share\\\\foo\\\\bar", L
"\\\\server\\share\\foo\\bar");
198 test_canonicalize(L
"\\\\server\\\\share\\\\foo\\\\bar", L
"\\\\server\\share\\foo\\bar");
199 test_canonicalize(L
"\\\\server\\share\\..\\foo", L
"\\\\server\\foo");
200 test_canonicalize(L
"\\\\server\\..\\..\\share\\.\\foo", L
"\\\\server\\share\\foo");
204 void test_path_win32__8dot3_name(void)
208 bool disable8dot3
= is_8dot3_disabled();
210 /* Some guaranteed short names */
211 shortname
= git_win32_path_8dot3_name("C:\\Program Files");
212 cl_assert(!shortname
|| !strcmp(shortname
, "PROGRA~1")); // null when 8.3 stripped, otherwise in 8.3 format
213 git__free(shortname
);
215 cl_assert_equal_s("WINDOWS", (shortname
= git_win32_path_8dot3_name("C:\\WINDOWS")));
216 git__free(shortname
);
218 /* Create some predictible short names */
219 cl_must_pass(p_mkdir(".foo", 0777));
220 cl_assert_equal_s(disable8dot3
? ".foo" : "FOO~1", (shortname
= git_win32_path_8dot3_name(".foo")));
221 git__free(shortname
);
223 cl_git_write2file("bar~1", "foobar\n", 7, O_RDWR
|O_CREAT
, 0666);
224 cl_must_pass(p_mkdir(".bar", 0777));
225 cl_assert_equal_s(disable8dot3
? ".bar" : "BAR~2", (shortname
= git_win32_path_8dot3_name(".bar")));
226 git__free(shortname
);