configure: Build with -Wstrict-prototypes if supported.
[wine/multimedia.git] / dlls / qmgr / tests / job.c
blobd0e2306bfc0055350e7d1c203fcc29d183eccd57
1 /*
2 * Unit test suite for Background Copy Job Interface
4 * Copyright 2007 Google (Roy Shea)
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.
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.
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
21 #include <stdio.h>
23 #define COBJMACROS
25 #include "wine/test.h"
26 #include "bits.h"
27 #include "initguid.h"
29 /* Globals used by many tests */
30 static const WCHAR test_displayName[] = {'T', 'e', 's', 't', 0};
31 static const WCHAR test_remoteNameA[] = {'r','e','m','o','t','e','A', 0};
32 static const WCHAR test_remoteNameB[] = {'r','e','m','o','t','e','B', 0};
33 static const WCHAR test_localNameA[] = {'l','o','c','a','l','A', 0};
34 static const WCHAR test_localNameB[] = {'l','o','c','a','l','B', 0};
35 static WCHAR *test_currentDir;
36 static WCHAR *test_remotePathA;
37 static WCHAR *test_remotePathB;
38 static WCHAR *test_localPathA;
39 static WCHAR *test_localPathB;
40 static IBackgroundCopyManager *test_manager;
41 static IBackgroundCopyJob *test_job;
42 static GUID test_jobId;
43 static BG_JOB_TYPE test_type;
45 static BOOL init_paths(void)
47 static const WCHAR format[] = {'%','s','\\','%','s', 0};
48 DWORD n;
50 n = GetCurrentDirectoryW(0, NULL);
51 if (n == 0)
53 skip("Couldn't get current directory size\n");
54 return FALSE;
57 test_currentDir = HeapAlloc(GetProcessHeap(), 0, n * sizeof(WCHAR));
58 test_localPathA
59 = HeapAlloc(GetProcessHeap(), 0,
60 (n + 1 + lstrlenW(test_localNameA)) * sizeof(WCHAR));
61 test_localPathB
62 = HeapAlloc(GetProcessHeap(), 0,
63 (n + 1 + lstrlenW(test_localNameB)) * sizeof(WCHAR));
64 test_remotePathA
65 = HeapAlloc(GetProcessHeap(), 0,
66 (n + 1 + lstrlenW(test_remoteNameA)) * sizeof(WCHAR));
67 test_remotePathB
68 = HeapAlloc(GetProcessHeap(), 0,
69 (n + 1 + lstrlenW(test_remoteNameB)) * sizeof(WCHAR));
71 if (!test_currentDir || !test_localPathA || !test_localPathB
72 || !test_remotePathA || !test_remotePathB)
74 skip("Couldn't allocate memory for full paths\n");
75 return FALSE;
78 if (GetCurrentDirectoryW(n, test_currentDir) != n - 1)
80 skip("Couldn't get current directory\n");
81 return FALSE;
84 wsprintfW(test_localPathA, format, test_currentDir, test_localNameA);
85 wsprintfW(test_localPathB, format, test_currentDir, test_localNameB);
86 wsprintfW(test_remotePathA, format, test_currentDir, test_remoteNameA);
87 wsprintfW(test_remotePathB, format, test_currentDir, test_remoteNameB);
89 return TRUE;
92 /* Generic test setup */
93 static BOOL setup(void)
95 HRESULT hres;
97 test_manager = NULL;
98 test_job = NULL;
99 memset(&test_jobId, 0, sizeof test_jobId);
100 test_type = BG_JOB_TYPE_DOWNLOAD;
102 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
103 CLSCTX_LOCAL_SERVER,
104 &IID_IBackgroundCopyManager,
105 (void **) &test_manager);
106 if(hres != S_OK)
107 return FALSE;
109 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
110 test_type, &test_jobId, &test_job);
111 if(hres != S_OK)
113 IBackgroundCopyManager_Release(test_manager);
114 return FALSE;
117 return TRUE;
120 /* Generic test cleanup */
121 static void teardown(void)
123 IBackgroundCopyJob_Release(test_job);
124 IBackgroundCopyManager_Release(test_manager);
127 /* FIXME: Remove when Wine has implemented this */
128 DEFINE_GUID(CLSID_BackgroundCopyManager2_0, 0x6d18ad12, 0xbde3, 0x4393, 0xb3,0x11, 0x09,0x9c,0x34,0x6e,0x6d,0xf9);
130 static BOOL check_bits20(void)
132 HRESULT hres;
133 IBackgroundCopyManager *manager;
134 BOOL ret = TRUE;
136 hres = CoCreateInstance(&CLSID_BackgroundCopyManager2_0, NULL,
137 CLSCTX_LOCAL_SERVER,
138 &IID_IBackgroundCopyManager,
139 (void **) &manager);
141 if (hres == REGDB_E_CLASSNOTREG)
143 ret = FALSE;
145 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
147 * Remove when Wine is fixed
149 if (setup())
151 HRESULT hres2;
153 hres2 = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
154 test_localPathA);
155 if (hres2 == S_OK)
157 trace("Running on Wine, claim 2.0 is present\n");
158 ret = TRUE;
160 teardown();
164 if (manager)
165 IBackgroundCopyManager_Release(manager);
167 return ret;
170 /* Test that the jobId is properly set */
171 static void test_GetId(void)
173 HRESULT hres;
174 GUID tmpId;
176 hres = IBackgroundCopyJob_GetId(test_job, &tmpId);
177 ok(hres == S_OK, "GetId failed: %08x\n", hres);
178 if(hres != S_OK)
180 skip("Unable to get ID of test_job.\n");
181 return;
183 ok(memcmp(&tmpId, &test_jobId, sizeof tmpId) == 0, "Got incorrect GUID\n");
186 /* Test that the type is properly set */
187 static void test_GetType(void)
189 HRESULT hres;
190 BG_JOB_TYPE type;
192 hres = IBackgroundCopyJob_GetType(test_job, &type);
193 ok(hres == S_OK, "GetType failed: %08x\n", hres);
194 if(hres != S_OK)
196 skip("Unable to get type of test_job.\n");
197 return;
199 ok(type == test_type, "Got incorrect type\n");
202 /* Test that the display name is properly set */
203 static void test_GetName(void)
205 HRESULT hres;
206 LPWSTR displayName;
208 hres = IBackgroundCopyJob_GetDisplayName(test_job, &displayName);
209 ok(hres == S_OK, "GetName failed: %08x\n", hres);
210 if(hres != S_OK)
212 skip("Unable to get display name of test_job.\n");
213 return;
215 ok(lstrcmpW(displayName, test_displayName) == 0, "Got incorrect type\n");
216 CoTaskMemFree(displayName);
219 /* Test adding a file */
220 static void test_AddFile(void)
222 HRESULT hres;
224 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
225 test_localPathA);
226 ok(hres == S_OK, "First call to AddFile failed: 0x%08x\n", hres);
227 if (hres != S_OK)
229 skip("Unable to add first file to job\n");
230 return;
233 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathB,
234 test_localPathB);
235 ok(hres == S_OK, "Second call to AddFile failed: 0x%08x\n", hres);
238 /* Test adding a set of files */
239 static void test_AddFileSet(void)
241 HRESULT hres;
242 BG_FILE_INFO files[2] =
244 {test_remotePathA, test_localPathA},
245 {test_remotePathB, test_localPathB}
247 hres = IBackgroundCopyJob_AddFileSet(test_job, 2, files);
248 ok(hres == S_OK, "AddFileSet failed: 0x%08x\n", hres);
251 /* Test creation of a job enumerator */
252 static void test_EnumFiles(void)
254 HRESULT hres;
255 IEnumBackgroundCopyFiles *enumFiles;
256 ULONG res;
258 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
259 test_localPathA);
260 if (hres != S_OK)
262 skip("Unable to add file to job\n");
263 return;
266 hres = IBackgroundCopyJob_EnumFiles(test_job, &enumFiles);
267 ok(hres == S_OK, "EnumFiles failed: 0x%08x\n", hres);
268 if(hres != S_OK)
270 skip("Unable to create file enumerator.\n");
271 return;
274 res = IEnumBackgroundCopyFiles_Release(enumFiles);
275 ok(res == 0, "Bad ref count on release: %u\n", res);
278 /* Test getting job progress */
279 static void test_GetProgress_preTransfer(void)
281 HRESULT hres;
282 BG_JOB_PROGRESS progress;
284 hres = IBackgroundCopyJob_GetProgress(test_job, &progress);
285 ok(hres == S_OK, "GetProgress failed: 0x%08x\n", hres);
286 if (hres != S_OK)
288 skip("Unable to get job progress\n");
289 teardown();
290 return;
293 ok(progress.BytesTotal == 0, "Incorrect BytesTotal: %x%08x\n",
294 (DWORD)(progress.BytesTotal >> 32), (DWORD)progress.BytesTotal);
295 ok(progress.BytesTransferred == 0, "Incorrect BytesTransferred: %x%08x\n",
296 (DWORD)(progress.BytesTransferred >> 32), (DWORD)progress.BytesTransferred);
297 ok(progress.FilesTotal == 0, "Incorrect FilesTotal: %u\n", progress.FilesTotal);
298 ok(progress.FilesTransferred == 0, "Incorrect FilesTransferred %u\n", progress.FilesTransferred);
301 /* Test getting job state */
302 static void test_GetState(void)
304 HRESULT hres;
305 BG_JOB_STATE state;
307 state = BG_JOB_STATE_ERROR;
308 hres = IBackgroundCopyJob_GetState(test_job, &state);
309 ok(hres == S_OK, "GetState failed: 0x%08x\n", hres);
310 if (hres != S_OK)
312 skip("Unable to get job state\n");
313 return;
315 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
318 /* Test resuming a job */
319 static void test_ResumeEmpty(void)
321 HRESULT hres;
322 BG_JOB_STATE state;
324 hres = IBackgroundCopyJob_Resume(test_job);
325 ok(hres == BG_E_EMPTY, "Resume failed to return BG_E_EMPTY error: 0x%08x\n", hres);
326 if (hres != BG_E_EMPTY)
328 skip("Failed calling resume job\n");
329 return;
332 state = BG_JOB_STATE_ERROR;
333 hres = IBackgroundCopyJob_GetState(test_job, &state);
334 if (hres != S_OK)
336 skip("Unable to get job state\n");
337 return;
339 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
342 static void makeFile(WCHAR *name, const char *contents)
344 HANDLE file;
345 DWORD w, len = strlen(contents);
347 DeleteFileW(name);
348 file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
349 FILE_ATTRIBUTE_NORMAL, NULL);
350 ok(file != INVALID_HANDLE_VALUE, "CreateFile\n");
351 ok(WriteFile(file, contents, len, &w, NULL), "WriteFile\n");
352 CloseHandle(file);
355 static void compareFiles(WCHAR *n1, WCHAR *n2)
357 char b1[256];
358 char b2[256];
359 DWORD s1, s2;
360 HANDLE f1, f2;
362 f1 = CreateFileW(n1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
363 FILE_ATTRIBUTE_NORMAL, NULL);
364 ok(f1 != INVALID_HANDLE_VALUE, "CreateFile\n");
366 f2 = CreateFileW(n2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
367 FILE_ATTRIBUTE_NORMAL, NULL);
368 ok(f2 != INVALID_HANDLE_VALUE, "CreateFile\n");
370 /* Neither of these files is very big */
371 ok(ReadFile(f1, b1, sizeof b1, &s1, NULL), "ReadFile\n");
372 ok(ReadFile(f2, b2, sizeof b2, &s2, NULL), "ReadFile\n");
374 CloseHandle(f1);
375 CloseHandle(f2);
377 ok(s1 == s2, "Files differ in length\n");
378 ok(memcmp(b1, b2, s1) == 0, "Files differ in contents\n");
381 /* Test a complete transfer for local files */
382 static void test_CompleteLocal(void)
384 static const int timeout_sec = 30;
385 HRESULT hres;
386 BG_JOB_STATE state;
387 int i;
389 DeleteFileW(test_localPathA);
390 DeleteFileW(test_localPathB);
391 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
392 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
394 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
395 test_localPathA);
396 if (hres != S_OK)
398 skip("Unable to add file to job\n");
399 return;
402 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathB,
403 test_localPathB);
404 if (hres != S_OK)
406 skip("Unable to add file to job\n");
407 return;
410 hres = IBackgroundCopyJob_Resume(test_job);
411 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
413 for (i = 0; i < timeout_sec; ++i)
415 hres = IBackgroundCopyJob_GetState(test_job, &state);
416 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
417 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
418 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
419 "Bad state: %d\n", state);
420 if (state == BG_JOB_STATE_TRANSFERRED)
421 break;
422 Sleep(1000);
425 ok(i < timeout_sec, "BITS jobs timed out\n");
426 hres = IBackgroundCopyJob_Complete(test_job);
427 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
428 hres = IBackgroundCopyJob_GetState(test_job, &state);
429 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
430 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
432 compareFiles(test_remotePathA, test_localPathA);
433 compareFiles(test_remotePathB, test_localPathB);
435 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
436 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
437 DeleteFileW(test_localPathA);
438 DeleteFileW(test_localPathB);
441 /* Test a complete transfer for local files */
442 static void test_CompleteLocalURL(void)
444 static const WCHAR prot[] = {'f','i','l','e',':','/','/', 0};
445 static const int timeout_sec = 30;
446 WCHAR *urlA, *urlB;
447 HRESULT hres;
448 BG_JOB_STATE state;
449 int i;
451 DeleteFileW(test_localPathA);
452 DeleteFileW(test_localPathB);
453 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
454 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
456 urlA = HeapAlloc(GetProcessHeap(), 0,
457 (7 + lstrlenW(test_remotePathA) + 1) * sizeof urlA[0]);
458 urlB = HeapAlloc(GetProcessHeap(), 0,
459 (7 + lstrlenW(test_remotePathB) + 1) * sizeof urlB[0]);
460 if (!urlA || !urlB)
462 skip("Unable to allocate memory for URLs\n");
463 HeapFree(GetProcessHeap(), 0, urlA);
464 HeapFree(GetProcessHeap(), 0, urlB);
465 return;
468 lstrcpyW(urlA, prot);
469 lstrcatW(urlA, test_remotePathA);
470 lstrcpyW(urlB, prot);
471 lstrcatW(urlB, test_remotePathB);
473 hres = IBackgroundCopyJob_AddFile(test_job, urlA, test_localPathA);
474 if (hres != S_OK)
476 skip("Unable to add file to job\n");
477 HeapFree(GetProcessHeap(), 0, urlA);
478 HeapFree(GetProcessHeap(), 0, urlB);
479 return;
482 hres = IBackgroundCopyJob_AddFile(test_job, urlB, test_localPathB);
483 if (hres != S_OK)
485 skip("Unable to add file to job\n");
486 HeapFree(GetProcessHeap(), 0, urlA);
487 HeapFree(GetProcessHeap(), 0, urlB);
488 return;
491 hres = IBackgroundCopyJob_Resume(test_job);
492 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
494 for (i = 0; i < timeout_sec; ++i)
496 hres = IBackgroundCopyJob_GetState(test_job, &state);
497 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
498 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
499 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
500 "Bad state: %d\n", state);
501 if (state == BG_JOB_STATE_TRANSFERRED)
502 break;
503 Sleep(1000);
506 ok(i < timeout_sec, "BITS jobs timed out\n");
507 hres = IBackgroundCopyJob_Complete(test_job);
508 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
509 hres = IBackgroundCopyJob_GetState(test_job, &state);
510 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
511 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
513 compareFiles(test_remotePathA, test_localPathA);
514 compareFiles(test_remotePathB, test_localPathB);
516 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
517 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
518 DeleteFileW(test_localPathA);
519 DeleteFileW(test_localPathB);
521 HeapFree(GetProcessHeap(), 0, urlA);
522 HeapFree(GetProcessHeap(), 0, urlB);
525 typedef void (*test_t)(void);
527 START_TEST(job)
529 static const test_t tests[] = {
530 test_GetId,
531 test_GetType,
532 test_GetName,
533 test_GetProgress_preTransfer,
534 test_GetState,
535 test_ResumeEmpty,
538 static const test_t tests_bits20[] = {
539 test_AddFile,
540 test_AddFileSet,
541 test_EnumFiles,
542 test_CompleteLocal,
543 test_CompleteLocalURL,
546 const test_t *test;
548 if (!init_paths())
549 return;
551 CoInitialize(NULL);
553 for (test = tests; *test; ++test)
555 /* Keep state separate between tests. */
556 if (!setup())
558 skip("Unable to setup test\n");
559 break;
561 (*test)();
562 teardown();
565 if (check_bits20())
567 for (test = tests_bits20; *test; ++test)
569 /* Keep state separate between tests. */
570 if (!setup())
572 skip("Unable to setup test\n");
573 break;
575 (*test)();
576 teardown();
579 else
581 win_skip("Tests need BITS 2.0 or higher\n");
584 CoUninitialize();