ole32: Add tests for item moniker.
[wine] / dlls / ole32 / tests / moniker.c
1 /*
2  * Moniker Tests
3  *
4  * Copyright 2004 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define _WIN32_DCOM
22 #define COBJMACROS
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "comcat.h"
30
31 #include "wine/test.h"
32
33 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08lx\n", hr)
34 #define COUNTOF(x) (sizeof(x) / sizeof(x[0]))
35
36 static const WCHAR wszFileName1[] = {'c',':','\\','w','i','n','d','o','w','s','\\','t','e','s','t','1','.','d','o','c',0};
37 static const WCHAR wszFileName2[] = {'c',':','\\','w','i','n','d','o','w','s','\\','t','e','s','t','2','.','d','o','c',0};
38
39 static int count_moniker_matches(IBindCtx * pbc, IEnumMoniker * spEM)
40 {
41     IMoniker * spMoniker;
42     int monCnt=0, matchCnt=0;
43
44     while ((IEnumMoniker_Next(spEM, 1, &spMoniker, NULL)==S_OK))
45     {
46         HRESULT hr;
47         WCHAR * szDisplayn;
48         monCnt++;
49         hr=IMoniker_GetDisplayName(spMoniker, pbc, NULL, &szDisplayn);
50         if (SUCCEEDED(hr))
51         {
52             if (!lstrcmpW(szDisplayn, wszFileName1) || !lstrcmpW(szDisplayn, wszFileName2))
53                 matchCnt++;
54             CoTaskMemFree(szDisplayn);
55         }
56     }
57     trace("Total number of monikers is %i\n", monCnt);
58     return matchCnt;
59 }
60
61 static void test_MkParseDisplayName(void)
62 {
63     IBindCtx * pbc = NULL;
64     HRESULT hr;
65     IMoniker * pmk  = NULL;
66     IMoniker * pmk1 = NULL;
67     IMoniker * pmk2 = NULL;
68     ULONG eaten;
69     int matchCnt;
70     IUnknown * object = NULL;
71
72     IUnknown *lpEM1;
73
74     IEnumMoniker *spEM1  = NULL;
75     IEnumMoniker *spEM2  = NULL;
76     IEnumMoniker *spEM3  = NULL;
77
78     DWORD pdwReg1=0;
79     DWORD grflags=0;
80     DWORD pdwReg2=0;
81     IRunningObjectTable * pprot=NULL;
82
83     /* CLSID of My Computer */
84     static const WCHAR wszDisplayName[] = {'c','l','s','i','d',':',
85         '2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-','A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D',':',0};
86
87     hr = CreateBindCtx(0, &pbc);
88     ok_ole_success(hr, CreateBindCtx);
89
90     hr = MkParseDisplayName(pbc, wszDisplayName, &eaten, &pmk);
91     todo_wine { ok_ole_success(hr, MkParseDisplayName); }
92
93     if (object)
94     {
95         hr = IMoniker_BindToObject(pmk, pbc, NULL, &IID_IUnknown, (LPVOID*)&object);
96         ok_ole_success(hr, IMoniker_BindToObject);
97
98         IUnknown_Release(object);
99     }
100     IBindCtx_Release(pbc);
101
102     /* Test the EnumMoniker interface */
103     hr = CreateBindCtx(0, &pbc);
104     ok_ole_success(hr, CreateBindCtx);
105
106     hr = CreateFileMoniker(wszFileName1, &pmk1);
107     ok(hr==0, "CreateFileMoniker for file hr=%08lx\n", hr);
108     hr = CreateFileMoniker(wszFileName2, &pmk2);
109     ok(hr==0, "CreateFileMoniker for file hr=%08lx\n", hr);
110     hr = IBindCtx_GetRunningObjectTable(pbc, &pprot);
111     ok(hr==0, "IBindCtx_GetRunningObjectTable hr=%08lx\n", hr);
112
113     /* Check EnumMoniker before registering */
114     hr = IRunningObjectTable_EnumRunning(pprot, &spEM1);
115     ok(hr==0, "IRunningObjectTable_EnumRunning hr=%08lx\n", hr);
116     hr = IEnumMoniker_QueryInterface(spEM1, &IID_IUnknown, (void*) &lpEM1);
117     /* Register a couple of Monikers and check is ok */
118     ok(hr==0, "IEnumMoniker_QueryInterface hr %08lx %p\n", hr, lpEM1);
119     hr = MK_E_NOOBJECT;
120     
121     matchCnt = count_moniker_matches(pbc, spEM1);
122     trace("Number of matches is %i\n", matchCnt);
123
124     grflags= grflags | ROTFLAGS_REGISTRATIONKEEPSALIVE;
125     hr = IRunningObjectTable_Register(pprot, grflags, lpEM1, pmk1, &pdwReg1);
126     ok(hr==0, "IRunningObjectTable_Register hr=%08lx %p %08lx %p %p %ld\n",
127         hr, pprot, grflags, lpEM1, pmk1, pdwReg1);
128
129     trace("IROT::Register\n");
130     grflags=0;
131     grflags= grflags | ROTFLAGS_REGISTRATIONKEEPSALIVE;
132     hr = IRunningObjectTable_Register(pprot, grflags, lpEM1, pmk2, &pdwReg2);
133     ok(hr==0, "IRunningObjectTable_Register hr=%08lx %p %08lx %p %p %ld\n", hr,
134        pprot, grflags, lpEM1, pmk2, pdwReg2);
135
136     hr = IRunningObjectTable_EnumRunning(pprot, &spEM2);
137     ok(hr==0, "IRunningObjectTable_EnumRunning hr=%08lx\n", hr);
138
139     matchCnt = count_moniker_matches(pbc, spEM2);
140     ok(matchCnt==2, "Number of matches should be equal to 2 not %i\n", matchCnt);
141
142     trace("IEnumMoniker::Clone\n");
143     IEnumMoniker_Clone(spEM2, &spEM3);
144
145     matchCnt = count_moniker_matches(pbc, spEM3);
146     ok(matchCnt==0, "Number of matches should be equal to 0 not %i\n", matchCnt);
147     trace("IEnumMoniker::Reset\n");
148     IEnumMoniker_Reset(spEM3);
149
150     matchCnt = count_moniker_matches(pbc, spEM3);
151     ok(matchCnt==2, "Number of matches should be equal to 2 not %i\n", matchCnt);
152
153     IRunningObjectTable_Revoke(pprot,pdwReg1);
154     IRunningObjectTable_Revoke(pprot,pdwReg2);
155     IEnumMoniker_Release(spEM1);
156     IEnumMoniker_Release(spEM1);
157     IEnumMoniker_Release(spEM2);
158     IEnumMoniker_Release(spEM3);
159     IMoniker_Release(pmk1);
160     IMoniker_Release(pmk2);
161     IRunningObjectTable_Release(pprot);
162
163     IBindCtx_Release(pbc);
164 }
165
166 static const LARGE_INTEGER llZero;
167
168 static const BYTE expected_class_moniker_marshal_data[] =
169 {
170     0x4d,0x45,0x4f,0x57,0x04,0x00,0x00,0x00,
171     0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
172     0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
173     0x1a,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
174     0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
175     0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,
176     0x05,0xe0,0x02,0x00,0x00,0x00,0x00,0x00,
177     0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
178     0x00,0x00,0x00,0x00,
179 };
180
181 static const BYTE expected_class_moniker_saved_data[] =
182 {
183      0x05,0xe0,0x02,0x00,0x00,0x00,0x00,0x00,
184      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
185      0x00,0x00,0x00,0x00,
186 };
187
188 static const BYTE expected_class_moniker_comparison_data[] =
189 {
190      0x1a,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
191      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
192      0x05,0xe0,0x02,0x00,0x00,0x00,0x00,0x00,
193      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
194 };
195
196 static const BYTE expected_item_moniker_comparison_data[] =
197 {
198      0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
199      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
200      0x21,0x00,0x54,0x00,0x45,0x00,0x53,0x00,
201      0x54,0x00,0x00,0x00,
202 };
203
204 static const BYTE expected_item_moniker_saved_data[] =
205 {
206      0x02,0x00,0x00,0x00,0x21,0x00,0x05,0x00,
207      0x00,0x00,0x54,0x65,0x73,0x74,0x00,
208 };
209
210 static const BYTE expected_item_moniker_marshal_data[] =
211 {
212      0x4d,0x45,0x4f,0x57,0x04,0x00,0x00,0x00,
213      0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
214      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
215      0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
216      0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
217      0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,
218      0x02,0x00,0x00,0x00,0x21,0x00,0x05,0x00,
219      0x00,0x00,0x54,0x65,0x73,0x74,0x00,
220 };
221
222 static void test_moniker(
223     const char *testname, IMoniker *moniker,
224     const BYTE *expected_moniker_marshal_data, size_t sizeof_expected_moniker_marshal_data,
225     const BYTE *expected_moniker_saved_data, size_t sizeof_expected_moniker_saved_data,
226     const BYTE *expected_moniker_comparison_data, size_t sizeof_expected_moniker_comparison_data)
227 {
228     IStream * stream;
229     IROTData * rotdata;
230     HRESULT hr;
231     HGLOBAL hglobal;
232     LPBYTE moniker_data;
233     DWORD moniker_size;
234     DWORD i;
235     BOOL same = TRUE;
236     BYTE buffer[128];
237
238     /* IROTData::GetComparisonData test */
239
240     hr = IMoniker_QueryInterface(moniker, &IID_IROTData, (void **)&rotdata);
241     ok_ole_success(hr, IMoniker_QueryInterface(IID_IROTData));
242
243     hr = IROTData_GetComparisonData(rotdata, buffer, sizeof(buffer), &moniker_size);
244     ok_ole_success(hr, IROTData_GetComparisonData);
245
246     if (hr != S_OK) moniker_size = 0;
247
248     /* first check we have the right amount of data */
249     ok(moniker_size == sizeof_expected_moniker_comparison_data,
250         "%s: Size of comparison data differs (expected %d, actual %ld)\n",
251         testname, sizeof_expected_moniker_comparison_data, moniker_size);
252
253     /* then do a byte-by-byte comparison */
254     for (i = 0; i < min(moniker_size, sizeof_expected_moniker_comparison_data); i++)
255     {
256         if (expected_moniker_comparison_data[i] != buffer[i])
257         {
258             same = FALSE;
259             break;
260         }
261     }
262
263     ok(same, "%s: Comparison data differs\n", testname);
264     if (!same)
265     {
266         for (i = 0; i < moniker_size; i++)
267         {
268             if (i % 8 == 0) trace("     ");
269             trace("0x%02x,", buffer[i]);
270             if (i % 8 == 7) trace("\n");
271         }
272         trace("\n");
273     }
274
275     IROTData_Release(rotdata);
276   
277     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
278   
279     /* Saving */
280
281     hr = IMoniker_Save(moniker, stream, TRUE);
282     ok_ole_success(hr, IMoniker_Save);
283
284     hr = GetHGlobalFromStream(stream, &hglobal);
285     ok_ole_success(hr, GetHGlobalFromStream);
286
287     moniker_size = GlobalSize(hglobal);
288
289     moniker_data = GlobalLock(hglobal);
290
291     /* first check we have the right amount of data */
292     ok(moniker_size == sizeof_expected_moniker_saved_data,
293         "%s: Size of saved data differs (expected %d, actual %ld)\n",
294         testname, sizeof_expected_moniker_saved_data, moniker_size);
295
296     /* then do a byte-by-byte comparison */
297     for (i = 0; i < min(moniker_size, sizeof_expected_moniker_saved_data); i++)
298     {
299         if (expected_moniker_saved_data[i] != moniker_data[i])
300         {
301             same = FALSE;
302             break;
303         }
304     }
305
306     ok(same, "%s: Saved data differs\n", testname);
307     if (!same)
308     {
309         for (i = 0; i < moniker_size; i++)
310         {
311             if (i % 8 == 0) trace("     ");
312             trace("0x%02x,", moniker_data[i]);
313             if (i % 8 == 7) trace("\n");
314         }
315         trace("\n");
316     }
317
318     GlobalUnlock(hglobal);
319
320     IStream_Release(stream);
321
322     /* Marshaling tests */
323
324     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
325     ok_ole_success(hr, CreateStreamOnHGlobal);
326
327     hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
328     ok_ole_success(hr, CoMarshalInterface);
329
330     hr = GetHGlobalFromStream(stream, &hglobal);
331     ok_ole_success(hr, GetHGlobalFromStream);
332
333     moniker_size = GlobalSize(hglobal);
334
335     moniker_data = GlobalLock(hglobal);
336
337     /* first check we have the right amount of data */
338     ok(moniker_size == sizeof_expected_moniker_marshal_data,
339         "%s: Size of marshaled data differs (expected %d, actual %ld)\n",
340         testname, sizeof_expected_moniker_marshal_data, moniker_size);
341
342     /* then do a byte-by-byte comparison */
343     for (i = 0; i < min(moniker_size, sizeof_expected_moniker_marshal_data); i++)
344     {
345         if (expected_moniker_marshal_data[i] != moniker_data[i])
346         {
347             same = FALSE;
348             break;
349         }
350     }
351
352     ok(same, "%s: Marshaled data differs\n", testname);
353     if (!same)
354     {
355         for (i = 0; i < moniker_size; i++)
356         {
357             if (i % 8 == 0) trace("     ");
358             trace("0x%02x,", moniker_data[i]);
359             if (i % 8 == 7) trace("\n");
360         }
361         trace("\n");
362     }
363
364     GlobalUnlock(hglobal);
365
366     IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
367     hr = CoReleaseMarshalData(stream);
368     ok_ole_success(hr, CoReleaseMarshalData);
369
370     IStream_Release(stream);
371 }
372
373 static void test_class_moniker(void)
374 {
375     HRESULT hr;
376     IMoniker *moniker;
377     DWORD moniker_type;
378     DWORD hash;
379
380     hr = CreateClassMoniker(&CLSID_StdComponentCategoriesMgr, &moniker);
381     todo_wine ok_ole_success(hr, CreateClassMoniker);
382     if (!moniker) return;
383
384     test_moniker("class moniker", moniker, 
385         expected_class_moniker_marshal_data, sizeof(expected_class_moniker_marshal_data),
386         expected_class_moniker_saved_data, sizeof(expected_class_moniker_saved_data),
387         expected_class_moniker_comparison_data, sizeof(expected_class_moniker_comparison_data));
388
389     /* Hashing */
390
391     hr = IMoniker_Hash(moniker, &hash);
392     ok_ole_success(hr, IMoniker_Hash);
393
394     ok(hash == CLSID_StdComponentCategoriesMgr.Data1,
395         "Hash value != Data1 field of clsid, instead was 0x%08lx\n",
396         hash);
397
398     /* IsSystemMoniker test */
399
400     hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
401     ok_ole_success(hr, IMoniker_IsSystemMoniker);
402
403     ok(moniker_type == MKSYS_CLASSMONIKER,
404         "dwMkSys != MKSYS_CLASSMONIKER, instead was 0x%08lx",
405         moniker_type);
406
407     IMoniker_Release(moniker);
408 }
409
410 static void test_file_moniker(WCHAR* path)
411 {
412     IStream *stream;
413     IMoniker *moniker1 = NULL, *moniker2 = NULL;
414     HRESULT hr;
415
416     hr = CreateFileMoniker(path, &moniker1);
417     ok_ole_success(hr, CreateFileMoniker); 
418
419     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
420
421     /* Marshal */
422     hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker1, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
423     ok_ole_success(hr, CoMarshalInterface);
424     
425     /* Rewind */
426     hr = IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
427     ok_ole_success(hr, IStream_Seek);
428
429     /* Unmarshal */
430     hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void**)&moniker2);
431     ok_ole_success(hr, CoUnmarshalInterface);
432
433     hr = IMoniker_IsEqual(moniker1, moniker2);
434     ok_ole_success(hr, IsEqual);
435
436     IStream_Release(stream);
437     if (moniker1) 
438         IMoniker_Release(moniker1);
439     if (moniker2) 
440         IMoniker_Release(moniker2);
441 }
442
443 static void test_file_monikers(void)
444 {
445     static WCHAR wszFile[][30] = {
446         {'\\', 'w','i','n','d','o','w','s','\\','s','y','s','t','e','m','\\','t','e','s','t','1','.','d','o','c',0},
447         {'\\', 'a','b','c','d','e','f','g','\\','h','i','j','k','l','\\','m','n','o','p','q','r','s','t','u','.','m','n','o',0},
448         /* These map to themselves in Windows-1252 & 932 (Shift-JIS) */
449         {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0},
450         /* U+2020 = DAGGER     = 0x86 (1252) = 0x813f (932)
451          * U+20AC = EURO SIGN  = 0x80 (1252) =  undef (932)
452          * U+0100 .. = Latin extended-A
453          */ 
454         {0x20ac, 0x2020, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107, 0x108, 0x109, 0x10a, 0x10b, 0x10c,  0},
455         };
456
457     int i; 
458
459     trace("ACP is %u\n", GetACP());
460
461     for (i = 0; i < COUNTOF(wszFile); ++i)
462     {
463         int j ;
464         for (j = lstrlenW(wszFile[i]); j > 0; --j)
465         {
466             wszFile[i][j] = 0;
467             test_file_moniker(wszFile[i]);
468         }
469     }
470 }
471
472 static void test_item_moniker()
473 {
474     HRESULT hr;
475     IMoniker *moniker;
476     DWORD moniker_type;
477     DWORD hash;
478     static const WCHAR wszDelimeter[] = {'!',0};
479     static const WCHAR wszObjectName[] = {'T','e','s','t',0};
480
481     hr = CreateItemMoniker(wszDelimeter, wszObjectName, &moniker);
482     ok_ole_success(hr, CreateItemMoniker);
483
484     test_moniker("item moniker", moniker, 
485         expected_item_moniker_marshal_data, sizeof(expected_item_moniker_marshal_data),
486         expected_item_moniker_saved_data, sizeof(expected_item_moniker_saved_data),
487         expected_item_moniker_comparison_data, sizeof(expected_item_moniker_comparison_data));
488
489     /* Hashing */
490
491     hr = IMoniker_Hash(moniker, &hash);
492     ok_ole_success(hr, IMoniker_Hash);
493
494     todo_wine
495     ok(hash == 0x73c,
496         "Hash value != 0x73c, instead was 0x%08lx\n",
497         hash);
498
499     /* IsSystemMoniker test */
500
501     hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
502     ok_ole_success(hr, IMoniker_IsSystemMoniker);
503
504     ok(moniker_type == MKSYS_ITEMMONIKER,
505         "dwMkSys != MKSYS_ITEMMONIKER, instead was 0x%08lx",
506         moniker_type);
507
508     IMoniker_Release(moniker);
509 }
510
511 START_TEST(moniker)
512 {
513     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
514
515     test_MkParseDisplayName();
516     test_class_moniker();
517     test_file_monikers();
518     test_item_moniker();
519
520     /* FIXME: test moniker creation funcs and parsing other moniker formats */
521
522     CoUninitialize();
523 }