]> git.proxmox.com Git - libgit2.git/blob - tests/path/win32.c
Fix test failures when 8.3 is disabled
[libgit2.git] / tests / path / win32.c
1
2 #include "clar_libgit2.h"
3 #include "path.h"
4
5 #ifdef GIT_WIN32
6 #include "win32/path_w32.h"
7
8 static bool is_8dot3_disabled(void)
9 {
10 wchar_t shortPath[MAX_PATH];
11 wchar_t *dest = L"longer_than_8dot3";
12 FILE *fp = _wfopen(dest, L"w");
13 cl_assert(fp);
14 fclose(fp);
15 cl_assert(GetShortPathNameW(dest, shortPath, MAX_PATH) > 0);
16 DeleteFileW(dest);
17 return !wcscmp(dest, shortPath);
18 }
19 #endif
20
21 void test_utf8_to_utf16(const char *utf8_in, const wchar_t *utf16_expected)
22 {
23 #ifdef GIT_WIN32
24 git_win32_path path_utf16;
25 int path_utf16len;
26
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);
30 #else
31 GIT_UNUSED(utf8_in);
32 GIT_UNUSED(utf16_expected);
33 #endif
34 }
35
36 void test_path_win32__utf8_to_utf16(void)
37 {
38 #ifdef GIT_WIN32
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:\\");
43 #endif
44 }
45
46 void test_path_win32__removes_trailing_slash(void)
47 {
48 #ifdef GIT_WIN32
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");
54 #endif
55 }
56
57 void test_path_win32__squashes_multiple_slashes(void)
58 {
59 #ifdef GIT_WIN32
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");
62 #endif
63 }
64
65 void test_path_win32__unc(void)
66 {
67 #ifdef GIT_WIN32
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");
70 #endif
71 }
72
73 void test_path_win32__honors_max_path(void)
74 {
75 #ifdef GIT_WIN32
76 git_win32_path path_utf16;
77
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");
82
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"));
85 #endif
86 }
87
88 void test_path_win32__dot_and_dotdot(void)
89 {
90 #ifdef GIT_WIN32
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:\\");
99
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");
104
105 test_utf8_to_utf16("C:\\Foo\\..\\..\\Bar", L"\\\\?\\C:\\Bar");
106 #endif
107 }
108
109 void test_path_win32__absolute_from_no_drive_letter(void)
110 {
111 #ifdef GIT_WIN32
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");
115 #endif
116 }
117
118 void test_path_win32__absolute_from_relative(void)
119 {
120 #ifdef GIT_WIN32
121 char cwd_backup[MAX_PATH];
122
123 cl_must_pass(p_getcwd(cwd_backup, MAX_PATH));
124 cl_must_pass(p_chdir("C:/"));
125
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:\\");
131
132 cl_must_pass(p_chdir("C:/Windows"));
133
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");
139
140 cl_must_pass(p_chdir(cwd_backup));
141 #endif
142 }
143
144 void test_canonicalize(const wchar_t *in, const wchar_t *expected)
145 {
146 #ifdef GIT_WIN32
147 git_win32_path canonical;
148
149 cl_assert(wcslen(in) < MAX_PATH);
150 wcscpy(canonical, in);
151
152 cl_must_pass(git_win32_path_canonicalize(canonical));
153 cl_assert_equal_wcs(expected, canonical);
154 #else
155 GIT_UNUSED(in);
156 GIT_UNUSED(expected);
157 #endif
158 }
159
160 void test_path_win32__canonicalize(void)
161 {
162 #ifdef GIT_WIN32
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:\\");
171
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"");
181
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:\\");
189
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");
194
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");
201 #endif
202 }
203
204 void test_path_win32__8dot3_name(void)
205 {
206 #ifdef GIT_WIN32
207 char *shortname;
208 bool disable8dot3 = is_8dot3_disabled();
209
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);
214
215 cl_assert_equal_s("WINDOWS", (shortname = git_win32_path_8dot3_name("C:\\WINDOWS")));
216 git__free(shortname);
217
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);
222
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);
227 #endif
228 }