setupapi: Fix for SetupGetIntField, with tests.

This commit is contained in:
Paul Vriens 2008-03-31 19:51:21 +02:00 committed by Alexandre Julliard
parent 14f86f8778
commit ebec0a9697
2 changed files with 75 additions and 8 deletions

View File

@ -1711,21 +1711,26 @@ BOOL WINAPI SetupGetIntField( PINFCONTEXT context, DWORD index, PINT result )
char *end, *buffer = localbuff;
DWORD required;
INT res;
BOOL ret = FALSE;
BOOL ret;
if (!SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required ))
if (!(ret = SetupGetStringFieldA( context, index, localbuff, sizeof(localbuff), &required )))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required ))) return FALSE;
if (!SetupGetStringFieldA( context, index, buffer, required, NULL )) goto done;
if (!(ret = SetupGetStringFieldA( context, index, buffer, required, NULL ))) goto done;
}
res = strtol( buffer, &end, 0 );
if (end != buffer && !*end)
/* The call to SetupGetStringFieldA succeeded. If buffer is empty we have an optional field */
if (!*buffer) *result = 0;
else
{
*result = res;
ret = TRUE;
res = strtol( buffer, &end, 0 );
if (end != buffer && !*end) *result = res;
else
{
SetLastError( ERROR_INVALID_DATA );
ret = FALSE;
}
}
else SetLastError( ERROR_INVALID_DATA );
done:
if (buffer != localbuff) HeapFree( GetProcessHeap(), 0, buffer );

View File

@ -475,6 +475,67 @@ static void test_pSetupGetField(void)
SetupCloseInfFile( hinf );
}
static void test_SetupGetIntField(void)
{
static const struct
{
const char *key;
const char *fields;
DWORD index;
INT value;
DWORD err;
} keys[] =
{
/* key fields index expected int errorcode */
{ "Key=", "48", 1, 48, ERROR_SUCCESS },
{ "Key=", "48", 0, -1, ERROR_INVALID_DATA },
{ "123=", "48", 0, 123, ERROR_SUCCESS },
{ "Key=", "0x4", 1, 4, ERROR_SUCCESS },
{ "Key=", "Field1", 1, -1, ERROR_INVALID_DATA },
{ "Key=", "Field1,34", 2, 34, ERROR_SUCCESS },
{ "Key=", "Field1,,Field3", 2, 0, ERROR_SUCCESS },
{ "Key=", "Field1,", 2, 0, ERROR_SUCCESS }
};
unsigned int i;
for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++)
{
HINF hinf;
char buffer[MAX_INF_STRING_LENGTH];
INFCONTEXT context;
UINT err;
BOOL retb;
INT intfield;
strcpy( buffer, STD_HEADER "[TestSection]\n" );
strcat( buffer, keys[i].key );
strcat( buffer, keys[i].fields );
hinf = test_file_contents( buffer, &err);
ok( hinf != NULL, "Expected valid INF file\n" );
SetupFindFirstLineA( hinf, "TestSection", "Key", &context );
SetLastError( 0xdeadbeef );
intfield = -1;
retb = SetupGetIntField( &context, keys[i].index, &intfield );
if ( keys[i].err == ERROR_SUCCESS )
{
ok( retb, "Expected success\n" );
ok( GetLastError() == ERROR_SUCCESS ||
GetLastError() == 0xdeadbeef /* win9x, NT4 */,
"Expected ERROR_SUCCESS or 0xdeadbeef, got %u\n", GetLastError() );
}
else
{
ok( !retb, "Expected failure\n" );
ok( GetLastError() == keys[i].err,
"Expected %d, got %u\n", keys[i].err, GetLastError() );
}
ok( intfield == keys[i].value, "Expected %d, got %d\n", keys[i].value, intfield );
SetupCloseInfFile( hinf );
}
}
static void test_GLE(void)
{
static const char *inf =
@ -597,6 +658,7 @@ START_TEST(parser)
test_key_names();
test_close_inf_file();
test_pSetupGetField();
test_SetupGetIntField();
test_GLE();
DeleteFileA( tmpfilename );
}