profile.c 45.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Unit tests for profile functions
 *
 * Copyright (c) 2003 Stefan Leichter
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include <stdarg.h>
22
#include <stdio.h>
23

24
#include "wine/test.h"
25
#include "windef.h"
26 27 28 29 30 31
#include "winbase.h"
#include "windows.h"

#define KEY      "ProfileInt"
#define SECTION  "Test"
#define TESTFILE ".\\testwine.ini"
32
#define TESTFILE2 ".\\testwine2.ini"
33 34 35 36 37 38 39 40

struct _profileInt { 
    LPCSTR section;
    LPCSTR key;
    LPCSTR value;
    LPCSTR iniFile;
    INT defaultVal;
    UINT result;
41
    UINT result9x;
42 43 44 45 46
};

static void test_profile_int(void)
{
    struct _profileInt profileInt[]={
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
         { NULL,    NULL, NULL,          NULL,     70, 0          , 0}, /*  0 */
         { NULL,    NULL, NULL,          TESTFILE, -1, 4294967295U, 0},
         { NULL,    NULL, NULL,          TESTFILE,  1, 1          , 0},
         { SECTION, NULL, NULL,          TESTFILE, -1, 4294967295U, 0},
         { SECTION, NULL, NULL,          TESTFILE,  1, 1          , 0},
         { NULL,    KEY,  NULL,          TESTFILE, -1, 4294967295U, 0}, /*  5 */
         { NULL,    KEY,  NULL,          TESTFILE,  1, 1          , 0},
         { SECTION, KEY,  NULL,          TESTFILE, -1, 4294967295U, 4294967295U},
         { SECTION, KEY,  NULL,          TESTFILE,  1, 1          , 1},
         { SECTION, KEY,  "-1",          TESTFILE, -1, 4294967295U, 4294967295U},
         { SECTION, KEY,  "-1",          TESTFILE,  1, 4294967295U, 4294967295U}, /* 10 */
         { SECTION, KEY,  "1",           TESTFILE, -1, 1          , 1},
         { SECTION, KEY,  "1",           TESTFILE,  1, 1          , 1},
         { SECTION, KEY,  "+1",          TESTFILE, -1, 1          , 0},
         { SECTION, KEY,  "+1",          TESTFILE,  1, 1          , 0},
         { SECTION, KEY,  "4294967296",  TESTFILE, -1, 0          , 0}, /* 15 */
         { SECTION, KEY,  "4294967296",  TESTFILE,  1, 0          , 0},
         { SECTION, KEY,  "4294967297",  TESTFILE, -1, 1          , 1},
         { SECTION, KEY,  "4294967297",  TESTFILE,  1, 1          , 1},
         { SECTION, KEY,  "-4294967297", TESTFILE, -1, 4294967295U, 4294967295U},
         { SECTION, KEY,  "-4294967297", TESTFILE,  1, 4294967295U, 4294967295U}, /* 20 */
         { SECTION, KEY,  "42A94967297", TESTFILE, -1, 42         , 42},
         { SECTION, KEY,  "42A94967297", TESTFILE,  1, 42         , 42},
         { SECTION, KEY,  "B4294967297", TESTFILE, -1, 0          , 0},
         { SECTION, KEY,  "B4294967297", TESTFILE,  1, 0          , 0},
72 73 74 75 76 77 78 79 80 81 82 83 84
    };
    int i, num_test = (sizeof(profileInt)/sizeof(struct _profileInt));
    UINT res;

    DeleteFileA( TESTFILE);

    for (i=0; i < num_test; i++) {
        if (profileInt[i].value)
            WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value, 
                                      profileInt[i].iniFile);

       res = GetPrivateProfileIntA(profileInt[i].section, profileInt[i].key, 
                 profileInt[i].defaultVal, profileInt[i].iniFile); 
85 86 87
       ok((res == profileInt[i].result) || (res == profileInt[i].result9x),
	    "test<%02d>: ret<%010u> exp<%010u><%010u>\n",
            i, res, profileInt[i].result, profileInt[i].result9x);
88 89 90 91 92
    }

    DeleteFileA( TESTFILE);
}

93
static void test_profile_string(void)
94
{
95 96 97 98 99 100
    static WCHAR emptyW[] = { 0 };
    static WCHAR keyW[] = { 'k','e','y',0 };
    static WCHAR sW[] = { 's',0 };
    static WCHAR TESTFILE2W[] = {'.','\\','t','e','s','t','w','i','n','e','2','.','i','n','i',0};
    static WCHAR valsectionW[] = {'v','a','l','_','e','_','s','e','c','t','i','o','n',0 };
    static WCHAR valnokeyW[] = {'v','a','l','_','n','o','_','k','e','y',0};
101 102 103 104
    HANDLE h;
    int ret;
    DWORD count;
    char buf[100];
105
    WCHAR bufW[100];
106 107 108 109
    char *p;
    /* test that lines without an '=' will not be enumerated */
    /* in the case below, name2 is a key while name3 is not. */
    char content[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
110 111
    char content2[]="\r\nkey=val_no_section\r\n[]\r\nkey=val_e_section\r\n"
                    "[s]\r\n=val_no_key\r\n[t]\r\n";
112 113 114 115 116 117 118
    DeleteFileA( TESTFILE2);
    h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
    if( h == INVALID_HANDLE_VALUE) return;
    WriteFile( h, content, sizeof(content), &count, NULL);
    CloseHandle( h);
119

120 121 122 123 124 125
    /* enumerate the keys */
    ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
        TESTFILE2);
    for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1) 
        p[-1] = ',';
    /* and test */
126
    ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
127
            buf);
128

129 130 131 132 133 134
    /* add a new key to test that the file is quite usable */
    WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2); 
    ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
        TESTFILE2);
    for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1) 
        p[-1] = ',';
135 136 137
    ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
            ret, buf);

138 139 140 141 142 143 144 145 146 147 148
    h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
    if( h == INVALID_HANDLE_VALUE) return;
    WriteFile( h, content2, sizeof(content2), &count, NULL);
    CloseHandle( h);

    /* works only in unicode, ascii crashes */
    ret=GetPrivateProfileStringW(emptyW, keyW, emptyW, bufW,
                                 sizeof(bufW)/sizeof(bufW[0]), TESTFILE2W);
    todo_wine
149 150
    ok(ret == 13, "expected 13, got %u\n", ret);
    todo_wine
151 152 153 154 155 156 157
    ok(!lstrcmpW(valsectionW,bufW), "expected %s, got %s\n",
        wine_dbgstr_w(valsectionW), wine_dbgstr_w(bufW) );

    /* works only in unicode, ascii crashes */
    ret=GetPrivateProfileStringW(sW, emptyW, emptyW, bufW,
                                 sizeof(bufW)/sizeof(bufW[0]), TESTFILE2W);
    todo_wine
158 159
    ok(ret == 10, "expected 10, got %u\n", ret);
    todo_wine
160 161 162
    ok(!lstrcmpW(valnokeyW,bufW), "expected %s, got %s\n",
        wine_dbgstr_w(valnokeyW), wine_dbgstr_w(bufW) );

163 164 165
    DeleteFileA( TESTFILE2);
}

166 167 168 169 170 171 172
static void test_profile_sections(void)
{
    HANDLE h;
    int ret;
    DWORD count;
    char buf[100];
    char *p;
173
    static const char content[]="[section1]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n[section2]\r\n";
174
    static const char testfile4[]=".\\testwine4.ini";
175
    BOOL on_win98 = FALSE;
176 177 178 179 180 181 182 183

    DeleteFileA( testfile4 );
    h = CreateFileA( testfile4, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile4);
    if( h == INVALID_HANDLE_VALUE) return;
    WriteFile( h, content, sizeof(content), &count, NULL);
    CloseHandle( h);

184 185 186 187
    /* Some parameter checking */
    SetLastError(0xdeadbeef);
    ret = GetPrivateProfileSectionA( NULL, NULL, 0, NULL );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
188 189 190 191
    ok( GetLastError() == ERROR_INVALID_PARAMETER ||
        GetLastError() == 0xdeadbeef /* Win98 */,
        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    if (GetLastError() == 0xdeadbeef) on_win98 = TRUE;
192 193 194 195

    SetLastError(0xdeadbeef);
    ret = GetPrivateProfileSectionA( NULL, NULL, 0, testfile4 );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
196 197 198 199 200 201 202 203 204 205 206
    ok( GetLastError() == ERROR_INVALID_PARAMETER ||
        GetLastError() == 0xdeadbeef /* Win98 */,
        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());

    if (!on_win98)
    {
        SetLastError(0xdeadbeef);
        ret = GetPrivateProfileSectionA( "section1", NULL, 0, testfile4 );
        ok( ret == 0, "expected return size 0, got %d\n", ret );
        ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
    }
207 208 209 210

    SetLastError(0xdeadbeef);
    ret = GetPrivateProfileSectionA( NULL, buf, sizeof(buf), testfile4 );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
211 212 213
    ok( GetLastError() == ERROR_INVALID_PARAMETER ||
        broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
214 215 216 217 218

    SetLastError(0xdeadbeef);
    ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    todo_wine
219 220 221
    ok( GetLastError() == ERROR_FILE_NOT_FOUND ||
        broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
222

223 224 225 226 227 228 229 230 231
    /* Existing empty section with no keys */
    SetLastError(0xdeadbeef);
    ret=GetPrivateProfileSectionA("section2", buf, sizeof(buf), testfile4);
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    ok( GetLastError() == ERROR_SUCCESS ||
        broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "expected ERROR_SUCCESS, got %d\n", GetLastError());

    /* Existing section with keys and values*/
232
    SetLastError(0xdeadbeef);
233
    ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
234 235 236 237
    for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
        p[-1] = ',';
    ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
            ret, buf);
238
    ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
239 240 241
    ok( GetLastError() == ERROR_SUCCESS ||
        broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "expected ERROR_SUCCESS, got %d\n", GetLastError());
242

243 244 245 246 247 248 249 250
    /* Overflow*/
    ret=GetPrivateProfileSectionA("section1", buf, 24, testfile4);
    for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
        p[-1] = ',';
    ok( ret == 22 && !strcmp( buf, "name1=val1,name2=,name"), "wrong section returned(%d): %s\n",
        ret, buf);
    ok( buf[ret] == 0 && buf[ret+1] == 0, "returned buffer not terminated with double-null\n" );

251 252 253
    DeleteFileA( testfile4 );
}

254
static void test_profile_sections_names(void)
255 256 257 258 259 260 261 262 263
{
    HANDLE h;
    int ret;
    DWORD count;
    char buf[100];
    WCHAR bufW[100];
    static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
    static const char testfile3[]=".\\testwine3.ini";
    static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
264
    static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
265 266 267 268 269 270 271 272 273
    DeleteFileA( testfile3 );
    h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
    if( h == INVALID_HANDLE_VALUE) return;
    WriteFile( h, content, sizeof(content), &count, NULL);
    CloseHandle( h);

    /* Test with sufficiently large buffer */
274
    memset(buf, 0xc, sizeof(buf));
275
    ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
276 277 278 279 280 281 282
    ok( ret == 27 ||
        broken(ret == 28), /* Win9x, WinME */
        "expected return size 27, got %d\n", ret );
    ok( (buf[ret-1] == 0 && buf[ret] == 0) ||
        broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
        "returned buffer not terminated with double-null\n" );

283
    /* Test with exactly fitting buffer */
284
    memset(buf, 0xc, sizeof(buf));
285
    ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
286 287 288
    ok( ret == 26 ||
        broken(ret == 28), /* Win9x, WinME */
        "expected return size 26, got %d\n", ret );
289
    todo_wine
290 291 292 293 294
    ok( (buf[ret+1] == 0 && buf[ret] == 0) || /* W2K3 and higher */
        broken(buf[ret+1] == 0xc && buf[ret] == 0) || /* NT4, W2K, WinXP */
        broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
        "returned buffer not terminated with double-null\n" );

295
    /* Test with a buffer too small */
296
    memset(buf, 0xc, sizeof(buf));
297 298
    ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
    ok( ret == 25, "expected return size 25, got %d\n", ret );
299 300
    /* Win9x and WinME only fills the buffer with complete section names (double-null terminated) */
    count = strlen("section1") + sizeof(CHAR) + strlen("section2");
301
    todo_wine
302 303 304 305
    ok( (buf[ret+1] == 0 && buf[ret] == 0) ||
        broken(buf[count] == 0 && buf[count+1] == 0), /* Win9x, WinME */
        "returned buffer not terminated with double-null\n" );

306
    /* Tests on nonexistent file */
307
    memset(buf, 0xc, sizeof(buf));
308
    ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
309 310 311
    ok( ret == 0 ||
        broken(ret == 1), /* Win9x, WinME */
        "expected return size 0, got %d\n", ret );
312 313
    ok( buf[0] == 0, "returned buffer not terminated with null\n" );
    ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
314 315
    
    /* Test with sufficiently large buffer */
316
    SetLastError(0xdeadbeef);
317
    memset(bufW, 0xcc, sizeof(bufW));
318
    ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
319
    if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
320
    {
321
        win_skip("GetPrivateProfileSectionNamesW is not implemented\n");
322 323 324
        DeleteFileA( testfile3 );
        return;
    }
325
    ok( ret == 27, "expected return size 27, got %d\n", ret );
326
    ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
327 328
    
    /* Test with exactly fitting buffer */
329
    memset(bufW, 0xcc, sizeof(bufW));
330 331
    ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
    ok( ret == 26, "expected return size 26, got %d\n", ret );
332 333 334 335
    ok( (bufW[ret+1] == 0 && bufW[ret] == 0) || /* W2K3 and higher */
        broken(bufW[ret+1] == 0xcccc && bufW[ret] == 0), /* NT4, W2K, WinXP */
        "returned buffer not terminated with double-null\n" );

336
    /* Test with a buffer too small */
337
    memset(bufW, 0xcc, sizeof(bufW));
338 339
    ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
    ok( ret == 25, "expected return size 25, got %d\n", ret );
340
    ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
341 342
    
    DeleteFileA( testfile3 );
343

344
    /* Tests on nonexistent file */
345 346 347 348 349
    memset(bufW, 0xcc, sizeof(bufW));
    ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
    ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
350 351
}

352 353 354
/* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION,  some testing here */
static void test_profile_existing(void)
{
355 356 357
    static const char *testfile1 = ".\\winesharing1.ini";
    static const char *testfile2 = ".\\winesharing2.ini";

358 359 360
    static const struct {
        DWORD dwDesiredAccess;
        DWORD dwShareMode;
361 362
        DWORD write_error;
        BOOL read_error;
363
        DWORD broken_error;
364
    } pe[] = {
365 366 367 368 369 370
        {GENERIC_READ,  FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
        {GENERIC_READ,  FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
        {GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
        {GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
        {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
        {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
371 372
        {GENERIC_READ,  FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
        {GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
373
        /*Thief demo (bug 5024) opens .ini file like this*/
374
        {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */}
375 376 377 378 379 380 381 382 383 384
    };

    int i;
    BOOL ret;
    DWORD size;
    HANDLE h = 0;
    char buffer[MAX_PATH];

    for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
    {
385 386
        h = CreateFile(testfile1, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
                       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
387 388
        ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
        SetLastError(0xdeadbeef);
389 390 391

        ret = WritePrivateProfileString(SECTION, KEY, "12345", testfile1);
        if (!pe[i].write_error)
392
        {
393 394 395
            if (!ret)
                ok( broken(GetLastError() == pe[i].broken_error),
                    "%d: WritePrivateProfileString failed with error %u\n", i, GetLastError() );
396
            CloseHandle(h);
397
            size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
398 399 400 401
            if (ret)
                ok( size == 5, "%d: test failed, number of characters copied: %d instead of 5\n", i, size );
            else
                ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
402 403 404 405 406
        }
        else
        {
            DWORD err = GetLastError();
            ok( !ret, "%d: WritePrivateProfileString succeeded\n", i );
407 408 409
            if (!ret)
                ok( err == pe[i].write_error, "%d: WritePrivateProfileString failed with error %u/%u\n",
                    i, err, pe[i].write_error );
410
            CloseHandle(h);
411
            size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
412 413
            ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
        }
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

        ok( DeleteFile(testfile1), "delete failed\n" );
    }

    h = CreateFile(testfile2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    sprintf( buffer, "[%s]\r\n%s=123\r\n", SECTION, KEY );
    ok( WriteFile( h, buffer, strlen(buffer), &size, NULL ), "failed to write\n" );
    CloseHandle( h );

    for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
    {
        h = CreateFile(testfile2, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
        SetLastError(0xdeadbeef);
        ret = GetPrivateProfileStringA(SECTION, KEY, NULL, buffer, MAX_PATH, testfile2);
430
        /* Win9x and WinME returns 0 for all cases except the first one */
431
        if (!pe[i].read_error)
432 433 434
            ok( ret ||
                broken(!ret && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
                "%d: GetPrivateProfileString failed with error %u\n", i, GetLastError() );
435 436 437
        else
            ok( !ret, "%d: GetPrivateProfileString succeeded\n", i );
        CloseHandle(h);
438
    }
439
    ok( DeleteFile(testfile2), "delete failed\n" );
440 441
}

442
static void test_profile_delete_on_close(void)
443 444 445 446 447 448 449 450
{
    static CHAR testfile[] = ".\\testwine5.ini";
    HANDLE h;
    DWORD size, res;
    static const char contents[] = "[" SECTION "]\n" KEY "=123\n";

    h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                    CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
451 452
    res = WriteFile( h, contents, sizeof contents - 1, &size, NULL );
    ok( res, "Cannot write test file: %x\n", GetLastError() );
453 454
    ok( size == sizeof contents - 1, "Test file: partial write\n");

455
    SetLastError(0xdeadbeef);
456
    res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
457 458 459
    ok( res == 123 ||
        broken(res == 0 && GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x, WinME */
        "Got %d instead of 123\n", res);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

    /* This also deletes the file */
    CloseHandle(h);
}

static void test_profile_refresh(void)
{
    static CHAR testfile[] = ".\\winetest4.ini";
    HANDLE h;
    DWORD size, res;
    static const char contents1[] = "[" SECTION "]\n" KEY "=123\n";
    static const char contents2[] = "[" SECTION "]\n" KEY "=124\n";

    h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                    CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
475 476
    res = WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL );
    ok( res, "Cannot write test file: %x\n", GetLastError() );
477
    ok( size == sizeof contents1 - 1, "Test file: partial write\n");
478

479
    SetLastError(0xdeadbeef);
480
    res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
481 482 483
    ok( res == 123 ||
        broken(res == 0 && GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x, WinME */
        "Got %d instead of 123\n", res);
484

485 486 487 488 489 490
    CloseHandle(h);

    /* Test proper invalidation of wine's profile file cache */

    h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                    CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
491 492
    res = WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL );
    ok( res, "Cannot write test file: %x\n", GetLastError() );
493 494
    ok( size == sizeof contents2 - 1, "Test file: partial write\n");

495
    SetLastError(0xdeadbeef);
496
    res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
497 498 499
    ok( res == 124 ||
        broken(res == 0 && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "Got %d instead of 124\n", res);
500

501 502
    /* This also deletes the file */
    CloseHandle(h);
503 504 505 506

    /* Cache must be invalidated if file no longer exists and default must be returned */
    SetLastError(0xdeadbeef);
    res = GetPrivateProfileInt(SECTION, KEY, 421, testfile);
507
    ok( res == 421 ||
508 509
        broken(res == 0 && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
        "Got %d instead of 421\n", res);
510 511
}

512 513 514 515 516 517 518 519 520 521 522
static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
{
    HANDLE hfile;
    DWORD count;

    hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
    WriteFile(hfile, data, size, &count, NULL);
    CloseHandle(hfile);
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536
static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
{
    int i;

    for(i = 0;i < MAX_PATH;++i)
        if(emptystr[i] != 0)
        {
            trace("emptystr[%d] = %d\n",i,emptystr[i]);
            return FALSE;
        }

    return TRUE;
}

537
static void test_GetPrivateProfileString(const char *content, const char *descript)
538
{
539
    DWORD ret, len;
540
    CHAR buf[MAX_PATH];
541
    CHAR def_val[MAX_PATH];
542 543
    CHAR path[MAX_PATH];
    CHAR windir[MAX_PATH];
544 545 546
    /* NT series crashes on r/o empty strings, so pass an r/w
       empty string and check for modification */
    CHAR emptystr[MAX_PATH] = "";
547 548 549 550
    LPSTR tempfile;

    static const char filename[] = ".\\winetest.ini";

551 552
    trace("test_GetPrivateProfileStringA: %s\n", descript);

553 554 555 556 557 558 559 560 561 562 563 564
    if(!lstrcmpA(descript, "CR only"))
    {
        SetLastError(0xdeadbeef);
        ret = GetPrivateProfileStringW(NULL, NULL, NULL,
                                       NULL, 0, NULL);
        if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
        {
            win_skip("Win9x and WinME don't handle 'CR only' correctly\n");
            return;
        }
    }

565
    create_test_file(filename, content, lstrlenA(content));
566

567 568 569 570
    /* Run this test series with caching. Wine won't cache profile
       files younger than 2.1 seconds. */
    Sleep(2500);

571
    /* lpAppName is NULL */
572
    memset(buf, 0xc, sizeof(buf));
573 574 575
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA(NULL, "name1", "default",
                                   buf, MAX_PATH, filename);
576 577 578 579
    ok(ret == 18 ||
       broken(ret == 19), /* Win9x and WinME */
       "Expected 18, got %d\n", ret);
    len = lstrlenA("section1") + sizeof(CHAR) + lstrlenA("section2") + 2 * sizeof(CHAR);
580 581
    ok(!memcmp(buf, "section1\0section2\0\0", len),
       "Expected \"section1\\0section2\\0\\0\", got \"%s\"\n", buf);
582 583

    /* lpAppName is empty */
584
    memset(buf, 0xc, sizeof(buf));
585
    lstrcpyA(buf, "kumquat");
586
    ret = GetPrivateProfileStringA(emptystr, "name1", "default",
587 588 589
                                   buf, MAX_PATH, filename);
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
590
    ok(emptystr_ok(emptystr), "AppName modified\n");
591 592

    /* lpAppName is missing */
593
    memset(buf, 0xc,sizeof(buf));
594 595 596 597 598 599 600
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("notasection", "name1", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);

    /* lpAppName is empty, lpDefault is NULL */
601
    memset(buf, 0xc,sizeof(buf));
602
    lstrcpyA(buf, "kumquat");
603
    ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
604 605
                                   buf, MAX_PATH, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
606 607 608
    ok(!lstrcmpA(buf, "") ||
       broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
       "Expected \"\", got \"%s\"\n", buf);
609
    ok(emptystr_ok(emptystr), "AppName modified\n");
610 611

    /* lpAppName is empty, lpDefault is empty */
612
    memset(buf, 0xc,sizeof(buf));
613
    lstrcpyA(buf, "kumquat");
614
    ret = GetPrivateProfileStringA(emptystr, "name1", "",
615 616 617
                                   buf, MAX_PATH, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
    ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
618
    ok(emptystr_ok(emptystr), "AppName modified\n");
619 620

    /* lpAppName is empty, lpDefault has trailing blank characters */
621
    memset(buf, 0xc,sizeof(buf));
622
    lstrcpyA(buf, "kumquat");
623
    /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
624
    lstrcpyA(def_val, "default  ");
625
    ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
626
                                   buf, MAX_PATH, filename);
627 628
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
629
    ok(emptystr_ok(emptystr), "AppName modified\n");
630 631

    /* lpAppName is empty, many blank characters in lpDefault */
632
    memset(buf, 0xc,sizeof(buf));
633
    lstrcpyA(buf, "kumquat");
634
    /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
635
    lstrcpyA(def_val, "one two  ");
636
    ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
637
                                   buf, MAX_PATH, filename);
638 639
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
640
    ok(emptystr_ok(emptystr), "AppName modified\n");
641 642

    /* lpAppName is empty, blank character but not trailing in lpDefault */
643
    memset(buf, 0xc,sizeof(buf));
644
    lstrcpyA(buf, "kumquat");
645
    ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
646 647 648
                                   buf, MAX_PATH, filename);
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
649
    ok(emptystr_ok(emptystr), "AppName modified\n");
650 651

    /* lpKeyName is NULL */
652
    memset(buf, 0xc,sizeof(buf));
653 654 655 656 657 658 659 660
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", NULL, "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 18, "Expected 18, got %d\n", ret);
    ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
       "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf);

    /* lpKeyName is empty */
661
    memset(buf, 0xc,sizeof(buf));
662
    lstrcpyA(buf, "kumquat");
663
    ret = GetPrivateProfileStringA("section1", emptystr, "default",
664
                                   buf, MAX_PATH, filename);
665 666
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
667
    ok(emptystr_ok(emptystr), "KeyName modified\n");
668 669

    /* lpKeyName is missing */
670
    memset(buf, 0xc,sizeof(buf));
671 672 673 674 675 676 677
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "notakey", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);

    /* lpKeyName is empty, lpDefault is NULL */
678
    memset(buf, 0xc,sizeof(buf));
679
    lstrcpyA(buf, "kumquat");
680
    ret = GetPrivateProfileStringA("section1", emptystr, NULL,
681 682
                                   buf, MAX_PATH, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
683 684 685
    ok(!lstrcmpA(buf, "") ||
       broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
       "Expected \"\", got \"%s\"\n", buf);
686
    ok(emptystr_ok(emptystr), "KeyName modified\n");
687 688

    /* lpKeyName is empty, lpDefault is empty */
689
    memset(buf, 0xc,sizeof(buf));
690
    lstrcpyA(buf, "kumquat");
691
    ret = GetPrivateProfileStringA("section1", emptystr, "",
692 693
                                   buf, MAX_PATH, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
694
    ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
695
    ok(emptystr_ok(emptystr), "KeyName modified\n");
696 697

    /* lpKeyName is empty, lpDefault has trailing blank characters */
698
    memset(buf, 0xc,sizeof(buf));
699
    lstrcpyA(buf, "kumquat");
700
    /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
701
    lstrcpyA(def_val, "default  ");
702
    ret = GetPrivateProfileStringA("section1", emptystr, def_val,
703
                                   buf, MAX_PATH, filename);
704 705
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
706
    ok(emptystr_ok(emptystr), "KeyName modified\n");
707 708 709 710 711 712 713 714 715

    if (0) /* crashes */
    {
        /* lpReturnedString is NULL */
        ret = GetPrivateProfileStringA("section1", "name1", "default",
                                       NULL, MAX_PATH, filename);
    }

    /* lpFileName is NULL */
716
    memset(buf, 0xc,sizeof(buf));
717 718 719
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, MAX_PATH, NULL);
720 721 722 723 724 725
    ok(ret == 7 ||
       broken(ret == 0), /* Win9x, WinME */
       "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default") ||
       broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
       "Expected \"default\", got \"%s\"\n", buf);
726 727

    /* lpFileName is empty */
728
    memset(buf, 0xc,sizeof(buf));
729 730 731 732 733 734 735
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, MAX_PATH, "");
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);

    /* lpFileName is nonexistent */
736
    memset(buf, 0xc,sizeof(buf));
737 738 739 740 741 742 743
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, MAX_PATH, "nonexistent");
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);

    /* nSize is 0 */
744
    memset(buf, 0xc,sizeof(buf));
745 746 747 748 749 750 751
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, 0, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
    ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);

    /* nSize is exact size of output */
752
    memset(buf, 0xc,sizeof(buf));
753 754 755 756 757 758 759
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, 4, filename);
    ok(ret == 3, "Expected 3, got %d\n", ret);
    ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);

    /* nSize has room for NULL terminator */
760
    memset(buf, 0xc,sizeof(buf));
761 762 763 764 765 766 767
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, 5, filename);
    ok(ret == 4, "Expected 4, got %d\n", ret);
    ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);

    /* output is 1 character */
768
    memset(buf, 0xc,sizeof(buf));
769 770 771 772 773 774 775
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name4", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 1, "Expected 1, got %d\n", ret);
    ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);

    /* output is 1 character, no room for NULL terminator */
776
    memset(buf, 0xc,sizeof(buf));
777 778 779 780 781 782 783
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name4", "default",
                                   buf, 1, filename);
    ok(ret == 0, "Expected 0, got %d\n", ret);
    ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);

    /* lpAppName is NULL, not enough room for final section name */
784
    memset(buf, 0xc,sizeof(buf));
785 786 787 788
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA(NULL, "name1", "default",
                                   buf, 16, filename);
    ok(ret == 14, "Expected 14, got %d\n", ret);
789
    len = lstrlenA("section1") + 2 * sizeof(CHAR);
790 791
    todo_wine
    ok(!memcmp(buf, "section1\0secti\0\0", ret + 2) ||
792
       broken(!memcmp(buf, "section1\0\0", len)), /* Win9x, WinME */
793
       "Expected \"section1\\0secti\\0\\0\", got \"%s\"\n", buf);
794 795

    /* lpKeyName is NULL, not enough room for final key name */
796
    memset(buf, 0xc,sizeof(buf));
797 798 799 800
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", NULL, "default",
                                   buf, 16, filename);
    ok(ret == 14, "Expected 14, got %d\n", ret);
801 802
    todo_wine
    ok(!memcmp(buf, "name1\0name2\0na\0\0", ret + 2) ||
803
       broken(!memcmp(buf, "name1\0name2\0n\0\0", ret + 1)), /* Win9x, WinME */
804
       "Expected \"name1\\0name2\\0na\\0\\0\", got \"%s\"\n", buf);
805 806

    /* key value has quotation marks which are stripped */
807
    memset(buf, 0xc,sizeof(buf));
808 809 810 811 812 813 814
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name2", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 4, "Expected 4, got %d\n", ret);
    ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);

    /* case does not match */
815
    memset(buf, 0xc,sizeof(buf));
816 817 818 819 820 821 822
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 4, "Expected 4, got %d\n", ret);
    ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);

    /* only filename is used */
823
    memset(buf, 0xc,sizeof(buf));
824 825 826 827 828 829 830
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
                                   buf, MAX_PATH, "winetest.ini");
    ok(ret == 7, "Expected 7, got %d\n", ret);
    ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);

    GetWindowsDirectoryA(windir, MAX_PATH);
831 832 833 834 835 836 837 838
    SetLastError(0xdeadbeef);
    ret = GetTempFileNameA(windir, "pre", 0, path);
    if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
    {
        skip("Not allowed to create a file in the Windows directory\n");
        DeleteFileA(filename);
        return;
    }
839
    tempfile = strrchr(path, '\\') + 1;
840
    create_test_file(path, content, lstrlenA(content));
841 842

    /* only filename is used, file exists in windows directory */
843
    memset(buf, 0xc,sizeof(buf));
844 845 846 847 848 849 850
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
                                   buf, MAX_PATH, tempfile);
    ok(ret == 4, "Expected 4, got %d\n", ret);
    ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);

    /* successful case */
851
    memset(buf, 0xc,sizeof(buf));
852 853 854 855 856 857
    lstrcpyA(buf, "kumquat");
    ret = GetPrivateProfileStringA("section1", "name1", "default",
                                   buf, MAX_PATH, filename);
    ok(ret == 4, "Expected 4, got %d\n", ret);
    ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);

858 859 860 861 862 863 864 865
 /* Existing section with no keys in an existing file */
    memset(buf, 0xc,sizeof(buf));
    SetLastError(0xdeadbeef);
    ret=GetPrivateProfileStringA("section2", "DoesntExist", "",
                                 buf, MAX_PATH, filename);
    ok( ret == 0, "expected return size 0, got %d\n", ret );
    ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
    todo_wine
866 867 868
   ok( GetLastError() == 0xdeadbeef ||
       GetLastError() == ERROR_FILE_NOT_FOUND /* Win 7 */,
       "expected 0xdeadbeef or ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
869 870


871 872 873 874
    DeleteFileA(path);
    DeleteFileA(filename);
}

875
static BOOL check_binary_file_data(LPCSTR path, const VOID *data, DWORD size)
876 877 878 879 880 881 882 883 884
{
    HANDLE file;
    CHAR buf[MAX_PATH];
    BOOL ret;

    file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
    if (file == INVALID_HANDLE_VALUE)
      return FALSE;

885 886 887 888 889 890
    if(size != GetFileSize(file, NULL) )
    {
        CloseHandle(file);
        return FALSE;
    }

891 892 893 894 895
    ret = ReadFile(file, buf, size, &size, NULL);
    CloseHandle(file);
    if (!ret)
      return FALSE;

896 897
    return !memcmp(buf, data, size);
}
898

899 900 901
static BOOL check_file_data(LPCSTR path, LPCSTR data)
{
    return check_binary_file_data(path, data, lstrlenA(data));
902 903 904 905 906 907 908 909 910
}

static void test_WritePrivateProfileString(void)
{
    BOOL ret;
    LPCSTR data;
    CHAR path[MAX_PATH];
    CHAR temp[MAX_PATH];

911 912 913 914 915 916 917 918 919 920 921 922 923
    SetLastError(0xdeadbeef);
    ret = WritePrivateProfileStringW(NULL, NULL, NULL, NULL);
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        /* Win9x/WinME needs (variable) timeouts between tests and even long timeouts don't
         * guarantee a correct result.
         * Win9x/WinMe also produces different ini files where there is always a newline before
         * a section start (except for the first one).
         */
        win_skip("WritePrivateProfileString on Win9x/WinME is hard to test reliably\n");
        return;
    }

924 925 926 927 928 929 930 931 932 933
    GetTempPathA(MAX_PATH, temp);
    GetTempFileNameA(temp, "wine", 0, path);
    DeleteFileA(path);

    /* path is not created yet */

    /* NULL lpAppName */
    SetLastError(0xdeadbeef);
    ret = WritePrivateProfileStringA(NULL, "key", "string", path);
    ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
934 935 936
    ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
       broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
       broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
937 938 939 940 941 942 943 944 945 946 947
       "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
    ok(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES,
       "Expected path to not exist\n");

    GetTempFileNameA(temp, "wine", 0, path);

    /* NULL lpAppName, path exists */
    data = "";
    SetLastError(0xdeadbeef);
    ret = WritePrivateProfileStringA(NULL, "key", "string", path);
    ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
948 949 950
    ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
       broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
       broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
951
       "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
952 953 954
    ok(check_file_data(path, data), "File doesn't match\n");
    DeleteFileA(path);

955 956 957
    if (0)
    {
    /* empty lpAppName, crashes on NT4 and higher */
958 959 960 961
    data = "[]\r\n"
           "key=string\r\n";
    ret = WritePrivateProfileStringA("", "key", "string", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
962
    ok(check_file_data(path, data), "File doesn't match\n");
963
    DeleteFileA(path);
964
    }
965 966 967 968 969 970 971 972 973 974 975

    /* NULL lpKeyName */
    data = "";
    ret = WritePrivateProfileStringA("App", NULL, "string", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
    todo_wine
    {
        ok(check_file_data(path, data), "File doesn't match\n");
    }
    DeleteFileA(path);

976 977 978
    if (0)
    {
    /* empty lpKeyName, crashes on NT4 and higher */
979 980 981 982 983 984 985 986 987
    data = "[App]\r\n"
           "=string\r\n";
    ret = WritePrivateProfileStringA("App", "", "string", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
    todo_wine
    {
        ok(check_file_data(path, data), "File doesn't match\n");
    }
    DeleteFileA(path);
988
    }
989 990 991 992 993 994 995

    /* NULL lpString */
    data = "";
    ret = WritePrivateProfileStringA("App", "key", NULL, path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
    todo_wine
    {
996 997 998
        ok(check_file_data(path, data) ||
           (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
           "File doesn't match\n");
999 1000 1001 1002 1003 1004 1005
    }
    DeleteFileA(path);

    /* empty lpString */
    data = "[App]\r\n"
           "key=\r\n";
    ret = WritePrivateProfileStringA("App", "key", "", path);
1006 1007 1008 1009 1010 1011
    ok(ret == TRUE ||
       broken(!ret), /* Win9x and WinME */
       "Expected TRUE, got %d\n", ret);
    ok(check_file_data(path, data) ||
       (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
       "File doesn't match\n");
1012 1013 1014 1015 1016 1017
    DeleteFileA(path);

    /* empty lpFileName */
    SetLastError(0xdeadbeef);
    ret = WritePrivateProfileStringA("App", "key", "string", "");
    ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1018 1019
    ok(GetLastError() == ERROR_ACCESS_DENIED ||
       broken(GetLastError() == ERROR_PATH_NOT_FOUND), /* Win9x and WinME */
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
       "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());

    /* The resulting file will be  X:\\%WINDIR%\\win1.tmp */
    GetWindowsDirectoryA(temp, MAX_PATH);
    GetTempFileNameA(temp, "win", 1, path);
    DeleteFileA(path);

    /* relative path in lpFileName */
    data = "[App]\r\n"
           "key=string\r\n";
    ret = WritePrivateProfileStringA("App", "key", "string", "win1.tmp");
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1032
    ok(check_file_data(path, data), "File doesn't match\n");
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    DeleteFileA(path);

    GetTempPathA(MAX_PATH, temp);
    GetTempFileNameA(temp, "wine", 0, path);

    /* build up an INI file */
    WritePrivateProfileStringA("App1", "key1", "string1", path);
    WritePrivateProfileStringA("App1", "key2", "string2", path);
    WritePrivateProfileStringA("App1", "key3", "string3", path);
    WritePrivateProfileStringA("App2", "key4", "string4", path);

    /* make an addition and verify the INI */
    data = "[App1]\r\n"
           "key1=string1\r\n"
           "key2=string2\r\n"
           "key3=string3\r\n"
           "[App2]\r\n"
           "key4=string4\r\n"
           "[App3]\r\n"
           "key5=string5\r\n";
    ret = WritePrivateProfileStringA("App3", "key5", "string5", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1055
    ok(check_file_data(path, data), "File doesn't match\n");
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

    /* lpString is NULL, key2 key is deleted */
    data = "[App1]\r\n"
           "key1=string1\r\n"
           "key3=string3\r\n"
           "[App2]\r\n"
           "key4=string4\r\n"
           "[App3]\r\n"
           "key5=string5\r\n";
    ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1067
    ok(check_file_data(path, data), "File doesn't match\n");
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

    /* try to delete key2 again */
    data = "[App1]\r\n"
           "key1=string1\r\n"
           "key3=string3\r\n"
           "[App2]\r\n"
           "key4=string4\r\n"
           "[App3]\r\n"
           "key5=string5\r\n";
    ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1079
    ok(check_file_data(path, data), "File doesn't match\n");
1080 1081 1082 1083 1084 1085 1086 1087

    /* lpKeyName is NULL, App1 section is deleted */
    data = "[App2]\r\n"
           "key4=string4\r\n"
           "[App3]\r\n"
           "key5=string5\r\n";
    ret = WritePrivateProfileStringA("App1", NULL, "string1", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1088
    ok(check_file_data(path, data), "File doesn't match\n");
1089 1090 1091 1092 1093 1094

    /* lpString is not needed to delete a section */
    data = "[App3]\r\n"
           "key5=string5\r\n";
    ret = WritePrivateProfileStringA("App2", NULL, NULL, path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1095
    ok(check_file_data(path, data), "File doesn't match\n");
1096 1097 1098 1099 1100

    /* leave just the section */
    data = "[App3]\r\n";
    ret = WritePrivateProfileStringA("App3", "key5", NULL, path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1101
    ok(check_file_data(path, data), "File doesn't match\n");
1102
    DeleteFileA(path);
1103

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    /* NULLs in file before first section. Should be preserved in output */
    data = "Data \0 before \0 first \0 section"    /* 31 bytes */
           "\r\n[section1]\r\n"                    /* 14 bytes */
           "key1=string1\r\n";                     /* 14 bytes */
    GetTempFileNameA(temp, "wine", 0, path);
    create_test_file(path, data, 31);
    ret = WritePrivateProfileStringA("section1", "key1", "string1", path);
    ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
    todo_wine
    ok( check_binary_file_data(path, data, 59) ||
        broken( check_binary_file_data(path,        /* Windows 9x */
            "Data \0 before \0 first \0 section"    /* 31 bytes */
            "\r\n\r\n[section1]\r\n"                /* 14 bytes */
            "key1=string1"                          /* 14 bytes */
            , 59)), "File doesn't match\n");
1119 1120 1121
    DeleteFileA(path);
}

1122 1123 1124
START_TEST(profile)
{
    test_profile_int();
1125
    test_profile_string();
1126
    test_profile_sections();
1127
    test_profile_sections_names();
1128
    test_profile_existing();
1129
    test_profile_delete_on_close();
1130
    test_profile_refresh();
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    test_GetPrivateProfileString(
        "[section1]\r\n"
        "name1=val1\r\n"
        "name2=\"val2\"\r\n"
        "name3\r\n"
        "name4=a\r\n"
        "[section2]\r\n",
        "CR+LF");
    test_GetPrivateProfileString(
        "[section1]\r"
        "name1=val1\r"
        "name2=\"val2\"\r"
        "name3\r"
        "name4=a\r"
        "[section2]\r",
        "CR only");
1147
    test_WritePrivateProfileString();
1148
}