crypt32: Free the encoded msg (Coverity).
[wine] / dlls / atl100 / tests / atl.c
1 /*
2  * Copyright 2012 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <stdio.h>
21
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <atlbase.h>
26
27 #include <wine/test.h>
28
29 static const GUID CLSID_Test =
30     {0x178fc163,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
31 #define CLSID_TEST_STR "178fc163-0000-0000-0000-000000000046"
32
33 static const GUID CATID_CatTest1 =
34     {0x178fc163,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x46}};
35 #define CATID_CATTEST1_STR "178fc163-0000-0000-0000-000000000146"
36
37 static const GUID CATID_CatTest2 =
38     {0x178fc163,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x46}};
39 #define CATID_CATTEST2_STR "178fc163-0000-0000-0000-000000000246"
40
41 static void test_winmodule(void)
42 {
43     _AtlCreateWndData create_data[3];
44     _ATL_WIN_MODULE winmod;
45     void *p;
46     HRESULT hres;
47
48     winmod.cbSize = 0xdeadbeef;
49     hres = AtlWinModuleInit(&winmod);
50     ok(hres == E_INVALIDARG, "AtlWinModuleInit failed: %08x\n", hres);
51
52     winmod.cbSize = sizeof(winmod);
53     winmod.m_pCreateWndList = (void*)0xdeadbeef;
54     winmod.m_csWindowCreate.LockCount = 0xdeadbeef;
55     winmod.m_rgWindowClassAtoms.m_aT = (void*)0xdeadbeef;
56     winmod.m_rgWindowClassAtoms.m_nSize = 0xdeadbeef;
57     winmod.m_rgWindowClassAtoms.m_nAllocSize = 0xdeadbeef;
58     hres = AtlWinModuleInit(&winmod);
59     ok(hres == S_OK, "AtlWinModuleInit failed: %08x\n", hres);
60     ok(!winmod.m_pCreateWndList, "winmod.m_pCreateWndList = %p\n", winmod.m_pCreateWndList);
61     ok(winmod.m_csWindowCreate.LockCount == -1, "winmod.m_csWindowCreate.LockCount = %d\n",
62        winmod.m_csWindowCreate.LockCount);
63     ok(winmod.m_rgWindowClassAtoms.m_aT == (void*)0xdeadbeef, "winmod.m_rgWindowClassAtoms.m_aT = %p\n",
64        winmod.m_rgWindowClassAtoms.m_aT);
65     ok(winmod.m_rgWindowClassAtoms.m_nSize == 0xdeadbeef, "winmod.m_rgWindowClassAtoms.m_nSize = %d\n",
66        winmod.m_rgWindowClassAtoms.m_nSize);
67     ok(winmod.m_rgWindowClassAtoms.m_nAllocSize == 0xdeadbeef, "winmod.m_rgWindowClassAtoms.m_nAllocSize = %d\n",
68        winmod.m_rgWindowClassAtoms.m_nAllocSize);
69
70     InitializeCriticalSection(&winmod.m_csWindowCreate);
71
72     AtlWinModuleAddCreateWndData(&winmod, create_data, (void*)0xdead0001);
73     ok(winmod.m_pCreateWndList == create_data, "winmod.m_pCreateWndList != create_data\n");
74     ok(create_data[0].m_pThis == (void*)0xdead0001, "unexpected create_data[0].m_pThis %p\n", create_data[0].m_pThis);
75     ok(create_data[0].m_dwThreadID == GetCurrentThreadId(), "unexpected create_data[0].m_dwThreadID %x\n",
76        create_data[0].m_dwThreadID);
77     ok(!create_data[0].m_pNext, "unexpected create_data[0].m_pNext %p\n", create_data[0].m_pNext);
78
79     AtlWinModuleAddCreateWndData(&winmod, create_data+1, (void*)0xdead0002);
80     ok(winmod.m_pCreateWndList == create_data+1, "winmod.m_pCreateWndList != create_data\n");
81     ok(create_data[1].m_pThis == (void*)0xdead0002, "unexpected create_data[1].m_pThis %p\n", create_data[1].m_pThis);
82     ok(create_data[1].m_dwThreadID == GetCurrentThreadId(), "unexpected create_data[1].m_dwThreadID %x\n",
83        create_data[1].m_dwThreadID);
84     ok(create_data[1].m_pNext == create_data, "unexpected create_data[1].m_pNext %p\n", create_data[1].m_pNext);
85
86     AtlWinModuleAddCreateWndData(&winmod, create_data+2, (void*)0xdead0003);
87     ok(winmod.m_pCreateWndList == create_data+2, "winmod.m_pCreateWndList != create_data\n");
88     ok(create_data[2].m_pThis == (void*)0xdead0003, "unexpected create_data[2].m_pThis %p\n", create_data[2].m_pThis);
89     ok(create_data[2].m_dwThreadID == GetCurrentThreadId(), "unexpected create_data[2].m_dwThreadID %x\n",
90        create_data[2].m_dwThreadID);
91     ok(create_data[2].m_pNext == create_data+1, "unexpected create_data[2].m_pNext %p\n", create_data[2].m_pNext);
92
93     p = AtlWinModuleExtractCreateWndData(&winmod);
94     ok(p == (void*)0xdead0003, "unexpected AtlWinModuleExtractCreateWndData result %p\n", p);
95     ok(winmod.m_pCreateWndList == create_data+1, "winmod.m_pCreateWndList != create_data\n");
96     ok(create_data[2].m_pNext == create_data+1, "unexpected create_data[2].m_pNext %p\n", create_data[2].m_pNext);
97
98     create_data[1].m_dwThreadID = 0xdeadbeef;
99
100     p = AtlWinModuleExtractCreateWndData(&winmod);
101     ok(p == (void*)0xdead0001, "unexpected AtlWinModuleExtractCreateWndData result %p\n", p);
102     ok(winmod.m_pCreateWndList == create_data+1, "winmod.m_pCreateWndList != create_data\n");
103     ok(!create_data[0].m_pNext, "unexpected create_data[0].m_pNext %p\n", create_data[0].m_pNext);
104     ok(!create_data[1].m_pNext, "unexpected create_data[1].m_pNext %p\n", create_data[1].m_pNext);
105
106     p = AtlWinModuleExtractCreateWndData(&winmod);
107     ok(!p, "unexpected AtlWinModuleExtractCreateWndData result %p\n", p);
108     ok(winmod.m_pCreateWndList == create_data+1, "winmod.m_pCreateWndList != create_data\n");
109 }
110
111 #define test_key_exists(a,b) _test_key_exists(__LINE__,a,b)
112 static void _test_key_exists(unsigned line, HKEY root, const char *key_name)
113 {
114     HKEY key;
115     DWORD res;
116
117     res = RegOpenKeyA(root, key_name, &key);
118     ok_(__FILE__,line)(res == ERROR_SUCCESS, "Could not open key %s\n", key_name);
119     if(res == ERROR_SUCCESS)
120         RegCloseKey(key);
121 }
122
123 #define test_key_not_exists(a,b) _test_key_not_exists(__LINE__,a,b)
124 static void _test_key_not_exists(unsigned line, HKEY root, const char *key_name)
125 {
126     HKEY key;
127     DWORD res;
128
129     res = RegOpenKeyA(root, key_name, &key);
130     ok_(__FILE__,line)(res == ERROR_FILE_NOT_FOUND, "Attempting to open %s returned %u\n", key_name, res);
131     if(res == ERROR_SUCCESS)
132         RegCloseKey(key);
133 }
134
135 static void test_regcat(void)
136 {
137     HRESULT hres;
138
139     const struct _ATL_CATMAP_ENTRY catmap[] = {
140         {_ATL_CATMAP_ENTRY_IMPLEMENTED, &CATID_CatTest1},
141         {_ATL_CATMAP_ENTRY_REQUIRED, &CATID_CatTest2},
142         {_ATL_CATMAP_ENTRY_END}
143     };
144
145     hres = AtlRegisterClassCategoriesHelper(&CLSID_Test, catmap, TRUE);
146     ok(hres == S_OK, "AtlRegisterClassCategoriesHelper failed: %08x\n", hres);
147
148     test_key_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}");
149     test_key_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}\\Implemented Categories\\{" CATID_CATTEST1_STR "}");
150     test_key_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}\\Required Categories\\{" CATID_CATTEST2_STR "}");
151
152     hres = AtlRegisterClassCategoriesHelper(&CLSID_Test, catmap, FALSE);
153     ok(hres == S_OK, "AtlRegisterClassCategoriesHelper failed: %08x\n", hres);
154
155     test_key_not_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}\\Implemented Categories");
156     test_key_not_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}\\Required Categories");
157     test_key_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}");
158
159     ok(RegDeleteKeyA(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}") == ERROR_SUCCESS, "Could not delete key\n");
160
161     hres = AtlRegisterClassCategoriesHelper(&CLSID_Test, NULL, TRUE);
162     ok(hres == S_OK, "AtlRegisterClassCategoriesHelper failed: %08x\n", hres);
163
164     test_key_not_exists(HKEY_CLASSES_ROOT, "CLSID\\{" CLSID_TEST_STR "}");
165 }
166
167 static void test_typelib(void)
168 {
169     ITypeLib *typelib;
170     HINSTANCE inst;
171     size_t len;
172     BSTR path;
173     HRESULT hres;
174
175     static const WCHAR scrrun_dll_suffixW[] = {'\\','s','c','r','r','u','n','.','d','l','l',0};
176     static const WCHAR mshtml_tlb_suffixW[] = {'\\','m','s','h','t','m','l','.','t','l','b',0};
177
178     inst = LoadLibraryA("scrrun.dll");
179     ok(inst != NULL, "Could not load scrrun.dll\n");
180
181     typelib = NULL;
182     hres = AtlLoadTypeLib(inst, NULL, &path, &typelib);
183     ok(hres == S_OK, "AtlLoadTypeLib failed: %08x\n", hres);
184     FreeLibrary(inst);
185
186     len = SysStringLen(path);
187     ok(len > sizeof(scrrun_dll_suffixW)/sizeof(WCHAR)
188        && lstrcmpiW(path+len-sizeof(scrrun_dll_suffixW)/sizeof(WCHAR), scrrun_dll_suffixW),
189        "unexpected path %s\n", wine_dbgstr_w(path));
190     SysFreeString(path);
191     ok(typelib != NULL, "typelib == NULL\n");
192     ITypeLib_Release(typelib);
193
194     inst = LoadLibraryA("mshtml.dll");
195     ok(inst != NULL, "Could not load mshtml.dll\n");
196
197     typelib = NULL;
198     hres = AtlLoadTypeLib(inst, NULL, &path, &typelib);
199     ok(hres == S_OK, "AtlLoadTypeLib failed: %08x\n", hres);
200     FreeLibrary(inst);
201
202     len = SysStringLen(path);
203     ok(len > sizeof(mshtml_tlb_suffixW)/sizeof(WCHAR)
204        && lstrcmpiW(path+len-sizeof(mshtml_tlb_suffixW)/sizeof(WCHAR), mshtml_tlb_suffixW),
205        "unexpected path %s\n", wine_dbgstr_w(path));
206     SysFreeString(path);
207     ok(typelib != NULL, "typelib == NULL\n");
208     ITypeLib_Release(typelib);
209 }
210
211 static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface, REFIID riid, void **ppv)
212 {
213     if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
214         *ppv = iface;
215         return S_OK;
216     }
217
218     ok(0, "unexpected call\n");
219     return E_NOINTERFACE;
220 }
221
222 static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
223 {
224     return 2;
225 }
226
227 static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
228 {
229     return 1;
230 }
231
232 static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
233 {
234     ok(0, "unexpected call\n");
235     return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
239         IConnectionPointContainer **ppCPC)
240 {
241     ok(0, "unexpected call\n");
242     return E_NOTIMPL;
243 }
244
245 static int advise_cnt;
246
247 static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
248                                              DWORD *pdwCookie)
249 {
250     ok(pUnkSink == (IUnknown*)0xdead0000, "pUnkSink = %p\n", pUnkSink);
251     *pdwCookie = 0xdeadbeef;
252     advise_cnt++;
253     return S_OK;
254 }
255
256 static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
257 {
258     ok(dwCookie == 0xdeadbeef, "dwCookie = %x\n", dwCookie);
259     advise_cnt--;
260     return S_OK;
261 }
262
263 static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
264                                                       IEnumConnections **ppEnum)
265 {
266     ok(0, "unexpected call\n");
267     return E_NOTIMPL;
268 }
269
270 static const IConnectionPointVtbl ConnectionPointVtbl =
271 {
272     ConnectionPoint_QueryInterface,
273     ConnectionPoint_AddRef,
274     ConnectionPoint_Release,
275     ConnectionPoint_GetConnectionInterface,
276     ConnectionPoint_GetConnectionPointContainer,
277     ConnectionPoint_Advise,
278     ConnectionPoint_Unadvise,
279     ConnectionPoint_EnumConnections
280 };
281
282 static IConnectionPoint ConnectionPoint = { &ConnectionPointVtbl };
283
284 static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
285         REFIID riid, void **ppv)
286 {
287     if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
288         *ppv = iface;
289         return S_OK;
290     }
291
292     ok(0, "unexpected call\n");
293     return E_NOTIMPL;
294 }
295
296 static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
297 {
298     return 2;
299 }
300
301 static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
302 {
303     return 1;
304 }
305
306 static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
307         IEnumConnectionPoints **ppEnum)
308 {
309     ok(0, "unexpected call\n");
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
314         REFIID riid, IConnectionPoint **ppCP)
315 {
316     ok(IsEqualGUID(riid, &CLSID_Test), "unexpected riid\n");
317     *ppCP = &ConnectionPoint;
318     return S_OK;
319 }
320
321 static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl = {
322     ConnectionPointContainer_QueryInterface,
323     ConnectionPointContainer_AddRef,
324     ConnectionPointContainer_Release,
325     ConnectionPointContainer_EnumConnectionPoints,
326     ConnectionPointContainer_FindConnectionPoint
327 };
328
329 static IConnectionPointContainer ConnectionPointContainer = { &ConnectionPointContainerVtbl };
330
331 static void test_cp(void)
332 {
333     DWORD cookie = 0;
334     HRESULT hres;
335
336     hres = AtlAdvise((IUnknown*)&ConnectionPointContainer, (IUnknown*)0xdead0000, &CLSID_Test, &cookie);
337     ok(hres == S_OK, "AtlAdvise failed: %08x\n", hres);
338     ok(cookie == 0xdeadbeef, "cookie = %x\n", cookie);
339     ok(advise_cnt == 1, "advise_cnt = %d\n", advise_cnt);
340
341     hres = AtlUnadvise((IUnknown*)&ConnectionPointContainer, &CLSID_Test, 0xdeadbeef);
342     ok(hres == S_OK, "AtlUnadvise failed: %08x\n", hres);
343     ok(!advise_cnt, "advise_cnt = %d\n", advise_cnt);
344 }
345
346 START_TEST(atl)
347 {
348     CoInitialize(NULL);
349
350     test_winmodule();
351     test_regcat();
352     test_typelib();
353     test_cp();
354
355     CoUninitialize();
356 }