wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / qmgr / tests / job.c
blobf972547e9d27f5b8113ea4d44a85c6203a303b71
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 WCHAR test_remotePathA[MAX_PATH];
32 static WCHAR test_remotePathB[MAX_PATH];
33 static WCHAR test_localPathA[MAX_PATH];
34 static WCHAR test_localPathB[MAX_PATH];
35 static IBackgroundCopyManager *test_manager;
36 static IBackgroundCopyJob *test_job;
37 static GUID test_jobId;
38 static BG_JOB_TYPE test_type;
40 static VOID init_paths(void)
42 WCHAR tmpDir[MAX_PATH];
43 WCHAR prefix[] = {'q', 'm', 'g', 'r', 0};
45 GetTempPathW(MAX_PATH, tmpDir);
47 GetTempFileNameW(tmpDir, prefix, 0, test_localPathA);
48 GetTempFileNameW(tmpDir, prefix, 0, test_localPathB);
49 GetTempFileNameW(tmpDir, prefix, 0, test_remotePathA);
50 GetTempFileNameW(tmpDir, prefix, 0, test_remotePathB);
53 /* Generic test setup */
54 static BOOL setup(void)
56 HRESULT hres;
58 test_manager = NULL;
59 test_job = NULL;
60 memset(&test_jobId, 0, sizeof test_jobId);
61 test_type = BG_JOB_TYPE_DOWNLOAD;
63 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
64 CLSCTX_LOCAL_SERVER,
65 &IID_IBackgroundCopyManager,
66 (void **) &test_manager);
67 if(hres != S_OK)
68 return FALSE;
70 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
71 test_type, &test_jobId, &test_job);
72 if(hres != S_OK)
74 IBackgroundCopyManager_Release(test_manager);
75 return FALSE;
78 return TRUE;
81 /* Generic test cleanup */
82 static void teardown(void)
84 IBackgroundCopyJob_Release(test_job);
85 IBackgroundCopyManager_Release(test_manager);
88 /* FIXME: Remove when Wine has implemented this */
89 DEFINE_GUID(CLSID_BackgroundCopyManager2_0, 0x6d18ad12, 0xbde3, 0x4393, 0xb3,0x11, 0x09,0x9c,0x34,0x6e,0x6d,0xf9);
91 static BOOL check_bits20(void)
93 HRESULT hres;
94 IBackgroundCopyManager *manager;
95 BOOL ret = TRUE;
97 hres = CoCreateInstance(&CLSID_BackgroundCopyManager2_0, NULL,
98 CLSCTX_LOCAL_SERVER,
99 &IID_IBackgroundCopyManager,
100 (void **) &manager);
102 if (hres == REGDB_E_CLASSNOTREG)
104 ret = FALSE;
106 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
108 * Remove when Wine is fixed
110 if (setup())
112 HRESULT hres2;
114 hres2 = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
115 test_localPathA);
116 if (hres2 == S_OK)
118 trace("Running on Wine, claim 2.0 is present\n");
119 ret = TRUE;
121 teardown();
125 if (manager)
126 IBackgroundCopyManager_Release(manager);
128 return ret;
131 /* Test that the jobId is properly set */
132 static void test_GetId(void)
134 HRESULT hres;
135 GUID tmpId;
137 hres = IBackgroundCopyJob_GetId(test_job, &tmpId);
138 ok(hres == S_OK, "GetId failed: %08x\n", hres);
139 if(hres != S_OK)
141 skip("Unable to get ID of test_job.\n");
142 return;
144 ok(memcmp(&tmpId, &test_jobId, sizeof tmpId) == 0, "Got incorrect GUID\n");
147 /* Test that the type is properly set */
148 static void test_GetType(void)
150 HRESULT hres;
151 BG_JOB_TYPE type;
153 hres = IBackgroundCopyJob_GetType(test_job, &type);
154 ok(hres == S_OK, "GetType failed: %08x\n", hres);
155 if(hres != S_OK)
157 skip("Unable to get type of test_job.\n");
158 return;
160 ok(type == test_type, "Got incorrect type\n");
163 /* Test that the display name is properly set */
164 static void test_GetName(void)
166 HRESULT hres;
167 LPWSTR displayName;
169 hres = IBackgroundCopyJob_GetDisplayName(test_job, &displayName);
170 ok(hres == S_OK, "GetName failed: %08x\n", hres);
171 if(hres != S_OK)
173 skip("Unable to get display name of test_job.\n");
174 return;
176 ok(lstrcmpW(displayName, test_displayName) == 0, "Got incorrect type\n");
177 CoTaskMemFree(displayName);
180 /* Test adding a file */
181 static void test_AddFile(void)
183 HRESULT hres;
185 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
186 test_localPathA);
187 ok(hres == S_OK, "First call to AddFile failed: 0x%08x\n", hres);
188 if (hres != S_OK)
190 skip("Unable to add first file to job\n");
191 return;
194 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathB,
195 test_localPathB);
196 ok(hres == S_OK, "Second call to AddFile failed: 0x%08x\n", hres);
199 /* Test adding a set of files */
200 static void test_AddFileSet(void)
202 HRESULT hres;
203 BG_FILE_INFO files[2] =
205 {test_remotePathA, test_localPathA},
206 {test_remotePathB, test_localPathB}
208 hres = IBackgroundCopyJob_AddFileSet(test_job, 2, files);
209 ok(hres == S_OK, "AddFileSet failed: 0x%08x\n", hres);
212 /* Test creation of a job enumerator */
213 static void test_EnumFiles(void)
215 HRESULT hres;
216 IEnumBackgroundCopyFiles *enumFiles;
217 ULONG res;
219 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
220 test_localPathA);
221 if (hres != S_OK)
223 skip("Unable to add file to job\n");
224 return;
227 hres = IBackgroundCopyJob_EnumFiles(test_job, &enumFiles);
228 ok(hres == S_OK, "EnumFiles failed: 0x%08x\n", hres);
229 if(hres != S_OK)
231 skip("Unable to create file enumerator.\n");
232 return;
235 res = IEnumBackgroundCopyFiles_Release(enumFiles);
236 ok(res == 0, "Bad ref count on release: %u\n", res);
239 /* Test getting job progress */
240 static void test_GetProgress_preTransfer(void)
242 HRESULT hres;
243 BG_JOB_PROGRESS progress;
245 hres = IBackgroundCopyJob_GetProgress(test_job, &progress);
246 ok(hres == S_OK, "GetProgress failed: 0x%08x\n", hres);
247 if (hres != S_OK)
249 skip("Unable to get job progress\n");
250 teardown();
251 return;
254 ok(progress.BytesTotal == 0, "Incorrect BytesTotal: %x%08x\n",
255 (DWORD)(progress.BytesTotal >> 32), (DWORD)progress.BytesTotal);
256 ok(progress.BytesTransferred == 0, "Incorrect BytesTransferred: %x%08x\n",
257 (DWORD)(progress.BytesTransferred >> 32), (DWORD)progress.BytesTransferred);
258 ok(progress.FilesTotal == 0, "Incorrect FilesTotal: %u\n", progress.FilesTotal);
259 ok(progress.FilesTransferred == 0, "Incorrect FilesTransferred %u\n", progress.FilesTransferred);
262 /* Test getting job state */
263 static void test_GetState(void)
265 HRESULT hres;
266 BG_JOB_STATE state;
268 state = BG_JOB_STATE_ERROR;
269 hres = IBackgroundCopyJob_GetState(test_job, &state);
270 ok(hres == S_OK, "GetState failed: 0x%08x\n", hres);
271 if (hres != S_OK)
273 skip("Unable to get job state\n");
274 return;
276 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
279 /* Test resuming a job */
280 static void test_ResumeEmpty(void)
282 HRESULT hres;
283 BG_JOB_STATE state;
285 hres = IBackgroundCopyJob_Resume(test_job);
286 ok(hres == BG_E_EMPTY, "Resume failed to return BG_E_EMPTY error: 0x%08x\n", hres);
287 if (hres != BG_E_EMPTY)
289 skip("Failed calling resume job\n");
290 return;
293 state = BG_JOB_STATE_ERROR;
294 hres = IBackgroundCopyJob_GetState(test_job, &state);
295 if (hres != S_OK)
297 skip("Unable to get job state\n");
298 return;
300 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
303 static void makeFile(WCHAR *name, const char *contents)
305 HANDLE file;
306 DWORD w, len = strlen(contents);
308 DeleteFileW(name);
309 file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
310 FILE_ATTRIBUTE_NORMAL, NULL);
311 ok(file != INVALID_HANDLE_VALUE, "CreateFile\n");
312 ok(WriteFile(file, contents, len, &w, NULL), "WriteFile\n");
313 CloseHandle(file);
316 static void compareFiles(WCHAR *n1, WCHAR *n2)
318 char b1[256];
319 char b2[256];
320 DWORD s1, s2;
321 HANDLE f1, f2;
323 f1 = CreateFileW(n1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
324 FILE_ATTRIBUTE_NORMAL, NULL);
325 ok(f1 != INVALID_HANDLE_VALUE, "CreateFile\n");
327 f2 = CreateFileW(n2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
328 FILE_ATTRIBUTE_NORMAL, NULL);
329 ok(f2 != INVALID_HANDLE_VALUE, "CreateFile\n");
331 /* Neither of these files is very big */
332 ok(ReadFile(f1, b1, sizeof b1, &s1, NULL), "ReadFile\n");
333 ok(ReadFile(f2, b2, sizeof b2, &s2, NULL), "ReadFile\n");
335 CloseHandle(f1);
336 CloseHandle(f2);
338 ok(s1 == s2, "Files differ in length\n");
339 ok(memcmp(b1, b2, s1) == 0, "Files differ in contents\n");
342 /* Test a complete transfer for local files */
343 static void test_CompleteLocal(void)
345 static const int timeout_sec = 30;
346 HRESULT hres;
347 BG_JOB_STATE state;
348 int i;
350 DeleteFileW(test_localPathA);
351 DeleteFileW(test_localPathB);
352 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
353 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
355 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
356 test_localPathA);
357 if (hres != S_OK)
359 skip("Unable to add file to job\n");
360 return;
363 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathB,
364 test_localPathB);
365 if (hres != S_OK)
367 skip("Unable to add file to job\n");
368 return;
371 hres = IBackgroundCopyJob_Resume(test_job);
372 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
374 for (i = 0; i < timeout_sec; ++i)
376 hres = IBackgroundCopyJob_GetState(test_job, &state);
377 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
378 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
379 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
380 "Bad state: %d\n", state);
381 if (state == BG_JOB_STATE_TRANSFERRED)
382 break;
383 Sleep(1000);
386 ok(i < timeout_sec, "BITS jobs timed out\n");
387 hres = IBackgroundCopyJob_Complete(test_job);
388 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
389 hres = IBackgroundCopyJob_GetState(test_job, &state);
390 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
391 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
393 compareFiles(test_remotePathA, test_localPathA);
394 compareFiles(test_remotePathB, test_localPathB);
396 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
397 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
398 DeleteFileW(test_localPathA);
399 DeleteFileW(test_localPathB);
402 /* Test a complete transfer for local files */
403 static void test_CompleteLocalURL(void)
405 static const WCHAR prot[] = {'f','i','l','e',':','/','/', 0};
406 static const int timeout_sec = 30;
407 WCHAR *urlA, *urlB;
408 HRESULT hres;
409 BG_JOB_STATE state;
410 int i;
412 DeleteFileW(test_localPathA);
413 DeleteFileW(test_localPathB);
414 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
415 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
417 urlA = HeapAlloc(GetProcessHeap(), 0,
418 (7 + lstrlenW(test_remotePathA) + 1) * sizeof urlA[0]);
419 urlB = HeapAlloc(GetProcessHeap(), 0,
420 (7 + lstrlenW(test_remotePathB) + 1) * sizeof urlB[0]);
421 if (!urlA || !urlB)
423 skip("Unable to allocate memory for URLs\n");
424 HeapFree(GetProcessHeap(), 0, urlA);
425 HeapFree(GetProcessHeap(), 0, urlB);
426 return;
429 lstrcpyW(urlA, prot);
430 lstrcatW(urlA, test_remotePathA);
431 lstrcpyW(urlB, prot);
432 lstrcatW(urlB, test_remotePathB);
434 hres = IBackgroundCopyJob_AddFile(test_job, urlA, test_localPathA);
435 if (hres != S_OK)
437 skip("Unable to add file to job\n");
438 HeapFree(GetProcessHeap(), 0, urlA);
439 HeapFree(GetProcessHeap(), 0, urlB);
440 return;
443 hres = IBackgroundCopyJob_AddFile(test_job, urlB, test_localPathB);
444 if (hres != S_OK)
446 skip("Unable to add file to job\n");
447 HeapFree(GetProcessHeap(), 0, urlA);
448 HeapFree(GetProcessHeap(), 0, urlB);
449 return;
452 hres = IBackgroundCopyJob_Resume(test_job);
453 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
455 for (i = 0; i < timeout_sec; ++i)
457 hres = IBackgroundCopyJob_GetState(test_job, &state);
458 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
459 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
460 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
461 "Bad state: %d\n", state);
462 if (state == BG_JOB_STATE_TRANSFERRED)
463 break;
464 Sleep(1000);
467 ok(i < timeout_sec, "BITS jobs timed out\n");
468 hres = IBackgroundCopyJob_Complete(test_job);
469 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
470 hres = IBackgroundCopyJob_GetState(test_job, &state);
471 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
472 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
474 compareFiles(test_remotePathA, test_localPathA);
475 compareFiles(test_remotePathB, test_localPathB);
477 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
478 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
479 DeleteFileW(test_localPathA);
480 DeleteFileW(test_localPathB);
482 HeapFree(GetProcessHeap(), 0, urlA);
483 HeapFree(GetProcessHeap(), 0, urlB);
486 typedef void (*test_t)(void);
488 START_TEST(job)
490 static const test_t tests[] = {
491 test_GetId,
492 test_GetType,
493 test_GetName,
494 test_GetProgress_preTransfer,
495 test_GetState,
496 test_ResumeEmpty,
499 static const test_t tests_bits20[] = {
500 test_AddFile,
501 test_AddFileSet,
502 test_EnumFiles,
503 test_CompleteLocal,
504 test_CompleteLocalURL,
507 const test_t *test;
509 init_paths();
511 CoInitialize(NULL);
513 for (test = tests; *test; ++test)
515 /* Keep state separate between tests. */
516 if (!setup())
518 skip("Unable to setup test\n");
519 break;
521 (*test)();
522 teardown();
525 if (check_bits20())
527 for (test = tests_bits20; *test; ++test)
529 /* Keep state separate between tests. */
530 if (!setup())
532 skip("Unable to setup test\n");
533 break;
535 (*test)();
536 teardown();
539 else
541 win_skip("Tests need BITS 2.0 or higher\n");
544 CoUninitialize();