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