Bug 1890689 Don't pretend to pre-buffer with DynamicResampler r=pehrsons
[gecko.git] / browser / modules / test / unit / test_FirefoxBridgeExtensionUtilsNativeManifest.js
blobcef550d705ba805c17e80f7fa743194c5b83056b
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const { AppConstants } = ChromeUtils.importESModule(
7   "resource://gre/modules/AppConstants.sys.mjs"
8 );
9 const { FileUtils } = ChromeUtils.importESModule(
10   "resource://gre/modules/FileUtils.sys.mjs"
12 const { FirefoxBridgeExtensionUtils } = ChromeUtils.importESModule(
13   "resource:///modules/FirefoxBridgeExtensionUtils.sys.mjs"
16 const DUAL_BROWSER_EXTENSION_ORIGIN = ["chrome-extension://fake-origin/"];
17 const NATIVE_MESSAGING_HOST_ID = "org.mozilla.firefox_bridge_test";
19 let dir = FileUtils.getDir("TmpD", ["NativeMessagingHostsTest"]);
20 dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
22 let userDir = dir.clone();
23 userDir.append("user");
24 userDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
26 let appDir = dir.clone();
27 appDir.append("app");
28 appDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
30 let dirProvider = {
31   getFile(property) {
32     if (property == "Home") {
33       return userDir.clone();
34     } else if (property == "AppData") {
35       return appDir.clone();
36     }
37     return null;
38   },
41 try {
42   Services.dirsvc.undefine("Home");
43 } catch (e) {}
44 try {
45   Services.dirsvc.undefine("AppData");
46 } catch (e) {}
47 Services.dirsvc.registerProvider(dirProvider);
49 registerCleanupFunction(() => {
50   Services.dirsvc.unregisterProvider(dirProvider);
51   dir.remove(true);
52 });
54 const USER_TEST_PATH = PathUtils.join(userDir.path, "manifestDir");
56 let binFile = null;
57 add_setup(async function () {
58   binFile = Services.dirsvc.get("XREExeF", Ci.nsIFile).parent.clone();
59   if (AppConstants.platform == "win") {
60     binFile.append("nmhproxy.exe");
61   } else if (AppConstants.platform == "macosx") {
62     binFile.append("nmhproxy");
63   } else {
64     throw new Error("Unsupported platform");
65   }
66 });
68 function getExpectedOutput() {
69   return {
70     name: NATIVE_MESSAGING_HOST_ID,
71     description: "Firefox Native Messaging Host",
72     path: binFile.path,
73     type: "stdio",
74     allowed_origins: DUAL_BROWSER_EXTENSION_ORIGIN,
75   };
78 add_task(async function test_maybeWriteManifestFiles() {
79   await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
80     USER_TEST_PATH,
81     NATIVE_MESSAGING_HOST_ID,
82     DUAL_BROWSER_EXTENSION_ORIGIN
83   );
84   let expectedOutput = JSON.stringify(getExpectedOutput());
85   let nmhManifestFilePath = PathUtils.join(
86     USER_TEST_PATH,
87     `${NATIVE_MESSAGING_HOST_ID}.json`
88   );
89   let nmhManifestFileContent = await IOUtils.readUTF8(nmhManifestFilePath);
90   await IOUtils.remove(nmhManifestFilePath);
91   Assert.equal(nmhManifestFileContent, expectedOutput);
92 });
94 add_task(async function test_maybeWriteManifestFilesIncorrect() {
95   let nmhManifestFile = await IOUtils.getFile(
96     USER_TEST_PATH,
97     `${NATIVE_MESSAGING_HOST_ID}.json`
98   );
100   let incorrectInput = {
101     name: NATIVE_MESSAGING_HOST_ID,
102     description: "Manifest with unexpected description",
103     path: binFile.path,
104     type: "stdio",
105     allowed_origins: DUAL_BROWSER_EXTENSION_ORIGIN,
106   };
107   await IOUtils.writeJSON(nmhManifestFile.path, incorrectInput);
109   // Write correct JSON to the file and check to make sure it matches
110   // the expected output
111   await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
112     USER_TEST_PATH,
113     NATIVE_MESSAGING_HOST_ID,
114     DUAL_BROWSER_EXTENSION_ORIGIN
115   );
116   let expectedOutput = JSON.stringify(getExpectedOutput());
118   let nmhManifestFilePath = PathUtils.join(
119     USER_TEST_PATH,
120     `${NATIVE_MESSAGING_HOST_ID}.json`
121   );
122   let nmhManifestFileContent = await IOUtils.readUTF8(nmhManifestFilePath);
123   await IOUtils.remove(nmhManifestFilePath);
124   Assert.equal(nmhManifestFileContent, expectedOutput);
127 add_task(async function test_maybeWriteManifestFilesAlreadyExists() {
128   // Write file and confirm it exists
129   await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
130     USER_TEST_PATH,
131     NATIVE_MESSAGING_HOST_ID,
132     DUAL_BROWSER_EXTENSION_ORIGIN
133   );
134   let nmhManifestFile = await IOUtils.getFile(
135     USER_TEST_PATH,
136     `${NATIVE_MESSAGING_HOST_ID}.json`
137   );
139   // Modify file modificatiomn time to be older than the write time
140   let oldModificationTime = Date.now() - 1000000;
141   let setModificationTime = await IOUtils.setModificationTime(
142     nmhManifestFile.path,
143     oldModificationTime
144   );
145   Assert.equal(oldModificationTime, setModificationTime);
147   // Call function which writes correct JSON to the file and make sure
148   // the modification time is the same, meaning we haven't written anything
149   await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
150     USER_TEST_PATH,
151     NATIVE_MESSAGING_HOST_ID,
152     DUAL_BROWSER_EXTENSION_ORIGIN
153   );
154   let stat = await IOUtils.stat(nmhManifestFile.path);
155   await IOUtils.remove(nmhManifestFile.path);
156   Assert.equal(stat.lastModified, oldModificationTime);
159 add_task(async function test_maybeWriteManifestFilesDirDoesNotExist() {
160   let testDir = dir.clone();
161   // This folder does not exist, so we want to make sure it's created
162   testDir.append("dirDoesNotExist");
163   await FirefoxBridgeExtensionUtils.maybeWriteManifestFiles(
164     testDir.path,
165     NATIVE_MESSAGING_HOST_ID,
166     DUAL_BROWSER_EXTENSION_ORIGIN
167   );
169   ok(await IOUtils.exists(testDir.path));
170   ok(
171     await IOUtils.exists(
172       PathUtils.join(testDir.path, `${NATIVE_MESSAGING_HOST_ID}.json`)
173     )
174   );
175   await IOUtils.remove(testDir.path, { recursive: true });
178 add_task(async function test_ensureRegistered() {
179   let expectedJSONDirPath = null;
180   let nativeHostId = "org.mozilla.firefox_bridge_nmh";
181   if (AppConstants.NIGHTLY_BUILD) {
182     nativeHostId = "org.mozilla.firefox_bridge_nmh_nightly";
183   } else if (AppConstants.MOZ_DEV_EDITION) {
184     nativeHostId = "org.mozilla.firefox_bridge_nmh_dev";
185   } else if (AppConstants.IS_ESR) {
186     nativeHostId = "org.mozilla.firefox_bridge_nmh_esr";
187   }
189   if (AppConstants.platform == "macosx") {
190     expectedJSONDirPath = PathUtils.joinRelative(
191       userDir.path,
192       "Library/Application Support/Google/Chrome/NativeMessagingHosts/"
193     );
194   } else if (AppConstants.platform == "win") {
195     expectedJSONDirPath = PathUtils.joinRelative(
196       appDir.path,
197       "Mozilla\\Firefox"
198     );
199   } else {
200     throw new Error("Unsupported platform");
201   }
203   ok(!(await IOUtils.exists(expectedJSONDirPath)));
204   let expectedJSONPath = PathUtils.join(
205     expectedJSONDirPath,
206     `${nativeHostId}.json`
207   );
209   await FirefoxBridgeExtensionUtils.ensureRegistered();
210   let realOutput = {
211     name: nativeHostId,
212     description: "Firefox Native Messaging Host",
213     path: binFile.path,
214     type: "stdio",
215     allowed_origins: FirefoxBridgeExtensionUtils.getExtensionOrigins(),
216   };
218   let expectedOutput = JSON.stringify(realOutput);
219   let JSONContent = await IOUtils.readUTF8(expectedJSONPath);
220   await IOUtils.remove(expectedJSONPath);
221   Assert.equal(JSONContent, expectedOutput);