-- v9: advapi32/tests: Add tests for RegLoadAppKey.
From: Santino Mazza smazza@codeweavers.com
Signed-off-by: Santino Mazza smazza@codeweavers.com --- dlls/advapi32/tests/registry.c | 80 +++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-)
diff --git a/dlls/advapi32/tests/registry.c b/dlls/advapi32/tests/registry.c index df56b0968ee..5f060b0a5be 100644 --- a/dlls/advapi32/tests/registry.c +++ b/dlls/advapi32/tests/registry.c @@ -1600,7 +1600,83 @@ static void test_reg_unload_key(void) DeleteFileA("saved_key.LOG"); }
-/* tests that show that RegConnectRegistry and +/* Helper function to wait for a file blocked by the registry to be available */ +static void wait_file_available(char *path) +{ + int attempts = 0; + HANDLE file = NULL; + + while (((file = CreateFileA(path, GENERIC_READ, 0, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) + && attempts++ <= 10) + { + Sleep(200); + } + ok(attempts <= 10, "couldn't open file %s after 10 attempts error %ld.\n", path, GetLastError()); + CloseHandle(file); +} + +static void test_reg_load_app_key(void) +{ + DWORD ret, size; + char temppath[MAX_PATH], hivefilepath[MAX_PATH]; + const BYTE test_data[] = "Hello World"; + BYTE output[sizeof(test_data)]; + HKEY appkey = NULL; + + GetTempPathA(sizeof(temppath), temppath); + GetTempFileNameA(temppath, "key", 0, hivefilepath); + DeleteFileA(hivefilepath); + + if (!set_privileges(SE_BACKUP_NAME, TRUE) || + !set_privileges(SE_RESTORE_NAME, FALSE)) + { + win_skip("Failed to set SE_BACKUP_NAME privileges, skipping tests\n"); + return; + } + + ret = RegSaveKeyA(hkey_main, hivefilepath, NULL); + if (ret != ERROR_SUCCESS) + { + win_skip("Failed to save test key 0x%lx\n", ret); + return; + } + + set_privileges(SE_BACKUP_NAME, FALSE); + set_privileges(SE_RESTORE_NAME, FALSE); + + /* Test simple key load */ + /* Check if the changes are saved */ + ret = RegLoadAppKeyA(hivefilepath, &appkey, KEY_READ | KEY_WRITE, 0, 0); + ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret); + ok(appkey != NULL, "got a null key\n"); + + ret = RegSetValueExA(appkey, "testkey", 0, REG_BINARY, test_data, sizeof(test_data)); + todo_wine ok(ret == ERROR_SUCCESS, "couldn't set key value %lx\n", ret); + RegCloseKey(appkey); + + wait_file_available(hivefilepath); + + appkey = NULL; + ret = RegLoadAppKeyA(hivefilepath, &appkey, KEY_READ, 0, 0); + ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret); + ok(appkey != NULL, "got a null key\n"); + + size = sizeof(test_data); + memset(output, 0xff, sizeof(output)); + ret = RegGetValueA(appkey, NULL, "testkey", RRF_RT_REG_BINARY, NULL, output, &size); + todo_wine ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret); + ok(size == sizeof(test_data), "size doesn't match %ld != %ld\n", size, (DWORD)sizeof(test_data)); + for (int i = 0; i < sizeof(test_data); ++i) + todo_wine ok(test_data[i] == output[i], "output is not what expected i=%d %d != %d\n", i, test_data[i], output[i]); + + RegCloseKey(appkey); + + wait_file_available(hivefilepath); + DeleteFileA(hivefilepath); +} + +/* tests that show that RegConnectRegistry and OpenSCManager accept computer names without the \ prefix (what MSDN says). */ static void test_regconnectregistry( void) @@ -4486,6 +4562,7 @@ START_TEST(registry) test_reg_save_key(); test_reg_load_key(); test_reg_unload_key(); + test_reg_load_app_key(); test_reg_copy_tree(); test_reg_delete_tree(); test_rw_order(); @@ -4502,6 +4579,5 @@ START_TEST(registry)
/* cleanup */ delete_key( hkey_main ); - test_regconnectregistry(); }
Huw Davies (@huw) commented about dlls/advapi32/tests/registry.c:
- RegCloseKey(appkey);
- wait_file_available(hivefilepath);
- appkey = NULL;
- ret = RegLoadAppKeyA(hivefilepath, &appkey, KEY_READ, 0, 0);
- ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
- ok(appkey != NULL, "got a null key\n");
- size = sizeof(test_data);
- memset(output, 0xff, sizeof(output));
- ret = RegGetValueA(appkey, NULL, "testkey", RRF_RT_REG_BINARY, NULL, output, &size);
- todo_wine ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
- ok(size == sizeof(test_data), "size doesn't match %ld != %ld\n", size, (DWORD)sizeof(test_data));
- for (int i = 0; i < sizeof(test_data); ++i)
todo_wine ok(test_data[i] == output[i], "output is not what expected i=%d %d != %d\n", i, test_data[i], output[i]);
Using `memcmp()` here would be simpler.
Huw Davies (@huw) commented about dlls/advapi32/tests/registry.c:
- ret = RegLoadAppKeyA(hivefilepath, &appkey, KEY_READ, 0, 0);
- ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
- ok(appkey != NULL, "got a null key\n");
- size = sizeof(test_data);
- memset(output, 0xff, sizeof(output));
- ret = RegGetValueA(appkey, NULL, "testkey", RRF_RT_REG_BINARY, NULL, output, &size);
- todo_wine ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
- ok(size == sizeof(test_data), "size doesn't match %ld != %ld\n", size, (DWORD)sizeof(test_data));
- for (int i = 0; i < sizeof(test_data); ++i)
todo_wine ok(test_data[i] == output[i], "output is not what expected i=%d %d != %d\n", i, test_data[i], output[i]);
- RegCloseKey(appkey);
- wait_file_available(hivefilepath);
- DeleteFileA(hivefilepath);
Testing the return value of `DeleteFileA()` would confirm we haven't left behind the file.
Huw Davies (@huw) commented about dlls/advapi32/tests/registry.c:
/* cleanup */ delete_key( hkey_main );
Please remove this whitespace-only change.
On Thu Sep 8 08:53:27 2022 +0000, Huw Davies wrote:
Using `memcmp()` here would be simpler.
I've made it like this because it makes it easier when debugging, I saw this kind of checks in other places like bcrypt. But now that I see it its true, I'm testing RegLoadAppKey and not RegGetValue, so it shouldn't be problem to use memcmp.