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 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};
50 n
= GetCurrentDirectoryW(0, NULL
);
53 skip("Couldn't get current directory size\n");
57 test_currentDir
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
59 = HeapAlloc(GetProcessHeap(), 0,
60 (n
+ 1 + lstrlenW(test_localNameA
)) * sizeof(WCHAR
));
62 = HeapAlloc(GetProcessHeap(), 0,
63 (n
+ 1 + lstrlenW(test_localNameB
)) * sizeof(WCHAR
));
65 = HeapAlloc(GetProcessHeap(), 0,
66 (n
+ 1 + lstrlenW(test_remoteNameA
)) * sizeof(WCHAR
));
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");
78 if (GetCurrentDirectoryW(n
, test_currentDir
) != n
- 1)
80 skip("Couldn't get current directory\n");
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
);
92 /* Generic test setup */
93 static BOOL
setup(void)
99 memset(&test_jobId
, 0, sizeof test_jobId
);
100 test_type
= BG_JOB_TYPE_DOWNLOAD
;
102 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager
, NULL
,
104 &IID_IBackgroundCopyManager
,
105 (void **) &test_manager
);
109 hres
= IBackgroundCopyManager_CreateJob(test_manager
, test_displayName
,
110 test_type
, &test_jobId
, &test_job
);
113 IBackgroundCopyManager_Release(test_manager
);
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)
133 IBackgroundCopyManager
*manager
;
136 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager2_0
, NULL
,
138 &IID_IBackgroundCopyManager
,
141 if (hres
== REGDB_E_CLASSNOTREG
)
145 /* FIXME: Wine implements 2.0 functionality but doesn't advertise 2.0
147 * Remove when Wine is fixed
153 hres2
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
157 trace("Running on Wine, claim 2.0 is present\n");
165 IBackgroundCopyManager_Release(manager
);
170 /* Test that the jobId is properly set */
171 static void test_GetId(void)
176 hres
= IBackgroundCopyJob_GetId(test_job
, &tmpId
);
177 ok(hres
== S_OK
, "GetId failed: %08x\n", hres
);
180 skip("Unable to get ID of test_job.\n");
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)
192 hres
= IBackgroundCopyJob_GetType(test_job
, &type
);
193 ok(hres
== S_OK
, "GetType failed: %08x\n", hres
);
196 skip("Unable to get type of test_job.\n");
199 ok(type
== test_type
, "Got incorrect type\n");
202 /* Test that the display name is properly set */
203 static void test_GetName(void)
208 hres
= IBackgroundCopyJob_GetDisplayName(test_job
, &displayName
);
209 ok(hres
== S_OK
, "GetName failed: %08x\n", hres
);
212 skip("Unable to get display name of test_job.\n");
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)
224 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
226 ok(hres
== S_OK
, "First call to AddFile failed: 0x%08x\n", hres
);
229 skip("Unable to add first file to job\n");
233 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
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)
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)
255 IEnumBackgroundCopyFiles
*enumFiles
;
258 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
262 skip("Unable to add file to job\n");
266 hres
= IBackgroundCopyJob_EnumFiles(test_job
, &enumFiles
);
267 ok(hres
== S_OK
, "EnumFiles failed: 0x%08x\n", hres
);
270 skip("Unable to create file enumerator.\n");
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)
282 BG_JOB_PROGRESS progress
;
284 hres
= IBackgroundCopyJob_GetProgress(test_job
, &progress
);
285 ok(hres
== S_OK
, "GetProgress failed: 0x%08x\n", hres
);
288 skip("Unable to get job progress\n");
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)
307 state
= BG_JOB_STATE_ERROR
;
308 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
309 ok(hres
== S_OK
, "GetState failed: 0x%08x\n", hres
);
312 skip("Unable to get job state\n");
315 ok(state
== BG_JOB_STATE_SUSPENDED
, "Incorrect job state: %d\n", state
);
318 /* Test resuming a job */
319 static void test_ResumeEmpty(void)
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");
332 state
= BG_JOB_STATE_ERROR
;
333 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
336 skip("Unable to get job state\n");
339 ok(state
== BG_JOB_STATE_SUSPENDED
, "Incorrect job state: %d\n", state
);
342 static void makeFile(WCHAR
*name
, const char *contents
)
345 DWORD w
, len
= strlen(contents
);
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");
355 static void compareFiles(WCHAR
*n1
, WCHAR
*n2
)
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");
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;
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
,
398 skip("Unable to add file to job\n");
402 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
406 skip("Unable to add file to job\n");
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
)
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;
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]);
462 skip("Unable to allocate memory for URLs\n");
463 HeapFree(GetProcessHeap(), 0, urlA
);
464 HeapFree(GetProcessHeap(), 0, urlB
);
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
);
476 skip("Unable to add file to job\n");
477 HeapFree(GetProcessHeap(), 0, urlA
);
478 HeapFree(GetProcessHeap(), 0, urlB
);
482 hres
= IBackgroundCopyJob_AddFile(test_job
, urlB
, test_localPathB
);
485 skip("Unable to add file to job\n");
486 HeapFree(GetProcessHeap(), 0, urlA
);
487 HeapFree(GetProcessHeap(), 0, urlB
);
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
)
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);
529 static const test_t tests
[] = {
533 test_GetProgress_preTransfer
,
538 static const test_t tests_bits20
[] = {
543 test_CompleteLocalURL
,
553 for (test
= tests
; *test
; ++test
)
555 /* Keep state separate between tests. */
558 skip("Unable to setup test\n");
567 for (test
= tests_bits20
; *test
; ++test
)
569 /* Keep state separate between tests. */
572 skip("Unable to setup test\n");
581 win_skip("Tests need BITS 2.0 or higher\n");