2 * XMLLite IXmlReader tests
4 * Copyright 2010 (C) 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 const char xmldecl_full
[] = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
57 static IStream
*create_stream_on_data(const char *data
, int size
)
59 IStream
*stream
= NULL
;
64 hglobal
= GlobalAlloc(GHND
, size
);
65 ptr
= GlobalLock(hglobal
);
67 memcpy(ptr
, data
, size
);
69 hr
= CreateStreamOnHGlobal(hglobal
, TRUE
, &stream
);
70 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
71 ok(stream
!= NULL
, "Expected non-NULL stream\n");
73 GlobalUnlock(hglobal
);
78 static void ok_pos_(IXmlReader
*reader
, int line
, int pos
, int line_broken
,
79 int pos_broken
, int todo
, int _line_
)
85 hr
= IXmlReader_GetLineNumber(reader
, &l
);
86 ok_(__FILE__
, _line_
)(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
87 hr
= IXmlReader_GetLinePosition(reader
, &p
);
88 ok_(__FILE__
, _line_
)(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
90 if (line_broken
== -1 && pos_broken
== -1)
93 broken_state
= broken((line_broken
== -1 ? line
: line_broken
) == l
&&
94 (pos_broken
== -1 ? pos
: pos_broken
) == p
);
98 ok_(__FILE__
, _line_
)((l
== line
&& pos
== p
) || broken_state
,
99 "Expected (%d,%d), got (%d,%d)\n", line
, pos
, l
, p
);
102 ok_(__FILE__
, _line_
)((l
== line
&& pos
== p
) || broken_state
,
103 "Expected (%d,%d), got (%d,%d)\n", line
, pos
, l
, p
);
106 #define ok_pos(reader, l, p, l_brk, p_brk, todo) ok_pos_(reader, l, p, l_brk, p_brk, todo, __LINE__)
108 typedef struct input_iids_t
{
113 static const IID
*setinput_full
[] = {
114 &IID_IXmlReaderInput
,
116 &IID_ISequentialStream
,
120 /* this applies to early xmllite versions */
121 static const IID
*setinput_full_old
[] = {
122 &IID_IXmlReaderInput
,
123 &IID_ISequentialStream
,
128 /* after ::SetInput(IXmlReaderInput*) */
129 static const IID
*setinput_readerinput
[] = {
131 &IID_ISequentialStream
,
135 static const IID
*empty_seq
[] = {
139 static input_iids_t input_iids
;
141 static void ok_iids_(const input_iids_t
*iids
, const IID
**expected
, const IID
**exp_broken
, int todo
, int line
)
145 while (expected
[i
++]) size
++;
149 ok_(__FILE__
, line
)(iids
->count
== size
, "Sequence size mismatch (%d), got (%d)\n", size
, iids
->count
);
152 ok_(__FILE__
, line
)(iids
->count
== size
, "Sequence size mismatch (%d), got (%d)\n", size
, iids
->count
);
154 if (iids
->count
!= size
) return;
156 for (i
= 0; i
< size
; i
++) {
157 ok_(__FILE__
, line
)(IsEqualGUID(&iids
->iids
[i
], expected
[i
]) ||
158 (exp_broken
? broken(IsEqualGUID(&iids
->iids
[i
], exp_broken
[i
])) : FALSE
),
159 "Wrong IID(%d), got (%s)\n", i
, debugstr_guid(&iids
->iids
[i
]));
162 #define ok_iids(got, exp, brk, todo) ok_iids_(got, exp, brk, todo, __LINE__)
164 static const char *state_to_str(XmlReadState state
)
166 static const char* state_names
[] = {
167 "XmlReadState_Initial",
168 "XmlReadState_Interactive",
169 "XmlReadState_Error",
170 "XmlReadState_EndOfFile",
171 "XmlReadState_Closed"
174 static const char unknown
[] = "unknown";
178 case XmlReadState_Initial
:
179 case XmlReadState_Interactive
:
180 case XmlReadState_Error
:
181 case XmlReadState_EndOfFile
:
182 case XmlReadState_Closed
:
183 return state_names
[state
];
189 static const char *type_to_str(XmlNodeType type
)
191 static const char* type_names
[] = {
193 "XmlNodeType_Element",
194 "XmlNodeType_Attribute",
198 "XmlNodeType_ProcessingInstruction",
199 "XmlNodeType_Comment",
201 "XmlNodeType_DocumentType",
203 "XmlNodeType_Whitespace",
205 "XmlNodeType_EndElement",
207 "XmlNodeType_XmlDeclaration"
210 static const char unknown
[] = "unknown";
214 case XmlNodeType_None
:
215 case XmlNodeType_Element
:
216 case XmlNodeType_Attribute
:
217 case XmlNodeType_Text
:
218 case XmlNodeType_CDATA
:
219 case XmlNodeType_ProcessingInstruction
:
220 case XmlNodeType_Comment
:
221 case XmlNodeType_DocumentType
:
222 case XmlNodeType_Whitespace
:
223 case XmlNodeType_EndElement
:
224 case XmlNodeType_XmlDeclaration
:
225 return type_names
[type
];
231 static void test_read_state_(IXmlReader
*reader
, XmlReadState expected
,
232 XmlReadState exp_broken
, int todo
, int line
)
238 state
= -1; /* invalid value */
239 hr
= IXmlReader_GetProperty(reader
, XmlReaderProperty_ReadState
, (LONG_PTR
*)&state
);
240 ok_(__FILE__
, line
)(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
242 if (exp_broken
== -1)
245 broken_state
= broken(exp_broken
== state
);
250 ok_(__FILE__
, line
)(state
== expected
|| broken_state
, "Expected (%s), got (%s)\n",
251 state_to_str(expected
), state_to_str(state
));
254 ok_(__FILE__
, line
)(state
== expected
|| broken_state
, "Expected (%s), got (%s)\n",
255 state_to_str(expected
), state_to_str(state
));
258 #define test_read_state(reader, exp, brk, todo) test_read_state_(reader, exp, brk, todo, __LINE__)
260 typedef struct _testinput
262 IUnknown IUnknown_iface
;
266 static inline testinput
*impl_from_IUnknown(IUnknown
*iface
)
268 return CONTAINING_RECORD(iface
, testinput
, IUnknown_iface
);
271 static HRESULT WINAPI
testinput_QueryInterface(IUnknown
*iface
, REFIID riid
, void** ppvObj
)
273 if (IsEqualGUID( riid
, &IID_IUnknown
))
276 IUnknown_AddRef(iface
);
280 input_iids
.iids
[input_iids
.count
++] = *riid
;
284 return E_NOINTERFACE
;
287 static ULONG WINAPI
testinput_AddRef(IUnknown
*iface
)
289 testinput
*This
= impl_from_IUnknown(iface
);
290 return InterlockedIncrement(&This
->ref
);
293 static ULONG WINAPI
testinput_Release(IUnknown
*iface
)
295 testinput
*This
= impl_from_IUnknown(iface
);
298 ref
= InterlockedDecrement(&This
->ref
);
301 HeapFree(GetProcessHeap(), 0, This
);
307 static const struct IUnknownVtbl testinput_vtbl
=
309 testinput_QueryInterface
,
314 static HRESULT
testinput_createinstance(void **ppObj
)
318 input
= HeapAlloc(GetProcessHeap(), 0, sizeof (*input
));
319 if(!input
) return E_OUTOFMEMORY
;
321 input
->IUnknown_iface
.lpVtbl
= &testinput_vtbl
;
324 *ppObj
= &input
->IUnknown_iface
;
329 static BOOL
init_pointers(void)
331 /* don't free module here, it's to be unloaded on exit */
332 HMODULE mod
= LoadLibraryA("xmllite.dll");
336 win_skip("xmllite library not available\n");
340 #define MAKEFUNC(f) if (!(p##f = (void*)GetProcAddress(mod, #f))) return FALSE;
341 MAKEFUNC(CreateXmlReader
);
342 MAKEFUNC(CreateXmlReaderInputWithEncodingName
);
348 static void test_reader_create(void)
357 pCreateXmlReader(&IID_IXmlReader
, NULL
, NULL
);
358 pCreateXmlReader(NULL
, (LPVOID
*)&reader
, NULL
);
361 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader
, NULL
);
362 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
364 test_read_state(reader
, XmlReadState_Closed
, -1, FALSE
);
366 /* Null input pointer, releases previous input */
367 hr
= IXmlReader_SetInput(reader
, NULL
);
368 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
370 test_read_state(reader
, XmlReadState_Initial
, XmlReadState_Closed
, FALSE
);
372 /* test input interface selection sequence */
373 hr
= testinput_createinstance((void**)&input
);
374 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
378 input_iids
.count
= 0;
379 hr
= IXmlReader_SetInput(reader
, input
);
380 ok(hr
== E_NOINTERFACE
, "Expected E_NOINTERFACE, got %08x\n", hr
);
381 ok_iids(&input_iids
, setinput_full
, setinput_full_old
, FALSE
);
382 IUnknown_Release(input
);
384 IXmlReader_Release(reader
);
387 static void test_readerinput(void)
389 IXmlReaderInput
*reader_input
;
390 IXmlReader
*reader
, *reader2
;
391 IUnknown
*obj
, *input
;
396 hr
= pCreateXmlReaderInputWithEncodingName(NULL
, NULL
, NULL
, FALSE
, NULL
, NULL
);
397 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
398 hr
= pCreateXmlReaderInputWithEncodingName(NULL
, NULL
, NULL
, FALSE
, NULL
, &reader_input
);
399 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
401 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
402 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
404 ref
= IStream_AddRef(stream
);
405 ok(ref
== 2, "Expected 2, got %d\n", ref
);
406 IStream_Release(stream
);
407 hr
= pCreateXmlReaderInputWithEncodingName((IUnknown
*)stream
, NULL
, NULL
, FALSE
, NULL
, &reader_input
);
408 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
410 /* IXmlReaderInput grabs a stream reference */
411 ref
= IStream_AddRef(stream
);
412 ok(ref
== 3, "Expected 3, got %d\n", ref
);
413 IStream_Release(stream
);
415 /* try ::SetInput() with valid IXmlReaderInput */
416 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader
, NULL
);
417 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
419 ref
= IUnknown_AddRef(reader_input
);
420 ok(ref
== 2, "Expected 2, got %d\n", ref
);
421 IUnknown_Release(reader_input
);
423 hr
= IXmlReader_SetInput(reader
, reader_input
);
424 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
426 test_read_state(reader
, XmlReadState_Initial
, -1, FALSE
);
428 /* IXmlReader grabs a IXmlReaderInput reference */
429 ref
= IUnknown_AddRef(reader_input
);
430 ok(ref
== 3, "Expected 3, got %d\n", ref
);
431 IUnknown_Release(reader_input
);
433 ref
= IStream_AddRef(stream
);
434 ok(ref
== 4, "Expected 4, got %d\n", ref
);
435 IStream_Release(stream
);
437 /* reset input and check state */
438 hr
= IXmlReader_SetInput(reader
, NULL
);
439 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
441 test_read_state(reader
, XmlReadState_Initial
, XmlReadState_Closed
, FALSE
);
443 IXmlReader_Release(reader
);
445 ref
= IStream_AddRef(stream
);
446 ok(ref
== 3, "Expected 3, got %d\n", ref
);
447 IStream_Release(stream
);
449 ref
= IUnknown_AddRef(reader_input
);
450 ok(ref
== 2, "Expected 2, got %d\n", ref
);
451 IUnknown_Release(reader_input
);
453 /* IID_IXmlReaderInput */
454 /* it returns a kind of private undocumented vtable incompatible with IUnknown,
455 so it's not a COM interface actually.
456 Such query will be used only to check if input is really IXmlReaderInput */
457 obj
= (IUnknown
*)0xdeadbeef;
458 hr
= IUnknown_QueryInterface(reader_input
, &IID_IXmlReaderInput
, (void**)&obj
);
459 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
460 ref
= IUnknown_AddRef(reader_input
);
461 ok(ref
== 3, "Expected 3, got %d\n", ref
);
462 IUnknown_Release(reader_input
);
464 IUnknown_Release(reader_input
);
465 IUnknown_Release(reader_input
);
466 IStream_Release(stream
);
468 /* test input interface selection sequence */
469 hr
= testinput_createinstance((void**)&input
);
470 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
472 input_iids
.count
= 0;
473 ref
= IUnknown_AddRef(input
);
474 ok(ref
== 2, "Expected 2, got %d\n", ref
);
475 IUnknown_Release(input
);
476 hr
= pCreateXmlReaderInputWithEncodingName(input
, NULL
, NULL
, FALSE
, NULL
, &reader_input
);
477 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
478 ok_iids(&input_iids
, empty_seq
, NULL
, FALSE
);
479 /* IXmlReaderInput stores stream interface as IUnknown */
480 ref
= IUnknown_AddRef(input
);
481 ok(ref
== 3, "Expected 3, got %d\n", ref
);
482 IUnknown_Release(input
);
484 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader
, NULL
);
485 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
487 input_iids
.count
= 0;
488 ref
= IUnknown_AddRef(reader_input
);
489 ok(ref
== 2, "Expected 2, got %d\n", ref
);
490 IUnknown_Release(reader_input
);
491 ref
= IUnknown_AddRef(input
);
492 ok(ref
== 3, "Expected 3, got %d\n", ref
);
493 IUnknown_Release(input
);
494 hr
= IXmlReader_SetInput(reader
, reader_input
);
495 ok(hr
== E_NOINTERFACE
, "Expected E_NOINTERFACE, got %08x\n", hr
);
496 ok_iids(&input_iids
, setinput_readerinput
, NULL
, FALSE
);
498 test_read_state(reader
, XmlReadState_Closed
, -1, FALSE
);
500 ref
= IUnknown_AddRef(input
);
501 ok(ref
== 3, "Expected 3, got %d\n", ref
);
502 IUnknown_Release(input
);
504 ref
= IUnknown_AddRef(reader_input
);
505 ok(ref
== 3 || broken(ref
== 2) /* versions 1.0.x and 1.1.x - XP, Vista */,
506 "Expected 3, got %d\n", ref
);
507 IUnknown_Release(reader_input
);
508 /* repeat another time, no check or caching here */
509 input_iids
.count
= 0;
510 hr
= IXmlReader_SetInput(reader
, reader_input
);
511 ok(hr
== E_NOINTERFACE
, "Expected E_NOINTERFACE, got %08x\n", hr
);
512 ok_iids(&input_iids
, setinput_readerinput
, NULL
, FALSE
);
515 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader2
, NULL
);
516 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
518 /* resolving from IXmlReaderInput to IStream/ISequentialStream is done at
519 ::SetInput() level, each time it's called */
520 input_iids
.count
= 0;
521 hr
= IXmlReader_SetInput(reader2
, reader_input
);
522 ok(hr
== E_NOINTERFACE
, "Expected E_NOINTERFACE, got %08x\n", hr
);
523 ok_iids(&input_iids
, setinput_readerinput
, NULL
, FALSE
);
525 IXmlReader_Release(reader2
);
526 IXmlReader_Release(reader
);
528 IUnknown_Release(reader_input
);
529 IUnknown_Release(input
);
532 static void test_reader_state(void)
537 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader
, NULL
);
538 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
540 /* invalid arguments */
541 hr
= IXmlReader_GetProperty(reader
, XmlReaderProperty_ReadState
, NULL
);
542 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
544 IXmlReader_Release(reader
);
547 static void test_read_xmldeclaration(void)
555 hr
= pCreateXmlReader(&IID_IXmlReader
, (LPVOID
*)&reader
, NULL
);
556 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
558 /* position methods with Null args */
559 hr
= IXmlReader_GetLineNumber(reader
, NULL
);
560 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
562 hr
= IXmlReader_GetLinePosition(reader
, NULL
);
563 ok(hr
== E_INVALIDARG
, "Expected E_INVALIDARG, got %08x\n", hr
);
565 stream
= create_stream_on_data(xmldecl_full
, sizeof(xmldecl_full
));
567 hr
= IXmlReader_SetInput(reader
, (IUnknown
*)stream
);
568 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
570 ok_pos(reader
, 0, 0, -1, -1, FALSE
);
573 hr
= IXmlReader_Read(reader
, &type
);
575 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
576 ok(type
== XmlNodeType_XmlDeclaration
,
577 "Expected XmlNodeType_XmlDeclaration, got %s\n", type_to_str(type
));
579 /* new version 1.2.x and 1.3.x properly update position for <?xml ?> */
580 ok_pos(reader
, 1, 3, -1, 55, TRUE
);
582 /* check attributes */
583 hr
= IXmlReader_MoveToNextAttribute(reader
);
584 todo_wine
ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
585 ok_pos(reader
, 1, 7, -1, 55, TRUE
);
587 hr
= IXmlReader_MoveToFirstAttribute(reader
);
588 todo_wine
ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
589 ok_pos(reader
, 1, 7, -1, 55, TRUE
);
591 hr
= IXmlReader_GetAttributeCount(reader
, &count
);
593 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
594 ok(count
== 3, "Expected 3, got %d\n", count
);
596 hr
= IXmlReader_GetDepth(reader
, &count
);
598 ok(hr
== S_OK
, "Expected S_OK, got %08x\n", hr
);
599 ok(count
== 1, "Expected 1, got %d\n", count
);
602 IStream_Release(stream
);
603 IXmlReader_Release(reader
);
610 r
= CoInitialize( NULL
);
611 ok( r
== S_OK
, "failed to init com\n");
613 if (!init_pointers())
619 test_reader_create();
622 test_read_xmldeclaration();