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
25 #include "wine/test.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)
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");
55 IBackgroundCopyManager_Release(manager
);
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)
80 memset(&test_jobId
, 0, sizeof test_jobId
);
81 test_type
= BG_JOB_TYPE_DOWNLOAD
;
83 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager
, NULL
,
85 &IID_IBackgroundCopyManager
,
86 (void **) &test_manager
);
90 hres
= IBackgroundCopyManager_CreateJob(test_manager
, test_displayName
,
91 test_type
, &test_jobId
, &test_job
);
94 IBackgroundCopyManager_Release(test_manager
);
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)
114 IBackgroundCopyManager
*manager
;
117 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager2_0
, NULL
,
119 &IID_IBackgroundCopyManager
,
122 if (hres
== REGDB_E_CLASSNOTREG
)
126 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
128 * Remove when Wine is fixed
134 hres2
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
138 trace("Running on Wine, claim 2.0 is present\n");
146 IBackgroundCopyManager_Release(manager
);
151 /* Test that the jobId is properly set */
152 static void test_GetId(void)
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)
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)
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)
190 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
192 ok(hres
== S_OK
, "First call to AddFile failed: 0x%08x\n", hres
);
194 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
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)
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)
216 IEnumBackgroundCopyFiles
*enumFiles
;
219 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
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)
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)
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)
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
)
277 DWORD w
, len
= strlen(contents
);
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");
287 static void compareFiles(WCHAR
*n1
, WCHAR
*n2
)
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");
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;
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
,
328 ok(hres
== S_OK
, "got 0x%08x\n", hres
);
330 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
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
)
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;
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]);
386 skip("Unable to allocate memory for URLs\n");
387 HeapFree(GetProcessHeap(), 0, urlA
);
388 HeapFree(GetProcessHeap(), 0, urlB
);
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
)
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)
442 /* check default flags */
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)
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);
464 static const test_t tests
[] = {
468 test_GetProgress_preTransfer
,
472 test_NotifyInterface
,
475 static const test_t tests_bits20
[] = {
480 test_CompleteLocalURL
,
490 if (FAILED(test_create_manager()))
493 win_skip("Failed to create Manager instance, skipping tests\n");
497 for (test
= tests
, i
= 0; *test
; ++test
, ++i
)
499 /* Keep state separate between tests. */
502 ok(0, "tests:%d: Unable to setup test\n", i
);
511 for (test
= tests_bits20
, i
= 0; *test
; ++test
, ++i
)
513 /* Keep state separate between tests. */
516 ok(0, "tests_bits20:%d: Unable to setup test\n", i
);
525 win_skip("Tests need BITS 2.0 or higher\n");