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