browser_sandbox_test.js (2647B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 function test() { 8 waitForExplicitFinish(); 9 10 // Types of processes to test, taken from GeckoProcessTypes.h 11 // GPU process might not run depending on the platform, so we need it to be 12 // the last one of the list to allow the remainingTests logic below to work 13 // as expected. 14 // 15 // For UtilityProcess, allow constructing a string made of the process type 16 // and the sandbox variant we want to test, e.g., 17 // utility:0 for GENERIC_UTILITY 18 // utility:1 for AppleMedia/WMF on macOS/Windows 19 var processTypes = ["tab", "socket", "rdd", "gmplugin", "utility:0", "gpu"]; 20 21 const platform = SpecialPowers.Services.appinfo.OS; 22 if (platform === "WINNT" || platform === "Darwin") { 23 processTypes.push("utility:1"); 24 } 25 26 // A callback called after each test-result. 27 let sandboxTestResult = (subject, topic, data) => { 28 let { testid, passed, message } = JSON.parse(data); 29 ok( 30 passed, 31 "Test " + testid + (passed ? " passed: " : " failed: ") + message 32 ); 33 }; 34 Services.obs.addObserver(sandboxTestResult, "sandbox-test-result"); 35 36 var remainingTests = processTypes.length; 37 38 // A callback that is notified when a child process is done running tests. 39 let sandboxTestDone = () => { 40 remainingTests = remainingTests - 1; 41 if (remainingTests == 0) { 42 // Clean up test file 43 if (homeTestFile.exists()) { 44 ok(homeTestFile.isFile(), "homeTestFile should be a file"); 45 if (homeTestFile.isFile()) { 46 homeTestFile.remove(false); 47 } 48 } 49 50 Services.obs.removeObserver(sandboxTestResult, "sandbox-test-result"); 51 Services.obs.removeObserver(sandboxTestDone, "sandbox-test-done"); 52 53 // Notify SandboxTest component that it should terminate the connection 54 // with the child processes. 55 comp.finishTests(); 56 // Notify mochitest that all process tests are complete. 57 finish(); 58 } 59 }; 60 Services.obs.addObserver(sandboxTestDone, "sandbox-test-done"); 61 62 var comp = Cc["@mozilla.org/sandbox/sandbox-test;1"].getService( 63 Ci.mozISandboxTest 64 ); 65 66 let homeTestFile; 67 try { 68 homeTestFile = Services.dirsvc.get("Home", Ci.nsIFile); 69 homeTestFile.append(".mozilla_gpu_sandbox_read_test"); 70 if (!homeTestFile.exists()) { 71 homeTestFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600); 72 } 73 } catch (e) { 74 ok(false, "Failed to create home test file: " + e); 75 } 76 77 comp.startTests(processTypes); 78 }