From e3e2a5c0acaa7d6fe26b57393037cfa2ba839f4e Mon Sep 17 00:00:00 2001 From: Francois Gouget Date: Fri, 11 Jan 2008 09:52:16 +0100 Subject: [PATCH] kernel32/tests: Add more ExpandEnvironmentStringsA() tests. Document the observed ExpandEnvironmentStrings() behavior. --- dlls/kernel32/environ.c | 19 +++++++++++++- dlls/kernel32/tests/environ.c | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/dlls/kernel32/environ.c b/dlls/kernel32/environ.c index 414ccbfb38a..7089c206996 100644 --- a/dlls/kernel32/environ.c +++ b/dlls/kernel32/environ.c @@ -305,6 +305,8 @@ BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value ) /*********************************************************************** * ExpandEnvironmentStringsA (KERNEL32.@) * + * See ExpandEnvironmentStringsW. + * * Note: overlapping buffers are not supported; this is how it should be. * FIXME: return value is wrong for MBCS */ @@ -334,6 +336,21 @@ DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count ) /*********************************************************************** * ExpandEnvironmentStringsW (KERNEL32.@) + * + * Replaces references to environment variables of the form '%EnvVar%' + * by their value. If the environment variable does not exist, then the + * reference is left as is. + * + * PARAMS + * src [I] The string to be expanded. + * dst [O] The buffer in which to put the expanded string. + * len [I] The buffer size, in characters. + * + * RETURNS + * The number of characters copied into the buffer. If the buffer is + * too small, then the required buffer size, in characters including the + * trailing '\0', is returned. + * If the function fails for some other reason, then it returns 0. */ DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len ) { @@ -346,7 +363,7 @@ DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len ) RtlInitUnicodeString(&us_src, src); - /* make sure we don't overflow maximum UNICODE_STRING size */ + /* make sure we don't overflow the maximum UNICODE_STRING size */ if (len > 0x7fff) len = 0x7fff; diff --git a/dlls/kernel32/tests/environ.c b/dlls/kernel32/tests/environ.c index 3e65e92b685..a0e425c8eab 100644 --- a/dlls/kernel32/tests/environ.c +++ b/dlls/kernel32/tests/environ.c @@ -217,9 +217,55 @@ static void test_GetSetEnvironmentVariableW(void) static void test_ExpandEnvironmentStringsA(void) { + const char* value="Long long value"; + const char* not_an_env_var="%NotAnEnvVar%"; char buf[256], buf1[256], buf2[0x8000]; DWORD ret_size, ret_size1; + SetEnvironmentVariableA("EnvVar", value); + + ret_size = ExpandEnvironmentStringsA(NULL, buf1, sizeof(buf1)); + ok(ret_size == 1 || ret_size == 0 /* Win9x */, + "ExpandEnvironmentStrings returned %d\n", ret_size); + + /* Try to get the required buffer size 'the natural way' */ + strcpy(buf, "%EnvVar%"); + ret_size = ExpandEnvironmentStringsA(buf, NULL, 0); + /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */ + ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 || ret_size == 0 /* Win95 */, + "ExpandEnvironmentStrings returned %d instead of %d\n", + ret_size, strlen(value)+1); + if (ret_size == strlen(value)+2) + trace("ExpandEnvironmentStrings is buggy: it returned len + 2\n"); + + /* Again, side-stepping the Win95 bug */ + ret_size = ExpandEnvironmentStringsA(buf, buf1, 0); + /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */ + ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2, + "ExpandEnvironmentStrings returned %d instead of %d\n", + ret_size, strlen(value)+1); + + /* Try with a buffer that's too small */ + ret_size = ExpandEnvironmentStringsA(buf, buf1, 12); + /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */ + ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2, + "ExpandEnvironmentStrings returned %d instead of %d\n", + ret_size, strlen(value)+1); + + /* Try with a buffer of just the right size */ + /* v5.1.2600.2945 (XP SP2) needs and returns len + 2 here! */ + ret_size = ExpandEnvironmentStringsA(buf, buf1, ret_size); + ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2, + "ExpandEnvironmentStrings returned %d instead of %d\n", + ret_size, strlen(value)+1); + ok(!strcmp(buf1, value), "ExpandEnvironmentStrings returned [%s]\n", buf1); + + /* Try with an unset environment variable */ + strcpy(buf, not_an_env_var); + ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1)); + ok(ret_size == strlen(not_an_env_var)+1, "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, strlen(value)+1); + ok(!strcmp(buf1, not_an_env_var), "ExpandEnvironmentStrings returned [%s]\n", buf1); + /* test a large destination size */ strcpy(buf, "12345"); ret_size = ExpandEnvironmentStringsA(buf, buf2, sizeof(buf2)); @@ -231,6 +277,8 @@ static void test_ExpandEnvironmentStringsA(void) if (ERROR_ENVVAR_NOT_FOUND == GetLastError()) return; ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf1, ret_size); + + SetEnvironmentVariableA("EnvVar", NULL); } static BOOL (WINAPI *pGetComputerNameExA)(COMPUTER_NAME_FORMAT,LPSTR,LPDWORD);