1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for the RegGetvalueW API
5  * PROGRAMMER:      Jérôme Gardou <jerome.gardou@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
10 START_TEST(RegEnumValueW)
11 {
12     LONG ErrorCode;
13     HKEY TestKey;
14     ULONG Data;
15     DWORD NameLength, DataLength;
16     DWORD DataType;
17     WCHAR NameBuffer[7];
18 
19     /* Create our Test Key */
20     ErrorCode = RegCreateKeyW( HKEY_CURRENT_USER, L"Software\\ReactOS_apitest", &TestKey );
21     ok_dec(ErrorCode, ERROR_SUCCESS);
22 
23     /* All NULL is invalid */
24     ErrorCode = RegEnumValueW(TestKey, 0, NULL, NULL, NULL, NULL, NULL, NULL);
25     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
26 
27     /* Asking for the buffer length is not enough. */
28     NameLength = 8;
29     ErrorCode = RegEnumValueW(TestKey, 0, NULL, &NameLength, NULL, NULL, NULL, NULL);
30     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
31 
32     /* Or maybe it is, you just have to nicely ask */
33     NameLength = 0;
34     ErrorCode = RegEnumValueW(TestKey, 0, NULL, &NameLength, NULL, NULL, NULL, NULL);
35     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
36 
37     /* Name buffer alone is also prohibited */
38     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, NULL, NULL, NULL, NULL, NULL);
39     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
40 
41     /* You need to ask for both a minima */
42     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, NULL, NULL, NULL);
43     ok_dec(ErrorCode, ERROR_NO_MORE_ITEMS);
44 
45     /* What if I only want to know the value type ? */
46     ErrorCode = RegEnumValueW(TestKey, 0, NULL, NULL, &DataType, NULL, NULL, NULL);
47     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
48 
49     /* Set a value */
50     Data = 0xF00DF00D;
51     ErrorCode = RegSetValueExW(TestKey, L"Value1", 0, REG_BINARY, (LPBYTE)&Data, sizeof(Data));
52     ok_dec(ErrorCode, ERROR_SUCCESS);
53 
54     /* Try various combinations of Arguments */
55     NameLength = 7;
56     ErrorCode = RegEnumValueW(TestKey, 0, NULL, &NameLength, NULL, NULL, NULL, NULL);
57     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
58     /* Provide a buffer this time */
59     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, NULL, NULL, NULL);
60     ok_dec(ErrorCode, ERROR_SUCCESS);
61     /* This doesn't include the NULL terminator */
62     ok_dec(NameLength, 6);
63     /* But the string is NULL terminated */
64     ok_hex(NameBuffer[6], L'\0');
65     ok(wcscmp(NameBuffer, L"Value1") == 0, "%S\n", NameBuffer);
66 
67     /* See if skipping the NULL value is a problem */
68     NameLength = 6;
69     memset(NameBuffer, 0xBA, sizeof(NameBuffer));
70     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, NULL, NULL, NULL);
71     ok_dec(ErrorCode, ERROR_MORE_DATA);
72     /* In fact, this is unchanged */
73     ok_dec(NameLength, 6);
74     /* And the string is untouched */
75     ok_hex(NameBuffer[6], 0xBABA);
76     ok_hex(NameBuffer[5], 0xBABA);
77     ok_hex(NameBuffer[4], 0xBABA);
78     ok_hex(NameBuffer[3], 0xBABA);
79     ok_hex(NameBuffer[2], 0xBABA);
80     ok_hex(NameBuffer[1], 0xBABA);
81     ok_hex(NameBuffer[0], 0xBABA);
82 
83     /* Of course, anything smaller is an outrage */
84     NameLength = 5;
85     memset(NameBuffer, 0xBA, sizeof(NameBuffer));
86     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, NULL, NULL, NULL);
87     ok_dec(ErrorCode, ERROR_MORE_DATA);
88     /* Untouched... */
89     ok_dec(NameLength, 5);
90     /* And the string is untouched */
91     ok_hex(NameBuffer[6], 0xBABA);
92     ok_hex(NameBuffer[5], 0xBABA);
93     ok_hex(NameBuffer[4], 0xBABA);
94     ok_hex(NameBuffer[3], 0xBABA);
95     ok_hex(NameBuffer[2], 0xBABA);
96     ok_hex(NameBuffer[1], 0xBABA);
97     ok_hex(NameBuffer[0], 0xBABA);
98 
99     /* Of course, asking for data without caring for the name would be strange... */
100     DataLength = 0;
101     ErrorCode = RegEnumValueW(TestKey, 0, NULL, NULL, NULL, NULL, NULL, &DataLength);
102     ok_dec(ErrorCode, ERROR_INVALID_PARAMETER);
103 
104     DataLength = 0;
105     NameLength = 7;
106     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, &DataType, NULL, &DataLength);
107     ok_dec(ErrorCode, ERROR_SUCCESS);
108     ok_dec(DataLength, sizeof(Data));
109     ok_hex(DataType, REG_BINARY);
110 
111     /* Same, but this time with NULL data buffer and non-zero data size */
112     DataLength = 2;
113     NameLength = 7;
114     DataType = 0x01234567;
115     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, &DataType, NULL, &DataLength);
116     ok_dec(ErrorCode, ERROR_SUCCESS);
117     ok_dec(DataLength, sizeof(Data));
118     ok_hex(DataType, REG_BINARY);
119 
120     /* Same, but this time with data buffer and shrunk data size */
121     DataLength = 2;
122     Data = 0;
123     DataType = 0x01234567;
124     NameLength = 7;
125     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, &DataType, (LPBYTE)&Data, &DataLength);
126     ok_dec(ErrorCode, ERROR_MORE_DATA);
127     ok_dec(DataLength, sizeof(Data));
128     ok_hex(Data, 0);
129     ok_hex(DataType, REG_BINARY);
130 
131     /* Put the right parameters */
132     DataLength = sizeof(Data);
133     NameLength = 7;
134     Data = 0;
135     DataType = 0x01234567;
136     ErrorCode = RegEnumValueW(TestKey, 0, NameBuffer, &NameLength, NULL, &DataType, (LPBYTE)&Data, &DataLength);
137     ok_dec(ErrorCode, ERROR_SUCCESS);
138     ok_dec(DataLength, sizeof(Data));
139     ok_hex(Data, 0xF00DF00D);
140     ok_hex(DataType, REG_BINARY);
141 
142     /* Delete the key */
143     ErrorCode = RegDeleteKeyW(TestKey, L"");
144     ok_dec(ErrorCode, ERROR_SUCCESS);
145     RegCloseKey(TestKey);
146 }
147 
148