win32.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

#include "clar_libgit2.h"
#include "path.h"

#ifdef GIT_WIN32
#include "win32/path_w32.h"
#endif

void test_utf8_to_utf16(const char *utf8_in, const wchar_t *utf16_expected)
{
#ifdef GIT_WIN32
	git_win32_path path_utf16;
	int path_utf16len;

	cl_assert((path_utf16len = git_win32_path_from_utf8(path_utf16, utf8_in)) >= 0);
	cl_assert_equal_wcs(utf16_expected, path_utf16);
	cl_assert_equal_i(wcslen(utf16_expected), path_utf16len);
#else
	GIT_UNUSED(utf8_in);
	GIT_UNUSED(utf16_expected);
#endif
}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
void test_utf8_to_utf16_relative(const char* utf8_in, const wchar_t* utf16_expected)
{
#ifdef GIT_WIN32
	git_win32_path path_utf16;
	int path_utf16len;

	cl_assert((path_utf16len = git_win32_path_relative_from_utf8(path_utf16, utf8_in)) >= 0);
	cl_assert_equal_wcs(utf16_expected, path_utf16);
	cl_assert_equal_i(wcslen(utf16_expected), path_utf16len);
#else
	GIT_UNUSED(utf8_in);
	GIT_UNUSED(utf16_expected);
#endif
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
void test_path_win32__utf8_to_utf16(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("C:\\", L"\\\\?\\C:\\");
	test_utf8_to_utf16("c:\\", L"\\\\?\\c:\\");
	test_utf8_to_utf16("C:/", L"\\\\?\\C:\\");
	test_utf8_to_utf16("c:/", L"\\\\?\\c:\\");
#endif
}

void test_path_win32__removes_trailing_slash(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("C:\\Foo\\", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:\\Foo\\\\", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:\\Foo\\\\", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:/Foo/", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:/Foo///", L"\\\\?\\C:\\Foo");
#endif
}

void test_path_win32__squashes_multiple_slashes(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("C:\\\\Foo\\Bar\\\\Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
	test_utf8_to_utf16("C://Foo/Bar///Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
#endif
}

void test_path_win32__unc(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("\\\\server\\c$\\unc\\path", L"\\\\?\\UNC\\server\\c$\\unc\\path");
	test_utf8_to_utf16("//server/git/style/unc/path", L"\\\\?\\UNC\\server\\git\\style\\unc\\path");
#endif
}

void test_path_win32__honors_max_path(void)
{
#ifdef GIT_WIN32
	git_win32_path path_utf16;

	test_utf8_to_utf16("C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
		L"\\\\?\\C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
	test_utf8_to_utf16("\\\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
		L"\\\\?\\UNC\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");

	cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 260 chars and is sadly too long for windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
	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"));
#endif
}

void test_path_win32__dot_and_dotdot(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("C:\\Foo\\..\\Foobar", L"\\\\?\\C:\\Foobar");
	test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar", L"\\\\?\\C:\\Foo\\Foobar");
	test_utf8_to_utf16("C:\\Foo\\Bar\\..\\Foobar\\..", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:\\Foobar\\..", L"\\\\?\\C:\\");
	test_utf8_to_utf16("C:/Foo/Bar/../Foobar", L"\\\\?\\C:\\Foo\\Foobar");
	test_utf8_to_utf16("C:/Foo/Bar/../Foobar/../Asdf/", L"\\\\?\\C:\\Foo\\Asdf");
	test_utf8_to_utf16("C:/Foo/Bar/../Foobar/..", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("C:/Foo/..", L"\\\\?\\C:\\");

	test_utf8_to_utf16("C:\\Foo\\Bar\\.\\Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
	test_utf8_to_utf16("C:\\.\\Foo\\.\\Bar\\.\\Foobar\\.\\", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
	test_utf8_to_utf16("C:/Foo/Bar/./Foobar", L"\\\\?\\C:\\Foo\\Bar\\Foobar");
	test_utf8_to_utf16("C:/Foo/../Bar/./Foobar/../", L"\\\\?\\C:\\Bar");

	test_utf8_to_utf16("C:\\Foo\\..\\..\\Bar", L"\\\\?\\C:\\Bar");
#endif
}

void test_path_win32__absolute_from_no_drive_letter(void)
{
#ifdef GIT_WIN32
	test_utf8_to_utf16("\\Foo", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("\\Foo\\Bar", L"\\\\?\\C:\\Foo\\Bar");
	test_utf8_to_utf16("/Foo/Bar", L"\\\\?\\C:\\Foo\\Bar");
#endif
}

void test_path_win32__absolute_from_relative(void)
{
#ifdef GIT_WIN32
	char cwd_backup[MAX_PATH];

	cl_must_pass(p_getcwd(cwd_backup, MAX_PATH));
	cl_must_pass(p_chdir("C:/"));

	test_utf8_to_utf16("Foo", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("..\\..\\Foo", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("Foo\\..", L"\\\\?\\C:\\");
	test_utf8_to_utf16("Foo\\..\\..", L"\\\\?\\C:\\");
	test_utf8_to_utf16("", L"\\\\?\\C:\\");

	cl_must_pass(p_chdir("C:/Windows"));

	test_utf8_to_utf16("Foo", L"\\\\?\\C:\\Windows\\Foo");
	test_utf8_to_utf16("Foo\\Bar", L"\\\\?\\C:\\Windows\\Foo\\Bar");
	test_utf8_to_utf16("..\\Foo", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16("Foo\\..\\Bar", L"\\\\?\\C:\\Windows\\Bar");
	test_utf8_to_utf16("", L"\\\\?\\C:\\Windows");

	cl_must_pass(p_chdir(cwd_backup));
#endif
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
void test_path_win32__keeps_relative(void)
{
#ifdef GIT_WIN32
	/* Relative paths stay relative */
	test_utf8_to_utf16_relative("Foo", L"Foo");
	test_utf8_to_utf16_relative("..\\..\\Foo", L"..\\..\\Foo");
	test_utf8_to_utf16_relative("Foo\\..", L"Foo\\..");
	test_utf8_to_utf16_relative("Foo\\..\\..", L"Foo\\..\\..");
	test_utf8_to_utf16_relative("Foo\\Bar", L"Foo\\Bar");
	test_utf8_to_utf16_relative("Foo\\..\\Bar", L"Foo\\..\\Bar");
	test_utf8_to_utf16_relative("../../Foo", L"..\\..\\Foo");
	test_utf8_to_utf16_relative("Foo/..", L"Foo\\..");
	test_utf8_to_utf16_relative("Foo/../..", L"Foo\\..\\..");
	test_utf8_to_utf16_relative("Foo/Bar", L"Foo\\Bar");
	test_utf8_to_utf16_relative("Foo/../Bar", L"Foo\\..\\Bar");
	test_utf8_to_utf16_relative("Foo/../Bar/", L"Foo\\..\\Bar\\");
	test_utf8_to_utf16_relative("", L"");

	/* Absolute paths are canonicalized */
	test_utf8_to_utf16_relative("\\Foo", L"\\\\?\\C:\\Foo");
	test_utf8_to_utf16_relative("/Foo/Bar/", L"\\\\?\\C:\\Foo\\Bar");
	test_utf8_to_utf16_relative("\\\\server\\c$\\unc\\path", L"\\\\?\\UNC\\server\\c$\\unc\\path");
#endif
}

172
#ifdef GIT_WIN32
173
static void test_canonicalize(const wchar_t *in, const wchar_t *expected)
174 175 176 177 178 179 180 181 182
{
	git_win32_path canonical;

	cl_assert(wcslen(in) < MAX_PATH);
	wcscpy(canonical, in);

	cl_must_pass(git_win32_path_canonicalize(canonical));
	cl_assert_equal_wcs(expected, canonical);
}
183
#endif
184

185
static void test_remove_namespace(const wchar_t *in, const wchar_t *expected)
186 187 188 189 190 191 192
{
#ifdef GIT_WIN32
	git_win32_path canonical;

	cl_assert(wcslen(in) < MAX_PATH);
	wcscpy(canonical, in);

193
	git_win32_path_remove_namespace(canonical, wcslen(in));
194 195 196 197 198 199 200
	cl_assert_equal_wcs(expected, canonical);
#else
	GIT_UNUSED(in);
	GIT_UNUSED(expected);
#endif
}

201
void test_path_win32__remove_namespace(void)
202
{
203 204 205 206 207 208 209 210
	test_remove_namespace(L"\\\\?\\C:\\Temp\\Foo", L"C:\\Temp\\Foo");
	test_remove_namespace(L"\\\\?\\C:\\", L"C:\\");
	test_remove_namespace(L"\\\\?\\", L"");

	test_remove_namespace(L"\\??\\C:\\Temp\\Foo", L"C:\\Temp\\Foo");
	test_remove_namespace(L"\\??\\C:\\", L"C:\\");
	test_remove_namespace(L"\\??\\", L"");

211
	test_remove_namespace(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	test_remove_namespace(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
	test_remove_namespace(L"\\\\?\\UNC\\server\\C$", L"\\\\server\\C$");
	test_remove_namespace(L"\\\\?\\UNC\\server\\", L"\\\\server");
	test_remove_namespace(L"\\\\?\\UNC\\server", L"\\\\server");

	test_remove_namespace(L"\\??\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
	test_remove_namespace(L"\\??\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
	test_remove_namespace(L"\\??\\UNC\\server\\C$", L"\\\\server\\C$");
	test_remove_namespace(L"\\??\\UNC\\server\\", L"\\\\server");
	test_remove_namespace(L"\\??\\UNC\\server", L"\\\\server");

	test_remove_namespace(L"\\\\server\\C$\\folder", L"\\\\server\\C$\\folder");
	test_remove_namespace(L"\\\\server\\C$", L"\\\\server\\C$");
	test_remove_namespace(L"\\\\server\\", L"\\\\server");
	test_remove_namespace(L"\\\\server", L"\\\\server");

	test_remove_namespace(L"C:\\Foo\\Bar", L"C:\\Foo\\Bar");
	test_remove_namespace(L"C:\\", L"C:\\");
	test_remove_namespace(L"", L"");

232 233
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
void test_path_win32__canonicalize(void)
{
#ifdef GIT_WIN32
	test_canonicalize(L"C:\\Foo\\Bar", L"C:\\Foo\\Bar");
	test_canonicalize(L"C:\\Foo\\", L"C:\\Foo");
	test_canonicalize(L"C:\\Foo\\\\", L"C:\\Foo");
	test_canonicalize(L"C:\\Foo\\..\\Bar", L"C:\\Bar");
	test_canonicalize(L"C:\\Foo\\..\\..\\Bar", L"C:\\Bar");
	test_canonicalize(L"C:\\Foo\\..\\..\\..\\..\\", L"C:\\");
	test_canonicalize(L"C:/Foo/Bar", L"C:\\Foo\\Bar");
	test_canonicalize(L"C:/", L"C:\\");

	test_canonicalize(L"\\\\?\\C:\\Foo\\Bar", L"\\\\?\\C:\\Foo\\Bar");
	test_canonicalize(L"\\\\?\\C:\\Foo\\Bar\\", L"\\\\?\\C:\\Foo\\Bar");
	test_canonicalize(L"\\\\?\\C:\\\\Foo\\.\\Bar\\\\..\\", L"\\\\?\\C:\\Foo");
	test_canonicalize(L"\\\\?\\C:\\\\", L"\\\\?\\C:\\");
	test_canonicalize(L"//?/C:/", L"\\\\?\\C:\\");
	test_canonicalize(L"//?/C:/../../Foo/", L"\\\\?\\C:\\Foo");
	test_canonicalize(L"//?/C:/Foo/../../", L"\\\\?\\C:\\");

	test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\?\\UNC\\server\\C$\\folder");
	test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\", L"\\\\?\\UNC\\server\\C$\\folder");
	test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\", L"\\\\?\\UNC\\server\\C$\\folder");
	test_canonicalize(L"\\\\?\\UNC\\server\\C$\\folder\\..\\..\\..\\..\\share\\", L"\\\\?\\UNC\\server\\share");

	test_canonicalize(L"\\\\server\\share", L"\\\\server\\share");
	test_canonicalize(L"\\\\server\\share\\", L"\\\\server\\share");
	test_canonicalize(L"\\\\server\\share\\\\foo\\\\bar", L"\\\\server\\share\\foo\\bar");
	test_canonicalize(L"\\\\server\\\\share\\\\foo\\\\bar", L"\\\\server\\share\\foo\\bar");
	test_canonicalize(L"\\\\server\\share\\..\\foo", L"\\\\server\\foo");
	test_canonicalize(L"\\\\server\\..\\..\\share\\.\\foo", L"\\\\server\\share\\foo");
#endif
}
267 268 269 270 271 272

void test_path_win32__8dot3_name(void)
{
#ifdef GIT_WIN32
	char *shortname;

273
	if (!cl_sandbox_supports_8dot3())
274 275
		clar__skip();

276 277
	/* Some guaranteed short names */
	cl_assert_equal_s("PROGRA~1", (shortname = git_win32_path_8dot3_name("C:\\Program Files")));
278 279 280 281 282 283 284
	git__free(shortname);

	cl_assert_equal_s("WINDOWS", (shortname = git_win32_path_8dot3_name("C:\\WINDOWS")));
	git__free(shortname);

	/* Create some predictible short names */
	cl_must_pass(p_mkdir(".foo", 0777));
285
	cl_assert_equal_s("FOO~1", (shortname = git_win32_path_8dot3_name(".foo")));
286 287 288 289
	git__free(shortname);

	cl_git_write2file("bar~1", "foobar\n", 7, O_RDWR|O_CREAT, 0666);
	cl_must_pass(p_mkdir(".bar", 0777));
290
	cl_assert_equal_s("BAR~2", (shortname = git_win32_path_8dot3_name(".bar")));
291 292 293
	git__free(shortname);
#endif
}