msxml3/tests: Internal schema doc storage tests.
[wine.git] / dlls / msxml3 / tests / schema.c
blob0731d59c8cdc4d862a2153f3b2c4af51a8b69c34
1 /*
2 * Schema test
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
22 #include <stdio.h>
23 #include <assert.h>
24 #define COBJMACROS
26 #include "initguid.h"
27 #include "windows.h"
28 #include "ole2.h"
29 #include "xmldom.h"
30 #include "msxml2.h"
31 #include "dispex.h"
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'"
48 " name='test1.xdr'>"
49 " <ElementType name='x' dt:type='boolean'/>"
50 " <ElementType name='y'>"
51 " <datatype dt:type='int'/>"
52 " </ElementType>"
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'/>"
58 " </ElementType>"
59 "</Schema>";
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'"
66 " name='test2.xdr'>"
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'/>"
74 " </ElementType>"
75 "</Schema>";
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'"
82 " name='test3.xdr'>"
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'/>"
90 " </ElementType>"
91 "</Schema>";
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'>"
99 " <complexType>"
100 " <sequence maxOccurs='unbounded'>"
101 " <any/>"
102 " </sequence>"
103 " </complexType>"
104 " </element>"
105 "</schema>";
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'>"
113 " <complexType>"
114 " <sequence maxOccurs='unbounded'>"
115 " <any/>"
116 " </sequence>"
117 " </complexType>"
118 " </element>"
119 "</schema>";
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'>"
127 " <complexType>"
128 " <sequence maxOccurs='unbounded'>"
129 " <any/>"
130 " </sequence>"
131 " </complexType>"
132 " </element>"
133 "</schema>";
136 #define check_ref_expr(expr, n) { \
137 LONG ref = expr; \
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) { \
149 HRESULT r = expr; \
150 ok(r == S_OK, #expr " returned %x\n", r); \
153 #define ole_expect(expr, expect) { \
154 HRESULT r = expr; \
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);
166 return ret;
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)
178 int i;
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)
186 VARIANT v;
187 V_VT(&v) = VT_DISPATCH;
188 V_DISPATCH(&v) = (IDispatch*)doc;
189 return v;
192 static void* _create_object(const GUID *clsid, const char *name, const IID *iid, int line)
194 void *obj = NULL;
195 HRESULT hr;
197 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, iid, &obj);
198 if (hr != S_OK)
199 win_skip_(__FILE__,line)("failed to create %s instance: 0x%08x\n", name, hr);
201 return obj;
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;
218 VARIANT v;
219 VARIANT_BOOL b;
220 BSTR str;
222 doc = create_document(&IID_IXMLDOMDocument2);
223 if (!doc)
224 return;
226 cache = create_cache(&IID_IXMLDOMSchemaCollection);
227 if(!cache)
229 IXMLDOMDocument2_Release(doc);
230 return;
233 VariantInit(&v);
234 str = SysAllocString(xdr_schema_xml);
235 ole_check(IXMLDOMDocument2_loadXML(doc, str, &b));
236 ok(b == VARIANT_TRUE, "b %04x\n", b);
237 SysFreeString(str);
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);
245 SysFreeString(str);
247 V_VT(&v) = VT_INT;
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 */
261 V_VT(&v) = VT_INT;
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 */
282 V_VT(&v) = VT_INT;
283 ole_expect(IXMLDOMDocument2_putref_schemas(doc, v), E_FAIL);
284 check_refs(IXMLDOMSchemaCollection, cache, 2);
286 /* calling with VT_EMPTY releases the schema */
287 V_VT(&v) = VT_EMPTY;
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 */
301 V_VT(&v) = VT_NULL;
302 ole_check(IXMLDOMDocument2_putref_schemas(doc, v));
303 check_refs(IXMLDOMSchemaCollection, cache, 1);
305 /* refs now 1 */
306 /* set again */
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;
326 VARIANT_BOOL b;
328 schema1 = create_document(&IID_IXMLDOMDocument2);
329 schema2 = create_document(&IID_IXMLDOMDocument2);
330 schema3 = create_document(&IID_IXMLDOMDocument2);
332 cache1 = create_cache(&IID_IXMLDOMSchemaCollection);
333 cache2 = create_cache(&IID_IXMLDOMSchemaCollection);
334 cache3 = create_cache(&IID_IXMLDOMSchemaCollection);
336 if (!schema1 || !schema2 || !schema3 || !cache1 || !cache2 || !cache3)
338 if (schema1) IXMLDOMDocument2_Release(schema1);
339 if (schema2) IXMLDOMDocument2_Release(schema2);
340 if (schema3) IXMLDOMDocument2_Release(schema3);
342 if (cache1) IXMLDOMSchemaCollection_Release(cache1);
343 if (cache2) IXMLDOMSchemaCollection_Release(cache2);
344 if (cache3) IXMLDOMSchemaCollection_Release(cache2);
346 return;
349 ole_check(IXMLDOMDocument2_loadXML(schema1, _bstr_(xdr_schema1_xml), &b));
350 ok(b == VARIANT_TRUE, "failed to load XML\n");
352 ole_check(IXMLDOMDocument2_loadXML(schema2, _bstr_(xdr_schema2_xml), &b));
353 ok(b == VARIANT_TRUE, "failed to load XML\n");
355 ole_check(IXMLDOMDocument2_loadXML(schema3, _bstr_(xdr_schema3_xml), &b));
356 ok(b == VARIANT_TRUE, "failed to load XML\n");
358 ole_check(IXMLDOMSchemaCollection_add(cache1, _bstr_(xdr_schema1_uri), _variantdoc_(schema1)));
359 ole_check(IXMLDOMSchemaCollection_add(cache2, _bstr_(xdr_schema2_uri), _variantdoc_(schema2)));
360 ole_check(IXMLDOMSchemaCollection_add(cache3, _bstr_(xdr_schema3_uri), _variantdoc_(schema3)));
362 check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
363 check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
364 check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
365 schema1 = NULL;
366 schema2 = NULL;
367 schema3 = NULL;
369 /* releasing the original doc does not affect the schema cache */
370 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache1, _bstr_(xdr_schema1_uri), (IXMLDOMNode**)&schema1));
371 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache2, _bstr_(xdr_schema2_uri), (IXMLDOMNode**)&schema2));
372 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache3, _bstr_(xdr_schema3_uri), (IXMLDOMNode**)&schema3));
374 /* we get a read-only domdoc interface, created just for us */
375 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
376 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
377 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
379 todo_wine ole_check(IXMLDOMSchemaCollection_addCollection(cache2, cache1));
380 todo_wine ole_check(IXMLDOMSchemaCollection_addCollection(cache3, cache2));
382 /* merging collections does not affect the ref count */
383 check_refs(IXMLDOMSchemaCollection, cache1, 1);
384 check_refs(IXMLDOMSchemaCollection, cache2, 1);
385 check_refs(IXMLDOMSchemaCollection, cache3, 1);
387 /* nor does it affect the domdoc instances */
388 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
389 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
390 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
392 if (schema1) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
393 if (schema2) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
394 if (schema3) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
395 schema1 = NULL;
396 schema2 = NULL;
397 schema3 = NULL;
399 /* releasing the domdoc instances doesn't change the cache */
400 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache1, _bstr_(xdr_schema1_uri), (IXMLDOMNode**)&schema1));
401 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache2, _bstr_(xdr_schema2_uri), (IXMLDOMNode**)&schema2));
402 todo_wine ole_check(IXMLDOMSchemaCollection_get(cache3, _bstr_(xdr_schema3_uri), (IXMLDOMNode**)&schema3));
404 /* we can just get them again */
405 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
406 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
407 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
409 /* releasing the caches does not affect the domdoc instances */
410 check_ref_expr(IXMLDOMSchemaCollection_Release(cache1), 0);
411 check_ref_expr(IXMLDOMSchemaCollection_Release(cache2), 0);
412 check_ref_expr(IXMLDOMSchemaCollection_Release(cache3), 0);
414 /* they're just for us */
415 if (schema1) todo_wine check_refs(IXMLDOMDocument2, schema1, 1);
416 if (schema2) todo_wine check_refs(IXMLDOMDocument2, schema2, 1);
417 if (schema3) todo_wine check_refs(IXMLDOMDocument2, schema3, 1);
419 if (schema1) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema1), 0);
420 if (schema2) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema2), 0);
421 if (schema3) todo_wine check_ref_expr(IXMLDOMDocument2_Release(schema3), 0);
423 free_bstrs();
426 START_TEST(schema)
428 HRESULT r;
430 r = CoInitialize( NULL );
431 ok( r == S_OK, "failed to init com\n");
433 test_schema_refs();
434 test_collection_refs();
436 CoUninitialize();