Hash :
475c6eba
Author :
Date :
2022-01-17T17:16:02
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
#include "clar_libgit2.h"
#include "futils.h"
#include "sysdir.h"
#include "win32/findfile.h"
#ifdef GIT_WIN32
static char *path_save;
static git_str gfw_path_root = GIT_STR_INIT;
static git_str gfw_registry_root = GIT_STR_INIT;
#endif
void test_win32_systemdir__initialize(void)
{
#ifdef GIT_WIN32
git_str path_env = GIT_STR_INIT;
path_save = cl_getenv("PATH");
git_win32__set_registry_system_dir(L"");
cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";C:\\fakefakedoesnotexist"));
cl_setenv("PATH", path_env.ptr);
cl_git_pass(git_str_puts(&gfw_path_root, clar_sandbox_path()));
cl_git_pass(git_str_puts(&gfw_path_root, "/fake_gfw_path_install"));
cl_git_pass(git_str_puts(&gfw_registry_root, clar_sandbox_path()));
cl_git_pass(git_str_puts(&gfw_registry_root, "/fake_gfw_registry_install"));
git_str_dispose(&path_env);
#endif
}
void test_win32_systemdir__cleanup(void)
{
#ifdef GIT_WIN32
cl_fixture_cleanup("fake_gfw_path_install");
cl_fixture_cleanup("fake_gfw_registry_install");
git_str_dispose(&gfw_path_root);
git_str_dispose(&gfw_registry_root);
cl_setenv("PATH", path_save);
git__free(path_save);
path_save = NULL;
git_win32__set_registry_system_dir(NULL);
cl_sandbox_set_search_path_defaults();
#endif
}
#ifdef GIT_WIN32
static void fix_path(git_str *s)
{
char *c;
for (c = s->ptr; *c; c++) {
if (*c == '/')
*c = '\\';
}
}
static void populate_fake_gfw(
git_str *expected_etc_dir,
const char *root,
const char *token,
bool create_gitconfig,
bool create_mingw64_gitconfig,
bool add_to_path,
bool add_to_registry)
{
git_str bin_path = GIT_STR_INIT, exe_path = GIT_STR_INIT,
etc_path = GIT_STR_INIT, mingw64_path = GIT_STR_INIT,
config_path = GIT_STR_INIT, path_env = GIT_STR_INIT,
config_data = GIT_STR_INIT;
cl_git_pass(git_str_puts(&bin_path, root));
cl_git_pass(git_str_puts(&bin_path, "/cmd"));
cl_git_pass(git_futils_mkdir_r(bin_path.ptr, 0755));
cl_git_pass(git_str_puts(&exe_path, bin_path.ptr));
cl_git_pass(git_str_puts(&exe_path, "/git.cmd"));
cl_git_mkfile(exe_path.ptr, "This is a fake executable.");
cl_git_pass(git_str_puts(&etc_path, root));
cl_git_pass(git_str_puts(&etc_path, "/etc"));
cl_git_pass(git_futils_mkdir_r(etc_path.ptr, 0755));
cl_git_pass(git_str_puts(&mingw64_path, root));
cl_git_pass(git_str_puts(&mingw64_path, "/mingw64/etc"));
cl_git_pass(git_futils_mkdir_r(mingw64_path.ptr, 0755));
if (create_gitconfig) {
git_str_clear(&config_data);
git_str_printf(&config_data, "[gfw]\n\ttest = etc %s\n", token);
cl_git_pass(git_str_puts(&config_path, etc_path.ptr));
cl_git_pass(git_str_puts(&config_path, "/gitconfig"));
cl_git_mkfile(config_path.ptr, config_data.ptr);
}
if (create_mingw64_gitconfig) {
git_str_clear(&config_data);
git_str_printf(&config_data, "[gfw]\n\ttest = mingw64 %s\n", token);
git_str_clear(&config_path);
cl_git_pass(git_str_puts(&config_path, mingw64_path.ptr));
cl_git_pass(git_str_puts(&config_path, "/gitconfig"));
cl_git_mkfile(config_path.ptr, config_data.ptr);
}
if (add_to_path) {
fix_path(&bin_path);
cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";"));
cl_git_pass(git_str_puts(&path_env, bin_path.ptr));
cl_git_pass(git_str_puts(&path_env, ";C:\\fakefakedoesnotexist"));
cl_setenv("PATH", path_env.ptr);
}
if (add_to_registry) {
git_win32_path registry_path;
size_t offset = 0;
cl_assert(git_win32_path_from_utf8(registry_path, root) >= 0);
if (wcsncmp(registry_path, L"\\\\?\\", CONST_STRLEN("\\\\?\\")) == 0)
offset = CONST_STRLEN("\\\\?\\");
git_win32__set_registry_system_dir(registry_path + offset);
}
cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, etc_path.ptr));
cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, mingw64_path.ptr));
git_str_dispose(&bin_path);
git_str_dispose(&exe_path);
git_str_dispose(&etc_path);
git_str_dispose(&mingw64_path);
git_str_dispose(&config_path);
git_str_dispose(&path_env);
git_str_dispose(&config_data);
}
static void populate_fake_ecosystem(
git_str *expected_etc_dir,
bool create_gitconfig,
bool create_mingw64_gitconfig,
bool path,
bool registry)
{
if (path)
populate_fake_gfw(expected_etc_dir, gfw_path_root.ptr, "path", create_gitconfig, create_mingw64_gitconfig, true, false);
if (registry)
populate_fake_gfw(expected_etc_dir, gfw_registry_root.ptr, "registry", create_gitconfig, create_mingw64_gitconfig, false, true);
}
#endif
void test_win32_systemdir__finds_etc_in_path(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config *cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, true, false, true, false);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("etc path", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__finds_mingw64_etc_in_path(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, false, true, true, false);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("mingw64 path", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__prefers_etc_to_mingw64_in_path(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, true, true, true, false);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("etc path", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__finds_etc_in_registry(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, true, false, false, true);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("etc registry", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__finds_mingw64_etc_in_registry(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, false, true, false, true);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("mingw64 registry", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__prefers_etc_to_mingw64_in_registry(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, true, true, false, true);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("etc registry", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}
void test_win32_systemdir__prefers_path_to_registry(void)
{
#ifdef GIT_WIN32
git_str expected = GIT_STR_INIT, out = GIT_STR_INIT;
git_config* cfg;
git_buf value = GIT_BUF_INIT;
populate_fake_ecosystem(&expected, true, true, true, true);
cl_git_pass(git_win32__find_system_dirs(&out, "etc"));
cl_assert_equal_s(out.ptr, expected.ptr);
git_sysdir_reset();
cl_git_pass(git_config_open_default(&cfg));
cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test"));
cl_assert_equal_s("etc path", value.ptr);
git_buf_dispose(&value);
git_str_dispose(&expected);
git_str_dispose(&out);
git_config_free(cfg);
#endif
}