mstask: ITask::GetErrorRetryCount() is not implemented.
[wine.git] / dlls / mstask / tests / task.c
blob3739db06928e246bc06dc45913b320f7f8e499a7
1 /*
2 * Test suite for Task interface
4 * Copyright (C) 2008 Google (Roy Shea)
5 * Copyright (C) 2018 Dmitry Timoshkov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define COBJMACROS
24 #include "corerror.h"
25 #include "mstask.h"
26 #include "wine/test.h"
28 static ITaskScheduler *test_task_scheduler;
29 static ITask *test_task;
30 static const WCHAR empty[] = {0};
32 static BOOL setup_task(void)
34 HRESULT hres;
35 const WCHAR task_name[] = {'T','e','s','t','i','n','g', 0};
37 hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
38 &IID_ITaskScheduler, (void **) &test_task_scheduler);
39 if(hres != S_OK)
40 return FALSE;
41 hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name, &CLSID_CTask,
42 &IID_ITask, (IUnknown**)&test_task);
43 if(hres != S_OK)
45 ITaskScheduler_Release(test_task_scheduler);
46 return FALSE;
48 return TRUE;
51 static void cleanup_task(void)
53 ITask_Release(test_task);
54 ITaskScheduler_Release(test_task_scheduler);
57 static LPCWSTR path_resolve_name(LPCWSTR base_name)
59 static WCHAR buffer[MAX_PATH];
60 int len;
62 len = SearchPathW(NULL, base_name, NULL, 0, NULL, NULL);
63 if (len == 0)
64 return base_name;
65 else if (len < MAX_PATH)
67 SearchPathW(NULL, base_name, NULL, MAX_PATH, buffer, NULL);
68 return buffer;
70 return NULL;
73 static void test_SetApplicationName_GetApplicationName(void)
75 BOOL setup;
76 HRESULT hres;
77 LPWSTR stored_name;
78 LPCWSTR full_name;
79 const WCHAR non_application_name[] = {'N','o','S','u','c','h',
80 'A','p','p','l','i','c','a','t','i','o','n', 0};
81 const WCHAR notepad_exe[] = {
82 'n','o','t','e','p','a','d','.','e','x','e', 0};
83 const WCHAR notepad[] = {'n','o','t','e','p','a','d', 0};
85 setup = setup_task();
86 ok(setup, "Failed to setup test_task\n");
87 if (!setup)
89 skip("Failed to create task. Skipping tests.\n");
90 return;
93 /* Attempt getting before setting application name */
94 hres = ITask_GetApplicationName(test_task, &stored_name);
95 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
96 if (hres == S_OK)
98 ok(!lstrcmpiW(stored_name, empty),
99 "Got %s, expected empty string\n", wine_dbgstr_w(stored_name));
100 CoTaskMemFree(stored_name);
103 /* Set application name to a nonexistent application and then get
104 * the application name that is actually stored */
105 hres = ITask_SetApplicationName(test_task, non_application_name);
106 ok(hres == S_OK, "Failed setting name %s: %08x\n",
107 wine_dbgstr_w(non_application_name), hres);
108 hres = ITask_GetApplicationName(test_task, &stored_name);
109 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
110 if (hres == S_OK)
112 full_name = path_resolve_name(non_application_name);
113 ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
114 wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
115 CoTaskMemFree(stored_name);
118 /* Set a valid application name with program type extension and then
119 * get the stored name */
120 hres = ITask_SetApplicationName(test_task, notepad_exe);
121 ok(hres == S_OK, "Failed setting name %s: %08x\n",
122 wine_dbgstr_w(notepad_exe), hres);
123 hres = ITask_GetApplicationName(test_task, &stored_name);
124 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
125 if (hres == S_OK)
127 full_name = path_resolve_name(notepad_exe);
128 ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
129 wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
130 CoTaskMemFree(stored_name);
133 /* Set a valid application name without program type extension and
134 * then get the stored name */
135 hres = ITask_SetApplicationName(test_task, notepad);
136 ok(hres == S_OK, "Failed setting name %s: %08x\n", wine_dbgstr_w(notepad), hres);
137 hres = ITask_GetApplicationName(test_task, &stored_name);
138 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
139 if (hres == S_OK)
141 full_name = path_resolve_name(notepad_exe); /* XP SP1 appends .exe */
142 if (lstrcmpiW(stored_name, full_name) != 0)
144 full_name = path_resolve_name(notepad);
145 ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
146 wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
148 CoTaskMemFree(stored_name);
151 /* After having a valid application name set, set application the name
152 * to a nonexistent application and then get the name that is
153 * actually stored */
154 hres = ITask_SetApplicationName(test_task, non_application_name);
155 ok(hres == S_OK, "Failed setting name %s: %08x\n",
156 wine_dbgstr_w(non_application_name), hres);
157 hres = ITask_GetApplicationName(test_task, &stored_name);
158 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
159 if (hres == S_OK)
161 full_name = path_resolve_name(non_application_name);
162 ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
163 wine_dbgstr_w(stored_name), wine_dbgstr_w(full_name));
164 CoTaskMemFree(stored_name);
167 /* Clear application name */
168 hres = ITask_SetApplicationName(test_task, empty);
169 ok(hres == S_OK, "Failed setting name %s: %08x\n", wine_dbgstr_w(empty), hres);
170 hres = ITask_GetApplicationName(test_task, &stored_name);
171 ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
172 if (hres == S_OK)
174 ok(!lstrcmpiW(stored_name, empty),
175 "Got %s, expected empty string\n", wine_dbgstr_w(stored_name));
176 CoTaskMemFree(stored_name);
179 cleanup_task();
180 return;
183 static void test_CreateTrigger(void)
185 BOOL setup;
186 HRESULT hres;
187 WORD trigger_index;
188 ITaskTrigger *test_trigger;
190 setup = setup_task();
191 ok(setup, "Failed to setup test_task\n");
192 if (!setup)
194 skip("Failed to create task. Skipping tests.\n");
195 return;
198 hres = ITask_CreateTrigger(test_task, &trigger_index, &test_trigger);
199 ok(hres == S_OK, "Failed to create trigger: 0x%08x\n", hres);
200 if (hres != S_OK)
202 cleanup_task();
203 return;
206 ITaskTrigger_Release(test_trigger);
207 cleanup_task();
208 return;
211 static void test_SetParameters_GetParameters(void)
213 BOOL setup;
214 HRESULT hres;
215 LPWSTR parameters;
216 const WCHAR parameters_a[] = {'f','o','o','.','t','x','t', 0};
217 const WCHAR parameters_b[] = {'f','o','o','.','t','x','t',' ',
218 'b','a','r','.','t','x','t', 0};
220 setup = setup_task();
221 ok(setup, "Failed to setup test_task\n");
222 if (!setup)
224 skip("Failed to create task. Skipping tests.\n");
225 return;
228 /* Get parameters before setting them */
229 hres = ITask_GetParameters(test_task, &parameters);
230 ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
231 if (hres == S_OK)
233 ok(!lstrcmpW(parameters, empty),
234 "Got %s, expected empty string\n", wine_dbgstr_w(parameters));
235 CoTaskMemFree(parameters);
238 /* Set parameters to a simple string */
239 hres = ITask_SetParameters(test_task, parameters_a);
240 ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
241 wine_dbgstr_w(parameters_a), hres);
242 hres = ITask_GetParameters(test_task, &parameters);
243 ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
244 if (hres == S_OK)
246 ok(!lstrcmpW(parameters, parameters_a), "Got %s, expected %s\n",
247 wine_dbgstr_w(parameters), wine_dbgstr_w(parameters_a));
248 CoTaskMemFree(parameters);
251 /* Update parameters to a different simple string */
252 hres = ITask_SetParameters(test_task, parameters_b);
253 ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
254 wine_dbgstr_w(parameters_b), hres);
255 hres = ITask_GetParameters(test_task, &parameters);
256 ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
257 if (hres == S_OK)
259 ok(!lstrcmpW(parameters, parameters_b), "Got %s, expected %s\n",
260 wine_dbgstr_w(parameters), wine_dbgstr_w(parameters_b));
261 CoTaskMemFree(parameters);
264 /* Clear parameters */
265 hres = ITask_SetParameters(test_task, empty);
266 ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
267 wine_dbgstr_w(empty), hres);
268 hres = ITask_GetParameters(test_task, &parameters);
269 ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
270 if (hres == S_OK)
272 ok(!lstrcmpW(parameters, empty),
273 "Got %s, expected empty string\n", wine_dbgstr_w(parameters));
274 CoTaskMemFree(parameters);
277 cleanup_task();
278 return;
281 static void test_SetComment_GetComment(void)
283 BOOL setup;
284 HRESULT hres;
285 LPWSTR comment;
286 const WCHAR comment_a[] = {'C','o','m','m','e','n','t','.', 0};
287 const WCHAR comment_b[] = {'L','o','n','g','e','r',' ',
288 'c','o','m','m','e','n','t','.', 0};
290 setup = setup_task();
291 ok(setup, "Failed to setup test_task\n");
292 if (!setup)
294 skip("Failed to create task. Skipping tests.\n");
295 return;
298 /* Get comment before setting it*/
299 hres = ITask_GetComment(test_task, &comment);
300 ok(hres == S_OK, "GetComment failed: %08x\n", hres);
301 if (hres == S_OK)
303 ok(!lstrcmpW(comment, empty),
304 "Got %s, expected empty string\n", wine_dbgstr_w(comment));
305 CoTaskMemFree(comment);
308 /* Set comment to a simple string */
309 hres = ITask_SetComment(test_task, comment_a);
310 ok(hres == S_OK, "Failed setting comment %s: %08x\n",
311 wine_dbgstr_w(comment_a), hres);
312 hres = ITask_GetComment(test_task, &comment);
313 ok(hres == S_OK, "GetComment failed: %08x\n", hres);
314 if (hres == S_OK)
316 ok(!lstrcmpW(comment, comment_a), "Got %s, expected %s\n",
317 wine_dbgstr_w(comment), wine_dbgstr_w(comment_a));
318 CoTaskMemFree(comment);
321 /* Update comment to a different simple string */
322 hres = ITask_SetComment(test_task, comment_b);
323 ok(hres == S_OK, "Failed setting comment %s: %08x\n",
324 wine_dbgstr_w(comment_b), hres);
325 hres = ITask_GetComment(test_task, &comment);
326 ok(hres == S_OK, "GetComment failed: %08x\n", hres);
327 if (hres == S_OK)
329 ok(!lstrcmpW(comment, comment_b), "Got %s, expected %s\n",
330 wine_dbgstr_w(comment), wine_dbgstr_w(comment_b));
331 CoTaskMemFree(comment);
334 /* Clear comment */
335 hres = ITask_SetComment(test_task, empty);
336 ok(hres == S_OK, "Failed setting comment %s: %08x\n",
337 wine_dbgstr_w(empty), hres);
338 hres = ITask_GetComment(test_task, &comment);
339 ok(hres == S_OK, "GetComment failed: %08x\n", hres);
340 if (hres == S_OK)
342 ok(!lstrcmpW(comment, empty),
343 "Got %s, expected empty string\n", wine_dbgstr_w(comment));
344 CoTaskMemFree(comment);
347 cleanup_task();
348 return;
351 static void test_SetMaxRunTime_GetMaxRunTime(void)
353 BOOL setup;
354 HRESULT hres;
355 DWORD max_run_time;
357 setup = setup_task();
358 ok(setup, "Failed to setup test_task\n");
359 if (!setup)
361 skip("Failed to create task. Skipping tests.\n");
362 return;
365 /* Default time is 3 days:
366 * 3 days * 24 hours * 60 minutes * 60 seconds * 1000 ms = 259200000 */
367 max_run_time = 0;
368 hres = ITask_GetMaxRunTime(test_task, &max_run_time);
369 ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
370 ok(max_run_time == 259200000, "Expected 259200000: %d\n", max_run_time);
372 /* Basic set test */
373 max_run_time = 0;
374 hres = ITask_SetMaxRunTime(test_task, 1234);
375 ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
376 hres = ITask_GetMaxRunTime(test_task, &max_run_time);
377 ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
378 ok(max_run_time == 1234, "Expected 1234: %d\n", max_run_time);
380 /* Verify that time can be set to zero */
381 max_run_time = 1;
382 hres = ITask_SetMaxRunTime(test_task, 0);
383 ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
384 hres = ITask_GetMaxRunTime(test_task, &max_run_time);
385 ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
386 ok(max_run_time == 0, "Expected 0: %d\n", max_run_time);
388 /* Check resolution by setting time to one */
389 max_run_time = 0;
390 hres = ITask_SetMaxRunTime(test_task, 1);
391 ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
392 hres = ITask_GetMaxRunTime(test_task, &max_run_time);
393 ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
394 ok(max_run_time == 1, "Expected 1: %d\n", max_run_time);
396 /* Verify that time can be set to INFINITE */
397 max_run_time = 0;
398 hres = ITask_SetMaxRunTime(test_task, INFINITE);
399 ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
400 hres = ITask_GetMaxRunTime(test_task, &max_run_time);
401 ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
402 ok(max_run_time == INFINITE, "Expected INFINITE: %d\n", max_run_time);
404 cleanup_task();
405 return;
408 static void test_SetAccountInformation_GetAccountInformation(void)
410 BOOL setup;
411 HRESULT hres;
412 LPWSTR account_name;
413 const WCHAR dummy_account_name[] = {'N', 'o', 'S', 'u', 'c', 'h',
414 'A', 'c', 'c', 'o', 'u', 'n', 't', 0};
415 const WCHAR dummy_account_name_b[] = {'N', 'o', 'S', 'u', 'c', 'h',
416 'A', 'c', 'c', 'o', 'u', 'n', 't', 'B', 0};
418 setup = setup_task();
419 ok(setup, "Failed to setup test_task\n");
420 if (!setup)
422 skip("Failed to create task. Skipping tests.\n");
423 return;
426 /* Get account information before it is set */
427 hres = ITask_GetAccountInformation(test_task, &account_name);
428 /* WinXP returns HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND): 0x80070002 but
429 * Win2K returns SCHED_E_CANNOT_OPEN_TASK: 0x8004130d
430 * Win9x doesn't support security services */
431 if (hres == SCHED_E_NO_SECURITY_SERVICES || hres == SCHED_E_SERVICE_NOT_RUNNING)
433 win_skip("Security services are not supported\n");
434 cleanup_task();
435 return;
437 ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
438 hres == SCHED_E_CANNOT_OPEN_TASK,
439 "Unset account name generated: 0x%08x\n", hres);
441 /* Attempt to set to a dummy account without a password */
442 /* This test passes on WinXP but fails on Win2K */
443 hres = ITask_SetAccountInformation(test_task, dummy_account_name, NULL);
444 ok(hres == S_OK,
445 "Failed setting dummy account with no password: %08x\n", hres);
446 hres = ITask_GetAccountInformation(test_task, &account_name);
447 ok(hres == S_OK ||
448 broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
449 hres == SCHED_E_CANNOT_OPEN_TASK ||
450 hres == 0x200), /* win2k */
451 "GetAccountInformation failed: %08x\n", hres);
452 if (hres == S_OK)
454 ok(!lstrcmpW(account_name, dummy_account_name),
455 "Got %s, expected %s\n", wine_dbgstr_w(account_name),
456 wine_dbgstr_w(dummy_account_name));
457 CoTaskMemFree(account_name);
460 /* Attempt to set to a dummy account with a (invalid) password */
461 /* This test passes on WinXP but fails on Win2K */
462 hres = ITask_SetAccountInformation(test_task, dummy_account_name_b,
463 dummy_account_name_b);
464 ok(hres == S_OK,
465 "Failed setting dummy account with password: %08x\n", hres);
466 hres = ITask_GetAccountInformation(test_task, &account_name);
467 ok(hres == S_OK ||
468 broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
469 hres == SCHED_E_CANNOT_OPEN_TASK ||
470 hres == 0x200), /* win2k */
471 "GetAccountInformation failed: %08x\n", hres);
472 if (hres == S_OK)
474 ok(!lstrcmpW(account_name, dummy_account_name_b),
475 "Got %s, expected %s\n", wine_dbgstr_w(account_name),
476 wine_dbgstr_w(dummy_account_name_b));
477 CoTaskMemFree(account_name);
480 /* Attempt to set to the local system account */
481 hres = ITask_SetAccountInformation(test_task, empty, NULL);
482 ok(hres == S_OK, "Failed setting system account: %08x\n", hres);
483 hres = ITask_GetAccountInformation(test_task, &account_name);
484 ok(hres == S_OK ||
485 broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
486 hres == SCHED_E_CANNOT_OPEN_TASK ||
487 hres == 0x200), /* win2k */
488 "GetAccountInformation failed: %08x\n", hres);
489 if (hres == S_OK)
491 ok(!lstrcmpW(account_name, empty),
492 "Got %s, expected empty string\n", wine_dbgstr_w(account_name));
493 CoTaskMemFree(account_name);
496 cleanup_task();
497 return;
500 static void test_task_state(void)
502 BOOL setup;
503 HRESULT hr, status;
504 DWORD flags;
505 WORD val1;
507 setup = setup_task();
508 ok(setup, "Failed to setup test_task\n");
509 if (!setup)
511 skip("Failed to create task. Skipping tests.\n");
512 return;
515 if (0) /* crashes under Windows */
516 hr = ITask_GetFlags(test_task, NULL);
518 flags = 0xdeadbeef;
519 hr = ITask_GetFlags(test_task, &flags);
520 ok(hr == S_OK, "GetFlags error %#x\n", hr);
521 ok(flags == 0, "got %#x\n", flags);
523 if (0) /* crashes under Windows */
524 hr = ITask_GetTaskFlags(test_task, NULL);
526 flags = 0xdeadbeef;
527 hr = ITask_GetTaskFlags(test_task, &flags);
528 ok(hr == S_OK, "GetTaskFlags error %#x\n", hr);
529 ok(flags == 0, "got %#x\n", flags);
531 if (0) /* crashes under Windows */
532 hr = ITask_GetStatus(test_task, NULL);
534 status = 0xdeadbeef;
535 hr = ITask_GetStatus(test_task, &status);
536 ok(status == SCHED_S_TASK_NOT_SCHEDULED, "got %#x\n", status);
538 if (0) /* crashes under Windows */
539 hr = ITask_GetErrorRetryCount(test_task, NULL);
541 hr = ITask_GetErrorRetryCount(test_task, &val1);
542 ok(hr == E_NOTIMPL, "got %#x\n", hr);
544 cleanup_task();
547 START_TEST(task)
549 CoInitialize(NULL);
550 test_SetApplicationName_GetApplicationName();
551 test_CreateTrigger();
552 test_SetParameters_GetParameters();
553 test_SetComment_GetComment();
554 test_SetMaxRunTime_GetMaxRunTime();
555 test_SetAccountInformation_GetAccountInformation();
556 test_task_state();
557 CoUninitialize();