4 * Copyright 2008 Piotr Caban
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
36 CH_PUTDOCUMENTLOCATOR,
39 CH_STARTPREFIXMAPPING,
44 CH_IGNORABLEWHITESPACE,
45 CH_PROCESSINGINSTRUCTION,
49 DEFINE_GUID(CLSID_SAXXMLReader60, 0x88d96a0c, 0xf192, 0x11d4, 0xa6,0x5f, 0x00,0x40,0x96,0x32,0x51,0xe5);
51 static const WCHAR szSimpleXML[] = {
52 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','\"','1','.','0','\"',' ','?','>','\n',
53 '<','B','a','n','k','A','c','c','o','u','n','t','>','\n',
54 ' ',' ',' ','<','N','u','m','b','e','r','>','1','2','3','4','<','/','N','u','m','b','e','r','>','\n',
55 ' ',' ',' ','<','N','a','m','e','>','C','a','p','t','a','i','n',' ','A','h','a','b','<','/','N','a','m','e','>','\n',
56 '<','/','B','a','n','k','A','c','c','o','u','n','t','>','\n','\0'
59 static const WCHAR szCarriageRetTest[] = {
60 '<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"','?','>','\r','\n',
61 '<','B','a','n','k','A','c','c','o','u','n','t','>','\r','\n',
62 '\t','<','N','u','m','b','e','r','>','1','2','3','4','<','/','N','u','m','b','e','r','>','\r','\n',
63 '\t','<','N','a','m','e','>','C','a','p','t','a','i','n',' ','A','h','a','b','<','/','N','a','m','e','>','\r','\n',
64 '<','/','B','a','n','k','A','c','c','o','u','n','t','>','\0'
67 static const CHAR szTestXML[] =
68 "<?xml version=\"1.0\" ?>\n"
70 " <Number>1234</Number>\n"
71 " <Name>Captain Ahab</Name>\n"
74 typedef struct _contenthandlercheck {
81 } content_handler_test;
83 static content_handler_test contentHandlerTest1[] = {
84 { CH_PUTDOCUMENTLOCATOR, 0, 0 },
85 { CH_STARTDOCUMENT, 0, 0 },
86 { CH_STARTELEMENT, 2, 14, "", "BankAccount", "BankAccount" },
87 { CH_CHARACTERS, 2, 14, "\n " },
88 { CH_STARTELEMENT, 3, 12, "", "Number", "Number" },
89 { CH_CHARACTERS, 3, 12, "1234" },
90 { CH_ENDELEMENT, 3, 18, "", "Number", "Number" },
91 { CH_CHARACTERS, 3, 25, "\n " },
92 { CH_STARTELEMENT, 4, 10, "", "Name", "Name" },
93 { CH_CHARACTERS, 4, 10, "Captain Ahab" },
94 { CH_ENDELEMENT, 4, 24, "", "Name", "Name" },
95 { CH_CHARACTERS, 4, 29, "\n" },
96 { CH_ENDELEMENT, 5, 3, "", "BankAccount", "BankAccount" },
97 { CH_ENDDOCUMENT, 0, 0 },
101 static content_handler_test contentHandlerTest2[] = {
102 { CH_PUTDOCUMENTLOCATOR, 0, 0 },
103 { CH_STARTDOCUMENT, 0, 0 },
104 { CH_STARTELEMENT, 2, 14, "", "BankAccount", "BankAccount" },
105 { CH_CHARACTERS, 2, 14, "\n" },
106 { CH_CHARACTERS, 2, 16, "\t" },
107 { CH_STARTELEMENT, 3, 10, "", "Number", "Number" },
108 { CH_CHARACTERS, 3, 10, "1234" },
109 { CH_ENDELEMENT, 3, 16, "", "Number", "Number" },
110 { CH_CHARACTERS, 3, 23, "\n" },
111 { CH_CHARACTERS, 3, 25, "\t" },
112 { CH_STARTELEMENT, 4, 8, "", "Name", "Name" },
113 { CH_CHARACTERS, 4, 8, "Captain Ahab" },
114 { CH_ENDELEMENT, 4, 22, "", "Name", "Name" },
115 { CH_CHARACTERS, 4, 27, "\n" },
116 { CH_ENDELEMENT, 5, 3, "", "BankAccount", "BankAccount" },
117 { CH_ENDDOCUMENT, 0, 0 },
121 static content_handler_test *expectCall;
122 static ISAXLocator *locator;
124 static void test_saxstr(unsigned line, const WCHAR *szStr, int nStr, const char *szTest)
130 ok_(__FILE__,line) (szStr == NULL, "szStr != NULL\n");
131 ok_(__FILE__,line) (nStr == 0, "nStr = %d, expected 0\n", nStr);
135 len = strlen(szTest);
136 ok_(__FILE__,line) (len == nStr, "nStr = %d, expected %d (%s)\n", nStr, len, szTest);
140 MultiByteToWideChar(CP_ACP, 0, szTest, -1, buf, sizeof(buf)/sizeof(WCHAR));
141 ok_(__FILE__,line) (!memcmp(szStr, buf, len*sizeof(WCHAR)), "unexpected szStr %s, expected %s\n",
142 wine_dbgstr_wn(szStr, nStr), szTest);
145 static BOOL test_expect_call(CH id)
147 ok(expectCall->id == id, "unexpected call %d, expected %d\n", id, expectCall->id);
148 return expectCall->id == id;
151 static void test_locator(unsigned line, int loc_line, int loc_column)
154 ISAXLocator_getLineNumber(locator, &rline);
155 ISAXLocator_getColumnNumber(locator, &rcolumn);
157 ok_(__FILE__,line) (rline == loc_line,
158 "unexpected line %d, expected %d\n", rline, loc_line);
159 ok_(__FILE__,line) (rcolumn == loc_column,
160 "unexpected column %d, expected %d\n", rcolumn, loc_column);
163 static HRESULT WINAPI contentHandler_QueryInterface(
164 ISAXContentHandler* iface,
170 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXContentHandler))
176 return E_NOINTERFACE;
182 static ULONG WINAPI contentHandler_AddRef(
183 ISAXContentHandler* iface)
188 static ULONG WINAPI contentHandler_Release(
189 ISAXContentHandler* iface)
194 static HRESULT WINAPI contentHandler_putDocumentLocator(
195 ISAXContentHandler* iface,
196 ISAXLocator *pLocator)
198 if(!test_expect_call(CH_PUTDOCUMENTLOCATOR))
202 test_locator(__LINE__, expectCall->line, expectCall->column);
208 static HRESULT WINAPI contentHandler_startDocument(
209 ISAXContentHandler* iface)
211 if(!test_expect_call(CH_STARTDOCUMENT))
214 test_locator(__LINE__, expectCall->line, expectCall->column);
220 static HRESULT WINAPI contentHandler_endDocument(
221 ISAXContentHandler* iface)
223 if(!test_expect_call(CH_ENDDOCUMENT))
226 test_locator(__LINE__, expectCall->line, expectCall->column);
232 static HRESULT WINAPI contentHandler_startPrefixMapping(
233 ISAXContentHandler* iface,
234 const WCHAR *pPrefix,
239 if(!test_expect_call(CH_ENDDOCUMENT))
242 test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
243 test_saxstr(__LINE__, pUri, nUri, expectCall->arg2);
244 test_locator(__LINE__, expectCall->line, expectCall->column);
250 static HRESULT WINAPI contentHandler_endPrefixMapping(
251 ISAXContentHandler* iface,
252 const WCHAR *pPrefix,
255 if(!test_expect_call(CH_ENDPREFIXMAPPING))
258 test_saxstr(__LINE__, pPrefix, nPrefix, expectCall->arg1);
259 test_locator(__LINE__, expectCall->line, expectCall->column);
265 static HRESULT WINAPI contentHandler_startElement(
266 ISAXContentHandler* iface,
267 const WCHAR *pNamespaceUri,
269 const WCHAR *pLocalName,
273 ISAXAttributes *pAttr)
275 if(!test_expect_call(CH_STARTELEMENT))
278 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
279 test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
280 test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
281 test_locator(__LINE__, expectCall->line, expectCall->column);
287 static HRESULT WINAPI contentHandler_endElement(
288 ISAXContentHandler* iface,
289 const WCHAR *pNamespaceUri,
291 const WCHAR *pLocalName,
296 if(!test_expect_call(CH_ENDELEMENT))
299 test_saxstr(__LINE__, pNamespaceUri, nNamespaceUri, expectCall->arg1);
300 test_saxstr(__LINE__, pLocalName, nLocalName, expectCall->arg2);
301 test_saxstr(__LINE__, pQName, nQName, expectCall->arg3);
302 test_locator(__LINE__, expectCall->line, expectCall->column);
308 static HRESULT WINAPI contentHandler_characters(
309 ISAXContentHandler* iface,
313 if(!test_expect_call(CH_CHARACTERS))
316 test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
317 test_locator(__LINE__, expectCall->line, expectCall->column);
323 static HRESULT WINAPI contentHandler_ignorableWhitespace(
324 ISAXContentHandler* iface,
328 if(!test_expect_call(CH_IGNORABLEWHITESPACE))
331 test_saxstr(__LINE__, pChars, nChars, expectCall->arg1);
332 test_locator(__LINE__, expectCall->line, expectCall->column);
338 static HRESULT WINAPI contentHandler_processingInstruction(
339 ISAXContentHandler* iface,
340 const WCHAR *pTarget,
345 if(!test_expect_call(CH_PROCESSINGINSTRUCTION))
348 test_saxstr(__LINE__, pTarget, nTarget, expectCall->arg1);
349 test_saxstr(__LINE__, pData, nData, expectCall->arg2);
350 test_locator(__LINE__, expectCall->line, expectCall->column);
356 static HRESULT WINAPI contentHandler_skippedEntity(
357 ISAXContentHandler* iface,
361 if(!test_expect_call(CH_SKIPPEDENTITY))
364 test_saxstr(__LINE__, pName, nName, expectCall->arg1);
365 test_locator(__LINE__, expectCall->line, expectCall->column);
372 static const ISAXContentHandlerVtbl contentHandlerVtbl =
374 contentHandler_QueryInterface,
375 contentHandler_AddRef,
376 contentHandler_Release,
377 contentHandler_putDocumentLocator,
378 contentHandler_startDocument,
379 contentHandler_endDocument,
380 contentHandler_startPrefixMapping,
381 contentHandler_endPrefixMapping,
382 contentHandler_startElement,
383 contentHandler_endElement,
384 contentHandler_characters,
385 contentHandler_ignorableWhitespace,
386 contentHandler_processingInstruction,
387 contentHandler_skippedEntity
390 static ISAXContentHandler contentHandler = { &contentHandlerVtbl };
392 static HRESULT WINAPI isaxerrorHandler_QueryInterface(
393 ISAXErrorHandler* iface,
399 if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ISAXErrorHandler))
405 return E_NOINTERFACE;
411 static ULONG WINAPI isaxerrorHandler_AddRef(
412 ISAXErrorHandler* iface)
417 static ULONG WINAPI isaxerrorHandler_Release(
418 ISAXErrorHandler* iface)
423 static HRESULT WINAPI isaxerrorHandler_error(
424 ISAXErrorHandler* iface,
425 ISAXLocator *pLocator,
426 const WCHAR *pErrorMessage,
432 static HRESULT WINAPI isaxerrorHandler_fatalError(
433 ISAXErrorHandler* iface,
434 ISAXLocator *pLocator,
435 const WCHAR *pErrorMessage,
441 static HRESULT WINAPI isaxerrorHanddler_ignorableWarning(
442 ISAXErrorHandler* iface,
443 ISAXLocator *pLocator,
444 const WCHAR *pErrorMessage,
450 static const ISAXErrorHandlerVtbl errorHandlerVtbl =
452 isaxerrorHandler_QueryInterface,
453 isaxerrorHandler_AddRef,
454 isaxerrorHandler_Release,
455 isaxerrorHandler_error,
456 isaxerrorHandler_fatalError,
457 isaxerrorHanddler_ignorableWarning
460 static ISAXErrorHandler errorHandler = { &errorHandlerVtbl };
462 static void test_saxreader(void)
465 ISAXXMLReader *reader = NULL;
467 ISAXContentHandler *lpContentHandler;
468 ISAXErrorHandler *lpErrorHandler;
470 SAFEARRAYBOUND SADim[1];
471 char *pSAData = NULL;
473 ULARGE_INTEGER liSize;
477 static const CHAR testXmlA[] = "test.xml";
478 static const WCHAR testXmlW[] = {'t','e','s','t','.','x','m','l',0};
479 IXMLDOMDocument *domDocument;
483 hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
484 &IID_ISAXXMLReader, (LPVOID*)&reader);
485 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
487 hr = ISAXXMLReader_getContentHandler(reader, NULL);
488 ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
490 hr = ISAXXMLReader_getErrorHandler(reader, NULL);
491 ok(hr == E_POINTER, "Expected E_POINTER, got %08x\n", hr);
493 hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
494 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
495 ok(lpContentHandler == NULL, "Expected %p, got %p\n", NULL, lpContentHandler);
497 hr = ISAXXMLReader_getErrorHandler(reader, &lpErrorHandler);
498 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
499 ok(lpErrorHandler == NULL, "Expected %p, got %p\n", NULL, lpErrorHandler);
501 hr = ISAXXMLReader_putContentHandler(reader, NULL);
502 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
504 hr = ISAXXMLReader_putContentHandler(reader, &contentHandler);
505 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
507 hr = ISAXXMLReader_putErrorHandler(reader, &errorHandler);
508 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
510 hr = ISAXXMLReader_getContentHandler(reader, &lpContentHandler);
511 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
512 ok(lpContentHandler == &contentHandler, "Expected %p, got %p\n", &contentHandler, lpContentHandler);
514 V_VT(&var) = VT_BSTR;
515 V_BSTR(&var) = SysAllocString(szSimpleXML);
517 expectCall = contentHandlerTest1;
518 hr = ISAXXMLReader_parse(reader, var);
519 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
520 test_expect_call(CH_ENDTEST);
525 SADim[0].cElements= sizeof(szTestXML)-1;
526 pSA = SafeArrayCreate(VT_UI1, 1, SADim);
527 SafeArrayAccessData(pSA, (void**)&pSAData);
528 memcpy(pSAData, szTestXML, sizeof(szTestXML)-1);
529 SafeArrayUnaccessData(pSA);
530 V_VT(&var) = VT_ARRAY|VT_UI1;
533 expectCall = contentHandlerTest1;
534 hr = ISAXXMLReader_parse(reader, var);
535 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
536 test_expect_call(CH_ENDTEST);
538 SafeArrayDestroy(pSA);
540 CreateStreamOnHGlobal(NULL, TRUE, &iStream);
541 liSize.QuadPart = strlen(szTestXML);
542 IStream_SetSize(iStream, liSize);
543 IStream_Write(iStream, szTestXML, strlen(szTestXML), &bytesWritten);
545 IStream_Seek(iStream, liPos, STREAM_SEEK_SET, NULL);
546 V_VT(&var) = VT_UNKNOWN|VT_DISPATCH;
547 V_UNKNOWN(&var) = (IUnknown*)iStream;
549 expectCall = contentHandlerTest1;
550 hr = ISAXXMLReader_parse(reader, var);
551 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
552 test_expect_call(CH_ENDTEST);
554 IStream_Release(iStream);
556 V_VT(&var) = VT_BSTR;
557 V_BSTR(&var) = SysAllocString(szCarriageRetTest);
559 expectCall = contentHandlerTest2;
560 hr = ISAXXMLReader_parse(reader, var);
561 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
562 test_expect_call(CH_ENDTEST);
566 file = CreateFileA(testXmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
567 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
568 WriteFile(file, szTestXML, sizeof(szTestXML)-1, &bytesWritten, NULL);
571 expectCall = contentHandlerTest1;
572 hr = ISAXXMLReader_parseURL(reader, testXmlW);
573 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
574 test_expect_call(CH_ENDTEST);
576 DeleteFileA(testXmlA);
578 hr = CoCreateInstance(&CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
579 &IID_IXMLDOMDocument, (LPVOID*)&domDocument);
582 skip("Failed to create DOMDocument instance\n");
585 bstrData = SysAllocString(szSimpleXML);
586 hr = IXMLDOMDocument_loadXML(domDocument, bstrData, &vBool);
587 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
588 V_VT(&var) = VT_UNKNOWN;
589 V_UNKNOWN(&var) = (IUnknown*)domDocument;
591 expectCall = contentHandlerTest2;
592 hr = ISAXXMLReader_parse(reader, var);
593 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
594 test_expect_call(CH_ENDTEST);
595 IXMLDOMDocument_Release(domDocument);
597 ISAXXMLReader_Release(reader);
598 SysFreeString(bstrData);
601 /* UTF-8 data with UTF-8 BOM and UTF-16 in prolog */
602 static const CHAR UTF8BOMTest[] =
603 "\xEF\xBB\xBF<?xml version = \"1.0\" encoding = \"UTF-16\"?>\n"
606 struct enc_test_entry_t {
614 static const struct enc_test_entry_t encoding_test_data[] = {
615 { &CLSID_SAXXMLReader, "CLSID_SAXXMLReader", UTF8BOMTest, 0xc00ce56f, 1 },
616 { &CLSID_SAXXMLReader30, "CLSID_SAXXMLReader30", UTF8BOMTest, 0xc00ce56f, 1 },
617 { &CLSID_SAXXMLReader40, "CLSID_SAXXMLReader40", UTF8BOMTest, S_OK, 0 },
618 { &CLSID_SAXXMLReader60, "CLSID_SAXXMLReader60", UTF8BOMTest, S_OK, 0 },
622 static void test_encoding(void)
624 const struct enc_test_entry_t *entry = encoding_test_data;
625 static const WCHAR testXmlW[] = {'t','e','s','t','.','x','m','l',0};
626 static const CHAR testXmlA[] = "test.xml";
627 ISAXXMLReader *reader;
634 hr = CoCreateInstance(entry->guid, NULL, CLSCTX_INPROC_SERVER, &IID_ISAXXMLReader, (void**)&reader);
637 win_skip("can't create %s instance\n", entry->clsid);
642 file = CreateFileA(testXmlA, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
643 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
644 WriteFile(file, UTF8BOMTest, sizeof(UTF8BOMTest)-1, &written, NULL);
647 hr = ISAXXMLReader_parseURL(reader, testXmlW);
649 todo_wine ok(hr == entry->hr, "Expected 0x%08x, got 0x%08x. CLSID %s\n", entry->hr, hr, entry->clsid);
651 ok(hr == entry->hr, "Expected 0x%08x, got 0x%08x. CLSID %s\n", entry->hr, hr, entry->clsid);
653 DeleteFileA(testXmlA);
654 ISAXXMLReader_Release(reader);
660 START_TEST(saxreader)
662 ISAXXMLReader *reader;
665 hr = CoInitialize(NULL);
666 ok(hr == S_OK, "failed to init com\n");
668 hr = CoCreateInstance(&CLSID_SAXXMLReader, NULL, CLSCTX_INPROC_SERVER,
669 &IID_ISAXXMLReader, (void**)&reader);
673 skip("Failed to create SAXXMLReader instance\n");
677 ISAXXMLReader_Release(reader);