a0b5ee849b7ab16a0d6d683f73b63c8daf31951b
[reactos.git] / rostests / winetests / avifil32 / api.c
1 /*
2 * Unit test suite for AVI Functions
3 *
4 * Copyright 2008 Detlef Riekenberg
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
22 #define WIN32_NO_STATUS
23 #define _INC_WINDOWS
24 #define COM_NO_WINDOWS_H
25
26 #define COBJMACROS
27 #define CONST_VTABLE
28
29 #include <wine/test.h>
30 #include <initguid.h>
31 #include <wingdi.h>
32 #include <vfw.h>
33
34 /* ########################### */
35
36 static const CHAR winetest0[] = "winetest0";
37 static const CHAR winetest1[] = "winetest1";
38 static const CHAR testfilename[] = "wine_avifil32_test.avi";
39
40 /* ########################### */
41
42 static const DWORD deffh[] = /* file_header */
43 {
44 FOURCC_RIFF, 0x34c6 /* length */, formtypeAVI,
45 FOURCC_LIST, 0x1ac /* length */,
46 listtypeAVIHEADER, ckidAVIMAINHDR, sizeof(MainAVIHeader),
47 };
48
49 static const MainAVIHeader defmah =
50 {
51 0x00008256, /* dwMicroSecPerFrame */
52 0x000080e8, /* dwMaxBytesPerSec */
53 0x00000000, /* dwPaddingGranularity */
54 0x00000910, /* dwFlags */
55 1, /* dwTotalFrames */
56 0, /* dwInitialFrames */
57 2, /* dwStreams */
58 0x00100000, /* dwSuggestedBufferSize*/
59 8, /* dwWidth */
60 6, /* dwHeight */
61 { 0, 0, 0, 0 } /* dwReserved[4] */
62 };
63
64 static const AVIStreamHeader defash0 =
65 {
66 streamtypeVIDEO, /* fccType */
67 0x30323449, /* fccHandler */
68 0x00000000, /* dwFlags */
69 0, /* wPriority */
70 0, /* wLanguage */
71 0, /* dwInitialFrames */
72 0x000003e9, /* dwScale */
73 0x00007530, /* dwRate */
74 0, /* dwStart */
75 1, /* dwLength */
76 0x00100000, /* dwSuggestedBufferSize*/
77 0xffffffff, /* dwQuality */
78 0, /* dwSampleSize */
79 { 0, 0, 0, 0 } /* short left right top bottom */
80 };
81
82 static const AVIStreamHeader defash1 =
83 {
84 /* AVIStreamHeader */
85 streamtypeAUDIO, /* fccType */
86 1, /* fccHandler */
87 0, /* dwFlags */
88 0, /* wPriority */
89 0, /* wLanguage */
90 0, /* dwInitialFrames */
91 1, /* dwScale */
92 0x00002b11, /* dwRate */
93 0, /* dwStart */
94 0x00000665, /* dwLength */
95 0x00003000, /* dwSuggestedBufferSize*/
96 0xffffffff, /* dwQuality */
97 2, /* dwSampleSize */
98 { 0, 0, 0, 0 } /* short left right top bottom */
99 };
100
101 static const PCMWAVEFORMAT defpcmwf =
102 {
103 {
104 1, /* wFormatTag */
105 2, /* nChannels */
106 11025, /* nSamplesPerSec */
107 22050, /* nAvgBytesPerSec */
108 2, /* nBlockAlign */
109 },
110 8, /* wBitsPerSample */
111 };
112
113 typedef struct common_avi_headers {
114 DWORD fh[sizeof(deffh)];
115 MainAVIHeader mah;
116 AVIStreamHeader ash0;
117 AVIStreamHeader ash1;
118 PCMWAVEFORMAT pcmwf;
119 } COMMON_AVI_HEADERS;
120
121 /* Extra data needed to get the VFW API to load the file */
122 /* DWORD deffh */
123 /* MainAVIHeader mah */
124 static const DWORD streamlist[] =
125 {
126 FOURCC_LIST, 0xd4 /* length */,
127 listtypeSTREAMHEADER, ckidSTREAMHEADER, 0x38 /* length */,
128 };
129 /* AVIStreamHeader ash0 */
130 static const DWORD videostreamformat[] =
131 {
132 ckidSTREAMFORMAT, 0x28 /* length */,
133 0x00000028, 0x00000008, 0x00000006, 0x00180001,
134 0x30323449, 0x00000090, 0x00000000, 0x00000000,
135 0x00000000, 0x00000000,
136 };
137 static const DWORD padding1[] =
138 {
139 ckidAVIPADDING, 0xc /* length */,
140 0x00000004, 0x00000000, 0x63643030
141 };
142 static const DWORD videopropheader[] =
143 {
144 0x70727076, 0x44 /* length */,
145 0x00000000, 0x00000000,
146 0x0000001e, 0x00000008, 0x00000006, 0x00100009,
147 0x00000008, 0x00000006, 0x00000001, 0x00000006,
148 0x00000008, 0x00000006, 0x00000008, 0x00000000,
149 0x00000000, 0x00000000, 0x00000000,
150 FOURCC_LIST, 0x70 /* length */,
151 listtypeSTREAMHEADER, ckidSTREAMHEADER, 0x38 /* length */,
152 };
153 /* AVIStreamHeader ash1 */
154 static const DWORD audiostreamformat_pre[] =
155 {
156 ckidSTREAMFORMAT, sizeof(PCMWAVEFORMAT) /* length */,
157 };
158 /* PCMWAVEFORMAT pcmwf */
159 static DWORD data[] =
160 {
161 ckidAVIPADDING, 0xc /* length */,
162 0x00000004, 0x00000000, 0x62773130,
163 ckidAVIPADDING, 0xc /* length */,
164 0x6c6d646f, 0x686c6d64, 0x000000f8,
165 FOURCC_LIST, 0x18 /* length */,
166 0x4f464e49,
167 0x54465349, 0xc /* length */,
168 0x6676614c, 0x332e3235, 0x00302e37,
169 ckidAVIPADDING, 0x4 /* length */,
170 0,
171 FOURCC_LIST, 0xd1b /* length */, listtypeAVIMOVIE,
172 0, 0
173 };
174
175 /* ########################### */
176
177 static void test_AVISaveOptions(void)
178 {
179 AVICOMPRESSOPTIONS options[2];
180 LPAVICOMPRESSOPTIONS poptions[2];
181 PAVISTREAM streams[2] = {NULL, NULL};
182 HRESULT hres;
183 DWORD res;
184 LONG lres;
185
186 poptions[0] = &options[0];
187 poptions[1] = &options[1];
188 ZeroMemory(options, sizeof(options));
189
190 SetLastError(0xdeadbeef);
191 hres = CreateEditableStream(&streams[0], NULL);
192 ok(hres == AVIERR_OK, "0: got 0x%x and %p (expected AVIERR_OK)\n", hres, streams[0]);
193
194 SetLastError(0xdeadbeef);
195 hres = CreateEditableStream(&streams[1], NULL);
196 ok(hres == AVIERR_OK, "1: got 0x%x and %p (expected AVIERR_OK)\n", hres, streams[1]);
197
198 SetLastError(0xdeadbeef);
199 hres = EditStreamSetNameA(streams[0], winetest0);
200 ok(hres == AVIERR_OK, "0: got 0x%x (expected AVIERR_OK)\n", hres);
201
202 SetLastError(0xdeadbeef);
203 hres = EditStreamSetNameA(streams[1], winetest1);
204 ok(hres == AVIERR_OK, "1: got 0x%x (expected AVIERR_OK)\n", hres);
205
206 if (winetest_interactive) {
207 SetLastError(0xdeadbeef);
208 res = AVISaveOptions(0, ICMF_CHOOSE_DATARATE |ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_ALLCOMPRESSORS,
209 2, streams, poptions);
210 trace("got %u with 0x%x/%u\n", res, GetLastError(), GetLastError());
211 }
212
213 SetLastError(0xdeadbeef);
214 lres = AVISaveOptionsFree(2, poptions);
215 ok(lres == AVIERR_OK, "got 0x%x with 0x%x/%u\n", lres, GetLastError(), GetLastError());
216
217 SetLastError(0xdeadbeef);
218 res = AVIStreamRelease(streams[0]);
219 ok(res == 0, "0: got refcount %u (expected 0)\n", res);
220
221 SetLastError(0xdeadbeef);
222 res = AVIStreamRelease(streams[1]);
223 ok(res == 0, "1: got refcount %u (expected 0)\n", res);
224
225 }
226
227 /* ########################### */
228
229 static void test_EditStreamSetInfo(void)
230 {
231 PAVISTREAM stream = NULL;
232 HRESULT hres;
233 AVISTREAMINFO info, info2;
234
235 hres = CreateEditableStream(&stream, NULL);
236 ok(hres == AVIERR_OK, "got 0x%08X, expected AVIERR_OK\n", hres);
237
238 /* Size parameter is somehow checked (notice the crash with size=-1 below) */
239 hres = EditStreamSetInfo(stream, NULL, 0);
240 ok( hres == AVIERR_BADSIZE, "got 0x%08X, expected AVIERR_BADSIZE\n", hres);
241
242 hres = EditStreamSetInfo(stream, NULL, sizeof(AVISTREAMINFO)-1 );
243 ok( hres == AVIERR_BADSIZE, "got 0x%08X, expected AVIERR_BADSIZE\n", hres);
244
245 if(0)
246 {
247 /* Crashing - first parameter not checked */
248 EditStreamSetInfo(NULL, &info, sizeof(AVISTREAMINFO) );
249
250 /* Crashing - second parameter not checked */
251 EditStreamSetInfo(stream, NULL, sizeof(AVISTREAMINFO) );
252
253 EditStreamSetInfo(stream, NULL, -1);
254 }
255
256 hres = AVIStreamInfo(stream, &info, sizeof(AVISTREAMINFO) );
257 ok( hres == 0, "got 0x%08X, expected 0\n", hres);
258
259 /* Does the function check what's it's updating ? */
260
261 #define IS_INFO_UPDATED(m) do { \
262 hres = EditStreamSetInfo(stream, &info, sizeof(AVISTREAMINFO) ); \
263 ok( hres == 0, "got 0x%08X, expected 0\n", hres); \
264 hres = AVIStreamInfo(stream, &info2, sizeof(AVISTREAMINFO) ); \
265 ok( hres == 0, "got 0x%08X, expected 0\n", hres); \
266 ok( info2.m == info.m, "EditStreamSetInfo did not update "#m" parameter\n" ); \
267 } while(0)
268
269 info.dwStart++;
270 IS_INFO_UPDATED(dwStart);
271 info.dwStart = 0;
272 IS_INFO_UPDATED(dwStart);
273
274 info.wPriority++;
275 IS_INFO_UPDATED(wPriority);
276 info.wPriority = 0;
277 IS_INFO_UPDATED(wPriority);
278
279 info.wLanguage++;
280 IS_INFO_UPDATED(wLanguage);
281 info.wLanguage = 0;
282 IS_INFO_UPDATED(wLanguage);
283
284 info.dwScale++;
285 IS_INFO_UPDATED(dwScale);
286 info.dwScale = 0;
287 IS_INFO_UPDATED(dwScale);
288
289 info.dwRate++;
290 IS_INFO_UPDATED(dwRate);
291 info.dwRate = 0;
292 IS_INFO_UPDATED(dwRate);
293
294 info.dwQuality++;
295 IS_INFO_UPDATED(dwQuality);
296 info.dwQuality = 0;
297 IS_INFO_UPDATED(dwQuality);
298 info.dwQuality = -2;
299 IS_INFO_UPDATED(dwQuality);
300 info.dwQuality = ICQUALITY_HIGH+1;
301 IS_INFO_UPDATED(dwQuality);
302
303 info.rcFrame.left = 0;
304 IS_INFO_UPDATED(rcFrame.left);
305 info.rcFrame.top = 0;
306 IS_INFO_UPDATED(rcFrame.top);
307 info.rcFrame.right = 0;
308 IS_INFO_UPDATED(rcFrame.right);
309 info.rcFrame.bottom = 0;
310 IS_INFO_UPDATED(rcFrame.bottom);
311
312 info.rcFrame.left = -1;
313 IS_INFO_UPDATED(rcFrame.left);
314 info.rcFrame.top = -1;
315 IS_INFO_UPDATED(rcFrame.top);
316 info.rcFrame.right = -1;
317 IS_INFO_UPDATED(rcFrame.right);
318 info.rcFrame.bottom = -1;
319 IS_INFO_UPDATED(rcFrame.bottom);
320 AVIStreamRelease(stream);
321 #undef IS_INFO_UPDATED
322 }
323
324
325 static void init_test_struct(COMMON_AVI_HEADERS *cah)
326 {
327 memcpy(cah->fh, deffh, sizeof(deffh));
328 cah->mah = defmah;
329 cah->ash0 = defash0;
330 cah->ash1 = defash1;
331 cah->pcmwf = defpcmwf;
332 }
333
334 static void create_avi_file(const COMMON_AVI_HEADERS *cah, char *filename)
335 {
336 HANDLE hFile;
337 DWORD written;
338
339 hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
340
341 ok(hFile != INVALID_HANDLE_VALUE, "Couldn't create file\n");
342
343 WriteFile(hFile, &cah->fh, sizeof(deffh), &written, NULL);
344 WriteFile(hFile, &cah->mah, sizeof(MainAVIHeader), &written, NULL);
345 WriteFile(hFile, streamlist, sizeof(streamlist), &written, NULL);
346 WriteFile(hFile, &cah->ash0, 0x38, &written, NULL);
347 WriteFile(hFile, videostreamformat, sizeof(videostreamformat), &written, NULL);
348 WriteFile(hFile, padding1, sizeof(padding1), &written, NULL);
349 WriteFile(hFile, videopropheader, sizeof(videopropheader), &written, NULL);
350 WriteFile(hFile, &cah->ash1, 0x38, &written, NULL);
351 WriteFile(hFile, audiostreamformat_pre, sizeof(audiostreamformat_pre), &written, NULL);
352 WriteFile(hFile, &cah->pcmwf, sizeof(PCMWAVEFORMAT), &written, NULL);
353 WriteFile(hFile, data, sizeof(data), &written, NULL);
354
355 CloseHandle(hFile);
356 }
357
358 static void test_default_data(void)
359 {
360 COMMON_AVI_HEADERS cah;
361 char filename[MAX_PATH];
362 PAVIFILE pFile;
363 int res;
364 LONG lSize;
365 PAVISTREAM pStream0;
366 PAVISTREAM pStream1;
367 AVISTREAMINFO asi0;
368 AVISTREAMINFO asi1;
369 WAVEFORMATEX wfx;
370
371 GetTempPath(MAX_PATH, filename);
372 strcpy(filename+strlen(filename), testfilename);
373
374 init_test_struct(&cah);
375 create_avi_file(&cah, filename);
376
377 res = AVIFileOpen(&pFile, filename, OF_SHARE_DENY_WRITE, 0L);
378 ok(res != AVIERR_BADFORMAT, "Unable to open file: error1=%u\n", AVIERR_BADFORMAT);
379 ok(res != AVIERR_MEMORY, "Unable to open file: error2=%u\n", AVIERR_MEMORY);
380 ok(res != AVIERR_FILEREAD, "Unable to open file: error3=%u\n", AVIERR_FILEREAD);
381 ok(res != AVIERR_FILEOPEN, "Unable to open file: error4=%u\n", AVIERR_FILEOPEN);
382 ok(res != REGDB_E_CLASSNOTREG, "Unable to open file: error5=%u\n", REGDB_E_CLASSNOTREG);
383 ok(res == 0, "Unable to open file: error=%u\n", res);
384
385 res = AVIFileGetStream(pFile, &pStream0, 0, 0);
386 ok(res == 0, "Unable to open video stream: error=%u\n", res);
387
388 res = AVIFileGetStream(pFile, &pStream1, 0, 1);
389 ok(res == 0, "Unable to open audio stream: error=%u\n", res);
390
391 res = AVIStreamInfo(pStream0, &asi0, sizeof(AVISTREAMINFO));
392 ok(res == 0, "Unable to read stream info: error=%u\n", res);
393
394 res = AVIStreamInfo(pStream1, &asi1, sizeof(AVISTREAMINFO));
395 ok(res == 0, "Unable to read stream info: error=%u\n", res);
396
397 res = AVIStreamReadFormat(pStream0, AVIStreamStart(pStream1), NULL, &lSize);
398 ok(res == 0, "Unable to read format size: error=%u\n", res);
399
400 res = AVIStreamReadFormat(pStream1, AVIStreamStart(pStream1), &wfx, &lSize);
401 ok(res == 0, "Unable to read format: error=%u\n", res);
402
403 ok(asi0.fccType == streamtypeVIDEO, "got 0x%x (expected streamtypeVIDEO)\n", asi0.fccType);
404 ok(asi0.fccHandler == 0x30323449, "got 0x%x (expected 0x30323449)\n", asi0.fccHandler);
405 ok(asi0.dwFlags == 0, "got %u (expected 0)\n", asi0.dwFlags);
406 ok(asi0.wPriority == 0, "got %u (expected 0)\n", asi0.wPriority);
407 ok(asi0.wLanguage == 0, "got %u (expected 0)\n", asi0.wLanguage);
408 ok(asi0.dwScale == 1001, "got %u (expected 1001)\n", asi0.dwScale);
409 ok(asi0.dwRate == 30000, "got %u (expected 30000)\n", asi0.dwRate);
410 ok(asi0.dwStart == 0, "got %u (expected 0)\n", asi0.dwStart);
411 ok(asi0.dwLength == 1, "got %u (expected 1)\n", asi0.dwLength);
412 ok(asi0.dwInitialFrames == 0, "got %u (expected 0)\n", asi0.dwInitialFrames);
413 ok(asi0.dwSuggestedBufferSize == 0, "got %u (expected 0)\n", asi0.dwSuggestedBufferSize);
414 ok(asi0.dwQuality == 0xffffffff, "got 0x%x (expected 0xffffffff)\n", asi0.dwQuality);
415 ok(asi0.dwSampleSize == 0, "got %u (expected 0)\n", asi0.dwSampleSize);
416 ok(asi0.rcFrame.left == 0, "got %u (expected 0)\n", asi0.rcFrame.left);
417 ok(asi0.rcFrame.top == 0, "got %u (expected 0)\n", asi0.rcFrame.top);
418 ok(asi0.rcFrame.right == 8, "got %u (expected 8)\n", asi0.rcFrame.right); /* these are based on the values in the mah and not */
419 ok(asi0.rcFrame.bottom == 6, "got %u (expected 6)\n", asi0.rcFrame.bottom);/* on the ones in the ash which are 0 here */
420 ok(asi0.dwEditCount == 0, "got %u (expected 0)\n", asi0.dwEditCount);
421 ok(asi0.dwFormatChangeCount == 0, "got %u (expected 0)\n", asi0.dwFormatChangeCount);
422
423 ok(asi1.fccType == streamtypeAUDIO, "got 0x%x (expected streamtypeVIDEO)\n", asi1.fccType);
424 ok(asi1.fccHandler == 0x1, "got 0x%x (expected 0x1)\n", asi1.fccHandler);
425 ok(asi1.dwFlags == 0, "got %u (expected 0)\n", asi1.dwFlags);
426 ok(asi1.wPriority == 0, "got %u (expected 0)\n", asi1.wPriority);
427 ok(asi1.wLanguage == 0, "got %u (expected 0)\n", asi1.wLanguage);
428 ok(asi1.dwScale == 1, "got %u (expected 1)\n", asi1.dwScale);
429 ok(asi1.dwRate == 11025, "got %u (expected 11025)\n", asi1.dwRate);
430 ok(asi1.dwStart == 0, "got %u (expected 0)\n", asi1.dwStart);
431 ok(asi1.dwLength == 1637, "got %u (expected 1637)\n", asi1.dwLength);
432 ok(asi1.dwInitialFrames == 0, "got %u (expected 0)\n", asi1.dwInitialFrames);
433 ok(asi1.dwSuggestedBufferSize == 0, "got %u (expected 0)\n", asi1.dwSuggestedBufferSize);
434 ok(asi1.dwQuality == 0xffffffff, "got 0x%x (expected 0xffffffff)\n", asi1.dwQuality);
435 ok(asi1.dwSampleSize == 2, "got %u (expected 2)\n", asi1.dwSampleSize);
436 ok(asi1.rcFrame.left == 0, "got %u (expected 0)\n", asi1.rcFrame.left);
437 ok(asi1.rcFrame.top == 0, "got %u (expected 0)\n", asi1.rcFrame.top);
438 ok(asi1.rcFrame.right == 0, "got %u (expected 0)\n", asi1.rcFrame.right);
439 ok(asi1.rcFrame.bottom == 0, "got %u (expected 0)\n", asi1.rcFrame.bottom);
440 ok(asi1.dwEditCount == 0, "got %u (expected 0)\n", asi1.dwEditCount);
441 ok(asi1.dwFormatChangeCount == 0, "got %u (expected 0)\n", asi1.dwFormatChangeCount);
442
443 ok(wfx.wFormatTag == 1, "got %u (expected 1)\n",wfx.wFormatTag);
444 ok(wfx.nChannels == 2, "got %u (expected 2)\n",wfx.nChannels);
445 ok(wfx.wFormatTag == 1, "got %u (expected 1)\n",wfx.wFormatTag);
446 ok(wfx.nSamplesPerSec == 11025, "got %u (expected 11025)\n",wfx.nSamplesPerSec);
447 ok(wfx.nAvgBytesPerSec == 22050, "got %u (expected 22050)\n",wfx.nAvgBytesPerSec);
448 ok(wfx.nBlockAlign == 2, "got %u (expected 2)\n",wfx.nBlockAlign);
449
450 AVIStreamRelease(pStream0);
451 AVIStreamRelease(pStream1);
452 AVIFileRelease(pFile);
453 ok(DeleteFile(filename) !=0, "Deleting file %s failed\n", filename);
454 }
455
456 static void test_amh_corruption(void)
457 {
458 COMMON_AVI_HEADERS cah;
459 char filename[MAX_PATH];
460 PAVIFILE pFile;
461 int res;
462
463 GetTempPath(MAX_PATH, filename);
464 strcpy(filename+strlen(filename), testfilename);
465
466 /* Make sure only AVI files with the proper headers will be loaded */
467 init_test_struct(&cah);
468 cah.fh[3] = mmioFOURCC('A', 'V', 'i', ' ');
469
470 create_avi_file(&cah, filename);
471 res = AVIFileOpen(&pFile, filename, OF_SHARE_DENY_WRITE, 0L);
472 ok(res != 0, "Able to open file: error=%u\n", res);
473
474 ok(DeleteFile(filename) !=0, "Deleting file %s failed\n", filename);
475 }
476
477 static void test_ash1_corruption(void)
478 {
479 COMMON_AVI_HEADERS cah;
480 char filename[MAX_PATH];
481 PAVIFILE pFile;
482 int res;
483 PAVISTREAM pStream1;
484 AVISTREAMINFO asi1;
485
486 GetTempPath(MAX_PATH, filename);
487 strcpy(filename+strlen(filename), testfilename);
488
489 /* Corrupt the sample size in the audio stream header */
490 init_test_struct(&cah);
491 cah.ash1.dwSampleSize = 0xdeadbeef;
492
493 create_avi_file(&cah, filename);
494
495 res = AVIFileOpen(&pFile, filename, OF_SHARE_DENY_WRITE, 0L);
496 ok(res == 0, "Unable to open file: error=%u\n", res);
497
498 res = AVIFileGetStream(pFile, &pStream1, 0, 1);
499 ok(res == 0, "Unable to open audio stream: error=%u\n", res);
500
501 res = AVIStreamInfo(pStream1, &asi1, sizeof(AVISTREAMINFO));
502 ok(res == 0, "Unable to read stream info: error=%u\n", res);
503
504 /* The result will still be 2, because the value is dynamically replaced with the nBlockAlign
505 value from the stream format header. The next test will prove this */
506 ok(asi1.dwSampleSize == 2, "got %u (expected 2)\n", asi1.dwSampleSize);
507
508 AVIStreamRelease(pStream1);
509 AVIFileRelease(pFile);
510 ok(DeleteFile(filename) !=0, "Deleting file %s failed\n", filename);
511 }
512
513 static void test_ash1_corruption2(void)
514 {
515 COMMON_AVI_HEADERS cah;
516 char filename[MAX_PATH];
517 PAVIFILE pFile;
518 int res;
519 PAVISTREAM pStream1;
520 AVISTREAMINFO asi1;
521
522 GetTempPath(MAX_PATH, filename);
523 strcpy(filename+strlen(filename), testfilename);
524
525 /* Corrupt the block alignment in the audio format header */
526 init_test_struct(&cah);
527 cah.pcmwf.wf.nBlockAlign = 0xdead;
528
529 create_avi_file(&cah, filename);
530
531 res = AVIFileOpen(&pFile, filename, OF_SHARE_DENY_WRITE, 0L);
532 ok(res == 0, "Unable to open file: error=%u\n", res);
533
534 res = AVIFileGetStream(pFile, &pStream1, 0, 1);
535 ok(res == 0, "Unable to open audio stream: error=%u\n", res);
536
537 ok(AVIStreamInfo(pStream1, &asi1, sizeof(AVISTREAMINFO)) == 0, "Unable to read stream info\n");
538
539 /* The result will also be the corrupt value, as explained above. */
540 ok(asi1.dwSampleSize == 0xdead, "got 0x%x (expected 0xdead)\n", asi1.dwSampleSize);
541
542 AVIStreamRelease(pStream1);
543 AVIFileRelease(pFile);
544 ok(DeleteFile(filename) !=0, "Deleting file %s failed\n", filename);
545 }
546
547 /* Outer IUnknown for COM aggregation tests */
548 struct unk_impl {
549 IUnknown IUnknown_iface;
550 LONG ref;
551 IUnknown *inner_unk;
552 };
553
554 static inline struct unk_impl *impl_from_IUnknown(IUnknown *iface)
555 {
556 return CONTAINING_RECORD(iface, struct unk_impl, IUnknown_iface);
557 }
558
559 static HRESULT WINAPI unk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
560 {
561 struct unk_impl *This = impl_from_IUnknown(iface);
562 LONG ref = This->ref;
563 HRESULT hr;
564
565 if (IsEqualGUID(riid, &IID_IUnknown))
566 {
567 *ppv = iface;
568 IUnknown_AddRef(iface);
569 return S_OK;
570 }
571
572 hr = IUnknown_QueryInterface(This->inner_unk, riid, ppv);
573 if (hr == S_OK)
574 {
575 trace("Working around COM aggregation ref counting bug\n");
576 ok(ref == This->ref, "Outer ref count expected %d got %d\n", ref, This->ref);
577 IUnknown_AddRef((IUnknown*)*ppv);
578 ref = IUnknown_Release(This->inner_unk);
579 ok(ref == 1, "Inner ref count expected 1 got %d\n", ref);
580 }
581
582 return hr;
583 }
584
585 static ULONG WINAPI unk_AddRef(IUnknown *iface)
586 {
587 struct unk_impl *This = impl_from_IUnknown(iface);
588
589 return InterlockedIncrement(&This->ref);
590 }
591
592 static ULONG WINAPI unk_Release(IUnknown *iface)
593 {
594 struct unk_impl *This = impl_from_IUnknown(iface);
595
596 return InterlockedDecrement(&This->ref);
597 }
598
599 static const IUnknownVtbl unk_vtbl =
600 {
601 unk_QueryInterface,
602 unk_AddRef,
603 unk_Release
604 };
605
606 static void test_COM(void)
607 {
608 struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
609 IAVIFile *avif = NULL;
610 IPersistFile *pf;
611 IUnknown *unk;
612 LONG refcount;
613 HRESULT hr;
614
615 /* COM aggregation */
616 hr = CoCreateInstance(&CLSID_AVIFile, &unk_obj.IUnknown_iface, CLSCTX_INPROC_SERVER,
617 &IID_IUnknown, (void**)&unk_obj.inner_unk);
618 ok(hr == S_OK, "COM aggregation failed: %08x, expected S_OK\n", hr);
619 hr = IUnknown_QueryInterface(&unk_obj.IUnknown_iface, &IID_IAVIFile, (void**)&avif);
620 ok(hr == S_OK, "QueryInterface for IID_IAVIFile failed: %08x\n", hr);
621 refcount = IAVIFile_AddRef(avif);
622 ok(refcount == unk_obj.ref, "AVIFile just pretends to support COM aggregation\n");
623 refcount = IAVIFile_Release(avif);
624 ok(refcount == unk_obj.ref, "AVIFile just pretends to support COM aggregation\n");
625 hr = IAVIFile_QueryInterface(avif, &IID_IPersistFile, (void**)&pf);
626 ok(hr == S_OK, "QueryInterface for IID_IPersistFile failed: %08x\n", hr);
627 refcount = IPersistFile_Release(pf);
628 ok(refcount == unk_obj.ref, "AVIFile just pretends to support COM aggregation\n");
629 refcount = IAVIFile_Release(avif);
630 ok(refcount == 19, "Outer ref count should be back at 19 but is %d\n", refcount);
631 refcount = IUnknown_Release(unk_obj.inner_unk);
632 ok(refcount == 0, "Inner ref count should be 0 but is %u\n", refcount);
633
634 /* Invalid RIID */
635 hr = CoCreateInstance(&CLSID_AVIFile, NULL, CLSCTX_INPROC_SERVER, &IID_IAVIStream,
636 (void**)&avif);
637 ok(hr == E_NOINTERFACE, "AVIFile create failed: %08x, expected E_NOINTERFACE\n", hr);
638
639 /* Same refcount */
640 hr = CoCreateInstance(&CLSID_AVIFile, NULL, CLSCTX_INPROC_SERVER, &IID_IAVIFile, (void**)&avif);
641 ok(hr == S_OK, "AVIFile create failed: %08x, expected S_OK\n", hr);
642 refcount = IAVIFile_AddRef(avif);
643 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
644 hr = IAVIFile_QueryInterface(avif, &IID_IUnknown, (void**)&unk);
645 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
646 refcount = IUnknown_AddRef(unk);
647 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
648 hr = IAVIFile_QueryInterface(avif, &IID_IPersistFile, (void**)&pf);
649 ok(hr == S_OK, "QueryInterface for IID_IPersistFile failed: %08x\n", hr);
650 refcount = IPersistFile_AddRef(pf);
651 ok(refcount == 6, "refcount == %u, expected 6\n", refcount);
652
653 while (IAVIFile_Release(avif));
654 }
655
656 START_TEST(api)
657 {
658
659 AVIFileInit();
660 test_EditStreamSetInfo();
661 test_AVISaveOptions();
662 test_default_data();
663 test_amh_corruption();
664 test_ash1_corruption();
665 test_ash1_corruption2();
666 test_COM();
667 AVIFileExit();
668
669 }