1 /*
2  * Test suite for TaskScheduler interface
3  *
4  * Copyright (C) 2008 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 #define COBJMACROS
22 
23 #include "corerror.h"
24 
25 #include "initguid.h"
26 #include "mstask.h"
27 #include "wine/test.h"
28 
29 static ITaskScheduler *test_task_scheduler;
30 
31 static const WCHAR does_not_existW[] = {'\\','\\','d','o','e','s','_','n','o','t','_','e','x','i','s','t',0};
32 
33 static void test_NewWorkItem(void)
34 {
35     HRESULT hres;
36     ITask *task;
37     const WCHAR task_name[] = {'T', 'e', 's', 't', 'i', 'n', 'g', 0};
38     GUID GUID_BAD;
39 
40     /* Initialize a GUID that will not be a recognized CLSID or a IID */
41     CoCreateGuid(&GUID_BAD);
42 
43     /* Create TaskScheduler */
44     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
45             &IID_ITaskScheduler, (void **) &test_task_scheduler);
46     ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
47     if (hres != S_OK)
48     {
49         skip("Failed to create task scheduler.  Skipping tests.\n");
50         return;
51     }
52 
53     /* Test basic task creation */
54     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
55             &CLSID_CTask, &IID_ITask, (IUnknown**)&task);
56     ok(hres == S_OK, "NewNetworkItem failed: %08x\n", hres);
57     if (hres == S_OK)
58         ITask_Release(task);
59 
60     /* Task creation attempt using invalid work item class ID */
61     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
62             &GUID_BAD, &IID_ITask, (IUnknown**)&task);
63     ok(hres == CLASS_E_CLASSNOTAVAILABLE,
64             "Expected CLASS_E_CLASSNOTAVAILABLE: %08x\n", hres);
65 
66     /* Task creation attempt using invalid interface ID */
67     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
68             &CLSID_CTask, &GUID_BAD, (IUnknown**)&task);
69     ok(hres == E_NOINTERFACE, "Expected E_NOINTERFACE: %08x\n", hres);
70 
71     /* Task creation attempt using invalid work item class and interface ID */
72     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name,
73             &GUID_BAD, &GUID_BAD, (IUnknown**)&task);
74     ok(hres == CLASS_E_CLASSNOTAVAILABLE,
75             "Expected CLASS_E_CLASSNOTAVAILABLE: %08x\n", hres);
76 
77     ITaskScheduler_Release(test_task_scheduler);
78     return;
79 }
80 
81 static void test_Activate(void)
82 {
83     HRESULT hres;
84     ITask *task = NULL;
85     const WCHAR not_task_name[] =
86             {'N', 'o', 'S', 'u', 'c', 'h', 'T', 'a', 's', 'k', 0};
87 
88     /* Create TaskScheduler */
89     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
90             &IID_ITaskScheduler, (void **) &test_task_scheduler);
91     ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
92     if (hres != S_OK)
93     {
94         skip("Failed to create task scheduler.  Skipping tests.\n");
95         return;
96     }
97 
98     /* Attempt to activate a nonexistent task */
99     hres = ITaskScheduler_Activate(test_task_scheduler, not_task_name,
100             &IID_ITask, (IUnknown**)&task);
101     ok(hres == COR_E_FILENOTFOUND, "Expected COR_E_FILENOTFOUND: %08x\n", hres);
102 
103     ITaskScheduler_Release(test_task_scheduler);
104     return;
105 }
106 
107 static void test_GetTargetComputer(void)
108 {
109     HRESULT hres;
110     WCHAR *oldname;
111 
112     /* Create TaskScheduler */
113     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
114             &IID_ITaskScheduler, (void **) &test_task_scheduler);
115     ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
116     if (hres != S_OK)
117     {
118         skip("Failed to create task scheduler.\n");
119         return;
120     }
121 
122     if (0)
123     {
124         /* This crashes on w2k */
125         hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, NULL);
126         ok(hres == E_INVALIDARG, "got 0x%x (expected E_INVALIDARG)\n", hres);
127     }
128 
129     hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &oldname);
130     ok((hres == S_OK) && oldname && oldname[0] == '\\' && oldname[1] == '\\' && oldname[2],
131         "got 0x%x and %s (expected S_OK and an unc name)\n", hres, wine_dbgstr_w(oldname));
132 
133     CoTaskMemFree(oldname);
134 
135     ITaskScheduler_Release(test_task_scheduler);
136     return;
137 }
138 
139 static void test_SetTargetComputer(void)
140 {
141     WCHAR buffer[MAX_COMPUTERNAME_LENGTH + 3];  /* extra space for two '\' and a zero */
142     DWORD len = MAX_COMPUTERNAME_LENGTH + 1;    /* extra space for a zero */
143     WCHAR *oldname = NULL;
144     WCHAR *name = NULL;
145     HRESULT hres;
146 
147 
148     buffer[0] = '\\';
149     buffer[1] = '\\';
150     if (!GetComputerNameW(buffer + 2, &len))
151         return;
152 
153     /* Create TaskScheduler */
154     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
155             &IID_ITaskScheduler, (void **) &test_task_scheduler);
156     ok(hres == S_OK, "CTaskScheduler CoCreateInstance failed: %08x\n", hres);
157     if (hres != S_OK)
158     {
159         skip("Failed to create task scheduler.  Skipping tests.\n");
160         return;
161     }
162 
163     hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &oldname);
164     ok(hres == S_OK, "got 0x%x and %s (expected S_OK)\n", hres, wine_dbgstr_w(oldname));
165 
166     /* NULL is an alias for the local computer */
167     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, NULL);
168     ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
169     hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &name);
170     ok((hres == S_OK && !lstrcmpiW(name, buffer)),
171         "got 0x%x with %s (expected S_OK and %s)\n",
172         hres, wine_dbgstr_w(name), wine_dbgstr_w(buffer));
173     CoTaskMemFree(name);
174 
175     /* The name must be valid */
176     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, does_not_existW);
177     ok(hres == HRESULT_FROM_WIN32(ERROR_BAD_NETPATH), "got 0x%x (expected 0x80070035)\n", hres);
178     /* the name of the target computer is unchanged */
179     hres = ITaskScheduler_GetTargetComputer(test_task_scheduler, &name);
180     ok((hres == S_OK && !lstrcmpiW(name, buffer)),
181         "got 0x%x with %s (expected S_OK and %s)\n",
182         hres, wine_dbgstr_w(name), wine_dbgstr_w(buffer));
183     CoTaskMemFree(name);
184 
185     /* the two backslashes are optional */
186     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, oldname + 2);
187     if (hres == E_ACCESSDENIED)
188     {
189         skip("SetTargetComputer failed with E_ACCESSDENIED (needs admin rights)\n");
190         goto done;
191     }
192     ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
193 
194     /* the case is ignored */
195     CharUpperW(buffer);
196     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, buffer);
197     ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
198     CharLowerW(buffer);
199     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, buffer);
200     ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
201 
202     /* cleanup */
203     hres = ITaskScheduler_SetTargetComputer(test_task_scheduler, oldname);
204     ok(hres == S_OK, "got 0x%x (expected S_OK)\n", hres);
205 
206 done:
207     CoTaskMemFree(oldname);
208     ITaskScheduler_Release(test_task_scheduler);
209     return;
210 }
211 
212 static void test_Enum(void)
213 {
214     ITaskScheduler *scheduler;
215     IEnumWorkItems *tasks;
216     HRESULT hr;
217 
218     hr = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
219             &IID_ITaskScheduler, (void **)&scheduler);
220     ok(hr == S_OK, "got 0x%08x\n", hr);
221 
222 if (0) { /* crashes on win2k */
223     hr = ITaskScheduler_Enum(scheduler, NULL);
224     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
225 }
226 
227     hr = ITaskScheduler_Enum(scheduler, &tasks);
228     ok(hr == S_OK, "got 0x%08x\n", hr);
229     IEnumWorkItems_Release(tasks);
230 
231     ITaskScheduler_Release(scheduler);
232 }
233 
234 START_TEST(task_scheduler)
235 {
236     CoInitialize(NULL);
237     test_NewWorkItem();
238     test_Activate();
239     test_GetTargetComputer();
240     test_SetTargetComputer();
241     test_Enum();
242     CoUninitialize();
243 }
244