summaryrefslogtreecommitdiffstats
path: root/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerWinImpl.js
blob: d0c2a6c357fa522edf3207694fa39c12c41b995c (plain)
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
"use strict";

// Unit tests for Windows scheduled task generation.

const { updateAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);
updateAppInfo();

const { TaskScheduler } = ChromeUtils.importESModule(
  "resource://gre/modules/TaskScheduler.sys.mjs"
);

const { WinImpl } = ChromeUtils.importESModule(
  "resource://gre/modules/TaskSchedulerWinImpl.sys.mjs"
);

const WinSvc = Cc["@mozilla.org/win-task-scheduler-service;1"].getService(
  Ci.nsIWinTaskSchedulerService
);

const uuidGenerator = Services.uuid;

function randomName() {
  return (
    "moz-taskschd-test-" + uuidGenerator.generateUUID().toString().slice(1, -1)
  );
}

const gFolderName = randomName();

// Override task folder name, to prevent colliding with other tests.
WinImpl._taskFolderName = function () {
  return gFolderName;
};
WinImpl._taskFolderNameParts = function () {
  return {
    parentName: "\\",
    subName: gFolderName,
  };
};

registerCleanupFunction(async () => {
  await TaskScheduler.deleteAllTasks();
});

add_task(async function test_create() {
  const taskName = "test-task-1";
  const rawTaskName = WinImpl._formatTaskName(taskName);
  const folderName = WinImpl._taskFolderName();
  const exePath = "C:\\Program Files\\XYZ\\123.exe";
  const workingDir = "C:\\Program Files\\XYZ";
  const argsIn = [
    "x.txt",
    "c:\\x.txt",
    'C:\\"HELLO WORLD".txt',
    "only space.txt",
  ];
  const expectedArgsOutStr = [
    "x.txt",
    "c:\\x.txt",
    '"C:\\\\\\"HELLO WORLD\\".txt"',
    '"only space.txt"',
  ].join(" ");
  const description = "Entities: < &. Non-ASCII: abc😀def.";
  const intervalSecsIn = 2 * 60 * 60; // 2 hours
  const expectedIntervalOutWin10 = "PT2H"; // Windows 10 regroups by hours and minutes
  const expectedIntervalOutWin7 = `PT${intervalSecsIn}S`; // Windows 7 doesn't regroup

  await TaskScheduler.registerTask(taskName, exePath, intervalSecsIn, {
    disabled: true,
    args: argsIn,
    description,
    workingDirectory: workingDir,
  });

  // Read back the task
  const readBackXML = WinSvc.getTaskXML(folderName, rawTaskName);
  const parser = new DOMParser();
  const doc = parser.parseFromString(readBackXML, "text/xml");
  Assert.equal(doc.documentElement.tagName, "Task");

  // Check for the values set above
  Assert.equal(doc.querySelector("Actions Exec Command").textContent, exePath);
  Assert.equal(
    doc.querySelector("Actions Exec WorkingDirectory").textContent,
    workingDir
  );
  Assert.equal(
    doc.querySelector("Actions Exec Arguments").textContent,
    expectedArgsOutStr
  );
  Assert.equal(
    doc.querySelector("RegistrationInfo Description").textContent,
    description
  );
  Assert.equal(
    doc.querySelector("RegistrationInfo Author").textContent,
    Services.appinfo.vendor
  );

  Assert.equal(doc.querySelector("Settings Enabled").textContent, "false");

  // Note: It's a little too tricky to check for a specific StartBoundary value reliably here, given
  // that it gets set relative to Date.now(), so I'm skipping that.
  const intervalOut = doc.querySelector(
    "Triggers TimeTrigger Repetition Interval"
  ).textContent;
  Assert.ok(
    intervalOut == expectedIntervalOutWin7 ||
      intervalOut == expectedIntervalOutWin10
  );

  // Validate the XML
  WinSvc.validateTaskDefinition(readBackXML);

  // Update
  const updatedExePath = "C:\\Program Files (x86)\\ABC\\foo.exe";
  const updatedIntervalSecsIn = 3 * 60 * 60; // 3 hours
  const expectedUpdatedIntervalOutWin10 = "PT3H";
  const expectedUpdatedIntervalOutWin7 = `PT${updatedIntervalSecsIn}S`;

  await TaskScheduler.registerTask(
    taskName,
    updatedExePath,
    updatedIntervalSecsIn,
    {
      disabled: true,
      args: argsIn,
      description,
      workingDirectory: workingDir,
    }
  );

  // Read back the updated task
  const readBackUpdatedXML = WinSvc.getTaskXML(folderName, rawTaskName);
  const updatedDoc = parser.parseFromString(readBackUpdatedXML, "text/xml");
  Assert.equal(updatedDoc.documentElement.tagName, "Task");

  // Check for updated values
  Assert.equal(
    updatedDoc.querySelector("Actions Exec Command").textContent,
    updatedExePath
  );

  Assert.notEqual(
    doc.querySelector("Triggers TimeTrigger StartBoundary").textContent,
    updatedDoc.querySelector("Triggers TimeTrigger StartBoundary").textContent
  );
  const updatedIntervalOut = updatedDoc.querySelector(
    "Triggers TimeTrigger Repetition Interval"
  ).textContent;
  Assert.ok(
    updatedIntervalOut == expectedUpdatedIntervalOutWin7 ||
      updatedIntervalOut == expectedUpdatedIntervalOutWin10
  );

  // Check that the folder really was there
  {
    const { parentName, subName } = WinImpl._taskFolderNameParts();
    let threw;
    try {
      WinSvc.deleteFolder(parentName, subName);
    } catch (ex) {
      threw = ex;
    }
    Assert.equal(threw.result, Cr.NS_ERROR_FILE_DIR_NOT_EMPTY);
  }

  // Delete
  await TaskScheduler.deleteAllTasks();

  // Check that the folder is gone
  {
    const { parentName, subName } = WinImpl._taskFolderNameParts();
    let threw;
    try {
      WinSvc.deleteFolder(parentName, subName);
    } catch (ex) {
      threw = ex;
    }
    Assert.equal(threw.result, Cr.NS_ERROR_FILE_NOT_FOUND);
  }

  // Format and validate the XML with the task not disabled
  const enabledXML = WinImpl._formatTaskDefinitionXML(exePath, intervalSecsIn, {
    args: argsIn,
    description,
    workingDirectory: workingDir,
  });
  Assert.equal(WinSvc.validateTaskDefinition(enabledXML), 0 /* S_OK */);

  // Format and validate with no options
  const basicXML = WinImpl._formatTaskDefinitionXML(
    "foo",
    TaskScheduler.MIN_INTERVAL_SECONDS
  );
  Assert.equal(WinSvc.validateTaskDefinition(basicXML), 0 /* S_OK */);
});