302de66acc8fc07c0a760dc4c8a731af08268c7b
[reactos.git] / modules / rostests / winetests / xmllite / reader.c
1 /*
2 * IXmlReader tests
3 *
4 * Copyright 2010, 2012-2013, 2016-2017 Nikolay Sivov
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #define COBJMACROS
26 #define CONST_VTABLE
27
28 //#include <stdarg.h>
29 #include <stdio.h>
30
31 #include <windef.h>
32 #include <winbase.h>
33 #include <winnls.h>
34 #include <initguid.h>
35 #include <ole2.h>
36 #include <xmllite.h>
37 #include <wine/test.h>
38
39 DEFINE_GUID(IID_IXmlReaderInput, 0x0b3ccc9b, 0x9214, 0x428b, 0xa2, 0xae, 0xef, 0x3a, 0xa8, 0x71, 0xaf, 0xda);
40
41 static WCHAR *a2w(const char *str)
42 {
43 int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
44 WCHAR *ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
45 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
46 return ret;
47 }
48
49 static void free_str(WCHAR *str)
50 {
51 HeapFree(GetProcessHeap(), 0, str);
52 }
53
54 static int strcmp_wa(const WCHAR *str1, const char *stra)
55 {
56 WCHAR *str2 = a2w(stra);
57 int r = lstrcmpW(str1, str2);
58 free_str(str2);
59 return r;
60 }
61
62 static const char xmldecl_full[] = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
63 static const char xmldecl_short[] = "<?xml version=\"1.0\"?><RegistrationInfo/>";
64
65 static IStream *create_stream_on_data(const void *data, unsigned int size)
66 {
67 IStream *stream = NULL;
68 HGLOBAL hglobal;
69 void *ptr;
70 HRESULT hr;
71
72 hglobal = GlobalAlloc(GHND, size);
73 ptr = GlobalLock(hglobal);
74
75 memcpy(ptr, data, size);
76
77 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
78 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
79 ok(stream != NULL, "Expected non-NULL stream\n");
80
81 GlobalUnlock(hglobal);
82
83 return stream;
84 }
85
86 static void test_reader_pos(IXmlReader *reader, UINT line, UINT pos, UINT line_broken,
87 UINT pos_broken, int _line_)
88 {
89 UINT l = ~0u, p = ~0u;
90 BOOL broken_state;
91
92 IXmlReader_GetLineNumber(reader, &l);
93 IXmlReader_GetLinePosition(reader, &p);
94
95 if (line_broken == ~0u && pos_broken == ~0u)
96 broken_state = FALSE;
97 else
98 broken_state = broken((line_broken == ~0u ? line : line_broken) == l &&
99 (pos_broken == ~0u ? pos : pos_broken) == p);
100
101 ok_(__FILE__, _line_)((l == line && pos == p) || broken_state,
102 "Expected (%d,%d), got (%d,%d)\n", line, pos, l, p);
103 }
104 #define TEST_READER_POSITION(reader, line, pos) \
105 test_reader_pos(reader, line, pos, ~0u, ~0u, __LINE__)
106 #define TEST_READER_POSITION2(reader, line, pos, line_broken, pos_broken) \
107 test_reader_pos(reader, line, pos, line_broken, pos_broken, __LINE__)
108
109 typedef struct input_iids_t {
110 IID iids[10];
111 int count;
112 } input_iids_t;
113
114 static const IID *setinput_full[] = {
115 &IID_IXmlReaderInput,
116 &IID_IStream,
117 &IID_ISequentialStream,
118 NULL
119 };
120
121 /* this applies to early xmllite versions */
122 static const IID *setinput_full_old[] = {
123 &IID_IXmlReaderInput,
124 &IID_ISequentialStream,
125 &IID_IStream,
126 NULL
127 };
128
129 /* after ::SetInput(IXmlReaderInput*) */
130 static const IID *setinput_readerinput[] = {
131 &IID_IStream,
132 &IID_ISequentialStream,
133 NULL
134 };
135
136 static const IID *empty_seq[] = {
137 NULL
138 };
139
140 static input_iids_t input_iids;
141
142 static void ok_iids_(const input_iids_t *iids, const IID **expected, const IID **exp_broken, BOOL todo, int line)
143 {
144 int i = 0, size = 0;
145
146 while (expected[i++]) size++;
147
148 todo_wine_if (todo)
149 ok_(__FILE__, line)(iids->count == size, "Sequence size mismatch (%d), got (%d)\n", size, iids->count);
150
151 if (iids->count != size) return;
152
153 for (i = 0; i < size; i++) {
154 ok_(__FILE__, line)(IsEqualGUID(&iids->iids[i], expected[i]) ||
155 (exp_broken ? broken(IsEqualGUID(&iids->iids[i], exp_broken[i])) : FALSE),
156 "Wrong IID(%d), got %s\n", i, wine_dbgstr_guid(&iids->iids[i]));
157 }
158 }
159 #define ok_iids(got, exp, brk, todo) ok_iids_(got, exp, brk, todo, __LINE__)
160
161 static const char *state_to_str(XmlReadState state)
162 {
163 static const char* state_names[] = {
164 "XmlReadState_Initial",
165 "XmlReadState_Interactive",
166 "XmlReadState_Error",
167 "XmlReadState_EndOfFile",
168 "XmlReadState_Closed"
169 };
170
171 static const char unknown[] = "unknown";
172
173 switch (state)
174 {
175 case XmlReadState_Initial:
176 case XmlReadState_Interactive:
177 case XmlReadState_Error:
178 case XmlReadState_EndOfFile:
179 case XmlReadState_Closed:
180 return state_names[state];
181 default:
182 return unknown;
183 }
184 }
185
186 static const char *type_to_str(XmlNodeType type)
187 {
188 static const char* type_names[] = {
189 "XmlNodeType_None",
190 "XmlNodeType_Element",
191 "XmlNodeType_Attribute",
192 "XmlNodeType_Text",
193 "XmlNodeType_CDATA",
194 "", "",
195 "XmlNodeType_ProcessingInstruction",
196 "XmlNodeType_Comment",
197 "",
198 "XmlNodeType_DocumentType",
199 "", "",
200 "XmlNodeType_Whitespace",
201 "",
202 "XmlNodeType_EndElement",
203 "",
204 "XmlNodeType_XmlDeclaration"
205 };
206
207 static const char unknown[] = "unknown";
208
209 switch (type)
210 {
211 case XmlNodeType_None:
212 case XmlNodeType_Element:
213 case XmlNodeType_Attribute:
214 case XmlNodeType_Text:
215 case XmlNodeType_CDATA:
216 case XmlNodeType_ProcessingInstruction:
217 case XmlNodeType_Comment:
218 case XmlNodeType_DocumentType:
219 case XmlNodeType_Whitespace:
220 case XmlNodeType_EndElement:
221 case XmlNodeType_XmlDeclaration:
222 return type_names[type];
223 default:
224 return unknown;
225 }
226 }
227
228 #define set_input_string(a,b) _set_input_string(__LINE__,a,b);
229 static void _set_input_string(unsigned line, IXmlReader *reader, const char *xml)
230 {
231 IStream *stream;
232 HRESULT hr;
233
234 stream = create_stream_on_data(xml, strlen(xml));
235
236 hr = IXmlReader_SetInput(reader, (IUnknown *)stream);
237 ok_(__FILE__,line)(hr == S_OK, "got %08x\n", hr);
238
239 IStream_Release(stream);
240 }
241
242 #define read_node(a,b) _read_node(__LINE__,a,b)
243 static void _read_node(unsigned line, IXmlReader *reader, XmlNodeType expected_type)
244 {
245 XmlNodeType type;
246 HRESULT hr;
247
248 hr = IXmlReader_Read(reader, &type);
249 if (expected_type == XmlNodeType_None)
250 ok_(__FILE__,line)(hr == S_FALSE, "Read returned %08x, expected S_FALSE\n", hr);
251 else
252 ok_(__FILE__,line)(hr == S_OK, "Read returned %08x\n", hr);
253 ok_(__FILE__,line)(type == expected_type, "read type %d, expected %d\n", type, expected_type);
254 }
255
256 #define next_attribute(a) _next_attribute(__LINE__,a)
257 static void _next_attribute(unsigned line, IXmlReader *reader)
258 {
259 HRESULT hr;
260 hr = IXmlReader_MoveToNextAttribute(reader);
261 ok_(__FILE__,line)(hr == S_OK, "MoveToNextAttribute returned %08x\n", hr);
262 }
263
264 #define move_to_element(a) _move_to_element(__LINE__,a)
265 static void _move_to_element(unsigned line, IXmlReader *reader)
266 {
267 HRESULT hr;
268 hr = IXmlReader_MoveToElement(reader);
269 ok_(__FILE__,line)(hr == S_OK, "MoveToElement failed: %08x\n", hr);
270 }
271
272 static void test_read_state(IXmlReader *reader, XmlReadState expected,
273 XmlReadState exp_broken, int line)
274 {
275 BOOL broken_state;
276 LONG_PTR state;
277
278 state = -1; /* invalid state value */
279 IXmlReader_GetProperty(reader, XmlReaderProperty_ReadState, &state);
280
281 if (exp_broken == -1)
282 broken_state = FALSE;
283 else
284 broken_state = broken(exp_broken == state);
285
286 ok_(__FILE__, line)(state == expected || broken_state, "Expected (%s), got (%s)\n",
287 state_to_str(expected), state_to_str(state));
288 }
289
290 #define TEST_READER_STATE(reader, state) test_read_state(reader, state, -1, __LINE__)
291 #define TEST_READER_STATE2(reader, state, brk) test_read_state(reader, state, brk, __LINE__)
292
293 #define reader_value(a,b) _reader_value(__LINE__,a,b)
294 static const WCHAR *_reader_value(unsigned line, IXmlReader *reader, const char *expect)
295 {
296 const WCHAR *str = (void*)0xdeadbeef;
297 ULONG len = 0xdeadbeef;
298 HRESULT hr;
299
300 hr = IXmlReader_GetValue(reader, &str, &len);
301 ok_(__FILE__,line)(hr == S_OK, "GetValue returned %08x\n", hr);
302 ok_(__FILE__,line)(len == lstrlenW(str), "len = %u\n", len);
303 ok_(__FILE__,line)(!strcmp_wa(str, expect), "value = %s\n", wine_dbgstr_w(str));
304 return str;
305 }
306
307 #define reader_name(a,b) _reader_name(__LINE__,a,b)
308 static const WCHAR *_reader_name(unsigned line, IXmlReader *reader, const char *expect)
309 {
310 const WCHAR *str = (void*)0xdeadbeef;
311 ULONG len = 0xdeadbeef;
312 HRESULT hr;
313
314 hr = IXmlReader_GetLocalName(reader, &str, &len);
315 ok_(__FILE__,line)(hr == S_OK, "GetLocalName returned %08x\n", hr);
316 ok_(__FILE__,line)(len == lstrlenW(str), "len = %u\n", len);
317 ok_(__FILE__,line)(!strcmp_wa(str, expect), "name = %s\n", wine_dbgstr_w(str));
318 return str;
319 }
320
321 #define reader_prefix(a,b) _reader_prefix(__LINE__,a,b)
322 static const WCHAR *_reader_prefix(unsigned line, IXmlReader *reader, const char *expect)
323 {
324 const WCHAR *str = (void*)0xdeadbeef;
325 ULONG len = 0xdeadbeef;
326 HRESULT hr;
327
328 hr = IXmlReader_GetPrefix(reader, &str, &len);
329 ok_(__FILE__,line)(hr == S_OK, "GetPrefix returned %08x\n", hr);
330 ok_(__FILE__,line)(len == lstrlenW(str), "len = %u\n", len);
331 ok_(__FILE__,line)(!strcmp_wa(str, expect), "prefix = %s\n", wine_dbgstr_w(str));
332 return str;
333 }
334
335 #define reader_namespace(a,b) _reader_namespace(__LINE__,a,b)
336 static const WCHAR *_reader_namespace(unsigned line, IXmlReader *reader, const char *expect)
337 {
338 const WCHAR *str = (void*)0xdeadbeef;
339 ULONG len = 0xdeadbeef;
340 HRESULT hr;
341
342 hr = IXmlReader_GetNamespaceUri(reader, &str, &len);
343 ok_(__FILE__,line)(hr == S_OK, "GetNamespaceUri returned %08x\n", hr);
344 ok_(__FILE__,line)(len == lstrlenW(str), "len = %u\n", len);
345 ok_(__FILE__,line)(!strcmp_wa(str, expect), "namespace = %s\n", wine_dbgstr_w(str));
346 return str;
347 }
348
349 #define reader_qname(a,b) _reader_qname(a,b,__LINE__)
350 static const WCHAR *_reader_qname(IXmlReader *reader, const char *expect, unsigned line)
351 {
352 const WCHAR *str = (void*)0xdeadbeef;
353 ULONG len = 0xdeadbeef;
354 HRESULT hr;
355
356 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
357 ok_(__FILE__,line)(hr == S_OK, "GetQualifiedName returned %08x\n", hr);
358 ok_(__FILE__,line)(len == lstrlenW(str), "len = %u\n", len);
359 ok_(__FILE__,line)(!strcmp_wa(str, expect), "name = %s\n", wine_dbgstr_w(str));
360 return str;
361 }
362
363 #define read_value_char(a,b) _read_value_char(a,b,__LINE__)
364 static void _read_value_char(IXmlReader *reader, WCHAR expected_char, unsigned line)
365 {
366 WCHAR c = 0xffff;
367 UINT count = 0;
368 HRESULT hr;
369
370 hr = IXmlReader_ReadValueChunk(reader, &c, 1, &count);
371 ok_(__FILE__,line)(hr == S_OK, "got %08x\n", hr);
372 ok_(__FILE__,line)(count == 1, "got %u\n", c);
373 ok_(__FILE__,line)(c == expected_char, "got %x\n", c);
374 }
375
376 typedef struct _testinput
377 {
378 IUnknown IUnknown_iface;
379 LONG ref;
380 } testinput;
381
382 static inline testinput *impl_from_IUnknown(IUnknown *iface)
383 {
384 return CONTAINING_RECORD(iface, testinput, IUnknown_iface);
385 }
386
387 static HRESULT WINAPI testinput_QueryInterface(IUnknown *iface, REFIID riid, void** ppvObj)
388 {
389 if (IsEqualGUID( riid, &IID_IUnknown ))
390 {
391 *ppvObj = iface;
392 IUnknown_AddRef(iface);
393 return S_OK;
394 }
395
396 input_iids.iids[input_iids.count++] = *riid;
397
398 *ppvObj = NULL;
399
400 return E_NOINTERFACE;
401 }
402
403 static ULONG WINAPI testinput_AddRef(IUnknown *iface)
404 {
405 testinput *This = impl_from_IUnknown(iface);
406 return InterlockedIncrement(&This->ref);
407 }
408
409 static ULONG WINAPI testinput_Release(IUnknown *iface)
410 {
411 testinput *This = impl_from_IUnknown(iface);
412 LONG ref;
413
414 ref = InterlockedDecrement(&This->ref);
415 if (ref == 0)
416 {
417 HeapFree(GetProcessHeap(), 0, This);
418 }
419
420 return ref;
421 }
422
423 static const struct IUnknownVtbl testinput_vtbl =
424 {
425 testinput_QueryInterface,
426 testinput_AddRef,
427 testinput_Release
428 };
429
430 static HRESULT testinput_createinstance(void **ppObj)
431 {
432 testinput *input;
433
434 input = HeapAlloc(GetProcessHeap(), 0, sizeof (*input));
435 if(!input) return E_OUTOFMEMORY;
436
437 input->IUnknown_iface.lpVtbl = &testinput_vtbl;
438 input->ref = 1;
439
440 *ppObj = &input->IUnknown_iface;
441
442 return S_OK;
443 }
444
445 static HRESULT WINAPI teststream_QueryInterface(ISequentialStream *iface, REFIID riid, void **obj)
446 {
447 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_ISequentialStream))
448 {
449 *obj = iface;
450 return S_OK;
451 }
452
453 *obj = NULL;
454 return E_NOINTERFACE;
455 }
456
457 static ULONG WINAPI teststream_AddRef(ISequentialStream *iface)
458 {
459 return 2;
460 }
461
462 static ULONG WINAPI teststream_Release(ISequentialStream *iface)
463 {
464 return 1;
465 }
466
467 static int stream_readcall;
468
469 static HRESULT WINAPI teststream_Read(ISequentialStream *iface, void *pv, ULONG cb, ULONG *pread)
470 {
471 static const char xml[] = "<!-- comment -->";
472
473 if (stream_readcall++)
474 {
475 *pread = 0;
476 return E_PENDING;
477 }
478
479 *pread = sizeof(xml) / 2;
480 memcpy(pv, xml, *pread);
481 return S_OK;
482 }
483
484 static HRESULT WINAPI teststream_Write(ISequentialStream *iface, const void *pv, ULONG cb, ULONG *written)
485 {
486 ok(0, "unexpected call\n");
487 return E_NOTIMPL;
488 }
489
490 static const ISequentialStreamVtbl teststreamvtbl =
491 {
492 teststream_QueryInterface,
493 teststream_AddRef,
494 teststream_Release,
495 teststream_Read,
496 teststream_Write
497 };
498
499 static HRESULT WINAPI resolver_QI(IXmlResolver *iface, REFIID riid, void **obj)
500 {
501 ok(0, "unexpected call, riid %s\n", wine_dbgstr_guid(riid));
502
503 if (IsEqualIID(riid, &IID_IXmlResolver) || IsEqualIID(riid, &IID_IUnknown))
504 {
505 *obj = iface;
506 IXmlResolver_AddRef(iface);
507 return S_OK;
508 }
509
510 *obj = NULL;
511 return E_NOINTERFACE;
512 }
513
514 static ULONG WINAPI resolver_AddRef(IXmlResolver *iface)
515 {
516 return 2;
517 }
518
519 static ULONG WINAPI resolver_Release(IXmlResolver *iface)
520 {
521 return 1;
522 }
523
524 static HRESULT WINAPI resolver_ResolveUri(IXmlResolver *iface, const WCHAR *base_uri,
525 const WCHAR *public_id, const WCHAR *system_id, IUnknown **input)
526 {
527 ok(0, "unexpected call\n");
528 return E_NOTIMPL;
529 }
530
531 static const IXmlResolverVtbl resolvervtbl =
532 {
533 resolver_QI,
534 resolver_AddRef,
535 resolver_Release,
536 resolver_ResolveUri
537 };
538
539 static IXmlResolver testresolver = { &resolvervtbl };
540
541 static void test_reader_create(void)
542 {
543 IXmlResolver *resolver;
544 IUnknown *input, *unk;
545 IXmlReader *reader;
546 DtdProcessing dtd;
547 XmlNodeType nodetype;
548 HRESULT hr;
549
550 /* crashes native */
551 if (0)
552 {
553 CreateXmlReader(&IID_IXmlReader, NULL, NULL);
554 CreateXmlReader(NULL, (void**)&reader, NULL);
555 }
556
557 hr = CreateXmlReader(&IID_IStream, (void **)&unk, NULL);
558 ok(hr == E_NOINTERFACE, "got %08x\n", hr);
559
560 hr = CreateXmlReader(&IID_IUnknown, (void **)&unk, NULL);
561 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
562 hr = IUnknown_QueryInterface(unk, &IID_IXmlReader, (void **)&reader);
563 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
564 ok(unk == (IUnknown *)reader, "unexpected interface\n");
565 IXmlReader_Release(reader);
566 IUnknown_Release(unk);
567
568 hr = CreateXmlReader(&IID_IUnknown, (void **)&reader, NULL);
569 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
570 IXmlReader_Release(reader);
571
572 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
573 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
574
575 TEST_READER_STATE(reader, XmlReadState_Closed);
576
577 nodetype = XmlNodeType_Element;
578 hr = IXmlReader_GetNodeType(reader, &nodetype);
579 ok(hr == S_FALSE, "got %08x\n", hr);
580 ok(nodetype == XmlNodeType_None, "got %d\n", nodetype);
581
582 /* crashes on XP, 2k3, works on newer versions */
583 if (0)
584 {
585 hr = IXmlReader_GetNodeType(reader, NULL);
586 ok(hr == E_INVALIDARG, "got %08x\n", hr);
587 }
588
589 resolver = (void*)0xdeadbeef;
590 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_XmlResolver, (LONG_PTR*)&resolver);
591 ok(hr == S_OK, "got 0x%08x\n", hr);
592 ok(resolver == NULL, "got %p\n", resolver);
593
594 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_XmlResolver, 0);
595 ok(hr == S_OK, "got 0x%08x\n", hr);
596
597 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_XmlResolver, (LONG_PTR)&testresolver);
598 ok(hr == S_OK, "got 0x%08x\n", hr);
599
600 resolver = NULL;
601 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_XmlResolver, (LONG_PTR*)&resolver);
602 ok(hr == S_OK, "got 0x%08x\n", hr);
603 ok(resolver == &testresolver, "got %p\n", resolver);
604 IXmlResolver_Release(resolver);
605
606 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_XmlResolver, 0);
607 ok(hr == S_OK, "got 0x%08x\n", hr);
608
609 dtd = 2;
610 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_DtdProcessing, (LONG_PTR*)&dtd);
611 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
612 ok(dtd == DtdProcessing_Prohibit, "got %d\n", dtd);
613
614 dtd = 2;
615 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, dtd);
616 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
617
618 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, -1);
619 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
620
621 /* Null input pointer, releases previous input */
622 hr = IXmlReader_SetInput(reader, NULL);
623 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
624
625 TEST_READER_STATE2(reader, XmlReadState_Initial, XmlReadState_Closed);
626
627 /* test input interface selection sequence */
628 hr = testinput_createinstance((void**)&input);
629 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
630
631 if (hr == S_OK)
632 {
633 input_iids.count = 0;
634 hr = IXmlReader_SetInput(reader, input);
635 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
636 ok_iids(&input_iids, setinput_full, setinput_full_old, FALSE);
637 IUnknown_Release(input);
638 }
639 IXmlReader_Release(reader);
640 }
641
642 static void test_readerinput(void)
643 {
644 IXmlReaderInput *reader_input;
645 IXmlReader *reader, *reader2;
646 IUnknown *obj, *input;
647 IStream *stream, *stream2;
648 XmlNodeType nodetype;
649 HRESULT hr;
650 LONG ref;
651
652 hr = CreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, NULL);
653 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
654 hr = CreateXmlReaderInputWithEncodingName(NULL, NULL, NULL, FALSE, NULL, &reader_input);
655 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
656
657 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
658 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
659
660 ref = IStream_AddRef(stream);
661 ok(ref == 2, "Expected 2, got %d\n", ref);
662 IStream_Release(stream);
663 hr = CreateXmlReaderInputWithEncodingName((IUnknown*)stream, NULL, NULL, FALSE, NULL, &reader_input);
664 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
665
666 hr = IUnknown_QueryInterface(reader_input, &IID_IStream, (void**)&stream2);
667 ok(hr == E_NOINTERFACE, "Expected S_OK, got %08x\n", hr);
668
669 hr = IUnknown_QueryInterface(reader_input, &IID_ISequentialStream, (void**)&stream2);
670 ok(hr == E_NOINTERFACE, "Expected S_OK, got %08x\n", hr);
671
672 /* IXmlReaderInput grabs a stream reference */
673 ref = IStream_AddRef(stream);
674 ok(ref == 3, "Expected 3, got %d\n", ref);
675 IStream_Release(stream);
676
677 /* try ::SetInput() with valid IXmlReaderInput */
678 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
679 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
680
681 ref = IUnknown_AddRef(reader_input);
682 ok(ref == 2, "Expected 2, got %d\n", ref);
683 IUnknown_Release(reader_input);
684
685 hr = IXmlReader_SetInput(reader, reader_input);
686 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
687
688 TEST_READER_STATE(reader, XmlReadState_Initial);
689
690 nodetype = XmlNodeType_Element;
691 hr = IXmlReader_GetNodeType(reader, &nodetype);
692 ok(hr == S_OK, "got %08x\n", hr);
693 ok(nodetype == XmlNodeType_None, "got %d\n", nodetype);
694
695 /* IXmlReader grabs a IXmlReaderInput reference */
696 ref = IUnknown_AddRef(reader_input);
697 ok(ref == 3, "Expected 3, got %d\n", ref);
698 IUnknown_Release(reader_input);
699
700 ref = IStream_AddRef(stream);
701 ok(ref == 4, "Expected 4, got %d\n", ref);
702 IStream_Release(stream);
703
704 /* reset input and check state */
705 hr = IXmlReader_SetInput(reader, NULL);
706 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
707
708 TEST_READER_STATE2(reader, XmlReadState_Initial, XmlReadState_Closed);
709
710 IXmlReader_Release(reader);
711
712 ref = IStream_AddRef(stream);
713 ok(ref == 3, "Expected 3, got %d\n", ref);
714 IStream_Release(stream);
715
716 ref = IUnknown_AddRef(reader_input);
717 ok(ref == 2, "Expected 2, got %d\n", ref);
718 IUnknown_Release(reader_input);
719
720 /* IID_IXmlReaderInput */
721 /* it returns a kind of private undocumented vtable incompatible with IUnknown,
722 so it's not a COM interface actually.
723 Such query will be used only to check if input is really IXmlReaderInput */
724 obj = (IUnknown*)0xdeadbeef;
725 hr = IUnknown_QueryInterface(reader_input, &IID_IXmlReaderInput, (void**)&obj);
726 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
727 ref = IUnknown_AddRef(reader_input);
728 ok(ref == 3, "Expected 3, got %d\n", ref);
729 IUnknown_Release(reader_input);
730
731 IUnknown_Release(reader_input);
732 IUnknown_Release(reader_input);
733 IStream_Release(stream);
734
735 /* test input interface selection sequence */
736 input = NULL;
737 hr = testinput_createinstance((void**)&input);
738 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
739
740 input_iids.count = 0;
741 ref = IUnknown_AddRef(input);
742 ok(ref == 2, "Expected 2, got %d\n", ref);
743 IUnknown_Release(input);
744 hr = CreateXmlReaderInputWithEncodingName(input, NULL, NULL, FALSE, NULL, &reader_input);
745 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
746 ok_iids(&input_iids, empty_seq, NULL, FALSE);
747 /* IXmlReaderInput stores stream interface as IUnknown */
748 ref = IUnknown_AddRef(input);
749 ok(ref == 3, "Expected 3, got %d\n", ref);
750 IUnknown_Release(input);
751
752 hr = CreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader, NULL);
753 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
754
755 input_iids.count = 0;
756 ref = IUnknown_AddRef(reader_input);
757 ok(ref == 2, "Expected 2, got %d\n", ref);
758 IUnknown_Release(reader_input);
759 ref = IUnknown_AddRef(input);
760 ok(ref == 3, "Expected 3, got %d\n", ref);
761 IUnknown_Release(input);
762 hr = IXmlReader_SetInput(reader, reader_input);
763 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
764 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
765
766 TEST_READER_STATE(reader, XmlReadState_Closed);
767
768 ref = IUnknown_AddRef(input);
769 ok(ref == 3, "Expected 3, got %d\n", ref);
770 IUnknown_Release(input);
771
772 ref = IUnknown_AddRef(reader_input);
773 ok(ref == 3 || broken(ref == 2) /* versions 1.0.x and 1.1.x - XP, Vista */,
774 "Expected 3, got %d\n", ref);
775 IUnknown_Release(reader_input);
776 /* repeat another time, no check or caching here */
777 input_iids.count = 0;
778 hr = IXmlReader_SetInput(reader, reader_input);
779 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
780 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
781
782 /* another reader */
783 hr = CreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader2, NULL);
784 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
785
786 /* resolving from IXmlReaderInput to IStream/ISequentialStream is done at
787 ::SetInput() level, each time it's called */
788 input_iids.count = 0;
789 hr = IXmlReader_SetInput(reader2, reader_input);
790 ok(hr == E_NOINTERFACE, "Expected E_NOINTERFACE, got %08x\n", hr);
791 ok_iids(&input_iids, setinput_readerinput, NULL, FALSE);
792
793 IXmlReader_Release(reader2);
794 IXmlReader_Release(reader);
795
796 IUnknown_Release(reader_input);
797 IUnknown_Release(input);
798 }
799
800 static void test_reader_state(void)
801 {
802 XmlNodeType nodetype;
803 IXmlReader *reader;
804 HRESULT hr;
805
806 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
807 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
808
809 /* invalid arguments */
810 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_ReadState, NULL);
811 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
812
813 /* attempt to read on closed reader */
814 TEST_READER_STATE(reader, XmlReadState_Closed);
815
816 if (0)
817 {
818 /* newer versions crash here, probably because no input was set */
819 hr = IXmlReader_Read(reader, &nodetype);
820 ok(hr == S_FALSE, "got %08x\n", hr);
821 }
822 set_input_string(reader, "xml");
823 TEST_READER_STATE(reader, XmlReadState_Initial);
824
825 nodetype = XmlNodeType_Element;
826 hr = IXmlReader_Read(reader, &nodetype);
827 todo_wine
828 ok(FAILED(hr), "got %08x\n", hr);
829 ok(nodetype == XmlNodeType_None, "Unexpected node type %d\n", nodetype);
830
831 todo_wine
832 TEST_READER_STATE(reader, XmlReadState_Error);
833
834 nodetype = XmlNodeType_Element;
835 hr = IXmlReader_Read(reader, &nodetype);
836 todo_wine
837 ok(FAILED(hr), "got %08x\n", hr);
838 ok(nodetype == XmlNodeType_None, "Unexpected node type %d\n", nodetype);
839
840 IXmlReader_Release(reader);
841 }
842
843 static void test_reader_depth(IXmlReader *reader, UINT depth, UINT brk, int line)
844 {
845 BOOL condition;
846 UINT d = ~0u;
847
848 IXmlReader_GetDepth(reader, &d);
849
850 condition = d == depth;
851 if (brk != ~0u)
852 condition |= broken(d == brk);
853 ok_(__FILE__, line)(condition, "Unexpected nesting depth %u, expected %u\n", d, depth);
854 }
855
856 #define TEST_DEPTH(reader, depth) test_reader_depth(reader, depth, ~0u, __LINE__)
857 #define TEST_DEPTH2(reader, depth, brk) test_reader_depth(reader, depth, brk, __LINE__)
858
859 static void test_read_xmldeclaration(void)
860 {
861 static const struct
862 {
863 WCHAR name[12];
864 WCHAR val[12];
865 } name_val[] =
866 {
867 { {'v','e','r','s','i','o','n',0}, {'1','.','0',0} },
868 { {'e','n','c','o','d','i','n','g',0}, {'U','T','F','-','8',0} },
869 { {'s','t','a','n','d','a','l','o','n','e',0}, {'y','e','s',0} }
870 };
871 IXmlReader *reader;
872 IStream *stream;
873 HRESULT hr;
874 XmlNodeType type;
875 UINT count = 0, len, i;
876 BOOL ret;
877 const WCHAR *val;
878
879 hr = CreateXmlReader(&IID_IXmlReader, (LPVOID*)&reader, NULL);
880 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
881
882 stream = create_stream_on_data(xmldecl_full, sizeof(xmldecl_full));
883
884 hr = IXmlReader_SetInput(reader, (IUnknown*)stream);
885 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
886
887 hr = IXmlReader_GetAttributeCount(reader, &count);
888 ok(hr == S_OK, "got %08x\n", hr);
889 ok(count == 0, "got %d\n", count);
890
891 /* try to move without attributes */
892 hr = IXmlReader_MoveToElement(reader);
893 ok(hr == S_FALSE, "got %08x\n", hr);
894
895 hr = IXmlReader_MoveToNextAttribute(reader);
896 ok(hr == S_FALSE, "got %08x\n", hr);
897
898 hr = IXmlReader_MoveToFirstAttribute(reader);
899 ok(hr == S_FALSE, "got %08x\n", hr);
900
901 TEST_READER_POSITION(reader, 0, 0);
902
903 read_node(reader, XmlNodeType_XmlDeclaration);
904
905 /* new version 1.2.x and 1.3.x properly update position for <?xml ?> */
906 TEST_READER_POSITION2(reader, 1, 3, ~0u, 55);
907
908 TEST_DEPTH(reader, 0);
909 TEST_READER_STATE(reader, XmlReadState_Interactive);
910
911 reader_value(reader, "");
912
913 /* check attributes */
914 next_attribute(reader);
915
916 TEST_DEPTH(reader, 1);
917
918 type = XmlNodeType_None;
919 hr = IXmlReader_GetNodeType(reader, &type);
920 ok(hr == S_OK, "got %08x\n", hr);
921 ok(type == XmlNodeType_Attribute, "got %d\n", type);
922
923 TEST_READER_POSITION2(reader, 1, 7, ~0u, 55);
924
925 /* try to move from last attribute */
926 next_attribute(reader);
927 next_attribute(reader);
928 hr = IXmlReader_MoveToNextAttribute(reader);
929 ok(hr == S_FALSE, "got %08x\n", hr);
930
931 type = XmlNodeType_None;
932 hr = IXmlReader_GetNodeType(reader, &type);
933 ok(hr == S_OK, "got %08x\n", hr);
934 ok(type == XmlNodeType_Attribute, "got %d\n", type);
935
936 hr = IXmlReader_MoveToFirstAttribute(reader);
937 ok(hr == S_OK, "got %08x\n", hr);
938 TEST_READER_POSITION2(reader, 1, 7, ~0u, 55);
939
940 hr = IXmlReader_GetAttributeCount(reader, NULL);
941 ok(hr == E_INVALIDARG, "got %08x\n", hr);
942
943 hr = IXmlReader_GetAttributeCount(reader, &count);
944 ok(hr == S_OK, "got %08x\n", hr);
945 ok(count == 3, "Expected 3, got %d\n", count);
946
947 for (i = 0; i < count; i++)
948 {
949 len = 0;
950 hr = IXmlReader_GetLocalName(reader, &val, &len);
951 ok(hr == S_OK, "got %08x\n", hr);
952 ok(len == lstrlenW(name_val[i].name), "expected %u, got %u\n", lstrlenW(name_val[i].name), len);
953 ok(!lstrcmpW(name_val[i].name, val), "expected %s, got %s\n", wine_dbgstr_w(name_val[i].name), wine_dbgstr_w(val));
954
955 len = 0;
956 hr = IXmlReader_GetValue(reader, &val, &len);
957 ok(hr == S_OK, "got %08x\n", hr);
958 ok(len == lstrlenW(name_val[i].val), "expected %u, got %u\n", lstrlenW(name_val[i].val), len);
959 ok(!lstrcmpW(name_val[i].val, val), "expected %s, got %s\n", wine_dbgstr_w(name_val[i].val), wine_dbgstr_w(val));
960
961 hr = IXmlReader_MoveToNextAttribute(reader);
962 ok(hr == ((i < count - 1) ? S_OK : S_FALSE), "got %08x\n", hr);
963 }
964
965 TEST_DEPTH(reader, 1);
966
967 move_to_element(reader);
968 TEST_READER_POSITION2(reader, 1, 3, ~0u, 55);
969
970 type = XmlNodeType_None;
971 hr = IXmlReader_GetNodeType(reader, &type);
972 ok(hr == S_OK, "got %08x\n", hr);
973 ok(type == XmlNodeType_XmlDeclaration, "got %d\n", type);
974
975 type = XmlNodeType_XmlDeclaration;
976 hr = IXmlReader_Read(reader, &type);
977 /* newer versions return syntax error here cause document is incomplete,
978 it makes more sense than invalid char error */
979 todo_wine {
980 ok(hr == WC_E_SYNTAX || broken(hr == WC_E_XMLCHARACTER), "got 0x%08x\n", hr);
981 ok(type == XmlNodeType_None, "got %d\n", type);
982 }
983 IStream_Release(stream);
984
985 /* test short variant */
986 stream = create_stream_on_data(xmldecl_short, sizeof(xmldecl_short));
987
988 hr = IXmlReader_SetInput(reader, (IUnknown *)stream);
989 ok(hr == S_OK, "expected S_OK, got %08x\n", hr);
990
991 read_node(reader, XmlNodeType_XmlDeclaration);
992 TEST_READER_POSITION2(reader, 1, 3, ~0u, 21);
993 TEST_READER_STATE(reader, XmlReadState_Interactive);
994
995 hr = IXmlReader_GetAttributeCount(reader, &count);
996 ok(hr == S_OK, "expected S_OK, got %08x\n", hr);
997 ok(count == 1, "expected 1, got %d\n", count);
998
999 ret = IXmlReader_IsEmptyElement(reader);
1000 ok(!ret, "element should not be empty\n");
1001
1002 reader_value(reader, "");
1003 reader_name(reader, "xml");
1004
1005 reader_qname(reader, "xml");
1006
1007 /* check attributes */
1008 next_attribute(reader);
1009
1010 type = -1;
1011 hr = IXmlReader_GetNodeType(reader, &type);
1012 ok(hr == S_OK, "expected S_OK, got %08x\n", hr);
1013 ok(type == XmlNodeType_Attribute, "got %d\n", type);
1014 TEST_READER_POSITION2(reader, 1, 7, ~0u, 21);
1015
1016 /* try to move from last attribute */
1017 hr = IXmlReader_MoveToNextAttribute(reader);
1018 ok(hr == S_FALSE, "expected S_FALSE, got %08x\n", hr);
1019
1020 read_node(reader, XmlNodeType_Element);
1021 TEST_READER_POSITION2(reader, 1, 23, ~0u, 40);
1022 TEST_READER_STATE(reader, XmlReadState_Interactive);
1023
1024 hr = IXmlReader_GetAttributeCount(reader, &count);
1025 ok(hr == S_OK, "expected S_OK, got %08x\n", hr);
1026 ok(count == 0, "expected 0, got %d\n", count);
1027
1028 ret = IXmlReader_IsEmptyElement(reader);
1029 ok(ret, "element should be empty\n");
1030
1031 reader_value(reader, "");
1032 reader_name(reader, "RegistrationInfo");
1033
1034 type = -1;
1035 hr = IXmlReader_Read(reader, &type);
1036 todo_wine
1037 ok(hr == WC_E_SYNTAX || hr == WC_E_XMLCHARACTER /* XP */, "expected WC_E_SYNTAX, got %08x\n", hr);
1038 ok(type == XmlNodeType_None, "expected XmlNodeType_None, got %s\n", type_to_str(type));
1039 TEST_READER_POSITION(reader, 1, 41);
1040 todo_wine
1041 TEST_READER_STATE(reader, XmlReadState_Error);
1042
1043 IStream_Release(stream);
1044 IXmlReader_Release(reader);
1045 }
1046
1047 struct test_entry {
1048 const char *xml;
1049 const char *name;
1050 const char *value;
1051 HRESULT hr;
1052 HRESULT hr_broken; /* this is set to older version results */
1053 BOOL todo;
1054 };
1055
1056 static struct test_entry comment_tests[] = {
1057 { "<!-- comment -->", "", " comment ", S_OK },
1058 { "<!-- - comment-->", "", " - comment", S_OK },
1059 { "<!-- -- comment-->", NULL, NULL, WC_E_COMMENT, WC_E_GREATERTHAN },
1060 { "<!-- -- comment--->", NULL, NULL, WC_E_COMMENT, WC_E_GREATERTHAN },
1061 { NULL }
1062 };
1063
1064 static void test_read_comment(void)
1065 {
1066 static const char *teststr = "<a>text<!-- comment --></a>";
1067 struct test_entry *test = comment_tests;
1068 static const XmlNodeType types[] =
1069 {
1070 XmlNodeType_Element,
1071 XmlNodeType_Text,
1072 XmlNodeType_Comment,
1073 XmlNodeType_EndElement,
1074 };
1075 unsigned int i = 0;
1076 IXmlReader *reader;
1077 XmlNodeType type;
1078 HRESULT hr;
1079
1080 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1081 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1082
1083 set_input_string(reader, teststr);
1084
1085 while (IXmlReader_Read(reader, &type) == S_OK)
1086 {
1087 const WCHAR *value;
1088
1089 ok(type == types[i], "%d: unexpected node type %d\n", i, type);
1090
1091 if (type == XmlNodeType_Text || type == XmlNodeType_Comment)
1092 {
1093 hr = IXmlReader_GetValue(reader, &value, NULL);
1094 ok(hr == S_OK, "got %08x\n", hr);
1095 ok(*value != 0, "Expected node value\n");
1096 }
1097 i++;
1098 }
1099
1100 while (test->xml)
1101 {
1102 set_input_string(reader, test->xml);
1103
1104 type = XmlNodeType_None;
1105 hr = IXmlReader_Read(reader, &type);
1106 if (test->hr_broken)
1107 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1108 else
1109 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1110 if (hr == S_OK)
1111 {
1112 const WCHAR *str;
1113
1114 ok(type == XmlNodeType_Comment, "got %d for %s\n", type, test->xml);
1115
1116 reader_name(reader, "");
1117
1118 str = NULL;
1119 hr = IXmlReader_GetLocalName(reader, &str, NULL);
1120 ok(hr == S_OK, "got 0x%08x\n", hr);
1121 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1122
1123 reader_qname(reader, "");
1124
1125 str = NULL;
1126 hr = IXmlReader_GetQualifiedName(reader, &str, NULL);
1127 ok(hr == S_OK, "got 0x%08x\n", hr);
1128 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1129
1130 /* value */
1131 reader_value(reader, test->value);
1132 }
1133
1134 test++;
1135 }
1136
1137 IXmlReader_Release(reader);
1138 }
1139
1140 static struct test_entry pi_tests[] = {
1141 { "<?pi?>", "pi", "", S_OK },
1142 { "<?pi ?>", "pi", "", S_OK },
1143 { "<?pi ?>", "pi", "", S_OK },
1144 { "<?pi pi data?>", "pi", "pi data", S_OK },
1145 { "<?pi pi data ?>", "pi", "pi data ", S_OK },
1146 { "<?pi data ?>", "pi", "data ", S_OK },
1147 { "<?pi:pi?>", NULL, NULL, NC_E_NAMECOLON, WC_E_NAMECHARACTER },
1148 { "<?:pi ?>", NULL, NULL, WC_E_PI, WC_E_NAMECHARACTER },
1149 { "<?-pi ?>", NULL, NULL, WC_E_PI, WC_E_NAMECHARACTER },
1150 { "<?xml-stylesheet ?>", "xml-stylesheet", "", S_OK },
1151 { NULL }
1152 };
1153
1154 static void test_read_pi(void)
1155 {
1156 struct test_entry *test = pi_tests;
1157 IXmlReader *reader;
1158 HRESULT hr;
1159
1160 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1161 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1162
1163 while (test->xml)
1164 {
1165 XmlNodeType type;
1166
1167 set_input_string(reader, test->xml);
1168
1169 type = XmlNodeType_None;
1170 hr = IXmlReader_Read(reader, &type);
1171 if (test->hr_broken)
1172 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1173 else
1174 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1175 if (hr == S_OK)
1176 {
1177 const WCHAR *str;
1178 WCHAR *str_exp;
1179 UINT len;
1180
1181 ok(type == XmlNodeType_ProcessingInstruction, "got %d for %s\n", type, test->xml);
1182
1183 reader_name(reader, test->name);
1184
1185 len = 0;
1186 str = NULL;
1187 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1188 ok(hr == S_OK, "got 0x%08x\n", hr);
1189 ok(len == strlen(test->name), "got %u\n", len);
1190 str_exp = a2w(test->name);
1191 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1192 free_str(str_exp);
1193
1194 /* value */
1195 reader_value(reader, test->value);
1196 }
1197
1198 test++;
1199 }
1200
1201 IXmlReader_Release(reader);
1202 }
1203
1204 struct nodes_test {
1205 const char *xml;
1206 struct {
1207 XmlNodeType type;
1208 const char *value;
1209 } nodes[20];
1210 };
1211
1212 static const char misc_test_xml[] =
1213 "<!-- comment1 -->"
1214 "<!-- comment2 -->"
1215 "<?pi1 pi1body ?>"
1216 "<!-- comment3 -->"
1217 " \t \r \n"
1218 "<!-- comment4 -->"
1219 "<a>"
1220 "\r\n\t"
1221 "<b/>"
1222 "text"
1223 "<!-- comment -->"
1224 "text2"
1225 "<?pi pibody ?>"
1226 "\r\n"
1227 "</a>"
1228 ;
1229
1230 static struct nodes_test misc_test = {
1231 misc_test_xml,
1232 {
1233 {XmlNodeType_Comment, " comment1 "},
1234 {XmlNodeType_Comment, " comment2 "},
1235 {XmlNodeType_ProcessingInstruction, "pi1body "},
1236 {XmlNodeType_Comment, " comment3 "},
1237 {XmlNodeType_Whitespace, " \t \n \n"},
1238 {XmlNodeType_Comment, " comment4 "},
1239 {XmlNodeType_Element, ""},
1240 {XmlNodeType_Whitespace, "\n\t"},
1241 {XmlNodeType_Element, ""},
1242 {XmlNodeType_Text, "text"},
1243 {XmlNodeType_Comment, " comment "},
1244 {XmlNodeType_Text, "text2"},
1245 {XmlNodeType_ProcessingInstruction, "pibody "},
1246 {XmlNodeType_Whitespace, "\n"},
1247 {XmlNodeType_EndElement, ""},
1248 {XmlNodeType_None, ""}
1249 }
1250 };
1251
1252 static void test_read_full(void)
1253 {
1254 struct nodes_test *test = &misc_test;
1255 IXmlReader *reader;
1256 HRESULT hr;
1257 int i;
1258
1259 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1260 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1261
1262 set_input_string(reader, test->xml);
1263
1264 i = 0;
1265 do
1266 {
1267 read_node(reader, test->nodes[i].type);
1268 reader_value(reader, test->nodes[i].value);
1269 } while(test->nodes[i++].type != XmlNodeType_None);
1270
1271 IXmlReader_Release(reader);
1272 }
1273
1274 static const char test_public_dtd[] =
1275 "<!DOCTYPE testdtd PUBLIC \"pubid\" \"externalid uri\" >";
1276
1277 static void test_read_public_dtd(void)
1278 {
1279 static const WCHAR dtdnameW[] = {'t','e','s','t','d','t','d',0};
1280 IXmlReader *reader;
1281 const WCHAR *str;
1282 XmlNodeType type;
1283 UINT len, count;
1284 HRESULT hr;
1285
1286 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1287 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1288
1289 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, DtdProcessing_Parse);
1290 ok(hr == S_OK, "got 0x%8x\n", hr);
1291
1292 set_input_string(reader, test_public_dtd);
1293
1294 read_node(reader, XmlNodeType_DocumentType);
1295
1296 count = 0;
1297 hr = IXmlReader_GetAttributeCount(reader, &count);
1298 ok(hr == S_OK, "got %08x\n", hr);
1299 ok(count == 2, "got %d\n", count);
1300
1301 hr = IXmlReader_MoveToFirstAttribute(reader);
1302 ok(hr == S_OK, "got %08x\n", hr);
1303
1304 type = XmlNodeType_None;
1305 hr = IXmlReader_GetNodeType(reader, &type);
1306 ok(hr == S_OK, "got %08x\n", hr);
1307 ok(type == XmlNodeType_Attribute, "got %d\n", type);
1308
1309 reader_name(reader, "PUBLIC");
1310 reader_value(reader, "pubid");
1311
1312 next_attribute(reader);
1313
1314 type = XmlNodeType_None;
1315 hr = IXmlReader_GetNodeType(reader, &type);
1316 ok(hr == S_OK, "got %08x\n", hr);
1317 ok(type == XmlNodeType_Attribute, "got %d\n", type);
1318
1319 reader_name(reader, "SYSTEM");
1320 reader_value(reader, "externalid uri");
1321
1322 move_to_element(reader);
1323 reader_name(reader, "testdtd");
1324
1325 len = 0;
1326 str = NULL;
1327 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1328 ok(hr == S_OK, "got 0x%08x\n", hr);
1329 todo_wine {
1330 ok(len == lstrlenW(dtdnameW), "got %u\n", len);
1331 ok(!lstrcmpW(str, dtdnameW), "got %s\n", wine_dbgstr_w(str));
1332 }
1333 IXmlReader_Release(reader);
1334 }
1335
1336 static const char test_system_dtd[] =
1337 "<!DOCTYPE testdtd SYSTEM \"externalid uri\" >"
1338 "<!-- comment -->";
1339
1340 static void test_read_system_dtd(void)
1341 {
1342 static const WCHAR dtdnameW[] = {'t','e','s','t','d','t','d',0};
1343 IXmlReader *reader;
1344 const WCHAR *str;
1345 XmlNodeType type;
1346 UINT len, count;
1347 HRESULT hr;
1348
1349 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1350 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1351
1352 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_DtdProcessing, DtdProcessing_Parse);
1353 ok(hr == S_OK, "got 0x%8x\n", hr);
1354
1355 set_input_string(reader, test_system_dtd);
1356
1357 read_node(reader, XmlNodeType_DocumentType);
1358
1359 count = 0;
1360 hr = IXmlReader_GetAttributeCount(reader, &count);
1361 ok(hr == S_OK, "got %08x\n", hr);
1362 ok(count == 1, "got %d\n", count);
1363
1364 hr = IXmlReader_MoveToFirstAttribute(reader);
1365 ok(hr == S_OK, "got %08x\n", hr);
1366
1367 type = XmlNodeType_None;
1368 hr = IXmlReader_GetNodeType(reader, &type);
1369 ok(hr == S_OK, "got %08x\n", hr);
1370 ok(type == XmlNodeType_Attribute, "got %d\n", type);
1371
1372 reader_name(reader, "SYSTEM");
1373 reader_value(reader, "externalid uri");
1374
1375 move_to_element(reader);
1376 reader_name(reader, "testdtd");
1377
1378 len = 0;
1379 str = NULL;
1380 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1381 ok(hr == S_OK, "got 0x%08x\n", hr);
1382 todo_wine {
1383 ok(len == lstrlenW(dtdnameW), "got %u\n", len);
1384 ok(!lstrcmpW(str, dtdnameW), "got %s\n", wine_dbgstr_w(str));
1385 }
1386
1387 read_node(reader, XmlNodeType_Comment);
1388
1389 IXmlReader_Release(reader);
1390 }
1391
1392 static struct test_entry element_tests[] = {
1393 { "<a/>", "a", "", S_OK },
1394 { "<a />", "a", "", S_OK },
1395 { "<a:b/>", "a:b", "", NC_E_UNDECLAREDPREFIX },
1396 { "<:a/>", NULL, NULL, NC_E_QNAMECHARACTER },
1397 { "< a/>", NULL, NULL, NC_E_QNAMECHARACTER },
1398 { "<a>", "a", "", S_OK },
1399 { "<a >", "a", "", S_OK },
1400 { "<a \r \t\n>", "a", "", S_OK },
1401 { "</a>", NULL, NULL, NC_E_QNAMECHARACTER },
1402 { "<a:b:c />", NULL, NULL, NC_E_QNAMECOLON },
1403 { "<:b:c />", NULL, NULL, NC_E_QNAMECHARACTER },
1404 { NULL }
1405 };
1406
1407 static void test_read_element(void)
1408 {
1409 struct test_entry *test = element_tests;
1410 static const char stag[] =
1411 "<a attr1=\"_a\">"
1412 "<b attr2=\"_b\">"
1413 "text"
1414 "<c attr3=\"_c\"/>"
1415 "<d attr4=\"_d\"></d>"
1416 "</b>"
1417 "</a>";
1418 static const UINT depths[] = { 0, 1, 2, 2, 2, 3, 2, 1 };
1419 IXmlReader *reader;
1420 XmlNodeType type;
1421 unsigned int i;
1422 UINT depth;
1423 HRESULT hr;
1424
1425 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1426 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1427
1428 while (test->xml)
1429 {
1430 set_input_string(reader, test->xml);
1431
1432 type = XmlNodeType_None;
1433 hr = IXmlReader_Read(reader, &type);
1434 if (test->hr_broken)
1435 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1436 else
1437 todo_wine_if(test->hr == NC_E_UNDECLAREDPREFIX)
1438 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1439 if (hr == S_OK)
1440 {
1441 const WCHAR *str;
1442 WCHAR *str_exp;
1443 UINT len;
1444
1445 ok(type == XmlNodeType_Element, "got %d for %s\n", type, test->xml);
1446
1447 len = 0;
1448 str = NULL;
1449 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1450 ok(hr == S_OK, "got 0x%08x\n", hr);
1451 ok(len == strlen(test->name), "got %u\n", len);
1452 str_exp = a2w(test->name);
1453 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1454 free_str(str_exp);
1455
1456 /* value */
1457 reader_value(reader, "");
1458 }
1459
1460 test++;
1461 }
1462
1463 /* test reader depth increment */
1464 set_input_string(reader, stag);
1465
1466 i = 0;
1467 while (IXmlReader_Read(reader, &type) == S_OK)
1468 {
1469 UINT count;
1470
1471 ok(type == XmlNodeType_Element || type == XmlNodeType_EndElement ||
1472 type == XmlNodeType_Text, "Unexpected node type %d\n", type);
1473
1474 depth = 123;
1475 hr = IXmlReader_GetDepth(reader, &depth);
1476 ok(hr == S_OK, "got %08x\n", hr);
1477 ok(depth == depths[i], "%u: got depth %u, expected %u\n", i, depth, depths[i]);
1478
1479 if (type == XmlNodeType_Element || type == XmlNodeType_EndElement)
1480 {
1481 const WCHAR *prefix;
1482
1483 prefix = NULL;
1484 hr = IXmlReader_GetPrefix(reader, &prefix, NULL);
1485 ok(hr == S_OK, "got %08x\n", hr);
1486 ok(prefix != NULL, "got %p\n", prefix);
1487
1488 if (!*prefix)
1489 {
1490 const WCHAR *local, *qname;
1491
1492 local = NULL;
1493 hr = IXmlReader_GetLocalName(reader, &local, NULL);
1494 ok(hr == S_OK, "got %08x\n", hr);
1495 ok(local != NULL, "got %p\n", local);
1496
1497 qname = NULL;
1498 hr = IXmlReader_GetQualifiedName(reader, &qname, NULL);
1499 ok(hr == S_OK, "got %08x\n", hr);
1500 ok(qname != NULL, "got %p\n", qname);
1501
1502 ok(local == qname, "expected same pointer\n");
1503 }
1504 }
1505
1506 if (type == XmlNodeType_EndElement)
1507 {
1508 count = 1;
1509 hr = IXmlReader_GetAttributeCount(reader, &count);
1510 ok(hr == S_OK, "got %08x\n", hr);
1511 ok(count == 0, "got %u\n", count);
1512 }
1513
1514 if (type == XmlNodeType_Element)
1515 {
1516 count = 0;
1517 hr = IXmlReader_GetAttributeCount(reader, &count);
1518 ok(hr == S_OK, "got %08x\n", hr);
1519
1520 /* moving to attributes increases depth */
1521 if (count)
1522 {
1523 const WCHAR *value;
1524
1525 reader_value(reader, "");
1526
1527 hr = IXmlReader_MoveToFirstAttribute(reader);
1528 ok(hr == S_OK, "got %08x\n", hr);
1529
1530 hr = IXmlReader_GetValue(reader, &value, NULL);
1531 ok(*value != 0, "Unexpected value %s\n", wine_dbgstr_w(value));
1532
1533 depth = 123;
1534 hr = IXmlReader_GetDepth(reader, &depth);
1535 ok(hr == S_OK, "got %08x\n", hr);
1536 ok(depth == depths[i] + 1, "%u: got depth %u, expected %u\n", i, depth, depths[i] + 1);
1537
1538 move_to_element(reader);
1539 reader_value(reader, "");
1540
1541 depth = 123;
1542 hr = IXmlReader_GetDepth(reader, &depth);
1543 ok(hr == S_OK, "got %08x\n", hr);
1544 ok(depth == depths[i], "%u: got depth %u, expected %u\n", i, depth, depths[i]);
1545 }
1546 }
1547
1548 i++;
1549 }
1550
1551 /* start/end tag mismatch */
1552 set_input_string(reader, "<a></b>");
1553
1554 read_node(reader, XmlNodeType_Element);
1555
1556 type = XmlNodeType_Element;
1557 hr = IXmlReader_Read(reader, &type);
1558 ok(hr == WC_E_ELEMENTMATCH, "got %08x\n", hr);
1559 ok(type == XmlNodeType_None, "got %d\n", type);
1560 TEST_READER_STATE(reader, XmlReadState_Error);
1561
1562 IXmlReader_Release(reader);
1563 }
1564
1565 static ISequentialStream teststream = { &teststreamvtbl };
1566
1567 static void test_read_pending(void)
1568 {
1569 IXmlReader *reader;
1570 const WCHAR *value;
1571 XmlNodeType type;
1572 HRESULT hr;
1573 int c;
1574
1575 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1576 ok(hr == S_OK, "S_OK, got 0x%08x\n", hr);
1577
1578 hr = IXmlReader_SetInput(reader, (IUnknown*)&teststream);
1579 ok(hr == S_OK, "got 0x%08x\n", hr);
1580
1581 /* first read call returns incomplete node, second attempt fails with E_PENDING */
1582 stream_readcall = 0;
1583 type = XmlNodeType_Element;
1584 hr = IXmlReader_Read(reader, &type);
1585 ok(hr == S_OK || broken(hr == E_PENDING), "got 0x%08x\n", hr);
1586 /* newer versions are happy when it's enough data to detect node type,
1587 older versions keep reading until it fails to read more */
1588 todo_wine
1589 ok(stream_readcall == 1 || broken(stream_readcall > 1), "got %d\n", stream_readcall);
1590 ok(type == XmlNodeType_Comment || broken(type == XmlNodeType_None), "got %d\n", type);
1591
1592 /* newer versions' GetValue() makes an attempt to read more */
1593 c = stream_readcall;
1594 value = (void*)0xdeadbeef;
1595 hr = IXmlReader_GetValue(reader, &value, NULL);
1596 ok(hr == E_PENDING, "got 0x%08x\n", hr);
1597 ok(value == NULL || broken(value == (void*)0xdeadbeef) /* Win8 sets it to NULL */, "got %p\n", value);
1598 ok(c < stream_readcall || broken(c == stream_readcall), "got %d, expected %d\n", stream_readcall, c+1);
1599
1600 IXmlReader_Release(reader);
1601 }
1602
1603 static void test_readvaluechunk(void)
1604 {
1605 IXmlReader *reader;
1606 XmlNodeType type;
1607 WCHAR buf[64];
1608 WCHAR b;
1609 HRESULT hr;
1610 UINT c;
1611
1612 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1613 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1614
1615 set_input_string(reader, "<!-- comment1 --><!-- comment2 -->");
1616
1617 hr = IXmlReader_Read(reader, &type);
1618 ok(hr == S_OK, "got %08x\n", hr);
1619 ok(type == XmlNodeType_Comment, "type = %u\n", type);
1620
1621 read_value_char(reader, ' ');
1622 read_value_char(reader, 'c');
1623
1624 /* portion read as chunk is skipped from resulting node value */
1625 reader_value(reader, "omment1 ");
1626
1627 /* once value is returned/allocated it's not possible to read by chunk */
1628 c = 0;
1629 b = 0;
1630 hr = IXmlReader_ReadValueChunk(reader, &b, 1, &c);
1631 ok(hr == S_FALSE, "got %08x\n", hr);
1632 ok(c == 0, "got %u\n", c);
1633 ok(b == 0, "got %x\n", b);
1634
1635 c = 0xdeadbeef;
1636 hr = IXmlReader_ReadValueChunk(reader, buf, 0, &c);
1637 ok(hr == S_OK, "got %08x\n", hr);
1638 ok(!c, "c = %u\n", c);
1639
1640 reader_value(reader, "omment1 ");
1641
1642 /* read comment2 */
1643 read_node(reader, XmlNodeType_Comment);
1644
1645 c = 0xdeadbeef;
1646 hr = IXmlReader_ReadValueChunk(reader, buf, 0, &c);
1647 ok(hr == S_OK, "got %08x\n", hr);
1648 ok(!c, "c = %u\n", c);
1649
1650 c = 0xdeadbeef;
1651 memset(buf, 0xcc, sizeof(buf));
1652 hr = IXmlReader_ReadValueChunk(reader, buf, sizeof(buf)/sizeof(WCHAR), &c);
1653 ok(hr == S_OK, "got %08x\n", hr);
1654 ok(c == 10, "got %u\n", c);
1655 ok(buf[c] == 0xcccc, "buffer overflow\n");
1656 buf[c] = 0;
1657 ok(!strcmp_wa(buf, " comment2 "), "buf = %s\n", wine_dbgstr_w(buf));
1658
1659 c = 0xdeadbeef;
1660 memset(buf, 0xcc, sizeof(buf));
1661 hr = IXmlReader_ReadValueChunk(reader, buf, sizeof(buf)/sizeof(WCHAR), &c);
1662 ok(hr == S_FALSE, "got %08x\n", hr);
1663 ok(!c, "got %u\n", c);
1664
1665 /* portion read as chunk is skipped from resulting node value */
1666 reader_value(reader, "");
1667
1668 /* once value is returned/allocated it's not possible to read by chunk */
1669 c = 0xdeadbeef;
1670 b = 0xffff;
1671 hr = IXmlReader_ReadValueChunk(reader, &b, 1, &c);
1672 ok(hr == S_FALSE, "got %08x\n", hr);
1673 ok(c == 0, "got %u\n", c);
1674 ok(b == 0xffff, "got %x\n", b);
1675
1676 reader_value(reader, "");
1677
1678 IXmlReader_Release(reader);
1679 }
1680
1681 static struct test_entry cdata_tests[] = {
1682 { "<a><![CDATA[ ]]data ]]></a>", "", " ]]data ", S_OK },
1683 { "<a><![CDATA[<![CDATA[ data ]]]]></a>", "", "<![CDATA[ data ]]", S_OK },
1684 { "<a><![CDATA[\n \r\n \n\n ]]></a>", "", "\n \n \n\n ", S_OK, S_OK },
1685 { "<a><![CDATA[\r \r\r\n \n\n ]]></a>", "", "\n \n\n \n\n ", S_OK, S_OK },
1686 { "<a><![CDATA[\r\r \n\r \r \n\n ]]></a>", "", "\n\n \n\n \n \n\n ", S_OK },
1687 { NULL }
1688 };
1689
1690 static void test_read_cdata(void)
1691 {
1692 struct test_entry *test = cdata_tests;
1693 IXmlReader *reader;
1694 HRESULT hr;
1695
1696 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1697 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1698
1699 while (test->xml)
1700 {
1701 XmlNodeType type;
1702
1703 set_input_string(reader, test->xml);
1704
1705 type = XmlNodeType_None;
1706 hr = IXmlReader_Read(reader, &type);
1707
1708 /* read one more to get to CDATA */
1709 if (type == XmlNodeType_Element)
1710 {
1711 type = XmlNodeType_None;
1712 hr = IXmlReader_Read(reader, &type);
1713 }
1714
1715 if (test->hr_broken)
1716 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1717 else
1718 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1719 if (hr == S_OK)
1720 {
1721 const WCHAR *str;
1722 UINT len;
1723
1724 ok(type == XmlNodeType_CDATA, "got %d for %s\n", type, test->xml);
1725
1726 reader_name(reader, "");
1727
1728 str = NULL;
1729 hr = IXmlReader_GetLocalName(reader, &str, NULL);
1730 ok(hr == S_OK, "got 0x%08x\n", hr);
1731 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1732
1733 len = 1;
1734 str = NULL;
1735 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1736 ok(hr == S_OK, "got 0x%08x\n", hr);
1737 ok(len == 0, "got %u\n", len);
1738 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1739
1740 str = NULL;
1741 hr = IXmlReader_GetQualifiedName(reader, &str, NULL);
1742 ok(hr == S_OK, "got 0x%08x\n", hr);
1743 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1744
1745 /* value */
1746 reader_value(reader, test->value);
1747 }
1748
1749 test++;
1750 }
1751
1752 IXmlReader_Release(reader);
1753 }
1754
1755 static struct test_entry text_tests[] = {
1756 { "<a>simple text</a>", "", "simple text", S_OK },
1757 { "<a>text ]]> text</a>", "", "", WC_E_CDSECTEND },
1758 { "<a>\n \r\n \n\n text</a>", "", "\n \n \n\n text", S_OK, S_OK },
1759 { "<a>\r \r\r\n \n\n text</a>", "", "\n \n\n \n\n text", S_OK, S_OK },
1760 { NULL }
1761 };
1762
1763 static void test_read_text(void)
1764 {
1765 struct test_entry *test = text_tests;
1766 IXmlReader *reader;
1767 HRESULT hr;
1768
1769 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1770 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1771
1772 while (test->xml)
1773 {
1774 XmlNodeType type;
1775
1776 set_input_string(reader, test->xml);
1777
1778 type = XmlNodeType_None;
1779 hr = IXmlReader_Read(reader, &type);
1780
1781 /* read one more to get to text node */
1782 if (type == XmlNodeType_Element)
1783 {
1784 type = XmlNodeType_None;
1785 hr = IXmlReader_Read(reader, &type);
1786 }
1787 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1788 if (hr == S_OK)
1789 {
1790 const WCHAR *str;
1791 UINT len;
1792
1793 ok(type == XmlNodeType_Text, "got %d for %s\n", type, test->xml);
1794
1795 reader_name(reader, "");
1796
1797 str = NULL;
1798 hr = IXmlReader_GetLocalName(reader, &str, NULL);
1799 ok(hr == S_OK, "got 0x%08x\n", hr);
1800 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1801
1802 len = 1;
1803 str = NULL;
1804 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1805 ok(hr == S_OK, "got 0x%08x\n", hr);
1806 ok(len == 0, "got %u\n", len);
1807 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1808
1809 str = NULL;
1810 hr = IXmlReader_GetQualifiedName(reader, &str, NULL);
1811 ok(hr == S_OK, "got 0x%08x\n", hr);
1812 ok(*str == 0, "got %s\n", wine_dbgstr_w(str));
1813
1814 /* value */
1815 reader_value(reader, test->value);
1816 }
1817
1818 test++;
1819 }
1820
1821 IXmlReader_Release(reader);
1822 }
1823
1824 struct test_entry_empty {
1825 const char *xml;
1826 BOOL empty;
1827 };
1828
1829 static struct test_entry_empty empty_element_tests[] = {
1830 { "<a></a>", FALSE },
1831 { "<a/>", TRUE },
1832 { NULL }
1833 };
1834
1835 static void test_isemptyelement(void)
1836 {
1837 struct test_entry_empty *test = empty_element_tests;
1838 IXmlReader *reader;
1839 HRESULT hr;
1840
1841 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1842 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1843
1844 while (test->xml)
1845 {
1846 XmlNodeType type;
1847 BOOL ret;
1848
1849 set_input_string(reader, test->xml);
1850
1851 type = XmlNodeType_None;
1852 hr = IXmlReader_Read(reader, &type);
1853 ok(hr == S_OK, "got 0x%08x\n", hr);
1854 ok(type == XmlNodeType_Element, "got %d\n", type);
1855
1856 ret = IXmlReader_IsEmptyElement(reader);
1857 ok(ret == test->empty, "got %d, expected %d. xml=%s\n", ret, test->empty, test->xml);
1858
1859 test++;
1860 }
1861
1862 IXmlReader_Release(reader);
1863 }
1864
1865 static struct test_entry attributes_tests[] = {
1866 { "<a attr1=\"attrvalue\"/>", "attr1", "attrvalue", S_OK },
1867 { "<a attr1=\"a\'\'ttrvalue\"/>", "attr1", "a\'\'ttrvalue", S_OK },
1868 { "<a attr1=\'a\"ttrvalue\'/>", "attr1", "a\"ttrvalue", S_OK },
1869 { "<a attr1=\' \'/>", "attr1", " ", S_OK },
1870 { "<a attr1=\" \"/>", "attr1", " ", S_OK },
1871 { "<a attr1=\"\r\n \r \n \t\n\r\"/>", "attr1", " ", S_OK },
1872 { "<a attr1=\" val \"/>", "attr1", " val ", S_OK },
1873 { "<a attr1=\"\r\n\tval\n\"/>", "attr1", " val ", S_OK },
1874 { "<a attr1=\"val&#32;\"/>", "attr1", "val ", S_OK },
1875 { "<a attr1=\"val&#x20;\"/>", "attr1", "val ", S_OK },
1876 { "<a attr1=\"&lt;&gt;&amp;&apos;&quot;\"/>", "attr1", "<>&\'\"", S_OK },
1877 { "<a attr1=\"&entname;\"/>", NULL, NULL, WC_E_UNDECLAREDENTITY },
1878 { "<a attr1=\"val&#xfffe;\"/>", NULL, NULL, WC_E_XMLCHARACTER },
1879 { "<a attr1=\"val &#a;\"/>", NULL, NULL, WC_E_DIGIT, WC_E_SEMICOLON },
1880 { "<a attr1=\"val &#12a;\"/>", NULL, NULL, WC_E_SEMICOLON },
1881 { "<a attr1=\"val &#x12g;\"/>", NULL, NULL, WC_E_SEMICOLON },
1882 { "<a attr1=\"val &#xg;\"/>", NULL, NULL, WC_E_HEXDIGIT, WC_E_SEMICOLON },
1883 { "<a attr1=attrvalue/>", NULL, NULL, WC_E_QUOTE },
1884 { "<a attr1=\"attr<value\"/>", NULL, NULL, WC_E_LESSTHAN },
1885 { "<a attr1=\"&entname\"/>", NULL, NULL, WC_E_SEMICOLON },
1886 { NULL }
1887 };
1888
1889 static void test_read_attribute(void)
1890 {
1891 struct test_entry *test = attributes_tests;
1892 IXmlReader *reader;
1893 HRESULT hr;
1894
1895 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1896 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1897
1898 while (test->xml)
1899 {
1900 XmlNodeType type;
1901
1902 set_input_string(reader, test->xml);
1903
1904 hr = IXmlReader_Read(reader, NULL);
1905
1906 if (test->hr_broken)
1907 ok(hr == test->hr || broken(hr == test->hr_broken), "got %08x for %s\n", hr, test->xml);
1908 else
1909 ok(hr == test->hr, "got %08x for %s\n", hr, test->xml);
1910 if (hr == S_OK)
1911 {
1912 const WCHAR *str;
1913 WCHAR *str_exp;
1914 UINT len;
1915
1916 type = XmlNodeType_None;
1917 hr = IXmlReader_GetNodeType(reader, &type);
1918 ok(hr == S_OK, "Failed to get node type, %#x\n", hr);
1919
1920 ok(type == XmlNodeType_Element, "got %d for %s\n", type, test->xml);
1921
1922 hr = IXmlReader_MoveToFirstAttribute(reader);
1923 ok(hr == S_OK, "got 0x%08x\n", hr);
1924
1925 reader_name(reader, test->name);
1926
1927 len = 1;
1928 str = NULL;
1929 hr = IXmlReader_GetQualifiedName(reader, &str, &len);
1930 ok(hr == S_OK, "got 0x%08x\n", hr);
1931 ok(len == strlen(test->name), "got %u\n", len);
1932 str_exp = a2w(test->name);
1933 ok(!lstrcmpW(str, str_exp), "got %s\n", wine_dbgstr_w(str));
1934 free_str(str_exp);
1935
1936 /* value */
1937 reader_value(reader, test->value);
1938 }
1939
1940 test++;
1941 }
1942
1943 IXmlReader_Release(reader);
1944 }
1945
1946 static void test_reader_properties(void)
1947 {
1948 IXmlReader *reader;
1949 LONG_PTR value;
1950 HRESULT hr;
1951
1952 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1953 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1954
1955 value = 0;
1956 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_MaxElementDepth, &value);
1957 ok(hr == S_OK, "GetProperty failed: %08x\n", hr);
1958 ok(value == 256, "Unexpected default max depth value %ld\n", value);
1959
1960 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MultiLanguage, 0);
1961 ok(hr == S_OK, "SetProperty failed: %08x\n", hr);
1962
1963 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MaxElementDepth, 0);
1964 ok(hr == S_OK, "SetProperty failed: %08x\n", hr);
1965
1966 value = 256;
1967 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_MaxElementDepth, &value);
1968 ok(hr == S_OK, "GetProperty failed: %08x\n", hr);
1969 ok(value == 0, "Unexpected max depth value %ld\n", value);
1970
1971 IXmlReader_Release(reader);
1972 }
1973
1974 static void test_prefix(void)
1975 {
1976 static const struct
1977 {
1978 const char *xml;
1979 const char *prefix1;
1980 const char *prefix2;
1981 const char *prefix3;
1982 } prefix_tests[] =
1983 {
1984 { "<b xmlns=\"defns\" xml:a=\"a ns\"/>", "", "", "xml" },
1985 { "<c:b xmlns:c=\"c ns\" xml:a=\"a ns\"/>", "c", "xmlns", "xml" },
1986 };
1987 IXmlReader *reader;
1988 unsigned int i;
1989 HRESULT hr;
1990
1991 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
1992 ok(hr == S_OK, "S_OK, got %08x\n", hr);
1993
1994 for (i = 0; i < sizeof(prefix_tests)/sizeof(prefix_tests[0]); i++) {
1995 XmlNodeType type;
1996
1997 set_input_string(reader, prefix_tests[i].xml);
1998
1999 hr = IXmlReader_Read(reader, &type);
2000 ok(hr == S_OK, "Read() failed, %#x\n", hr);
2001 ok(type == XmlNodeType_Element, "Unexpected node type %d.\n", type);
2002
2003 reader_prefix(reader, prefix_tests[i].prefix1);
2004
2005 hr = IXmlReader_MoveToFirstAttribute(reader);
2006 ok(hr == S_OK, "MoveToFirstAttribute() failed, %#x.\n", hr);
2007
2008 hr = IXmlReader_GetNodeType(reader, &type);
2009 ok(hr == S_OK, "GetNodeType() failed, %#x.\n", hr);
2010 ok(type == XmlNodeType_Attribute, "Unexpected node type %d.\n", type);
2011
2012 reader_prefix(reader, prefix_tests[i].prefix2);
2013
2014 next_attribute(reader);
2015
2016 hr = IXmlReader_GetNodeType(reader, &type);
2017 ok(hr == S_OK, "GetNodeType() failed, %#x.\n", hr);
2018 ok(type == XmlNodeType_Attribute, "Unexpected node type %d.\n", type);
2019
2020 reader_prefix(reader, prefix_tests[i].prefix3);
2021
2022 /* back to the element, check prefix */
2023 move_to_element(reader);
2024 reader_prefix(reader, prefix_tests[i].prefix1);
2025 }
2026
2027 IXmlReader_Release(reader);
2028 }
2029
2030 static void test_namespaceuri(void)
2031 {
2032 struct uri_test
2033 {
2034 const char *xml;
2035 const char *uri[5];
2036 } uri_tests[] =
2037 {
2038 { "<a xmlns=\"defns a\"><b xmlns=\"defns b\"><c xmlns=\"defns c\"/></b></a>",
2039 { "defns a", "defns b", "defns c", "defns b", "defns a" }},
2040 { "<r:a xmlns=\"defns a\" xmlns:r=\"ns r\"/>",
2041 { "ns r" }},
2042 { "<r:a xmlns=\"defns a\" xmlns:r=\"ns r\"><b/></r:a>",
2043 { "ns r", "defns a", "ns r" }},
2044 { "<a xmlns=\"defns a\" xmlns:r=\"ns r\"><r:b/></a>",
2045 { "defns a", "ns r", "defns a" }},
2046 { "<a><b><c/></b></a>",
2047 { "", "", "", "", "" }},
2048 { "<a>text</a>",
2049 { "", "", "" }},
2050 { "<a>\r\n</a>",
2051 { "", "", "" }},
2052 { "<a><![CDATA[data]]></a>",
2053 { "", "", "" }},
2054 { "<?xml version=\"1.0\" ?><a/>",
2055 { "", "" }},
2056 { "<a><?pi ?></a>",
2057 { "", "", "" }},
2058 { "<a><!-- comment --></a>",
2059 { "", "", "" }},
2060 };
2061 IXmlReader *reader;
2062 XmlNodeType type;
2063 unsigned int i;
2064 HRESULT hr;
2065
2066 hr = CreateXmlReader(&IID_IXmlReader, (void**)&reader, NULL);
2067 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2068
2069 for (i = 0; i < sizeof(uri_tests)/sizeof(uri_tests[0]); i++) {
2070 unsigned int j = 0;
2071
2072 set_input_string(reader, uri_tests[i].xml);
2073
2074 type = ~0u;
2075 while (IXmlReader_Read(reader, &type) == S_OK) {
2076 const WCHAR *local, *qname;
2077 UINT length, length2;
2078
2079 ok(type == XmlNodeType_Element ||
2080 type == XmlNodeType_Text ||
2081 type == XmlNodeType_CDATA ||
2082 type == XmlNodeType_ProcessingInstruction ||
2083 type == XmlNodeType_Comment ||
2084 type == XmlNodeType_Whitespace ||
2085 type == XmlNodeType_EndElement ||
2086 type == XmlNodeType_XmlDeclaration, "Unexpected node type %d.\n", type);
2087
2088 local = NULL;
2089 length = 0;
2090 hr = IXmlReader_GetLocalName(reader, &local, &length);
2091 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2092 ok(local != NULL, "Unexpected NULL local name pointer\n");
2093
2094 qname = NULL;
2095 length2 = 0;
2096 hr = IXmlReader_GetQualifiedName(reader, &qname, &length2);
2097 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2098 ok(qname != NULL, "Unexpected NULL qualified name pointer\n");
2099
2100 if (type == XmlNodeType_Element ||
2101 type == XmlNodeType_EndElement ||
2102 type == XmlNodeType_ProcessingInstruction ||
2103 type == XmlNodeType_XmlDeclaration)
2104 {
2105 ok(*local != 0, "Unexpected empty local name\n");
2106 ok(length > 0, "Unexpected local name length\n");
2107
2108 ok(*qname != 0, "Unexpected empty qualified name\n");
2109 ok(length2 > 0, "Unexpected qualified name length\n");
2110 }
2111
2112 reader_namespace(reader, uri_tests[i].uri[j]);
2113
2114 j++;
2115 }
2116 ok(type == XmlNodeType_None, "Unexpected node type %d\n", type);
2117 }
2118
2119 IXmlReader_Release(reader);
2120 }
2121
2122 static void test_read_charref(void)
2123 {
2124 static const char testA[] = "<a b=\"c\">&#x1f3;&#x103;&gt;</a>";
2125 static const WCHAR chardataW[] = {0x01f3,0x0103,'>',0};
2126 const WCHAR *value;
2127 IXmlReader *reader;
2128 XmlNodeType type;
2129 HRESULT hr;
2130
2131 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2132 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2133
2134 set_input_string(reader, testA);
2135
2136 hr = IXmlReader_Read(reader, &type);
2137 ok(hr == S_OK, "got %08x\n", hr);
2138 ok(type == XmlNodeType_Element, "Unexpected node type %d\n", type);
2139
2140 hr = IXmlReader_Read(reader, &type);
2141 ok(hr == S_OK, "got %08x\n", hr);
2142 ok(type == XmlNodeType_Text, "Unexpected node type %d\n", type);
2143
2144 hr = IXmlReader_GetValue(reader, &value, NULL);
2145 ok(hr == S_OK, "got %08x\n", hr);
2146 ok(!lstrcmpW(value, chardataW), "Text value : %s\n", wine_dbgstr_w(value));
2147
2148 hr = IXmlReader_Read(reader, &type);
2149 ok(hr == S_OK, "got %08x\n", hr);
2150 ok(type == XmlNodeType_EndElement, "Unexpected node type %d\n", type);
2151
2152 hr = IXmlReader_Read(reader, &type);
2153 ok(hr == S_FALSE, "got %08x\n", hr);
2154 ok(type == XmlNodeType_None, "Unexpected node type %d\n", type);
2155
2156 IXmlReader_Release(reader);
2157 }
2158
2159 static void test_encoding_detection(void)
2160 {
2161 static const struct encoding_testW
2162 {
2163 WCHAR text[16];
2164 }
2165 encoding_testsW[] =
2166 {
2167 { { '<','?','p','i',' ','?','>',0 } },
2168 { { '<','!','-','-',' ','c','-','-','>',0 } },
2169 { { 0xfeff,'<','a','/','>',0 } },
2170 { { '<','a','/','>',0 } },
2171 };
2172 static const char *encoding_testsA[] =
2173 {
2174 "<?pi ?>",
2175 "<!-- comment -->",
2176 "\xef\xbb\xbf<a/>", /* UTF-8 BOM */
2177 "<a/>",
2178 };
2179 IXmlReader *reader;
2180 XmlNodeType type;
2181 IStream *stream;
2182 unsigned int i;
2183 HRESULT hr;
2184
2185 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2186 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2187
2188 /* there's no way to query detected encoding back, so just verify that document is browsable */
2189
2190 for (i = 0; i < sizeof(encoding_testsA)/sizeof(encoding_testsA[0]); i++)
2191 {
2192 set_input_string(reader, encoding_testsA[i]);
2193
2194 type = XmlNodeType_None;
2195 hr = IXmlReader_Read(reader, &type);
2196 ok(hr == S_OK, "got %08x\n", hr);
2197 ok(type != XmlNodeType_None, "Unexpected node type %d\n", type);
2198 }
2199
2200 for (i = 0; i < sizeof(encoding_testsW)/sizeof(encoding_testsW[0]); i++)
2201 {
2202 stream = create_stream_on_data(encoding_testsW[i].text, lstrlenW(encoding_testsW[i].text) * sizeof(WCHAR));
2203
2204 hr = IXmlReader_SetInput(reader, (IUnknown *)stream);
2205 ok(hr == S_OK, "got %08x\n", hr);
2206
2207 type = XmlNodeType_None;
2208 hr = IXmlReader_Read(reader, &type);
2209 ok(hr == S_OK, "%u: got %08x\n", i, hr);
2210 ok(type != XmlNodeType_None, "%u: unexpected node type %d\n", i, type);
2211
2212 IStream_Release(stream);
2213 }
2214
2215 IXmlReader_Release(reader);
2216 }
2217
2218 static void test_eof_state(IXmlReader *reader, BOOL eof)
2219 {
2220 LONG_PTR state;
2221 HRESULT hr;
2222
2223 ok(IXmlReader_IsEOF(reader) == eof, "Unexpected IsEOF() result\n");
2224 hr = IXmlReader_GetProperty(reader, XmlReaderProperty_ReadState, &state);
2225 ok(hr == S_OK, "GetProperty() failed, %#x\n", hr);
2226 ok((state == XmlReadState_EndOfFile) == eof, "Unexpected EndOfFile state %ld\n", state);
2227 }
2228
2229 static void test_endoffile(void)
2230 {
2231 IXmlReader *reader;
2232 XmlNodeType type;
2233 HRESULT hr;
2234
2235 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2236 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2237
2238 test_eof_state(reader, FALSE);
2239
2240 set_input_string(reader, "<a/>");
2241
2242 test_eof_state(reader, FALSE);
2243
2244 type = XmlNodeType_None;
2245 hr = IXmlReader_Read(reader, &type);
2246 ok(hr == S_OK, "got %#x\n", hr);
2247 ok(type == XmlNodeType_Element, "Unexpected type %d\n", type);
2248
2249 test_eof_state(reader, FALSE);
2250
2251 type = XmlNodeType_Element;
2252 hr = IXmlReader_Read(reader, &type);
2253 ok(hr == S_FALSE, "got %#x\n", hr);
2254 ok(type == XmlNodeType_None, "Unexpected type %d\n", type);
2255
2256 test_eof_state(reader, TRUE);
2257
2258 hr = IXmlReader_SetInput(reader, NULL);
2259 ok(hr == S_OK, "got %08x\n", hr);
2260
2261 test_eof_state(reader, FALSE);
2262
2263 IXmlReader_Release(reader);
2264
2265 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2266 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2267
2268 set_input_string(reader, "<a/>text");
2269
2270 type = XmlNodeType_None;
2271 hr = IXmlReader_Read(reader, &type);
2272 ok(hr == S_OK, "got %#x\n", hr);
2273 ok(type == XmlNodeType_Element, "Unexpected type %d\n", type);
2274
2275 test_eof_state(reader, FALSE);
2276
2277 type = XmlNodeType_Element;
2278 hr = IXmlReader_Read(reader, &type);
2279 ok(hr == WC_E_SYNTAX, "got %#x\n", hr);
2280 ok(type == XmlNodeType_None, "Unexpected type %d\n", type);
2281
2282 test_eof_state(reader, FALSE);
2283
2284 hr = IXmlReader_SetInput(reader, NULL);
2285 ok(hr == S_OK, "got %08x\n", hr);
2286
2287 IXmlReader_Release(reader);
2288 }
2289
2290 static void test_max_element_depth(void)
2291 {
2292 static const char *xml =
2293 "<a>"
2294 "<b attrb=\"_b\">"
2295 "<c>"
2296 "<d></d>"
2297 "</c>"
2298 "</b>"
2299 "</a>";
2300 XmlNodeType nodetype;
2301 unsigned int count;
2302 IXmlReader *reader;
2303 HRESULT hr;
2304
2305 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2306 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2307
2308 set_input_string(reader, xml);
2309
2310 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MaxElementDepth, 2);
2311 ok(hr == S_OK, "got %08x\n", hr);
2312
2313 TEST_DEPTH(reader, 0);
2314
2315 hr = IXmlReader_Read(reader, NULL);
2316 ok(hr == S_OK, "got %08x\n", hr);
2317
2318 TEST_DEPTH(reader, 0);
2319
2320 hr = IXmlReader_Read(reader, NULL);
2321 ok(hr == S_OK, "got %08x\n", hr);
2322
2323 TEST_DEPTH(reader, 1);
2324 TEST_READER_STATE(reader, XmlReadState_Interactive);
2325
2326 hr = IXmlReader_Read(reader, NULL);
2327 ok(hr == SC_E_MAXELEMENTDEPTH, "got %08x\n", hr);
2328
2329 TEST_DEPTH2(reader, 0, 2);
2330 TEST_READER_STATE(reader, XmlReadState_Error);
2331
2332 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MaxElementDepth, 10);
2333 ok(hr == S_OK, "got %08x\n", hr);
2334
2335 hr = IXmlReader_Read(reader, NULL);
2336 ok(hr == SC_E_MAXELEMENTDEPTH, "got %08x\n", hr);
2337
2338 TEST_DEPTH2(reader, 0, 2);
2339 TEST_READER_STATE(reader, XmlReadState_Error);
2340
2341 /* test if stepping into attributes enforces depth limit too */
2342 set_input_string(reader, xml);
2343
2344 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MaxElementDepth, 2);
2345 ok(hr == S_OK, "got %08x\n", hr);
2346
2347 TEST_DEPTH(reader, 0);
2348
2349 hr = IXmlReader_Read(reader, NULL);
2350 ok(hr == S_OK, "got %08x\n", hr);
2351
2352 TEST_DEPTH(reader, 0);
2353
2354 hr = IXmlReader_Read(reader, NULL);
2355 ok(hr == S_OK, "got %08x\n", hr);
2356
2357 TEST_DEPTH(reader, 1);
2358
2359 hr = IXmlReader_MoveToFirstAttribute(reader);
2360 ok(hr == S_OK, "got %08x\n", hr);
2361
2362 TEST_DEPTH(reader, 2);
2363 TEST_READER_STATE(reader, XmlReadState_Interactive);
2364
2365 nodetype = 123;
2366 hr = IXmlReader_Read(reader, &nodetype);
2367 ok(hr == SC_E_MAXELEMENTDEPTH, "got %08x\n", hr);
2368 ok(nodetype == XmlNodeType_None, "got node type %d\n", nodetype);
2369
2370 nodetype = 123;
2371 hr = IXmlReader_Read(reader, &nodetype);
2372 ok(hr == SC_E_MAXELEMENTDEPTH, "got %08x\n", hr);
2373 ok(nodetype == XmlNodeType_None, "got node type %d\n", nodetype);
2374
2375 TEST_DEPTH2(reader, 0, 2);
2376 TEST_READER_STATE(reader, XmlReadState_Error);
2377
2378 /* set max depth to 0, this disables depth limit */
2379 set_input_string(reader, xml);
2380
2381 hr = IXmlReader_SetProperty(reader, XmlReaderProperty_MaxElementDepth, 0);
2382 ok(hr == S_OK, "got %08x\n", hr);
2383
2384 count = 0;
2385 while (IXmlReader_Read(reader, NULL) == S_OK)
2386 count++;
2387 ok(count == 8, "Unexpected node number %u\n", count);
2388 TEST_READER_STATE(reader, XmlReadState_EndOfFile);
2389
2390 IXmlReader_Release(reader);
2391 }
2392
2393 static void test_reader_position(void)
2394 {
2395 static const char *xml = "<c:a xmlns:c=\"nsdef c\" b=\"attr b\">\n</c:a>";
2396 IXmlReader *reader;
2397 XmlNodeType type;
2398 UINT position;
2399 HRESULT hr;
2400
2401 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2402 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2403
2404 TEST_READER_STATE(reader, XmlReadState_Closed);
2405
2406 /* position methods with Null args */
2407 hr = IXmlReader_GetLineNumber(reader, NULL);
2408 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
2409
2410 hr = IXmlReader_GetLinePosition(reader, NULL);
2411 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
2412
2413 position = 123;
2414 hr = IXmlReader_GetLinePosition(reader, &position);
2415 ok(hr == S_FALSE, "got %#x\n", hr);
2416 ok(position == 0, "got %u\n", position);
2417
2418 position = 123;
2419 hr = IXmlReader_GetLineNumber(reader, &position);
2420 ok(hr == S_FALSE, "got %#x\n", hr);
2421 ok(position == 0, "got %u\n", position);
2422
2423 set_input_string(reader, xml);
2424
2425 TEST_READER_STATE(reader, XmlReadState_Initial);
2426 TEST_READER_POSITION(reader, 0, 0);
2427 hr = IXmlReader_Read(reader, &type);
2428 ok(hr == S_OK, "got %08x\n", hr);
2429 ok(type == XmlNodeType_Element, "got type %d\n", type);
2430 TEST_READER_POSITION2(reader, 1, 2, ~0u, 34);
2431
2432 next_attribute(reader);
2433 TEST_READER_POSITION2(reader, 1, 6, ~0u, 34);
2434
2435 next_attribute(reader);
2436 TEST_READER_POSITION2(reader, 1, 24, ~0u, 34);
2437
2438 move_to_element(reader);
2439 TEST_READER_POSITION2(reader, 1, 2, ~0u, 34);
2440
2441 hr = IXmlReader_Read(reader, &type);
2442 ok(hr == S_OK, "got %08x\n", hr);
2443 ok(type == XmlNodeType_Whitespace, "got type %d\n", type);
2444 TEST_READER_POSITION2(reader, 1, 35, 2, 6);
2445
2446 hr = IXmlReader_Read(reader, &type);
2447 ok(hr == S_OK, "got %08x\n", hr);
2448 ok(type == XmlNodeType_EndElement, "got type %d\n", type);
2449 TEST_READER_POSITION2(reader, 2, 3, 2, 6);
2450
2451 hr = IXmlReader_SetInput(reader, NULL);
2452 ok(hr == S_OK, "got %08x\n", hr);
2453 TEST_READER_STATE2(reader, XmlReadState_Initial, XmlReadState_Closed);
2454 TEST_READER_POSITION(reader, 0, 0);
2455
2456 IXmlReader_Release(reader);
2457 }
2458
2459 static void test_string_pointers(void)
2460 {
2461 const WCHAR *ns, *nsq, *empty, *xmlns_ns, *xmlns_name, *name, *p, *q, *xml, *ptr, *value;
2462 IXmlReader *reader;
2463 HRESULT hr;
2464
2465 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2466 ok(hr == S_OK, "S_OK, got %08x\n", hr);
2467
2468 set_input_string(reader, "<elem xmlns=\"myns\">myns<elem2 /></elem>");
2469
2470 read_node(reader, XmlNodeType_Element);
2471 empty = reader_value(reader, "");
2472 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2473 name = reader_name(reader, "elem");
2474 ok(name == reader_qname(reader, "elem"), "name != qname\n");
2475 ns = reader_namespace(reader, "myns");
2476
2477 next_attribute(reader);
2478 ptr = reader_value(reader, "myns");
2479 if (ns != ptr)
2480 {
2481 win_skip("attr value is different than namespace pointer, assuming old xmllite\n");
2482 IXmlReader_Release(reader);
2483 return;
2484 }
2485 ok(ns == ptr, "ns != value\n");
2486 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2487 xmlns_ns = reader_namespace(reader, "http://www.w3.org/2000/xmlns/");
2488 xmlns_name = reader_name(reader, "xmlns");
2489 ok(xmlns_name == reader_qname(reader, "xmlns"), "xmlns_name != qname\n");
2490
2491 read_node(reader, XmlNodeType_Text);
2492 ok(ns != reader_value(reader, "myns"), "ns == value\n");
2493 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2494 ok(empty == reader_namespace(reader, ""), "empty != namespace\n");
2495 ok(empty == reader_name(reader, ""), "empty != name\n");
2496 ok(empty == reader_qname(reader, ""), "empty != qname\n");
2497
2498 read_node(reader, XmlNodeType_Element);
2499 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2500 ok(ns == reader_namespace(reader, "myns"), "empty != namespace\n");
2501
2502 read_node(reader, XmlNodeType_EndElement);
2503 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2504 ok(name == reader_name(reader, "elem"), "empty != name\n");
2505 ok(name == reader_qname(reader, "elem"), "empty != qname\n");
2506 ok(ns == reader_namespace(reader, "myns"), "empty != namespace\n");
2507
2508 set_input_string(reader, "<elem xmlns:p=\"myns\" xmlns:q=\"mynsq\"><p:elem2 q:attr=\"\"></p:elem2></elem>");
2509
2510 read_node(reader, XmlNodeType_Element);
2511 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2512 name = reader_name(reader, "elem");
2513 ok(empty == reader_namespace(reader, ""), "empty != namespace\n");
2514
2515 next_attribute(reader);
2516 ns = reader_value(reader, "myns");
2517 ok(xmlns_name == reader_prefix(reader, "xmlns"), "xmlns_name != prefix\n");
2518 p = reader_name(reader, "p");
2519 ok(xmlns_ns == reader_namespace(reader, "http://www.w3.org/2000/xmlns/"), "xmlns_ns != namespace\n");
2520
2521 next_attribute(reader);
2522 nsq = reader_value(reader, "mynsq");
2523 ok(xmlns_name == reader_prefix(reader, "xmlns"), "xmlns_name != prefix\n");
2524 q = reader_name(reader, "q");
2525 ok(xmlns_ns == reader_namespace(reader, "http://www.w3.org/2000/xmlns/"), "xmlns_ns != namespace\n");
2526
2527 read_node(reader, XmlNodeType_Element);
2528 ok(p == reader_prefix(reader, "p"), "p != prefix\n");
2529 ok(ns == reader_namespace(reader, "myns"), "empty != namespace\n");
2530 name = reader_qname(reader, "p:elem2");
2531
2532 next_attribute(reader);
2533 ok(empty != reader_value(reader, ""), "empty == value\n");
2534 ok(q == reader_prefix(reader, "q"), "q != prefix\n");
2535 ok(nsq == reader_namespace(reader, "mynsq"), "nsq != namespace\n");
2536
2537 read_node(reader, XmlNodeType_EndElement);
2538 ptr = reader_qname(reader, "p:elem2"); todo_wine ok(name != ptr, "q == qname\n");
2539
2540 set_input_string(reader, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
2541
2542 read_node(reader, XmlNodeType_XmlDeclaration);
2543 ok(empty == reader_value(reader, ""), "empty != value\n");
2544 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2545 xml = reader_name(reader, "xml");
2546 ptr = reader_qname(reader, "xml"); todo_wine ok(xml == ptr, "xml != qname\n");
2547 ok(empty == reader_namespace(reader, ""), "empty != namespace\n");
2548
2549 next_attribute(reader);
2550 ok(empty == reader_prefix(reader, ""), "empty != prefix\n");
2551 ok(empty == reader_namespace(reader, ""), "empty != namespace\n");
2552
2553 set_input_string(reader, "<elem xmlns:p=\"myns\"><p:elem2 attr=\"\" /></elem>");
2554
2555 read_node(reader, XmlNodeType_Element);
2556 next_attribute(reader);
2557 read_value_char(reader, 'm');
2558 p = reader_value(reader, "yns");
2559
2560 read_node(reader, XmlNodeType_Element);
2561 ns = reader_namespace(reader, "myns");
2562 ok(ns+1 == p, "ns+1 != p\n");
2563
2564 set_input_string(reader, "<elem attr=\"value\"></elem>");
2565
2566 read_node(reader, XmlNodeType_Element);
2567 next_attribute(reader);
2568 name = reader_name(reader, "attr");
2569 value = reader_value(reader, "value");
2570
2571 move_to_element(reader);
2572 next_attribute(reader);
2573 ok(name == reader_name(reader, "attr"), "attr pointer changed\n");
2574 ok(value == reader_value(reader, "value"), "value pointer changed\n");
2575
2576 IXmlReader_Release(reader);
2577 }
2578
2579 static void test_attribute_by_name(void)
2580 {
2581 static const char *xml = "<a><elem xmlns=\"myns\" a=\"value a\" b=\"value b\" xmlns:ns=\"ns uri\" "
2582 "ns:c=\"value c\" c=\"value c2\"/></a>";
2583 static const WCHAR xmlns_uriW[] = {'h','t','t','p',':','/','/','w','w','w','.','w','3','.','o','r','g','/',
2584 '2','0','0','0','/','x','m','l','n','s','/',0};
2585 static const WCHAR nsuriW[] = {'n','s',' ','u','r','i',0};
2586 static const WCHAR xmlnsW[] = {'x','m','l','n','s',0};
2587 static const WCHAR mynsW[] = {'m','y','n','s',0};
2588 static const WCHAR nsW[] = {'n','s',0};
2589 static const WCHAR emptyW[] = {0};
2590 static const WCHAR aW[] = {'a',0};
2591 static const WCHAR bW[] = {'b',0};
2592 static const WCHAR cW[] = {'c',0};
2593 IXmlReader *reader;
2594 HRESULT hr;
2595
2596 hr = CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL);
2597 ok(hr == S_OK, "Failed to create reader, hr %#x.\n", hr);
2598
2599 set_input_string(reader, xml);
2600
2601 hr = IXmlReader_MoveToAttributeByName(reader, NULL, NULL);
2602 ok(hr == E_INVALIDARG || broken(hr == S_FALSE) /* WinXP */, "Unexpected hr %#x.\n", hr);
2603
2604 hr = IXmlReader_MoveToAttributeByName(reader, emptyW, NULL);
2605 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2606
2607 read_node(reader, XmlNodeType_Element);
2608
2609 hr = IXmlReader_MoveToAttributeByName(reader, emptyW, NULL);
2610 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2611
2612 read_node(reader, XmlNodeType_Element);
2613
2614 hr = IXmlReader_MoveToAttributeByName(reader, NULL, NULL);
2615 ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);
2616
2617 hr = IXmlReader_MoveToAttributeByName(reader, NULL, xmlns_uriW);
2618 ok(hr == E_INVALIDARG, "Unexpected hr %#x.\n", hr);
2619
2620 hr = IXmlReader_MoveToAttributeByName(reader, emptyW, xmlns_uriW);
2621 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2622
2623 hr = IXmlReader_MoveToAttributeByName(reader, xmlnsW, NULL);
2624 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2625
2626 hr = IXmlReader_MoveToAttributeByName(reader, xmlnsW, xmlns_uriW);
2627 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2628 reader_value(reader, "myns");
2629
2630 hr = IXmlReader_MoveToAttributeByName(reader, aW, NULL);
2631 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2632 reader_value(reader, "value a");
2633
2634 hr = IXmlReader_MoveToAttributeByName(reader, bW, NULL);
2635 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2636 reader_value(reader, "value b");
2637
2638 hr = IXmlReader_MoveToAttributeByName(reader, aW, mynsW);
2639 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2640
2641 hr = IXmlReader_MoveToAttributeByName(reader, nsW, NULL);
2642 ok(hr == S_FALSE, "Unexpected hr %#x.\n", hr);
2643
2644 hr = IXmlReader_MoveToAttributeByName(reader, nsW, xmlns_uriW);
2645 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2646 reader_value(reader, "ns uri");
2647
2648 hr = IXmlReader_MoveToAttributeByName(reader, bW, emptyW);
2649 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2650 reader_value(reader, "value b");
2651
2652 hr = IXmlReader_MoveToAttributeByName(reader, cW, NULL);
2653 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2654 reader_value(reader, "value c2");
2655
2656 hr = IXmlReader_MoveToAttributeByName(reader, cW, nsuriW);
2657 ok(hr == S_OK, "Unexpected hr %#x.\n", hr);
2658 reader_value(reader, "value c");
2659
2660 IXmlReader_Release(reader);
2661 }
2662
2663 START_TEST(reader)
2664 {
2665 test_reader_create();
2666 test_readerinput();
2667 test_reader_state();
2668 test_read_attribute();
2669 test_read_cdata();
2670 test_read_comment();
2671 test_read_pi();
2672 test_read_system_dtd();
2673 test_read_public_dtd();
2674 test_read_element();
2675 test_isemptyelement();
2676 test_read_text();
2677 test_read_full();
2678 test_read_pending();
2679 test_readvaluechunk();
2680 test_read_xmldeclaration();
2681 test_reader_properties();
2682 test_prefix();
2683 test_namespaceuri();
2684 test_read_charref();
2685 test_encoding_detection();
2686 test_endoffile();
2687 test_max_element_depth();
2688 test_reader_position();
2689 test_string_pointers();
2690 test_attribute_by_name();
2691 }