d2d99fc23f113bd9fb2cfdfc7dad11fdc62977c0
[reactos.git] / rostests / winetests / msxml3 / xmldoc.c
1 /*
2 * IXMLDocument tests
3 *
4 * Copyright 2007 James Hawkins
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 COBJMACROS
22
23 #include <stdio.h>
24 #include "windows.h"
25 #include "ole2.h"
26 #include "msxml2.h"
27 #include "ocidl.h"
28
29 #include "wine/test.h"
30
31 /* Deprecated Error Code */
32 #define XML_E_INVALIDATROOTLEVEL 0xc00ce556
33
34 static void create_xml_file(LPCSTR filename)
35 {
36 DWORD dwNumberOfBytesWritten;
37 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
38 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
39
40 static const char data[] =
41 "<?xml version=\"1.0\" ?>\n"
42 "<BankAccount>\n"
43 " <Number>1234</Number>\n"
44 " <Name>Captain Ahab</Name>\n"
45 "</BankAccount>";
46
47 WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
48 CloseHandle(hf);
49 }
50
51 static void test_xmldoc(void)
52 {
53 HRESULT hr;
54 IXMLDocument *doc = NULL;
55 IXMLElement *element = NULL, *child = NULL, *value = NULL;
56 IXMLElementCollection *collection = NULL, *inner = NULL;
57 IPersistStreamInit *psi = NULL;
58 IStream *stream = NULL;
59 HGLOBAL hglobal;
60 HANDLE hfile;
61 LPVOID ptr;
62 DWORD file_size, read;
63 CHAR path[MAX_PATH];
64 LONG type, num_child;
65 VARIANT vIndex, vName;
66 BSTR name = NULL;
67
68 static const WCHAR szBankAccount[] = {'B','A','N','K','A','C','C','O','U','N','T',0};
69 static const WCHAR szNumber[] = {'N','U','M','B','E','R',0};
70 static const WCHAR szNumVal[] = {'1','2','3','4',0};
71 static const WCHAR szName[] = {'N','A','M','E',0};
72 static const WCHAR szNameVal[] = {'C','a','p','t','a','i','n',' ','A','h','a','b',0};
73
74 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
75 &IID_IXMLDocument, (LPVOID*)&doc);
76 if (FAILED(hr))
77 {
78 skip("Failed to create XMLDocument instance\n");
79 return;
80 }
81
82 create_xml_file("bank.xml");
83 GetFullPathNameA("bank.xml", MAX_PATH, path, NULL);
84
85 hfile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
86 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
87 ok(hfile != INVALID_HANDLE_VALUE, "Expected a valid file handle\n");
88 file_size = GetFileSize(hfile, NULL);
89
90 hglobal = GlobalAlloc(GHND, file_size);
91 ptr = GlobalLock(hglobal);
92
93 ReadFile(hfile, ptr, file_size, &read, NULL);
94 ok(file_size == read, "Expected to read the whole file, read %d\n", read);
95
96 hr = CreateStreamOnHGlobal(hglobal, TRUE, &stream);
97 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
98 ok(stream != NULL, "Expected non-NULL stream\n");
99
100 CloseHandle(hfile);
101 GlobalUnlock(hglobal);
102
103 hr = IXMLDocument_QueryInterface(doc, &IID_IPersistStreamInit, (LPVOID *)&psi);
104 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
105 ok(psi != NULL, "Expected non-NULL psi\n");
106
107 hr = IXMLDocument_get_root(doc, &element);
108 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
109 ok(element == NULL, "Expected NULL element\n");
110
111 hr = IPersistStreamInit_Load(psi, stream);
112 ok(hr == S_OK || hr == XML_E_INVALIDATROOTLEVEL, "Expected S_OK, got %08x\n", hr);
113 if(hr == XML_E_INVALIDATROOTLEVEL)
114 goto cleanup;
115
116 ok(stream != NULL, "Expected non-NULL stream\n");
117
118 hr = IXMLDocument_get_root(doc, &element);
119 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
120 ok(element != NULL, "Expected non-NULL element\n");
121
122 hr = IXMLElement_get_type(element, &type);
123 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
124 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
125
126 hr = IXMLElement_get_tagName(element, &name);
127 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
128 ok(!lstrcmpW(name, szBankAccount), "Expected BANKACCOUNT\n");
129 SysFreeString(name);
130
131 hr = IXMLElement_get_children(element, &collection);
132 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
133 ok(collection != NULL, "Expected non-NULL collection\n");
134
135 hr = IXMLElementCollection_get_length(collection, &num_child);
136 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
137 ok(num_child == 2, "Expected 2, got %d\n", num_child);
138
139 V_VT(&vIndex) = VT_I4;
140 V_I4(&vIndex) = 0;
141 V_VT(&vName) = VT_ERROR;
142 V_ERROR(&vName) = DISP_E_PARAMNOTFOUND;
143 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
144 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
145 ok(child != NULL, "Expected non-NULL child\n");
146
147 hr = IXMLElement_get_type(child, &type);
148 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
149 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
150
151 hr = IXMLElement_get_tagName(child, &name);
152 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
153 ok(!lstrcmpW(name, szNumber), "Expected NUMBER\n");
154 SysFreeString(name);
155
156 hr = IXMLElement_get_children(child, &inner);
157 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
158 ok(inner != NULL, "Expected non-NULL inner\n");
159
160 hr = IXMLElementCollection_get_length(inner, &num_child);
161 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
162 ok(num_child == 1, "Expected 1, got %d\n", num_child);
163
164 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
165 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
166 ok(value != NULL, "Expected non-NULL value\n");
167
168 hr = IXMLElement_get_type(value, &type);
169 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
170 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
171
172 hr = IXMLElement_get_text(value, &name);
173 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
174 ok(!lstrcmpW(name, szNumVal), "Expected '1234'\n");
175 SysFreeString(name);
176
177 IXMLElementCollection_Release(inner);
178
179 inner = (IXMLElementCollection *)0xdeadbeef;
180 hr = IXMLElement_get_children(value, &inner);
181 ok(hr == 1, "Expected 1, got %08x\n", hr);
182 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
183
184 IXMLElement_Release(value);
185 IXMLElement_Release(child);
186 value = NULL;
187 child = NULL;
188
189 V_I4(&vIndex) = 1;
190 hr = IXMLElementCollection_item(collection, vIndex, vName, (IDispatch **)&child);
191 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
192 ok(child != NULL, "Expected non-NULL child\n");
193
194 hr = IXMLElement_get_type(child, &type);
195 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
196 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
197
198 hr = IXMLElement_get_tagName(child, &name);
199 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
200 ok(!lstrcmpW(name, szName), "Expected NAME\n");
201 SysFreeString(name);
202
203 hr = IXMLElement_get_children(child, &inner);
204 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
205 ok(inner != NULL, "Expected non-NULL inner\n");
206
207 hr = IXMLElementCollection_get_length(inner, &num_child);
208 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
209 ok(num_child == 1, "Expected 1, got %d\n", num_child);
210
211 V_I4(&vIndex) = 0;
212 hr = IXMLElementCollection_item(inner, vIndex, vName, (IDispatch **)&value);
213 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
214 ok(value != NULL, "Expected non-NULL value\n");
215
216 hr = IXMLElement_get_type(value, &type);
217 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
218 ok(type == XMLELEMTYPE_TEXT, "Expected XMLELEMTYPE_TEXT, got %d\n", type);
219
220 hr = IXMLElement_get_text(value, &name);
221 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
222 ok(!lstrcmpW(name, szNameVal), "Expected 'Captain Ahab'\n");
223 SysFreeString(name);
224
225 IXMLElementCollection_Release(inner);
226
227 inner = (IXMLElementCollection *)0xdeadbeef;
228 hr = IXMLElement_get_children(value, &inner);
229 ok(hr == 1, "Expected 1, got %08x\n", hr);
230 ok(inner == NULL, "Expected NULL inner, got %p\n", inner);
231
232 IXMLElement_Release(value);
233 IXMLElement_Release(child);
234 IXMLElementCollection_Release(collection);
235 IXMLElement_Release(element);
236 cleanup:
237 IStream_Release(stream);
238 IPersistStreamInit_Release(psi);
239 IXMLDocument_Release(doc);
240
241 DeleteFileA("bank.xml");
242 }
243
244 static void test_createElement(void)
245 {
246 HRESULT hr;
247 IXMLDocument *doc = NULL;
248 IXMLElement *element = NULL, *root = NULL;
249 VARIANT vType, vName;
250 LONG type;
251
252 hr = CoCreateInstance(&CLSID_XMLDocument, NULL, CLSCTX_INPROC_SERVER,
253 &IID_IXMLDocument, (LPVOID*)&doc);
254 if (FAILED(hr))
255 {
256 skip("Failed to create XMLDocument instance\n");
257 return;
258 }
259
260 /* invalid vType type */
261 V_VT(&vType) = VT_NULL;
262 V_VT(&vName) = VT_NULL;
263 element = (IXMLElement *)0xdeadbeef;
264 hr = IXMLDocument_createElement(doc, vType, vName, &element);
265 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
266 ok(element == NULL, "Expected NULL element\n");
267
268 /* invalid vType value */
269 V_VT(&vType) = VT_I4;
270 V_I4(&vType) = -1;
271 V_VT(&vName) = VT_NULL;
272 hr = IXMLDocument_createElement(doc, vType, vName, &element);
273 /* Up to and including SP7, createElement returns an element. */
274 if(hr == S_OK)
275 {
276 ok(element != NULL, "Expected element\n");
277 if (element != NULL)
278 {
279 hr = IXMLElement_get_type(element, &type);
280 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
281 /* SP7 returns an XMLELEMTYPE_ELEMENT */
282 ok(type == XMLELEMTYPE_OTHER || type == XMLELEMTYPE_ELEMENT,
283 "Expected XMLELEMTYPE_OTHER || XMLELEMTYPE_ELEMENT, got %d\n", type);
284
285 IXMLElement_Release(element);
286 }
287 }
288 else
289 {
290 ok(hr == E_NOTIMPL, "Expected E_NOTIMPL, got %08x\n", hr);
291 ok(element == NULL, "Expected NULL element\n");
292 }
293
294 /* invalid vName type */
295 V_VT(&vType) = VT_I4;
296 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
297 V_VT(&vName) = VT_I4;
298 hr = IXMLDocument_createElement(doc, vType, vName, &element);
299 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
300 ok(element != NULL, "Expected non-NULL element\n");
301
302 hr = IXMLElement_get_type(element, &type);
303 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
304 ok(type == XMLELEMTYPE_ELEMENT, "Expected XMLELEMTYPE_ELEMENT, got %d\n", type);
305
306 IXMLElement_Release(element);
307
308 /* NULL element */
309 V_VT(&vType) = VT_I4;
310 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
311 V_VT(&vName) = VT_I4;
312 hr = IXMLDocument_createElement(doc, vType, vName, NULL);
313 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %08x\n", hr);
314
315 root = (IXMLElement *)0xdeadbeef;
316 hr = IXMLDocument_get_root(doc, &root);
317 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
318 ok(root == NULL, "Expected NULL root\n");
319
320 V_VT(&vType) = VT_I4;
321 V_I4(&vType) = XMLELEMTYPE_ELEMENT;
322 V_VT(&vName) = VT_NULL;
323 hr = IXMLDocument_createElement(doc, vType, vName, &element);
324 ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
325 ok(element != NULL, "Expected non-NULL element\n");
326
327 /* createElement does not set the new element as root */
328 root = (IXMLElement *)0xdeadbeef;
329 hr = IXMLDocument_get_root(doc, &root);
330 ok(hr == E_FAIL, "Expected E_FAIL, got %08x\n", hr);
331 ok(root == NULL, "Expected NULL root\n");
332
333 IXMLElement_Release(element);
334 IXMLDocument_Release(doc);
335 }
336
337 START_TEST(xmldoc)
338 {
339 HRESULT hr;
340
341 hr = CoInitialize(NULL);
342 ok(hr == S_OK, "failed to init com\n");
343
344 test_xmldoc();
345 test_createElement();
346
347 CoUninitialize();
348 }