wintrust: Fix copy-paste error.
[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 <assert.h>
23 #include <stdio.h>
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 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 char object[] =
50 "xof 0302txt 0064\n"
51 "Header Object\n"
52 "{\n"
53 "1; 2; 3;\n"
54 "}\n";
55
56 static void init_function_pointers(void)
57 {
58     /* We have to use LoadLibrary as no d3dxof functions are referenced directly */
59     hd3dxof = LoadLibraryA("d3dxof.dll");
60
61     pDirectXFileCreate = (void *)GetProcAddress(hd3dxof, "DirectXFileCreate");
62 }
63
64 static ULONG getRefcount(IUnknown *iface)
65 {
66     IUnknown_AddRef(iface);
67     return IUnknown_Release(iface);
68 }
69
70 static void test_refcount(void)
71 {
72     HRESULT hr;
73     ULONG ref;
74     LPDIRECTXFILE lpDirectXFile = NULL;
75     LPDIRECTXFILEENUMOBJECT lpdxfeo;
76     LPDIRECTXFILEDATA lpdxfd;
77     DXFILELOADMEMORY dxflm;
78
79     if (!pDirectXFileCreate)
80     {
81         win_skip("DirectXFileCreate is not available\n");
82         return;
83     }
84
85     hr = pDirectXFileCreate(&lpDirectXFile);
86     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
87     if(!lpDirectXFile)
88     {
89         skip("Couldn't create DirectXFile interface\n");
90         return;
91     }
92
93     ref = getRefcount( (IUnknown *) lpDirectXFile);
94     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
95     ref = IDirectXFile_AddRef(lpDirectXFile);
96     ok(ref == 2, "Got refcount %d, expected 1\n", ref);
97     ref = IDirectXFile_Release(lpDirectXFile);
98     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
99
100     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, strlen(template));
101     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
102
103     dxflm.lpMemory = &object;
104     dxflm.dSize = strlen(object);
105     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, DXFILELOAD_FROMMEMORY, &lpdxfeo);
106     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
107     ref = getRefcount( (IUnknown *) lpDirectXFile);
108     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
109     ref = getRefcount( (IUnknown *) lpdxfeo);
110     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
111
112     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
113     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
114     ref = getRefcount( (IUnknown *) lpDirectXFile);
115     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
116     ref = getRefcount( (IUnknown *) lpdxfeo);
117     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
118     /* Enum object gets references to all top level objects */
119     ref = getRefcount( (IUnknown *) lpdxfd);
120     ok(ref == 2, "Got refcount %d, expected 2\n", ref);
121
122     ref = IDirectXFile_Release(lpDirectXFile);
123     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
124     /* Nothing changes for all other objects */
125     ref = getRefcount( (IUnknown *) lpdxfeo);
126     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
127     ref = getRefcount( (IUnknown *) lpdxfd);
128     ok(ref == 2, "Got refcount %d, expected 1\n", ref);
129
130     ref = IDirectXFileEnumObject_Release(lpdxfeo);
131     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
132     /* Enum object releases references to all top level objects */
133     ref = getRefcount( (IUnknown *) lpdxfd);
134     ok(ref == 1, "Got refcount %d, expected 1\n", ref);
135
136     ref = IDirectXFileData_Release(lpdxfd);
137     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
138 }
139
140 static void test_CreateEnumObject(void)
141 {
142     HRESULT hr;
143     ULONG ref;
144     LPDIRECTXFILE lpDirectXFile = NULL;
145     LPDIRECTXFILEENUMOBJECT lpdxfeo;
146     LPDIRECTXFILEDATA lpdxfd;
147     DXFILELOADMEMORY dxflm;
148     BYTE* pdata;
149     DWORD size;
150
151     if (!pDirectXFileCreate)
152     {
153         win_skip("DirectXFileCreate is not available\n");
154         return;
155     }
156
157     hr = pDirectXFileCreate(&lpDirectXFile);
158     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
159     if(!lpDirectXFile)
160     {
161         skip("Couldn't create DirectXFile interface\n");
162         return;
163     }
164
165     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, strlen(template));
166     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
167
168     dxflm.lpMemory = &object;
169     dxflm.dSize = strlen(object);
170     /* Check that only lowest 4 bits are relevant in DXFILELOADOPTIONS */
171     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, &dxflm, 0xFFFFFFF0 + DXFILELOAD_FROMMEMORY, &lpdxfeo);
172     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
173
174     hr = IDirectXFileEnumObject_GetNextDataObject(lpdxfeo, &lpdxfd);
175     ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
176
177     hr = IDirectXFileData_GetData(lpdxfd, NULL, &size, (void**)&pdata);
178     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
179
180     ok(size == 8, "Retrieved data size is wrong\n");
181     ok((*((WORD*)pdata) == 1) && (*((WORD*)(pdata+2)) == 2) && (*((DWORD*)(pdata+4)) == 3), "Retrieved data is wrong\n");
182
183     ref = IDirectXFileEnumObject_Release(lpdxfeo);
184     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
185
186     ref = IDirectXFile_Release(lpDirectXFile);
187     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
188
189     ref = IDirectXFileData_Release(lpdxfd);
190     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
191 }
192
193 /* Set it to 1 to expand the string when dumping the object. This is useful when there is
194  * only one string in a sub-object (very common). Use with care, this may lead to a crash. */
195 #define EXPAND_STRING 0
196
197 static void process_data(LPDIRECTXFILEDATA lpDirectXFileData, int* plevel)
198 {
199     HRESULT hr;
200     char name[100];
201     GUID clsid;
202     CONST GUID* clsid_type = NULL;
203     char str_clsid[40];
204     char str_clsid_type[40];
205     DWORD len= 100;
206     LPDIRECTXFILEOBJECT pChildObj;
207     int i;
208     int j = 0;
209     LPBYTE pData;
210     DWORD k, size;
211
212     hr = IDirectXFileData_GetId(lpDirectXFileData, &clsid);
213     ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
214     hr = IDirectXFileData_GetName(lpDirectXFileData, name, &len);
215     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
216     hr = IDirectXFileData_GetType(lpDirectXFileData, &clsid_type);
217     ok(hr == DXFILE_OK, "IDirectXFileData_GetType: %x\n", hr);
218     hr = IDirectXFileData_GetData(lpDirectXFileData, NULL, &size, (void**)&pData);
219     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
220     for (i = 0; i < *plevel; i++)
221         printf("  ");
222     debugstr_guid(str_clsid, &clsid);
223     debugstr_guid(str_clsid_type, clsid_type);
224     printf("Found object '%s' - %s - %s - %d\n", name, str_clsid, str_clsid_type, size);
225
226     if (EXPAND_STRING && size == 4)
227     {
228         char * str = *(char**)pData;
229         printf("string %s\n", str);
230     }
231     else if (size)
232     {
233         for (k = 0; k < size; k++)
234         {
235             if (k && !(k%16))
236                 printf("\n");
237             printf("%02x ", pData[k]);
238         }
239         printf("\n");
240     }
241     (*plevel)++;
242     while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(lpDirectXFileData, &pChildObj)))
243     {
244         LPDIRECTXFILEDATA p1;
245         LPDIRECTXFILEDATAREFERENCE p2;
246         LPDIRECTXFILEBINARY p3;
247         j++;
248
249         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileData, (void **) &p1);
250         if (SUCCEEDED(hr))
251         {
252             for (i = 0; i < *plevel; i++)
253                 printf("  ");
254             printf("Found Data (%d)\n", j);
255             process_data(p1, plevel);
256             IDirectXFileData_Release(p1);
257         }
258         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileDataReference, (void **) &p2);
259         if (SUCCEEDED(hr))
260         {
261             LPDIRECTXFILEDATA pfdo;
262             for (i = 0; i < *plevel; i++)
263                 printf("  ");
264             printf("Found Data Reference (%d)\n", j);
265 #if 0
266             hr = IDirectXFileDataReference_GetId(lpDirectXFileData, &clsid);
267             ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
268             hr = IDirectXFileDataReference_GetName(lpDirectXFileData, name, &len);
269             ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
270 #endif
271             IDirectXFileDataReference_Resolve(p2, &pfdo);
272             process_data(pfdo, plevel);
273             IDirectXFileData_Release(pfdo);
274             IDirectXFileDataReference_Release(p2);
275         }
276         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileBinary, (void **) &p3);
277         if (SUCCEEDED(hr))
278         {
279             for (i = 0; i < *plevel; i++)
280                 printf("  ");
281             printf("Found Binary (%d)\n", j);
282             IDirectXFileBinary_Release(p3);
283         }
284     }
285     (*plevel)--;
286     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileData_GetNextObject: %x\n", hr);
287 }
288
289 static void test_dump(void)
290 {
291     HRESULT hr;
292     ULONG ref;
293     LPDIRECTXFILE lpDirectXFile = NULL;
294     LPDIRECTXFILEENUMOBJECT lpDirectXFileEnumObject = NULL;
295     LPDIRECTXFILEDATA lpDirectXFileData = NULL;
296     HANDLE hFile;
297     LPVOID pvData = NULL;
298     DWORD cbSize;
299
300     if (!pDirectXFileCreate)
301     {
302         win_skip("DirectXFileCreate is not available\n");
303         goto exit;
304     }
305
306     /* Dump data only if there is an object and a template */
307     hFile = CreateFileA("objects.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
308     if (hFile == INVALID_HANDLE_VALUE)
309       return;
310     CloseHandle(hFile);
311
312     hFile = CreateFileA("templates.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
313     if (hFile == INVALID_HANDLE_VALUE)
314       return;
315
316     pvData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 10000);
317
318     if (!ReadFile(hFile, pvData, 10000, &cbSize, NULL))
319     {
320       skip("Template file is too big\n");
321       goto exit;
322     }
323
324     printf("Load %d bytes\n", cbSize);
325
326     hr = pDirectXFileCreate(&lpDirectXFile);
327     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
328     if(!lpDirectXFile)
329     {
330         skip("Couldn't create DirectXFile interface\n");
331         goto exit;
332     }
333
334     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, pvData, strlen(pvData));
335     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
336
337     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, (LPVOID)"objects.txt", DXFILELOAD_FROMFILE, &lpDirectXFileEnumObject);
338     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
339
340     while (SUCCEEDED(hr = IDirectXFileEnumObject_GetNextDataObject(lpDirectXFileEnumObject, &lpDirectXFileData)))
341     {
342         int level = 0;
343         printf("\n");
344         process_data(lpDirectXFileData, &level);
345         IDirectXFileData_Release(lpDirectXFileData);
346     }
347     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
348
349     ref = IDirectXFile_Release(lpDirectXFileEnumObject);
350     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
351
352     ref = IDirectXFile_Release(lpDirectXFile);
353     ok(ref == 0, "Got refcount %d, expected 0\n", ref);
354
355     CloseHandle(hFile);
356
357 exit:
358     HeapFree(GetProcessHeap(), 0, pvData);
359 }
360
361 START_TEST(d3dxof)
362 {
363     init_function_pointers();
364
365     test_refcount();
366     test_CreateEnumObject();
367     test_dump();
368
369     FreeLibrary(hd3dxof);
370 }