File: schtasks.c

package info (click to toggle)
wine 5.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 220,300 kB
  • sloc: ansic: 3,035,623; perl: 19,098; yacc: 16,358; makefile: 10,088; javascript: 9,150; objc: 6,590; lex: 5,734; python: 1,914; cpp: 1,042; sh: 759; java: 749; xml: 557; awk: 69; cs: 17
file content (235 lines) | stat: -rw-r--r-- 7,024 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright 2018 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS

#include "initguid.h"
#include "taskschd.h"

#include "wine/test.h"

static ITaskService *service;
static ITaskFolder *root;

static const char xml_a[] =
    "<?xml version=\"1.0\"?>\n"
    "<Task xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">\n"
    "  <RegistrationInfo>\n"
    "    <Description>\"Task1\"</Description>\n"
    "  </RegistrationInfo>\n"
    "  <Settings>\n"
    "    <Enabled>false</Enabled>\n"
    "    <Hidden>false</Hidden>\n"
    "  </Settings>\n"
    "  <Actions>\n"
    "    <Exec>\n"
    "      <Command>\"task1.exe\"</Command>\n"
    "    </Exec>\n"
    "  </Actions>\n"
    "</Task>\n";

static WCHAR *a2w(const char *str)
{
    WCHAR *ret;
    int len;

    if(!str)
        return NULL;

    len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
    ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);

    return ret;
}

#define run_command(a) _run_command(__LINE__,a)
static DWORD _run_command(unsigned line, const char *cmd)
{
    STARTUPINFOA si = {sizeof(STARTUPINFOA)};
    PROCESS_INFORMATION pi;
    char command[1024];
    BOOL r;
    DWORD ret;

    si.dwFlags = STARTF_USESTDHANDLES;
    si.hStdInput  = INVALID_HANDLE_VALUE;
    si.hStdOutput = INVALID_HANDLE_VALUE;
    si.hStdError  = INVALID_HANDLE_VALUE;

    strcpy(command, cmd);
    r = CreateProcessA(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    ok_(__FILE__,line)(r, "CreateProcess failed: %u\n", GetLastError());
    if(!r) return -1;

    ret = WaitForSingleObject(pi.hProcess, 10000);
    ok_(__FILE__,line)(ret == WAIT_OBJECT_0, "wait failed\n");
    if (ret == WAIT_TIMEOUT)
        TerminateProcess(pi.hProcess, -1);

    r = GetExitCodeProcess(pi.hProcess, &ret);
    ok_(__FILE__,line)(r, "GetExitCodeProcess failed: %u\n", GetLastError());

    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    return ret;
}

#define register_task(a) _register_task(__LINE__,a)
static void _register_task(unsigned line, const char *task_name_a)
{
    IRegisteredTask *task;
    VARIANT empty;
    WCHAR *task_name, *xml;
    HRESULT hres;

    V_VT(&empty) = VT_EMPTY;
    task_name = a2w(task_name_a);
    xml = a2w(xml_a);

    /* make sure it's not registered */
    ITaskFolder_DeleteTask(root, task_name, 0);

    hres = ITaskFolder_RegisterTask(root, task_name, xml, TASK_CREATE, empty, empty,
                                    TASK_LOGON_NONE, empty, &task);
    ok_(__FILE__,line)(hres == S_OK, "RegisterTask failed: %08x\n", hres);
    HeapFree(GetProcessHeap(), 0, task_name);
    HeapFree(GetProcessHeap(), 0, xml);

    IRegisteredTask_Release(task);
}

#define unregister_task(a) _unregister_task(__LINE__,a)
static void _unregister_task(unsigned line, const char *task_name_a)
{
    WCHAR *task_name;
    HRESULT hres;

    task_name = a2w(task_name_a);

    hres = ITaskFolder_DeleteTask(root, task_name, 0);
    ok_(__FILE__,line)(hres == S_OK, "DeleteTask failed: %08x\n", hres);

    HeapFree(GetProcessHeap(), 0, task_name);
}

static void create_file(const char *file_name, const char *data)
{
    HANDLE file;
    DWORD size;
    BOOL r;

    file = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
    if(file == INVALID_HANDLE_VALUE)
        return;

    r = WriteFile(file, data, strlen(data), &size, NULL);
    ok(r, "WriteFile failed: %u\n", GetLastError());
    CloseHandle(file);
}

static BOOL initialize_task_service(void)
{
    VARIANT empty;
    HRESULT hres;

    hres = CoCreateInstance(&CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER,
                            &IID_ITaskService, (void **)&service);
    if(hres != S_OK) {
        ok(hres == REGDB_E_CLASSNOTREG, "Could not create task service: %08x\n", hres);
        win_skip("Task service not available\n");
        return FALSE;
    }

    V_VT(&empty) = VT_EMPTY;
    hres = ITaskService_Connect(service, empty, empty, empty, empty);
    ok(hres == S_OK, "Connect failed: %08x\n", hres);

    hres = ITaskService_GetFolder(service, NULL, &root);
    ok(hres == S_OK, "GetFolder error %08x\n", hres);
    return TRUE;
}

START_TEST(schtasks)
{
    static WCHAR wineW[] = {'\\','w','i','n','e',0};
    static WCHAR wine_testW[] = {'\\','w','i','n','e','\\','t','e','s','t',0};
    DWORD r;

    CoInitialize(NULL);
    if(!initialize_task_service()) {
        CoUninitialize();
        return;
    }

    r = run_command("schtasks");
    ok(r == 0, "r = %u\n", r);

    register_task("winetest");

    r = run_command("schtasks /change /tn winetest /enable");
    ok(r == 0, "r = %u\n", r);

    unregister_task("winetest");

    r = run_command("schtasks /change /tn winetest /enable");
    ok(r == 1, "r = %u\n", r);

    register_task("wine\\test\\winetest");

    r = run_command("schtasks /CHANGE /tn wine\\test\\winetest /enable");
    ok(r == 0, "r = %u\n", r);

    r = run_command("schtasks /delete /f /tn wine\\test\\winetest");
    ok(r == 0, "r = %u\n", r);

    r = run_command("schtasks /Change /tn wine\\test\\winetest /enable");
    ok(r == 1, "r = %u\n", r);

    create_file("test.xml", xml_a);

    r = run_command("schtasks /create /xml test.xml /tn wine\\winetest");
    ok(r == 0, "r = %u\n", r);

    r = run_command("schtasks /change /tn wine\\winetest /enable");
    ok(r == 0, "r = %u\n", r);

    r = run_command("schtasks /create /xml test.xml /f /tn wine\\winetest");
    ok(r == 0, "r = %u\n", r); /* task already exists, but /f argument provided */

    r = run_command("schtasks /create /xml test.xml /tn wine\\winetest");
    ok(r == 1, "r = %u\n", r); /* task already exists */

    r = run_command("schtasks /create /tn wine\\winetest");
    ok(r == E_FAIL, "r = %x\n", r); /* missing arguments */

    r = run_command("schtasks /Delete /f /tn wine\\winetest");
    ok(r == 0, "r = %u\n", r);

    r = DeleteFileA("test.xml");
    ok(r, "DeleteFileA failed: %u\n", GetLastError());

    ITaskFolder_DeleteFolder(root, wine_testW, 0);
    ITaskFolder_DeleteFolder(root, wineW, 0);

    ITaskFolder_Release(root);
    ITaskService_Release(service);
    CoUninitialize();
}