quartz/tests: Remove superfluous pointer casts.
[wine] / dlls / mstask / tests / task.c
1 /*
2  * Test suite for Task 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 #include "mstask.h"
25 #include "wine/test.h"
26
27 static ITaskScheduler *test_task_scheduler;
28 static ITask *test_task;
29 static const WCHAR empty[] = {0};
30
31 /* allocate some tmp string space */
32 /* FIXME: this is not 100% thread-safe */
33 static char *get_tmp_space(int size)
34 {
35     static char *list[16];
36     static long pos;
37     char *ret;
38     int idx;
39
40     idx = ++pos % (sizeof(list)/sizeof(list[0]));
41     if ((ret = realloc(list[idx], size)))
42         list[idx] = ret;
43     return ret;
44 }
45
46 static const char *dbgstr_w(LPCWSTR str)
47 {
48     char *buf;
49     int len;
50     if(!str)
51         return "(null)";
52     len = lstrlenW(str) + 1;
53     buf = get_tmp_space(len);
54     WideCharToMultiByte(CP_ACP, 0, str, -1, buf, len, NULL, NULL);
55     return buf;
56 }
57
58 static BOOL setup_task(void)
59 {
60     HRESULT hres;
61     const WCHAR task_name[] = {'T','e','s','t','i','n','g', 0};
62
63     hres = CoCreateInstance(&CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,
64             &IID_ITaskScheduler, (void **) &test_task_scheduler);
65     if(hres != S_OK)
66         return FALSE;
67     hres = ITaskScheduler_NewWorkItem(test_task_scheduler, task_name, &CLSID_CTask,
68             &IID_ITask, (IUnknown**)&test_task);
69     if(hres != S_OK)
70     {
71         ITaskScheduler_Release(test_task_scheduler);
72         return FALSE;
73     }
74     return TRUE;
75 }
76
77 static void cleanup_task(void)
78 {
79     ITask_Release(test_task);
80     ITaskScheduler_Release(test_task_scheduler);
81 }
82
83 static LPCWSTR path_resolve_name(LPCWSTR base_name)
84 {
85     static WCHAR buffer[MAX_PATH];
86     int len;
87
88     len = SearchPathW(NULL, base_name, NULL, 0, NULL, NULL);
89     if (len == 0)
90         return base_name;
91     else if (len < MAX_PATH)
92     {
93         SearchPathW(NULL, base_name, NULL, MAX_PATH, buffer, NULL);
94         return buffer;
95     }
96     return NULL;
97 }
98
99 static void test_SetApplicationName_GetApplicationName(void)
100 {
101     BOOL setup;
102     HRESULT hres;
103     LPWSTR stored_name;
104     LPCWSTR full_name;
105     const WCHAR non_application_name[] = {'N','o','S','u','c','h',
106             'A','p','p','l','i','c','a','t','i','o','n', 0};
107     const WCHAR notepad_exe[] = {
108             'n','o','t','e','p','a','d','.','e','x','e', 0};
109     const WCHAR notepad[] = {'n','o','t','e','p','a','d', 0};
110
111     setup = setup_task();
112     ok(setup, "Failed to setup test_task\n");
113     if (!setup)
114     {
115         skip("Failed to create task.  Skipping tests.\n");
116         return;
117     }
118
119     /* Attempt getting before setting application name */
120     hres = ITask_GetApplicationName(test_task, &stored_name);
121     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
122     if (hres == S_OK)
123     {
124         ok(!lstrcmpiW(stored_name, empty),
125                 "Got %s, expected empty string\n", dbgstr_w(stored_name));
126         CoTaskMemFree(stored_name);
127     }
128
129     /* Set application name to a nonexistent application and then get
130      * the application name that is actually stored */
131     hres = ITask_SetApplicationName(test_task, non_application_name);
132     ok(hres == S_OK, "Failed setting name %s: %08x\n",
133             dbgstr_w(non_application_name), hres);
134     hres = ITask_GetApplicationName(test_task, &stored_name);
135     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
136     if (hres == S_OK)
137     {
138         full_name = path_resolve_name(non_application_name);
139         ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
140                 dbgstr_w(stored_name), dbgstr_w(full_name));
141         CoTaskMemFree(stored_name);
142     }
143
144     /* Set a valid application name with program type extension and then
145      * get the stored name */
146     hres = ITask_SetApplicationName(test_task, notepad_exe);
147     ok(hres == S_OK, "Failed setting name %s: %08x\n",
148             dbgstr_w(notepad_exe), hres);
149     hres = ITask_GetApplicationName(test_task, &stored_name);
150     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
151     if (hres == S_OK)
152     {
153         full_name = path_resolve_name(notepad_exe);
154         ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
155                 dbgstr_w(stored_name), dbgstr_w(full_name));
156         CoTaskMemFree(stored_name);
157     }
158
159     /* Set a valid application name without program type extension and
160      * then get the stored name */
161     hres = ITask_SetApplicationName(test_task, notepad);
162     ok(hres == S_OK, "Failed setting name %s: %08x\n", dbgstr_w(notepad), hres);
163     hres = ITask_GetApplicationName(test_task, &stored_name);
164     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
165     if (hres == S_OK)
166     {
167         full_name = path_resolve_name(notepad_exe);  /* XP SP1 appends .exe */
168         if (lstrcmpiW(stored_name, full_name) != 0)
169         {
170             full_name = path_resolve_name(notepad);
171             ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
172                dbgstr_w(stored_name), dbgstr_w(full_name));
173         }
174         CoTaskMemFree(stored_name);
175     }
176
177     /* After having a valid application name set, set application the name
178      * to a nonexistent application and then get the name that is
179      * actually stored */
180     hres = ITask_SetApplicationName(test_task, non_application_name);
181     ok(hres == S_OK, "Failed setting name %s: %08x\n",
182             dbgstr_w(non_application_name), hres);
183     hres = ITask_GetApplicationName(test_task, &stored_name);
184     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
185     if (hres == S_OK)
186     {
187         full_name = path_resolve_name(non_application_name);
188         ok(!lstrcmpiW(stored_name, full_name), "Got %s, expected %s\n",
189                 dbgstr_w(stored_name), dbgstr_w(full_name));
190         CoTaskMemFree(stored_name);
191     }
192
193     /* Clear application name */
194     hres = ITask_SetApplicationName(test_task, empty);
195     ok(hres == S_OK, "Failed setting name %s: %08x\n", dbgstr_w(empty), hres);
196     hres = ITask_GetApplicationName(test_task, &stored_name);
197     ok(hres == S_OK, "GetApplicationName failed: %08x\n", hres);
198     if (hres == S_OK)
199     {
200         ok(!lstrcmpiW(stored_name, empty),
201                 "Got %s, expected empty string\n", dbgstr_w(stored_name));
202         CoTaskMemFree(stored_name);
203     }
204
205     cleanup_task();
206     return;
207 }
208
209 static void test_CreateTrigger(void)
210 {
211     BOOL setup;
212     HRESULT hres;
213     WORD trigger_index;
214     ITaskTrigger *test_trigger;
215
216     setup = setup_task();
217     ok(setup, "Failed to setup test_task\n");
218     if (!setup)
219     {
220         skip("Failed to create task.  Skipping tests.\n");
221         return;
222     }
223
224     hres = ITask_CreateTrigger(test_task, &trigger_index, &test_trigger);
225     ok(hres == S_OK, "Failed to create trigger: 0x%08x\n", hres);
226     if (hres != S_OK)
227     {
228         cleanup_task();
229         return;
230     }
231
232     ITaskTrigger_Release(test_trigger);
233     cleanup_task();
234     return;
235 }
236
237 static void test_SetParameters_GetParameters(void)
238 {
239     BOOL setup;
240     HRESULT hres;
241     LPWSTR parameters;
242     const WCHAR parameters_a[] = {'f','o','o','.','t','x','t', 0};
243     const WCHAR parameters_b[] = {'f','o','o','.','t','x','t',' ',
244         'b','a','r','.','t','x','t', 0};
245
246     setup = setup_task();
247     ok(setup, "Failed to setup test_task\n");
248     if (!setup)
249     {
250         skip("Failed to create task.  Skipping tests.\n");
251         return;
252     }
253
254     /* Get parameters before setting them */
255     hres = ITask_GetParameters(test_task, &parameters);
256     ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
257     if (hres == S_OK)
258     {
259         ok(!lstrcmpW(parameters, empty),
260                 "Got %s, expected empty string\n", dbgstr_w(parameters));
261         CoTaskMemFree(parameters);
262     }
263
264     /* Set parameters to a simple string */
265     hres = ITask_SetParameters(test_task, parameters_a);
266     ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
267             dbgstr_w(parameters_a), hres);
268     hres = ITask_GetParameters(test_task, &parameters);
269     ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
270     if (hres == S_OK)
271     {
272         ok(!lstrcmpW(parameters, parameters_a), "Got %s, expected %s\n",
273                 dbgstr_w(parameters), dbgstr_w(parameters_a));
274         CoTaskMemFree(parameters);
275     }
276
277     /* Update parameters to a different simple string */
278     hres = ITask_SetParameters(test_task, parameters_b);
279     ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
280             dbgstr_w(parameters_b), hres);
281     hres = ITask_GetParameters(test_task, &parameters);
282     ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
283     if (hres == S_OK)
284     {
285         ok(!lstrcmpW(parameters, parameters_b), "Got %s, expected %s\n",
286                 dbgstr_w(parameters), dbgstr_w(parameters_b));
287         CoTaskMemFree(parameters);
288     }
289
290     /* Clear parameters */
291     hres = ITask_SetParameters(test_task, empty);
292     ok(hres == S_OK, "Failed setting parameters %s: %08x\n",
293             dbgstr_w(empty), hres);
294     hres = ITask_GetParameters(test_task, &parameters);
295     ok(hres == S_OK, "GetParameters failed: %08x\n", hres);
296     if (hres == S_OK)
297     {
298         ok(!lstrcmpW(parameters, empty),
299                 "Got %s, expected empty string\n", dbgstr_w(parameters));
300         CoTaskMemFree(parameters);
301     }
302
303     cleanup_task();
304     return;
305 }
306
307 static void test_SetComment_GetComment(void)
308 {
309     BOOL setup;
310     HRESULT hres;
311     LPWSTR comment;
312     const WCHAR comment_a[] = {'C','o','m','m','e','n','t','.', 0};
313     const WCHAR comment_b[] = {'L','o','n','g','e','r',' ',
314             'c','o','m','m','e','n','t','.', 0};
315
316     setup = setup_task();
317     ok(setup, "Failed to setup test_task\n");
318     if (!setup)
319     {
320         skip("Failed to create task.  Skipping tests.\n");
321         return;
322     }
323
324     /* Get comment before setting it*/
325     hres = ITask_GetComment(test_task, &comment);
326     ok(hres == S_OK, "GetComment failed: %08x\n", hres);
327     if (hres == S_OK)
328     {
329         ok(!lstrcmpW(comment, empty),
330                 "Got %s, expected empty string\n", dbgstr_w(comment));
331         CoTaskMemFree(comment);
332     }
333
334     /* Set comment to a simple string */
335     hres = ITask_SetComment(test_task, comment_a);
336     ok(hres == S_OK, "Failed setting comment %s: %08x\n",
337             dbgstr_w(comment_a), hres);
338     hres = ITask_GetComment(test_task, &comment);
339     ok(hres == S_OK, "GetComment failed: %08x\n", hres);
340     if (hres == S_OK)
341     {
342         ok(!lstrcmpW(comment, comment_a), "Got %s, expected %s\n",
343                 dbgstr_w(comment), dbgstr_w(comment_a));
344         CoTaskMemFree(comment);
345     }
346
347     /* Update comment to a different simple string */
348     hres = ITask_SetComment(test_task, comment_b);
349     ok(hres == S_OK, "Failed setting comment %s: %08x\n",
350             dbgstr_w(comment_b), hres);
351     hres = ITask_GetComment(test_task, &comment);
352     ok(hres == S_OK, "GetComment failed: %08x\n", hres);
353     if (hres == S_OK)
354     {
355         ok(!lstrcmpW(comment, comment_b), "Got %s, expected %s\n",
356                 dbgstr_w(comment), dbgstr_w(comment_b));
357         CoTaskMemFree(comment);
358     }
359
360     /* Clear comment */
361     hres = ITask_SetComment(test_task, empty);
362     ok(hres == S_OK, "Failed setting comment %s: %08x\n",
363             dbgstr_w(empty), hres);
364     hres = ITask_GetComment(test_task, &comment);
365     ok(hres == S_OK, "GetComment failed: %08x\n", hres);
366     if (hres == S_OK)
367     {
368         ok(!lstrcmpW(comment, empty),
369                 "Got %s, expected empty string\n", dbgstr_w(comment));
370         CoTaskMemFree(comment);
371     }
372
373     cleanup_task();
374     return;
375 }
376
377 static void test_SetMaxRunTime_GetMaxRunTime(void)
378 {
379     BOOL setup;
380     HRESULT hres;
381     DWORD max_run_time;
382
383     setup = setup_task();
384     ok(setup, "Failed to setup test_task\n");
385     if (!setup)
386     {
387         skip("Failed to create task.  Skipping tests.\n");
388         return;
389     }
390
391     /* Default time is 3 days:
392      * 3 days * 24 hours * 60 minutes * 60 seconds * 1000 ms = 259200000 */
393     max_run_time = 0;
394     hres = ITask_GetMaxRunTime(test_task, &max_run_time);
395     ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
396     ok(max_run_time == 259200000, "Expected 259200000: %d\n", max_run_time);
397
398     /* Basic set test */
399     max_run_time = 0;
400     hres = ITask_SetMaxRunTime(test_task, 1234);
401     ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
402     hres = ITask_GetMaxRunTime(test_task, &max_run_time);
403     ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
404     ok(max_run_time == 1234, "Expected 1234: %d\n", max_run_time);
405
406     /* Verify that time can be set to zero */
407     max_run_time = 1;
408     hres = ITask_SetMaxRunTime(test_task, 0);
409     ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
410     hres = ITask_GetMaxRunTime(test_task, &max_run_time);
411     ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
412     ok(max_run_time == 0, "Expected 0: %d\n", max_run_time);
413
414     /* Check resolution by setting time to one */
415     max_run_time = 0;
416     hres = ITask_SetMaxRunTime(test_task, 1);
417     ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
418     hres = ITask_GetMaxRunTime(test_task, &max_run_time);
419     ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
420     ok(max_run_time == 1, "Expected 1: %d\n", max_run_time);
421
422     /* Verify that time can be set to INFINITE */
423     max_run_time = 0;
424     hres = ITask_SetMaxRunTime(test_task, INFINITE);
425     ok(hres == S_OK, "Failed to set max runtime: 0x%08x\n", hres);
426     hres = ITask_GetMaxRunTime(test_task, &max_run_time);
427     ok(hres == S_OK, "Failed to get max runtime: 0x%08x\n", hres);
428     ok(max_run_time == INFINITE, "Expected INFINITE: %d\n", max_run_time);
429
430     cleanup_task();
431     return;
432 }
433
434 static void test_SetAccountInformation_GetAccountInformation(void)
435 {
436     BOOL setup;
437     HRESULT hres;
438     LPWSTR account_name;
439     const WCHAR dummy_account_name[] = {'N', 'o', 'S', 'u', 'c', 'h',
440             'A', 'c', 'c', 'o', 'u', 'n', 't', 0};
441     const WCHAR dummy_account_name_b[] = {'N', 'o', 'S', 'u', 'c', 'h',
442             'A', 'c', 'c', 'o', 'u', 'n', 't', 'B', 0};
443
444     setup = setup_task();
445     ok(setup, "Failed to setup test_task\n");
446     if (!setup)
447     {
448         skip("Failed to create task.  Skipping tests.\n");
449         return;
450     }
451
452     /* Get account information before it is set */
453     hres = ITask_GetAccountInformation(test_task, &account_name);
454     /* WinXP returns HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND): 0x80070002 but
455      * Win2K returns SCHED_E_CANNOT_OPEN_TASK: 0x8004130d
456      * Win9x doesn't support security services */
457     if (hres == SCHED_E_NO_SECURITY_SERVICES)
458     {
459         win_skip("Security services are not supported\n");
460         cleanup_task();
461         return;
462     }
463     ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
464             hres == SCHED_E_CANNOT_OPEN_TASK,
465             "Unset account name generated: 0x%08x\n", hres);
466
467     /* Attempt to set to a dummy account without a password */
468     /* This test passes on WinXP but fails on Win2K */
469     hres = ITask_SetAccountInformation(test_task, dummy_account_name, NULL);
470     ok(hres == S_OK,
471             "Failed setting dummy account with no password: %08x\n", hres);
472     hres = ITask_GetAccountInformation(test_task, &account_name);
473     ok(hres == S_OK ||
474        broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
475               hres == SCHED_E_CANNOT_OPEN_TASK),
476        "GetAccountInformation failed: %08x\n", hres);
477     if (hres == S_OK)
478     {
479         ok(!lstrcmpW(account_name, dummy_account_name),
480                 "Got %s, expected %s\n", dbgstr_w(account_name),
481                 dbgstr_w(dummy_account_name));
482         CoTaskMemFree(account_name);
483     }
484
485     /* Attempt to set to a dummy account with a (invalid) password */
486     /* This test passes on WinXP but fails on Win2K */
487     hres = ITask_SetAccountInformation(test_task, dummy_account_name_b,
488             dummy_account_name_b);
489     ok(hres == S_OK,
490             "Failed setting dummy account with password: %08x\n", hres);
491     hres = ITask_GetAccountInformation(test_task, &account_name);
492     ok(hres == S_OK ||
493        broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
494               hres == SCHED_E_CANNOT_OPEN_TASK),
495        "GetAccountInformation failed: %08x\n", hres);
496     if (hres == S_OK)
497     {
498         ok(!lstrcmpW(account_name, dummy_account_name_b),
499                 "Got %s, expected %s\n", dbgstr_w(account_name),
500                 dbgstr_w(dummy_account_name_b));
501         CoTaskMemFree(account_name);
502     }
503
504     /* Attempt to set to the local system account */
505     hres = ITask_SetAccountInformation(test_task, empty, NULL);
506     ok(hres == S_OK, "Failed setting system account: %08x\n", hres);
507     hres = ITask_GetAccountInformation(test_task, &account_name);
508     ok(hres == S_OK ||
509        broken(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
510               hres == SCHED_E_CANNOT_OPEN_TASK),
511        "GetAccountInformation failed: %08x\n", hres);
512     if (hres == S_OK)
513     {
514         ok(!lstrcmpW(account_name, empty),
515                 "Got %s, expected empty string\n", dbgstr_w(account_name));
516         CoTaskMemFree(account_name);
517     }
518
519     cleanup_task();
520     return;
521 }
522
523 START_TEST(task)
524 {
525     CoInitialize(NULL);
526     test_SetApplicationName_GetApplicationName();
527     test_CreateTrigger();
528     test_SetParameters_GetParameters();
529     test_SetComment_GetComment();
530     test_SetMaxRunTime_GetMaxRunTime();
531     test_SetAccountInformation_GetAccountInformation();
532     CoUninitialize();
533 }