4 * Copyright 2004 Robert Shearman
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.
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.
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
31 #include "wine/test.h"
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]))
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};
39 static int count_moniker_matches(IBindCtx * pbc, IEnumMoniker * spEM)
42 int monCnt=0, matchCnt=0;
44 while ((IEnumMoniker_Next(spEM, 1, &spMoniker, NULL)==S_OK))
49 hr=IMoniker_GetDisplayName(spMoniker, pbc, NULL, &szDisplayn);
52 if (!lstrcmpW(szDisplayn, wszFileName1) || !lstrcmpW(szDisplayn, wszFileName2))
54 CoTaskMemFree(szDisplayn);
57 trace("Total number of monikers is %i\n", monCnt);
61 static void test_MkParseDisplayName(void)
63 IBindCtx * pbc = NULL;
65 IMoniker * pmk = NULL;
66 IMoniker * pmk1 = NULL;
67 IMoniker * pmk2 = NULL;
70 IUnknown * object = NULL;
74 IEnumMoniker *spEM1 = NULL;
75 IEnumMoniker *spEM2 = NULL;
76 IEnumMoniker *spEM3 = NULL;
81 IRunningObjectTable * pprot=NULL;
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};
87 hr = CreateBindCtx(0, &pbc);
88 ok_ole_success(hr, CreateBindCtx);
90 hr = MkParseDisplayName(pbc, wszDisplayName, &eaten, &pmk);
91 todo_wine { ok_ole_success(hr, MkParseDisplayName); }
95 hr = IMoniker_BindToObject(pmk, pbc, NULL, &IID_IUnknown, (LPVOID*)&object);
96 ok_ole_success(hr, IMoniker_BindToObject);
98 IUnknown_Release(object);
100 IBindCtx_Release(pbc);
102 /* Test the EnumMoniker interface */
103 hr = CreateBindCtx(0, &pbc);
104 ok_ole_success(hr, CreateBindCtx);
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);
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);
121 matchCnt = count_moniker_matches(pbc, spEM1);
122 trace("Number of matches is %i\n", matchCnt);
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);
129 trace("IROT::Register\n");
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);
136 hr = IRunningObjectTable_EnumRunning(pprot, &spEM2);
137 ok(hr==0, "IRunningObjectTable_EnumRunning hr=%08lx\n", hr);
139 matchCnt = count_moniker_matches(pbc, spEM2);
140 ok(matchCnt==2, "Number of matches should be equal to 2 not %i\n", matchCnt);
142 trace("IEnumMoniker::Clone\n");
143 IEnumMoniker_Clone(spEM2, &spEM3);
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);
150 matchCnt = count_moniker_matches(pbc, spEM3);
151 ok(matchCnt==2, "Number of matches should be equal to 2 not %i\n", matchCnt);
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);
163 IBindCtx_Release(pbc);
166 static const LARGE_INTEGER llZero;
168 static const BYTE expected_class_moniker_marshal_data[] =
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,
181 static const BYTE expected_class_moniker_saved_data[] =
183 0x05,0xe0,0x02,0x00,0x00,0x00,0x00,0x00,
184 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
188 static const BYTE expected_class_moniker_comparison_data[] =
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,
196 static const BYTE expected_item_moniker_comparison_data[] =
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,
204 static const BYTE expected_item_moniker_saved_data[] =
206 0x02,0x00,0x00,0x00,0x21,0x00,0x05,0x00,
207 0x00,0x00,0x54,0x65,0x73,0x74,0x00,
210 static const BYTE expected_item_moniker_marshal_data[] =
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,
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)
238 /* IROTData::GetComparisonData test */
240 hr = IMoniker_QueryInterface(moniker, &IID_IROTData, (void **)&rotdata);
241 ok_ole_success(hr, IMoniker_QueryInterface(IID_IROTData));
243 hr = IROTData_GetComparisonData(rotdata, buffer, sizeof(buffer), &moniker_size);
244 ok_ole_success(hr, IROTData_GetComparisonData);
246 if (hr != S_OK) moniker_size = 0;
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);
253 /* then do a byte-by-byte comparison */
254 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_comparison_data); i++)
256 if (expected_moniker_comparison_data[i] != buffer[i])
263 ok(same, "%s: Comparison data differs\n", testname);
266 for (i = 0; i < moniker_size; i++)
268 if (i % 8 == 0) trace(" ");
269 trace("0x%02x,", buffer[i]);
270 if (i % 8 == 7) trace("\n");
275 IROTData_Release(rotdata);
277 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
281 hr = IMoniker_Save(moniker, stream, TRUE);
282 ok_ole_success(hr, IMoniker_Save);
284 hr = GetHGlobalFromStream(stream, &hglobal);
285 ok_ole_success(hr, GetHGlobalFromStream);
287 moniker_size = GlobalSize(hglobal);
289 moniker_data = GlobalLock(hglobal);
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);
296 /* then do a byte-by-byte comparison */
297 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_saved_data); i++)
299 if (expected_moniker_saved_data[i] != moniker_data[i])
306 ok(same, "%s: Saved data differs\n", testname);
309 for (i = 0; i < moniker_size; i++)
311 if (i % 8 == 0) trace(" ");
312 trace("0x%02x,", moniker_data[i]);
313 if (i % 8 == 7) trace("\n");
318 GlobalUnlock(hglobal);
320 IStream_Release(stream);
322 /* Marshaling tests */
324 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
325 ok_ole_success(hr, CreateStreamOnHGlobal);
327 hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
328 ok_ole_success(hr, CoMarshalInterface);
330 hr = GetHGlobalFromStream(stream, &hglobal);
331 ok_ole_success(hr, GetHGlobalFromStream);
333 moniker_size = GlobalSize(hglobal);
335 moniker_data = GlobalLock(hglobal);
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);
342 /* then do a byte-by-byte comparison */
343 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_marshal_data); i++)
345 if (expected_moniker_marshal_data[i] != moniker_data[i])
352 ok(same, "%s: Marshaled data differs\n", testname);
355 for (i = 0; i < moniker_size; i++)
357 if (i % 8 == 0) trace(" ");
358 trace("0x%02x,", moniker_data[i]);
359 if (i % 8 == 7) trace("\n");
364 GlobalUnlock(hglobal);
366 IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
367 hr = CoReleaseMarshalData(stream);
368 ok_ole_success(hr, CoReleaseMarshalData);
370 IStream_Release(stream);
373 static void test_class_moniker(void)
380 hr = CreateClassMoniker(&CLSID_StdComponentCategoriesMgr, &moniker);
381 todo_wine ok_ole_success(hr, CreateClassMoniker);
382 if (!moniker) return;
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));
391 hr = IMoniker_Hash(moniker, &hash);
392 ok_ole_success(hr, IMoniker_Hash);
394 ok(hash == CLSID_StdComponentCategoriesMgr.Data1,
395 "Hash value != Data1 field of clsid, instead was 0x%08lx\n",
398 /* IsSystemMoniker test */
400 hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
401 ok_ole_success(hr, IMoniker_IsSystemMoniker);
403 ok(moniker_type == MKSYS_CLASSMONIKER,
404 "dwMkSys != MKSYS_CLASSMONIKER, instead was 0x%08lx",
407 IMoniker_Release(moniker);
410 static void test_file_moniker(WCHAR* path)
413 IMoniker *moniker1 = NULL, *moniker2 = NULL;
416 hr = CreateFileMoniker(path, &moniker1);
417 ok_ole_success(hr, CreateFileMoniker);
419 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
422 hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker1, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
423 ok_ole_success(hr, CoMarshalInterface);
426 hr = IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
427 ok_ole_success(hr, IStream_Seek);
430 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void**)&moniker2);
431 ok_ole_success(hr, CoUnmarshalInterface);
433 hr = IMoniker_IsEqual(moniker1, moniker2);
434 ok_ole_success(hr, IsEqual);
436 IStream_Release(stream);
438 IMoniker_Release(moniker1);
440 IMoniker_Release(moniker2);
443 static void test_file_monikers(void)
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
454 {0x20ac, 0x2020, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107, 0x108, 0x109, 0x10a, 0x10b, 0x10c, 0},
459 trace("ACP is %u\n", GetACP());
461 for (i = 0; i < COUNTOF(wszFile); ++i)
464 for (j = lstrlenW(wszFile[i]); j > 0; --j)
467 test_file_moniker(wszFile[i]);
472 static void test_item_moniker()
478 static const WCHAR wszDelimeter[] = {'!',0};
479 static const WCHAR wszObjectName[] = {'T','e','s','t',0};
481 hr = CreateItemMoniker(wszDelimeter, wszObjectName, &moniker);
482 ok_ole_success(hr, CreateItemMoniker);
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));
491 hr = IMoniker_Hash(moniker, &hash);
492 ok_ole_success(hr, IMoniker_Hash);
496 "Hash value != 0x73c, instead was 0x%08lx\n",
499 /* IsSystemMoniker test */
501 hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
502 ok_ole_success(hr, IMoniker_IsSystemMoniker);
504 ok(moniker_type == MKSYS_ITEMMONIKER,
505 "dwMkSys != MKSYS_ITEMMONIKER, instead was 0x%08lx",
508 IMoniker_Release(moniker);
513 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
515 test_MkParseDisplayName();
516 test_class_moniker();
517 test_file_monikers();
520 /* FIXME: test moniker creation funcs and parsing other moniker formats */