userenv.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Unit test suite for userenv functions
 *
 * Copyright 2008 Google (Lei Zhang)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21
#include <stdio.h>
22 23 24 25 26
#include <stdlib.h>
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
27
#include "winnls.h"
28
#include "winreg.h"
29 30 31 32 33 34

#include "userenv.h"

#include "wine/test.h"

#define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
35
#define expect_env(EXPECTED,GOT,VAR) ok((GOT)==(EXPECTED), "Expected %d, got %d for %s (%d)\n", (EXPECTED), (GOT), (VAR), j)
36
#define expect_gle(EXPECTED) ok(GetLastError() == (EXPECTED), "Expected %d, got %ld\n", (EXPECTED), GetLastError())
37

38 39
static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct profile_item
{
    const char * name;
};

/* Helper function for retrieving environment variables */
static BOOL get_env(const WCHAR * env, const char * var, char ** result)
{
    const WCHAR * p = env;
    int envlen, varlen, buflen;
    char buf[256];

    if (!env || !var || !result) return FALSE;

    varlen = strlen(var);
    do
    {
57 58
        if (!WideCharToMultiByte( CP_ACP, 0, p, -1, buf, sizeof(buf), NULL, NULL )) buf[sizeof(buf)-1] = 0;
        envlen = strlen(buf);
59
        if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
60 61 62 63 64 65 66 67 68 69
        {
            if (buf[varlen] == '=')
            {
                buflen = strlen(buf);
                *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
                if (!*result) return FALSE;
                memcpy(*result, buf, buflen + 1);
                return TRUE;
            }
        }
70 71
        while (*p) p++;
        p++;
72 73 74
    } while (*p);
    return FALSE;
}
75 76 77

static void test_create_env(void)
{
78
    BOOL r, is_wow64 = FALSE;
79
    HANDLE htok;
80
    WCHAR * env[4];
81
    char * st, systemroot[100], programdata[100];
82 83 84
    int i, j;

    static const struct profile_item common_vars[] = {
85 86 87 88 89 90 91 92 93 94 95
        { "ComSpec" },
        { "COMPUTERNAME" },
        { "NUMBER_OF_PROCESSORS" },
        { "OS" },
        { "PROCESSOR_ARCHITECTURE" },
        { "PROCESSOR_IDENTIFIER" },
        { "PROCESSOR_LEVEL" },
        { "PROCESSOR_REVISION" },
        { "SystemDrive" },
        { "SystemRoot" },
        { "windir" }
96 97
    };
    static const struct profile_item common_post_nt4_vars[] = {
98
        { "ALLUSERSPROFILE" },
99
        { "ProgramData" },
100 101 102 103 104 105
        { "TEMP" },
        { "TMP" },
        { "CommonProgramFiles" },
        { "ProgramFiles" },
        { "PATH" },
        { "USERPROFILE" }
106
    };
107
    static const struct profile_item common_win64_vars[] = {
108 109
        { "ProgramFiles(x86)" },
        { "CommonProgramFiles(x86)" },
110 111
        { "ProgramW6432" },
        { "CommonProgramW6432" }
112
    };
113

114 115 116
    r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
    expect(TRUE, r);

117
    r = GetEnvironmentVariableA("SystemRoot", systemroot, sizeof(systemroot));
118
    ok(r != 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
119 120 121 122

    r = SetEnvironmentVariableA("SystemRoot", "overwrite");
    expect(TRUE, r);

123
    r = GetEnvironmentVariableA("ProgramData", programdata, sizeof(programdata));
124
    ok(r != 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
125 126 127 128

    r = SetEnvironmentVariableA("ProgramData", "overwrite");
    expect(TRUE, r);

129 130 131 132 133 134
    if (0)
    {
        /* Crashes on NT4 */
        r = CreateEnvironmentBlock(NULL, NULL, FALSE);
        expect(FALSE, r);
    }
135 136 137 138

    r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
    expect(TRUE, r);

139 140 141 142 143 144
    if (0)
    {
        /* Crashes on NT4 */
        r = CreateEnvironmentBlock(NULL, htok, FALSE);
        expect(FALSE, r);
    }
145

146
    r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
147 148
    expect(TRUE, r);

149
    r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
150
    expect(TRUE, r);
151

152
    r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
153 154
    expect(TRUE, r);

155
    r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
156 157
    expect(TRUE, r);

158 159 160
    r = SetEnvironmentVariableA("SystemRoot", systemroot);
    expect(TRUE, r);

161 162 163
    r = SetEnvironmentVariableA("ProgramData", programdata);
    expect(TRUE, r);

164 165 166 167 168
    for(i=0; i<4; i++)
    {
        r = get_env(env[i], "SystemRoot", &st);
        ok(!strcmp(st, "SystemRoot=overwrite"), "%s\n", st);
        expect(TRUE, r);
169
        HeapFree(GetProcessHeap(), 0, st);
170 171 172 173 174

        r = get_env(env[i], "ProgramData", &st);
        ok(strcmp(st, "ProgramData=overwrite"), "%s\n", st);
        expect(TRUE, r);
        HeapFree(GetProcessHeap(), 0, st);
175 176
    }

177
    /* Test for common environment variables (NT4 and higher) */
178
    for (i = 0; i < ARRAY_SIZE(common_vars); i++)
179
    {
180 181 182
        for (j = 0; j < 4; j++)
        {
            r = get_env(env[j], common_vars[i].name, &st);
183
            expect_env(TRUE, r, common_vars[i].name);
184
            if (r) HeapFree(GetProcessHeap(), 0, st);
185
        }
186
    }
187

188 189 190 191 192 193 194
    /* Test for common environment variables (post NT4) */
    if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
    {
        win_skip("Some environment variables are not present on NT4\n");
    }
    else
    {
195
        for (i = 0; i < ARRAY_SIZE(common_post_nt4_vars); i++)
196 197 198 199
        {
            for (j = 0; j < 4; j++)
            {
                r = get_env(env[j], common_post_nt4_vars[i].name, &st);
200
                expect_env(TRUE, r, common_post_nt4_vars[i].name);
201
                if (r) HeapFree(GetProcessHeap(), 0, st);
202 203 204 205
            }
        }
    }

206 207 208
    if(pIsWow64Process)
        pIsWow64Process(GetCurrentProcess(), &is_wow64);
    if (sizeof(void*)==8 || is_wow64)
209
    {
210
        for (i = 0; i < ARRAY_SIZE(common_win64_vars); i++)
211
        {
212 213 214 215 216 217
            for (j=0; j<4; j++)
            {
                r = get_env(env[j], common_win64_vars[i].name, &st);
                ok(r || broken(!r)/* Vista,2k3,XP */, "Expected 1, got 0 for %s\n", common_win64_vars[i].name);
                if (r) HeapFree(GetProcessHeap(), 0, st);
            }
218
        }
219
    }
220

221
    r = get_env(env[0], "WINE_XYZZY", &st);
222
    expect(FALSE, r);
223

224
    r = get_env(env[1], "WINE_XYZZY", &st);
225
    expect(FALSE, r);
226

227
    r = get_env(env[2], "WINE_XYZZY", &st);
228
    expect(TRUE, r);
229 230
    if (r) HeapFree(GetProcessHeap(), 0, st);

231
    r = get_env(env[3], "WINE_XYZZY", &st);
232
    expect(TRUE, r);
233
    if (r) HeapFree(GetProcessHeap(), 0, st);
234

235
    for (i = 0; i < ARRAY_SIZE(env); i++)
236 237 238 239
    {
        r = DestroyEnvironmentBlock(env[i]);
        expect(TRUE, r);
    }
240 241
}

242 243 244 245 246 247 248 249 250 251 252
static void test_get_profiles_dir(void)
{
    static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
    static const char ProfilesDirectory[] = "ProfilesDirectory";
    BOOL r;
    DWORD cch, profiles_len;
    LONG l;
    HKEY key;
    char *profiles_dir, *buf, small_buf[1];

    l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
253
    ok(!l, "RegOpenKeyExA failed: %ld\n", GetLastError());
254

255 256 257
    l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &cch);
    if (l)
    {
258
        win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
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
        return;
    }
    buf = HeapAlloc(GetProcessHeap(), 0, cch);
    RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
    RegCloseKey(key);
    profiles_len = ExpandEnvironmentStringsA(buf, NULL, 0);
    profiles_dir = HeapAlloc(GetProcessHeap(), 0, profiles_len);
    ExpandEnvironmentStringsA(buf, profiles_dir, profiles_len);
    HeapFree(GetProcessHeap(), 0, buf);

    SetLastError(0xdeadbeef);
    r = GetProfilesDirectoryA(NULL, NULL);
    expect(FALSE, r);
    expect_gle(ERROR_INVALID_PARAMETER);
    SetLastError(0xdeadbeef);
    r = GetProfilesDirectoryA(NULL, &cch);
    expect(FALSE, r);
    expect_gle(ERROR_INVALID_PARAMETER);
    SetLastError(0xdeadbeef);
    cch = 1;
    r = GetProfilesDirectoryA(small_buf, &cch);
    expect(FALSE, r);
    expect_gle(ERROR_INSUFFICIENT_BUFFER);
    /* MSDN claims the returned character count includes the NULL terminator
     * when the buffer is too small, but that's not in fact what gets returned.
     */
285
    ok(cch == profiles_len - 1, "expected %ld, got %ld\n", profiles_len - 1, cch);
286 287 288 289
    /* Allocate one more character than the return value to prevent a buffer
     * overrun.
     */
    buf = HeapAlloc(GetProcessHeap(), 0, cch + 1);
290 291 292 293
    r = GetProfilesDirectoryA(buf, &cch);
    /* Rather than a BOOL, the return value is also the number of characters
     * stored in the buffer.
     */
294
    ok(profiles_len - 1 == r, "expected %ld, got %d\n", profiles_len - 1, r);
295 296 297 298
    ok(!strcmp(buf, profiles_dir), "expected %s, got %s\n", profiles_dir, buf);

    HeapFree(GetProcessHeap(), 0, buf);
    HeapFree(GetProcessHeap(), 0, profiles_dir);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

    SetLastError(0xdeadbeef);
    r = GetProfilesDirectoryW(NULL, NULL);
    expect(FALSE, r);
    expect_gle(ERROR_INVALID_PARAMETER);

    cch = 0;
    SetLastError(0xdeadbeef);
    r = GetProfilesDirectoryW(NULL, &cch);
    expect(FALSE, r);
    expect_gle(ERROR_INSUFFICIENT_BUFFER);
    ok(cch, "expected cch > 0\n");

    SetLastError(0xdeadbeef);
    r = GetProfilesDirectoryW(NULL, &cch);
    expect(FALSE, r);
    expect_gle(ERROR_INSUFFICIENT_BUFFER);
316 317
}

318 319 320
static void test_get_user_profile_dir(void)
{
    BOOL ret;
321
    DWORD error, len, len2;
322 323 324 325 326 327 328 329 330 331 332
    HANDLE token;
    char *dirA;
    WCHAR *dirW;

    if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL, 0 ))
    {
        win_skip("Skipping tests on NT4\n");
        return;
    }

    ret = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token );
333
    ok(ret, "expected success %lu\n", GetLastError());
334 335 336 337 338

    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryA( NULL, NULL, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
339
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
340 341 342 343 344

    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryA( token, NULL, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
345
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
346 347 348 349 350 351

    dirA = HeapAlloc( GetProcessHeap(), 0, 32 );
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryA( token, dirA, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
352
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
353 354 355 356 357 358 359
    HeapFree( GetProcessHeap(), 0, dirA );

    len = 0;
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryA( token, NULL, &len );
    error = GetLastError();
    ok(!ret, "expected failure\n");
360 361
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
    ok(!len, "expected 0, got %lu\n", len);
362 363 364 365 366 367 368 369

    len = 0;
    dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 32 );
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryA( token, dirA, &len );
    error = GetLastError();
    ok(!ret, "expected failure\n");
    HeapFree( GetProcessHeap(), 0, dirA );
370 371 372 373 374 375 376
    if (error == ERROR_INSUFFICIENT_BUFFER)
    {
        ok(len, "expected len > 0\n");

        dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
        SetLastError( 0xdeadbeef );
        ret = GetUserProfileDirectoryA( token, dirA, &len );
377
        ok(ret, "expected success %lu\n", GetLastError());
378
        ok(len, "expected len > 0\n");
379
        ok(lstrlenA( dirA ) == len - 1, "length mismatch %d != %ld - 1\n", lstrlenA( dirA ), len );
380 381 382 383 384
        trace("%s\n", dirA);
        HeapFree( GetProcessHeap(), 0, dirA );
    }
    else
        ok(broken(error == ERROR_INVALID_PARAMETER) /* win10 1809+ */,
385
           "unexpected error %lu\n", error);
386 387 388 389 390

    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( NULL, NULL, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
391
    todo_wine ok(error == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %lu\n", error);
392 393 394 395 396

    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( token, NULL, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
397
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
398 399 400 401 402 403

    dirW = HeapAlloc( GetProcessHeap(), 0, 32 );
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( token, dirW, NULL );
    error = GetLastError();
    ok(!ret, "expected failure\n");
404
    ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %lu\n", error);
405 406 407 408 409 410 411
    HeapFree( GetProcessHeap(), 0, dirW );

    len = 0;
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( token, NULL, &len );
    error = GetLastError();
    ok(!ret, "expected failure\n");
412
    ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %lu\n", error);
413 414 415 416 417
    ok(len, "expected len > 0\n");

    dirW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR) );
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( token, dirW, &len );
418
    ok(ret, "expected success %lu\n", GetLastError());
419
    ok(len, "expected len > 0\n");
420
    ok(lstrlenW( dirW ) == len - 1, "length mismatch %d != %ld - 1\n", lstrlenW( dirW ), len );
421 422
    HeapFree( GetProcessHeap(), 0, dirW );

423 424 425 426 427 428 429 430
    len2 = 0;
    dirW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(WCHAR) );
    SetLastError( 0xdeadbeef );
    ret = GetUserProfileDirectoryW( token, dirW, &len2 );
    error = GetLastError();
    ok(!ret, "expected failure\n");
    HeapFree( GetProcessHeap(), 0, dirW );
    if (error == ERROR_INSUFFICIENT_BUFFER)
431
        ok(len2 == len, "expected %ld, got %ld\n", len, len2);
432 433
    else
        ok(broken(error == ERROR_INVALID_PARAMETER) /* win10 1809+ */,
434
           "unexpected error %lu\n", error);
435

436 437 438
    CloseHandle( token );
}

439 440
START_TEST(userenv)
{
441 442
    pIsWow64Process = (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");

443
    test_create_env();
444
    test_get_profiles_dir();
445
    test_get_user_profile_dir();
446
}