Commit b6d4be61 authored by Eric Pouech's avatar Eric Pouech Committed by Alexandre Julliard

msvcrt/tests: Improve environment tests (initial conditions).

parent d2992dba
...@@ -46,6 +46,8 @@ static const char *a_very_long_env_string = ...@@ -46,6 +46,8 @@ static const char *a_very_long_env_string =
static char ***(__cdecl *p__p__environ)(void); static char ***(__cdecl *p__p__environ)(void);
static WCHAR ***(__cdecl *p__p__wenviron)(void); static WCHAR ***(__cdecl *p__p__wenviron)(void);
static char ***(__cdecl *p__p___initenv)(void);
static wchar_t ***(__cdecl *p__p___winitenv)(void);
static void (__cdecl *p_get_environ)(char ***); static void (__cdecl *p_get_environ)(char ***);
static void (__cdecl *p_get_wenviron)(WCHAR ***); static void (__cdecl *p_get_wenviron)(WCHAR ***);
static errno_t (__cdecl *p_putenv_s)(const char*, const char*); static errno_t (__cdecl *p_putenv_s)(const char*, const char*);
...@@ -61,6 +63,8 @@ static void init(void) ...@@ -61,6 +63,8 @@ static void init(void)
p__p__environ = (void *)GetProcAddress(hmod, "__p__environ"); p__p__environ = (void *)GetProcAddress(hmod, "__p__environ");
p__p__wenviron = (void *)GetProcAddress(hmod, "__p__wenviron"); p__p__wenviron = (void *)GetProcAddress(hmod, "__p__wenviron");
p__p___initenv = (void *)GetProcAddress(hmod, "__p___initenv");
p__p___winitenv = (void *)GetProcAddress(hmod, "__p___winitenv");
p_environ = (void *)GetProcAddress(hmod, "_environ"); p_environ = (void *)GetProcAddress(hmod, "_environ");
p_wenviron = (void *)GetProcAddress(hmod, "_wenviron"); p_wenviron = (void *)GetProcAddress(hmod, "_wenviron");
p_get_environ = (void *)GetProcAddress(hmod, "_get_environ"); p_get_environ = (void *)GetProcAddress(hmod, "_get_environ");
...@@ -79,21 +83,32 @@ static void test_system(void) ...@@ -79,21 +83,32 @@ static void test_system(void)
ok(ret == 0, "Expected system to return 0, got %d\n", ret); ok(ret == 0, "Expected system to return 0, got %d\n", ret);
} }
static wchar_t *env_get_valueW( wchar_t **envp, const wchar_t *var )
{
unsigned i;
size_t len = wcslen( var );
if (!envp) return NULL;
for (i = 0; envp[i] != NULL; i++)
{
wchar_t *ptr;
if (!(ptr = wcschr( envp[i], L'=' ))) continue;
if (ptr - envp[i] == len && !memcmp( envp[i], var, len * sizeof(wchar_t) ))
return ptr + 1;
}
return NULL;
}
static void test__environ(void) static void test__environ(void)
{ {
int argc; int argc;
char **argv, **envp = NULL; char **argv, **envp = NULL, **initenv = NULL;
int i, mode = 0; int mode = 0;
ok( p_environ != NULL, "Expected the pointer to _environ to be non-NULL\n" ); ok( p_environ != NULL, "Expected the pointer to _environ to be non-NULL\n" );
if (p_environ) ok( *p_environ != NULL, "Expected _environ to be initialized on startup\n" );
ok( *p_environ != NULL, "Expected _environ to be initialized on startup\n" );
if (!p_environ || !*p_environ)
{
skip( "_environ pointers are not valid\n" );
return;
}
if (sizeof(void*) != sizeof(int)) if (sizeof(void*) != sizeof(int))
ok( !p__p__environ, "__p__environ() should be 32-bit only\n"); ok( !p__p__environ, "__p__environ() should be 32-bit only\n");
...@@ -110,32 +125,38 @@ static void test__environ(void) ...@@ -110,32 +125,38 @@ static void test__environ(void)
else else
win_skip( "_get_environ() is not available\n" ); win_skip( "_get_environ() is not available\n" );
if (p__p___initenv)
{
initenv = *p__p___initenv();
todo_wine
ok( initenv == *p_environ,
"Expected _environ to be equal to initial env\n" );
}
else
skip( "__p___initenv() is not available\n" );
/* Note that msvcrt from Windows versions older than Vista /* Note that msvcrt from Windows versions older than Vista
* expects the mode pointer parameter to be valid.*/ * expects the mode pointer parameter to be valid.*/
__getmainargs(&argc, &argv, &envp, 0, &mode); __getmainargs(&argc, &argv, &envp, 0, &mode);
ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" ); ok( envp != NULL,
if (!envp) "Expected initial environment block pointer to be non-NULL\n" );
{ todo_wine
skip( "Initial environment block pointer is not valid\n" ); ok( envp == *p_environ,
return; "Expected initial environment to be equal to _environ\n" );
}
for (i = 0; ; i++) ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
if (p__p___initenv)
{ {
if ((*p_environ)[i]) char **retptr = *p__p___initenv();
{
ok( envp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" ); ok( retptr != *p_environ,
ok( !strcmp((*p_environ)[i], envp[i]), "Expected _environ[] not to be equal to initial env\n" );
"Expected _environ and environment block pointer strings (%s vs. %s) to match\n", ok( retptr == initenv,
(*p_environ)[i], envp[i] ); "Unexpected modification of initial env\n" );
}
else
{
ok( !envp[i], "Expected environment block pointer element to be NULL, got %p\n", envp[i] );
break;
}
} }
ok( _putenv("cat=") == 0, "failed setting cat=\n" );
} }
static void test__wenviron(void) static void test__wenviron(void)
...@@ -143,36 +164,38 @@ static void test__wenviron(void) ...@@ -143,36 +164,38 @@ static void test__wenviron(void)
int argc; int argc;
char **argv, **envp = NULL; char **argv, **envp = NULL;
WCHAR **wargv, **wenvp = NULL; WCHAR **wargv, **wenvp = NULL;
int i, mode = 0; int mode = 0;
ok( p_wenviron != NULL, "Expected the pointer to _wenviron to be non-NULL\n" ); ok( p_wenviron != NULL, "Expected the pointer to _wenviron to be non-NULL\n" );
if (p_wenviron) ok( !*p_wenviron, "Expected _wenviron[] to be NULL, got %p\n", *p_wenviron );
ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron );
else
{
win_skip( "Pointer to _wenviron is not valid\n" );
return;
}
if (sizeof(void*) != sizeof(int)) if (sizeof(void*) != sizeof(int))
ok( !p__p__wenviron, "__p__wenviron() should be 32-bit only\n"); ok( !p__p__wenviron, "__p__wenviron() should be 32-bit only\n");
else else
ok( *p__p__wenviron() == NULL, "Expected _wenviron pointers to be NULL\n" ); ok( !*p__p__wenviron(), "Expected _wenviron to be NULL, got %p\n", *p_wenviron );
if (p_get_wenviron) if (p_get_wenviron)
{ {
WCHAR **retptr; WCHAR **retptr;
p_get_wenviron(&retptr); p_get_wenviron(&retptr);
ok( retptr == NULL, ok( retptr == *p_wenviron, "Expected _wenviron pointers to be NULL\n" );
"Expected _wenviron pointers to be NULL\n" );
} }
else else
win_skip( "_get_wenviron() is not available\n" ); win_skip( "_get_wenviron() is not available\n" );
if (p__p___winitenv)
{
wchar_t ***retptr = p__p___winitenv();
todo_wine
ok( !*retptr, "Expected initial env to be NULL\n" );
}
else
skip( "__p___winitenv() is not available\n" );
/* __getmainargs doesn't initialize _wenviron. */ /* __getmainargs doesn't initialize _wenviron. */
__getmainargs(&argc, &argv, &envp, 0, &mode); __getmainargs(&argc, &argv, &envp, 0, &mode);
ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron); ok( !*p_wenviron, "Expected _wenviron to be NULL\n");
ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" ); ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
if (!envp) if (!envp)
{ {
...@@ -182,24 +205,33 @@ static void test__wenviron(void) ...@@ -182,24 +205,33 @@ static void test__wenviron(void)
/* Neither does calling the non-Unicode environment manipulation functions. */ /* Neither does calling the non-Unicode environment manipulation functions. */
ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" ); ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron); ok( !*p_wenviron, "Expected _wenviron to be NULL\n" );
ok( _putenv("cat=") == 0, "failed deleting cat\n" );
/* _wenviron isn't initialized until __wgetmainargs is called or /* _wenviron isn't initialized until __wgetmainargs is called or
* one of the Unicode environment manipulation functions is called. */ * one of the Unicode environment manipulation functions is called. */
ok( _wputenv(L"cat=dog") == 0, "failed setting cat=dog\n" ); ok( _wputenv(L"cat=dog2") == 0, "failed setting cat=dog2\n" );
ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" ); ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
ok( _wputenv(L"cat=") == 0, "failed deleting cat\n" );
__wgetmainargs(&argc, &wargv, &wenvp, 0, &mode); __wgetmainargs(&argc, &wargv, &wenvp, 0, &mode);
ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
ok( wenvp != NULL, "Expected initial environment block pointer to be non-NULL\n" ); ok( wenvp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
if (!wenvp) todo_wine
ok( wenvp == *p_wenviron, "Expected initial environment to be _wenviron[]\n" );
if (p__p___winitenv)
{ {
skip( "Initial environment block pointer is not valid\n" ); wchar_t ***retptr = p__p___winitenv();
return; wchar_t *value;
ok( *retptr != NULL, "Expected *__p___winitenv() to be NULL\n" );
ok( *retptr != *p_wenviron,
"Expected _wenviron to be different from __p___winitenv() %p %p\n", *retptr, *p_wenviron );
/* test that w-initial env is derived from current _environ[] and not from ansi initial env */
value = env_get_valueW( *retptr, L"cat" );
todo_wine
ok( value && !wcscmp( value, L"dog" ),
"Expecting initial env to be derived from current env (got %ls)\n", value );
} }
_putenv("cat=");
/* Examine the returned pointer from __p__wenviron(), /* Examine the returned pointer from __p__wenviron(),
* if available, after _wenviron is initialized. */ * if available, after _wenviron is initialized. */
...@@ -216,22 +248,6 @@ static void test__wenviron(void) ...@@ -216,22 +248,6 @@ static void test__wenviron(void)
ok( retptr == *p_wenviron, ok( retptr == *p_wenviron,
"Expected _wenviron pointers to be identical\n" ); "Expected _wenviron pointers to be identical\n" );
} }
for (i = 0; ; i++)
{
if ((*p_wenviron)[i])
{
ok( wenvp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" );
ok( !wcscmp((*p_wenviron)[i], wenvp[i]),
"Expected _wenviron and environment block pointer strings (%s vs. %s) to match\n",
wine_dbgstr_w((*p_wenviron)[i]), wine_dbgstr_w(wenvp[i]) );
}
else
{
ok( !wenvp[i], "Expected environment block pointer element to be NULL, got %p\n", wenvp[i] );
break;
}
}
} }
static void test_environment_manipulation(void) static void test_environment_manipulation(void)
...@@ -312,8 +328,6 @@ START_TEST(environ) ...@@ -312,8 +328,6 @@ START_TEST(environ)
{ {
init(); init();
/* The environ tests should always be run first, as they assume
* that the process has not manipulated the environment. */
test__environ(); test__environ();
test__wenviron(); test__wenviron();
test_environment_manipulation(); test_environment_manipulation();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment