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