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