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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
const { FileUtils } = ChromeUtils.importESModule(
"resource://gre/modules/FileUtils.sys.mjs"
);
const { FirefoxBridgeExtensionUtils } = ChromeUtils.importESModule(
"resource:///modules/FirefoxBridgeExtensionUtils.sys.mjs"
);
const DUAL_BROWSER_EXTENSION_ORIGIN = ["chrome-extension://fake-origin/"];
const NATIVE_MESSAGING_HOST_ID = "org.mozilla.firefox_bridge_test";
if (AppConstants.platform == "win") {
var { MockRegistry } = ChromeUtils.importESModule(
"resource://testing-common/MockRegistry.sys.mjs"
);
}
let registry = null;
add_setup(() => {
if (AppConstants.platform == "win") {
registry = new MockRegistry();
registerCleanupFunction(() => {
registry.shutdown();
});
}
});
function resetMockRegistry() {
if (AppConstants.platform != "win") {
return;
}
registry.shutdown();
registry = new MockRegistry();
}
let dir = FileUtils.getDir("TmpD", ["NativeMessagingHostsTest"]);
dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
let userDir = dir.clone();
userDir.append("user");
userDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
let appDir = dir.clone();
appDir.append("app");
appDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
let dirProvider = {
getFile(property) {
if (property == "Home") {
return userDir.clone();
} else if (property == "AppData") {
return appDir.clone();
}
return null;
},
};
try {
Services.dirsvc.undefine("Home");
} catch (e) {}
try {
Services.dirsvc.undefine("AppData");
} catch (e) {}
Services.dirsvc.registerProvider(dirProvider);
registerCleanupFunction(() => {
Services.dirsvc.unregisterProvider(dirProvider);
dir.remove(true);
});
const USER_TEST_PATH = PathUtils.join(userDir.path, "manifestDir");
let binFile = null;
add_setup(async function () {
binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent.clone();
if (AppConstants.platform == "win") {
binFile.append("nmhproxy.exe");
} else if (AppConstants.platform == "macosx") {
binFile.append("nmhproxy");
} else {
throw new Error("Unsupported platform");
}
});
function getExpectedOutput() {
return {
name: NATIVE_MESSAGING_HOST_ID,
description: "Firefox Native Messaging Host",
path: binFile.path,
type: "stdio",
allowed_origins: DUAL_BROWSER_EXTENSION_ORIGIN,
};
}
function DumpWindowsRegistry() {
let key = "";
let pathBuffer = [];
if (AppConstants.platform == "win") {
function bufferPrint(line) {
let regPath = line.trimStart();
if (regPath.includes(":")) {
// After trimming white space, keys are formatted as
// ": <key> (<value_type>)". We can assume it's only ever
// going to be of type REG_SZ for this test.
key = regPath.slice(2, regPath.length - " (REG_SZ)".length);
} else {
pathBuffer.push(regPath);
}
}
MockRegistry.dump(
MockRegistry.getRoot(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER),
"",
bufferPrint
);
} else {
Assert.ok(false, "Only windows has a registry!");
}
return [pathBuffer, key];
}
add_task(async function test_maybeWriteManifestFiles() {
await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
USER_TEST_PATH,
NATIVE_MESSAGING_HOST_ID,
DUAL_BROWSER_EXTENSION_ORIGIN
);
let expectedOutput = JSON.stringify(getExpectedOutput());
let nmhManifestFilePath = PathUtils.join(
USER_TEST_PATH,
`${NATIVE_MESSAGING_HOST_ID}.json`
);
let nmhManifestFileContent = await IOUtils.readUTF8(nmhManifestFilePath);
await IOUtils.remove(nmhManifestFilePath);
Assert.equal(nmhManifestFileContent, expectedOutput);
});
add_task(async function test_maybeWriteManifestFilesIncorrect() {
let nmhManifestFile = await IOUtils.getFile(
USER_TEST_PATH,
`${NATIVE_MESSAGING_HOST_ID}.json`
);
let incorrectInput = {
name: NATIVE_MESSAGING_HOST_ID,
description: "Manifest with unexpected description",
path: binFile.path,
type: "stdio",
allowed_origins: DUAL_BROWSER_EXTENSION_ORIGIN,
};
await IOUtils.writeJSON(nmhManifestFile.path, incorrectInput);
// Write correct JSON to the file and check to make sure it matches
// the expected output
await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
USER_TEST_PATH,
NATIVE_MESSAGING_HOST_ID,
DUAL_BROWSER_EXTENSION_ORIGIN
);
let expectedOutput = JSON.stringify(getExpectedOutput());
let nmhManifestFilePath = PathUtils.join(
USER_TEST_PATH,
`${NATIVE_MESSAGING_HOST_ID}.json`
);
let nmhManifestFileContent = await IOUtils.readUTF8(nmhManifestFilePath);
await IOUtils.remove(nmhManifestFilePath);
Assert.equal(nmhManifestFileContent, expectedOutput);
});
add_task(async function test_maybeWriteManifestFilesAlreadyExists() {
// Write file and confirm it exists
await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
USER_TEST_PATH,
NATIVE_MESSAGING_HOST_ID,
DUAL_BROWSER_EXTENSION_ORIGIN
);
let nmhManifestFile = await IOUtils.getFile(
USER_TEST_PATH,
`${NATIVE_MESSAGING_HOST_ID}.json`
);
// Modify file modificatiomn time to be older than the write time
let oldModificationTime = Date.now() - 1000000;
let setModificationTime = await IOUtils.setModificationTime(
nmhManifestFile.path,
oldModificationTime
);
Assert.equal(oldModificationTime, setModificationTime);
// Call function which writes correct JSON to the file and make sure
// the modification time is the same, meaning we haven't written anything
await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
USER_TEST_PATH,
NATIVE_MESSAGING_HOST_ID,
DUAL_BROWSER_EXTENSION_ORIGIN
);
let stat = await IOUtils.stat(nmhManifestFile.path);
await IOUtils.remove(nmhManifestFile.path);
Assert.equal(stat.lastModified, oldModificationTime);
});
add_task(async function test_maybeWriteManifestFilesDirDoesNotExist() {
let testDir = dir.clone();
// This folder does not exist, so we want to make sure it's created
testDir.append("dirDoesNotExist");
await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
testDir.path,
NATIVE_MESSAGING_HOST_ID,
DUAL_BROWSER_EXTENSION_ORIGIN
);
ok(await IOUtils.exists(testDir.path));
ok(
await IOUtils.exists(
PathUtils.join(testDir.path, `${NATIVE_MESSAGING_HOST_ID}.json`)
)
);
await IOUtils.remove(testDir.path, { recursive: true });
});
add_task(async function test_ensureRegistered() {
let expectedJSONDirPath = null;
let nativeHostId = "org.mozilla.firefox_bridge_nmh";
if (AppConstants.NIGHTLY_BUILD) {
nativeHostId = "org.mozilla.firefox_bridge_nmh_nightly";
} else if (AppConstants.MOZ_DEV_EDITION) {
nativeHostId = "org.mozilla.firefox_bridge_nmh_dev";
} else if (AppConstants.IS_ESR) {
nativeHostId = "org.mozilla.firefox_bridge_nmh_esr";
}
if (AppConstants.platform == "macosx") {
expectedJSONDirPath = PathUtils.joinRelative(
userDir.path,
"Library/Application Support/Google/Chrome/NativeMessagingHosts/"
);
} else if (AppConstants.platform == "win") {
expectedJSONDirPath = PathUtils.joinRelative(
appDir.path,
"Mozilla\\Firefox"
);
resetMockRegistry();
} else {
throw new Error("Unsupported platform");
}
ok(!(await IOUtils.exists(expectedJSONDirPath)));
let expectedJSONPath = PathUtils.join(
expectedJSONDirPath,
`${nativeHostId}.json`
);
await FirefoxBridgeExtensionUtils.ensureRegistered();
let realOutput = {
name: nativeHostId,
description: "Firefox Native Messaging Host",
path: binFile.path,
type: "stdio",
allowed_origins: FirefoxBridgeExtensionUtils.getExtensionOrigins(),
};
let expectedOutput = JSON.stringify(realOutput);
let JSONContent = await IOUtils.readUTF8(expectedJSONPath);
await IOUtils.remove(expectedJSONPath);
Assert.equal(JSONContent, expectedOutput);
// Test that the registry key is written for Windows only
if (AppConstants.platform == "win") {
let [pathBuffer, key] = DumpWindowsRegistry();
Assert.equal(
pathBuffer.toString(),
[
"Software",
"Google",
"Chrome",
"NativeMessagingHosts",
nativeHostId,
].toString()
);
Assert.equal(key, expectedJSONPath);
}
});
add_task(async function test_maybeWriteNativeMessagingRegKeys() {
if (AppConstants.platform != "win") {
return;
}
resetMockRegistry();
FirefoxBridgeExtensionUtils.maybeWriteNativeMessagingRegKeys(
"Test\\Path\\For\\Reg\\Key",
binFile.parent.path,
NATIVE_MESSAGING_HOST_ID
);
let [pathBuffer, key] = DumpWindowsRegistry();
registry.shutdown();
Assert.equal(
pathBuffer.toString(),
["Test", "Path", "For", "Reg", "Key", NATIVE_MESSAGING_HOST_ID].toString()
);
console.log("The key is: " + key);
Assert.equal(key, `${binFile.parent.path}\\${NATIVE_MESSAGING_HOST_ID}.json`);
});
add_task(async function test_maybeWriteNativeMessagingRegKeysIncorrectValue() {
if (AppConstants.platform != "win") {
return;
}
resetMockRegistry();
registry.setValue(
Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
`Test\\Path\\For\\Reg\\Key\\${NATIVE_MESSAGING_HOST_ID}`,
"",
"IncorrectValue"
);
FirefoxBridgeExtensionUtils.maybeWriteNativeMessagingRegKeys(
"Test\\Path\\For\\Reg\\Key",
binFile.parent.path,
NATIVE_MESSAGING_HOST_ID
);
let [pathBuffer, key] = DumpWindowsRegistry();
registry.shutdown();
Assert.equal(
pathBuffer.toString(),
["Test", "Path", "For", "Reg", "Key", NATIVE_MESSAGING_HOST_ID].toString()
);
Assert.equal(key, `${binFile.parent.path}\\${NATIVE_MESSAGING_HOST_ID}.json`);
});
|