rsaenh: Use the remaining hash data as salt if requested.
[wine.git] / dlls / qmgr / tests / job.c
blobd42e3a5c9c2843fd4d1a8db35653c368e1d2b57f
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 HRESULT test_create_manager(void)
42 HRESULT hres;
43 IBackgroundCopyManager *manager = NULL;
45 /* Creating BITS instance */
46 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
47 &IID_IBackgroundCopyManager, (void **) &manager);
49 if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
50 win_skip("Needed Service is disabled\n");
51 return hres;
54 if (hres == S_OK)
55 IBackgroundCopyManager_Release(manager);
57 return hres;
60 static void init_paths(void)
62 WCHAR tmpDir[MAX_PATH];
63 WCHAR prefix[] = {'q', 'm', 'g', 'r', 0};
65 GetTempPathW(MAX_PATH, tmpDir);
67 GetTempFileNameW(tmpDir, prefix, 0, test_localPathA);
68 GetTempFileNameW(tmpDir, prefix, 0, test_localPathB);
69 GetTempFileNameW(tmpDir, prefix, 0, test_remotePathA);
70 GetTempFileNameW(tmpDir, prefix, 0, test_remotePathB);
73 /* Generic test setup */
74 static BOOL setup(void)
76 HRESULT hres;
78 test_manager = NULL;
79 test_job = NULL;
80 memset(&test_jobId, 0, sizeof test_jobId);
81 test_type = BG_JOB_TYPE_DOWNLOAD;
83 hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
84 CLSCTX_LOCAL_SERVER,
85 &IID_IBackgroundCopyManager,
86 (void **) &test_manager);
87 if(hres != S_OK)
88 return FALSE;
90 hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
91 test_type, &test_jobId, &test_job);
92 if(hres != S_OK)
94 IBackgroundCopyManager_Release(test_manager);
95 return FALSE;
98 return TRUE;
101 /* Generic test cleanup */
102 static void teardown(void)
104 IBackgroundCopyJob_Release(test_job);
105 IBackgroundCopyManager_Release(test_manager);
108 /* FIXME: Remove when Wine has implemented this */
109 DEFINE_GUID(CLSID_BackgroundCopyManager2_0, 0x6d18ad12, 0xbde3, 0x4393, 0xb3,0x11, 0x09,0x9c,0x34,0x6e,0x6d,0xf9);
111 static BOOL check_bits20(void)
113 HRESULT hres;
114 IBackgroundCopyManager *manager;
115 BOOL ret = TRUE;
117 hres = CoCreateInstance(&CLSID_BackgroundCopyManager2_0, NULL,
118 CLSCTX_LOCAL_SERVER,
119 &IID_IBackgroundCopyManager,
120 (void **) &manager);
122 if (hres == REGDB_E_CLASSNOTREG)
124 ret = FALSE;
126 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
128 * Remove when Wine is fixed
130 if (setup())
132 HRESULT hres2;
134 hres2 = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
135 test_localPathA);
136 if (hres2 == S_OK)
138 trace("Running on Wine, claim 2.0 is present\n");
139 ret = TRUE;
141 teardown();
145 if (manager)
146 IBackgroundCopyManager_Release(manager);
148 return ret;
151 /* Test that the jobId is properly set */
152 static void test_GetId(void)
154 HRESULT hres;
155 GUID tmpId;
157 hres = IBackgroundCopyJob_GetId(test_job, &tmpId);
158 ok(hres == S_OK, "GetId failed: %08x\n", hres);
159 ok(memcmp(&tmpId, &test_jobId, sizeof tmpId) == 0, "Got incorrect GUID\n");
162 /* Test that the type is properly set */
163 static void test_GetType(void)
165 HRESULT hres;
166 BG_JOB_TYPE type;
168 hres = IBackgroundCopyJob_GetType(test_job, &type);
169 ok(hres == S_OK, "GetType failed: %08x\n", hres);
170 ok(type == test_type, "Got incorrect type\n");
173 /* Test that the display name is properly set */
174 static void test_GetName(void)
176 HRESULT hres;
177 LPWSTR displayName;
179 hres = IBackgroundCopyJob_GetDisplayName(test_job, &displayName);
180 ok(hres == S_OK, "GetName failed: %08x\n", hres);
181 ok(lstrcmpW(displayName, test_displayName) == 0, "Got incorrect type\n");
182 CoTaskMemFree(displayName);
185 /* Test adding a file */
186 static void test_AddFile(void)
188 HRESULT hres;
190 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
191 test_localPathA);
192 ok(hres == S_OK, "First call to AddFile failed: 0x%08x\n", hres);
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 ok(hres == S_OK, "got 0x%08x\n", hres);
223 hres = IBackgroundCopyJob_EnumFiles(test_job, &enumFiles);
224 ok(hres == S_OK, "EnumFiles failed: 0x%08x\n", hres);
226 res = IEnumBackgroundCopyFiles_Release(enumFiles);
227 ok(res == 0, "Bad ref count on release: %u\n", res);
230 /* Test getting job progress */
231 static void test_GetProgress_preTransfer(void)
233 HRESULT hres;
234 BG_JOB_PROGRESS progress;
236 hres = IBackgroundCopyJob_GetProgress(test_job, &progress);
237 ok(hres == S_OK, "GetProgress failed: 0x%08x\n", hres);
239 ok(progress.BytesTotal == 0, "Incorrect BytesTotal: %x%08x\n",
240 (DWORD)(progress.BytesTotal >> 32), (DWORD)progress.BytesTotal);
241 ok(progress.BytesTransferred == 0, "Incorrect BytesTransferred: %x%08x\n",
242 (DWORD)(progress.BytesTransferred >> 32), (DWORD)progress.BytesTransferred);
243 ok(progress.FilesTotal == 0, "Incorrect FilesTotal: %u\n", progress.FilesTotal);
244 ok(progress.FilesTransferred == 0, "Incorrect FilesTransferred %u\n", progress.FilesTransferred);
247 /* Test getting job state */
248 static void test_GetState(void)
250 HRESULT hres;
251 BG_JOB_STATE state;
253 state = BG_JOB_STATE_ERROR;
254 hres = IBackgroundCopyJob_GetState(test_job, &state);
255 ok(hres == S_OK, "GetState failed: 0x%08x\n", hres);
256 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
259 /* Test resuming a job */
260 static void test_ResumeEmpty(void)
262 HRESULT hres;
263 BG_JOB_STATE state;
265 hres = IBackgroundCopyJob_Resume(test_job);
266 ok(hres == BG_E_EMPTY, "Resume failed to return BG_E_EMPTY error: 0x%08x\n", hres);
268 state = BG_JOB_STATE_ERROR;
269 hres = IBackgroundCopyJob_GetState(test_job, &state);
270 ok(hres == S_OK, "got 0x%08x\n", hres);
271 ok(state == BG_JOB_STATE_SUSPENDED, "Incorrect job state: %d\n", state);
274 static void makeFile(WCHAR *name, const char *contents)
276 HANDLE file;
277 DWORD w, len = strlen(contents);
279 DeleteFileW(name);
280 file = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
281 FILE_ATTRIBUTE_NORMAL, NULL);
282 ok(file != INVALID_HANDLE_VALUE, "CreateFile\n");
283 ok(WriteFile(file, contents, len, &w, NULL), "WriteFile\n");
284 CloseHandle(file);
287 static void compareFiles(WCHAR *n1, WCHAR *n2)
289 char b1[256];
290 char b2[256];
291 DWORD s1, s2;
292 HANDLE f1, f2;
294 f1 = CreateFileW(n1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
295 FILE_ATTRIBUTE_NORMAL, NULL);
296 ok(f1 != INVALID_HANDLE_VALUE, "CreateFile\n");
298 f2 = CreateFileW(n2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
299 FILE_ATTRIBUTE_NORMAL, NULL);
300 ok(f2 != INVALID_HANDLE_VALUE, "CreateFile\n");
302 /* Neither of these files is very big */
303 ok(ReadFile(f1, b1, sizeof b1, &s1, NULL), "ReadFile\n");
304 ok(ReadFile(f2, b2, sizeof b2, &s2, NULL), "ReadFile\n");
306 CloseHandle(f1);
307 CloseHandle(f2);
309 ok(s1 == s2, "Files differ in length\n");
310 ok(memcmp(b1, b2, s1) == 0, "Files differ in contents\n");
313 /* Test a complete transfer for local files */
314 static void test_CompleteLocal(void)
316 static const int timeout_sec = 30;
317 HRESULT hres;
318 BG_JOB_STATE state;
319 int i;
321 DeleteFileW(test_localPathA);
322 DeleteFileW(test_localPathB);
323 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
324 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
326 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathA,
327 test_localPathA);
328 ok(hres == S_OK, "got 0x%08x\n", hres);
330 hres = IBackgroundCopyJob_AddFile(test_job, test_remotePathB,
331 test_localPathB);
332 ok(hres == S_OK, "got 0x%08x\n", hres);
334 hres = IBackgroundCopyJob_Resume(test_job);
335 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
337 for (i = 0; i < timeout_sec; ++i)
339 hres = IBackgroundCopyJob_GetState(test_job, &state);
340 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
341 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
342 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
343 "Bad state: %d\n", state);
344 if (state == BG_JOB_STATE_TRANSFERRED)
345 break;
346 Sleep(1000);
349 ok(i < timeout_sec, "BITS jobs timed out\n");
350 hres = IBackgroundCopyJob_Complete(test_job);
351 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
352 hres = IBackgroundCopyJob_GetState(test_job, &state);
353 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
354 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
356 compareFiles(test_remotePathA, test_localPathA);
357 compareFiles(test_remotePathB, test_localPathB);
359 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
360 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
361 DeleteFileW(test_localPathA);
362 DeleteFileW(test_localPathB);
365 /* Test a complete transfer for local files */
366 static void test_CompleteLocalURL(void)
368 static const WCHAR prot[] = {'f','i','l','e',':','/','/', 0};
369 static const int timeout_sec = 30;
370 WCHAR *urlA, *urlB;
371 HRESULT hres;
372 BG_JOB_STATE state;
373 int i;
375 DeleteFileW(test_localPathA);
376 DeleteFileW(test_localPathB);
377 makeFile(test_remotePathA, "This is a WINE test file for BITS\n");
378 makeFile(test_remotePathB, "This is another WINE test file for BITS\n");
380 urlA = HeapAlloc(GetProcessHeap(), 0,
381 (7 + lstrlenW(test_remotePathA) + 1) * sizeof urlA[0]);
382 urlB = HeapAlloc(GetProcessHeap(), 0,
383 (7 + lstrlenW(test_remotePathB) + 1) * sizeof urlB[0]);
384 if (!urlA || !urlB)
386 skip("Unable to allocate memory for URLs\n");
387 HeapFree(GetProcessHeap(), 0, urlA);
388 HeapFree(GetProcessHeap(), 0, urlB);
389 return;
392 lstrcpyW(urlA, prot);
393 lstrcatW(urlA, test_remotePathA);
394 lstrcpyW(urlB, prot);
395 lstrcatW(urlB, test_remotePathB);
397 hres = IBackgroundCopyJob_AddFile(test_job, urlA, test_localPathA);
398 ok(hres == S_OK, "got 0x%08x\n", hres);
400 hres = IBackgroundCopyJob_AddFile(test_job, urlB, test_localPathB);
401 ok(hres == S_OK, "got 0x%08x\n", hres);
403 hres = IBackgroundCopyJob_Resume(test_job);
404 ok(hres == S_OK, "IBackgroundCopyJob_Resume\n");
406 for (i = 0; i < timeout_sec; ++i)
408 hres = IBackgroundCopyJob_GetState(test_job, &state);
409 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
410 ok(state == BG_JOB_STATE_QUEUED || state == BG_JOB_STATE_CONNECTING
411 || state == BG_JOB_STATE_TRANSFERRING || state == BG_JOB_STATE_TRANSFERRED,
412 "Bad state: %d\n", state);
413 if (state == BG_JOB_STATE_TRANSFERRED)
414 break;
415 Sleep(1000);
418 ok(i < timeout_sec, "BITS jobs timed out\n");
419 hres = IBackgroundCopyJob_Complete(test_job);
420 ok(hres == S_OK, "IBackgroundCopyJob_Complete\n");
421 hres = IBackgroundCopyJob_GetState(test_job, &state);
422 ok(hres == S_OK, "IBackgroundCopyJob_GetState\n");
423 ok(state == BG_JOB_STATE_ACKNOWLEDGED, "Bad state: %d\n", state);
425 compareFiles(test_remotePathA, test_localPathA);
426 compareFiles(test_remotePathB, test_localPathB);
428 ok(DeleteFileW(test_remotePathA), "DeleteFile\n");
429 ok(DeleteFileW(test_remotePathB), "DeleteFile\n");
430 DeleteFileW(test_localPathA);
431 DeleteFileW(test_localPathB);
433 HeapFree(GetProcessHeap(), 0, urlA);
434 HeapFree(GetProcessHeap(), 0, urlB);
437 static void test_NotifyFlags(void)
439 ULONG flags;
440 HRESULT hr;
442 /* check default flags */
443 flags = 0;
444 hr = IBackgroundCopyJob_GetNotifyFlags(test_job, &flags);
445 ok(hr == S_OK, "got 0x%08x\n", hr);
446 ok(flags == (BG_NOTIFY_JOB_ERROR | BG_NOTIFY_JOB_TRANSFERRED), "flags 0x%08x\n", flags);
449 static void test_NotifyInterface(void)
451 HRESULT hr;
452 IUnknown *unk;
454 unk = (IUnknown*)0xdeadbeef;
455 hr = IBackgroundCopyJob_GetNotifyInterface(test_job, &unk);
456 ok(hr == S_OK, "got 0x%08x\n", hr);
457 ok(unk == NULL, "got %p\n", unk);
460 typedef void (*test_t)(void);
462 START_TEST(job)
464 static const test_t tests[] = {
465 test_GetId,
466 test_GetType,
467 test_GetName,
468 test_GetProgress_preTransfer,
469 test_GetState,
470 test_ResumeEmpty,
471 test_NotifyFlags,
472 test_NotifyInterface,
475 static const test_t tests_bits20[] = {
476 test_AddFile,
477 test_AddFileSet,
478 test_EnumFiles,
479 test_CompleteLocal,
480 test_CompleteLocalURL,
483 const test_t *test;
484 int i;
486 init_paths();
488 CoInitialize(NULL);
490 if (FAILED(test_create_manager()))
492 CoUninitialize();
493 win_skip("Failed to create Manager instance, skipping tests\n");
494 return;
497 for (test = tests, i = 0; *test; ++test, ++i)
499 /* Keep state separate between tests. */
500 if (!setup())
502 ok(0, "tests:%d: Unable to setup test\n", i);
503 break;
505 (*test)();
506 teardown();
509 if (check_bits20())
511 for (test = tests_bits20, i = 0; *test; ++test, ++i)
513 /* Keep state separate between tests. */
514 if (!setup())
516 ok(0, "tests_bits20:%d: Unable to setup test\n", i);
517 break;
519 (*test)();
520 teardown();
523 else
525 win_skip("Tests need BITS 2.0 or higher\n");
528 CoUninitialize();