1 /*
2  * Unit test suite for Enum Background Copy Files Interface
3  *
4  * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24 
25 #include <stdarg.h>
26 
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winreg.h>
30 
31 #include <shlwapi.h>
32 //#include <stdio.h>
33 
34 #define COBJMACROS
35 
36 #include <wine/test.h>
37 #include <bits.h>
38 
39 /* Globals used by many tests */
40 #define NUM_FILES 2             /* At least two.  */
41 static const WCHAR test_remoteNameA[] = {'r','e','m','o','t','e','A', 0};
42 static const WCHAR test_localNameA[] = {'l','o','c','a','l','A', 0};
43 static const WCHAR test_remoteNameB[] = {'r','e','m','o','t','e','B', 0};
44 static const WCHAR test_localNameB[] = {'l','o','c','a','l','B', 0};
45 static const WCHAR test_displayName[] = {'T', 'e', 's', 't', 0};
46 static const ULONG test_fileCount = NUM_FILES;
47 static IBackgroundCopyJob *test_job;
48 static IBackgroundCopyManager *test_manager;
49 static IEnumBackgroundCopyFiles *test_enumFiles;
50 
51 /* Helper function to add a file to a job.  The helper function takes base
52    file name and creates properly formed path and URL strings for creation of
53    the file. */
54 static HRESULT addFileHelper(IBackgroundCopyJob* job,
55                              const WCHAR *localName, const WCHAR *remoteName)
56 {
57     DWORD urlSize;
58     WCHAR localFile[MAX_PATH];
59     WCHAR remoteUrl[MAX_PATH];
60     WCHAR remoteFile[MAX_PATH];
61 
62     GetCurrentDirectoryW(MAX_PATH, localFile);
63     PathAppendW(localFile, localName);
64     GetCurrentDirectoryW(MAX_PATH, remoteFile);
65     PathAppendW(remoteFile, remoteName);
66     urlSize = MAX_PATH;
67     UrlCreateFromPathW(remoteFile, remoteUrl, &urlSize, 0);
68     UrlUnescapeW(remoteUrl, NULL, &urlSize, URL_UNESCAPE_INPLACE);
69     return IBackgroundCopyJob_AddFile(job, remoteUrl, localFile);
70 }
71 
72 static HRESULT test_create_manager(void)
73 {
74     HRESULT hres;
75     IBackgroundCopyManager *manager = NULL;
76 
77     /* Creating BITS instance */
78     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
79                             &IID_IBackgroundCopyManager, (void **) &manager);
80 
81     if(hres == HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
82         win_skip("Needed Service is disabled\n");
83         return hres;
84     }
85 
86     if (hres == S_OK)
87     {
88         IBackgroundCopyJob *job;
89         GUID jobId;
90 
91         hres = IBackgroundCopyManager_CreateJob(manager, test_displayName, BG_JOB_TYPE_DOWNLOAD, &jobId, &job);
92         if (hres == S_OK)
93         {
94             hres = addFileHelper(job, test_localNameA, test_remoteNameA);
95             if (hres != S_OK)
96                 win_skip("AddFile() with file:// protocol failed. Tests will be skipped.\n");
97             IBackgroundCopyJob_Release(job);
98         }
99         IBackgroundCopyManager_Release(manager);
100     }
101 
102     return hres;
103 }
104 
105 /* Generic test setup */
106 static BOOL setup(void)
107 {
108     HRESULT hres;
109     GUID test_jobId;
110 
111     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
112                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
113                             (void **) &test_manager);
114     if(hres != S_OK)
115         return FALSE;
116 
117     hres = IBackgroundCopyManager_CreateJob(test_manager, test_displayName,
118                                             BG_JOB_TYPE_DOWNLOAD, &test_jobId,
119                                             &test_job);
120     if(hres != S_OK)
121     {
122         IBackgroundCopyManager_Release(test_manager);
123         return FALSE;
124     }
125 
126     if (addFileHelper(test_job, test_localNameA, test_remoteNameA) != S_OK
127         || addFileHelper(test_job, test_localNameB, test_remoteNameB) != S_OK
128         || IBackgroundCopyJob_EnumFiles(test_job, &test_enumFiles) != S_OK)
129     {
130         IBackgroundCopyJob_Release(test_job);
131         IBackgroundCopyManager_Release(test_manager);
132         return FALSE;
133     }
134 
135     return TRUE;
136 }
137 
138 /* Generic test cleanup */
139 static void teardown(void)
140 {
141     IEnumBackgroundCopyFiles_Release(test_enumFiles);
142     IBackgroundCopyJob_Release(test_job);
143     IBackgroundCopyManager_Release(test_manager);
144 }
145 
146 /* Test GetCount */
147 static void test_GetCount(void)
148 {
149     HRESULT hres;
150     ULONG fileCount;
151 
152     hres = IEnumBackgroundCopyFiles_GetCount(test_enumFiles, &fileCount);
153     ok(hres == S_OK, "GetCount failed: %08x\n", hres);
154     ok(fileCount == test_fileCount, "Got incorrect count\n");
155 }
156 
157 /* Test Next with a NULL pceltFetched*/
158 static void test_Next_walkListNull(void)
159 {
160     HRESULT hres;
161     IBackgroundCopyFile *file;
162     ULONG i;
163 
164     /* Fetch the available files */
165     for (i = 0; i < test_fileCount; i++)
166     {
167         hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
168         ok(hres == S_OK, "Next failed: %08x\n", hres);
169         IBackgroundCopyFile_Release(file);
170     }
171 
172     /* Attempt to fetch one more than the number of available files */
173     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, NULL);
174     ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
175 }
176 
177 /* Test Next by requesting one file at a time */
178 static void test_Next_walkList_1(void)
179 {
180     HRESULT hres;
181     IBackgroundCopyFile *file;
182     ULONG fetched;
183     ULONG i;
184 
185     /* Fetch the available files */
186     for (i = 0; i < test_fileCount; i++)
187     {
188         file = NULL;
189         fetched = 0;
190         hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
191         ok(hres == S_OK, "Next failed: %08x\n", hres);
192         ok(fetched == 1, "Next returned the incorrect number of files: %08x\n", hres);
193         ok(file != NULL, "Next returned NULL\n");
194         if (file)
195             IBackgroundCopyFile_Release(file);
196     }
197 
198     /* Attempt to fetch one more than the number of available files */
199     fetched = 0;
200     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 1, &file, &fetched);
201     ok(hres == S_FALSE, "Next off end of available files failed: %08x\n", hres);
202     ok(fetched == 0, "Next returned the incorrect number of files: %08x\n", hres);
203 }
204 
205 /* Test Next by requesting multiple files at a time */
206 static void test_Next_walkList_2(void)
207 {
208     HRESULT hres;
209     IBackgroundCopyFile *files[NUM_FILES];
210     ULONG fetched;
211     ULONG i;
212 
213     for (i = 0; i < test_fileCount; i++)
214         files[i] = NULL;
215 
216     fetched = 0;
217     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, test_fileCount, files, &fetched);
218     ok(hres == S_OK, "Next failed: %08x\n", hres);
219     ok(fetched == test_fileCount, "Next returned the incorrect number of files: %08x\n", hres);
220 
221     for (i = 0; i < test_fileCount; i++)
222     {
223         ok(files[i] != NULL, "Next returned NULL\n");
224         if (files[i])
225             IBackgroundCopyFile_Release(files[i]);
226     }
227 }
228 
229 /* Test Next Error conditions */
230 static void test_Next_errors(void)
231 {
232     HRESULT hres;
233     IBackgroundCopyFile *files[NUM_FILES];
234 
235     /* E_INVALIDARG: pceltFetched can ONLY be NULL if celt is 1 */
236     hres = IEnumBackgroundCopyFiles_Next(test_enumFiles, 2, files, NULL);
237     ok(hres == E_INVALIDARG, "Invalid call to Next succeeded: %08x\n", hres);
238 }
239 
240 /* Test skipping through the files in a list */
241 static void test_Skip_walkList(void)
242 {
243     HRESULT hres;
244     ULONG i;
245 
246     for (i = 0; i < test_fileCount; i++)
247     {
248         hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
249         ok(hres == S_OK, "Skip failed: %08x\n", hres);
250     }
251 
252     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, 1);
253     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
254 }
255 
256 /* Test skipping off the end of the list */
257 static void test_Skip_offEnd(void)
258 {
259     HRESULT hres;
260 
261     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount + 1);
262     ok(hres == S_FALSE, "Skip expected end of list: %08x\n", hres);
263 }
264 
265 /* Test resetting the file enumerator */
266 static void test_Reset(void)
267 {
268     HRESULT hres;
269 
270     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
271     ok(hres == S_OK, "Skip failed: %08x\n", hres);
272     hres = IEnumBackgroundCopyFiles_Reset(test_enumFiles);
273     ok(hres == S_OK, "Reset failed: %08x\n", hres);
274     hres = IEnumBackgroundCopyFiles_Skip(test_enumFiles, test_fileCount);
275     ok(hres == S_OK, "Reset failed: %08x\n", hres);
276 }
277 
278 typedef void (*test_t)(void);
279 
280 START_TEST(enum_files)
281 {
282     static const test_t tests[] = {
283         test_GetCount,
284         test_Next_walkListNull,
285         test_Next_walkList_1,
286         test_Next_walkList_2,
287         test_Next_errors,
288         test_Skip_walkList,
289         test_Skip_offEnd,
290         test_Reset,
291         0
292     };
293     const test_t *test;
294     int i;
295 
296     CoInitialize(NULL);
297 
298     if (FAILED(test_create_manager()))
299     {
300         CoUninitialize();
301         win_skip("Failed to create Manager instance, skipping tests\n");
302         return;
303     }
304 
305     for (test = tests, i = 0; *test; ++test, ++i)
306     {
307         /* Keep state separate between tests. */
308         if (!setup())
309         {
310             ok(0, "tests:%d: Unable to setup test\n", i);
311             break;
312         }
313         (*test)();
314         teardown();
315     }
316     CoUninitialize();
317 }
318