d3dxof/tests: Remove redundant NULL check before HeapFree (Smatch).
[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 static void init_function_pointers(void)
50 {
51     /* We have to use LoadLibrary as no d3dxof functions are referenced directly */
52     hd3dxof = LoadLibraryA("d3dxof.dll");
53
54     pDirectXFileCreate = (void *)GetProcAddress(hd3dxof, "DirectXFileCreate");
55 }
56
57 static unsigned long getRefcount(IUnknown *iface)
58 {
59     IUnknown_AddRef(iface);
60     return IUnknown_Release(iface);
61 }
62
63 static void test_d3dxof(void)
64 {
65     HRESULT hr;
66     unsigned long ref;
67     LPDIRECTXFILE lpDirectXFile = NULL;
68
69     if (!pDirectXFileCreate)
70     {
71         win_skip("DirectXFileCreate is not available\n");
72         return;
73     }
74
75     hr = pDirectXFileCreate(&lpDirectXFile);
76     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
77     if(!lpDirectXFile)
78     {
79         skip("Couldn't create DirectXFile interface\n");
80         return;
81     }
82
83     ref = getRefcount( (IUnknown *) lpDirectXFile);
84     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
85
86     ref = IDirectXFile_AddRef(lpDirectXFile);
87     ok(ref == 2, "Got refcount %ld, expected 1\n", ref);
88
89     ref = IDirectXFile_Release(lpDirectXFile);
90     ok(ref == 1, "Got refcount %ld, expected 1\n", ref);
91
92     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, template, strlen(template));
93     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
94
95     ref = IDirectXFile_Release(lpDirectXFile);
96     ok(ref == 0, "Got refcount %ld, expected 1\n", ref);
97 }
98
99 /* Set it to 1 to expand the string when dumping the object. This is usefull when there is
100  * only one string in a sub-object (very common). Use with care, this may lead to a crash. */
101 #define EXPAND_STRING 0
102
103 static void process_data(LPDIRECTXFILEDATA lpDirectXFileData, int* plevel)
104 {
105     HRESULT hr;
106     char name[100];
107     GUID clsid;
108     CONST GUID* clsid_type = NULL;
109     char str_clsid[40];
110     char str_clsid_type[40];
111     DWORD len= 100;
112     LPDIRECTXFILEOBJECT pChildObj;
113     int i,k;
114     int j = 0;
115     LPBYTE pData;
116     DWORD size;
117
118     hr = IDirectXFileData_GetId(lpDirectXFileData, &clsid);
119     ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
120     hr = IDirectXFileData_GetName(lpDirectXFileData, name, &len);
121     ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
122     hr = IDirectXFileData_GetType(lpDirectXFileData, &clsid_type);
123     ok(hr == DXFILE_OK, "IDirectXFileData_GetType: %x\n", hr);
124     hr = IDirectXFileData_GetData(lpDirectXFileData, NULL, &size, (void**)&pData);
125     ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
126     for (i = 0; i < *plevel; i++)
127         printf("  ");
128     debugstr_guid(str_clsid, &clsid);
129     debugstr_guid(str_clsid_type, clsid_type);
130     printf("Found object '%s' - %s - %s - %d\n", name, str_clsid, str_clsid_type, size);
131
132     if (EXPAND_STRING && size == 4)
133     {
134         char * str = *(char**)pData;
135         printf("string %s\n", str);
136     }
137     else if (size)
138     {
139         for (k = 0; k < size; k++)
140         {
141             if (k && !(k%16))
142                 printf("\n");
143             printf("%02x ", pData[k]);
144         }
145         printf("\n");
146     }
147     (*plevel)++;
148     while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(lpDirectXFileData, &pChildObj)))
149     {
150         LPDIRECTXFILEDATA p1;
151         LPDIRECTXFILEDATAREFERENCE p2;
152         LPDIRECTXFILEBINARY p3;
153         j++;
154
155         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileData, (void **) &p1);
156         if (SUCCEEDED(hr))
157         {
158             for (i = 0; i < *plevel; i++)
159                 printf("  ");
160             printf("Found Data (%d)\n", j);
161             process_data(p1, plevel);
162             IDirectXFileData_Release(p1);
163         }
164         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileDataReference, (void **) &p2);
165         if (SUCCEEDED(hr))
166         {
167             LPDIRECTXFILEDATA pfdo;
168             for (i = 0; i < *plevel; i++)
169                 printf("  ");
170             printf("Found Data Reference (%d)\n", j);
171 #if 0
172             hr = IDirectXFileDataReference_GetId(lpDirectXFileData, &clsid);
173             ok(hr == DXFILE_OK, "IDirectXFileData_GetId: %x\n", hr);
174             hr = IDirectXFileDataReference_GetName(lpDirectXFileData, name, &len);
175             ok(hr == DXFILE_OK, "IDirectXFileData_GetName: %x\n", hr);
176 #endif
177             IDirectXFileDataReference_Resolve(p2, &pfdo);
178             process_data(pfdo, plevel);
179             IDirectXFileData_Release(pfdo);
180             IDirectXFileDataReference_Release(p2);
181         }
182         hr = IDirectXFileObject_QueryInterface(pChildObj, &IID_IDirectXFileBinary, (void **) &p3);
183         if (SUCCEEDED(hr))
184         {
185             for (i = 0; i < *plevel; i++)
186                 printf("  ");
187             printf("Found Binary (%d)\n", j);
188             IDirectXFileBinary_Release(p3);
189         }
190     }
191     (*plevel)--;
192     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileData_GetNextObject: %x\n", hr);
193 }
194
195 static void test_dump(void)
196 {
197     HRESULT hr;
198     unsigned long ref;
199     LPDIRECTXFILE lpDirectXFile = NULL;
200     LPDIRECTXFILEENUMOBJECT lpDirectXFileEnumObject = NULL;
201     LPDIRECTXFILEDATA lpDirectXFileData = NULL;
202     HANDLE hFile;
203     LPVOID pvData = NULL;
204     DWORD cbSize;
205
206     if (!pDirectXFileCreate)
207     {
208         win_skip("DirectXFileCreate is not available\n");
209         goto exit;
210     }
211
212     /* Dump data only if there is an object and a template */
213     hFile = CreateFileA("objects.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
214     if (hFile == INVALID_HANDLE_VALUE)
215       return;
216     CloseHandle(hFile);
217
218     hFile = CreateFileA("templates.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
219     if (hFile == INVALID_HANDLE_VALUE)
220       return;
221
222     pvData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 10000);
223
224     if (!ReadFile(hFile, pvData, 10000, &cbSize, NULL))
225     {
226       skip("Template file is too big\n");
227       goto exit;
228     }
229
230     printf("Load %d (%d) bytes\n", cbSize, strlen(pvData));
231
232     hr = pDirectXFileCreate(&lpDirectXFile);
233     ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
234     if(!lpDirectXFile)
235     {
236         skip("Couldn't create DirectXFile interface\n");
237         goto exit;
238     }
239
240     hr = IDirectXFile_RegisterTemplates(lpDirectXFile, pvData, strlen(pvData));
241     ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
242
243     hr = IDirectXFile_CreateEnumObject(lpDirectXFile, (LPVOID)"objects.txt", DXFILELOAD_FROMFILE, &lpDirectXFileEnumObject);
244     ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
245
246     while (SUCCEEDED(hr = IDirectXFileEnumObject_GetNextDataObject(lpDirectXFileEnumObject, &lpDirectXFileData)))
247     {
248         int level = 0;
249         printf("\n");
250         process_data(lpDirectXFileData, &level);
251         IDirectXFileData_Release(lpDirectXFileData);
252     }
253     ok(hr == DXFILE_OK || hr == DXFILEERR_NOMOREOBJECTS, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
254
255     ref = IDirectXFile_Release(lpDirectXFileEnumObject);
256     ok(ref == 0, "Got refcount %ld, expected 0\n", ref);
257
258     ref = IDirectXFile_Release(lpDirectXFile);
259     ok(ref == 0, "Got refcount %ld, expected 0\n", ref);
260
261     CloseHandle(hFile);
262
263 exit:
264     HeapFree(GetProcessHeap(), 0, pvData);
265 }
266
267 START_TEST(d3dxof)
268 {
269     init_function_pointers();
270
271     test_d3dxof();
272     test_dump();
273
274     FreeLibrary(hd3dxof);
275 }