4 * Copyright 2007 Huw Davies
5 * Copyright 2010 Adam Martinson for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/test.h"
35 static const WCHAR xdr_schema_uri[] = {'x','-','s','c','h','e','m','a',':','t','e','s','t','.','x','m','l',0};
37 static const WCHAR xdr_schema_xml[] = {
38 '<','S','c','h','e','m','a',' ','x','m','l','n','s','=','\"','u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','x','m','l','-','d','a','t','a','\"','\n',
39 'x','m','l','n','s',':','d','t','=','\"','u','r','n',':','s','c','h','e','m','a','s','-','m','i','c','r','o','s','o','f','t','-','c','o','m',':','d','a','t','a','t','y','p','e','s','\"','>','\n',
40 '<','/','S','c','h','e','m','a','>','\n',0
43 static const CHAR xdr_schema1_uri[] = "x-schema:test1.xdr";
44 static const CHAR xdr_schema1_xml[] =
45 "<?xml version='1.0'?>"
46 "<Schema xmlns='urn:schemas-microsoft-com:xml-data'"
47 " xmlns:dt='urn:schemas-microsoft-com:datatypes'"
49 " <ElementType name='x' dt:type='boolean'/>"
50 " <ElementType name='y'>"
51 " <datatype dt:type='int'/>"
53 " <ElementType name='z'/>"
54 " <ElementType name='root' content='eltOnly' model='open' order='seq'>"
55 " <element type='x'/>"
56 " <element type='y'/>"
57 " <element type='z'/>"
61 static const CHAR xdr_schema2_uri[] = "x-schema:test2.xdr";
62 static const CHAR xdr_schema2_xml[] =
63 "<?xml version='1.0'?>"
64 "<Schema xmlns='urn:schemas-microsoft-com:xml-data'"
65 " xmlns:dt='urn:schemas-microsoft-com:datatypes'"
67 " <ElementType name='x' dt:type='bin.base64'/>"
68 " <ElementType name='y' dt:type='uuid'/>"
69 " <ElementType name='z'/>"
70 " <ElementType name='root' content='eltOnly' model='closed' order='one'>"
71 " <element type='x'/>"
72 " <element type='y'/>"
73 " <element type='z'/>"
77 static const CHAR xdr_schema3_uri[] = "x-schema:test3.xdr";
78 static const CHAR xdr_schema3_xml[] =
79 "<?xml version='1.0'?>"
80 "<Schema xmlns='urn:schemas-microsoft-com:xml-data'"
81 " xmlns:dt='urn:schemas-microsoft-com:datatypes'"
83 " <ElementType name='root' content='textOnly' model='open'>"
84 " <AttributeType name='x' dt:type='int'/>"
85 " <AttributeType name='y' dt:type='enumeration' dt:values='a b c'/>"
86 " <AttributeType name='z' dt:type='uuid'/>"
87 " <attribute type='x'/>"
88 " <attribute type='y'/>"
89 " <attribute type='z'/>"
93 static const CHAR xsd_schema1_uri[] = "x-schema:test1.xsd";
94 static const CHAR xsd_schema1_xml[] =
95 "<?xml version='1.0'?>"
96 "<schema xmlns='http://www.w3.org/2001/XMLSchema'"
97 " targetNamespace='x-schema:test1.xsd'>"
98 " <element name='root'>"
100 " <sequence maxOccurs='unbounded'>"
107 static const CHAR xsd_schema2_uri[] = "x-schema:test2.xsd";
108 static const CHAR xsd_schema2_xml[] =
109 "<?xml version='1.0'?>"
110 "<schema xmlns='http://www.w3.org/2001/XMLSchema'"
111 " targetNamespace='x-schema:test2.xsd'>"
112 " <element name='root'>"
114 " <sequence maxOccurs='unbounded'>"
121 static const CHAR xsd_schema3_uri[] = "x-schema:test3.xsd";
122 static const CHAR xsd_schema3_xml[] =
123 "<?xml version='1.0'?>"
124 "<schema xmlns='http://www.w3.org/2001/XMLSchema'"
125 " targetNamespace='x-schema:test3.xsd'>"
126 " <element name='root'>"
128 " <sequence maxOccurs='unbounded'>"
136 #define check_ref_expr(expr, n) { \
138 ok(ref == n, "expected %i refs, got %i\n", n, ref); \
141 #define check_refs(iface, obj, n) { \
142 LONG ref = iface ## _AddRef(obj); \
143 ok(ref == n+1, "expected %i refs, got %i\n", n+1, ref); \
144 ref = iface ## _Release(obj); \
145 ok(ref == n, "expected %i refs, got %i\n", n, ref); \
148 #define ole_check(expr) { \
150 ok(r == S_OK, #expr " returned %x\n", r); \
153 #define ole_expect(expr, expect) { \
155 ok(r == (expect), #expr " returned %x, expected %x\n", r, expect); \
158 static BSTR alloced_bstrs[256];
159 static int alloced_bstrs_count;
161 static BSTR alloc_str_from_narrow(const char *str)
163 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
164 BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */
165 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
169 static BSTR _bstr_(const char *str)
171 assert(alloced_bstrs_count < sizeof(alloced_bstrs)/sizeof(alloced_bstrs[0]));
172 alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str);
173 return alloced_bstrs[alloced_bstrs_count++];
176 static void free_bstrs(void)
179 for (i = 0; i < alloced_bstrs_count; i++)
180 SysFreeString(alloced_bstrs[i]);
181 alloced_bstrs_count = 0;
184 static VARIANT _variantdoc_(void* doc)
187 V_VT(&v) = VT_DISPATCH;
188 V_DISPATCH(&v) = (IDispatch*)doc;
192 static void* _create_object(const GUID *clsid, const char *name, const IID *iid, int line)
197 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, &obj);
199 win_skip_(__FILE__,line)("failed to create %s instance: 0x%08x\n", name, hr);
204 #define _create(cls) cls, #cls
206 #define create_document(iid) _create_object(&_create(CLSID_DOMDocument), iid, __LINE__)
208 #define create_document_version(v, iid) _create_object(&_create(CLSID_DOMDocument ## v), iid, __LINE__)
210 #define create_cache(iid) _create_object(&_create(CLSID_XMLSchemaCache), iid, __LINE__)
212 #define create_cache_version(v, iid) _create_object(&_create(CLSID_XMLSchemaCache ## v), iid, __LINE__)
214 static void test_schema_refs(void)
216 IXMLDOMDocument2 *doc;
217 IXMLDOMSchemaCollection *cache;
222 doc = create_document(&IID_IXMLDOMDocument2);
226 cache = create_cache(&IID_IXMLDOMSchemaCollection);
229 IXMLDOMDocument2_Release(doc);
234 str = SysAllocString(xdr_schema_xml);
235 ole_check(IXMLDOMDocument2_loadXML(doc, str, &b));
236 ok(b == VARIANT_TRUE, "b %04x\n", b);
239 str = SysAllocString(xdr_schema_uri);
240 ole_check(IXMLDOMSchemaCollection_add(cache, str, _variantdoc_(doc)));
242 /* IXMLDOMSchemaCollection_add doesn't add a ref on doc */
243 check_refs(IXMLDOMDocument2, doc, 1);
248 ole_expect(IXMLDOMDocument2_get_schemas(doc, &v), S_FALSE);
249 ok(V_VT(&v) == VT_NULL, "vt %x\n", V_VT(&v));
251 check_ref_expr(IXMLDOMSchemaCollection_AddRef(cache), 2);
252 V_VT(&v) = VT_DISPATCH;
253 V_DISPATCH(&v) = (IDispatch*)cache;
255 /* check that putref_schemas takes a ref */
256 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
257 check_refs(IXMLDOMSchemaCollection, cache, 3);
259 VariantClear(&v); /* refs now 2 */
262 /* check that get_schemas adds a ref */
263 ole_check(IXMLDOMDocument2_get_schemas(doc, &v));
264 ok(V_VT(&v) == VT_DISPATCH, "vt %x\n", V_VT(&v));
265 check_refs(IXMLDOMSchemaCollection, cache, 3);
267 /* get_schemas doesn't release a ref if passed VT_DISPATCH - ie it doesn't call VariantClear() */
268 ole_check(IXMLDOMDocument2_get_schemas(doc, &v));
269 ok(V_VT(&v) == VT_DISPATCH, "vt %x\n", V_VT(&v));
270 check_refs(IXMLDOMSchemaCollection, cache, 4);
272 /* release the two refs returned by get_schemas */
273 check_ref_expr(IXMLDOMSchemaCollection_Release(cache), 3);
274 check_ref_expr(IXMLDOMSchemaCollection_Release(cache), 2);
276 /* check that taking another ref on the document doesn't change the schema's ref count */
277 check_ref_expr(IXMLDOMDocument2_AddRef(doc), 2);
278 check_refs(IXMLDOMSchemaCollection, cache, 2);
279 check_ref_expr(IXMLDOMDocument2_Release(doc), 1);
281 /* call putref_schema with some odd variants */
283 ole_expect(IXMLDOMDocument2_putref_schemas(doc, v), E_FAIL);
284 check_refs(IXMLDOMSchemaCollection, cache, 2);
286 /* calling with VT_EMPTY releases the schema */
288 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
289 check_refs(IXMLDOMSchemaCollection, cache, 1);
291 /* try setting with VT_UNKNOWN */
292 check_ref_expr(IXMLDOMSchemaCollection_AddRef(cache), 2);
293 V_VT(&v) = VT_UNKNOWN;
294 V_UNKNOWN(&v) = (IUnknown*)cache;
295 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
296 check_refs(IXMLDOMSchemaCollection, cache, 3);
298 VariantClear(&v); /* refs now 2 */
300 /* calling with VT_NULL releases the schema */
302 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
303 check_refs(IXMLDOMSchemaCollection, cache, 1);
307 check_ref_expr(IXMLDOMSchemaCollection_AddRef(cache), 2);
308 V_VT(&v) = VT_UNKNOWN;
309 V_UNKNOWN(&v) = (IUnknown*)cache;
310 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
311 check_refs(IXMLDOMSchemaCollection, cache, 3);
313 VariantClear(&v); /* refs now 2 */
315 /* release the final ref on the doc which should release its ref on the schema */
316 check_ref_expr(IXMLDOMDocument2_Release(doc), 0);
318 check_refs(IXMLDOMSchemaCollection, cache, 1);
319 check_ref_expr(IXMLDOMSchemaCollection_Release(cache), 0);
322 static void test_collection_refs(void)
324 IXMLDOMDocument2 *schema1, *schema2, *schema3;
325 IXMLDOMSchemaCollection *cache1, *cache2, *cache3;
329 schema1 = create_document(&IID_IXMLDOMDocument2);
330 schema2 = create_document(&IID_IXMLDOMDocument2);
331 schema3 = create_document(&IID_IXMLDOMDocument2);
333 cache1 = create_cache(&IID_IXMLDOMSchemaCollection);
334 cache2 = create_cache(&IID_IXMLDOMSchemaCollection);
335 cache3 = create_cache(&IID_IXMLDOMSchemaCollection);
337 if (!schema1 || !schema2 || !schema3 || !cache1 || !cache2 || !cache3)
339 if (schema1) IXMLDOMDocument2_Release(schema1);
340 if (schema2) IXMLDOMDocument2_Release(schema2);
341 if (schema3) IXMLDOMDocument2_Release(schema3);
343 if (cache1) IXMLDOMSchemaCollection_Release(cache1);
344 if (cache2) IXMLDOMSchemaCollection_Release(cache2);
345 if (cache3) IXMLDOMSchemaCollection_Release(cache2);
350 ole_check(IXMLDOMDocument2_loadXML(schema1, _bstr_(xdr_schema1_xml), &b));
351 ok(b == VARIANT_TRUE, "failed to load XML\n");
353 ole_check(IXMLDOMDocument2_loadXML(schema2, _bstr_(xdr_schema2_xml), &b));
354 ok(b == VARIANT_TRUE, "failed to load XML\n");
356 ole_check(IXMLDOMDocument2_loadXML(schema3, _bstr_(xdr_schema3_xml), &b));
357 ok(b == VARIANT_TRUE, "failed to load XML\n");
359 ole_check(IXMLDOMSchemaCollection_add(cache1, _bstr_(xdr_schema1_uri), _variantdoc_(schema1)));
360 ole_check(IXMLDOMSchemaCollection_add(cache2, _bstr_(xdr_schema2_uri), _variantdoc_(schema2)));
361 ole_check(IXMLDOMSchemaCollection_add(cache3, _bstr_(xdr_schema3_uri), _variantdoc_(schema3)));
363 check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
364 check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
365 check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
370 /* releasing the original doc does not affect the schema cache */
371 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache1, _bstr_(xdr_schema1_uri), (IXMLDOMNode**)&schema1));
372 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache2, _bstr_(xdr_schema2_uri), (IXMLDOMNode**)&schema2));
373 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache3, _bstr_(xdr_schema3_uri), (IXMLDOMNode**)&schema3));
375 /* we get a read-only domdoc interface, created just for us */
376 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
377 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
378 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
380 ole_expect(IXMLDOMSchemaCollection_addCollection(cache1, NULL), E_POINTER);
381 ole_check(IXMLDOMSchemaCollection_addCollection(cache2, cache1));
382 ole_check(IXMLDOMSchemaCollection_addCollection(cache3, cache2));
385 ole_check(IXMLDOMSchemaCollection_get_length(cache1, &length));
386 ok(length == 1, "expected length 1, got %i", length);
389 ole_check(IXMLDOMSchemaCollection_get_length(cache2, &length));
390 ok(length == 2, "expected length 2, got %i", length);
393 ole_check(IXMLDOMSchemaCollection_get_length(cache3, &length));
394 ok(length == 3, "expected length 3, got %i", length);
397 /* merging collections does not affect the ref count */
398 check_refs(IXMLDOMSchemaCollection, cache1, 1);
399 check_refs(IXMLDOMSchemaCollection, cache2, 1);
400 check_refs(IXMLDOMSchemaCollection, cache3, 1);
402 /* nor does it affect the domdoc instances */
403 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
404 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
405 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
407 if (schema1) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
408 if (schema2) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
409 if (schema3) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
414 /* releasing the domdoc instances doesn't change the cache */
415 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache1, _bstr_(xdr_schema1_uri), (IXMLDOMNode**)&schema1));
416 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache2, _bstr_(xdr_schema2_uri), (IXMLDOMNode**)&schema2));
417 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache3, _bstr_(xdr_schema3_uri), (IXMLDOMNode**)&schema3));
419 /* we can just get them again */
420 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
421 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
422 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
424 /* releasing the caches does not affect the domdoc instances */
425 check_ref_expr(IXMLDOMSchemaCollection_Release(cache1), 0);
426 check_ref_expr(IXMLDOMSchemaCollection_Release(cache2), 0);
427 check_ref_expr(IXMLDOMSchemaCollection_Release(cache3), 0);
429 /* they're just for us */
430 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
431 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
432 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
434 if (schema1) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
435 if (schema2) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
436 if (schema3) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
441 static void test_length(void)
443 IXMLDOMDocument2 *schema1, *schema2, *schema3;
444 IXMLDOMSchemaCollection *cache;
449 schema1 = create_document(&IID_IXMLDOMDocument2);
450 schema2 = create_document(&IID_IXMLDOMDocument2);
451 schema3 = create_document(&IID_IXMLDOMDocument2);
453 cache = create_cache(&IID_IXMLDOMSchemaCollection);
455 if (!schema1 || !schema2 || !schema3 || !cache)
457 if (schema1) IXMLDOMDocument2_Release(schema1);
458 if (schema2) IXMLDOMDocument2_Release(schema2);
459 if (schema3) IXMLDOMDocument2_Release(schema3);
461 if (cache) IXMLDOMSchemaCollection_Release(cache);
468 ole_check(IXMLDOMDocument2_loadXML(schema1, _bstr_(xdr_schema1_xml), &b));
469 ok(b == VARIANT_TRUE, "failed to load XML\n");
471 ole_check(IXMLDOMDocument2_loadXML(schema2, _bstr_(xdr_schema2_xml), &b));
472 ok(b == VARIANT_TRUE, "failed to load XML\n");
474 ole_check(IXMLDOMDocument2_loadXML(schema3, _bstr_(xdr_schema3_xml), &b));
475 ok(b == VARIANT_TRUE, "failed to load XML\n");
477 ole_expect(IXMLDOMSchemaCollection_get_length(cache, NULL), E_POINTER);
479 /* MSDN lies; removing a non-existant entry produces no error */
480 ole_check(IXMLDOMSchemaCollection_remove(cache, NULL));
481 ole_check(IXMLDOMSchemaCollection_remove(cache, _bstr_(xdr_schema1_uri)));
484 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
485 ok(length == 0, "expected length 0, got %i\n", length);
487 ole_check(IXMLDOMSchemaCollection_add(cache, _bstr_(xdr_schema1_uri), _variantdoc_(schema1)));
490 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
491 ok(length == 1, "expected length 1, got %i\n", length);
493 ole_check(IXMLDOMSchemaCollection_add(cache, _bstr_(xdr_schema2_uri), _variantdoc_(schema2)));
496 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
497 ok(length == 2, "expected length 2, got %i\n", length);
499 ole_check(IXMLDOMSchemaCollection_add(cache, _bstr_(xdr_schema3_uri), _variantdoc_(schema3)));
502 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
503 ok(length == 3, "expected length 3, got %i\n", length);
505 /* adding with VT_NULL is the same as removing */
507 ole_check(IXMLDOMSchemaCollection_add(cache, _bstr_(xdr_schema1_uri), v));
510 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
511 ok(length == 2, "expected length 2, got %i\n", length);
513 ole_check(IXMLDOMSchemaCollection_remove(cache, _bstr_(xdr_schema2_uri)));
516 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
517 ok(length == 1, "expected length 1, got %i\n", length);
519 ole_check(IXMLDOMSchemaCollection_remove(cache, _bstr_(xdr_schema3_uri)));
522 ole_check(IXMLDOMSchemaCollection_get_length(cache, &length));
523 ok(length == 0, "expected length 0, got %i\n", length);
525 IXMLDOMDocument2_Release(schema1);
526 IXMLDOMDocument2_Release(schema2);
527 IXMLDOMDocument2_Release(schema3);
528 IXMLDOMSchemaCollection_Release(cache);
537 r = CoInitialize( NULL );
538 ok( r == S_OK, "failed to init com\n");
541 test_collection_refs();