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"
28 /* Globals used by many tests */
29 static const WCHAR test_displayName
[] = {'T', 'e', 's', 't', 0};
30 static const WCHAR test_remoteNameA
[] = {'r','e','m','o','t','e','A', 0};
31 static const WCHAR test_remoteNameB
[] = {'r','e','m','o','t','e','B', 0};
32 static const WCHAR test_localNameA
[] = {'l','o','c','a','l','A', 0};
33 static const WCHAR test_localNameB
[] = {'l','o','c','a','l','B', 0};
34 static WCHAR
*test_currentDir
;
35 static WCHAR
*test_remotePathA
;
36 static WCHAR
*test_remotePathB
;
37 static WCHAR
*test_localPathA
;
38 static WCHAR
*test_localPathB
;
39 static IBackgroundCopyManager
*test_manager
;
40 static IBackgroundCopyJob
*test_job
;
41 static GUID test_jobId
;
42 static BG_JOB_TYPE test_type
;
44 static BOOL
init_paths(void)
46 static const WCHAR format
[] = {'%','s','\\','%','s', 0};
49 n
= GetCurrentDirectoryW(0, NULL
);
52 skip("Couldn't get current directory size\n");
56 test_currentDir
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof(WCHAR
));
58 = HeapAlloc(GetProcessHeap(), 0,
59 (n
+ 1 + lstrlenW(test_localNameA
)) * sizeof(WCHAR
));
61 = HeapAlloc(GetProcessHeap(), 0,
62 (n
+ 1 + lstrlenW(test_localNameB
)) * sizeof(WCHAR
));
64 = HeapAlloc(GetProcessHeap(), 0,
65 (n
+ 1 + lstrlenW(test_remoteNameA
)) * sizeof(WCHAR
));
67 = HeapAlloc(GetProcessHeap(), 0,
68 (n
+ 1 + lstrlenW(test_remoteNameB
)) * sizeof(WCHAR
));
70 if (!test_currentDir
|| !test_localPathA
|| !test_localPathB
71 || !test_remotePathA
|| !test_remotePathB
)
73 skip("Couldn't allocate memory for full paths\n");
77 if (GetCurrentDirectoryW(n
, test_currentDir
) != n
- 1)
79 skip("Couldn't get current directory\n");
83 wsprintfW(test_localPathA
, format
, test_currentDir
, test_localNameA
);
84 wsprintfW(test_localPathB
, format
, test_currentDir
, test_localNameB
);
85 wsprintfW(test_remotePathA
, format
, test_currentDir
, test_remoteNameA
);
86 wsprintfW(test_remotePathB
, format
, test_currentDir
, test_remoteNameB
);
91 /* Generic test setup */
92 static BOOL
setup(void)
98 memset(&test_jobId
, 0, sizeof test_jobId
);
99 test_type
= BG_JOB_TYPE_DOWNLOAD
;
101 hres
= CoCreateInstance(&CLSID_BackgroundCopyManager
, NULL
,
103 &IID_IBackgroundCopyManager
,
104 (void **) &test_manager
);
108 hres
= IBackgroundCopyManager_CreateJob(test_manager
, test_displayName
,
109 test_type
, &test_jobId
, &test_job
);
112 IBackgroundCopyManager_Release(test_manager
);
119 /* Generic test cleanup */
120 static void teardown(void)
122 IBackgroundCopyJob_Release(test_job
);
123 IBackgroundCopyManager_Release(test_manager
);
126 /* Test that the jobId is properly set */
127 static void test_GetId(void)
132 hres
= IBackgroundCopyJob_GetId(test_job
, &tmpId
);
133 ok(hres
== S_OK
, "GetId failed: %08x\n", hres
);
136 skip("Unable to get ID of test_job.\n");
139 ok(memcmp(&tmpId
, &test_jobId
, sizeof tmpId
) == 0, "Got incorrect GUID\n");
142 /* Test that the type is properly set */
143 static void test_GetType(void)
148 hres
= IBackgroundCopyJob_GetType(test_job
, &type
);
149 ok(hres
== S_OK
, "GetType failed: %08x\n", hres
);
152 skip("Unable to get type of test_job.\n");
155 ok(type
== test_type
, "Got incorrect type\n");
158 /* Test that the display name is properly set */
159 static void test_GetName(void)
164 hres
= IBackgroundCopyJob_GetDisplayName(test_job
, &displayName
);
165 ok(hres
== S_OK
, "GetName failed: %08x\n", hres
);
168 skip("Unable to get display name of test_job.\n");
171 ok(lstrcmpW(displayName
, test_displayName
) == 0, "Got incorrect type\n");
172 CoTaskMemFree(displayName
);
175 /* Test adding a file */
176 static void test_AddFile(void)
180 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
182 ok(hres
== S_OK
, "First call to AddFile failed: 0x%08x\n", hres
);
185 skip("Unable to add first file to job\n");
189 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
191 ok(hres
== S_OK
, "Second call to AddFile failed: 0x%08x\n", hres
);
194 /* Test creation of a job enumerator */
195 static void test_EnumFiles(void)
198 IEnumBackgroundCopyFiles
*enumFiles
;
201 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
205 skip("Unable to add file to job\n");
209 hres
= IBackgroundCopyJob_EnumFiles(test_job
, &enumFiles
);
210 ok(hres
== S_OK
, "EnumFiles failed: 0x%08x\n", hres
);
213 skip("Unable to create file enumerator.\n");
217 res
= IEnumBackgroundCopyFiles_Release(enumFiles
);
218 ok(res
== 0, "Bad ref count on release: %u\n", res
);
221 /* Test getting job progress */
222 static void test_GetProgress_preTransfer(void)
225 BG_JOB_PROGRESS progress
;
227 hres
= IBackgroundCopyJob_GetProgress(test_job
, &progress
);
228 ok(hres
== S_OK
, "GetProgress failed: 0x%08x\n", hres
);
231 skip("Unable to get job progress\n");
236 ok(progress
.BytesTotal
== 0, "Incorrect BytesTotal: %llu\n", progress
.BytesTotal
);
237 ok(progress
.BytesTransferred
== 0, "Incorrect BytesTransferred: %llu\n", progress
.BytesTransferred
);
238 ok(progress
.FilesTotal
== 0, "Incorrect FilesTotal: %u\n", progress
.FilesTotal
);
239 ok(progress
.FilesTransferred
== 0, "Incorrect FilesTransferred %u\n", progress
.FilesTransferred
);
242 /* Test getting job state */
243 static void test_GetState(void)
248 state
= BG_JOB_STATE_ERROR
;
249 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
250 ok(hres
== S_OK
, "GetState failed: 0x%08x\n", hres
);
253 skip("Unable to get job state\n");
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
);
267 if (hres
!= BG_E_EMPTY
)
269 skip("Failed calling resume job\n");
273 state
= BG_JOB_STATE_ERROR
;
274 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
277 skip("Unable to get job state\n");
280 ok(state
== BG_JOB_STATE_SUSPENDED
, "Incorrect job state: %d\n", state
);
283 static void makeFile(WCHAR
*name
, const char *contents
)
286 DWORD w
, len
= strlen(contents
);
289 file
= CreateFileW(name
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
290 FILE_ATTRIBUTE_NORMAL
, NULL
);
291 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile\n");
292 ok(WriteFile(file
, contents
, len
, &w
, NULL
), "WriteFile\n");
296 static void compareFiles(WCHAR
*n1
, WCHAR
*n2
)
303 f1
= CreateFileW(n1
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
304 FILE_ATTRIBUTE_NORMAL
, NULL
);
305 ok(f1
!= INVALID_HANDLE_VALUE
, "CreateFile\n");
307 f2
= CreateFileW(n2
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
308 FILE_ATTRIBUTE_NORMAL
, NULL
);
309 ok(f1
!= INVALID_HANDLE_VALUE
, "CreateFile\n");
311 /* Neither of these files is very big */
312 ok(ReadFile(f1
, b1
, sizeof b1
, &s1
, NULL
), "ReadFile\n");
313 ok(ReadFile(f2
, b2
, sizeof b2
, &s2
, NULL
), "ReadFile\n");
318 ok(s1
== s2
, "Files differ in length\n");
319 ok(memcmp(b1
, b2
, s1
) == 0, "Files differ in contents\n");
322 /* Test a complete transfer for local files */
323 static void test_CompleteLocal(void)
325 static const int timeout_sec
= 30;
330 DeleteFileW(test_localPathA
);
331 DeleteFileW(test_localPathB
);
332 makeFile(test_remotePathA
, "This is a WINE test file for BITS\n");
333 makeFile(test_remotePathB
, "This is another WINE test file for BITS\n");
335 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathA
,
339 skip("Unable to add file to job\n");
343 hres
= IBackgroundCopyJob_AddFile(test_job
, test_remotePathB
,
347 skip("Unable to add file to job\n");
351 hres
= IBackgroundCopyJob_Resume(test_job
);
352 ok(hres
== S_OK
, "IBackgroundCopyJob_Resume\n");
354 for (i
= 0; i
< timeout_sec
; ++i
)
356 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
357 ok(hres
== S_OK
, "IBackgroundCopyJob_GetState\n");
358 ok(state
== BG_JOB_STATE_QUEUED
|| state
== BG_JOB_STATE_CONNECTING
359 || state
== BG_JOB_STATE_TRANSFERRING
|| state
== BG_JOB_STATE_TRANSFERRED
,
360 "Bad state: %d\n", state
);
361 if (state
== BG_JOB_STATE_TRANSFERRED
)
366 ok(i
< timeout_sec
, "BITS jobs timed out\n");
367 hres
= IBackgroundCopyJob_Complete(test_job
);
368 ok(hres
== S_OK
, "IBackgroundCopyJob_Complete\n");
369 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
370 ok(hres
== S_OK
, "IBackgroundCopyJob_GetState\n");
371 ok(state
== BG_JOB_STATE_ACKNOWLEDGED
, "Bad state: %d\n", state
);
373 compareFiles(test_remotePathA
, test_localPathA
);
374 compareFiles(test_remotePathB
, test_localPathB
);
376 ok(DeleteFileW(test_remotePathA
), "DeleteFile\n");
377 ok(DeleteFileW(test_remotePathB
), "DeleteFile\n");
380 /* Test a complete transfer for local files */
381 static void test_CompleteLocalURL(void)
383 static const WCHAR prot
[] = {'f','i','l','e',':','/','/', 0};
384 static const int timeout_sec
= 30;
390 DeleteFileW(test_localPathA
);
391 DeleteFileW(test_localPathB
);
392 makeFile(test_remotePathA
, "This is a WINE test file for BITS\n");
393 makeFile(test_remotePathB
, "This is another WINE test file for BITS\n");
395 urlA
= HeapAlloc(GetProcessHeap(), 0,
396 (7 + lstrlenW(test_remotePathA
) + 1) * sizeof urlA
[0]);
397 urlB
= HeapAlloc(GetProcessHeap(), 0,
398 (7 + lstrlenW(test_remotePathB
) + 1) * sizeof urlB
[0]);
401 skip("Unable to allocate memory for URLs\n");
405 lstrcpyW(urlA
, prot
);
406 lstrcatW(urlA
, test_remotePathA
);
407 lstrcpyW(urlB
, prot
);
408 lstrcatW(urlB
, test_remotePathB
);
410 hres
= IBackgroundCopyJob_AddFile(test_job
, urlA
, test_localPathA
);
413 skip("Unable to add file to job\n");
417 hres
= IBackgroundCopyJob_AddFile(test_job
, urlB
, test_localPathB
);
420 skip("Unable to add file to job\n");
424 hres
= IBackgroundCopyJob_Resume(test_job
);
425 ok(hres
== S_OK
, "IBackgroundCopyJob_Resume\n");
427 for (i
= 0; i
< timeout_sec
; ++i
)
429 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
430 ok(hres
== S_OK
, "IBackgroundCopyJob_GetState\n");
431 ok(state
== BG_JOB_STATE_QUEUED
|| state
== BG_JOB_STATE_CONNECTING
432 || state
== BG_JOB_STATE_TRANSFERRING
|| state
== BG_JOB_STATE_TRANSFERRED
,
433 "Bad state: %d\n", state
);
434 if (state
== BG_JOB_STATE_TRANSFERRED
)
439 ok(i
< timeout_sec
, "BITS jobs timed out\n");
440 hres
= IBackgroundCopyJob_Complete(test_job
);
441 ok(hres
== S_OK
, "IBackgroundCopyJob_Complete\n");
442 hres
= IBackgroundCopyJob_GetState(test_job
, &state
);
443 ok(hres
== S_OK
, "IBackgroundCopyJob_GetState\n");
444 ok(state
== BG_JOB_STATE_ACKNOWLEDGED
, "Bad state: %d\n", state
);
446 compareFiles(test_remotePathA
, test_localPathA
);
447 compareFiles(test_remotePathB
, test_localPathB
);
449 ok(DeleteFileW(test_remotePathA
), "DeleteFile\n");
450 ok(DeleteFileW(test_remotePathB
), "DeleteFile\n");
452 HeapFree(GetProcessHeap(), 0, urlA
);
453 HeapFree(GetProcessHeap(), 0, urlB
);
456 typedef void (*test_t
)(void);
460 static const test_t tests
[] = {
466 test_GetProgress_preTransfer
,
470 test_CompleteLocalURL
,
479 for (test
= tests
; *test
; ++test
)
481 /* Keep state seperate between tests. */
484 skip("Unable to setup test\n");