4 * Copyright 2010, 2012-2013 Nikolay Sivov
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
32 #include "wine/test.h"
34 DEFINE_GUID(IID_IXmlReaderInput, 0x0b3ccc9b, 0x9214, 0x428b, 0xa2, 0xae, 0xef, 0x3a, 0xa8, 0x71, 0xaf, 0xda);
36 static HRESULT (WINAPI *pCreateXmlReader)(REFIID riid, void **ppvObject, IMalloc *pMalloc);
37 static HRESULT (WINAPI *pCreateXmlReaderInputWithEncodingName)(IUnknown *stream,
42 IXmlReaderInput **ppInput);
43 static const char *debugstr_guid(REFIID riid)
47 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
48 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
49 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
50 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
55 static WCHAR *a2w(const char *str)
57 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
58 WCHAR *ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
59 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
63 static void free_str(WCHAR *str)
65 HeapFree(GetProcessHeap(), 0, str);
68 static const char xmldecl_full[] = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
70 static IStream *create_stream_on_data(const char *data, int size)
72 IStream *stream = NULL;
77 hglobal = GlobalAlloc(GHND, size);
78 ptr = GlobalLock(hglobal);
80 memcpy(ptr, data, size);
82 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
83 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
84 ok(stream != NULL, "Expected non-NULL stream\n");
86 GlobalUnlock(hglobal);
91 static void ok_pos_(IXmlReader *reader, int line, int pos, int line_broken,
92 int pos_broken, int todo, int _line_)
98 hr = IXmlReader_GetLineNumber(reader, &l);
99 ok_(__FILE__, _line_)(hr == S_OK, "Expected S_OK, got %08x\n", hr);
100 hr = IXmlReader_GetLinePosition(reader, &p);
101 ok_(__FILE__, _line_)(hr == S_OK, "Expected S_OK, got %08x\n", hr);
103 if (line_broken == -1 && pos_broken == -1)
106 broken_state = broken((line_broken == -1 ? line : line_broken) == l &&
107 (pos_broken == -1 ? pos : pos_broken) == p);
111 ok_(__FILE__, _line_)((l == line && pos == p) || broken_state,
112 "Expected (%d,%d), got (%d,%d)\n", line, pos, l, p);
115 ok_(__FILE__, _line_)((l == line && pos == p) || broken_state,
116 "Expected (%d,%d), got (%d,%d)\n", line, pos, l, p);
119 #define ok_pos(reader, l, p, l_brk, p_brk, todo) ok_pos_(reader, l, p, l_brk, p_brk, todo, __LINE__)
121 typedef struct input_iids_t {
126 static const IID *setinput_full[] = {
127 &IID_IXmlReaderInput,
129 &IID_ISequentialStream,
133 /* this applies to early xmllite versions */
134 static const IID *setinput_full_old[] = {
135 &IID_IXmlReaderInput,
136 &IID_ISequentialStream,
141 /* after ::SetInput(IXmlReaderInput*) */
142 static const IID *setinput_readerinput[] = {
144 &IID_ISequentialStream,
148 static const IID *empty_seq[] = {
152 static input_iids_t input_iids;
154 static void ok_iids_(const input_iids_t *iids, const IID **expected, const IID **exp_broken, int todo, int line)
158 while (expected[i++]) size++;
162 ok_(__FILE__, line)(iids->count == size, "Sequence size mismatch (%d), got (%d)\n", size, iids->count);
165 ok_(__FILE__, line)(iids->count == size, "Sequence size mismatch (%d), got (%d)\n", size, iids->count);
167 if (iids->count != size) return;
169 for (i = 0; i < size; i++) {
170 ok_(__FILE__, line)(IsEqualGUID(&iids->iids[i], expected[i]) ||
171 (exp_broken ? broken(IsEqualGUID(&iids->iids[i], exp_broken[i])) : FALSE),
172 "Wrong IID(%d), got (%s)\n", i, debugstr_guid(&iids->iids[i]));
175 #define ok_iids(got, exp, brk, todo) ok_iids_(got, exp, brk, todo, __LINE__)
177 static const char *state_to_str(XmlReadState state)
179 static const char* state_names[] = {
180 "XmlReadState_Initial",
181 "XmlReadState_Interactive",
182 "XmlReadState_Error",
183 "XmlReadState_EndOfFile",
184 "XmlReadState_Closed"
187 static const char unknown[] = "unknown";
191 case XmlReadState_Initial:
192 case XmlReadState_Interactive:
193 case XmlReadState_Error:
194 case XmlReadState_EndOfFile:
195 case XmlReadState_Closed:
196 return state_names[state];
202 static const char *type_to_str(XmlNodeType type)
204 static const char* type_names[] = {
206 "XmlNodeType_Element",
207 "XmlNodeType_Attribute",
211 "XmlNodeType_ProcessingInstruction",
212 "XmlNodeType_Comment",
214 "XmlNodeType_DocumentType",
216 "XmlNodeType_Whitespace",
218 "XmlNodeType_EndElement",
220 "XmlNodeType_XmlDeclaration"
223 static const char unknown[] = "unknown";
227 case XmlNodeType_None:
228 case XmlNodeType_Element:
229 case XmlNodeType_Attribute:
230 case XmlNodeType_Text:
231 case XmlNodeType_CDATA:
232 case XmlNodeType_ProcessingInstruction:
233 case XmlNodeType_Comment:
234 case XmlNodeType_DocumentType:
235 case XmlNodeType_Whitespace:
236 case XmlNodeType_EndElement:
237 case XmlNodeType_XmlDeclaration:
238 return type_names[type];
244 static void test_read_state_(IXmlReader *reader, XmlReadState expected,
245 XmlReadState exp_broken, int todo, int line)
251 state = -1; /* invalid value */
252 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_ReadState, (LONG_PTR*)&state);
253 ok_(__FILE__, line)(hr == S_OK, "Expected S_OK, got %08x\n", hr);
255 if (exp_broken == -1)
258 broken_state = broken(exp_broken == state);
263 ok_(__FILE__, line)(state == expected || broken_state, "Expected (%s), got (%s)\n",
264 state_to_str(expected), state_to_str(state));
267 ok_(__FILE__, line)(state == expected || broken_state, "Expected (%s), got (%s)\n",
268 state_to_str(expected), state_to_str(state));
271 #define test_read_state(reader, exp, brk, todo) test_read_state_(reader, exp, brk, todo, __LINE__)
273 typedef struct _testinput
275 IUnknown IUnknown_iface;
279 static inline testinput *impl_from_IUnknown(IUnknown *iface)
281 return CONTAINING_RECORD(iface, testinput, IUnknown_iface);
284 static HRESULT WINAPI testinput_QueryInterface(IUnknown *iface, REFIID riid, void** ppvObj)
286 if (IsEqualGUID( riid, &IID_IUnknown ))
289 IUnknown_AddRef(iface);
293 input_iids.iids[input_iids.count++] = *riid;
297 return E_NOINTERFACE;
300 static ULONG WINAPI testinput_AddRef(IUnknown *iface)
302 testinput *This = impl_from_IUnknown(iface);
303 return InterlockedIncrement(&This->ref);
306 static ULONG WINAPI testinput_Release(IUnknown *iface)
308 testinput *This = impl_from_IUnknown(iface);
311 ref = InterlockedDecrement(&This->ref);
314 HeapFree(GetProcessHeap(), 0, This);
320 static const struct IUnknownVtbl testinput_vtbl =
322 testinput_QueryInterface,
327 static HRESULT testinput_createinstance(void **ppObj)
331 input = HeapAlloc(GetProcessHeap(), 0, sizeof (*input));
332 if(!input) return E_OUTOFMEMORY;
334 input->IUnknown_iface.lpVtbl = &testinput_vtbl;
337 *ppObj = &input->IUnknown_iface;
342 static HRESULT WINAPI teststream_QueryInterface(ISequentialStream *iface, REFIID riid, void **obj)
344 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ISequentialStream))
351 return E_NOINTERFACE;
354 static ULONG WINAPI teststream_AddRef(ISequentialStream *iface)
359 static ULONG WINAPI teststream_Release(ISequentialStream *iface)
364 static int stream_readcall;
366 static HRESULT WINAPI teststream_Read(ISequentialStream *iface, void *pv, ULONG cb, ULONG *pread)
368 static const char xml[] = "<!-- comment -->";
370 if (stream_readcall++)
376 *pread = sizeof(xml) / 2;
377 memcpy(pv, xml, *pread);
381 static HRESULT WINAPI teststream_Write(ISequentialStream *iface, const void *pv, ULONG cb, ULONG *written)
383 ok(0, "unexpected call\n");
387 static const ISequentialStreamVtbl teststreamvtbl =
389 teststream_QueryInterface,
396 static BOOL init_pointers(void)
398 /* don't free module here, it's to be unloaded on exit */
399 HMODULE mod = LoadLibraryA("xmllite.dll");
403 win_skip("xmllite library not available\n");
407 #define MAKEFUNC(f) if (!(p##f = (void*)GetProcAddress(mod, #f))) return FALSE;
408 MAKEFUNC(CreateXmlReader);
409 MAKEFUNC(CreateXmlReaderInputWithEncodingName);
415 static void test_reader_create(void)
421 XmlNodeType nodetype;
426 pCreateXmlReader(&IID_IXmlReader, NULL, NULL);
427 pCreateXmlReader(NULL, (void**)&reader, NULL);
430 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
431 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
433 test_read_state(reader, XmlReadState_Closed, -1, FALSE);
435 nodetype = XmlNodeType_Element;
436 hr = IXmlReader_GetNodeType(reader, &nodetype);
437 ok(hr == S_FALSE, "got %08x\n", hr);
438 ok(nodetype == XmlNodeType_None, "got %d\n", nodetype);
441 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_DtdProcessing, (LONG_PTR*)&dtd);
442 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
443 ok(dtd == DtdProcessing_Prohibit, "got %d\n", dtd);
446 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, dtd);
447 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
449 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, -1);
450 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
452 /* Null input pointer, releases previous input */
453 hr = IXmlReader_SetInput(reader, NULL);
454 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
456 test_read_state(reader, XmlReadState_Initial, XmlReadState_Closed, FALSE);
458 /* test input interface selection sequence */
459 hr = testinput_createinstance((void**)&input);
460 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
464 input_iids.count = 0;
465 hr = IXmlReader_SetInput(reader, input);
466 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
467 ok_iids(&input_iids, setinput_full, setinput_full_old, FALSE);
468 IUnknown_Release(input);
470 IXmlReader_Release(reader);
473 static void test_readerinput(void)
475 IXmlReaderInput *reader_input;
476 IXmlReader *reader, *reader2;
477 IUnknown *obj, *input;
478 IStream *stream, *stream2;
479 XmlNodeType nodetype;
483 hr = pCreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, NULL);
484 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
485 hr = pCreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, &reader_input);
486 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
488 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
489 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
491 ref = IStream_AddRef(stream);
492 ok(ref == 2, "Expected 2, got %d\n", ref);
493 IStream_Release(stream);
494 hr = pCreateXmlReaderInputWithEncodingName((IUnknown*)stream, NULL, NULL, FALSE, NULL, &reader_input);
495 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
497 hr = IUnknown_QueryInterface(reader_input, &IID_IStream, (void**)&stream2);
498 ok(hr == E_NOINTERFACE, "Expected S_OK, got %08x\n", hr);
500 hr = IUnknown_QueryInterface(reader_input, &IID_ISequentialStream, (void**)&stream2);
501 ok(hr == E_NOINTERFACE, "Expected S_OK, got %08x\n", hr);
503 /* IXmlReaderInput grabs a stream reference */
504 ref = IStream_AddRef(stream);
505 ok(ref == 3, "Expected 3, got %d\n", ref);
506 IStream_Release(stream);
508 /* try ::SetInput() with valid IXmlReaderInput */
509 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
510 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
512 ref = IUnknown_AddRef(reader_input);
513 ok(ref == 2, "Expected 2, got %d\n", ref);
514 IUnknown_Release(reader_input);
516 hr = IXmlReader_SetInput(reader, reader_input);
517 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
519 test_read_state(reader, XmlReadState_Initial, -1, FALSE);
521 nodetype = XmlNodeType_Element;
522 hr = IXmlReader_GetNodeType(reader, &nodetype);
523 ok(hr == S_OK, "got %08x\n", hr);
524 ok(nodetype == XmlNodeType_None, "got %d\n", nodetype);
526 /* IXmlReader grabs a IXmlReaderInput reference */
527 ref = IUnknown_AddRef(reader_input);
528 ok(ref == 3, "Expected 3, got %d\n", ref);
529 IUnknown_Release(reader_input);
531 ref = IStream_AddRef(stream);
532 ok(ref == 4, "Expected 4, got %d\n", ref);
533 IStream_Release(stream);
535 /* reset input and check state */
536 hr = IXmlReader_SetInput(reader, NULL);
537 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
539 test_read_state(reader, XmlReadState_Initial, XmlReadState_Closed, FALSE);
541 IXmlReader_Release(reader);
543 ref = IStream_AddRef(stream);
544 ok(ref == 3, "Expected 3, got %d\n", ref);
545 IStream_Release(stream);
547 ref = IUnknown_AddRef(reader_input);
548 ok(ref == 2, "Expected 2, got %d\n", ref);
549 IUnknown_Release(reader_input);
551 /* IID_IXmlReaderInput */
552 /* it returns a kind of private undocumented vtable incompatible with IUnknown,
553 so it's not a COM interface actually.
554 Such query will be used only to check if input is really IXmlReaderInput */
555 obj = (IUnknown*)0xdeadbeef;
556 hr = IUnknown_QueryInterface(reader_input, &IID_IXmlReaderInput, (void**)&obj);
557 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
558 ref = IUnknown_AddRef(reader_input);
559 ok(ref == 3, "Expected 3, got %d\n", ref);
560 IUnknown_Release(reader_input);
562 IUnknown_Release(reader_input);
563 IUnknown_Release(reader_input);
564 IStream_Release(stream);
566 /* test input interface selection sequence */
568 hr = testinput_createinstance((void**)&input);
569 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
571 input_iids.count = 0;
572 ref = IUnknown_AddRef(input);
573 ok(ref == 2, "Expected 2, got %d\n", ref);
574 IUnknown_Release(input);
575 hr = pCreateXmlReaderInputWithEncodingName(input, NULL, NULL, FALSE, NULL, &reader_input);
576 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
577 ok_iids(&input_iids, empty_seq, NULL, FALSE);
578 /* IXmlReaderInput stores stream interface as IUnknown */
579 ref = IUnknown_AddRef(input);
580 ok(ref == 3, "Expected 3, got %d\n", ref);
581 IUnknown_Release(input);
583 hr = pCreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader, NULL);
584 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
586 input_iids.count = 0;
587 ref = IUnknown_AddRef(reader_input);
588 ok(ref == 2, "Expected 2, got %d\n", ref);
589 IUnknown_Release(reader_input);
590 ref = IUnknown_AddRef(input);
591 ok(ref == 3, "Expected 3, got %d\n", ref);
592 IUnknown_Release(input);
593 hr = IXmlReader_SetInput(reader, reader_input);
594 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
595 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
597 test_read_state(reader, XmlReadState_Closed, -1, FALSE);
599 ref = IUnknown_AddRef(input);
600 ok(ref == 3, "Expected 3, got %d\n", ref);
601 IUnknown_Release(input);
603 ref = IUnknown_AddRef(reader_input);
604 ok(ref == 3 || broken(ref == 2) /* versions 1.0.x and 1.1.x - XP, Vista */,
605 "Expected 3, got %d\n", ref);
606 IUnknown_Release(reader_input);
607 /* repeat another time, no check or caching here */
608 input_iids.count = 0;
609 hr = IXmlReader_SetInput(reader, reader_input);
610 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
611 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
614 hr = pCreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader2, NULL);
615 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
617 /* resolving from IXmlReaderInput to IStream/ISequentialStream is done at
618 ::SetInput() level, each time it's called */
619 input_iids.count = 0;
620 hr = IXmlReader_SetInput(reader2, reader_input);
621 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
622 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
624 IXmlReader_Release(reader2);
625 IXmlReader_Release(reader);
627 IUnknown_Release(reader_input);
628 IUnknown_Release(input);
631 static void test_reader_state(void)
634 XmlNodeType nodetype;
637 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
638 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
640 /* invalid arguments */
641 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_ReadState, NULL);
642 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
644 /* attempt to read on closed reader */
645 test_read_state(reader, XmlReadState_Closed, -1, 0);
648 /* newer versions crash here, probably cause no input was set */
649 hr = IXmlReader_Read(reader, &nodetype);
650 ok(hr == S_FALSE, "got %08x\n", hr);
652 IXmlReader_Release(reader);
655 static void test_read_xmldeclaration(void)
664 hr = pCreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader, NULL);
665 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
667 /* position methods with Null args */
668 hr = IXmlReader_GetLineNumber(reader, NULL);
669 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
671 hr = IXmlReader_GetLinePosition(reader, NULL);
672 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
674 stream = create_stream_on_data(xmldecl_full, sizeof(xmldecl_full));
676 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
677 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
679 hr = IXmlReader_GetAttributeCount(reader, &count);
680 ok(hr == S_OK, "got %08x\n", hr);
681 ok(count == 0, "got %d\n", count);
683 /* try to move without attributes */
684 hr = IXmlReader_MoveToElement(reader);
685 ok(hr == S_FALSE, "got %08x\n", hr);
687 hr = IXmlReader_MoveToNextAttribute(reader);
688 ok(hr == S_FALSE, "got %08x\n", hr);
690 hr = IXmlReader_MoveToFirstAttribute(reader);
691 ok(hr == S_FALSE, "got %08x\n", hr);
693 ok_pos(reader, 0, 0, -1, -1, FALSE);
696 hr = IXmlReader_Read(reader, &type);
697 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
698 ok(type == XmlNodeType_XmlDeclaration,
699 "Expected XmlNodeType_XmlDeclaration, got %s\n", type_to_str(type));
700 /* new version 1.2.x and 1.3.x properly update position for <?xml ?> */
701 ok_pos(reader, 1, 3, -1, 55, TRUE);
702 test_read_state(reader, XmlReadState_Interactive, -1, 0);
704 hr = IXmlReader_GetValue(reader, &val, NULL);
705 ok(hr == S_OK, "got %08x\n", hr);
706 ok(*val == 0, "got %s\n", wine_dbgstr_w(val));
708 /* check attributes */
709 hr = IXmlReader_MoveToNextAttribute(reader);
710 ok(hr == S_OK, "got %08x\n", hr);
712 type = XmlNodeType_None;
713 hr = IXmlReader_GetNodeType(reader, &type);
714 ok(hr == S_OK, "got %08x\n", hr);
715 ok(type == XmlNodeType_Attribute, "got %d\n", type);
717 ok_pos(reader, 1, 7, -1, 55, TRUE);
719 /* try to move from last attribute */
720 hr = IXmlReader_MoveToNextAttribute(reader);
721 ok(hr == S_OK, "got %08x\n", hr);
722 hr = IXmlReader_MoveToNextAttribute(reader);
723 ok(hr == S_OK, "got %08x\n", hr);
724 hr = IXmlReader_MoveToNextAttribute(reader);
725 ok(hr == S_FALSE, "got %08x\n", hr);
727 type = XmlNodeType_None;
728 hr = IXmlReader_GetNodeType(reader, &type);
729 ok(hr == S_OK, "got %08x\n", hr);
730 ok(type == XmlNodeType_Attribute, "got %d\n", type);
732 hr = IXmlReader_MoveToFirstAttribute(reader);
733 ok(hr == S_OK, "got %08x\n", hr);
734 ok_pos(reader, 1, 7, -1, 55, TRUE);
736 hr = IXmlReader_GetAttributeCount(reader, NULL);
737 ok(hr == E_INVALIDARG, "got %08x\n", hr);
739 hr = IXmlReader_GetAttributeCount(reader, &count);
740 ok(hr == S_OK, "got %08x\n", hr);
741 ok(count == 3, "Expected 3, got %d\n", count);
743 hr = IXmlReader_GetDepth(reader, &count);
744 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
746 ok(count == 1, "Expected 1, got %d\n", count);
748 hr = IXmlReader_MoveToElement(reader);
749 ok(hr == S_OK, "got %08x\n", hr);
751 type = XmlNodeType_None;
752 hr = IXmlReader_GetNodeType(reader, &type);
753 ok(hr == S_OK, "got %08x\n", hr);
754 ok(type == XmlNodeType_XmlDeclaration, "got %d\n", type);
756 type = XmlNodeType_XmlDeclaration;
757 hr = IXmlReader_Read(reader, &type);
758 /* newer versions return syntax error here cause document is incomplete,
759 it makes more sense than invalid char error */
761 ok(hr == WC_E_SYNTAX || broken(hr == WC_E_XMLCHARACTER), "got 0x%08x\n", hr);
762 ok(type == XmlNodeType_None, "got %d\n", type);
764 IStream_Release(stream);
765 IXmlReader_Release(reader);
773 HRESULT hr_broken; /* this is set to older version results */
777 static struct test_entry comment_tests[] = {
778 { "<!-- comment -->", "", " comment ", S_OK },
779 { "<!-- - comment-->", "", " - comment", S_OK },
780 { "<!-- -- comment-->", NULL, NULL, WC_E_COMMENT, WC_E_GREATERTHAN },
781 { "<!-- -- comment--->", NULL, NULL, WC_E_COMMENT, WC_E_GREATERTHAN },
785 static void test_read_comment(void)
787 struct test_entry *test = comment_tests;
791 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
792 ok(hr == S_OK, "S_OK, got %08x\n", hr);
799 stream = create_stream_on_data(test->xml, strlen(test->xml)+1);
800 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
801 ok(hr == S_OK, "got %08x\n", hr);
803 type = XmlNodeType_None;
804 hr = IXmlReader_Read(reader, &type);
806 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
808 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
815 ok(type == XmlNodeType_Comment, "got %d for %s\n", type, test->xml);
819 hr = IXmlReader_GetLocalName(reader, &str, &len);
820 ok(hr == S_OK, "got 0x%08x\n", hr);
821 ok(len == strlen(test->name), "got %u\n", len);
822 str_exp = a2w(test->name);
823 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
828 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
829 ok(hr == S_OK, "got 0x%08x\n", hr);
830 ok(len == strlen(test->name), "got %u\n", len);
831 str_exp = a2w(test->name);
832 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
838 hr = IXmlReader_GetValue(reader, &str, &len);
839 ok(hr == S_OK, "got 0x%08x\n", hr);
840 ok(len == strlen(test->value), "got %u\n", len);
841 str_exp = a2w(test->value);
842 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
846 IStream_Release(stream);
850 IXmlReader_Release(reader);
853 static struct test_entry pi_tests[] = {
854 { "<?pi?>", "pi", "", S_OK },
855 { "<?pi ?>", "pi", "", S_OK },
856 { "<?pi ?>", "pi", "", S_OK },
857 { "<?pi pi data?>", "pi", "pi data", S_OK },
858 { "<?pi pi data ?>", "pi", "pi data ", S_OK },
859 { "<?pi:pi?>", NULL, NULL, NC_E_NAMECOLON, WC_E_NAMECHARACTER },
860 { "<?:pi ?>", NULL, NULL, WC_E_PI, WC_E_NAMECHARACTER },
861 { "<?-pi ?>", NULL, NULL, WC_E_PI, WC_E_NAMECHARACTER },
862 { "<?xml-stylesheet ?>", "xml-stylesheet", "", S_OK },
866 static void test_read_pi(void)
868 struct test_entry *test = pi_tests;
872 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
873 ok(hr == S_OK, "S_OK, got %08x\n", hr);
880 stream = create_stream_on_data(test->xml, strlen(test->xml)+1);
881 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
882 ok(hr == S_OK, "got %08x\n", hr);
884 type = XmlNodeType_None;
885 hr = IXmlReader_Read(reader, &type);
887 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
889 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
896 ok(type == XmlNodeType_ProcessingInstruction, "got %d for %s\n", type, test->xml);
900 hr = IXmlReader_GetLocalName(reader, &str, &len);
901 ok(hr == S_OK, "got 0x%08x\n", hr);
902 ok(len == strlen(test->name), "got %u\n", len);
903 str_exp = a2w(test->name);
904 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
909 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
910 ok(hr == S_OK, "got 0x%08x\n", hr);
911 ok(len == strlen(test->name), "got %u\n", len);
912 str_exp = a2w(test->name);
913 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
917 len = !strlen(test->value);
919 hr = IXmlReader_GetValue(reader, &str, &len);
920 ok(hr == S_OK, "got 0x%08x\n", hr);
921 ok(len == strlen(test->value), "got %u\n", len);
922 str_exp = a2w(test->value);
923 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
927 IStream_Release(stream);
931 IXmlReader_Release(reader);
936 XmlNodeType types[20];
939 static const char misc_test_xml[] =
953 static struct nodes_test misc_test = {
958 XmlNodeType_ProcessingInstruction,
960 XmlNodeType_Whitespace,
965 XmlNodeType_ProcessingInstruction,
966 XmlNodeType_EndElement,
971 static void test_read_full(void)
973 struct nodes_test *test = &misc_test;
980 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
981 ok(hr == S_OK, "S_OK, got %08x\n", hr);
983 stream = create_stream_on_data(test->xml, strlen(test->xml)+1);
984 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
985 ok(hr == S_OK, "got %08x\n", hr);
988 type = XmlNodeType_None;
989 hr = IXmlReader_Read(reader, &type);
992 ok(test->types[i] != XmlNodeType_None, "%d: unexpected end of test data\n", i);
993 if (test->types[i] == XmlNodeType_None) break;
994 ok(type == test->types[i], "%d: got wrong type %d, expected %d\n", i, type, test->types[i]);
995 hr = IXmlReader_Read(reader, &type);
998 ok(test->types[i] == XmlNodeType_None, "incomplete sequence, got %d\n", test->types[i]);
1000 IStream_Release(stream);
1001 IXmlReader_Release(reader);
1004 static const char test_dtd[] =
1005 "<!DOCTYPE testdtd SYSTEM \"externalid uri\" >"
1008 static void test_read_dtd(void)
1010 static const WCHAR sysvalW[] = {'e','x','t','e','r','n','a','l','i','d',' ','u','r','i',0};
1011 static const WCHAR dtdnameW[] = {'t','e','s','t','d','t','d',0};
1012 static const WCHAR sysW[] = {'S','Y','S','T','E','M',0};
1020 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1021 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1023 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, DtdProcessing_Parse);
1024 ok(hr == S_OK, "got 0x%8x\n", hr);
1026 stream = create_stream_on_data(test_dtd, sizeof(test_dtd));
1027 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1028 ok(hr == S_OK, "got %08x\n", hr);
1030 type = XmlNodeType_None;
1031 hr = IXmlReader_Read(reader, &type);
1032 ok(hr == S_OK, "got 0x%8x\n", hr);
1033 ok(type == XmlNodeType_DocumentType, "got type %d\n", type);
1036 hr = IXmlReader_GetAttributeCount(reader, &count);
1037 ok(hr == S_OK, "got %08x\n", hr);
1038 ok(count == 1, "got %d\n", count);
1040 hr = IXmlReader_MoveToFirstAttribute(reader);
1041 ok(hr == S_OK, "got %08x\n", hr);
1043 type = XmlNodeType_None;
1044 hr = IXmlReader_GetNodeType(reader, &type);
1045 ok(hr == S_OK, "got %08x\n", hr);
1046 ok(type == XmlNodeType_Attribute, "got %d\n", type);
1050 hr = IXmlReader_GetLocalName(reader, &str, &len);
1051 ok(hr == S_OK, "got 0x%08x\n", hr);
1053 ok(len == lstrlenW(sysW), "got %u\n", len);
1054 ok(!lstrcmpW(str, sysW), "got %s\n", wine_dbgstr_w(str));
1058 hr = IXmlReader_GetValue(reader, &str, &len);
1059 ok(hr == S_OK, "got 0x%08x\n", hr);
1061 ok(len == lstrlenW(sysvalW), "got %u\n", len);
1062 ok(!lstrcmpW(str, sysvalW), "got %s\n", wine_dbgstr_w(str));
1064 hr = IXmlReader_MoveToElement(reader);
1065 ok(hr == S_OK, "got 0x%08x\n", hr);
1069 hr = IXmlReader_GetLocalName(reader, &str, &len);
1070 ok(hr == S_OK, "got 0x%08x\n", hr);
1071 ok(len == lstrlenW(dtdnameW), "got %u\n", len);
1072 ok(!lstrcmpW(str, dtdnameW), "got %s\n", wine_dbgstr_w(str));
1076 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1077 ok(hr == S_OK, "got 0x%08x\n", hr);
1078 ok(len == lstrlenW(dtdnameW), "got %u\n", len);
1079 ok(!lstrcmpW(str, dtdnameW), "got %s\n", wine_dbgstr_w(str));
1081 type = XmlNodeType_None;
1082 hr = IXmlReader_Read(reader, &type);
1083 ok(hr == S_OK, "got 0x%8x\n", hr);
1084 ok(type == XmlNodeType_Comment, "got type %d\n", type);
1086 IStream_Release(stream);
1087 IXmlReader_Release(reader);
1090 static struct test_entry element_tests[] = {
1091 { "<a/>", "a", "", S_OK },
1092 { "<a />", "a", "", S_OK },
1093 { "<a:b/>", "a:b", "", NC_E_UNDECLAREDPREFIX },
1094 { "<:a/>", NULL, NULL, NC_E_QNAMECHARACTER },
1095 { "< a/>", NULL, NULL, NC_E_QNAMECHARACTER },
1096 { "<a>", "a", "", S_OK },
1097 { "<a >", "a", "", S_OK },
1098 { "<a \r \t\n>", "a", "", S_OK },
1099 { "</a>", NULL, NULL, NC_E_QNAMECHARACTER },
1103 static void test_read_element(void)
1105 struct test_entry *test = element_tests;
1106 static const char stag[] = "<a><b></b></a>";
1107 static const char mismatch[] = "<a></b>";
1114 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1115 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1119 stream = create_stream_on_data(test->xml, strlen(test->xml)+1);
1120 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1121 ok(hr == S_OK, "got %08x\n", hr);
1123 type = XmlNodeType_None;
1124 hr = IXmlReader_Read(reader, &type);
1125 if (test->hr_broken)
1126 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1128 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1135 ok(type == XmlNodeType_Element, "got %d for %s\n", type, test->xml);
1139 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1140 ok(hr == S_OK, "got 0x%08x\n", hr);
1141 ok(len == strlen(test->name), "got %u\n", len);
1142 str_exp = a2w(test->name);
1143 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1149 hr = IXmlReader_GetValue(reader, &str, &len);
1150 ok(hr == S_OK, "got 0x%08x\n", hr);
1151 ok(len == 0, "got %u\n", len);
1152 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1155 IStream_Release(stream);
1159 /* test reader depth increment */
1160 stream = create_stream_on_data(stag, sizeof(stag));
1161 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1162 ok(hr == S_OK, "got %08x\n", hr);
1165 hr = IXmlReader_GetDepth(reader, &depth);
1166 ok(hr == S_OK, "got %08x\n", hr);
1167 ok(depth == 0, "got %d\n", depth);
1169 hr = IXmlReader_Read(reader, &type);
1170 ok(hr == S_OK, "got %08x\n", hr);
1173 hr = IXmlReader_GetDepth(reader, &depth);
1174 ok(hr == S_OK, "got %08x\n", hr);
1175 ok(depth == 0, "got %d\n", depth);
1177 hr = IXmlReader_Read(reader, &type);
1178 ok(hr == S_OK, "got %08x\n", hr);
1181 hr = IXmlReader_GetDepth(reader, &depth);
1182 ok(hr == S_OK, "got %08x\n", hr);
1183 ok(depth == 1, "got %d\n", depth);
1185 /* read end tag for inner element */
1186 type = XmlNodeType_None;
1187 hr = IXmlReader_Read(reader, &type);
1188 ok(hr == S_OK, "got %08x\n", hr);
1189 ok(type == XmlNodeType_EndElement, "got %d\n", type);
1192 hr = IXmlReader_GetDepth(reader, &depth);
1193 ok(hr == S_OK, "got %08x\n", hr);
1195 ok(depth == 2, "got %d\n", depth);
1197 /* read end tag for container element */
1198 type = XmlNodeType_None;
1199 hr = IXmlReader_Read(reader, &type);
1200 ok(hr == S_OK, "got %08x\n", hr);
1201 ok(type == XmlNodeType_EndElement, "got %d\n", type);
1204 hr = IXmlReader_GetDepth(reader, &depth);
1205 ok(hr == S_OK, "got %08x\n", hr);
1206 ok(depth == 1, "got %d\n", depth);
1208 IStream_Release(stream);
1210 /* start/end tag mismatch */
1211 stream = create_stream_on_data(mismatch, sizeof(mismatch));
1212 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1213 ok(hr == S_OK, "got %08x\n", hr);
1215 type = XmlNodeType_None;
1216 hr = IXmlReader_Read(reader, &type);
1217 ok(hr == S_OK, "got %08x\n", hr);
1218 ok(type == XmlNodeType_Element, "got %d\n", type);
1220 type = XmlNodeType_Element;
1221 hr = IXmlReader_Read(reader, &type);
1222 ok(hr == WC_E_ELEMENTMATCH, "got %08x\n", hr);
1224 ok(type == XmlNodeType_None, "got %d\n", type);
1226 IStream_Release(stream);
1228 IXmlReader_Release(reader);
1231 static ISequentialStream teststream = { &teststreamvtbl };
1233 static void test_read_pending(void)
1241 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1242 ok(hr == S_OK, "S_OK, got 0x%08x\n", hr);
1244 hr = IXmlReader_SetInput(reader, (IUnknown*)&teststream);
1245 ok(hr == S_OK, "got 0x%08x\n", hr);
1247 /* first read call returns incomplete node, second attempt fails with E_PENDING */
1248 stream_readcall = 0;
1249 type = XmlNodeType_Element;
1250 hr = IXmlReader_Read(reader, &type);
1251 ok(hr == S_OK || broken(hr == E_PENDING), "got 0x%08x\n", hr);
1252 /* newer versions are happy when it's enough data to detect node type,
1253 older versions keep reading until it fails to read more */
1254 ok(stream_readcall == 1 || broken(stream_readcall > 1), "got %d\n", stream_readcall);
1255 ok(type == XmlNodeType_Comment || broken(type == XmlNodeType_None), "got %d\n", type);
1257 /* newer versions' GetValue() makes an attempt to read more */
1258 c = stream_readcall;
1259 value = (void*)0xdeadbeef;
1260 hr = IXmlReader_GetValue(reader, &value, NULL);
1261 ok(hr == E_PENDING, "got 0x%08x\n", hr);
1262 ok(value == NULL || broken(value == (void*)0xdeadbeef) /* Win8 sets it to NULL */, "got %p\n", value);
1263 ok(c < stream_readcall || broken(c == stream_readcall), "got %d, expected %d\n", stream_readcall, c+1);
1265 IXmlReader_Release(reader);
1268 static void test_readvaluechunk(void)
1270 static const char testA[] = "<!-- comment1 -->";
1279 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1280 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1282 stream = create_stream_on_data(testA, sizeof(testA));
1283 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1284 ok(hr == S_OK, "got %08x\n", hr);
1286 hr = IXmlReader_Read(reader, &type);
1287 ok(hr == S_OK, "got %08x\n", hr);
1291 hr = IXmlReader_ReadValueChunk(reader, &b, 1, &c);
1292 ok(hr == S_OK, "got %08x\n", hr);
1293 ok(c == 1, "got %u\n", c);
1294 ok(b == ' ', "got %x\n", b);
1296 /* portion read as chunk is skipped from resulting node value */
1298 hr = IXmlReader_GetValue(reader, &value, NULL);
1299 ok(hr == S_OK, "got %08x\n", hr);
1300 ok(value[0] == 'c', "got %s\n", wine_dbgstr_w(value));
1302 /* once value is returned/allocated it's not possible to read by chunk */
1305 hr = IXmlReader_ReadValueChunk(reader, &b, 1, &c);
1306 ok(hr == S_FALSE, "got %08x\n", hr);
1307 ok(c == 0, "got %u\n", c);
1308 ok(b == 0, "got %x\n", b);
1311 hr = IXmlReader_GetValue(reader, &value, NULL);
1312 ok(hr == S_OK, "got %08x\n", hr);
1313 ok(value[0] == 'c', "got %s\n", wine_dbgstr_w(value));
1315 IXmlReader_Release(reader);
1318 static struct test_entry cdata_tests[] = {
1319 { "<a><![CDATA[ ]]data ]]></a>", "", " ]]data ", S_OK },
1320 { "<a><![CDATA[<![CDATA[ data ]]]]></a>", "", "<![CDATA[ data ]]", S_OK },
1321 { "<a><![CDATA[\n \r\n \n\n ]]></a>", "", "\n \n \n\n ", S_OK, S_OK, 1 },
1322 { "<a><![CDATA[\r \r\r\n \n\n ]]></a>", "", "\n \n\n \n\n ", S_OK, S_OK, 1 },
1323 { "<a><![CDATA[\r\r \n\r \r \n\n ]]></a>", "", "\n\n \n\n \n \n\n ", S_OK },
1327 static void test_read_cdata(void)
1329 struct test_entry *test = cdata_tests;
1333 hr = pCreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1334 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1341 stream = create_stream_on_data(test->xml, strlen(test->xml)+1);
1342 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
1343 ok(hr == S_OK, "got %08x\n", hr);
1345 type = XmlNodeType_None;
1346 hr = IXmlReader_Read(reader, &type);
1348 /* read one more to get to CDATA */
1349 if (type == XmlNodeType_Element)
1351 type = XmlNodeType_None;
1352 hr = IXmlReader_Read(reader, &type);
1355 if (test->hr_broken)
1356 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1358 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1365 ok(type == XmlNodeType_CDATA, "got %d for %s\n", type, test->xml);
1369 hr = IXmlReader_GetLocalName(reader, &str, &len);
1370 ok(hr == S_OK, "got 0x%08x\n", hr);
1371 ok(len == strlen(test->name), "got %u\n", len);
1372 str_exp = a2w(test->name);
1373 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1378 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1379 ok(hr == S_OK, "got 0x%08x\n", hr);
1380 ok(len == strlen(test->name), "got %u\n", len);
1381 str_exp = a2w(test->name);
1382 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1388 hr = IXmlReader_GetValue(reader, &str, &len);
1389 ok(hr == S_OK, "got 0x%08x\n", hr);
1391 str_exp = a2w(test->value);
1395 ok(len == strlen(test->value), "got %u\n", len);
1396 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1401 ok(len == strlen(test->value), "got %u\n", len);
1402 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1407 IStream_Release(stream);
1411 IXmlReader_Release(reader);
1418 r = CoInitialize( NULL );
1419 ok( r == S_OK, "failed to init com\n");
1421 if (!init_pointers())
1427 test_reader_create();
1429 test_reader_state();
1431 test_read_comment();
1434 test_read_element();
1436 test_read_pending();
1437 test_readvaluechunk();
1438 test_read_xmldeclaration();