From c5246d68f54b6392ef067c46cc8a334cbc23a479 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Mon, 17 Jun 2019 20:33:31 +0900 Subject: [PATCH] [KERNEL32_APITEST] Improve SetComputerNameExW testcase (#1661) Improve SetComputerNameExW testcase to check invalid characters. CORE-16122 --- .../apitests/kernel32/SetComputerNameExW.c | 201 +++++++++++++++--- 1 file changed, 173 insertions(+), 28 deletions(-) diff --git a/modules/rostests/apitests/kernel32/SetComputerNameExW.c b/modules/rostests/apitests/kernel32/SetComputerNameExW.c index 89ba26460a5..1b631da8c2f 100644 --- a/modules/rostests/apitests/kernel32/SetComputerNameExW.c +++ b/modules/rostests/apitests/kernel32/SetComputerNameExW.c @@ -37,31 +37,16 @@ static HKEY OpenComputerNameKey(void) return NULL; } -START_TEST(SetComputerNameExW) +static void DoTestComputerName(HKEY hKeyHN, HKEY hKeyCN, LPCWSTR pszNewName, BOOL bValid) { - static const WCHAR szNewName[] = L"SRVROSTEST"; LONG Error; BOOL ret; - HKEY hKeyHN, hKeyCN; DWORD cbData; WCHAR szNVHostNameOld[MAX_PATH], szNVHostNameNew[MAX_PATH]; WCHAR szHostNameOld[MAX_PATH], szHostNameNew[MAX_PATH]; WCHAR szComputerNameOld[MAX_PATH], szComputerNameNew[MAX_PATH]; - /* Open keys */ - hKeyHN = OpenHostNameKey(); - ok(hKeyHN != NULL, "hKeyHN is NULL\n"); - hKeyCN = OpenComputerNameKey(); - ok(hKeyCN != NULL, "hKeyCN is NULL\n"); - if (!hKeyHN || !hKeyCN) - { - if (hKeyHN) - RegCloseKey(hKeyHN); - if (hKeyCN) - RegCloseKey(hKeyCN); - skip("Unable to open keys. Missing Admin rights?\n"); - return; - } + trace("Testing '%S':\n", pszNewName); /* Get Old NV Hostname */ szNVHostNameOld[0] = UNICODE_NULL; @@ -85,35 +70,62 @@ START_TEST(SetComputerNameExW) ok(szComputerNameOld[0], "szComputerNameOld is empty\n"); /* Change the value */ - ret = SetComputerNameExW(ComputerNamePhysicalDnsHostname, szNewName); - ok_int(ret, TRUE); + SetLastError(0xDEADFACE); + ret = SetComputerNameExW(ComputerNamePhysicalDnsHostname, pszNewName); + ok_int(ret, bValid); + Error = GetLastError(); + if (bValid) + ok_long(Error, ERROR_SUCCESS); + else + ok_long(Error, ERROR_INVALID_PARAMETER); /* Get New NV Hostname */ szNVHostNameNew[0] = UNICODE_NULL; cbData = sizeof(szNVHostNameNew); Error = RegQueryValueExW(hKeyHN, L"NV Hostname", NULL, NULL, (LPBYTE)szNVHostNameNew, &cbData); ok_long(Error, ERROR_SUCCESS); - ok(szNVHostNameNew[0], "szNVHostNameNew is empty\n"); - ok(lstrcmpW(szNVHostNameNew, szNewName) == 0, - "szNVHostNameNew '%S' should be szNewName '%S'\n", szNVHostNameNew, szNewName); + if (bValid) + { + ok(szNVHostNameNew[0], "szNVHostNameNew is empty\n"); + ok(lstrcmpW(szNVHostNameNew, pszNewName) == 0, + "szNVHostNameNew '%S' should be pszNewName '%S'\n", szNVHostNameNew, pszNewName); + } /* Get New Hostname */ szHostNameNew[0] = UNICODE_NULL; cbData = sizeof(szHostNameNew); Error = RegQueryValueExW(hKeyHN, L"Hostname", NULL, NULL, (LPBYTE)szHostNameNew, &cbData); ok_long(Error, ERROR_SUCCESS); - ok(szHostNameNew[0], "szHostNameNew is empty\n"); - ok(lstrcmpW(szHostNameNew, szHostNameOld) == 0, - "szHostNameNew '%S' should be szHostNameOld '%S'\n", szHostNameNew, szHostNameOld); + if (bValid) + { + ok(szHostNameNew[0], "szHostNameNew is empty\n"); + ok(lstrcmpW(szHostNameNew, szHostNameOld) == 0, + "szHostNameNew '%S' should be szHostNameOld '%S'\n", szHostNameNew, szHostNameOld); + } /* Get New Computer Name */ szComputerNameNew[0] = UNICODE_NULL; cbData = sizeof(szComputerNameNew); Error = RegQueryValueExW(hKeyCN, L"ComputerName", NULL, NULL, (LPBYTE)szComputerNameNew, &cbData); ok_long(Error, ERROR_SUCCESS); - ok(szComputerNameNew[0], "szComputerNameNew is empty\n"); - ok(lstrcmpW(szComputerNameNew, szNewName) == 0, - "szComputerNameNew '%S' should be szNewName '%S'\n", szComputerNameNew, szNewName); + if (bValid) + { + ok(szComputerNameNew[0], "szComputerNameNew is empty\n"); + if (lstrlenW(pszNewName) > MAX_COMPUTERNAME_LENGTH) + { + WCHAR szTruncatedNewName[MAX_COMPUTERNAME_LENGTH + 1]; + lstrcpynW(szTruncatedNewName, pszNewName, ARRAYSIZE(szTruncatedNewName)); + ok(lstrcmpiW(szComputerNameNew, szTruncatedNewName) == 0, + "szComputerNameNew '%S' should be szTruncatedNewName '%S'\n", + szComputerNameNew, szTruncatedNewName); + } + else + { + ok(lstrcmpiW(szComputerNameNew, pszNewName) == 0, + "szComputerNameNew '%S' should be pszNewName '%S'\n", + szComputerNameNew, pszNewName); + } + } /* Restore the registry values */ cbData = (lstrlenW(szNVHostNameOld) + 1) * sizeof(WCHAR); @@ -127,6 +139,139 @@ START_TEST(SetComputerNameExW) cbData = (lstrlenW(szComputerNameOld) + 1) * sizeof(WCHAR); Error = RegSetValueExW(hKeyCN, L"ComputerName", 0, REG_SZ, (LPBYTE)szComputerNameOld, cbData); ok_long(Error, ERROR_SUCCESS); +} + +START_TEST(SetComputerNameExW) +{ + HKEY hKeyHN, hKeyCN; + static const WCHAR ValidSymbols[] = L"-_"; + static const WCHAR InvalidSymbols[] = L"\"/\\[]:|<>+=;,?"; + WCHAR szName[32]; + INT i, cchValidSymbols, cchInvalidSymbols; + LONG Error; + + /* Open keys */ + hKeyHN = OpenHostNameKey(); + ok(hKeyHN != NULL, "hKeyHN is NULL\n"); + hKeyCN = OpenComputerNameKey(); + ok(hKeyCN != NULL, "hKeyCN is NULL\n"); + if (!hKeyHN || !hKeyCN) + { + if (hKeyHN) + RegCloseKey(hKeyHN); + if (hKeyCN) + RegCloseKey(hKeyCN); + skip("Unable to open keys. Missing Admin rights?\n"); + return; + } + + cchValidSymbols = lstrlenW(ValidSymbols); + cchInvalidSymbols = lstrlenW(InvalidSymbols); + + /* Test names */ + + DoTestComputerName(hKeyHN, hKeyCN, L"SRVROSTEST", TRUE); + DoTestComputerName(hKeyHN, hKeyCN, L"SrvRosTest", TRUE); + + DoTestComputerName(hKeyHN, hKeyCN, L"", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"a", TRUE); + DoTestComputerName(hKeyHN, hKeyCN, L"A", TRUE); + DoTestComputerName(hKeyHN, hKeyCN, L"1", FALSE); // numeric-only + DoTestComputerName(hKeyHN, hKeyCN, L"@", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L".", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L" ", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\t", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\b", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L" a", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L" A", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L" 1", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\ta", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\tA", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\t1", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\ba", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\bA", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"\b1", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"a ", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A ", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1 ", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"a\t", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A\t", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1\t", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"a\b", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A\b", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1\b", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"a c", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A C", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1 c", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"a\tc", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A\tC", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1\tc", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"a\bc", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"A\bC", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"1\bc", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, ValidSymbols, TRUE); + DoTestComputerName(hKeyHN, hKeyCN, InvalidSymbols, FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"123456", FALSE); // numeric-only + DoTestComputerName(hKeyHN, hKeyCN, L"123.456", FALSE); + DoTestComputerName(hKeyHN, hKeyCN, L"123X456", TRUE); + DoTestComputerName(hKeyHN, hKeyCN, L"123X.456", FALSE); + + DoTestComputerName(hKeyHN, hKeyCN, L"ThisIsLongLongComputerName", TRUE); + + for (i = 0; i < cchValidSymbols; ++i) + { + szName[0] = ValidSymbols[i]; + szName[1] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE); + } + + for (i = 0; i < cchValidSymbols; ++i) + { + szName[0] = L'a'; + szName[1] = ValidSymbols[i]; + szName[2] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE); + } + + for (i = 0; i < cchValidSymbols; ++i) + { + szName[0] = L'A'; + szName[1] = ValidSymbols[i]; + szName[2] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE); + } + + for (i = 0; i < cchValidSymbols; ++i) + { + szName[0] = L'1'; + szName[1] = ValidSymbols[i]; + szName[2] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, TRUE); + } + + for (i = 0; i < cchInvalidSymbols; ++i) + { + szName[0] = L'A'; + szName[1] = InvalidSymbols[i]; + szName[2] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, FALSE); + } + + for (i = 0; i < cchInvalidSymbols; ++i) + { + szName[0] = L'1'; + szName[1] = InvalidSymbols[i]; + szName[2] = UNICODE_NULL; + DoTestComputerName(hKeyHN, hKeyCN, szName, FALSE); + } /* Close keys */ Error = RegCloseKey(hKeyHN);