msxml3: Properly escape character data in text nodes.
[wine] / dlls / msxml3 / tests / saxreader.c
1 /*
2  * XML test
3  *
4  * Copyright 2008 Piotr Caban
5  * Copyright 2011 Thomas Mullaly
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include "windows.h"
29 #include "ole2.h"
30 #include "msxml2.h"
31 #include "msxml2did.h"
32 #include "ocidl.h"
33 #include "dispex.h"
34
35 #include "wine/test.h"
36
37 #define EXPECT_HR(hr,hr_exp) \
38     ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)
39
40 #define EXPECT_REF(obj,ref) _expect_ref((IUnknown*)obj, ref, __LINE__)
41 static void _expect_ref(IUnknown* obj, ULONG ref, int line)
42 {
43     ULONG rc = IUnknown_AddRef(obj);
44     IUnknown_Release(obj);
45     ok_(__FILE__,line)(rc-1 == ref, "expected refcount %d, got %d\n", ref, rc-1);
46 }
47
48 static BSTR alloc_str_from_narrow(const char *str)
49 {
50     int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
51     BSTR ret = SysAllocStringLen(NULL, len - 1);  /* NUL character added automatically */
52     MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
53     return ret;
54 }
55
56 static BSTR alloced_bstrs[512];
57 static int alloced_bstrs_count;
58
59 static BSTR _bstr_(const char *str)
60 {
61     assert(alloced_bstrs_count < sizeof(alloced_bstrs)/sizeof(alloced_bstrs[0]));
62     alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str);
63     return alloced_bstrs[alloced_bstrs_count++];
64 }
65
66 static void free_bstrs(void)
67 {
68     int i;
69     for (i = 0; i < alloced_bstrs_count; i++)
70         SysFreeString(alloced_bstrs[i]);
71     alloced_bstrs_count = 0;
72 }
73
74 typedef enum _CH {
75     CH_ENDTEST,
76     CH_PUTDOCUMENTLOCATOR,
77     CH_STARTDOCUMENT,
78     CH_ENDDOCUMENT,
79     CH_STARTPREFIXMAPPING,
80     CH_ENDPREFIXMAPPING,
81     CH_STARTELEMENT,
82     CH_ENDELEMENT,
83     CH_CHARACTERS,
84     CH_IGNORABLEWHITESPACE,
85     CH_PROCESSINGINSTRUCTION,
86     CH_SKIPPEDENTITY,
87     EH_ERROR,
88     EH_FATALERROR,
89     EG_IGNORABLEWARNING
90 } CH;
91
92 static const WCHAR szSimpleXML[] = {
93 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','\"','1','.','0','\"',' ','?','>','\n',
94 '<','B','a','n','k','A','c','c','o','u','n','t','>','\n',
95 ' ',' ',' ','<','N','u','m','b','e','r','>','1','2','3','4','<','/','N','u','m','b','e','r','>','\n',
96 ' ',' ',' ','<','N','a','m','e','>','C','a','p','t','a','i','n',' ','A','h','a','b','<','/','N','a','m','e','>','\n',
97 '<','/','B','a','n','k','A','c','c','o','u','n','t','>','\n','\0'
98 };
99
100 static const WCHAR szCarriageRetTest[] = {
101 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"','?','>','\r','\n',
102 '<','B','a','n','k','A','c','c','o','u','n','t','>','\r','\n',
103 '\t','<','N','u','m','b','e','r','>','1','2','3','4','<','/','N','u','m','b','e','r','>','\r','\n',
104 '\t','<','N','a','m','e','>','C','a','p','t','a','i','n',' ','A','h','a','b','<','/','N','a','m','e','>','\r','\n',
105 '<','/','B','a','n','k','A','c','c','o','u','n','t','>','\r','\n','\0'
106 };
107
108 static const WCHAR szUtf16XML[] = {
109 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"',' ',
110 'e','n','c','o','d','i','n','g','=','"','U','T','F','-','1','6','"',' ',
111 's','t','a','n','d','a','l','o','n','e','=','"','n','o','"','?','>','\r','\n'
112 };
113
114 static const CHAR szUtf16BOM[] = {0xff, 0xfe};
115
116 static const CHAR szUtf8XML[] =
117 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n";
118
119 static const char utf8xml2[] =
120 "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\r\n";
121
122 static const CHAR szTestXML[] =
123 "<?xml version=\"1.0\" ?>\n"
124 "<BankAccount>\n"
125 "   <Number>1234</Number>\n"
126 "   <Name>Captain Ahab</Name>\n"
127 "</BankAccount>\n";
128
129 static const CHAR szTestAttributes[] =
130 "<?xml version=\"1.0\" ?>\n"
131 "<document xmlns:test=\"prefix_test\" xmlns=\"prefix\" test:arg1=\"arg1\" arg2=\"arg2\" test:ar3=\"arg3\">\n"
132 "<node1 xmlns:p=\"test\" />"
133 "</document>\n";
134
135 typedef struct _contenthandlercheck {
136     CH id;
137     int line;
138     int column;
139     int line_v6;
140     int column_v6;
141     const char *arg1;
142     const char *arg2;
143     const char *arg3;
144     HRESULT ret;
145 } content_handler_test;
146
147 static content_handler_test contentHandlerTest1[] = {
148     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0 },
149     { CH_STARTDOCUMENT, 0, 0, 1, 22 },
150     { CH_STARTELEMENT, 2, 14, 2, 13, "", "BankAccount", "BankAccount" },
151     { CH_CHARACTERS, 2, 14, 3, 4, "\n   " },
152     { CH_STARTELEMENT, 3, 12, 3, 11, "", "Number", "Number" },
153     { CH_CHARACTERS, 3, 12, 3, 16, "1234" },
154     { CH_ENDELEMENT, 3, 18, 3, 24, "", "Number", "Number" },
155     { CH_CHARACTERS, 3, 25, 4, 4, "\n   " },
156     { CH_STARTELEMENT, 4, 10, 4, 9, "", "Name", "Name" },
157     { CH_CHARACTERS, 4, 10, 4, 22, "Captain Ahab" },
158     { CH_ENDELEMENT, 4, 24, 4, 28, "", "Name", "Name" },
159     { CH_CHARACTERS, 4, 29, 5, 1, "\n" },
160     { CH_ENDELEMENT, 5, 3, 5, 14, "", "BankAccount", "BankAccount" },
161     { CH_ENDDOCUMENT, 0, 0, 6, 0 },
162     { CH_ENDTEST }
163 };
164
165 static content_handler_test contentHandlerTest2[] = {
166     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0 },
167     { CH_STARTDOCUMENT, 0, 0, 1, 21 },
168     { CH_STARTELEMENT, 2, 14, 2, 13, "", "BankAccount", "BankAccount" },
169     { CH_CHARACTERS, 2, 14, 3, 0, "\n" },
170     { CH_CHARACTERS, 2, 16, 3, 2, "\t" },
171     { CH_STARTELEMENT, 3, 10, 3, 9, "", "Number", "Number" },
172     { CH_CHARACTERS, 3, 10, 3, 14, "1234" },
173     { CH_ENDELEMENT, 3, 16, 3, 22, "", "Number", "Number" },
174     { CH_CHARACTERS, 3, 23, 4, 0, "\n" },
175     { CH_CHARACTERS, 3, 25, 4, 2, "\t" },
176     { CH_STARTELEMENT, 4, 8, 4, 7, "", "Name", "Name" },
177     { CH_CHARACTERS, 4, 8, 4, 20, "Captain Ahab" },
178     { CH_ENDELEMENT, 4, 22, 4, 26, "", "Name", "Name" },
179     { CH_CHARACTERS, 4, 27, 5, 0, "\n" },
180     { CH_ENDELEMENT, 5, 3, 5, 14, "", "BankAccount", "BankAccount" },
181     { CH_ENDDOCUMENT, 0, 0, 6, 0 },
182     { CH_ENDTEST }
183 };
184
185 static content_handler_test contentHandlerTestError[] = {
186     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0, NULL, NULL, NULL, E_FAIL },
187     { EH_FATALERROR, 0, 0, 0, 0, NULL, NULL, NULL, E_FAIL },
188     { CH_ENDTEST }
189 };
190
191 static content_handler_test contentHandlerTestCallbackResults[] = {
192     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0, NULL, NULL, NULL, S_FALSE },
193     { CH_STARTDOCUMENT, 0, 0, 1, 22, NULL, NULL, NULL, S_FALSE },
194     { EH_FATALERROR, 0, 0, 0, 0, NULL, NULL, NULL, S_FALSE },
195     { CH_ENDTEST }
196 };
197
198 static content_handler_test contentHandlerTestCallbackResult6[] = {
199     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0, NULL, NULL, NULL, S_FALSE },
200     { CH_STARTDOCUMENT, 0, 0, 1, 22, NULL, NULL, NULL, S_FALSE },
201     { CH_STARTELEMENT, 2, 14, 2, 13, "", "BankAccount", "BankAccount", S_FALSE },
202     { CH_CHARACTERS, 2, 14, 3, 4, "\n   ", NULL, NULL, S_FALSE },
203     { CH_STARTELEMENT, 3, 12, 3, 11, "", "Number", "Number", S_FALSE },
204     { CH_CHARACTERS, 3, 12, 3, 16, "1234", NULL, NULL, S_FALSE },
205     { CH_ENDELEMENT, 3, 18, 3, 24, "", "Number", "Number", S_FALSE },
206     { CH_CHARACTERS, 3, 25, 4, 4, "\n   ", NULL, NULL, S_FALSE },
207     { CH_STARTELEMENT, 4, 10, 4, 9, "", "Name", "Name", S_FALSE },
208     { CH_CHARACTERS, 4, 10, 4, 22, "Captain Ahab", NULL, NULL, S_FALSE },
209     { CH_ENDELEMENT, 4, 24, 4, 28, "", "Name", "Name", S_FALSE },
210     { CH_CHARACTERS, 4, 29, 5, 1, "\n", NULL, NULL, S_FALSE },
211     { CH_ENDELEMENT, 5, 3, 5, 14, "", "BankAccount", "BankAccount", S_FALSE },
212     { CH_ENDDOCUMENT, 0, 0, 6, 0, NULL, NULL, NULL, S_FALSE },
213     { CH_ENDTEST }
214 };
215
216 static content_handler_test contentHandlerTestAttributes[] = {
217     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0 },
218     { CH_STARTDOCUMENT, 0, 0, 1, 22 },
219     { CH_STARTPREFIXMAPPING, 2, 96, 2, 95, "test", "prefix_test" },
220     { CH_STARTPREFIXMAPPING, 2, 96, 2, 95, "", "prefix" },
221     { CH_STARTELEMENT, 2, 96, 2, 95, "prefix", "document", "document" },
222     { CH_CHARACTERS, 2, 96, 3, 1, "\n" },
223     { CH_STARTPREFIXMAPPING, 3, 25, 3, 24, "p", "test" },
224     { CH_STARTELEMENT, 3, 25, 3, 24, "prefix", "node1", "node1" },
225     { CH_ENDELEMENT, 3, 25, 3, 24, "prefix", "node1", "node1" },
226     { CH_ENDPREFIXMAPPING, 3, 25, 3, 24, "p" },
227     { CH_ENDELEMENT, 3, 27, 3, 35, "prefix", "document", "document" },
228     { CH_ENDPREFIXMAPPING, 3, 27, 3, 35, "" },
229     { CH_ENDPREFIXMAPPING, 3, 27, 3, 35, "test" },
230     { CH_ENDDOCUMENT, 0, 0, 4, 0 },
231     { CH_ENDTEST }
232 };
233
234 static content_handler_test contentHandlerTestAttributes6[] = {
235     { CH_PUTDOCUMENTLOCATOR, 0, 0, 1, 0 },
236     { CH_STARTDOCUMENT, 0, 0, 1, 22 },
237     { CH_STARTPREFIXMAPPING, 2, 96, 2, 95, "test", "prefix_test" },
238     { CH_STARTPREFIXMAPPING, 2, 96, 2, 95, "", "prefix" },
239     { CH_STARTELEMENT, 2, 96, 2, 95, "prefix", "document", "document" },
240     { CH_CHARACTERS, 2, 96, 3, 1, "\n" },
241     { CH_STARTPREFIXMAPPING, 3, 25, 3, 24, "p", "test" },
242     { CH_STARTELEMENT, 3, 25, 3, 24, "prefix", "node1", "node1" },
243     { CH_ENDELEMENT, 3, 25, 3, 24, "prefix", "node1", "node1" },
244     { CH_ENDPREFIXMAPPING, 3, 25, 3, 24, "p" },
245     { CH_ENDELEMENT, 3, 27, 3, 35, "prefix", "document", "document" },
246     { CH_ENDPREFIXMAPPING, 3, 27, 3, 35, "test" },
247     { CH_ENDPREFIXMAPPING, 3, 27, 3, 35, "" },
248     { CH_ENDDOCUMENT, 0, 0, 4, 0 },
249     { CH_ENDTEST }
250 };
251
252 static content_handler_test *expectCall;
253 static ISAXLocator *locator;
254 int msxml_version;
255
256 static void test_saxstr(unsigned line, const WCHAR *szStr, int nStr, const char *szTest)
257 {
258     WCHAR buf[1024];
259     int len;
260
261     if(!szTest) {
262         ok_(__FILE__,line) (szStr == NULL, "szStr != NULL\n");
263         ok_(__FILE__,line) (nStr == 0, "nStr = %d, expected 0\n", nStr);
264         return;
265     }
266
267     len = strlen(szTest);
268     ok_(__FILE__,line) (len == nStr, "nStr = %d, expected %d (%s)\n", nStr, len, szTest);
269     if(len != nStr)
270         return;
271
272     MultiByteToWideChar(CP_ACP, 0, szTest, -1, buf, sizeof(buf)/sizeof(WCHAR));
273     ok_(__FILE__,line) (!memcmp(szStr, buf, len*sizeof(WCHAR)), "unexpected szStr %s, expected %s\n",
274                         wine_dbgstr_wn(szStr, nStr), szTest);
275 }
276
277 static BOOL test_expect_call(CH id)
278 {
279     ok(expectCall->id == id, "unexpected call %d, expected %d\n", id, expectCall->id);
280     return expectCall->id == id;
281 }
282
283 static void test_locator(unsigned line, int loc_line, int loc_column)
284 {
285     int rcolumn, rline;
286     ISAXLocator_getLineNumber(locator, &rline);
287     ISAXLocator_getColumnNumber(locator, &rcolumn);
288
289     ok_(__FILE__,line) (rline == loc_line,
290             "unexpected line %d, expected %d\n", rline, loc_line);
291     ok_(__FILE__,line) (rcolumn == loc_column,
292             "unexpected column %d, expected %d\n", rcolumn, loc_column);
293 }
294
295 static HRESULT WINAPI contentHandler_QueryInterface(
296         ISAXContentHandler* iface,
297         REFIID riid,
298         void **ppvObject)
299 {
300     *ppvObject = NULL;
301
302     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXContentHandler))
303     {
304         *ppvObject = iface;
305     }
306     else
307     {
308         return E_NOINTERFACE;
309     }
310
311     return S_OK;
312 }
313
314 static ULONG WINAPI contentHandler_AddRef(
315         ISAXContentHandler* iface)
316 {
317     return 2;
318 }
319
320 static ULONG WINAPI contentHandler_Release(
321         ISAXContentHandler* iface)
322 {
323     return 1;
324 }
325
326 static HRESULT WINAPI contentHandler_putDocumentLocator(
327         ISAXContentHandler* iface,
328         ISAXLocator *pLocator)
329 {
330     HRESULT hr;
331
332     if(!test_expect_call(CH_PUTDOCUMENTLOCATOR))
333         return E_FAIL;
334
335     locator = pLocator;
336     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
337             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
338
339     if(msxml_version >= 6) {
340         ISAXAttributes *attr, *attr1;
341         IMXAttributes *mxattr;
342
343         EXPECT_REF(pLocator, 1);
344         hr = ISAXLocator_QueryInterface(pLocator, &IID_ISAXAttributes, (void**)&attr);
345         EXPECT_HR(hr, S_OK);
346         EXPECT_REF(pLocator, 2);
347         hr = ISAXLocator_QueryInterface(pLocator, &IID_ISAXAttributes, (void**)&attr1);
348         EXPECT_HR(hr, S_OK);
349         EXPECT_REF(pLocator, 3);
350         ok(attr == attr1, "got %p, %p\n", attr, attr1);
351
352         hr = ISAXAttributes_QueryInterface(attr, &IID_IMXAttributes, (void**)&mxattr);
353         EXPECT_HR(hr, E_NOINTERFACE);
354
355         ISAXAttributes_Release(attr);
356         ISAXAttributes_Release(attr1);
357     }
358
359     return (expectCall++)->ret;
360 }
361
362 static ISAXAttributes *test_attr_ptr;
363 static HRESULT WINAPI contentHandler_startDocument(
364         ISAXContentHandler* iface)
365 {
366     if(!test_expect_call(CH_STARTDOCUMENT))
367         return E_FAIL;
368
369     test_attr_ptr = NULL;
370     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
371             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
372
373     return (expectCall++)->ret;
374 }
375
376 static HRESULT WINAPI contentHandler_endDocument(
377         ISAXContentHandler* iface)
378 {
379     if(!test_expect_call(CH_ENDDOCUMENT))
380         return E_FAIL;
381
382     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
383             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
384
385     return (expectCall++)->ret;
386 }
387
388 static HRESULT WINAPI contentHandler_startPrefixMapping(
389         ISAXContentHandler* iface,
390         const WCHAR *pPrefix,
391         int nPrefix,
392         const WCHAR *pUri,
393         int nUri)
394 {
395     if(!test_expect_call(CH_STARTPREFIXMAPPING))
396         return E_FAIL;
397
398     test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
399     test_saxstr(__LINE__, pUri, nUri, expectCall->arg2);
400     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
401             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
402
403     return (expectCall++)->ret;
404 }
405
406 static HRESULT WINAPI contentHandler_endPrefixMapping(
407         ISAXContentHandler* iface,
408         const WCHAR *pPrefix,
409         int nPrefix)
410 {
411     if(!test_expect_call(CH_ENDPREFIXMAPPING))
412         return E_FAIL;
413
414     test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
415     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
416             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
417
418     return (expectCall++)->ret;
419 }
420
421 static HRESULT WINAPI contentHandler_startElement(
422         ISAXContentHandler* iface,
423         const WCHAR *pNamespaceUri,
424         int nNamespaceUri,
425         const WCHAR *pLocalName,
426         int nLocalName,
427         const WCHAR *pQName,
428         int nQName,
429         ISAXAttributes *pAttr)
430 {
431     int len;
432     HRESULT hres;
433
434     if(!test_expect_call(CH_STARTELEMENT))
435         return E_FAIL;
436
437     test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
438     test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
439     test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
440     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
441             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
442
443     if(!test_attr_ptr)
444         test_attr_ptr = pAttr;
445     ok(test_attr_ptr == pAttr, "Multiple ISAXAttributes instances are used (%p %p)\n", test_attr_ptr, pAttr);
446
447     if(expectCall == contentHandlerTestAttributes+4) {
448         const WCHAR *uri_ptr = NULL;
449         int i;
450         /* msxml3 returns attributes and namespaces in the input order */
451         hres = ISAXAttributes_getLength(pAttr, &len);
452         ok(hres == S_OK, "getLength returned %x\n", hres);
453         ok(len == 5, "Incorrect number of attributes: %d\n", len);
454         ok(msxml_version < 6, "wrong msxml_version: %d\n", msxml_version);
455
456         for(i=0; i<len; i++) {
457             hres = ISAXAttributes_getName(pAttr, i, &pNamespaceUri, &nNamespaceUri,
458                     &pLocalName, &nLocalName, &pQName, &nQName);
459             ok(hres == S_OK, "getName returned %x\n", hres);
460
461             if(nQName == 4) {
462                 todo_wine ok(i==3, "Incorrect attributes order\n");
463                 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "");
464                 test_saxstr(__LINE__, pLocalName, nLocalName, "arg2");
465                 test_saxstr(__LINE__, pQName, nQName, "arg2");
466             } else if(nQName == 5) {
467                 todo_wine ok(i==1, "Incorrect attributes order\n");
468                 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "");
469                 test_saxstr(__LINE__, pLocalName, nLocalName, "");
470                 test_saxstr(__LINE__, pQName, nQName, "xmlns");
471             } else if(nQName == 8) {
472                 todo_wine ok(i==4, "Incorrect attributes order\n");
473                 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "prefix_test");
474                 test_saxstr(__LINE__, pLocalName, nLocalName, "ar3");
475                 test_saxstr(__LINE__, pQName, nQName, "test:ar3");
476                 ok(uri_ptr == pNamespaceUri, "Incorrect NamespaceUri pointer\n");
477             } else if(nQName == 9) {
478                 todo_wine ok(i==2, "Incorrect attributes order\n");
479                 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "prefix_test");
480                 test_saxstr(__LINE__, pLocalName, nLocalName, "arg1");
481                 test_saxstr(__LINE__, pQName, nQName, "test:arg1");
482                 uri_ptr = pNamespaceUri;
483             } else if(nQName == 10) {
484                 todo_wine ok(i==0, "Incorrect attributes order\n");
485                 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "");
486                 test_saxstr(__LINE__, pLocalName, nLocalName, "");
487                 test_saxstr(__LINE__, pQName, nQName, "xmlns:test");
488             } else {
489                 ok(0, "Unexpected attribute\n");
490             }
491         }
492     } else if(expectCall == contentHandlerTestAttributes6+4) {
493         const WCHAR *uri_ptr;
494
495         /* msxml6 returns attributes first and then namespaces */
496         hres = ISAXAttributes_getLength(pAttr, &len);
497         ok(hres == S_OK, "getLength returned %x\n", hres);
498         ok(len == 5, "Incorrect number of attributes: %d\n", len);
499         ok(msxml_version >= 6, "wrong msxml_version: %d\n", msxml_version);
500
501         hres = ISAXAttributes_getName(pAttr, 0, &pNamespaceUri, &nNamespaceUri,
502                 &pLocalName, &nLocalName, &pQName, &nQName);
503         ok(hres == S_OK, "getName returned %x\n", hres);
504         test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "prefix_test");
505         test_saxstr(__LINE__, pLocalName, nLocalName, "arg1");
506         test_saxstr(__LINE__, pQName, nQName, "test:arg1");
507         uri_ptr = pNamespaceUri;
508
509         hres = ISAXAttributes_getName(pAttr, 1, &pNamespaceUri, &nNamespaceUri,
510                 &pLocalName, &nLocalName, &pQName, &nQName);
511         ok(hres == S_OK, "getName returned %x\n", hres);
512         test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "");
513         test_saxstr(__LINE__, pLocalName, nLocalName, "arg2");
514         test_saxstr(__LINE__, pQName, nQName, "arg2");
515
516         hres = ISAXAttributes_getName(pAttr, 2, &pNamespaceUri, &nNamespaceUri,
517                 &pLocalName, &nLocalName, &pQName, &nQName);
518         ok(hres == S_OK, "getName returned %x\n", hres);
519         test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "prefix_test");
520         test_saxstr(__LINE__, pLocalName, nLocalName, "ar3");
521         test_saxstr(__LINE__, pQName, nQName, "test:ar3");
522         ok(uri_ptr == pNamespaceUri, "Incorrect NamespaceUri pointer\n");
523
524         hres = ISAXAttributes_getName(pAttr, 3, &pNamespaceUri, &nNamespaceUri,
525                 &pLocalName, &nLocalName, &pQName, &nQName);
526         ok(hres == S_OK, "getName returned %x\n", hres);
527         test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "http://www.w3.org/2000/xmlns/");
528         test_saxstr(__LINE__, pLocalName, nLocalName, "");
529         test_saxstr(__LINE__, pQName, nQName, "xmlns:test");
530
531         hres = ISAXAttributes_getName(pAttr, 4, &pNamespaceUri, &nNamespaceUri,
532                 &pLocalName, &nLocalName, &pQName, &nQName);
533         ok(hres == S_OK, "getName returned %x\n", hres);
534         test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, "http://www.w3.org/2000/xmlns/");
535         test_saxstr(__LINE__, pLocalName, nLocalName, "");
536         test_saxstr(__LINE__, pQName, nQName, "xmlns");
537     }
538
539     return (expectCall++)->ret;
540 }
541
542 static HRESULT WINAPI contentHandler_endElement(
543         ISAXContentHandler* iface,
544         const WCHAR *pNamespaceUri,
545         int nNamespaceUri,
546         const WCHAR *pLocalName,
547         int nLocalName,
548         const WCHAR *pQName,
549         int nQName)
550 {
551     if(!test_expect_call(CH_ENDELEMENT))
552         return E_FAIL;
553
554     test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
555     test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
556     test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
557     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
558             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
559
560     return (expectCall++)->ret;
561 }
562
563 static HRESULT WINAPI contentHandler_characters(
564         ISAXContentHandler* iface,
565         const WCHAR *pChars,
566         int nChars)
567 {
568     if(!test_expect_call(CH_CHARACTERS))
569         return E_FAIL;
570
571     test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
572     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
573             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
574
575     return (expectCall++)->ret;
576 }
577
578 static HRESULT WINAPI contentHandler_ignorableWhitespace(
579         ISAXContentHandler* iface,
580         const WCHAR *pChars,
581         int nChars)
582 {
583     if(!test_expect_call(CH_IGNORABLEWHITESPACE))
584         return E_FAIL;
585
586     test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
587     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
588             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
589
590     return (expectCall++)->ret;
591 }
592
593 static HRESULT WINAPI contentHandler_processingInstruction(
594         ISAXContentHandler* iface,
595         const WCHAR *pTarget,
596         int nTarget,
597         const WCHAR *pData,
598         int nData)
599 {
600     if(!test_expect_call(CH_PROCESSINGINSTRUCTION))
601         return E_FAIL;
602
603     test_saxstr(__LINE__, pTarget, nTarget, expectCall->arg1);
604     test_saxstr(__LINE__, pData, nData, expectCall->arg2);
605     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
606             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
607
608     return (expectCall++)->ret;
609 }
610
611 static HRESULT WINAPI contentHandler_skippedEntity(
612         ISAXContentHandler* iface,
613         const WCHAR *pName,
614         int nName)
615 {
616     if(!test_expect_call(CH_SKIPPEDENTITY))
617         return E_FAIL;
618
619     test_saxstr(__LINE__, pName, nName, expectCall->arg1);
620     test_locator(__LINE__, msxml_version>=6 ? expectCall->line_v6 : expectCall->line,
621             msxml_version>=6 ? expectCall->column_v6 : expectCall->column);
622
623     return (expectCall++)->ret;
624 }
625
626
627 static const ISAXContentHandlerVtbl contentHandlerVtbl =
628 {
629     contentHandler_QueryInterface,
630     contentHandler_AddRef,
631     contentHandler_Release,
632     contentHandler_putDocumentLocator,
633     contentHandler_startDocument,
634     contentHandler_endDocument,
635     contentHandler_startPrefixMapping,
636     contentHandler_endPrefixMapping,
637     contentHandler_startElement,
638     contentHandler_endElement,
639     contentHandler_characters,
640     contentHandler_ignorableWhitespace,
641     contentHandler_processingInstruction,
642     contentHandler_skippedEntity
643 };
644
645 static ISAXContentHandler contentHandler = { &contentHandlerVtbl };
646
647 static HRESULT WINAPI isaxerrorHandler_QueryInterface(
648         ISAXErrorHandler* iface,
649         REFIID riid,
650         void **ppvObject)
651 {
652     *ppvObject = NULL;
653
654     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXErrorHandler))
655     {
656         *ppvObject = iface;
657     }
658     else
659     {
660         return E_NOINTERFACE;
661     }
662
663     return S_OK;
664 }
665
666 static ULONG WINAPI isaxerrorHandler_AddRef(
667         ISAXErrorHandler* iface)
668 {
669     return 2;
670 }
671
672 static ULONG WINAPI isaxerrorHandler_Release(
673         ISAXErrorHandler* iface)
674 {
675     return 1;
676 }
677
678 static HRESULT WINAPI isaxerrorHandler_error(
679         ISAXErrorHandler* iface,
680         ISAXLocator *pLocator,
681         const WCHAR *pErrorMessage,
682         HRESULT hrErrorCode)
683 {
684     ok(0, "unexpected call\n");
685     return S_OK;
686 }
687
688 static HRESULT WINAPI isaxerrorHandler_fatalError(
689         ISAXErrorHandler* iface,
690         ISAXLocator *pLocator,
691         const WCHAR *pErrorMessage,
692         HRESULT hrErrorCode)
693 {
694     if(!test_expect_call(EH_FATALERROR))
695         return E_FAIL;
696
697     ok(hrErrorCode == expectCall->ret, "hrErrorCode = %x, expected %x\n", hrErrorCode, expectCall->ret);
698
699     expectCall++;
700     return S_OK;
701 }
702
703 static HRESULT WINAPI isaxerrorHanddler_ignorableWarning(
704         ISAXErrorHandler* iface,
705         ISAXLocator *pLocator,
706         const WCHAR *pErrorMessage,
707         HRESULT hrErrorCode)
708 {
709     ok(0, "unexpected call\n");
710     return S_OK;
711 }
712
713 static const ISAXErrorHandlerVtbl errorHandlerVtbl =
714 {
715     isaxerrorHandler_QueryInterface,
716     isaxerrorHandler_AddRef,
717     isaxerrorHandler_Release,
718     isaxerrorHandler_error,
719     isaxerrorHandler_fatalError,
720     isaxerrorHanddler_ignorableWarning
721 };
722
723 static ISAXErrorHandler errorHandler = { &errorHandlerVtbl };
724
725 static HRESULT WINAPI isaxattributes_QueryInterface(
726         ISAXAttributes* iface,
727         REFIID riid,
728         void **ppvObject)
729 {
730     *ppvObject = NULL;
731
732     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXAttributes))
733     {
734         *ppvObject = iface;
735     }
736     else
737     {
738         return E_NOINTERFACE;
739     }
740
741     return S_OK;
742 }
743
744 static ULONG WINAPI isaxattributes_AddRef(ISAXAttributes* iface)
745 {
746     return 2;
747 }
748
749 static ULONG WINAPI isaxattributes_Release(ISAXAttributes* iface)
750 {
751     return 1;
752 }
753
754 static HRESULT WINAPI isaxattributes_getLength(ISAXAttributes* iface, int *length)
755 {
756     *length = 3;
757     return S_OK;
758 }
759
760 static HRESULT WINAPI isaxattributes_getURI(
761     ISAXAttributes* iface,
762     int nIndex,
763     const WCHAR **pUrl,
764     int *pUriSize)
765 {
766     ok(0, "unexpected call\n");
767     return E_NOTIMPL;
768 }
769
770 static HRESULT WINAPI isaxattributes_getLocalName(
771     ISAXAttributes* iface,
772     int nIndex,
773     const WCHAR **pLocalName,
774     int *pLocalNameLength)
775 {
776     ok(0, "unexpected call\n");
777     return E_NOTIMPL;
778 }
779
780 static HRESULT WINAPI isaxattributes_getQName(
781     ISAXAttributes* iface,
782     int index,
783     const WCHAR **QName,
784     int *QNameLength)
785 {
786     static const WCHAR attrqnamesW[][15] = {{'a',':','a','t','t','r','1','j','u','n','k',0},
787                                             {'a','t','t','r','2','j','u','n','k',0},
788                                             {'a','t','t','r','3',0}};
789     static const int attrqnamelen[] = {7, 5, 5};
790
791     ok(index >= 0 && index <= 2, "invalid index received %d\n", index);
792
793     *QName = attrqnamesW[index];
794     *QNameLength = attrqnamelen[index];
795
796     return S_OK;
797 }
798
799 static HRESULT WINAPI isaxattributes_getName(
800     ISAXAttributes* iface,
801     int nIndex,
802     const WCHAR **pUri,
803     int * pUriLength,
804     const WCHAR ** pLocalName,
805     int * pLocalNameSize,
806     const WCHAR ** pQName,
807     int * pQNameLength)
808 {
809     ok(0, "unexpected call\n");
810     return E_NOTIMPL;
811 }
812
813 static HRESULT WINAPI isaxattributes_getIndexFromName(
814     ISAXAttributes* iface,
815     const WCHAR * pUri,
816     int cUriLength,
817     const WCHAR * pLocalName,
818     int cocalNameLength,
819     int * index)
820 {
821     ok(0, "unexpected call\n");
822     return E_NOTIMPL;
823 }
824
825 static HRESULT WINAPI isaxattributes_getIndexFromQName(
826     ISAXAttributes* iface,
827     const WCHAR * pQName,
828     int nQNameLength,
829     int * index)
830 {
831     ok(0, "unexpected call\n");
832     return E_NOTIMPL;
833 }
834
835 static HRESULT WINAPI isaxattributes_getType(
836     ISAXAttributes* iface,
837     int nIndex,
838     const WCHAR ** pType,
839     int * pTypeLength)
840 {
841     ok(0, "unexpected call\n");
842     return E_NOTIMPL;
843 }
844
845 static HRESULT WINAPI isaxattributes_getTypeFromName(
846     ISAXAttributes* iface,
847     const WCHAR * pUri,
848     int nUri,
849     const WCHAR * pLocalName,
850     int nLocalName,
851     const WCHAR ** pType,
852     int * nType)
853 {
854     ok(0, "unexpected call\n");
855     return E_NOTIMPL;
856 }
857
858 static HRESULT WINAPI isaxattributes_getTypeFromQName(
859     ISAXAttributes* iface,
860     const WCHAR * pQName,
861     int nQName,
862     const WCHAR ** pType,
863     int * nType)
864 {
865     ok(0, "unexpected call\n");
866     return E_NOTIMPL;
867 }
868
869 static HRESULT WINAPI isaxattributes_getValue(ISAXAttributes* iface, int index,
870     const WCHAR **value, int *nValue)
871 {
872     static const WCHAR attrvaluesW[][10] = {{'a','1','j','u','n','k',0},
873                                             {'a','2','j','u','n','k',0},
874                                             {'<','&','"','>',0}};
875     static const int attrvalueslen[] = {2, 2, 4};
876
877     ok(index >= 0 && index <= 2, "invalid index received %d\n", index);
878
879     *value = attrvaluesW[index];
880     *nValue = attrvalueslen[index];
881
882     return S_OK;
883 }
884
885 static HRESULT WINAPI isaxattributes_getValueFromName(
886     ISAXAttributes* iface,
887     const WCHAR * pUri,
888     int nUri,
889     const WCHAR * pLocalName,
890     int nLocalName,
891     const WCHAR ** pValue,
892     int * nValue)
893 {
894     ok(0, "unexpected call\n");
895     return E_NOTIMPL;
896 }
897
898 static HRESULT WINAPI isaxattributes_getValueFromQName(
899     ISAXAttributes* iface,
900     const WCHAR * pQName,
901     int nQName,
902     const WCHAR ** pValue,
903     int * nValue)
904 {
905     ok(0, "unexpected call\n");
906     return E_NOTIMPL;
907 }
908
909 static const ISAXAttributesVtbl SAXAttributesVtbl =
910 {
911     isaxattributes_QueryInterface,
912     isaxattributes_AddRef,
913     isaxattributes_Release,
914     isaxattributes_getLength,
915     isaxattributes_getURI,
916     isaxattributes_getLocalName,
917     isaxattributes_getQName,
918     isaxattributes_getName,
919     isaxattributes_getIndexFromName,
920     isaxattributes_getIndexFromQName,
921     isaxattributes_getType,
922     isaxattributes_getTypeFromName,
923     isaxattributes_getTypeFromQName,
924     isaxattributes_getValue,
925     isaxattributes_getValueFromName,
926     isaxattributes_getValueFromQName
927 };
928
929 static ISAXAttributes saxattributes = { &SAXAttributesVtbl };
930
931 static int handler_addrefcalled;
932
933 static HRESULT WINAPI isaxlexical_QueryInterface(ISAXLexicalHandler* iface, REFIID riid, void **ppvObject)
934 {
935     *ppvObject = NULL;
936
937     if(IsEqualGUID(riid, &IID_IUnknown) ||
938        IsEqualGUID(riid, &IID_ISAXLexicalHandler))
939     {
940         *ppvObject = iface;
941     }
942     else
943     {
944         return E_NOINTERFACE;
945     }
946
947     return S_OK;
948 }
949
950 static ULONG WINAPI isaxlexical_AddRef(ISAXLexicalHandler* iface)
951 {
952     handler_addrefcalled++;
953     return 2;
954 }
955
956 static ULONG WINAPI isaxlexical_Release(ISAXLexicalHandler* iface)
957 {
958     return 1;
959 }
960
961 static HRESULT WINAPI isaxlexical_startDTD(ISAXLexicalHandler* iface,
962     const WCHAR * pName, int nName, const WCHAR * pPublicId,
963     int nPublicId, const WCHAR * pSystemId, int nSystemId)
964 {
965     ok(0, "call not expected\n");
966     return E_NOTIMPL;
967 }
968
969 static HRESULT WINAPI isaxlexical_endDTD(ISAXLexicalHandler* iface)
970 {
971     ok(0, "call not expected\n");
972     return E_NOTIMPL;
973 }
974
975 static HRESULT WINAPI isaxlexical_startEntity(ISAXLexicalHandler *iface,
976     const WCHAR * pName, int nName)
977 {
978     ok(0, "call not expected\n");
979     return E_NOTIMPL;
980 }
981
982 static HRESULT WINAPI isaxlexical_endEntity(ISAXLexicalHandler *iface,
983     const WCHAR * pName, int nName)
984 {
985     ok(0, "call not expected\n");
986     return E_NOTIMPL;
987 }
988
989 static HRESULT WINAPI isaxlexical_startCDATA(ISAXLexicalHandler *iface)
990 {
991     ok(0, "call not expected\n");
992     return E_NOTIMPL;
993 }
994
995 static HRESULT WINAPI isaxlexical_endCDATA(ISAXLexicalHandler *iface)
996 {
997     ok(0, "call not expected\n");
998     return E_NOTIMPL;
999 }
1000
1001 static HRESULT WINAPI isaxlexical_comment(ISAXLexicalHandler *iface,
1002     const WCHAR * pChars, int nChars)
1003 {
1004     ok(0, "call not expected\n");
1005     return E_NOTIMPL;
1006 }
1007
1008 static const ISAXLexicalHandlerVtbl SAXLexicalHandlerVtbl =
1009 {
1010    isaxlexical_QueryInterface,
1011    isaxlexical_AddRef,
1012    isaxlexical_Release,
1013    isaxlexical_startDTD,
1014    isaxlexical_endDTD,
1015    isaxlexical_startEntity,
1016    isaxlexical_endEntity,
1017    isaxlexical_startCDATA,
1018    isaxlexical_endCDATA,
1019    isaxlexical_comment
1020 };
1021
1022 static ISAXLexicalHandler saxlexicalhandler = { &SAXLexicalHandlerVtbl };
1023
1024 static HRESULT WINAPI isaxdecl_QueryInterface(ISAXDeclHandler* iface, REFIID riid, void **ppvObject)
1025 {
1026     *ppvObject = NULL;
1027
1028     if(IsEqualGUID(riid, &IID_IUnknown) ||
1029        IsEqualGUID(riid, &IID_ISAXDeclHandler))
1030     {
1031         *ppvObject = iface;
1032     }
1033     else
1034     {
1035         return E_NOINTERFACE;
1036     }
1037
1038     return S_OK;
1039 }
1040
1041 static ULONG WINAPI isaxdecl_AddRef(ISAXDeclHandler* iface)
1042 {
1043     handler_addrefcalled++;
1044     return 2;
1045 }
1046
1047 static ULONG WINAPI isaxdecl_Release(ISAXDeclHandler* iface)
1048 {
1049     return 1;
1050 }
1051
1052 static HRESULT WINAPI isaxdecl_elementDecl(ISAXDeclHandler* iface,
1053     const WCHAR * pName, int nName, const WCHAR * pModel, int nModel)
1054 {
1055     ok(0, "call not expected\n");
1056     return E_NOTIMPL;
1057 }
1058
1059 static HRESULT WINAPI isaxdecl_attributeDecl(ISAXDeclHandler* iface,
1060     const WCHAR * pElementName, int nElementName, const WCHAR * pAttributeName,
1061     int nAttributeName, const WCHAR * pType, int nType, const WCHAR * pValueDefault,
1062     int nValueDefault, const WCHAR * pValue, int nValue)
1063 {
1064     ok(0, "call not expected\n");
1065     return E_NOTIMPL;
1066 }
1067
1068 static HRESULT WINAPI isaxdecl_internalEntityDecl(ISAXDeclHandler* iface,
1069     const WCHAR * pName, int nName, const WCHAR * pValue, int nValue)
1070 {
1071     ok(0, "call not expected\n");
1072     return E_NOTIMPL;
1073 }
1074
1075 static HRESULT WINAPI isaxdecl_externalEntityDecl(ISAXDeclHandler* iface,
1076     const WCHAR * pName, int nName, const WCHAR * pPublicId, int nPublicId,
1077     const WCHAR * pSystemId, int nSystemId)
1078 {
1079     ok(0, "call not expected\n");
1080     return E_NOTIMPL;
1081 }
1082
1083 static const ISAXDeclHandlerVtbl SAXDeclHandlerVtbl =
1084 {
1085    isaxdecl_QueryInterface,
1086    isaxdecl_AddRef,
1087    isaxdecl_Release,
1088    isaxdecl_elementDecl,
1089    isaxdecl_attributeDecl,
1090    isaxdecl_internalEntityDecl,
1091    isaxdecl_externalEntityDecl
1092 };
1093
1094 static ISAXDeclHandler saxdeclhandler = { &SAXDeclHandlerVtbl };
1095
1096 typedef struct mxwriter_write_test_t {
1097     BOOL        last;
1098     const BYTE  *data;
1099     DWORD       cb;
1100     BOOL        null_written;
1101     BOOL        fail_write;
1102 } mxwriter_write_test;
1103
1104 typedef struct mxwriter_stream_test_t {
1105     VARIANT_BOOL        bom;
1106     const char          *encoding;
1107     mxwriter_write_test expected_writes[4];
1108 } mxwriter_stream_test;
1109
1110 static const mxwriter_write_test *current_write_test;
1111 static DWORD current_stream_test_index;
1112
1113 static HRESULT WINAPI istream_QueryInterface(IStream *iface, REFIID riid, void **ppvObject)
1114 {
1115     *ppvObject = NULL;
1116
1117     if(IsEqualGUID(riid, &IID_IStream) || IsEqualGUID(riid, &IID_IUnknown))
1118         *ppvObject = iface;
1119     else
1120         return E_NOINTERFACE;
1121
1122     return S_OK;
1123 }
1124
1125 static ULONG WINAPI istream_AddRef(IStream *iface)
1126 {
1127     return 2;
1128 }
1129
1130 static ULONG WINAPI istream_Release(IStream *iface)
1131 {
1132     return 1;
1133 }
1134
1135 static HRESULT WINAPI istream_Read(IStream *iface, void *pv, ULONG cb, ULONG *pcbRead)
1136 {
1137     ok(0, "unexpected call\n");
1138     return E_NOTIMPL;
1139 }
1140
1141 static HRESULT WINAPI istream_Write(IStream *iface, const void *pv, ULONG cb, ULONG *pcbWritten)
1142 {
1143     BOOL fail = FALSE;
1144
1145     ok(pv != NULL, "pv == NULL\n");
1146
1147     if(current_write_test->last) {
1148         ok(0, "Too many Write calls made on test %d\n", current_stream_test_index);
1149         return E_FAIL;
1150     }
1151
1152     fail = current_write_test->fail_write;
1153
1154     ok(current_write_test->cb == cb, "Expected %d, but got %d on test %d\n",
1155         current_write_test->cb, cb, current_stream_test_index);
1156
1157     if(!pcbWritten)
1158         ok(current_write_test->null_written, "pcbWritten was NULL on test %d\n", current_stream_test_index);
1159     else
1160         ok(!memcmp(current_write_test->data, pv, cb), "Unexpected data on test %d\n", current_stream_test_index);
1161
1162     ++current_write_test;
1163
1164     if(pcbWritten)
1165         *pcbWritten = cb;
1166
1167     return fail ? E_FAIL : S_OK;
1168 }
1169
1170 static HRESULT WINAPI istream_Seek(IStream *iface, LARGE_INTEGER dlibMove, DWORD dwOrigin,
1171         ULARGE_INTEGER *plibNewPosition)
1172 {
1173     ok(0, "unexpected call\n");
1174     return E_NOTIMPL;
1175 }
1176
1177 static HRESULT WINAPI istream_SetSize(IStream *iface, ULARGE_INTEGER libNewSize)
1178 {
1179     ok(0, "unexpected call\n");
1180     return E_NOTIMPL;
1181 }
1182
1183 static HRESULT WINAPI istream_CopyTo(IStream *iface, IStream *pstm, ULARGE_INTEGER cb,
1184         ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *plibWritten)
1185 {
1186     ok(0, "unexpected call\n");
1187     return E_NOTIMPL;
1188 }
1189
1190 static HRESULT WINAPI istream_Commit(IStream *iface, DWORD grfCommitFlags)
1191 {
1192     ok(0, "unexpected call\n");
1193     return E_NOTIMPL;
1194 }
1195
1196 static HRESULT WINAPI istream_Revert(IStream *iface)
1197 {
1198     ok(0, "unexpected call\n");
1199     return E_NOTIMPL;
1200 }
1201
1202 static HRESULT WINAPI istream_LockRegion(IStream *iface, ULARGE_INTEGER libOffset,
1203         ULARGE_INTEGER cb, DWORD dwLockType)
1204 {
1205     ok(0, "unexpected call\n");
1206     return E_NOTIMPL;
1207 }
1208
1209 static HRESULT WINAPI istream_UnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
1210         ULARGE_INTEGER cb, DWORD dwLockType)
1211 {
1212     ok(0, "unexpected call\n");
1213     return E_NOTIMPL;
1214 }
1215
1216 static HRESULT WINAPI istream_Stat(IStream *iface, STATSTG *pstatstg, DWORD grfStatFlag)
1217 {
1218     ok(0, "unexpected call\n");
1219     return E_NOTIMPL;
1220 }
1221
1222 static HRESULT WINAPI istream_Clone(IStream *iface, IStream **ppstm)
1223 {
1224     ok(0, "unexpected call\n");
1225     return E_NOTIMPL;
1226 }
1227
1228 static const IStreamVtbl StreamVtbl = {
1229     istream_QueryInterface,
1230     istream_AddRef,
1231     istream_Release,
1232     istream_Read,
1233     istream_Write,
1234     istream_Seek,
1235     istream_SetSize,
1236     istream_CopyTo,
1237     istream_Commit,
1238     istream_Revert,
1239     istream_LockRegion,
1240     istream_UnlockRegion,
1241     istream_Stat,
1242     istream_Clone
1243 };
1244
1245 static IStream mxstream = { &StreamVtbl };
1246
1247 static void test_saxreader(int version)
1248 {
1249     HRESULT hr;
1250     ISAXXMLReader *reader = NULL;
1251     VARIANT var;
1252     ISAXContentHandler *lpContentHandler;
1253     ISAXErrorHandler *lpErrorHandler;
1254     SAFEARRAY *pSA;
1255     SAFEARRAYBOUND SADim[1];
1256     char *pSAData = NULL;
1257     IStream *iStream;
1258     ULARGE_INTEGER liSize;
1259     LARGE_INTEGER liPos;
1260     ULONG bytesWritten;
1261     HANDLE file;
1262     static const CHAR testXmlA[] = "test.xml";
1263     static const WCHAR testXmlW[] = {'t','e','s','t','.','x','m','l',0};
1264     IXMLDOMDocument *domDocument;
1265     BSTR bstrData;
1266     VARIANT_BOOL vBool;
1267
1268     msxml_version = version;
1269     if(version == 3) {
1270         hr = CoCreateInstance(&CLSID_SAXXMLReader30, NULL, CLSCTX_INPROC_SERVER,
1271                 &IID_ISAXXMLReader, (LPVOID*)&reader);
1272     } else if(version == 6) {
1273         hr = CoCreateInstance(&CLSID_SAXXMLReader60, NULL, CLSCTX_INPROC_SERVER,
1274                 &IID_ISAXXMLReader, (LPVOID*)&reader);
1275         if(hr == REGDB_E_CLASSNOTREG) {
1276             win_skip("SAXXMLReader6 not registered\n");
1277             return;
1278         }
1279     } else {
1280         hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
1281                 &IID_ISAXXMLReader, (LPVOID*)&reader);
1282     }
1283     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1284
1285     if(version != 6) {
1286         hr = ISAXXMLReader_getContentHandler(reader, NULL);
1287         ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
1288
1289         hr = ISAXXMLReader_getErrorHandler(reader, NULL);
1290         ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
1291     }
1292
1293     hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
1294     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1295     ok(lpContentHandler == NULL, "Expected %p, got %p\n", NULL, lpContentHandler);
1296
1297     hr = ISAXXMLReader_getErrorHandler(reader, &lpErrorHandler);
1298     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1299     ok(lpErrorHandler == NULL, "Expected %p, got %p\n", NULL, lpErrorHandler);
1300
1301     hr = ISAXXMLReader_putContentHandler(reader, NULL);
1302     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1303
1304     hr = ISAXXMLReader_putContentHandler(reader, &contentHandler);
1305     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1306
1307     hr = ISAXXMLReader_putErrorHandler(reader, &errorHandler);
1308     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1309
1310     hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
1311     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1312     ok(lpContentHandler == &contentHandler, "Expected %p, got %p\n", &contentHandler, lpContentHandler);
1313
1314     V_VT(&var) = VT_BSTR;
1315     V_BSTR(&var) = SysAllocString(szSimpleXML);
1316
1317     expectCall = contentHandlerTest1;
1318     hr = ISAXXMLReader_parse(reader, var);
1319     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1320     test_expect_call(CH_ENDTEST);
1321
1322     VariantClear(&var);
1323
1324     SADim[0].lLbound= 0;
1325     SADim[0].cElements= sizeof(szTestXML)-1;
1326     pSA = SafeArrayCreate(VT_UI1, 1, SADim);
1327     SafeArrayAccessData(pSA, (void**)&pSAData);
1328     memcpy(pSAData, szTestXML, sizeof(szTestXML)-1);
1329     SafeArrayUnaccessData(pSA);
1330     V_VT(&var) = VT_ARRAY|VT_UI1;
1331     V_ARRAY(&var) = pSA;
1332
1333     expectCall = contentHandlerTest1;
1334     hr = ISAXXMLReader_parse(reader, var);
1335     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1336     test_expect_call(CH_ENDTEST);
1337
1338     SafeArrayDestroy(pSA);
1339
1340     CreateStreamOnHGlobal(NULL, TRUE, &iStream);
1341     liSize.QuadPart = strlen(szTestXML);
1342     IStream_SetSize(iStream, liSize);
1343     IStream_Write(iStream, szTestXML, strlen(szTestXML), &bytesWritten);
1344     liPos.QuadPart = 0;
1345     IStream_Seek(iStream, liPos, STREAM_SEEK_SET, NULL);
1346     V_VT(&var) = VT_UNKNOWN|VT_DISPATCH;
1347     V_UNKNOWN(&var) = (IUnknown*)iStream;
1348
1349     expectCall = contentHandlerTest1;
1350     hr = ISAXXMLReader_parse(reader, var);
1351     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1352     test_expect_call(CH_ENDTEST);
1353
1354     IStream_Release(iStream);
1355
1356     CreateStreamOnHGlobal(NULL, TRUE, &iStream);
1357     liSize.QuadPart = strlen(szTestAttributes);
1358     IStream_SetSize(iStream, liSize);
1359     IStream_Write(iStream, szTestAttributes, strlen(szTestAttributes), &bytesWritten);
1360     liPos.QuadPart = 0;
1361     IStream_Seek(iStream, liPos, STREAM_SEEK_SET, NULL);
1362     V_VT(&var) = VT_UNKNOWN|VT_DISPATCH;
1363     V_UNKNOWN(&var) = (IUnknown*)iStream;
1364
1365     if(version >= 6)
1366         expectCall = contentHandlerTestAttributes6;
1367     else
1368         expectCall = contentHandlerTestAttributes;
1369     hr = ISAXXMLReader_parse(reader, var);
1370     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1371     test_expect_call(CH_ENDTEST);
1372
1373     IStream_Release(iStream);
1374
1375     V_VT(&var) = VT_BSTR;
1376     V_BSTR(&var) = SysAllocString(szCarriageRetTest);
1377
1378     expectCall = contentHandlerTest2;
1379     hr = ISAXXMLReader_parse(reader, var);
1380     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1381     test_expect_call(CH_ENDTEST);
1382
1383     VariantClear(&var);
1384
1385     file = CreateFileA(testXmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1386     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1387     WriteFile(file, szTestXML, sizeof(szTestXML)-1, &bytesWritten, NULL);
1388     CloseHandle(file);
1389
1390     expectCall = contentHandlerTest1;
1391     hr = ISAXXMLReader_parseURL(reader, testXmlW);
1392     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1393     test_expect_call(CH_ENDTEST);
1394
1395     expectCall = contentHandlerTestError;
1396     hr = ISAXXMLReader_parseURL(reader, testXmlW);
1397     ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
1398     test_expect_call(CH_ENDTEST);
1399
1400     if(version >= 6)
1401         expectCall = contentHandlerTestCallbackResult6;
1402     else
1403         expectCall = contentHandlerTestCallbackResults;
1404     hr = ISAXXMLReader_parseURL(reader, testXmlW);
1405     ok(hr == (version>=6 ? S_OK : S_FALSE), "Expected S_FALSE, got %08x\n", hr);
1406     test_expect_call(CH_ENDTEST);
1407
1408     DeleteFileA(testXmlA);
1409
1410     hr = CoCreateInstance(&CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
1411             &IID_IXMLDOMDocument, (LPVOID*)&domDocument);
1412     if(FAILED(hr))
1413     {
1414         skip("Failed to create DOMDocument instance\n");
1415         return;
1416     }
1417     bstrData = SysAllocString(szSimpleXML);
1418     hr = IXMLDOMDocument_loadXML(domDocument, bstrData, &vBool);
1419     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1420     V_VT(&var) = VT_UNKNOWN;
1421     V_UNKNOWN(&var) = (IUnknown*)domDocument;
1422
1423     expectCall = contentHandlerTest2;
1424     hr = ISAXXMLReader_parse(reader, var);
1425     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1426     test_expect_call(CH_ENDTEST);
1427     IXMLDOMDocument_Release(domDocument);
1428
1429     ISAXXMLReader_Release(reader);
1430     SysFreeString(bstrData);
1431 }
1432
1433 struct saxreader_props_test_t
1434 {
1435     const char *prop_name;
1436     IUnknown   *iface;
1437 };
1438
1439 static const struct saxreader_props_test_t props_test_data[] = {
1440     { "http://xml.org/sax/properties/lexical-handler", (IUnknown*)&saxlexicalhandler },
1441     { "http://xml.org/sax/properties/declaration-handler", (IUnknown*)&saxdeclhandler },
1442     { 0 }
1443 };
1444
1445 static void test_saxreader_properties(void)
1446 {
1447     const struct saxreader_props_test_t *ptr = props_test_data;
1448     ISAXXMLReader *reader;
1449     HRESULT hr;
1450
1451     hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
1452             &IID_ISAXXMLReader, (void**)&reader);
1453     EXPECT_HR(hr, S_OK);
1454
1455     hr = ISAXXMLReader_getProperty(reader, _bstr_("http://xml.org/sax/properties/lexical-handler"), NULL);
1456     EXPECT_HR(hr, E_POINTER);
1457
1458     while (ptr->prop_name)
1459     {
1460         VARIANT v;
1461
1462         V_VT(&v) = VT_EMPTY;
1463         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1464         hr = ISAXXMLReader_getProperty(reader, _bstr_(ptr->prop_name), &v);
1465         EXPECT_HR(hr, S_OK);
1466         ok(V_VT(&v) == VT_UNKNOWN, "got %d\n", V_VT(&v));
1467         ok(V_UNKNOWN(&v) == NULL, "got %p\n", V_UNKNOWN(&v));
1468
1469         V_VT(&v) = VT_UNKNOWN;
1470         V_UNKNOWN(&v) = ptr->iface;
1471         hr = ISAXXMLReader_putProperty(reader, _bstr_(ptr->prop_name), v);
1472         EXPECT_HR(hr, S_OK);
1473
1474         V_VT(&v) = VT_EMPTY;
1475         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1476         handler_addrefcalled = 0;
1477         hr = ISAXXMLReader_getProperty(reader, _bstr_(ptr->prop_name), &v);
1478         EXPECT_HR(hr, S_OK);
1479         ok(V_VT(&v) == VT_UNKNOWN, "got %d\n", V_VT(&v));
1480         ok(V_UNKNOWN(&v) == ptr->iface, "got %p\n", V_UNKNOWN(&v));
1481         ok(handler_addrefcalled == 1, "AddRef called %d times\n", handler_addrefcalled);
1482         VariantClear(&v);
1483
1484         V_VT(&v) = VT_EMPTY;
1485         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1486         hr = ISAXXMLReader_putProperty(reader, _bstr_(ptr->prop_name), v);
1487         EXPECT_HR(hr, S_OK);
1488
1489         V_VT(&v) = VT_EMPTY;
1490         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1491         hr = ISAXXMLReader_getProperty(reader, _bstr_(ptr->prop_name), &v);
1492         EXPECT_HR(hr, S_OK);
1493         ok(V_VT(&v) == VT_UNKNOWN, "got %d\n", V_VT(&v));
1494         ok(V_UNKNOWN(&v) == NULL, "got %p\n", V_UNKNOWN(&v));
1495
1496         V_VT(&v) = VT_UNKNOWN;
1497         V_UNKNOWN(&v) = ptr->iface;
1498         hr = ISAXXMLReader_putProperty(reader, _bstr_(ptr->prop_name), v);
1499         EXPECT_HR(hr, S_OK);
1500
1501         /* only VT_EMPTY seems to be valid to reset property */
1502         V_VT(&v) = VT_I4;
1503         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1504         hr = ISAXXMLReader_putProperty(reader, _bstr_(ptr->prop_name), v);
1505         EXPECT_HR(hr, E_INVALIDARG);
1506
1507         V_VT(&v) = VT_EMPTY;
1508         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1509         hr = ISAXXMLReader_getProperty(reader, _bstr_(ptr->prop_name), &v);
1510         EXPECT_HR(hr, S_OK);
1511         ok(V_VT(&v) == VT_UNKNOWN, "got %d\n", V_VT(&v));
1512         ok(V_UNKNOWN(&v) == ptr->iface, "got %p\n", V_UNKNOWN(&v));
1513         VariantClear(&v);
1514
1515         V_VT(&v) = VT_UNKNOWN;
1516         V_UNKNOWN(&v) = NULL;
1517         hr = ISAXXMLReader_putProperty(reader, _bstr_(ptr->prop_name), v);
1518         EXPECT_HR(hr, S_OK);
1519
1520         V_VT(&v) = VT_EMPTY;
1521         V_UNKNOWN(&v) = (IUnknown*)0xdeadbeef;
1522         hr = ISAXXMLReader_getProperty(reader, _bstr_(ptr->prop_name), &v);
1523         EXPECT_HR(hr, S_OK);
1524         ok(V_VT(&v) == VT_UNKNOWN, "got %d\n", V_VT(&v));
1525         ok(V_UNKNOWN(&v) == NULL, "got %p\n", V_UNKNOWN(&v));
1526
1527         ptr++;
1528     }
1529
1530     ISAXXMLReader_Release(reader);
1531     free_bstrs();
1532 }
1533
1534 struct feature_ns_entry_t {
1535     const GUID *guid;
1536     const char *clsid;
1537     VARIANT_BOOL value;
1538 };
1539
1540 static const struct feature_ns_entry_t feature_ns_entry_data[] = {
1541     { &CLSID_SAXXMLReader,   "CLSID_SAXXMLReader",   VARIANT_TRUE },
1542     { &CLSID_SAXXMLReader30, "CLSID_SAXXMLReader30", VARIANT_TRUE },
1543     { &CLSID_SAXXMLReader40, "CLSID_SAXXMLReader40", VARIANT_TRUE },
1544     { &CLSID_SAXXMLReader60, "CLSID_SAXXMLReader60", VARIANT_TRUE },
1545     { 0 }
1546 };
1547
1548 static void test_saxreader_features(void)
1549 {
1550     const struct feature_ns_entry_t *entry = feature_ns_entry_data;
1551     ISAXXMLReader *reader;
1552
1553     while (entry->guid)
1554     {
1555         VARIANT_BOOL value;
1556         HRESULT hr;
1557
1558         hr = CoCreateInstance(entry->guid, NULL, CLSCTX_INPROC_SERVER, &IID_ISAXXMLReader, (void**)&reader);
1559         if (hr != S_OK)
1560         {
1561             win_skip("can't create %s instance\n", entry->clsid);
1562             entry++;
1563             continue;
1564         }
1565
1566         value = 0xc;
1567         hr = ISAXXMLReader_getFeature(reader, _bstr_("http://xml.org/sax/features/namespaces"), &value);
1568         EXPECT_HR(hr, S_OK);
1569
1570         ok(entry->value == value, "%s: got wrong default value %x, expected %x\n", entry->clsid, value, entry->value);
1571
1572         ISAXXMLReader_Release(reader);
1573
1574         entry++;
1575     }
1576 }
1577
1578 /* UTF-8 data with UTF-8 BOM and UTF-16 in prolog */
1579 static const CHAR UTF8BOMTest[] =
1580 "\xEF\xBB\xBF<?xml version = \"1.0\" encoding = \"UTF-16\"?>\n"
1581 "<a></a>\n";
1582
1583 struct enc_test_entry_t {
1584     const GUID *guid;
1585     const char *clsid;
1586     const char *data;
1587     HRESULT hr;
1588     int todo;
1589 };
1590
1591 static const struct enc_test_entry_t encoding_test_data[] = {
1592     { &CLSID_SAXXMLReader,   "CLSID_SAXXMLReader",   UTF8BOMTest, 0xc00ce56f, 1 },
1593     { &CLSID_SAXXMLReader30, "CLSID_SAXXMLReader30", UTF8BOMTest, 0xc00ce56f, 1 },
1594     { &CLSID_SAXXMLReader40, "CLSID_SAXXMLReader40", UTF8BOMTest, S_OK, 0 },
1595     { &CLSID_SAXXMLReader60, "CLSID_SAXXMLReader60", UTF8BOMTest, S_OK, 0 },
1596     { 0 }
1597 };
1598
1599 static void test_encoding(void)
1600 {
1601     const struct enc_test_entry_t *entry = encoding_test_data;
1602     static const WCHAR testXmlW[] = {'t','e','s','t','.','x','m','l',0};
1603     static const CHAR testXmlA[] = "test.xml";
1604     ISAXXMLReader *reader;
1605     DWORD written;
1606     HANDLE file;
1607     HRESULT hr;
1608
1609     while (entry->guid)
1610     {
1611         hr = CoCreateInstance(entry->guid, NULL, CLSCTX_INPROC_SERVER, &IID_ISAXXMLReader, (void**)&reader);
1612         if (hr != S_OK)
1613         {
1614             win_skip("can't create %s instance\n", entry->clsid);
1615             entry++;
1616             continue;
1617         }
1618
1619         file = CreateFileA(testXmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1620         ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1621         WriteFile(file, UTF8BOMTest, sizeof(UTF8BOMTest)-1, &written, NULL);
1622         CloseHandle(file);
1623
1624         hr = ISAXXMLReader_parseURL(reader, testXmlW);
1625         if (entry->todo)
1626             todo_wine ok(hr == entry->hr, "Expected 0x%08x, got 0x%08x. CLSID %s\n", entry->hr, hr, entry->clsid);
1627         else
1628             ok(hr == entry->hr, "Expected 0x%08x, got 0x%08x. CLSID %s\n", entry->hr, hr, entry->clsid);
1629
1630         DeleteFileA(testXmlA);
1631         ISAXXMLReader_Release(reader);
1632
1633         entry++;
1634     }
1635 }
1636
1637 static void test_mxwriter_handlers(void)
1638 {
1639     ISAXContentHandler *handler;
1640     IMXWriter *writer, *writer2;
1641     ISAXLexicalHandler *lh;
1642     HRESULT hr;
1643
1644     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
1645             &IID_IMXWriter, (void**)&writer);
1646     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1647
1648     EXPECT_REF(writer, 1);
1649
1650     /* ISAXContentHandler */
1651     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&handler);
1652     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1653     EXPECT_REF(writer, 2);
1654     EXPECT_REF(handler, 2);
1655
1656     hr = ISAXContentHandler_QueryInterface(handler, &IID_IMXWriter, (void**)&writer2);
1657     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1658     ok(writer2 == writer, "got %p, expected %p\n", writer2, writer);
1659     EXPECT_REF(writer, 3);
1660     EXPECT_REF(writer2, 3);
1661     IMXWriter_Release(writer2);
1662     ISAXContentHandler_Release(handler);
1663
1664     /* ISAXLexicalHandler */
1665     hr = IMXWriter_QueryInterface(writer, &IID_ISAXLexicalHandler, (void**)&lh);
1666     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1667     EXPECT_REF(writer, 2);
1668     EXPECT_REF(lh, 2);
1669
1670     hr = ISAXLexicalHandler_QueryInterface(lh, &IID_IMXWriter, (void**)&writer2);
1671     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1672     ok(writer2 == writer, "got %p, expected %p\n", writer2, writer);
1673     EXPECT_REF(writer, 3);
1674     EXPECT_REF(writer2, 3);
1675     IMXWriter_Release(writer2);
1676
1677     IMXWriter_Release(writer);
1678 }
1679
1680 struct msxmlsupported_data_t
1681 {
1682     const GUID *clsid;
1683     const char *name;
1684     BOOL supported;
1685 };
1686
1687 static struct msxmlsupported_data_t mxwriter_support_data[] =
1688 {
1689     { &CLSID_MXXMLWriter,   "MXXMLWriter" },
1690     { &CLSID_MXXMLWriter30, "MXXMLWriter30" },
1691     { &CLSID_MXXMLWriter40, "MXXMLWriter40" },
1692     { &CLSID_MXXMLWriter60, "MXXMLWriter60" },
1693     { NULL }
1694 };
1695
1696 static struct msxmlsupported_data_t mxattributes_support_data[] =
1697 {
1698     { &CLSID_SAXAttributes,   "SAXAttributes" },
1699     { &CLSID_SAXAttributes30, "SAXAttributes30" },
1700     { &CLSID_SAXAttributes40, "SAXAttributes40" },
1701     { &CLSID_SAXAttributes60, "SAXAttributes60" },
1702     { NULL }
1703 };
1704
1705 static BOOL is_clsid_supported(const GUID *clsid, const struct msxmlsupported_data_t *table)
1706 {
1707     while (table->clsid)
1708     {
1709         if (table->clsid == clsid) return table->supported;
1710         table++;
1711     }
1712     return FALSE;
1713 }
1714
1715 struct mxwriter_props_t
1716 {
1717     const GUID *clsid;
1718     VARIANT_BOOL bom;
1719     VARIANT_BOOL disable_escape;
1720     VARIANT_BOOL indent;
1721     VARIANT_BOOL omitdecl;
1722     VARIANT_BOOL standalone;
1723     const char *encoding;
1724 };
1725
1726 static const struct mxwriter_props_t mxwriter_default_props[] =
1727 {
1728     { &CLSID_MXXMLWriter,   VARIANT_TRUE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, "UTF-16" },
1729     { &CLSID_MXXMLWriter30, VARIANT_TRUE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, "UTF-16" },
1730     { &CLSID_MXXMLWriter40, VARIANT_TRUE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, "UTF-16" },
1731     { &CLSID_MXXMLWriter60, VARIANT_TRUE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, VARIANT_FALSE, "UTF-16" },
1732     { NULL }
1733 };
1734
1735 static void test_mxwriter_default_properties(const struct mxwriter_props_t *table)
1736 {
1737     int i = 0;
1738
1739     while (table->clsid)
1740     {
1741         IMXWriter *writer;
1742         VARIANT_BOOL b;
1743         BSTR encoding;
1744         HRESULT hr;
1745
1746         if (!is_clsid_supported(table->clsid, mxwriter_support_data))
1747         {
1748             table++;
1749             i++;
1750             continue;
1751         }
1752
1753         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
1754             &IID_IMXWriter, (void**)&writer);
1755         EXPECT_HR(hr, S_OK);
1756
1757         b = !table->bom;
1758         hr = IMXWriter_get_byteOrderMark(writer, &b);
1759         EXPECT_HR(hr, S_OK);
1760         ok(table->bom == b, "test %d: got BOM %d, expected %d\n", i, b, table->bom);
1761
1762         b = !table->disable_escape;
1763         hr = IMXWriter_get_disableOutputEscaping(writer, &b);
1764         EXPECT_HR(hr, S_OK);
1765         ok(table->disable_escape == b, "test %d: got disable escape %d, expected %d\n", i, b,
1766            table->disable_escape);
1767
1768         b = !table->indent;
1769         hr = IMXWriter_get_indent(writer, &b);
1770         EXPECT_HR(hr, S_OK);
1771         ok(table->indent == b, "test %d: got indent %d, expected %d\n", i, b, table->indent);
1772
1773         b = !table->omitdecl;
1774         hr = IMXWriter_get_omitXMLDeclaration(writer, &b);
1775         EXPECT_HR(hr, S_OK);
1776         ok(table->omitdecl == b, "test %d: got omitdecl %d, expected %d\n", i, b, table->omitdecl);
1777
1778         b = !table->standalone;
1779         hr = IMXWriter_get_standalone(writer, &b);
1780         EXPECT_HR(hr, S_OK);
1781         ok(table->standalone == b, "test %d: got standalone %d, expected %d\n", i, b, table->standalone);
1782
1783         hr = IMXWriter_get_encoding(writer, &encoding);
1784         EXPECT_HR(hr, S_OK);
1785         ok(!lstrcmpW(encoding, _bstr_(table->encoding)), "test %d: got encoding %s, expected %s\n",
1786             i, wine_dbgstr_w(encoding), table->encoding);
1787         SysFreeString(encoding);
1788
1789         IMXWriter_Release(writer);
1790
1791         table++;
1792         i++;
1793     }
1794 }
1795
1796 static void test_mxwriter_properties(void)
1797 {
1798     static const WCHAR utf16W[] = {'U','T','F','-','1','6',0};
1799     static const WCHAR emptyW[] = {0};
1800     static const WCHAR testW[] = {'t','e','s','t',0};
1801     ISAXContentHandler *content;
1802     IMXWriter *writer;
1803     VARIANT_BOOL b;
1804     HRESULT hr;
1805     BSTR str, str2;
1806     VARIANT dest;
1807
1808     test_mxwriter_default_properties(mxwriter_default_props);
1809
1810     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
1811             &IID_IMXWriter, (void**)&writer);
1812     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1813
1814     hr = IMXWriter_get_disableOutputEscaping(writer, NULL);
1815     ok(hr == E_POINTER, "got %08x\n", hr);
1816
1817     hr = IMXWriter_get_byteOrderMark(writer, NULL);
1818     ok(hr == E_POINTER, "got %08x\n", hr);
1819
1820     hr = IMXWriter_get_indent(writer, NULL);
1821     ok(hr == E_POINTER, "got %08x\n", hr);
1822
1823     hr = IMXWriter_get_omitXMLDeclaration(writer, NULL);
1824     ok(hr == E_POINTER, "got %08x\n", hr);
1825
1826     hr = IMXWriter_get_standalone(writer, NULL);
1827     ok(hr == E_POINTER, "got %08x\n", hr);
1828
1829     /* set and check */
1830     hr = IMXWriter_put_standalone(writer, VARIANT_TRUE);
1831     ok(hr == S_OK, "got %08x\n", hr);
1832
1833     b = VARIANT_FALSE;
1834     hr = IMXWriter_get_standalone(writer, &b);
1835     ok(hr == S_OK, "got %08x\n", hr);
1836     ok(b == VARIANT_TRUE, "got %d\n", b);
1837
1838     hr = IMXWriter_get_encoding(writer, NULL);
1839     EXPECT_HR(hr, E_POINTER);
1840
1841     /* UTF-16 is a default setting apparently */
1842     str = (void*)0xdeadbeef;
1843     hr = IMXWriter_get_encoding(writer, &str);
1844     EXPECT_HR(hr, S_OK);
1845     ok(lstrcmpW(str, utf16W) == 0, "expected empty string, got %s\n", wine_dbgstr_w(str));
1846
1847     str2 = (void*)0xdeadbeef;
1848     hr = IMXWriter_get_encoding(writer, &str2);
1849     ok(hr == S_OK, "got %08x\n", hr);
1850     ok(str != str2, "expected newly allocated, got same %p\n", str);
1851
1852     SysFreeString(str2);
1853     SysFreeString(str);
1854
1855     /* put empty string */
1856     str = SysAllocString(emptyW);
1857     hr = IMXWriter_put_encoding(writer, str);
1858     ok(hr == E_INVALIDARG, "got %08x\n", hr);
1859     SysFreeString(str);
1860
1861     str = (void*)0xdeadbeef;
1862     hr = IMXWriter_get_encoding(writer, &str);
1863     EXPECT_HR(hr, S_OK);
1864     ok(!lstrcmpW(str, _bstr_("UTF-16")), "got %s\n", wine_dbgstr_w(str));
1865     SysFreeString(str);
1866
1867     /* invalid encoding name */
1868     str = SysAllocString(testW);
1869     hr = IMXWriter_put_encoding(writer, str);
1870     ok(hr == E_INVALIDARG, "got %08x\n", hr);
1871     SysFreeString(str);
1872
1873     /* test case sensivity */
1874     hr = IMXWriter_put_encoding(writer, _bstr_("utf-8"));
1875     EXPECT_HR(hr, S_OK);
1876     str = (void*)0xdeadbeef;
1877     hr = IMXWriter_get_encoding(writer, &str);
1878     EXPECT_HR(hr, S_OK);
1879     ok(!lstrcmpW(str, _bstr_("utf-8")), "got %s\n", wine_dbgstr_w(str));
1880     SysFreeString(str);
1881
1882     hr = IMXWriter_put_encoding(writer, _bstr_("uTf-16"));
1883     EXPECT_HR(hr, S_OK);
1884     str = (void*)0xdeadbeef;
1885     hr = IMXWriter_get_encoding(writer, &str);
1886     EXPECT_HR(hr, S_OK);
1887     ok(!lstrcmpW(str, _bstr_("uTf-16")), "got %s\n", wine_dbgstr_w(str));
1888     SysFreeString(str);
1889
1890     /* how it affects document creation */
1891     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
1892     EXPECT_HR(hr, S_OK);
1893
1894     hr = ISAXContentHandler_startDocument(content);
1895     EXPECT_HR(hr, S_OK);
1896     hr = ISAXContentHandler_endDocument(content);
1897     EXPECT_HR(hr, S_OK);
1898
1899     V_VT(&dest) = VT_EMPTY;
1900     hr = IMXWriter_get_output(writer, &dest);
1901     EXPECT_HR(hr, S_OK);
1902     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
1903     ok(!lstrcmpW(_bstr_("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"yes\"?>\r\n"),
1904         V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
1905     VariantClear(&dest);
1906     ISAXContentHandler_Release(content);
1907
1908     hr = IMXWriter_get_version(writer, NULL);
1909     ok(hr == E_POINTER, "got %08x\n", hr);
1910     /* default version is 'surprisingly' 1.0 */
1911     hr = IMXWriter_get_version(writer, &str);
1912     ok(hr == S_OK, "got %08x\n", hr);
1913     ok(!lstrcmpW(str, _bstr_("1.0")), "got %s\n", wine_dbgstr_w(str));
1914     SysFreeString(str);
1915
1916     /* store version string as is */
1917     hr = IMXWriter_put_version(writer, NULL);
1918     ok(hr == E_INVALIDARG, "got %08x\n", hr);
1919
1920     hr = IMXWriter_put_version(writer, _bstr_("1.0"));
1921     ok(hr == S_OK, "got %08x\n", hr);
1922
1923     hr = IMXWriter_put_version(writer, _bstr_(""));
1924     ok(hr == S_OK, "got %08x\n", hr);
1925     hr = IMXWriter_get_version(writer, &str);
1926     ok(hr == S_OK, "got %08x\n", hr);
1927     ok(!lstrcmpW(str, _bstr_("")), "got %s\n", wine_dbgstr_w(str));
1928     SysFreeString(str);
1929
1930     hr = IMXWriter_put_version(writer, _bstr_("a.b"));
1931     ok(hr == S_OK, "got %08x\n", hr);
1932     hr = IMXWriter_get_version(writer, &str);
1933     ok(hr == S_OK, "got %08x\n", hr);
1934     ok(!lstrcmpW(str, _bstr_("a.b")), "got %s\n", wine_dbgstr_w(str));
1935     SysFreeString(str);
1936
1937     hr = IMXWriter_put_version(writer, _bstr_("2.0"));
1938     ok(hr == S_OK, "got %08x\n", hr);
1939     hr = IMXWriter_get_version(writer, &str);
1940     ok(hr == S_OK, "got %08x\n", hr);
1941     ok(!lstrcmpW(str, _bstr_("2.0")), "got %s\n", wine_dbgstr_w(str));
1942     SysFreeString(str);
1943
1944     IMXWriter_Release(writer);
1945     free_bstrs();
1946 }
1947
1948 static void test_mxwriter_flush(void)
1949 {
1950     ISAXContentHandler *content;
1951     IMXWriter *writer;
1952     LARGE_INTEGER pos;
1953     ULARGE_INTEGER pos2;
1954     IStream *stream;
1955     VARIANT dest;
1956     HRESULT hr;
1957
1958     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
1959             &IID_IMXWriter, (void**)&writer);
1960     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1961
1962     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1963     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1964     EXPECT_REF(stream, 1);
1965
1966     /* detach when nothing was attached */
1967     V_VT(&dest) = VT_EMPTY;
1968     hr = IMXWriter_put_output(writer, dest);
1969     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1970
1971     /* attach stream */
1972     V_VT(&dest) = VT_UNKNOWN;
1973     V_UNKNOWN(&dest) = (IUnknown*)stream;
1974     hr = IMXWriter_put_output(writer, dest);
1975     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1976     todo_wine EXPECT_REF(stream, 3);
1977
1978     /* detach setting VT_EMPTY destination */
1979     V_VT(&dest) = VT_EMPTY;
1980     hr = IMXWriter_put_output(writer, dest);
1981     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1982     EXPECT_REF(stream, 1);
1983
1984     V_VT(&dest) = VT_UNKNOWN;
1985     V_UNKNOWN(&dest) = (IUnknown*)stream;
1986     hr = IMXWriter_put_output(writer, dest);
1987     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1988
1989     /* flush() doesn't detach a stream */
1990     hr = IMXWriter_flush(writer);
1991     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1992     todo_wine EXPECT_REF(stream, 3);
1993
1994     pos.QuadPart = 0;
1995     hr = IStream_Seek(stream, pos, STREAM_SEEK_CUR, &pos2);
1996     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
1997     ok(pos2.QuadPart == 0, "expected stream beginning\n");
1998
1999     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2000     ok(hr == S_OK, "got %08x\n", hr);
2001
2002     hr = ISAXContentHandler_startDocument(content);
2003     ok(hr == S_OK, "got %08x\n", hr);
2004
2005     pos.QuadPart = 0;
2006     hr = IStream_Seek(stream, pos, STREAM_SEEK_CUR, &pos2);
2007     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
2008     ok(pos2.QuadPart != 0, "expected stream beginning\n");
2009
2010     /* already started */
2011     hr = ISAXContentHandler_startDocument(content);
2012     ok(hr == S_OK, "got %08x\n", hr);
2013
2014     hr = ISAXContentHandler_endDocument(content);
2015     ok(hr == S_OK, "got %08x\n", hr);
2016
2017     /* flushed on endDocument() */
2018     pos.QuadPart = 0;
2019     hr = IStream_Seek(stream, pos, STREAM_SEEK_CUR, &pos2);
2020     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
2021     ok(pos2.QuadPart != 0, "expected stream position moved\n");
2022
2023     ISAXContentHandler_Release(content);
2024     IStream_Release(stream);
2025     IMXWriter_Release(writer);
2026 }
2027
2028 static void test_mxwriter_startenddocument(void)
2029 {
2030     ISAXContentHandler *content;
2031     IMXWriter *writer;
2032     VARIANT dest;
2033     HRESULT hr;
2034
2035     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2036             &IID_IMXWriter, (void**)&writer);
2037     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
2038
2039     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2040     ok(hr == S_OK, "got %08x\n", hr);
2041
2042     hr = ISAXContentHandler_startDocument(content);
2043     ok(hr == S_OK, "got %08x\n", hr);
2044
2045     hr = ISAXContentHandler_endDocument(content);
2046     ok(hr == S_OK, "got %08x\n", hr);
2047
2048     V_VT(&dest) = VT_EMPTY;
2049     hr = IMXWriter_get_output(writer, &dest);
2050     ok(hr == S_OK, "got %08x\n", hr);
2051     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2052     ok(!lstrcmpW(_bstr_("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?>\r\n"), V_BSTR(&dest)),
2053         "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2054     VariantClear(&dest);
2055
2056     /* now try another startDocument */
2057     hr = ISAXContentHandler_startDocument(content);
2058     ok(hr == S_OK, "got %08x\n", hr);
2059     /* and get duplicated prolog */
2060     V_VT(&dest) = VT_EMPTY;
2061     hr = IMXWriter_get_output(writer, &dest);
2062     ok(hr == S_OK, "got %08x\n", hr);
2063     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2064     ok(!lstrcmpW(_bstr_("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?>\r\n"
2065                         "<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?>\r\n"), V_BSTR(&dest)),
2066         "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2067     VariantClear(&dest);
2068
2069     ISAXContentHandler_Release(content);
2070     IMXWriter_Release(writer);
2071
2072     /* now with omitted declaration */
2073     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2074             &IID_IMXWriter, (void**)&writer);
2075     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
2076
2077     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2078     ok(hr == S_OK, "got %08x\n", hr);
2079
2080     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2081     ok(hr == S_OK, "got %08x\n", hr);
2082
2083     hr = ISAXContentHandler_startDocument(content);
2084     ok(hr == S_OK, "got %08x\n", hr);
2085
2086     hr = ISAXContentHandler_endDocument(content);
2087     ok(hr == S_OK, "got %08x\n", hr);
2088
2089     V_VT(&dest) = VT_EMPTY;
2090     hr = IMXWriter_get_output(writer, &dest);
2091     ok(hr == S_OK, "got %08x\n", hr);
2092     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2093     ok(!lstrcmpW(_bstr_(""), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2094     VariantClear(&dest);
2095
2096     ISAXContentHandler_Release(content);
2097     IMXWriter_Release(writer);
2098
2099     free_bstrs();
2100 }
2101
2102 enum startendtype
2103 {
2104     StartElement,
2105     EndElement,
2106     StartEndElement
2107 };
2108
2109 struct writer_startendelement_t {
2110     const GUID *clsid;
2111     enum startendtype type;
2112     const char *uri;
2113     const char *local_name;
2114     const char *qname;
2115     const char *output;
2116     HRESULT hr;
2117     ISAXAttributes *attr;
2118 };
2119
2120 static const char startelement_xml[] = "<uri:local a:attr1=\"a1\" attr2=\"a2\" attr3=\"&lt;&amp;&quot;&gt;\">";
2121 static const char startendelement_xml[] = "<uri:local a:attr1=\"a1\" attr2=\"a2\" attr3=\"&lt;&amp;&quot;&gt;\"/>";
2122
2123 static const struct writer_startendelement_t writer_startendelement[] = {
2124     /* 0 */
2125     { &CLSID_MXXMLWriter,   StartElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2126     { &CLSID_MXXMLWriter30, StartElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2127     { &CLSID_MXXMLWriter40, StartElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2128     { &CLSID_MXXMLWriter60, StartElement, NULL, NULL, NULL, "<>", S_OK },
2129     { &CLSID_MXXMLWriter,   StartElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2130     /* 5 */
2131     { &CLSID_MXXMLWriter30, StartElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2132     { &CLSID_MXXMLWriter40, StartElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2133     { &CLSID_MXXMLWriter60, StartElement, "uri", NULL, NULL, "<>", S_OK },
2134     { &CLSID_MXXMLWriter,   StartElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2135     { &CLSID_MXXMLWriter30, StartElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2136     /* 10 */
2137     { &CLSID_MXXMLWriter40, StartElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2138     { &CLSID_MXXMLWriter60, StartElement, NULL, "local", NULL, "<>", S_OK },
2139     { &CLSID_MXXMLWriter,   StartElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2140     { &CLSID_MXXMLWriter30, StartElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2141     { &CLSID_MXXMLWriter40, StartElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2142     /* 15 */
2143     { &CLSID_MXXMLWriter60, StartElement, NULL, NULL, "qname", "<qname>", S_OK },
2144     { &CLSID_MXXMLWriter,   StartElement, "uri", "local", "qname", "<qname>", S_OK },
2145     { &CLSID_MXXMLWriter30, StartElement, "uri", "local", "qname", "<qname>", S_OK },
2146     { &CLSID_MXXMLWriter40, StartElement, "uri", "local", "qname", "<qname>", S_OK },
2147     { &CLSID_MXXMLWriter60, StartElement, "uri", "local", "qname", "<qname>", S_OK },
2148     /* 20 */
2149     { &CLSID_MXXMLWriter,   StartElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2150     { &CLSID_MXXMLWriter30, StartElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2151     { &CLSID_MXXMLWriter40, StartElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2152     { &CLSID_MXXMLWriter60, StartElement, "uri", "local", NULL, "<>", S_OK },
2153     { &CLSID_MXXMLWriter,   StartElement, "uri", "local", "uri:local", "<uri:local>", S_OK },
2154     /* 25 */
2155     { &CLSID_MXXMLWriter30, StartElement, "uri", "local", "uri:local", "<uri:local>", S_OK },
2156     { &CLSID_MXXMLWriter40, StartElement, "uri", "local", "uri:local", "<uri:local>", S_OK },
2157     { &CLSID_MXXMLWriter60, StartElement, "uri", "local", "uri:local", "<uri:local>", S_OK },
2158     { &CLSID_MXXMLWriter,   StartElement, "uri", "local", "uri:local2", "<uri:local2>", S_OK },
2159     { &CLSID_MXXMLWriter30, StartElement, "uri", "local", "uri:local2", "<uri:local2>", S_OK },
2160     /* 30 */
2161     { &CLSID_MXXMLWriter40, StartElement, "uri", "local", "uri:local2", "<uri:local2>", S_OK },
2162     { &CLSID_MXXMLWriter60, StartElement, "uri", "local", "uri:local2", "<uri:local2>", S_OK },
2163     /* endElement tests */
2164     { &CLSID_MXXMLWriter,   EndElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2165     { &CLSID_MXXMLWriter30, EndElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2166     { &CLSID_MXXMLWriter40, EndElement, NULL, NULL, NULL, NULL, E_INVALIDARG },
2167     /* 35 */
2168     { &CLSID_MXXMLWriter60, EndElement, NULL, NULL, NULL, "</>", S_OK },
2169     { &CLSID_MXXMLWriter,   EndElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2170     { &CLSID_MXXMLWriter30, EndElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2171     { &CLSID_MXXMLWriter40, EndElement, "uri", NULL, NULL, NULL, E_INVALIDARG },
2172     { &CLSID_MXXMLWriter60, EndElement, "uri", NULL, NULL, "</>", S_OK },
2173     /* 40 */
2174     { &CLSID_MXXMLWriter,   EndElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2175     { &CLSID_MXXMLWriter30, EndElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2176     { &CLSID_MXXMLWriter40, EndElement, NULL, "local", NULL, NULL, E_INVALIDARG },
2177     { &CLSID_MXXMLWriter60, EndElement, NULL, "local", NULL, "</>", S_OK },
2178     { &CLSID_MXXMLWriter,   EndElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2179     /* 45 */
2180     { &CLSID_MXXMLWriter30, EndElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2181     { &CLSID_MXXMLWriter40, EndElement, NULL, NULL, "qname", NULL, E_INVALIDARG },
2182     { &CLSID_MXXMLWriter60, EndElement, NULL, NULL, "qname", "</qname>", S_OK },
2183     { &CLSID_MXXMLWriter,   EndElement, "uri", "local", "qname", "</qname>", S_OK },
2184     { &CLSID_MXXMLWriter30, EndElement, "uri", "local", "qname", "</qname>", S_OK },
2185     /* 50 */
2186     { &CLSID_MXXMLWriter40, EndElement, "uri", "local", "qname", "</qname>", S_OK },
2187     { &CLSID_MXXMLWriter60, EndElement, "uri", "local", "qname", "</qname>", S_OK },
2188     { &CLSID_MXXMLWriter,   EndElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2189     { &CLSID_MXXMLWriter30, EndElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2190     { &CLSID_MXXMLWriter40, EndElement, "uri", "local", NULL, NULL, E_INVALIDARG },
2191     /* 55 */
2192     { &CLSID_MXXMLWriter60, EndElement, "uri", "local", NULL, "</>", S_OK },
2193     { &CLSID_MXXMLWriter,   EndElement, "uri", "local", "uri:local", "</uri:local>", S_OK },
2194     { &CLSID_MXXMLWriter30, EndElement, "uri", "local", "uri:local", "</uri:local>", S_OK },
2195     { &CLSID_MXXMLWriter40, EndElement, "uri", "local", "uri:local", "</uri:local>", S_OK },
2196     { &CLSID_MXXMLWriter60, EndElement, "uri", "local", "uri:local", "</uri:local>", S_OK },
2197     /* 60 */
2198     { &CLSID_MXXMLWriter,   EndElement, "uri", "local", "uri:local2", "</uri:local2>", S_OK },
2199     { &CLSID_MXXMLWriter30, EndElement, "uri", "local", "uri:local2", "</uri:local2>", S_OK },
2200     { &CLSID_MXXMLWriter40, EndElement, "uri", "local", "uri:local2", "</uri:local2>", S_OK },
2201     { &CLSID_MXXMLWriter60, EndElement, "uri", "local", "uri:local2", "</uri:local2>", S_OK },
2202
2203     /* with attributes */
2204     { &CLSID_MXXMLWriter,   StartElement, "uri", "local", "uri:local", startelement_xml, S_OK, &saxattributes },
2205     /* 65 */
2206     { &CLSID_MXXMLWriter30, StartElement, "uri", "local", "uri:local", startelement_xml, S_OK, &saxattributes },
2207     { &CLSID_MXXMLWriter40, StartElement, "uri", "local", "uri:local", startelement_xml, S_OK, &saxattributes },
2208     { &CLSID_MXXMLWriter60, StartElement, "uri", "local", "uri:local", startelement_xml, S_OK, &saxattributes },
2209     /* empty elements */
2210     { &CLSID_MXXMLWriter,   StartEndElement, "uri", "local", "uri:local", startendelement_xml, S_OK, &saxattributes },
2211     { &CLSID_MXXMLWriter30, StartEndElement, "uri", "local", "uri:local", startendelement_xml, S_OK, &saxattributes },
2212     /* 70 */
2213     { &CLSID_MXXMLWriter40, StartEndElement, "uri", "local", "uri:local", startendelement_xml, S_OK, &saxattributes },
2214     { &CLSID_MXXMLWriter60, StartEndElement, "uri", "local", "uri:local", startendelement_xml, S_OK, &saxattributes },
2215     { &CLSID_MXXMLWriter,   StartEndElement, "", "", "", "</>", S_OK },
2216     { &CLSID_MXXMLWriter30, StartEndElement, "", "", "", "</>", S_OK },
2217     { &CLSID_MXXMLWriter40, StartEndElement, "", "", "", "</>", S_OK },
2218     /* 75 */
2219     { &CLSID_MXXMLWriter60, StartEndElement, "", "", "", "</>", S_OK },
2220     { NULL }
2221 };
2222
2223 static void get_mxwriter_support_data(struct msxmlsupported_data_t *table)
2224 {
2225     while (table->clsid)
2226     {
2227         IMXWriter *writer;
2228         HRESULT hr;
2229
2230         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
2231             &IID_IMXWriter, (void**)&writer);
2232         if (hr == S_OK) IMXWriter_Release(writer);
2233
2234         table->supported = hr == S_OK;
2235         if (hr != S_OK) win_skip("class %s not supported\n", table->name);
2236
2237         table++;
2238     }
2239 }
2240
2241 static void get_mxattributes_support_data(struct msxmlsupported_data_t *table)
2242 {
2243     while (table->clsid)
2244     {
2245         IMXAttributes *attr;
2246         HRESULT hr;
2247
2248         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
2249             &IID_IMXAttributes, (void**)&attr);
2250         if (hr == S_OK) IMXAttributes_Release(attr);
2251
2252         table->supported = hr == S_OK;
2253         if (hr != S_OK) skip("class %s not supported\n", table->name);
2254
2255         table++;
2256     }
2257 }
2258
2259 static void test_mxwriter_startendelement_batch(const struct writer_startendelement_t *table)
2260 {
2261     int i = 0;
2262
2263     while (table->clsid)
2264     {
2265         ISAXContentHandler *content;
2266         IMXWriter *writer;
2267         HRESULT hr;
2268
2269         if (!is_clsid_supported(table->clsid, mxwriter_support_data))
2270         {
2271             table++;
2272             i++;
2273             continue;
2274         }
2275
2276         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
2277             &IID_IMXWriter, (void**)&writer);
2278         EXPECT_HR(hr, S_OK);
2279
2280         hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2281         EXPECT_HR(hr, S_OK);
2282
2283         hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2284         EXPECT_HR(hr, S_OK);
2285
2286         hr = ISAXContentHandler_startDocument(content);
2287         EXPECT_HR(hr, S_OK);
2288
2289         if (table->type == StartElement)
2290         {
2291             hr = ISAXContentHandler_startElement(content, _bstr_(table->uri), lstrlen(table->uri),
2292                 _bstr_(table->local_name), lstrlen(table->local_name), _bstr_(table->qname), lstrlen(table->qname), table->attr);
2293             ok(hr == table->hr, "test %d: got 0x%08x, expected 0x%08x\n", i, hr, table->hr);
2294         }
2295         else if (table->type == EndElement)
2296         {
2297             hr = ISAXContentHandler_endElement(content, _bstr_(table->uri), lstrlen(table->uri),
2298                 _bstr_(table->local_name), lstrlen(table->local_name), _bstr_(table->qname), lstrlen(table->qname));
2299             ok(hr == table->hr, "test %d: got 0x%08x, expected 0x%08x\n", i, hr, table->hr);
2300         }
2301         else
2302         {
2303             hr = ISAXContentHandler_startElement(content, _bstr_(table->uri), lstrlen(table->uri),
2304                 _bstr_(table->local_name), lstrlen(table->local_name), _bstr_(table->qname), lstrlen(table->qname), table->attr);
2305             ok(hr == table->hr, "test %d: got 0x%08x, expected 0x%08x\n", i, hr, table->hr);
2306             hr = ISAXContentHandler_endElement(content, _bstr_(table->uri), lstrlen(table->uri),
2307                 _bstr_(table->local_name), lstrlen(table->local_name), _bstr_(table->qname), lstrlen(table->qname));
2308             ok(hr == table->hr, "test %d: got 0x%08x, expected 0x%08x\n", i, hr, table->hr);
2309         }
2310
2311         /* test output */
2312         if (hr == S_OK)
2313         {
2314             VARIANT dest;
2315
2316             V_VT(&dest) = VT_EMPTY;
2317             hr = IMXWriter_get_output(writer, &dest);
2318             EXPECT_HR(hr, S_OK);
2319             ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2320             ok(!lstrcmpW(_bstr_(table->output), V_BSTR(&dest)),
2321                 "test %d: got wrong content %s, expected %s\n", i, wine_dbgstr_w(V_BSTR(&dest)), table->output);
2322             VariantClear(&dest);
2323         }
2324
2325         ISAXContentHandler_Release(content);
2326         IMXWriter_Release(writer);
2327
2328         table++;
2329         i++;
2330     }
2331
2332     free_bstrs();
2333 }
2334
2335 static void test_mxwriter_startendelement(void)
2336 {
2337     ISAXContentHandler *content;
2338     IMXWriter *writer;
2339     VARIANT dest;
2340     HRESULT hr;
2341
2342     test_mxwriter_startendelement_batch(writer_startendelement);
2343
2344     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2345             &IID_IMXWriter, (void**)&writer);
2346     ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
2347
2348     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2349     ok(hr == S_OK, "got %08x\n", hr);
2350
2351     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2352     ok(hr == S_OK, "got %08x\n", hr);
2353
2354     hr = ISAXContentHandler_startDocument(content);
2355     ok(hr == S_OK, "got %08x\n", hr);
2356
2357     /* all string pointers should be not null */
2358     hr = ISAXContentHandler_startElement(content, _bstr_(""), 0, _bstr_("b"), 1, _bstr_(""), 0, NULL);
2359     ok(hr == S_OK, "got %08x\n", hr);
2360
2361     V_VT(&dest) = VT_EMPTY;
2362     hr = IMXWriter_get_output(writer, &dest);
2363     ok(hr == S_OK, "got %08x\n", hr);
2364     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2365     ok(!lstrcmpW(_bstr_("<>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2366     VariantClear(&dest);
2367
2368     hr = ISAXContentHandler_startElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("b"), 1, NULL);
2369     ok(hr == S_OK, "got %08x\n", hr);
2370
2371     V_VT(&dest) = VT_EMPTY;
2372     hr = IMXWriter_get_output(writer, &dest);
2373     ok(hr == S_OK, "got %08x\n", hr);
2374     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2375     ok(!lstrcmpW(_bstr_("<><b>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2376     VariantClear(&dest);
2377
2378     hr = ISAXContentHandler_endElement(content, NULL, 0, NULL, 0, _bstr_("a:b"), 3);
2379     EXPECT_HR(hr, E_INVALIDARG);
2380
2381     hr = ISAXContentHandler_endElement(content, NULL, 0, _bstr_("b"), 1, _bstr_("a:b"), 3);
2382     EXPECT_HR(hr, E_INVALIDARG);
2383
2384     /* only local name is an error too */
2385     hr = ISAXContentHandler_endElement(content, NULL, 0, _bstr_("b"), 1, NULL, 0);
2386     EXPECT_HR(hr, E_INVALIDARG);
2387
2388     hr = ISAXContentHandler_endElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("b"), 1);
2389     EXPECT_HR(hr, S_OK);
2390
2391     V_VT(&dest) = VT_EMPTY;
2392     hr = IMXWriter_get_output(writer, &dest);
2393     EXPECT_HR(hr, S_OK);
2394     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2395     ok(!lstrcmpW(_bstr_("<><b></b>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2396     VariantClear(&dest);
2397
2398     hr = ISAXContentHandler_endDocument(content);
2399     EXPECT_HR(hr, S_OK);
2400
2401     V_VT(&dest) = VT_EMPTY;
2402     hr = IMXWriter_put_output(writer, dest);
2403     EXPECT_HR(hr, S_OK);
2404
2405     V_VT(&dest) = VT_EMPTY;
2406     hr = IMXWriter_get_output(writer, &dest);
2407     EXPECT_HR(hr, S_OK);
2408     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2409     ok(!lstrcmpW(_bstr_(""), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2410     VariantClear(&dest);
2411
2412     hr = ISAXContentHandler_startDocument(content);
2413     EXPECT_HR(hr, S_OK);
2414
2415     hr = ISAXContentHandler_startElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("abcdef"), 3, NULL);
2416     EXPECT_HR(hr, S_OK);
2417
2418     V_VT(&dest) = VT_EMPTY;
2419     hr = IMXWriter_get_output(writer, &dest);
2420     EXPECT_HR(hr, S_OK);
2421     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2422     ok(!lstrcmpW(_bstr_("<abc>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2423     VariantClear(&dest);
2424
2425     ISAXContentHandler_endDocument(content);
2426     IMXWriter_flush(writer);
2427
2428     hr = ISAXContentHandler_endElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("abdcdef"), 3);
2429     EXPECT_HR(hr, S_OK);
2430     V_VT(&dest) = VT_EMPTY;
2431     hr = IMXWriter_get_output(writer, &dest);
2432     EXPECT_HR(hr, S_OK);
2433     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2434     ok(!lstrcmpW(_bstr_("<abc></abd>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2435     VariantClear(&dest);
2436
2437     ISAXContentHandler_Release(content);
2438     IMXWriter_Release(writer);
2439     free_bstrs();
2440 }
2441
2442 struct writer_characters_t {
2443     const GUID *clsid;
2444     const char *data;
2445     const char *output;
2446 };
2447
2448 static const struct writer_characters_t writer_characters[] = {
2449     { &CLSID_MXXMLWriter,   "< > & \"", "&lt; &gt; &amp; \"" },
2450     { &CLSID_MXXMLWriter30, "< > & \"", "&lt; &gt; &amp; \"" },
2451     { &CLSID_MXXMLWriter40, "< > & \"", "&lt; &gt; &amp; \"" },
2452     { &CLSID_MXXMLWriter60, "< > & \"", "&lt; &gt; &amp; \"" },
2453     { NULL }
2454 };
2455
2456 static void test_mxwriter_characters(void)
2457 {
2458     static const WCHAR chardataW[] = {'T','E','S','T','C','H','A','R','D','A','T','A',' ','.',0};
2459     const struct writer_characters_t *table = writer_characters;
2460     ISAXContentHandler *content;
2461     IMXWriter *writer;
2462     VARIANT dest;
2463     HRESULT hr;
2464     int i = 0;
2465
2466     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2467             &IID_IMXWriter, (void**)&writer);
2468     EXPECT_HR(hr, S_OK);
2469
2470     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2471     EXPECT_HR(hr, S_OK);
2472
2473     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2474     EXPECT_HR(hr, S_OK);
2475
2476     hr = ISAXContentHandler_startDocument(content);
2477     EXPECT_HR(hr, S_OK);
2478
2479     hr = ISAXContentHandler_characters(content, NULL, 0);
2480     EXPECT_HR(hr, E_INVALIDARG);
2481
2482     hr = ISAXContentHandler_characters(content, chardataW, 0);
2483     EXPECT_HR(hr, S_OK);
2484
2485     hr = ISAXContentHandler_characters(content, chardataW, sizeof(chardataW)/sizeof(WCHAR) - 1);
2486     EXPECT_HR(hr, S_OK);
2487
2488     V_VT(&dest) = VT_EMPTY;
2489     hr = IMXWriter_get_output(writer, &dest);
2490     EXPECT_HR(hr, S_OK);
2491     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2492     ok(!lstrcmpW(_bstr_("TESTCHARDATA ."), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2493     VariantClear(&dest);
2494
2495     hr = ISAXContentHandler_endDocument(content);
2496     EXPECT_HR(hr, S_OK);
2497
2498     ISAXContentHandler_Release(content);
2499     IMXWriter_Release(writer);
2500
2501     /* try empty characters data to see if element is closed */
2502     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2503             &IID_IMXWriter, (void**)&writer);
2504     EXPECT_HR(hr, S_OK);
2505
2506     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2507     EXPECT_HR(hr, S_OK);
2508
2509     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2510     EXPECT_HR(hr, S_OK);
2511
2512     hr = ISAXContentHandler_startDocument(content);
2513     EXPECT_HR(hr, S_OK);
2514
2515     hr = ISAXContentHandler_startElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("a"), 1, NULL);
2516     EXPECT_HR(hr, S_OK);
2517
2518     hr = ISAXContentHandler_characters(content, chardataW, 0);
2519     EXPECT_HR(hr, S_OK);
2520
2521     hr = ISAXContentHandler_endElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("a"), 1);
2522     EXPECT_HR(hr, S_OK);
2523
2524     V_VT(&dest) = VT_EMPTY;
2525     hr = IMXWriter_get_output(writer, &dest);
2526     EXPECT_HR(hr, S_OK);
2527     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2528     ok(!lstrcmpW(_bstr_("<a></a>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2529     VariantClear(&dest);
2530
2531     ISAXContentHandler_Release(content);
2532     IMXWriter_Release(writer);
2533
2534     /* batch tests */
2535     while (table->clsid)
2536     {
2537         ISAXContentHandler *content;
2538         IMXWriter *writer;
2539         HRESULT hr;
2540
2541         if (!is_clsid_supported(table->clsid, mxwriter_support_data))
2542         {
2543             table++;
2544             i++;
2545             continue;
2546         }
2547
2548         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
2549             &IID_IMXWriter, (void**)&writer);
2550         EXPECT_HR(hr, S_OK);
2551
2552         hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2553         EXPECT_HR(hr, S_OK);
2554
2555         hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2556         EXPECT_HR(hr, S_OK);
2557
2558         hr = ISAXContentHandler_startDocument(content);
2559         EXPECT_HR(hr, S_OK);
2560
2561         hr = ISAXContentHandler_characters(content, _bstr_(table->data), strlen(table->data));
2562         EXPECT_HR(hr, S_OK);
2563
2564         /* test output */
2565         if (hr == S_OK)
2566         {
2567             VARIANT dest;
2568
2569             V_VT(&dest) = VT_EMPTY;
2570             hr = IMXWriter_get_output(writer, &dest);
2571             EXPECT_HR(hr, S_OK);
2572             ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2573             ok(!lstrcmpW(_bstr_(table->output), V_BSTR(&dest)),
2574                 "test %d: got wrong content %s, expected %s\n", i, wine_dbgstr_w(V_BSTR(&dest)), table->output);
2575             VariantClear(&dest);
2576         }
2577
2578         table++;
2579         i++;
2580     }
2581
2582     free_bstrs();
2583 }
2584
2585 static const mxwriter_stream_test mxwriter_stream_tests[] = {
2586     {
2587         VARIANT_TRUE,"UTF-16",
2588         {
2589             {FALSE,(const BYTE*)szUtf16BOM,sizeof(szUtf16BOM),TRUE},
2590             {FALSE,(const BYTE*)szUtf16XML,sizeof(szUtf16XML)},
2591             {TRUE}
2592         }
2593     },
2594     {
2595         VARIANT_FALSE,"UTF-16",
2596         {
2597             {FALSE,(const BYTE*)szUtf16XML,sizeof(szUtf16XML)},
2598             {TRUE}
2599         }
2600     },
2601     {
2602         VARIANT_TRUE,"UTF-8",
2603         {
2604             {FALSE,(const BYTE*)szUtf8XML,sizeof(szUtf8XML)-1},
2605             /* For some reason Windows makes an empty write call when UTF-8 encoding is used
2606              * and the writer is released.
2607              */
2608             {FALSE,NULL,0},
2609             {TRUE}
2610         }
2611     },
2612     {
2613         VARIANT_TRUE,"utf-8",
2614         {
2615             {FALSE,(const BYTE*)utf8xml2,sizeof(utf8xml2)-1},
2616             /* For some reason Windows makes an empty write call when UTF-8 encoding is used
2617              * and the writer is released.
2618              */
2619             {FALSE,NULL,0},
2620             {TRUE}
2621         }
2622     },
2623     {
2624         VARIANT_TRUE,"UTF-16",
2625         {
2626             {FALSE,(const BYTE*)szUtf16BOM,sizeof(szUtf16BOM),TRUE},
2627             {FALSE,(const BYTE*)szUtf16XML,sizeof(szUtf16XML)},
2628             {TRUE}
2629         }
2630     },
2631     {
2632         VARIANT_TRUE,"UTF-16",
2633         {
2634             {FALSE,(const BYTE*)szUtf16BOM,sizeof(szUtf16BOM),TRUE,TRUE},
2635             {FALSE,(const BYTE*)szUtf16XML,sizeof(szUtf16XML)},
2636             {TRUE}
2637         }
2638     }
2639 };
2640
2641 static void test_mxwriter_stream(void)
2642 {
2643     IMXWriter *writer;
2644     ISAXContentHandler *content;
2645     HRESULT hr;
2646     VARIANT dest;
2647     IStream *stream;
2648     LARGE_INTEGER pos;
2649     ULARGE_INTEGER pos2;
2650     DWORD test_count = sizeof(mxwriter_stream_tests)/sizeof(mxwriter_stream_tests[0]);
2651
2652     for(current_stream_test_index = 0; current_stream_test_index < test_count; ++current_stream_test_index) {
2653         const mxwriter_stream_test *test = mxwriter_stream_tests+current_stream_test_index;
2654
2655         hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2656                 &IID_IMXWriter, (void**)&writer);
2657         ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
2658
2659         hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2660         ok(hr == S_OK, "QueryInterface(ISAXContentHandler) failed: %08x\n", hr);
2661
2662         hr = IMXWriter_put_encoding(writer, _bstr_(test->encoding));
2663         ok(hr == S_OK, "put_encoding failed with %08x on test %d\n", hr, current_stream_test_index);
2664
2665         V_VT(&dest) = VT_UNKNOWN;
2666         V_UNKNOWN(&dest) = (IUnknown*)&mxstream;
2667         hr = IMXWriter_put_output(writer, dest);
2668         ok(hr == S_OK, "put_output failed with %08x on test %d\n", hr, current_stream_test_index);
2669         VariantClear(&dest);
2670
2671         hr = IMXWriter_put_byteOrderMark(writer, test->bom);
2672         ok(hr == S_OK, "put_byteOrderMark failed with %08x on test %d\n", hr, current_stream_test_index);
2673
2674         current_write_test = test->expected_writes;
2675
2676         hr = ISAXContentHandler_startDocument(content);
2677         ok(hr == S_OK, "startDocument failed with %08x on test %d\n", hr, current_stream_test_index);
2678
2679         hr = ISAXContentHandler_endDocument(content);
2680         ok(hr == S_OK, "endDocument failed with %08x on test %d\n", hr, current_stream_test_index);
2681
2682         ISAXContentHandler_Release(content);
2683         IMXWriter_Release(writer);
2684
2685         ok(current_write_test->last, "The last %d write calls on test %d were missed\n",
2686             (int)(current_write_test-test->expected_writes), current_stream_test_index);
2687     }
2688
2689     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2690             &IID_IMXWriter, (void**)&writer);
2691     ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
2692
2693     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2694     ok(hr == S_OK, "CreateStreamOnHGlobal failed: %08x\n", hr);
2695
2696     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2697     ok(hr == S_OK, "QueryInterface(ISAXContentHandler) failed: %08x\n", hr);
2698
2699     hr = IMXWriter_put_encoding(writer, _bstr_("UTF-8"));
2700     ok(hr == S_OK, "put_encoding failed: %08x\n", hr);
2701
2702     V_VT(&dest) = VT_UNKNOWN;
2703     V_UNKNOWN(&dest) = (IUnknown*)stream;
2704     hr = IMXWriter_put_output(writer, dest);
2705     ok(hr == S_OK, "put_output failed: %08x\n", hr);
2706
2707     hr = ISAXContentHandler_startDocument(content);
2708     ok(hr == S_OK, "startDocument failed: %08x\n", hr);
2709
2710     /* Setting output of the mxwriter causes the current output to be flushed,
2711      * and the writer to start over.
2712      */
2713     V_VT(&dest) = VT_EMPTY;
2714     hr = IMXWriter_put_output(writer, dest);
2715     ok(hr == S_OK, "put_output failed: %08x\n", hr);
2716
2717     pos.QuadPart = 0;
2718     hr = IStream_Seek(stream, pos, STREAM_SEEK_CUR, &pos2);
2719     ok(hr == S_OK, "Seek failed: %08x\n", hr);
2720     ok(pos2.QuadPart != 0, "expected stream position moved\n");
2721
2722     hr = ISAXContentHandler_startDocument(content);
2723     ok(hr == S_OK, "startDocument failed: %08x\n", hr);
2724
2725     hr = ISAXContentHandler_endDocument(content);
2726     ok(hr == S_OK, "endDocument failed: %08x\n", hr);
2727
2728     V_VT(&dest) = VT_EMPTY;
2729     hr = IMXWriter_get_output(writer, &dest);
2730     ok(hr == S_OK, "get_output failed: %08x\n", hr);
2731     ok(V_VT(&dest) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&dest));
2732     ok(!lstrcmpW(_bstr_("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?>\r\n"), V_BSTR(&dest)),
2733             "Got wrong content: %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2734     VariantClear(&dest);
2735
2736     /* test when BOM is written to output stream */
2737     V_VT(&dest) = VT_EMPTY;
2738     hr = IMXWriter_put_output(writer, dest);
2739     EXPECT_HR(hr, S_OK);
2740
2741     pos.QuadPart = 0;
2742     hr = IStream_Seek(stream, pos, STREAM_SEEK_SET, NULL);
2743     EXPECT_HR(hr, S_OK);
2744
2745     V_VT(&dest) = VT_UNKNOWN;
2746     V_UNKNOWN(&dest) = (IUnknown*)stream;
2747     hr = IMXWriter_put_output(writer, dest);
2748     EXPECT_HR(hr, S_OK);
2749
2750     hr = IMXWriter_put_byteOrderMark(writer, VARIANT_TRUE);
2751     EXPECT_HR(hr, S_OK);
2752
2753     hr = IMXWriter_put_encoding(writer, _bstr_("UTF-16"));
2754     EXPECT_HR(hr, S_OK);
2755
2756     hr = ISAXContentHandler_startDocument(content);
2757     EXPECT_HR(hr, S_OK);
2758
2759     pos.QuadPart = 0;
2760     pos2.QuadPart = 0;
2761     hr = IStream_Seek(stream, pos, STREAM_SEEK_CUR, &pos2);
2762     EXPECT_HR(hr, S_OK);
2763     ok(pos2.QuadPart == 2, "got wrong position\n");
2764
2765     ISAXContentHandler_Release(content);
2766     IMXWriter_Release(writer);
2767
2768     free_bstrs();
2769 }
2770
2771 static void test_mxwriter_encoding(void)
2772 {
2773     ISAXContentHandler *content;
2774     IMXWriter *writer;
2775     IStream *stream;
2776     VARIANT dest;
2777     HRESULT hr;
2778     HGLOBAL g;
2779     char *ptr;
2780     BSTR s;
2781
2782     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2783             &IID_IMXWriter, (void**)&writer);
2784     EXPECT_HR(hr, S_OK);
2785
2786     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2787     EXPECT_HR(hr, S_OK);
2788
2789     hr = IMXWriter_put_encoding(writer, _bstr_("UTF-8"));
2790     EXPECT_HR(hr, S_OK);
2791
2792     hr = ISAXContentHandler_startDocument(content);
2793     EXPECT_HR(hr, S_OK);
2794
2795     hr = ISAXContentHandler_endDocument(content);
2796     EXPECT_HR(hr, S_OK);
2797
2798     /* The content is always re-encoded to UTF-16 when the output is
2799      * retrieved as a BSTR.
2800      */
2801     V_VT(&dest) = VT_EMPTY;
2802     hr = IMXWriter_get_output(writer, &dest);
2803     EXPECT_HR(hr, S_OK);
2804     ok(V_VT(&dest) == VT_BSTR, "Expected VT_BSTR, got %d\n", V_VT(&dest));
2805     ok(!lstrcmpW(_bstr_("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?>\r\n"), V_BSTR(&dest)),
2806             "got wrong content: %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2807     VariantClear(&dest);
2808
2809     /* switch encoding when something is written already */
2810     hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2811     EXPECT_HR(hr, S_OK);
2812
2813     V_VT(&dest) = VT_UNKNOWN;
2814     V_UNKNOWN(&dest) = (IUnknown*)stream;
2815     hr = IMXWriter_put_output(writer, dest);
2816     EXPECT_HR(hr, S_OK);
2817
2818     hr = IMXWriter_put_encoding(writer, _bstr_("UTF-8"));
2819     EXPECT_HR(hr, S_OK);
2820
2821     /* write empty element */
2822     hr = ISAXContentHandler_startElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("a"), 1, NULL);
2823     EXPECT_HR(hr, S_OK);
2824
2825     hr = ISAXContentHandler_endElement(content, _bstr_(""), 0, _bstr_(""), 0, _bstr_("a"), 1);
2826     EXPECT_HR(hr, S_OK);
2827
2828     /* switch */
2829     hr = IMXWriter_put_encoding(writer, _bstr_("UTF-16"));
2830     EXPECT_HR(hr, S_OK);
2831
2832     hr = IMXWriter_flush(writer);
2833     EXPECT_HR(hr, S_OK);
2834
2835     hr = GetHGlobalFromStream(stream, &g);
2836     EXPECT_HR(hr, S_OK);
2837
2838     ptr = GlobalLock(g);
2839     ok(!strncmp(ptr, "<a/>", 4), "got %c%c%c%c\n", ptr[0],ptr[1],ptr[2],ptr[3]);
2840     GlobalUnlock(g);
2841
2842     /* so output is unaffected, encoding name is stored however */
2843     hr = IMXWriter_get_encoding(writer, &s);
2844     EXPECT_HR(hr, S_OK);
2845     ok(!lstrcmpW(s, _bstr_("UTF-16")), "got %s\n", wine_dbgstr_w(s));
2846     SysFreeString(s);
2847
2848     IStream_Release(stream);
2849
2850     ISAXContentHandler_Release(content);
2851     IMXWriter_Release(writer);
2852
2853     free_bstrs();
2854 }
2855
2856 static void test_obj_dispex(IUnknown *obj)
2857 {
2858     static const WCHAR starW[] = {'*',0};
2859     DISPID dispid = DISPID_SAX_XMLREADER_GETFEATURE;
2860     IDispatchEx *dispex;
2861     IUnknown *unk;
2862     DWORD props;
2863     UINT ticnt;
2864     HRESULT hr;
2865     BSTR name;
2866
2867     hr = IUnknown_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2868     EXPECT_HR(hr, S_OK);
2869     if (FAILED(hr)) return;
2870
2871     ticnt = 0;
2872     hr = IDispatchEx_GetTypeInfoCount(dispex, &ticnt);
2873     EXPECT_HR(hr, S_OK);
2874     ok(ticnt == 1, "ticnt=%u\n", ticnt);
2875
2876     name = SysAllocString(starW);
2877     hr = IDispatchEx_DeleteMemberByName(dispex, name, fdexNameCaseSensitive);
2878     EXPECT_HR(hr, E_NOTIMPL);
2879     SysFreeString(name);
2880
2881     hr = IDispatchEx_DeleteMemberByDispID(dispex, dispid);
2882     EXPECT_HR(hr, E_NOTIMPL);
2883
2884     props = 0;
2885     hr = IDispatchEx_GetMemberProperties(dispex, dispid, grfdexPropCanAll, &props);
2886     EXPECT_HR(hr, E_NOTIMPL);
2887     ok(props == 0, "expected 0 got %d\n", props);
2888
2889     hr = IDispatchEx_GetMemberName(dispex, dispid, &name);
2890     EXPECT_HR(hr, E_NOTIMPL);
2891     if (SUCCEEDED(hr)) SysFreeString(name);
2892
2893     hr = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, DISPID_SAX_XMLREADER_GETFEATURE, &dispid);
2894     EXPECT_HR(hr, E_NOTIMPL);
2895
2896     hr = IDispatchEx_GetNameSpaceParent(dispex, &unk);
2897     EXPECT_HR(hr, E_NOTIMPL);
2898     if (hr == S_OK && unk) IUnknown_Release(unk);
2899
2900     IDispatchEx_Release(dispex);
2901 }
2902
2903 static void test_dispex(void)
2904 {
2905      IVBSAXXMLReader *vbreader;
2906      ISAXXMLReader *reader;
2907      IUnknown *unk;
2908      HRESULT hr;
2909
2910      hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
2911                 &IID_ISAXXMLReader, (void**)&reader);
2912      EXPECT_HR(hr, S_OK);
2913
2914      hr = ISAXXMLReader_QueryInterface(reader, &IID_IUnknown, (void**)&unk);
2915      EXPECT_HR(hr, S_OK);
2916      test_obj_dispex(unk);
2917      IUnknown_Release(unk);
2918
2919      hr = ISAXXMLReader_QueryInterface(reader, &IID_IVBSAXXMLReader, (void**)&vbreader);
2920      EXPECT_HR(hr, S_OK);
2921      hr = IVBSAXXMLReader_QueryInterface(vbreader, &IID_IUnknown, (void**)&unk);
2922      EXPECT_HR(hr, S_OK);
2923      test_obj_dispex(unk);
2924      IUnknown_Release(unk);
2925      IVBSAXXMLReader_Release(vbreader);
2926
2927      ISAXXMLReader_Release(reader);
2928 }
2929
2930 static void test_mxwriter_dispex(void)
2931 {
2932     IDispatchEx *dispex;
2933     IMXWriter *writer;
2934     IUnknown *unk;
2935     HRESULT hr;
2936
2937     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2938             &IID_IMXWriter, (void**)&writer);
2939     EXPECT_HR(hr, S_OK);
2940
2941     hr = IMXWriter_QueryInterface(writer, &IID_IDispatchEx, (void**)&dispex);
2942     EXPECT_HR(hr, S_OK);
2943     hr = IDispatchEx_QueryInterface(dispex, &IID_IUnknown, (void**)&unk);
2944     test_obj_dispex(unk);
2945     IUnknown_Release(unk);
2946     IDispatchEx_Release(dispex);
2947
2948     IMXWriter_Release(writer);
2949 }
2950
2951 static void test_mxwriter_comment(void)
2952 {
2953     static const WCHAR commentW[] = {'c','o','m','m','e','n','t',0};
2954     ISAXContentHandler *content;
2955     ISAXLexicalHandler *lexical;
2956     IMXWriter *writer;
2957     VARIANT dest;
2958     HRESULT hr;
2959
2960     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
2961             &IID_IMXWriter, (void**)&writer);
2962     EXPECT_HR(hr, S_OK);
2963
2964     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
2965     EXPECT_HR(hr, S_OK);
2966
2967     hr = IMXWriter_QueryInterface(writer, &IID_ISAXLexicalHandler, (void**)&lexical);
2968     EXPECT_HR(hr, S_OK);
2969
2970     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
2971     EXPECT_HR(hr, S_OK);
2972
2973     hr = ISAXContentHandler_startDocument(content);
2974     EXPECT_HR(hr, S_OK);
2975
2976     hr = ISAXLexicalHandler_comment(lexical, NULL, 0);
2977     EXPECT_HR(hr, E_INVALIDARG);
2978
2979     hr = ISAXLexicalHandler_comment(lexical, commentW, 0);
2980     EXPECT_HR(hr, S_OK);
2981
2982     V_VT(&dest) = VT_EMPTY;
2983     hr = IMXWriter_get_output(writer, &dest);
2984     EXPECT_HR(hr, S_OK);
2985     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2986     ok(!lstrcmpW(_bstr_("<!---->\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2987     VariantClear(&dest);
2988
2989     hr = ISAXLexicalHandler_comment(lexical, commentW, sizeof(commentW)/sizeof(WCHAR)-1);
2990     EXPECT_HR(hr, S_OK);
2991
2992     V_VT(&dest) = VT_EMPTY;
2993     hr = IMXWriter_get_output(writer, &dest);
2994     EXPECT_HR(hr, S_OK);
2995     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
2996     ok(!lstrcmpW(_bstr_("<!---->\r\n<!--comment-->\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
2997     VariantClear(&dest);
2998
2999     ISAXContentHandler_Release(content);
3000     ISAXLexicalHandler_Release(lexical);
3001     IMXWriter_Release(writer);
3002     free_bstrs();
3003 }
3004
3005 static void test_mxwriter_cdata(void)
3006 {
3007     ISAXContentHandler *content;
3008     ISAXLexicalHandler *lexical;
3009     IMXWriter *writer;
3010     VARIANT dest;
3011     HRESULT hr;
3012
3013     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
3014             &IID_IMXWriter, (void**)&writer);
3015     EXPECT_HR(hr, S_OK);
3016
3017     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
3018     EXPECT_HR(hr, S_OK);
3019
3020     hr = IMXWriter_QueryInterface(writer, &IID_ISAXLexicalHandler, (void**)&lexical);
3021     EXPECT_HR(hr, S_OK);
3022
3023     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
3024     EXPECT_HR(hr, S_OK);
3025
3026     hr = ISAXContentHandler_startDocument(content);
3027     EXPECT_HR(hr, S_OK);
3028
3029     hr = ISAXLexicalHandler_startCDATA(lexical);
3030     EXPECT_HR(hr, S_OK);
3031
3032     V_VT(&dest) = VT_EMPTY;
3033     hr = IMXWriter_get_output(writer, &dest);
3034     EXPECT_HR(hr, S_OK);
3035     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
3036     ok(!lstrcmpW(_bstr_("<![CDATA["), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
3037     VariantClear(&dest);
3038
3039     hr = ISAXLexicalHandler_startCDATA(lexical);
3040     EXPECT_HR(hr, S_OK);
3041
3042     /* all these are escaped for text nodes */
3043     hr = ISAXContentHandler_characters(content, _bstr_("< > & \""), 7);
3044     EXPECT_HR(hr, S_OK);
3045
3046     hr = ISAXLexicalHandler_endCDATA(lexical);
3047     EXPECT_HR(hr, S_OK);
3048
3049     V_VT(&dest) = VT_EMPTY;
3050     hr = IMXWriter_get_output(writer, &dest);
3051     EXPECT_HR(hr, S_OK);
3052     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
3053     ok(!lstrcmpW(_bstr_("<![CDATA[<![CDATA[< > & \"]]>"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
3054     VariantClear(&dest);
3055
3056     ISAXContentHandler_Release(content);
3057     ISAXLexicalHandler_Release(lexical);
3058     IMXWriter_Release(writer);
3059     free_bstrs();
3060 }
3061
3062 static void test_mxwriter_dtd(void)
3063 {
3064     static const WCHAR nameW[] = {'n','a','m','e'};
3065     static const WCHAR pubW[] = {'p','u','b'};
3066     static const WCHAR sysW[] = {'s','y','s'};
3067     ISAXContentHandler *content;
3068     ISAXLexicalHandler *lexical;
3069     IMXWriter *writer;
3070     VARIANT dest;
3071     HRESULT hr;
3072
3073     hr = CoCreateInstance(&CLSID_MXXMLWriter, NULL, CLSCTX_INPROC_SERVER,
3074             &IID_IMXWriter, (void**)&writer);
3075     EXPECT_HR(hr, S_OK);
3076
3077     hr = IMXWriter_QueryInterface(writer, &IID_ISAXContentHandler, (void**)&content);
3078     EXPECT_HR(hr, S_OK);
3079
3080     hr = IMXWriter_QueryInterface(writer, &IID_ISAXLexicalHandler, (void**)&lexical);
3081     EXPECT_HR(hr, S_OK);
3082
3083     hr = IMXWriter_put_omitXMLDeclaration(writer, VARIANT_TRUE);
3084     EXPECT_HR(hr, S_OK);
3085
3086     hr = ISAXContentHandler_startDocument(content);
3087     EXPECT_HR(hr, S_OK);
3088
3089     hr = ISAXLexicalHandler_startDTD(lexical, NULL, 0, NULL, 0, NULL, 0);
3090     EXPECT_HR(hr, E_INVALIDARG);
3091
3092     hr = ISAXLexicalHandler_startDTD(lexical, NULL, 0, pubW, sizeof(pubW)/sizeof(WCHAR), NULL, 0);
3093     EXPECT_HR(hr, E_INVALIDARG);
3094
3095     hr = ISAXLexicalHandler_startDTD(lexical, NULL, 0, NULL, 0, sysW, sizeof(sysW)/sizeof(WCHAR));
3096     EXPECT_HR(hr, E_INVALIDARG);
3097
3098     hr = ISAXLexicalHandler_startDTD(lexical, NULL, 0, pubW, sizeof(pubW)/sizeof(WCHAR), sysW, sizeof(sysW)/sizeof(WCHAR));
3099     EXPECT_HR(hr, E_INVALIDARG);
3100
3101     hr = ISAXLexicalHandler_startDTD(lexical, nameW, sizeof(nameW)/sizeof(WCHAR), NULL, 0, NULL, 0);
3102     EXPECT_HR(hr, S_OK);
3103
3104     V_VT(&dest) = VT_EMPTY;
3105     hr = IMXWriter_get_output(writer, &dest);
3106     EXPECT_HR(hr, S_OK);
3107     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
3108     ok(!lstrcmpW(_bstr_("<!DOCTYPE name [\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
3109     VariantClear(&dest);
3110
3111     /* system id is required if public is present */
3112     hr = ISAXLexicalHandler_startDTD(lexical, nameW, sizeof(nameW)/sizeof(WCHAR), pubW, sizeof(pubW)/sizeof(WCHAR), NULL, 0);
3113     EXPECT_HR(hr, E_INVALIDARG);
3114
3115     hr = ISAXLexicalHandler_startDTD(lexical, nameW, sizeof(nameW)/sizeof(WCHAR),
3116         pubW, sizeof(pubW)/sizeof(WCHAR), sysW, sizeof(sysW)/sizeof(WCHAR));
3117     EXPECT_HR(hr, S_OK);
3118
3119     V_VT(&dest) = VT_EMPTY;
3120     hr = IMXWriter_get_output(writer, &dest);
3121     EXPECT_HR(hr, S_OK);
3122     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
3123     ok(!lstrcmpW(_bstr_("<!DOCTYPE name [\r\n<!DOCTYPE name PUBLIC \"pub\""
3124         "<!DOCTYPE name PUBLIC \"pub\" \"sys\" [\r\n"), V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
3125     VariantClear(&dest);
3126
3127     hr = ISAXLexicalHandler_endDTD(lexical);
3128     EXPECT_HR(hr, S_OK);
3129
3130     hr = ISAXLexicalHandler_endDTD(lexical);
3131     EXPECT_HR(hr, S_OK);
3132
3133     V_VT(&dest) = VT_EMPTY;
3134     hr = IMXWriter_get_output(writer, &dest);
3135     EXPECT_HR(hr, S_OK);
3136     ok(V_VT(&dest) == VT_BSTR, "got %d\n", V_VT(&dest));
3137     ok(!lstrcmpW(_bstr_("<!DOCTYPE name [\r\n<!DOCTYPE name PUBLIC \"pub\""
3138          "<!DOCTYPE name PUBLIC \"pub\" \"sys\" [\r\n]>\r\n]>\r\n"),
3139         V_BSTR(&dest)), "got wrong content %s\n", wine_dbgstr_w(V_BSTR(&dest)));
3140     VariantClear(&dest);
3141
3142     ISAXContentHandler_Release(content);
3143     ISAXLexicalHandler_Release(lexical);
3144     IMXWriter_Release(writer);
3145     free_bstrs();
3146 }
3147
3148 typedef struct {
3149     const CLSID *clsid;
3150     const char *uri;
3151     const char *local;
3152     const char *qname;
3153     const char *type;
3154     const char *value;
3155     HRESULT hr;
3156 } addattribute_test_t;
3157
3158 static const addattribute_test_t addattribute_data[] = {
3159     { &CLSID_SAXAttributes,   NULL, NULL, "ns:qname", NULL, "value", E_INVALIDARG },
3160     { &CLSID_SAXAttributes30, NULL, NULL, "ns:qname", NULL, "value", E_INVALIDARG },
3161     { &CLSID_SAXAttributes40, NULL, NULL, "ns:qname", NULL, "value", E_INVALIDARG },
3162     { &CLSID_SAXAttributes60, NULL, NULL, "ns:qname", NULL, "value", S_OK },
3163
3164     { &CLSID_SAXAttributes,   NULL, "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3165     { &CLSID_SAXAttributes30, NULL, "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3166     { &CLSID_SAXAttributes40, NULL, "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3167     { &CLSID_SAXAttributes60, NULL, "qname", "ns:qname", NULL, "value", S_OK },
3168
3169     { &CLSID_SAXAttributes,   "uri", "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3170     { &CLSID_SAXAttributes30, "uri", "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3171     { &CLSID_SAXAttributes40, "uri", "qname", "ns:qname", NULL, "value", E_INVALIDARG },
3172     { &CLSID_SAXAttributes60, "uri", "qname", "ns:qname", NULL, "value", S_OK },
3173
3174     { &CLSID_SAXAttributes,   "uri", "qname", "ns:qname", "type", "value", S_OK },
3175     { &CLSID_SAXAttributes30, "uri", "qname", "ns:qname", "type", "value", S_OK },
3176     { &CLSID_SAXAttributes40, "uri", "qname", "ns:qname", "type", "value", S_OK },
3177     { &CLSID_SAXAttributes60, "uri", "qname", "ns:qname", "type", "value", S_OK },
3178
3179     { NULL }
3180 };
3181
3182 static void test_mxattr_addAttribute(void)
3183 {
3184     const addattribute_test_t *table = addattribute_data;
3185     int i = 0;
3186
3187     while (table->clsid)
3188     {
3189         ISAXAttributes *saxattr;
3190         IMXAttributes *mxattr;
3191         HRESULT hr;
3192         int len;
3193
3194         if (!is_clsid_supported(table->clsid, mxattributes_support_data))
3195         {
3196             table++;
3197             i++;
3198             continue;
3199         }
3200
3201         hr = CoCreateInstance(table->clsid, NULL, CLSCTX_INPROC_SERVER,
3202             &IID_IMXAttributes, (void**)&mxattr);
3203         EXPECT_HR(hr, S_OK);
3204
3205         hr = IMXAttributes_QueryInterface(mxattr, &IID_ISAXAttributes, (void**)&saxattr);
3206         EXPECT_HR(hr, S_OK);
3207
3208         /* SAXAttributes30 and SAXAttributes60 both crash on this test */
3209         if (IsEqualGUID(table->clsid, &CLSID_SAXAttributes) ||
3210             IsEqualGUID(table->clsid, &CLSID_SAXAttributes30))
3211         {
3212             hr = ISAXAttributes_getLength(saxattr, NULL);
3213             EXPECT_HR(hr, E_POINTER);
3214         }
3215
3216         len = -1;
3217         hr = ISAXAttributes_getLength(saxattr, &len);
3218         EXPECT_HR(hr, S_OK);
3219         ok(len == 0, "got %d\n", len);
3220
3221         hr = IMXAttributes_addAttribute(mxattr, _bstr_(table->uri), _bstr_(table->local),
3222             _bstr_(table->qname), _bstr_(table->type), _bstr_(table->value));
3223         ok(hr == table->hr, "%d: got 0x%08x, expected 0x%08x\n", i, hr, table->hr);
3224
3225         len = -1;
3226         hr = ISAXAttributes_getLength(saxattr, &len);
3227         EXPECT_HR(hr, S_OK);
3228         if (table->hr == S_OK)
3229             ok(len == 1, "%d: got %d length, expected 0\n", i, len);
3230         else
3231             ok(len == 0, "%d: got %d length, expected 1\n", i, len);
3232
3233         ISAXAttributes_Release(saxattr);
3234         IMXAttributes_Release(mxattr);
3235
3236         table++;
3237         i++;
3238     }
3239
3240     free_bstrs();
3241 }
3242
3243 static void test_mxattr_clear(void)
3244 {
3245     ISAXAttributes *saxattr;
3246     IMXAttributes *mxattr;
3247     const WCHAR *ptr;
3248     HRESULT hr;
3249     int len;
3250
3251     hr = CoCreateInstance(&CLSID_SAXAttributes, NULL, CLSCTX_INPROC_SERVER,
3252         &IID_IMXAttributes, (void**)&mxattr);
3253     EXPECT_HR(hr, S_OK);
3254
3255     hr = IMXAttributes_QueryInterface(mxattr, &IID_ISAXAttributes, (void**)&saxattr);
3256     EXPECT_HR(hr, S_OK);
3257
3258     hr = ISAXAttributes_getQName(saxattr, 0, NULL, NULL);
3259     EXPECT_HR(hr, E_INVALIDARG);
3260
3261     hr = ISAXAttributes_getQName(saxattr, 0, &ptr, &len);
3262     EXPECT_HR(hr, E_INVALIDARG);
3263
3264     hr = IMXAttributes_addAttribute(mxattr, _bstr_("uri"), _bstr_("local"),
3265         _bstr_("qname"), _bstr_("type"), _bstr_("value"));
3266     EXPECT_HR(hr, S_OK);
3267
3268     len = -1;
3269     hr = ISAXAttributes_getLength(saxattr, &len);
3270     EXPECT_HR(hr, S_OK);
3271     ok(len == 1, "got %d\n", len);
3272
3273     len = -1;
3274     hr = ISAXAttributes_getQName(saxattr, 0, NULL, &len);
3275     EXPECT_HR(hr, E_POINTER);
3276     ok(len == -1, "got %d\n", len);
3277
3278     ptr = (void*)0xdeadbeef;
3279     hr = ISAXAttributes_getQName(saxattr, 0, &ptr, NULL);
3280     EXPECT_HR(hr, E_POINTER);
3281     ok(ptr == (void*)0xdeadbeef, "got %p\n", ptr);
3282
3283     len = 0;
3284     hr = ISAXAttributes_getQName(saxattr, 0, &ptr, &len);
3285     EXPECT_HR(hr, S_OK);
3286     ok(len == 5, "got %d\n", len);
3287     ok(!lstrcmpW(ptr, _bstr_("qname")), "got %s\n", wine_dbgstr_w(ptr));
3288
3289     hr = IMXAttributes_clear(mxattr);
3290     EXPECT_HR(hr, S_OK);
3291
3292     len = -1;
3293     hr = ISAXAttributes_getLength(saxattr, &len);
3294     EXPECT_HR(hr, S_OK);
3295     ok(len == 0, "got %d\n", len);
3296
3297     len = -1;
3298     ptr = (void*)0xdeadbeef;
3299     hr = ISAXAttributes_getQName(saxattr, 0, &ptr, &len);
3300     EXPECT_HR(hr, E_INVALIDARG);
3301     ok(len == -1, "got %d\n", len);
3302     ok(ptr == (void*)0xdeadbeef, "got %p\n", ptr);
3303
3304     IMXAttributes_Release(mxattr);
3305     ISAXAttributes_Release(saxattr);
3306     free_bstrs();
3307 }
3308
3309 START_TEST(saxreader)
3310 {
3311     ISAXXMLReader *reader;
3312     HRESULT hr;
3313
3314     hr = CoInitialize(NULL);
3315     ok(hr == S_OK, "failed to init com\n");
3316
3317     hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
3318             &IID_ISAXXMLReader, (void**)&reader);
3319
3320     if(FAILED(hr))
3321     {
3322         skip("Failed to create SAXXMLReader instance\n");
3323         CoUninitialize();
3324         return;
3325     }
3326     ISAXXMLReader_Release(reader);
3327
3328     test_saxreader(0);
3329     test_saxreader(3);
3330     test_saxreader(6);
3331     test_saxreader_properties();
3332     test_saxreader_features();
3333     test_encoding();
3334     test_dispex();
3335
3336     /* MXXMLWriter tests */
3337     get_mxwriter_support_data(mxwriter_support_data);
3338     if (is_clsid_supported(&CLSID_MXXMLWriter, mxwriter_support_data))
3339     {
3340         test_mxwriter_handlers();
3341         test_mxwriter_startenddocument();
3342         test_mxwriter_startendelement();
3343         test_mxwriter_characters();
3344         test_mxwriter_comment();
3345         test_mxwriter_cdata();
3346         test_mxwriter_dtd();
3347         test_mxwriter_properties();
3348         test_mxwriter_flush();
3349         test_mxwriter_stream();
3350         test_mxwriter_encoding();
3351         test_mxwriter_dispex();
3352     }
3353     else
3354         win_skip("MXXMLWriter not supported\n");
3355
3356     /* SAXAttributes tests */
3357     get_mxattributes_support_data(mxattributes_support_data);
3358     if (is_clsid_supported(&CLSID_SAXAttributes, mxattributes_support_data))
3359     {
3360         test_mxattr_addAttribute();
3361         test_mxattr_clear();
3362     }
3363     else
3364         skip("SAXAttributes not supported\n");
3365
3366     CoUninitialize();
3367 }