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 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)
60 memset(&test_jobId
, 0, sizeof test_jobId
);
61 test_type
= BG_JOB_TYPE_DOWNLOAD
;
63 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager
, NULL
,
65 &IID_IBackgroundCopyManager
,
66 (void **) &test_manager
);
70 hres
= IBackgroundCopyManager_CreateJob(test_manager
, test_displayName
,
71 test_type
, &test_jobId
, &test_job
);
74 IBackgroundCopyManager_Release(test_manager
);
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)
94 IBackgroundCopyManager
*manager
;
97 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager2_0
, NULL
,
99 &IID_IBackgroundCopyManager
,
102 if (hres
== REGDB_E_CLASSNOTREG
)
106 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
108 * Remove when Wine is fixed
114 hres2
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
118 trace("Running on Wine, claim 2.0 is present\n");
126 IBackgroundCopyManager_Release(manager
);
131 /* Test that the jobId is properly set */
132 static void test_GetId(void)
137 hres
= IBackgroundCopyJob_GetId(test_job
, &tmpId
);
138 ok(hres
== S_OK
, "GetId failed: %08x\n", hres
);
141 skip("Unable to get ID of test_job.\n");
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)
153 hres
= IBackgroundCopyJob_GetType(test_job
, &type
);
154 ok(hres
== S_OK
, "GetType failed: %08x\n", hres
);
157 skip("Unable to get type of test_job.\n");
160 ok(type
== test_type
, "Got incorrect type\n");
163 /* Test that the display name is properly set */
164 static void test_GetName(void)
169 hres
= IBackgroundCopyJob_GetDisplayName(test_job
, &displayName
);
170 ok(hres
== S_OK
, "GetName failed: %08x\n", hres
);
173 skip("Unable to get display name of test_job.\n");
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)
185 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
187 ok(hres
== S_OK
, "First call to AddFile failed: 0x%08x\n", hres
);
190 skip("Unable to add first file to job\n");
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
,
223 skip("Unable to add file to job\n");
227 hres
= IBackgroundCopyJob_EnumFiles(test_job
, &enumFiles
);
228 ok(hres
== S_OK
, "EnumFiles failed: 0x%08x\n", hres
);
231 skip("Unable to create file enumerator.\n");
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)
243 BG_JOB_PROGRESS progress
;
245 hres
= IBackgroundCopyJob_GetProgress(test_job
, &progress
);
246 ok(hres
== S_OK
, "GetProgress failed: 0x%08x\n", hres
);
249 skip("Unable to get job progress\n");
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)
268 state
= BG_JOB_STATE_ERROR
;
269 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
270 ok(hres
== S_OK
, "GetState failed: 0x%08x\n", hres
);
273 skip("Unable to get job state\n");
276 ok(state
== BG_JOB_STATE_SUSPENDED
, "Incorrect job state: %d\n", state
);
279 /* Test resuming a job */
280 static void test_ResumeEmpty(void)
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");
293 state
= BG_JOB_STATE_ERROR
;
294 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
297 skip("Unable to get job state\n");
300 ok(state
== BG_JOB_STATE_SUSPENDED
, "Incorrect job state: %d\n", state
);
303 static void makeFile(WCHAR
*name
, const char *contents
)
306 DWORD w
, len
= strlen(contents
);
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");
316 static void compareFiles(WCHAR
*n1
, WCHAR
*n2
)
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");
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;
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
,
359 skip("Unable to add file to job\n");
363 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
367 skip("Unable to add file to job\n");
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
)
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;
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]);
423 skip("Unable to allocate memory for URLs\n");
424 HeapFree(GetProcessHeap(), 0, urlA
);
425 HeapFree(GetProcessHeap(), 0, urlB
);
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
);
437 skip("Unable to add file to job\n");
438 HeapFree(GetProcessHeap(), 0, urlA
);
439 HeapFree(GetProcessHeap(), 0, urlB
);
443 hres
= IBackgroundCopyJob_AddFile(test_job
, urlB
, test_localPathB
);
446 skip("Unable to add file to job\n");
447 HeapFree(GetProcessHeap(), 0, urlA
);
448 HeapFree(GetProcessHeap(), 0, urlB
);
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
)
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);
490 static const test_t tests
[] = {
494 test_GetProgress_preTransfer
,
499 static const test_t tests_bits20
[] = {
504 test_CompleteLocalURL
,
513 for (test
= tests
; *test
; ++test
)
515 /* Keep state separate between tests. */
518 skip("Unable to setup test\n");
527 for (test
= tests_bits20
; *test
; ++test
)
529 /* Keep state separate between tests. */
532 skip("Unable to setup test\n");
541 win_skip("Tests need BITS 2.0 or higher\n");