Signed-off-by: Kirill Erofeev erofeev.info@gmail.com --- dlls/msvcrt/tests/dir.c | 194 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+)
diff --git a/dlls/msvcrt/tests/dir.c b/dlls/msvcrt/tests/dir.c index aa273ea..948b506 100644 --- a/dlls/msvcrt/tests/dir.c +++ b/dlls/msvcrt/tests/dir.c @@ -23,6 +23,7 @@ #include <stdlib.h> #include <stdio.h> #include <fcntl.h> +#include <direct.h> #include <sys/stat.h> #include <io.h> #include <mbctype.h> @@ -35,12 +36,18 @@ static int (__cdecl *p_makepath_s)(char *, size_t, const char *, const char *, const char *, const char *); static int (__cdecl *p_wmakepath_s)(wchar_t *, size_t, const wchar_t *,const wchar_t *, const wchar_t *, const wchar_t *);
+static int (__cdecl *p_searchenv_s)(const char*, const char*, char*, size_t); +static int (__cdecl *p_wsearchenv_s)(const wchar_t*, const wchar_t*, wchar_t*, size_t); + static void init(void) { HMODULE hmod = GetModuleHandleA("msvcrt.dll");
p_makepath_s = (void *)GetProcAddress(hmod, "_makepath_s"); p_wmakepath_s = (void *)GetProcAddress(hmod, "_wmakepath_s"); + + p_searchenv_s = (void *)GetProcAddress(hmod, "_searchenv_s"); + p_wsearchenv_s = (void *)GetProcAddress(hmod, "_wsearchenv_s"); }
typedef struct @@ -416,6 +423,191 @@ static void test_splitpath(void) _setmbcp(prev_cp); }
+static void test_search_environment(void) +{ + const char* dirs[] = { + "c:\search_env_test", + "c:\search_env_test\dir1", + "c:\search_env_test\dir2", + "c:\search_env_test\dir3longer", + NULL }; + + const char* files[] = { + "c:\search_env_test\dir1\1.dat", + "c:\search_env_test\dir1\2.dat", + "c:\search_env_test\dir2\1.dat", + "c:\search_env_test\dir2\3.dat", + "c:\search_env_test\dir3longer\3.dat", + NULL }; + + FILE* tmp_file = NULL; + const char** file_name = files; + const char** dir_name = dirs; + char result[MAX_PATH]; + WCHAR result_w[MAX_PATH]; + WCHAR filename_w[MAX_PATH]; + const WCHAR env_w[] = {'T', 'E', 'S', 'T', '_', 'P', 'A', 'T', 'H', '\0'}; + + const char eraser[] = "TEST_PATH=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + while (*dir_name){ + ok(!mkdir(*dir_name), "Failed to setup test environment (dir = %s)\n", *dir_name); + dir_name++; + } + + while (*file_name){ + tmp_file = fopen(*file_name, "wb"); + ok(tmp_file != NULL, "Failed to setup test environment (file = %s)\n", *file_name); + if (tmp_file) + fclose(tmp_file); + file_name++; + } + + + /*To catch some errors (usage of uninitialized memory) we will trick searchenv to set up memory to predefined values*/ + putenv(eraser); + _searchenv("1.dat", "TEST_PATH", result); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _searchenv("1.dat", "TEST_PATH", result); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + _searchenv("1.dat", "TEST_PATH", result); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _searchenv("3.dat", "TEST_PATH", result); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + putenv(eraser); + _searchenv("1.dat", "TEST_PATH", result); + putenv("TEST_PATH=;c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _searchenv("1.dat", "TEST_PATH", result); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + _searchenv("1.dat", "TEST_PATH", result); + putenv("TEST_PATH=c:\search_env_test\dir1;;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _searchenv("3.dat", "TEST_PATH", result); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + + + + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "1.dat", -1, filename_w, MAX_PATH); + _wsearchenv(filename_w, env_w, result_w); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _wsearchenv(filename_w, env_w, result_w); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "3.dat", -1, filename_w, MAX_PATH); + _wsearchenv(filename_w, env_w, result_w); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _wsearchenv(filename_w, env_w, result_w); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "1.dat", -1, filename_w, MAX_PATH); + _wsearchenv(filename_w, env_w, result_w); + putenv("TEST_PATH=;c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _wsearchenv(filename_w, env_w, result_w); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "3.dat", -1, filename_w, MAX_PATH); + _wsearchenv(filename_w, env_w, result_w); + putenv("TEST_PATH=c:\search_env_test\dir1;;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + _wsearchenv(filename_w, env_w, result_w); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + + + + if(p_searchenv_s){ + putenv(eraser); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_searchenv_s("3.dat", "TEST_PATH", result, MAX_PATH); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + putenv(eraser); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + putenv("TEST_PATH=;c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + p_searchenv_s("1.dat", "TEST_PATH", result, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_searchenv_s("3.dat", "TEST_PATH", result, MAX_PATH); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + }else{ + win_skip("Safe searchenv function is not available\n"); + } + + + + if(p_wsearchenv_s){ + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "1.dat", -1, filename_w, MAX_PATH); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "3.dat", -1, filename_w, MAX_PATH); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "1.dat", -1, filename_w, MAX_PATH); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + putenv("TEST_PATH=;c:\search_env_test\dir1;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir1\1.dat"), "Failed to found %s in %s got in %s\n", "1.dat", "c:\search_env_test\dir1", result); + + putenv(eraser); + MultiByteToWideChar( CP_ACP, 0, "3.dat", -1, filename_w, MAX_PATH); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + putenv("TEST_PATH=c:\search_env_test\dir1;;c:\search_env_test\dir2;c:\search_env_test\dir3longer"); + p_wsearchenv_s(filename_w, env_w, result_w, MAX_PATH); + WideCharToMultiByte(CP_ACP, 0, result_w, -1, result, MAX_PATH, NULL, NULL); + ok(!strcmp(result, "c:\search_env_test\dir2\3.dat"), "Failed to found %s in %s got in %s\n", "3.dat", "c:\search_env_test\dir2", result); + }else{ + win_skip("Safe wsearchenv function is not available\n"); + } + + + + putenv("TEST_PATH="); + + do { + file_name--; + ok(!remove(*file_name), "Failed cleanup test (file = %s)\n", *file_name); + }while(files != file_name); + + do { + dir_name--; + ok(!rmdir(*dir_name), "Failed cleanup test (dir = %s)\n", *dir_name); + }while(dirs != dir_name); +} + START_TEST(dir) { init(); @@ -424,4 +616,6 @@ START_TEST(dir) test_makepath(); test_makepath_s(); test_splitpath(); + + test_search_environment(); }