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 const BYTE expected_anti_moniker_marshal_data[] =
224 0x4d,0x45,0x4f,0x57,0x04,0x00,0x00,0x00,
225 0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
226 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
227 0x05,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
228 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
229 0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,
233 static const BYTE expected_anti_moniker_saved_data[] =
238 static const BYTE expected_anti_moniker_comparison_data[] =
240 0x05,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
241 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46,
245 static void test_moniker(
246 const char *testname, IMoniker *moniker,
247 const BYTE *expected_moniker_marshal_data, size_t sizeof_expected_moniker_marshal_data,
248 const BYTE *expected_moniker_saved_data, size_t sizeof_expected_moniker_saved_data,
249 const BYTE *expected_moniker_comparison_data, size_t sizeof_expected_moniker_comparison_data)
261 /* IROTData::GetComparisonData test */
263 hr = IMoniker_QueryInterface(moniker, &IID_IROTData, (void **)&rotdata);
264 ok_ole_success(hr, IMoniker_QueryInterface(IID_IROTData));
266 hr = IROTData_GetComparisonData(rotdata, buffer, sizeof(buffer), &moniker_size);
267 ok_ole_success(hr, IROTData_GetComparisonData);
269 if (hr != S_OK) moniker_size = 0;
271 /* first check we have the right amount of data */
272 ok(moniker_size == sizeof_expected_moniker_comparison_data,
273 "%s: Size of comparison data differs (expected %d, actual %ld)\n",
274 testname, sizeof_expected_moniker_comparison_data, moniker_size);
276 /* then do a byte-by-byte comparison */
277 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_comparison_data); i++)
279 if (expected_moniker_comparison_data[i] != buffer[i])
286 ok(same, "%s: Comparison data differs\n", testname);
289 for (i = 0; i < moniker_size; i++)
291 if (i % 8 == 0) trace(" ");
292 trace("0x%02x,", buffer[i]);
293 if (i % 8 == 7) trace("\n");
298 IROTData_Release(rotdata);
300 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
304 hr = IMoniker_Save(moniker, stream, TRUE);
305 ok_ole_success(hr, IMoniker_Save);
307 hr = GetHGlobalFromStream(stream, &hglobal);
308 ok_ole_success(hr, GetHGlobalFromStream);
310 moniker_size = GlobalSize(hglobal);
312 moniker_data = GlobalLock(hglobal);
314 /* first check we have the right amount of data */
315 ok(moniker_size == sizeof_expected_moniker_saved_data,
316 "%s: Size of saved data differs (expected %d, actual %ld)\n",
317 testname, sizeof_expected_moniker_saved_data, moniker_size);
319 /* then do a byte-by-byte comparison */
320 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_saved_data); i++)
322 if (expected_moniker_saved_data[i] != moniker_data[i])
329 ok(same, "%s: Saved data differs\n", testname);
332 for (i = 0; i < moniker_size; i++)
334 if (i % 8 == 0) trace(" ");
335 trace("0x%02x,", moniker_data[i]);
336 if (i % 8 == 7) trace("\n");
341 GlobalUnlock(hglobal);
343 IStream_Release(stream);
345 /* Marshaling tests */
347 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
348 ok_ole_success(hr, CreateStreamOnHGlobal);
350 hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
351 ok_ole_success(hr, CoMarshalInterface);
353 hr = GetHGlobalFromStream(stream, &hglobal);
354 ok_ole_success(hr, GetHGlobalFromStream);
356 moniker_size = GlobalSize(hglobal);
358 moniker_data = GlobalLock(hglobal);
360 /* first check we have the right amount of data */
361 ok(moniker_size == sizeof_expected_moniker_marshal_data,
362 "%s: Size of marshaled data differs (expected %d, actual %ld)\n",
363 testname, sizeof_expected_moniker_marshal_data, moniker_size);
365 /* then do a byte-by-byte comparison */
366 for (i = 0; i < min(moniker_size, sizeof_expected_moniker_marshal_data); i++)
368 if (expected_moniker_marshal_data[i] != moniker_data[i])
375 ok(same, "%s: Marshaled data differs\n", testname);
378 for (i = 0; i < moniker_size; i++)
380 if (i % 8 == 0) trace(" ");
381 trace("0x%02x,", moniker_data[i]);
382 if (i % 8 == 7) trace("\n");
387 GlobalUnlock(hglobal);
389 IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
390 hr = CoReleaseMarshalData(stream);
391 ok_ole_success(hr, CoReleaseMarshalData);
393 IStream_Release(stream);
396 static void test_class_moniker(void)
403 hr = CreateClassMoniker(&CLSID_StdComponentCategoriesMgr, &moniker);
404 todo_wine ok_ole_success(hr, CreateClassMoniker);
405 if (!moniker) return;
407 test_moniker("class moniker", moniker,
408 expected_class_moniker_marshal_data, sizeof(expected_class_moniker_marshal_data),
409 expected_class_moniker_saved_data, sizeof(expected_class_moniker_saved_data),
410 expected_class_moniker_comparison_data, sizeof(expected_class_moniker_comparison_data));
414 hr = IMoniker_Hash(moniker, &hash);
415 ok_ole_success(hr, IMoniker_Hash);
417 ok(hash == CLSID_StdComponentCategoriesMgr.Data1,
418 "Hash value != Data1 field of clsid, instead was 0x%08lx\n",
421 /* IsSystemMoniker test */
423 hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
424 ok_ole_success(hr, IMoniker_IsSystemMoniker);
426 ok(moniker_type == MKSYS_CLASSMONIKER,
427 "dwMkSys != MKSYS_CLASSMONIKER, instead was 0x%08lx",
430 IMoniker_Release(moniker);
433 static void test_file_moniker(WCHAR* path)
436 IMoniker *moniker1 = NULL, *moniker2 = NULL;
439 hr = CreateFileMoniker(path, &moniker1);
440 ok_ole_success(hr, CreateFileMoniker);
442 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
445 hr = CoMarshalInterface(stream, &IID_IMoniker, (IUnknown *)moniker1, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
446 ok_ole_success(hr, CoMarshalInterface);
449 hr = IStream_Seek(stream, llZero, STREAM_SEEK_SET, NULL);
450 ok_ole_success(hr, IStream_Seek);
453 hr = CoUnmarshalInterface(stream, &IID_IMoniker, (void**)&moniker2);
454 ok_ole_success(hr, CoUnmarshalInterface);
456 hr = IMoniker_IsEqual(moniker1, moniker2);
457 ok_ole_success(hr, IsEqual);
459 IStream_Release(stream);
461 IMoniker_Release(moniker1);
463 IMoniker_Release(moniker2);
466 static void test_file_monikers(void)
468 static WCHAR wszFile[][30] = {
469 {'\\', 'w','i','n','d','o','w','s','\\','s','y','s','t','e','m','\\','t','e','s','t','1','.','d','o','c',0},
470 {'\\', '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},
471 /* These map to themselves in Windows-1252 & 932 (Shift-JIS) */
472 {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0},
473 /* U+2020 = DAGGER = 0x86 (1252) = 0x813f (932)
474 * U+20AC = EURO SIGN = 0x80 (1252) = undef (932)
475 * U+0100 .. = Latin extended-A
477 {0x20ac, 0x2020, 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107, 0x108, 0x109, 0x10a, 0x10b, 0x10c, 0},
482 trace("ACP is %u\n", GetACP());
484 for (i = 0; i < COUNTOF(wszFile); ++i)
487 for (j = lstrlenW(wszFile[i]); j > 0; --j)
490 test_file_moniker(wszFile[i]);
495 static void test_item_moniker(void)
501 static const WCHAR wszDelimeter[] = {'!',0};
502 static const WCHAR wszObjectName[] = {'T','e','s','t',0};
504 hr = CreateItemMoniker(wszDelimeter, wszObjectName, &moniker);
505 ok_ole_success(hr, CreateItemMoniker);
507 test_moniker("item moniker", moniker,
508 expected_item_moniker_marshal_data, sizeof(expected_item_moniker_marshal_data),
509 expected_item_moniker_saved_data, sizeof(expected_item_moniker_saved_data),
510 expected_item_moniker_comparison_data, sizeof(expected_item_moniker_comparison_data));
514 hr = IMoniker_Hash(moniker, &hash);
515 ok_ole_success(hr, IMoniker_Hash);
519 "Hash value != 0x73c, instead was 0x%08lx\n",
522 /* IsSystemMoniker test */
524 hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
525 ok_ole_success(hr, IMoniker_IsSystemMoniker);
527 ok(moniker_type == MKSYS_ITEMMONIKER,
528 "dwMkSys != MKSYS_ITEMMONIKER, instead was 0x%08lx",
531 IMoniker_Release(moniker);
534 static void test_anti_moniker(void)
542 hr = CreateAntiMoniker(&moniker);
543 ok_ole_success(hr, CreateAntiMoniker);
544 if (!moniker) return;
546 test_moniker("anti moniker", moniker,
547 expected_anti_moniker_marshal_data, sizeof(expected_anti_moniker_marshal_data),
548 expected_anti_moniker_saved_data, sizeof(expected_anti_moniker_saved_data),
549 expected_anti_moniker_comparison_data, sizeof(expected_anti_moniker_comparison_data));
552 hr = IMoniker_Hash(moniker, &hash);
553 ok_ole_success(hr, IMoniker_Hash);
554 ok(hash == 0x80000001,
555 "Hash value != 0x80000001, instead was 0x%08lx\n",
558 /* IsSystemMoniker test */
559 hr = IMoniker_IsSystemMoniker(moniker, &moniker_type);
560 ok_ole_success(hr, IMoniker_IsSystemMoniker);
561 ok(moniker_type == MKSYS_ANTIMONIKER,
562 "dwMkSys != MKSYS_ANTIMONIKER, instead was 0x%08lx",
565 hr = IMoniker_Inverse(moniker, &inverse);
566 ok(hr == MK_E_NOINVERSE, "IMoniker_Inverse should have returned MK_E_NOINVERSE instead of 0x%08lx\n", hr);
567 ok(inverse == NULL, "inverse should have been set to NULL instead of %p\n", inverse);
569 IMoniker_Release(moniker);
574 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
576 test_MkParseDisplayName();
577 test_class_moniker();
578 test_file_monikers();
582 /* FIXME: test moniker creation funcs and parsing other moniker formats */