1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Test for CRegKey
5  * PROGRAMMERS:     Mark Jansen
6  *                  Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
7  */
8 
9 #include <apitest.h>
10 #include <atlbase.h>
11 
12 START_TEST(CRegKey)
13 {
14     CRegKey key;
15     CRegKey key2(HKEY_CURRENT_USER);
16 
17     ok(key.m_hKey == NULL, "Expected m_hKey to be initialized to 0, was: %p\n", key.m_hKey);
18     ok(key2.m_hKey == HKEY_CURRENT_USER, "Expected m_hKey to be initialized to HKEY_CURRENT_USER, was: %p\n", key2.m_hKey);
19     ok(key2 == HKEY_CURRENT_USER, "Expected operator HKEY() to be implemented\n");
20 
21     // Take ownership
22     CRegKey key3(key2);
23     ok(key3.m_hKey == HKEY_CURRENT_USER, "Expected m_hKey to be initialized to HKEY_CURRENT_USER, was: %p\n", key3.m_hKey);
24     ok(key2.m_hKey == NULL, "Expected m_hKey to be initialized to 0, was: %p\n", key2.m_hKey);
25 
26     LSTATUS lret;
27 
28     lret = key.Close();
29     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
30     lret = key2.Close();
31     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
32     lret = key3.Close();
33     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
34 
35     // read/write
36     lret = key.Open(HKEY_CURRENT_USER, _T("Environment"));
37     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
38     ok(key.m_hKey != NULL, "Expected m_hKey to not be NULL, was: %p\n", key.m_hKey);
39 
40 
41     HKEY tmp = key.m_hKey;
42     HKEY detached = key.Detach();
43     ok(key.m_hKey == NULL, "Expected m_hKey to be 0, was: %p\n", key.m_hKey);
44     ok(detached == tmp, "Expected detached to be %p, was: %p\n", tmp, detached);
45     key.Attach(detached);
46     ok(key.m_hKey == tmp, "Expected m_hKey to be %p, was: %p\n", tmp, key.m_hKey);
47 
48     lret = key2.Open(HKEY_CURRENT_USER, _T("Environment"), KEY_READ);
49     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
50 
51     lret = key3.Open(HKEY_CURRENT_USER, _T("Environment"), KEY_WRITE);
52     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
53 
54     TCHAR testdata[] = _T("XX-XX");
55     lret = key.SetValue(_T("APITEST_VALUE_NAME"), REG_SZ, testdata, sizeof(testdata));
56     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
57 
58     TCHAR buffer[100];
59     ULONG buffer_size = sizeof(buffer);
60     DWORD type = 0x12345;
61     memset(buffer, 0, sizeof(buffer));
62 
63     lret = key.QueryValue(_T("APITEST_VALUE_NAME"), &type, buffer, &buffer_size);
64     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
65     ok(type == REG_SZ, "Expected type to be REG_SZ, was: %lu\n", type);
66     ok(buffer_size == sizeof(testdata), "Expected buffer_size to be %u, was: %lu\n", sizeof(testdata), buffer_size);
67     ok(!memcmp(buffer, testdata, sizeof(testdata)), "Expected to get the same input as what was written!\n");
68 
69 
70     buffer_size = sizeof(buffer);
71     type = 0x12345;
72     memset(buffer, 0, sizeof(buffer));
73     lret = key2.QueryValue(_T("APITEST_VALUE_NAME"), &type, buffer, &buffer_size);
74     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
75     ok(type == REG_SZ, "Expected type to be REG_SZ, was: %lu\n", type);
76     ok(buffer_size == sizeof(testdata), "Expected buffer_size to be %u, was: %lu\n", sizeof(testdata), buffer_size);
77     ok(!memcmp(buffer, testdata, sizeof(testdata)), "Expected to get the same input as what was written!\n");
78 
79     buffer_size = sizeof(buffer);
80     type = 0x12345;
81     memset(buffer, 0, sizeof(buffer));
82     lret = key3.QueryValue(_T("APITEST_VALUE_NAME"), &type, buffer, &buffer_size);
83     ok(lret == ERROR_ACCESS_DENIED, "Expected lret to be ERROR_ACCESS_DENIED, was: %lu\n", lret);
84     ok(type == 0 || broken(type > 200), "Expected type to be 0, was: %lu\n", type);
85     ok(buffer_size == sizeof(buffer), "Expected buffer_size to be %u, was: %lu\n", sizeof(buffer), buffer_size);
86 
87 
88     lret = key2.SetValue(_T("APITEST_VALUE_NAME"), REG_SZ, testdata, sizeof(testdata));
89     ok(lret == ERROR_ACCESS_DENIED, "Expected lret to be ERROR_ACCESS_DENIED, was: %lu\n", lret);
90 
91     lret = key2.DeleteValue(_T("APITEST_VALUE_NAME"));
92     ok(lret == ERROR_ACCESS_DENIED, "Expected lret to be ERROR_ACCESS_DENIED, was: %lu\n", lret);
93 
94     DWORD dword = 0x54321;
95     lret = key2.QueryDWORDValue(_T("APITEST_VALUE_NAME"), dword);
96     ok(lret == ERROR_MORE_DATA, "Expected lret to be ERROR_MORE_DATA, was: %lu\n", lret);
97     ok(dword == 0x54321, "Expected dword to be 0x54321, was: %lu\n", dword);
98 
99     lret = key.SetValue(_T("APITEST_VALUE_NAME"), REG_SZ, testdata, sizeof(TCHAR));
100     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_ACCESS_DENIED, was: %lu\n", lret);
101 
102     dword = 0x54321;
103     lret = key2.QueryDWORDValue(_T("APITEST_VALUE_NAME"), dword);
104     ok(lret == ERROR_INVALID_DATA, "Expected lret to be ERROR_MORE_DATA, was: %lu\n", lret);
105     ok(dword != 0x54321, "Expected dword to NOT be 0x54321, was: %lu\n", dword);
106 
107     lret = key3.SetDWORDValue(_T("APITEST_VALUE_NAME"), 0x12345);
108     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
109 
110     dword = 0x54321;
111     lret = key2.QueryDWORDValue(_T("APITEST_VALUE_NAME"), dword);
112     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
113     ok(dword == 0x12345, "Expected dword to be 0x12345, was: %lu\n", dword);
114 
115 
116     lret = key3.DeleteValue(_T("APITEST_VALUE_NAME"));
117     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
118 
119 
120     lret = key.SetKeyValue(_T("APITEST_KEY_NAME"), _T("APITEST_VALUE"));
121     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
122 
123     CRegKey qv;
124 
125     // COUNTOF, not SIZEOF!!!!
126     lret = qv.Open(HKEY_CURRENT_USER, _T("Environment\\APITEST_KEY_NAME"));
127     buffer_size = _countof(buffer);
128     memset(buffer, 0, sizeof(buffer));
129     lret = qv.QueryStringValue(NULL, buffer, &buffer_size);
130     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
131     ok(buffer_size == _countof("APITEST_VALUE"), "Expected buffer_size to be %u, was: %lu\n", _countof("APITEST_VALUE"), buffer_size);
132     ok(!_tcscmp(buffer, _T("APITEST_VALUE")), "Expected to get the same input as what was written!\n");
133 
134     lret = key.SetKeyValue(_T("APITEST_KEY_NAME"), _T("APITEST_VALUE2"), _T("APITEST_VALUE_NAME"));
135     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
136 
137     buffer_size = _countof(buffer);
138     memset(buffer, 0, sizeof(buffer));
139     lret = qv.QueryStringValue(_T("APITEST_VALUE_NAME"), buffer, &buffer_size);
140     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
141     ok(buffer_size == _countof("APITEST_VALUE2"), "Expected buffer_size to be %u, was: %lu\n", _countof("APITEST_VALUE2"), buffer_size);
142     ok(!_tcscmp(buffer, _T("APITEST_VALUE2")), "Expected to get the same input as what was written!\n");
143 
144     lret = key.DeleteSubKey(_T("APITEST_KEY_NAME"));
145     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
146 
147     GUID guid, guid2;
148     memset(&guid, 56, sizeof(guid));
149 
150     lret = key.SetGUIDValue(_T("GUID_NAME"), guid);
151     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
152 
153     lret = key.QueryGUIDValue(_T("GUID_NAME"), guid2);
154     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
155     ok(!memcmp(&guid, &guid2, sizeof(guid)), "Expected guid to equal guid2\n");
156 
157     buffer_size = _countof(buffer);
158     memset(buffer, 0, sizeof(buffer));
159     lret = key2.QueryStringValue(_T("GUID_NAME"), buffer, &buffer_size);
160     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
161     ok(buffer_size == _countof("{38383838-3838-3838-3838-383838383838}"),
162         "Expected buffer_size to be %u, was: %lu\n", _countof("{38383838-3838-3838-3838-383838383838}"), buffer_size);
163     ok(!_tcscmp(buffer, _T("{38383838-3838-3838-3838-383838383838}")), "Expected to get the same input as what was written!\n");
164 
165     memset(&guid, 33, 5);
166     lret = key.SetBinaryValue(_T("BIN_NAME"), &guid, 5);
167     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
168 
169     buffer_size = sizeof(buffer);
170     memset(buffer, 0, sizeof(buffer));
171     lret = key.QueryBinaryValue(_T("GUID_NAME"), buffer, &buffer_size);
172     ok(lret == ERROR_INVALID_DATA, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
173     ok(buffer_size == sizeof(_T("{38383838-3838-3838-3838-383838383838}")),
174         "Expected buffer_size to be %u, was: %lu\n", sizeof(_T("{38383838-3838-3838-3838-383838383838}")), buffer_size);
175     ok(buffer[0] == '{', "Expected buffer[0] to be 123, was: %i\n", (int)buffer[0]);
176 
177     buffer_size = sizeof(buffer);
178     memset(buffer, 0, sizeof(buffer));
179     lret = key.QueryBinaryValue(_T("BIN_NAME"), buffer, &buffer_size);
180     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
181     ok(buffer_size == 5, "Expected buffer_size to be %i, was: %lu\n", 5, buffer_size);
182     ok(!memcmp(buffer, &guid, 5), "Expected the first 5 bytes of buffer to equal the data in null_guid\n");
183 
184     lret = key.DeleteValue(_T("GUID_NAME"));
185     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
186 
187     lret = key.DeleteValue(_T("BIN_NAME"));
188     ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
189 
190     {
191         CRegKey test1;
192         lret = test1.Create(HKEY_CURRENT_USER, _T("TEST1"));
193         ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
194 
195         CRegKey test2;
196         lret = test2.Create(test1, _T("TEST2"));
197         ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
198 
199         CRegKey test3;
200         lret = test3.Create(test2, _T("TEST3"));
201         ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
202     }
203     {
204         CRegKey key(HKEY_CURRENT_USER);
205         lret = key.RecurseDeleteKey(_T("TEST1"));
206         ok(lret == ERROR_SUCCESS, "Expected lret to be ERROR_SUCCESS, was: %lu\n", lret);
207     }
208 }
209