Release 1.5.29.
[wine] / dlls / d3dxof / tests / d3dxof.c
1 /*
2  * Some unit tests for d3dxof
3  *
4  * Copyright (C) 2008 Christian Costa
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 #define COBJMACROS
21
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "initguid.h"
26 #include "dxfile.h"
27
28 static inline void debugstr_guid( char* buf, CONST GUID *id )
29 {
30     sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
31             id->Data1, id->Data2, id->Data3,
32             id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
33             id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
34 }
35
36 static HMODULE hd3dxof;
37 static HRESULT (WINAPI *pDirectXFileCreate)(LPDIRECTXFILE*);
38
39 static char template[] =
40 "xof 0302txt 0064\n"
41 "template Header\n"
42 "{\n"
43 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
44 "WORD major;\n"
45 "WORD minor;\n"
46 "DWORD flags;\n"
47 "}\n";
48
49 /* Same version as above compressed with mszip */
50 static char compressed_template[] =
51 "xof 0302tzip0064\x71\x00\x00\x00\x61\x00\x5a\x00"
52 "\x43\x4B\x2B\x49\xCD\x2D\xC8\x49\x2C\x49\x55\xF0\x48\x4D\x4C\x49"
53 "\x2D\xE2\xAA\xE6\xB2\x31\x76\xB1\x30\x72\x74\x32\x31\xD6\x35\x33"
54 "\x72\x71\xD4\x35\x34\x74\x76\xD3\x75\x74\x32\xB6\xD4\x35\x30\x30"
55 "\x32\x70\x74\x33\x37\x74\x35\x31\x36\xB6\xE3\x0A\xF7\x0F\x72\x51"
56 "\xC8\x4D\xCC\xCA\x2F\xB2\x86\xB2\x33\xF3\x40\x6C\x17\x30\x27\x2D"
57 "\x27\x31\xBD\xD8\x9A\xAB\x96\x8B\x0B\x00";
58
59 static char object[] =
60 "xof 0302txt 0064\n"
61 "Header Object\n"
62 "{\n"
63 "1; 2; 3;\n"
64 "}\n";
65
66 /* Same version as above compressed with mszip */
67 static char compressed_object[] =
68 "xof 0302tzip0064\x2c\x00\x00\x00\x1c\x00\x20\x00"
69 "\x43\x4b\xf3\x48\x4d\x4c\x49\x2d\x52\xf0\x4f\xca\x4a\x4d\x2e\xe1"
70 "\xaa\xe6\x32\xb4\x56\x30\xb2\x56\x30\xb6\xe6\xaa\xe5\xe2\x02\x00";
71
72 static char empty_txt_file[]  = "xof 0302txt 0064";
73 static char empty_bin_file[]  = "xof 0302bin 0064";
74 /* MSZip data is generated with the command "MAKECAB.EXE /D Compress=ON /D CompressionType=MSZip file packed"
75  * Data in cab is after the filename (null terminated) and the 32-bit checksum:
76  * size (16-bit), packed_size (16-bit) and compressed data (with leading 16-bit CK signature)
77  * for each MSZIP chunk whose decompressed size cannot exceed 32768 bytes
78  * Data in x files is preceded by the size (32-bit) of the decompressed file including the xof header (16 bytes)
79  * It does not seem possible to generate an MSZip data chunk with no byte, so put just 1 byte here */
80 /* "\n" packed with MSZip => no text */
81 static char empty_tzip_file[] = "xof 0302tzip0064\x11\x00\x00\x00\x01\x00\x05\x00\x43\x4b\xe3\x02\x00";
82 /* "\n" packed with MSZip => no token (token are 16-bit and there is only 1 byte) */
83 static char empty_bzip_file[] = "xof 0302bzip0064\x11\x00\x00\x00\x01\x00\x05\x00\x43\x4b\xe3\x02\x00";
84 static char empty_cmp_file[]  = "xof 0302cmp 0064";
85 static char empty_xxxx_file[] = "xof 0302xxxx0064";
86
87 static char object_noname[] =
88 "xof 0302txt 0064\n"
89 "Header\n"
90 "{\n"
91 "1; 2; 3;\n"
92 "}\n";
93
94 static char template_syntax_empty_array[] =
95 "xof 0302txt 0064\n"
96 "template Buffer\n"
97 "{\n"
98 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
99 "DWORD num_elem;\n"
100 "array DWORD value[num_elem];\n"
101 "DWORD dummy;\n"
102 "}\n";
103
104 static char object_syntax_empty_array_semicolon[] =
105 "xof 0302txt 0064\n"
106 "Buffer\n"
107 "{\n"
108 "0;\n"
109 ";\n"
110 "1234;\n"
111 "}\n";
112
113 static char object_syntax_empty_array_nosemicolon[] =
114 "xof 0302txt 0064\n"
115 "Buffer\n"
116 "{\n"
117 "0;\n"
118 "1234;\n"
119 "}\n";
120
121 static char template_syntax_string[] =
122 "xof 0302txt 0064\n"
123 "template Filename\n"
124 "{\n"
125 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>\n"
126 "STRING filename;\n"
127 "}\n";
128
129 static char object_syntax_string_normal[] =
130 "xof 0302txt 0064\n"
131 "Filename\n"
132 "{\n"
133 "\"foobar\";\n"
134 "}\n";
135
136 static char object_syntax_string_with_separator[] =
137 "xof 0302txt 0064\n"
138 "Filename\n"
139 "{\n"
140 "\"foo;bar\";\n"
141 "}\n";
142
143 static void init_function_pointers(void)
144 {
145     /* We have to use LoadLibrary as no d3dxof functions are referenced directly */
146     hd3dxof = LoadLibraryA("d3dxof.dll");
147
148     pDirectXFileCreate = (void *)GetProcAddress(hd3dxof, "DirectXFileCreate");
149 }
150
151 static ULONG getRefcount(IUnknown *iface)
152 {
153     IUnknown_AddRef(iface);
154     return IUnknown_Release(iface);
155 }
156
157 static void test_refcount(void)
158 {
159     HRESULT hr;
160     ULONG ref;
161     LPDIRECTXFILE lpDirectXFile = NULL;
162     LPDIRECTXFILEENUMOBJECT lpdxfeo;
163     LPDIRECTXFILEDATA lpdxfd;
164     DXFILELOADMEMORY dxflm;
165
166     if (!pDirectXFileCreate)
167     {
168         win_skip("DirectXFileCreate is not available\n");
169         return;
170     }
171
172     hr = pDirectXFileCreate(&lpDirectXFile);
173     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
174     if (!lpDirectXFile)
175     {
176         skip("Couldn't create DirectXFile interface\n");
177         return;
178     }
179
180     ref = getRefcount( (IUnknown *) lpDirectXFile);
181     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
182     ref = IDirectXFile_AddRef(lpDirectXFile);
183     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
184     ref = IDirectXFile_Release(lpDirectXFile);
185     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
186
187     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
188     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
189
190     dxflm.lpMemory = &object;
191     dxflm.dSize = sizeof(object) - 1;
192     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
193     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
194     ref = getRefcount( (IUnknown *) lpDirectXFile);
195     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
196     ref = getRefcount( (IUnknown *) lpdxfeo);
197     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
198
199     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
200     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
201     ref = getRefcount( (IUnknown *) lpDirectXFile);
202     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
203     ref = getRefcount( (IUnknown *) lpdxfeo);
204     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
205     /* Enum object gets references to all top level objects */
206     ref = getRefcount( (IUnknown *) lpdxfd);
207     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
208
209     ref = IDirectXFile_Release(lpDirectXFile);
210     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
211     /* Nothing changes for all other objects */
212     ref = getRefcount( (IUnknown *) lpdxfeo);
213     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
214     ref = getRefcount( (IUnknown *) lpdxfd);
215     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
216
217     ref = IDirectXFileEnumObject_Release(lpdxfeo);
218     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
219     /* Enum object releases references to all top level objects */
220     ref = getRefcount( (IUnknown *) lpdxfd);
221     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
222
223     ref = IDirectXFileData_Release(lpdxfd);
224     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
225 }
226
227 static void test_CreateEnumObject(void)
228 {
229     HRESULT hr;
230     ULONG ref;
231     LPDIRECTXFILE lpDirectXFile = NULL;
232     LPDIRECTXFILEENUMOBJECT lpdxfeo;
233     LPDIRECTXFILEDATA lpdxfd;
234     DXFILELOADMEMORY dxflm;
235     BYTE* pdata;
236     DWORD size;
237
238     if (!pDirectXFileCreate)
239     {
240         win_skip("DirectXFileCreate is not available\n");
241         return;
242     }
243
244     hr = pDirectXFileCreate(&lpDirectXFile);
245     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
246     if (!lpDirectXFile)
247     {
248         skip("Couldn't create DirectXFile interface\n");
249         return;
250     }
251
252     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
253     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
254
255     dxflm.lpMemory = &object;
256     dxflm.dSize = sizeof(object) - 1;
257     /* Check that only lowest 4 bits are relevant in DXFILELOADOPTIONS */
258     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, 0xFFFFFFF0 + DXFILELOAD_FROMMEMORY, &lpdxfeo);
259     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
260
261     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
262     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
263
264     /* Get all data (szMember == NULL) */
265     hr = IDirectXFileData_GetData(lpdxfd, NULL, &size, (void**)&pdata);
266     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
267
268     ok(size == 8, "Retrieved data size is wrong (%u instead of 8)\n", size);
269     ok((*((WORD*)pdata) == 1) && (*((WORD*)(pdata+2)) == 2) && (*((DWORD*)(pdata+4)) == 3), "Retrieved data is wrong\n");
270
271     /* Get only "major" member (szMember == "major") */
272     hr = IDirectXFileData_GetData(lpdxfd, "major", &size, (void**)&pdata);
273     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
274
275     ok(size == 2, "Retrieved data size is wrong (%u instead of 2)\n", size);
276     ok(*((WORD*)pdata) == 1, "Retrieved data is wrong (%u instead of 1)\n", *((WORD*)pdata));
277
278     /* Get only "minor" member (szMember == "minor") */
279     hr = IDirectXFileData_GetData(lpdxfd, "minor", &size, (void**)&pdata);
280     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
281
282     ok(size == 2, "Retrieved data size is wrong (%u instead of 2)\n", size);
283     ok(*((WORD*)pdata) == 2, "Retrieved data is wrong (%u instead of 2)\n", *((WORD*)pdata));
284
285     /* Get only "flags" member (szMember == "flags") */
286     hr = IDirectXFileData_GetData(lpdxfd, "flags", &size, (void**)&pdata);
287     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
288
289     ok(size == 4, "Retrieved data size is wrong (%u instead of 4)\n", size);
290     ok(*((WORD*)pdata) == 3, "Retrieved data is wrong (%u instead of 3)\n", *((WORD*)pdata));
291
292     /* Try to get not existing member (szMember == "unknown") */
293     hr = IDirectXFileData_GetData(lpdxfd, "unknow", &size, (void**)&pdata);
294     ok(hr == DXFILEERR_BADDATAREFERENCE, "IDirectXFileData_GetData: %x\n", hr);
295
296     ref = IDirectXFileEnumObject_Release(lpdxfeo);
297     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
298
299     ref = IDirectXFile_Release(lpDirectXFile);
300     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
301
302     ref = IDirectXFileData_Release(lpdxfd);
303     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
304 }
305
306 static void test_file_types(void)
307 {
308     HRESULT hr;
309     LPDIRECTXFILE dxfile = NULL;
310     LPDIRECTXFILEENUMOBJECT enum_object;
311     DXFILELOADMEMORY lminfo;
312
313     if (!pDirectXFileCreate)
314     {
315         win_skip("DirectXFileCreate is not available\n");
316         return;
317     }
318
319     hr = pDirectXFileCreate(&dxfile);
320     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
321     if (!dxfile)
322     {
323         skip("Couldn't create DirectXFile interface\n");
324         return;
325     }
326
327     hr = IDirectXFile_RegisterTemplates(dxfile, empty_txt_file, sizeof(empty_txt_file) - 1);
328     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
329
330     hr = IDirectXFile_RegisterTemplates(dxfile, empty_bin_file, sizeof(empty_bin_file) - 1);
331     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
332
333     hr = IDirectXFile_RegisterTemplates(dxfile, empty_tzip_file, sizeof(empty_tzip_file) - 1);
334     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
335
336     hr = IDirectXFile_RegisterTemplates(dxfile, empty_bzip_file, sizeof(empty_bzip_file) - 1);
337     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
338
339     hr = IDirectXFile_RegisterTemplates(dxfile, empty_cmp_file, sizeof(empty_cmp_file) - 1);
340     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
341
342     hr = IDirectXFile_RegisterTemplates(dxfile, empty_xxxx_file, sizeof(empty_xxxx_file) - 1);
343     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
344
345     lminfo.lpMemory = empty_txt_file;
346     lminfo.dSize = sizeof(empty_txt_file) - 1;
347     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
348     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
349     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
350
351     lminfo.lpMemory = empty_bin_file;
352     lminfo.dSize = sizeof(empty_bin_file) - 1;
353     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
354     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
355     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
356
357     lminfo.lpMemory = empty_tzip_file;
358     lminfo.dSize = sizeof(empty_tzip_file) - 1;
359     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
360     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
361     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
362
363     lminfo.lpMemory = empty_bzip_file;
364     lminfo.dSize = sizeof(empty_bzip_file) - 1;
365     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
366     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
367     if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
368
369     lminfo.lpMemory = empty_cmp_file;
370     lminfo.dSize = sizeof(empty_cmp_file) - 1;
371     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
372     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFile_CreateEnumObject: %x\n", hr);
373
374     lminfo.lpMemory = empty_xxxx_file;
375     lminfo.dSize = sizeof(empty_xxxx_file) - 1;
376     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
377     ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFile_CreateEnumObject: %x\n", hr);
378
379     IDirectXFile_Release(dxfile);
380 }
381
382 static void test_compressed_files(void)
383 {
384     HRESULT hr;
385     LPDIRECTXFILE dxfile = NULL;
386     LPDIRECTXFILEENUMOBJECT enum_object;
387     LPDIRECTXFILEDATA file_data;
388     DXFILELOADMEMORY lminfo;
389     BYTE* data;
390     DWORD size;
391
392     if (!pDirectXFileCreate)
393     {
394         win_skip("DirectXFileCreate is not available\n");
395         return;
396     }
397
398     hr = pDirectXFileCreate(&dxfile);
399     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
400     if (!dxfile)
401     {
402         skip("Couldn't create DirectXFile interface\n");
403         return;
404     }
405
406     hr = IDirectXFile_RegisterTemplates(dxfile, compressed_template, sizeof(compressed_template) - 1);
407     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
408
409     lminfo.lpMemory = compressed_object;
410     lminfo.dSize = sizeof(compressed_object) - 1;
411     hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
412     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
413
414     hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &file_data);
415     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
416
417     hr = IDirectXFileData_GetData(file_data, NULL, &size, (void**)&data);
418     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
419
420     ok(size == 8, "Retrieved data size is wrong\n");
421     ok((*((WORD*)data) == 1) && (*((WORD*)(data+2)) == 2) && (*((DWORD*)(data+4)) == 3), "Retrieved data is wrong\n");
422
423     IDirectXFileData_Release(file_data);
424     IDirectXFileEnumObject_Release(enum_object);
425     IDirectXFile_Release(dxfile);
426 }
427
428 static void test_getname(void)
429 {
430     HRESULT hr;
431     ULONG ref;
432     LPDIRECTXFILE lpDirectXFile = NULL;
433     LPDIRECTXFILEENUMOBJECT lpdxfeo;
434     LPDIRECTXFILEDATA lpdxfd;
435     DXFILELOADMEMORY dxflm;
436     char name[100];
437     DWORD length;
438
439     if (!pDirectXFileCreate)
440     {
441         win_skip("DirectXFileCreate is not available\n");
442         return;
443     }
444
445     hr = pDirectXFileCreate(&lpDirectXFile);
446     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
447     if (!lpDirectXFile)
448     {
449         skip("Couldn't create DirectXFile interface\n");
450         return;
451     }
452
453     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, sizeof(template) - 1);
454     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
455
456     /* Check object with name */
457     dxflm.lpMemory = &object;
458     dxflm.dSize = sizeof(object) - 1;
459     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
460     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
461     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
462     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
463
464     hr = IDirectXFileData_GetName(lpdxfd, NULL, NULL);
465     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
466     hr = IDirectXFileData_GetName(lpdxfd, name, NULL);
467     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
468     hr = IDirectXFileData_GetName(lpdxfd, NULL, &length);
469     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
470     ok(length == 7, "Returned length should be 7 instead of %u\n", length);
471     length = sizeof(name);
472     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
473     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
474     ok(length == 7, "Returned length should be 7 instead of %u\n", length);
475     ok(!strcmp(name, "Object"), "Returned string should be 'Object' intead of '%s'\n", name);
476     length = 3;
477     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
478     ok(hr == DXFILEERR_BADVALUE, "IDirectXFileData_GetName: %x\n", hr);
479
480     ref = IDirectXFileEnumObject_Release(lpdxfeo);
481     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
482     ref = IDirectXFileData_Release(lpdxfd);
483     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
484
485     /* Check object without name */
486     dxflm.lpMemory = &object_noname;
487     dxflm.dSize = sizeof(object_noname) - 1;
488     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
489     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
490     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
491     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
492
493     hr = IDirectXFileData_GetName(lpdxfd, NULL, &length);
494     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
495     ok(length == 0, "Returned length should be 0 instead of %u\n", length);
496     length = sizeof(name);
497     hr = IDirectXFileData_GetName(lpdxfd, name, &length);
498     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
499     ok(length == 0, "Returned length should be 0 instead of %u\n", length);
500
501     ref = IDirectXFileEnumObject_Release(lpdxfeo);
502     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
503     ref = IDirectXFileData_Release(lpdxfd);
504     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
505     ref = IDirectXFile_Release(lpDirectXFile);
506     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
507 }
508
509 static void test_syntax(void)
510 {
511     HRESULT hr;
512     ULONG ref;
513     LPDIRECTXFILE lpDirectXFile = NULL;
514     LPDIRECTXFILEENUMOBJECT lpdxfeo;
515     LPDIRECTXFILEDATA lpdxfd;
516     DXFILELOADMEMORY dxflm;
517     DWORD size;
518     char** string;
519
520     if (!pDirectXFileCreate)
521     {
522         win_skip("DirectXFileCreate is not available\n");
523         return;
524     }
525
526     hr = pDirectXFileCreate(&lpDirectXFile);
527     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
528     if (!lpDirectXFile)
529     {
530         skip("Couldn't create DirectXFile interface\n");
531         return;
532     }
533
534     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template_syntax_empty_array, sizeof(template_syntax_empty_array) - 1);
535     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
536
537     dxflm.lpMemory = &object_syntax_empty_array_semicolon;
538     dxflm.dSize = sizeof(object_syntax_empty_array_semicolon) - 1;
539     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
540     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
541     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
542     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
543
544     ref = IDirectXFileEnumObject_Release(lpdxfeo);
545     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
546     if (hr == DXFILE_OK)
547     {
548         ref = IDirectXFileData_Release(lpdxfd);
549         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
550     }
551
552     dxflm.lpMemory = &object_syntax_empty_array_nosemicolon;
553     dxflm.dSize = sizeof(object_syntax_empty_array_nosemicolon) - 1;
554     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
555     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
556     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
557     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
558
559     ref = IDirectXFileEnumObject_Release(lpdxfeo);
560     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
561     if (hr == DXFILE_OK)
562     {
563         ref = IDirectXFileData_Release(lpdxfd);
564         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
565     }
566
567     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template_syntax_string, sizeof(template_syntax_string) - 1);
568     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
569
570     dxflm.lpMemory = &object_syntax_string_normal;
571     dxflm.dSize = sizeof(object_syntax_string_normal) - 1;
572     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
573     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
574     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
575     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
576     hr = IDirectXFileData_GetData(lpdxfd, NULL, &size, (void**)&string);
577     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
578     ok(size == sizeof(char*), "Got wrong data size %d\n", size);
579     ok(!strcmp(*string, "foobar"), "Got string %s, expected foobar\n", *string);
580
581     ref = IDirectXFileEnumObject_Release(lpdxfeo);
582     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
583     if (hr == DXFILE_OK)
584     {
585         ref = IDirectXFileData_Release(lpdxfd);
586         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
587     }
588
589     dxflm.lpMemory = &object_syntax_string_with_separator;
590     dxflm.dSize = sizeof(object_syntax_string_with_separator) - 1;
591     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
592     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
593     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
594     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
595     hr = IDirectXFileData_GetData(lpdxfd, NULL, &size, (void**)&string);
596     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
597     ok(size == sizeof(char*), "Got wrong data size %d\n", size);
598     ok(!strcmp(*string, "foo;bar"), "Got string %s, expected foo;bar\n", *string);
599
600     ref = IDirectXFileEnumObject_Release(lpdxfeo);
601     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
602     if (hr == DXFILE_OK)
603     {
604         ref = IDirectXFileData_Release(lpdxfd);
605         ok(ref == 0, "Got refcount %d, expected 0\n", ref);
606     }
607
608     ref = IDirectXFile_Release(lpDirectXFile);
609     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
610 }
611
612 /* Set it to 1 to expand the string when dumping the object. This is useful when there is
613  * only one string in a sub-object (very common). Use with care, this may lead to a crash. */
614 #define EXPAND_STRING 0
615
616 static void process_data(LPDIRECTXFILEDATA lpDirectXFileData, int level)
617 {
618     HRESULT hr;
619     char name[100];
620     GUID clsid;
621     CONST GUID* clsid_type = NULL;
622     char str_clsid[40];
623     char str_clsid_type[40];
624     DWORD len = 100;
625     LPDIRECTXFILEOBJECT pChildObj;
626     int i;
627     int j = 0;
628     LPBYTE pData;
629     DWORD k, size;
630
631     hr = IDirectXFileData_GetId(lpDirectXFileData, &clsid);
632     ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
633     hr = IDirectXFileData_GetName(lpDirectXFileData, name, &len);
634     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
635     hr = IDirectXFileData_GetType(lpDirectXFileData, &clsid_type);
636     ok(hr == DXFILE_OK, "IDirectXFileData_GetType: %x\n", hr);
637     hr = IDirectXFileData_GetData(lpDirectXFileData, NULL, &size, (void**)&pData);
638     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
639     for (i = 0; i < level; i++)
640         printf("  ");
641     debugstr_guid(str_clsid, &clsid);
642     debugstr_guid(str_clsid_type, clsid_type);
643     printf("Found object '%s' - %s - %s - %d\n", len ? name : "", str_clsid, str_clsid_type, size);
644
645     if (EXPAND_STRING && size == 4)
646     {
647         char * str = *(char**)pData;
648         printf("string %s\n", str);
649     }
650     else if (size)
651     {
652         for (k = 0; k < size; k++)
653         {
654             if (k && !(k%16))
655                 printf("\n");
656             printf("%02x ", pData[k]);
657         }
658         printf("\n");
659     }
660
661     level++;
662
663     while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(lpDirectXFileData, &pChildObj)))
664     {
665         LPDIRECTXFILEDATA p1;
666         LPDIRECTXFILEDATAREFERENCE p2;
667         LPDIRECTXFILEBINARY p3;
668         j++;
669
670         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileData, (void **) &p1);
671         if (SUCCEEDED(hr))
672         {
673             for (i = 0; i < level; i++)
674                 printf("  ");
675             printf("Found Data (%d)\n", j);
676             process_data(p1, level);
677             IDirectXFileData_Release(p1);
678         }
679         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileDataReference, (void **) &p2);
680         if (SUCCEEDED(hr))
681         {
682             LPDIRECTXFILEDATA pfdo;
683             for (i = 0; i < level; i++)
684                 printf("  ");
685             printf("Found Data Reference (%d)\n", j);
686 #if 0
687             hr = IDirectXFileDataReference_GetId(lpDirectXFileData, &clsid);
688             ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
689             hr = IDirectXFileDataReference_GetName(lpDirectXFileData, name, &len);
690             ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
691 #endif
692             IDirectXFileDataReference_Resolve(p2, &pfdo);
693             process_data(pfdo, level);
694             IDirectXFileData_Release(pfdo);
695             IDirectXFileDataReference_Release(p2);
696         }
697         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileBinary, (void **) &p3);
698         if (SUCCEEDED(hr))
699         {
700             for (i = 0; i < level; i++)
701                 printf("  ");
702             printf("Found Binary (%d)\n", j);
703             IDirectXFileBinary_Release(p3);
704         }
705         IDirectXFileObject_Release(pChildObj);
706     }
707
708     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileData_GetNextObject: %x\n", hr);
709 }
710
711 /* Dump an X file 'objects.x' and its related templates file 'templates.x' if they are both presents
712  * Useful for debug by comparing outputs from native and builtin dlls */
713 static void test_dump(void)
714 {
715     HRESULT hr;
716     ULONG ref;
717     LPDIRECTXFILE lpDirectXFile = NULL;
718     LPDIRECTXFILEENUMOBJECT lpDirectXFileEnumObject = NULL;
719     LPDIRECTXFILEDATA lpDirectXFileData = NULL;
720     HANDLE hFile;
721     LPVOID pvData = NULL;
722     DWORD cbSize;
723
724     if (!pDirectXFileCreate)
725     {
726         win_skip("DirectXFileCreate is not available\n");
727         goto exit;
728     }
729
730     /* Dump data only if there is an object and a template */
731     hFile = CreateFileA("objects.x", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
732     if (hFile == INVALID_HANDLE_VALUE)
733       return;
734     CloseHandle(hFile);
735
736     hFile = CreateFileA("templates.x", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
737     if (hFile == INVALID_HANDLE_VALUE)
738       return;
739
740     pvData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 10000);
741
742     if (!ReadFile(hFile, pvData, 10000, &cbSize, NULL))
743     {
744       skip("Templates file is too big\n");
745       goto exit;
746     }
747
748     printf("Load templates file (%d bytes)\n", cbSize);
749
750     hr = pDirectXFileCreate(&lpDirectXFile);
751     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
752     if(!lpDirectXFile)
753     {
754         skip("Couldn't create DirectXFile interface\n");
755         goto exit;
756     }
757
758     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, pvData, cbSize);
759     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
760
761     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, (LPVOID)"objects.x", DXFILELOAD_FROMFILE, &lpDirectXFileEnumObject);
762     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
763
764     while (SUCCEEDED(hr = IDirectXFileEnumObject_GetNextDataObject(lpDirectXFileEnumObject, &lpDirectXFileData)))
765     {
766         printf("\n");
767         process_data(lpDirectXFileData, 0);
768         IDirectXFileData_Release(lpDirectXFileData);
769     }
770     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
771
772     ref = IDirectXFile_Release(lpDirectXFileEnumObject);
773     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
774
775     ref = IDirectXFile_Release(lpDirectXFile);
776     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
777
778     CloseHandle(hFile);
779
780 exit:
781     HeapFree(GetProcessHeap(), 0, pvData);
782 }
783
784 START_TEST(d3dxof)
785 {
786     init_function_pointers();
787
788     test_refcount();
789     test_CreateEnumObject();
790     test_file_types();
791     test_compressed_files();
792     test_getname();
793     test_syntax();
794     test_dump();
795
796     FreeLibrary(hd3dxof);
797 }