]> git.proxmox.com Git - libgit2.git/blob - tests/path/win32.c
Merge pull request #2845 from rakuco/libssh2-detection-fix
[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 #endif
8
9 void test_utf8_to_utf16(const char *utf8_in, const wchar_t *utf16_expected)
10 {
11 #ifdef GIT_WIN32
12 git_win32_path path_utf16;
13 int path_utf16len;
14
15 cl_assert((path_utf16len = git_win32_path_from_utf8(path_utf16, utf8_in)) >= 0);
16 cl_assert_equal_wcs(utf16_expected, path_utf16);
17 cl_assert_equal_i(wcslen(utf16_expected), path_utf16len);
18 #else
19 GIT_UNUSED(utf8_in);
20 GIT_UNUSED(utf16_expected);
21 #endif
22 }
23
24 void test_path_win32__utf8_to_utf16(void)
25 {
26 #ifdef GIT_WIN32
27 test_utf8_to_utf16("C:\\", L"\\\\?\\C:\\");
28 test_utf8_to_utf16("c:\\", L"\\\\?\\c:\\");
29 test_utf8_to_utf16("C:/", L"\\\\?\\C:\\");
30 test_utf8_to_utf16("c:/", L"\\\\?\\c:\\");
31 #endif
32 }
33
34 void test_path_win32__removes_trailing_slash(void)
35 {
36 #ifdef GIT_WIN32
37 test_utf8_to_utf16("C:\\Foo\\", L"\\\\?\\C:\\Foo");
38 test_utf8_to_utf16("C:\\Foo\\\\", L"\\\\?\\C:\\Foo");
39 test_utf8_to_utf16("C:\\Foo\\\\", L"\\\\?\\C:\\Foo");
40 test_utf8_to_utf16("C:/Foo/", L"\\\\?\\C:\\Foo");
41 test_utf8_to_utf16("C:/Foo///", L"\\\\?\\C:\\Foo");
42 #endif
43 }
44
45 void test_path_win32__squashes_multiple_slashes(void)
46 {
47 #ifdef GIT_WIN32
48 test_utf8_to_utf16("C:\\\\Foo\\Bar\\\\Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
49 test_utf8_to_utf16("C://Foo/Bar///Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
50 #endif
51 }
52
53 void test_path_win32__unc(void)
54 {
55 #ifdef GIT_WIN32
56 test_utf8_to_utf16("\\\\server\\c$\\unc\\path", L"\\\\?\\UNC\\server\\c$\\unc\\path");
57 test_utf8_to_utf16("//server/git/style/unc/path", L"\\\\?\\UNC\\server\\git\\style\\unc\\path");
58 #endif
59 }
60
61 void test_path_win32__honors_max_path(void)
62 {
63 #ifdef GIT_WIN32
64 git_win32_path path_utf16;
65
66 test_utf8_to_utf16("C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
67 L"\\\\?\\C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
68 test_utf8_to_utf16("\\\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
69 L"\\\\?\\UNC\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
70
71 cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 260 chars and is sadly too long for windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
72 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"));
73 #endif
74 }
75
76 void test_path_win32__dot_and_dotdot(void)
77 {
78 #ifdef GIT_WIN32
79 test_utf8_to_utf16("C:\\Foo\\..\\Foobar", L"\\\\?\\C:\\Foobar");
80 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar", L"\\\\?\\C:\\Foo\\Foobar");
81 test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar\\..", L"\\\\?\\C:\\Foo");
82 test_utf8_to_utf16("C:\\Foobar\\..", L"\\\\?\\C:\\");
83 test_utf8_to_utf16("C:/Foo/Bar/../Foobar", L"\\\\?\\C:\\Foo\\Foobar");
84 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/../Asdf/", L"\\\\?\\C:\\Foo\\Asdf");
85 test_utf8_to_utf16("C:/Foo/Bar/../Foobar/..", L"\\\\?\\C:\\Foo");
86 test_utf8_to_utf16("C:/Foo/..", L"\\\\?\\C:\\");
87
88 test_utf8_to_utf16("C:\\Foo\\Bar\\.\\Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
89 test_utf8_to_utf16("C:\\.\\Foo\\.\\Bar\\.\\Foobar\\.\\", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
90 test_utf8_to_utf16("C:/Foo/Bar/./Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
91 test_utf8_to_utf16("C:/Foo/../Bar/./Foobar/../", L"\\\\?\\C:\\Bar");
92
93 test_utf8_to_utf16("C:\\Foo\\..\\..\\Bar", L"\\\\?\\C:\\Bar");
94 #endif
95 }
96
97 void test_path_win32__absolute_from_no_drive_letter(void)
98 {
99 #ifdef GIT_WIN32
100 test_utf8_to_utf16("\\Foo", L"\\\\?\\C:\\Foo");
101 test_utf8_to_utf16("\\Foo\\Bar", L"\\\\?\\C:\\Foo\\Bar");
102 test_utf8_to_utf16("/Foo/Bar", L"\\\\?\\C:\\Foo\\Bar");
103 #endif
104 }
105
106 void test_path_win32__absolute_from_relative(void)
107 {
108 #ifdef GIT_WIN32
109 char cwd_backup[MAX_PATH];
110
111 cl_must_pass(p_getcwd(cwd_backup, MAX_PATH));
112 cl_must_pass(p_chdir("C:/"));
113
114 test_utf8_to_utf16("Foo", L"\\\\?\\C:\\Foo");
115 test_utf8_to_utf16("..\\..\\Foo", L"\\\\?\\C:\\Foo");
116 test_utf8_to_utf16("Foo\\..", L"\\\\?\\C:\\");
117 test_utf8_to_utf16("Foo\\..\\..", L"\\\\?\\C:\\");
118 test_utf8_to_utf16("", L"\\\\?\\C:\\");
119
120 cl_must_pass(p_chdir("C:/Windows"));
121
122 test_utf8_to_utf16("Foo", L"\\\\?\\C:\\Windows\\Foo");
123 test_utf8_to_utf16("Foo\\Bar", L"\\\\?\\C:\\Windows\\Foo\\Bar");
124 test_utf8_to_utf16("..\\Foo", L"\\\\?\\C:\\Foo");
125 test_utf8_to_utf16("Foo\\..\\Bar", L"\\\\?\\C:\\Windows\\Bar");
126 test_utf8_to_utf16("", L"\\\\?\\C:\\Windows");
127
128 cl_must_pass(p_chdir(cwd_backup));
129 #endif
130 }
131
132 void test_canonicalize(const wchar_t *in, const wchar_t *expected)
133 {
134 #ifdef GIT_WIN32
135 git_win32_path canonical;
136
137 cl_assert(wcslen(in) < MAX_PATH);
138 wcscpy(canonical, in);
139
140 cl_must_pass(git_win32_path_canonicalize(canonical));
141 cl_assert_equal_wcs(expected, canonical);
142 #else
143 GIT_UNUSED(in);
144 GIT_UNUSED(expected);
145 #endif
146 }
147
148 void test_path_win32__canonicalize(void)
149 {
150 #ifdef GIT_WIN32
151 test_canonicalize(L"C:\\Foo\\Bar", L"C:\\Foo\\Bar");
152 test_canonicalize(L"C:\\Foo\\", L"C:\\Foo");
153 test_canonicalize(L"C:\\Foo\\\\", L"C:\\Foo");
154 test_canonicalize(L"C:\\Foo\\..\\Bar", L"C:\\Bar");
155 test_canonicalize(L"C:\\Foo\\..\\..\\Bar", L"C:\\Bar");
156 test_canonicalize(L"C:\\Foo\\..\\..\\..\\..\\", L"C:\\");
157 test_canonicalize(L"C:/Foo/Bar", L"C:\\Foo\\Bar");
158 test_canonicalize(L"C:/", L"C:\\");
159
160 test_canonicalize(L"Foo\\\\Bar\\\\Asdf\\\\", L"Foo\\Bar\\Asdf");
161 test_canonicalize(L"Foo\\\\Bar\\\\..\\\\Asdf\\", L"Foo\\Asdf");
162 test_canonicalize(L"Foo\\\\Bar\\\\.\\\\Asdf\\", L"Foo\\Bar\\Asdf");
163 test_canonicalize(L"Foo\\\\..\\Bar\\\\.\\\\Asdf\\", L"Bar\\Asdf");
164 test_canonicalize(L"\\", L"");
165 test_canonicalize(L"", L"");
166 test_canonicalize(L"Foo\\..\\..\\..\\..", L"");
167 test_canonicalize(L"..\\..\\..\\..", L"");
168 test_canonicalize(L"\\..\\..\\..\\..", L"");
169
170 test_canonicalize(L"\\\\?\\C:\\Foo\\Bar", L"\\\\?\\C:\\Foo\\Bar");
171 test_canonicalize(L"\\\\?\\C:\\Foo\\Bar\\", L"\\\\?\\C:\\Foo\\Bar");
172 test_canonicalize(L"\\\\?\\C:\\\\Foo\\.\\Bar\\\\..\\", L"\\\\?\\C:\\Foo");
173 test_canonicalize(L"\\\\?\\C:\\\\", L"\\\\?\\C:\\");
174 test_canonicalize(L"//?/C:/", L"\\\\?\\C:\\");
175 test_canonicalize(L"//?/C:/../../Foo/", L"\\\\?\\C:\\Foo");
176 test_canonicalize(L"//?/C:/Foo/../../", L"\\\\?\\C:\\");
177
178 test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\?\\UNC\\server\\C$\\folder");
179 test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\", L"\\\\?\\UNC\\server\\C$\\folder");
180 test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\", L"\\\\?\\UNC\\server\\C$\\folder");
181 test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\..\\..\\..\\..\\share\\", L"\\\\?\\UNC\\server\\share");
182
183 test_canonicalize(L"\\\\server\\share", L"\\\\server\\share");
184 test_canonicalize(L"\\\\server\\share\\", L"\\\\server\\share");
185 test_canonicalize(L"\\\\server\\share\\\\foo\\\\bar", L"\\\\server\\share\\foo\\bar");
186 test_canonicalize(L"\\\\server\\\\share\\\\foo\\\\bar", L"\\\\server\\share\\foo\\bar");
187 test_canonicalize(L"\\\\server\\share\\..\\foo", L"\\\\server\\foo");
188 test_canonicalize(L"\\\\server\\..\\..\\share\\.\\foo", L"\\\\server\\share\\foo");
189 #endif
190 }
191
192 void test_path_win32__8dot3_name(void)
193 {
194 #ifdef GIT_WIN32
195 char *shortname;
196
197 if (!cl_sandbox_supports_8dot3())
198 clar__skip();
199
200 /* Some guaranteed short names */
201 cl_assert_equal_s("PROGRA~1", (shortname = git_win32_path_8dot3_name("C:\\Program Files")));
202 git__free(shortname);
203
204 cl_assert_equal_s("WINDOWS", (shortname = git_win32_path_8dot3_name("C:\\WINDOWS")));
205 git__free(shortname);
206
207 /* Create some predictible short names */
208 cl_must_pass(p_mkdir(".foo", 0777));
209 cl_assert_equal_s("FOO~1", (shortname = git_win32_path_8dot3_name(".foo")));
210 git__free(shortname);
211
212 cl_git_write2file("bar~1", "foobar\n", 7, O_RDWR|O_CREAT, 0666);
213 cl_must_pass(p_mkdir(".bar", 0777));
214 cl_assert_equal_s("BAR~2", (shortname = git_win32_path_8dot3_name(".bar")));
215 git__free(shortname);
216 #endif
217 }