Release 1.4.1.
[wine] / dlls / qmgr / tests / qmgr.c
1 /*
2  * Unit test suite for bits functions
3  *
4  * Copyright 2007 Google (Roy Shea)
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 <stdio.h>
22
23 #define COBJMACROS
24
25 #include "wine/test.h"
26 #include "initguid.h"
27 #include "bits.h"
28
29 static WCHAR progname[MAX_PATH];
30
31 static void
32 test_CreateInstance(void)
33 {
34     HRESULT hres;
35     ULONG res;
36     IBackgroundCopyManager *manager = NULL;
37
38     /* Creating BITS instance */
39     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL, CLSCTX_LOCAL_SERVER,
40                             &IID_IBackgroundCopyManager, (void **) &manager);
41
42     if(hres == __HRESULT_FROM_WIN32(ERROR_SERVICE_DISABLED)) {
43         skip("Needed Service is disabled\n");
44         return;
45     }
46     ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
47     if(hres != S_OK) {
48         skip("Unable to create bits instance.\n");
49         return;
50     }
51
52     /* Releasing bits manager */
53     res = IBackgroundCopyManager_Release(manager);
54     ok(res == 0, "Bad ref count on release: %u\n", res);
55
56 }
57
58 static void test_CreateJob(void)
59 {
60     /* Job information */
61     static const WCHAR copyNameW[] = {'T', 'e', 's', 't', 0};
62     IBackgroundCopyJob* job = NULL;
63     GUID tmpId;
64     HRESULT hres;
65     ULONG res;
66     IBackgroundCopyManager* manager = NULL;
67
68     /* Setup */
69     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
70                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
71                             (void **) &manager);
72     if(hres != S_OK)
73     {
74         skip("Unable to create bits instance required for test.\n");
75         return;
76     }
77
78     /* Create bits job */
79     hres = IBackgroundCopyManager_CreateJob(manager, copyNameW,
80                                             BG_JOB_TYPE_DOWNLOAD, &tmpId,
81                                             &job);
82     ok(hres == S_OK, "CreateJob failed: %08x\n", hres);
83     if(hres != S_OK)
84         skip("Unable to create bits job.\n");
85     else
86     {
87         res = IBackgroundCopyJob_Release(job);
88         ok(res == 0, "Bad ref count on release: %u\n", res);
89     }
90
91     IBackgroundCopyManager_Release(manager);
92 }
93
94 static void test_EnumJobs(void)
95 {
96     /* Job Enumerator */
97     IEnumBackgroundCopyJobs* enumJobs;
98
99     static const WCHAR copyNameW[] = {'T', 'e', 's', 't', 0};
100     IBackgroundCopyManager *manager = NULL;
101     IBackgroundCopyJob *job = NULL;
102     HRESULT hres;
103     GUID tmpId;
104     ULONG res;
105
106     /* Setup */
107     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
108                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
109                             (void **) &manager);
110     if(hres != S_OK)
111     {
112         skip("Unable to create bits instance required for test.\n");
113         return;
114     }
115     hres = IBackgroundCopyManager_CreateJob(manager, copyNameW,
116                                             BG_JOB_TYPE_DOWNLOAD, &tmpId,
117                                             &job);
118     if(hres != S_OK)
119     {
120         skip("Unable to create bits job.\n");
121         IBackgroundCopyManager_Release(manager);
122         return;
123     }
124
125     hres = IBackgroundCopyManager_EnumJobs(manager, 0, &enumJobs);
126     ok(hres == S_OK, "EnumJobs failed: %08x\n", hres);
127     if(hres != S_OK)
128         skip("Unable to create job enumerator.\n");
129     else
130     {
131         res = IEnumBackgroundCopyJobs_Release(enumJobs);
132         ok(res == 0, "Bad ref count on release: %u\n", res);
133     }
134
135     /* Tear down */
136     IBackgroundCopyJob_Release(job);
137     IBackgroundCopyManager_Release(manager);
138 }
139
140 static void run_child(WCHAR *secret)
141 {
142     static const WCHAR format[] = {'%','s',' ','q','m','g','r',' ','%','s', 0};
143     WCHAR cmdline[MAX_PATH];
144     PROCESS_INFORMATION info;
145     STARTUPINFOW startup;
146
147     memset(&startup, 0, sizeof startup);
148     startup.cb = sizeof startup;
149
150     wsprintfW(cmdline, format, progname, secret);
151     ok(CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
152     winetest_wait_child_process(info.hProcess);
153     ok(CloseHandle(info.hProcess), "CloseHandle\n");
154     ok(CloseHandle(info.hThread), "CloseHandle\n");
155 }
156
157 static void do_child(const char *secretA)
158 {
159     WCHAR secretW[MAX_PATH];
160     IBackgroundCopyManager *manager = NULL;
161     GUID id;
162     IBackgroundCopyJob *job;
163     HRESULT hres;
164     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
165                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
166                             (void **) &manager);
167     if(hres != S_OK)
168     {
169         skip("Unable to create bits instance required for test.\n");
170         return;
171     }
172
173     MultiByteToWideChar(CP_ACP, 0, secretA, -1, secretW, MAX_PATH);
174     hres = IBackgroundCopyManager_CreateJob(manager, secretW,
175                                             BG_JOB_TYPE_DOWNLOAD, &id, &job);
176     ok(hres == S_OK, "CreateJob in child process\n");
177     IBackgroundCopyJob_Release(job);
178     IBackgroundCopyManager_Release(manager);
179 }
180
181 static void test_globalness(void)
182 {
183     static const WCHAR format[] = {'t','e','s','t','_','%','u', 0};
184     WCHAR secretName[MAX_PATH];
185     IEnumBackgroundCopyJobs* enumJobs;
186     IBackgroundCopyManager *manager = NULL;
187     HRESULT hres;
188     hres = CoCreateInstance(&CLSID_BackgroundCopyManager, NULL,
189                             CLSCTX_LOCAL_SERVER, &IID_IBackgroundCopyManager,
190                             (void **) &manager);
191     if(hres != S_OK)
192     {
193         skip("Unable to create bits instance required for test.\n");
194         return;
195     }
196
197     wsprintfW(secretName, format, GetTickCount());
198     run_child(secretName);
199
200     hres = IBackgroundCopyManager_EnumJobs(manager, 0, &enumJobs);
201     ok(hres == S_OK, "EnumJobs failed: %08x\n", hres);
202     if(hres != S_OK)
203         skip("Unable to create job enumerator.\n");
204     else
205     {
206         ULONG i, n;
207         IBackgroundCopyJob *job;
208         BOOL found = FALSE;
209
210         hres = IEnumBackgroundCopyJobs_GetCount(enumJobs, &n);
211         ok(hres == S_OK, "GetCount failed: %08x\n", hres);
212         for (i = 0; i < n && !found; ++i)
213         {
214             LPWSTR name;
215             IEnumBackgroundCopyJobs_Next(enumJobs, 1, &job, NULL);
216             IBackgroundCopyJob_GetDisplayName(job, &name);
217             if (lstrcmpW(name, secretName) == 0)
218                 found = TRUE;
219             CoTaskMemFree(name);
220             IBackgroundCopyJob_Release(job);
221         }
222         hres = IEnumBackgroundCopyJobs_Release(enumJobs);
223         ok(hres == S_OK, "Release failed: %08x\n", hres);
224         ok(found, "Adding a job in another process failed\n");
225     }
226
227     IBackgroundCopyManager_Release(manager);
228 }
229
230 START_TEST(qmgr)
231 {
232     char **argv;
233     int argc = winetest_get_mainargs(&argv);
234     MultiByteToWideChar(CP_ACP, 0, argv[0], -1, progname, MAX_PATH);
235
236     CoInitialize(NULL);
237     if (argc == 3)
238         do_child(argv[2]);
239     else
240     {
241         test_CreateInstance();
242         test_CreateJob();
243         test_EnumJobs();
244         test_globalness();
245     }
246     CoUninitialize();
247 }