test_fault_handler.js (4014B)
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 http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 // This test checks that we properly handle exceptions occuring when a 8 // network mapped drive is disconnected while we're reading from mmaped 9 // file on that drive. 10 // Because sharing folders requires network priviledges, this test cannot 11 // be completely automated. It will create the necessary files and wait 12 // while you run a `net share sharedfolder=...path...` in a CMD terminal 13 // with Administator priviledges. 14 // See bug 1551562 and bug 1707853 15 16 /* import-globals-from ../../zipwriter/test/unit/head_zipwriter.js */ 17 18 function wrapInputStream(input) { 19 let wrapper = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( 20 Ci.nsIScriptableInputStream 21 ); 22 wrapper.init(input); 23 return wrapper; 24 } 25 26 const { Subprocess } = ChromeUtils.importESModule( 27 "resource://gre/modules/Subprocess.sys.mjs" 28 ); 29 30 const XPI_NAME = "testjar.xpi"; 31 const SHARE_NAME = "sharedfolder"; 32 33 async function net(args, silent = false) { 34 if (!silent) { 35 info(`Executing "net ${args.join(" ")}"`); 36 } 37 let proc = await Subprocess.call({ 38 command: "C:\\Windows\\System32\\net.exe", 39 arguments: args, 40 environmentAppend: true, 41 stderr: "stdout", 42 }); 43 let { exitCode } = await proc.wait(); 44 let stdout = await proc.stdout.readString(); 45 if (!silent) { 46 info(`stdout: ${stdout}`); 47 equal(exitCode, 0); 48 } 49 50 // Introduce a small delay so we're sure the command effects are visible 51 await new Promise(resolve => do_timeout(500, resolve)); 52 return stdout; 53 } 54 55 const time = 1199145600000; // Jan 1st 2008 56 57 add_task(async function test() { 58 let tmpFile = do_get_profile().clone(); 59 tmpFile.append("sharedfolder"); 60 tmpFile.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755); 61 let tmpDir = tmpFile.clone(); 62 tmpFile.append(XPI_NAME); 63 64 let DATA = "a".repeat(1024 * 1024 * 10); // 10Mb 65 66 zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); 67 let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( 68 Ci.nsIStringInputStream 69 ); 70 stream.setByteStringData(DATA); 71 zipW.addEntryStream( 72 "data", 73 time * PR_USEC_PER_MSEC, 74 Ci.nsIZipWriter.COMPRESSION_NONE, 75 stream, 76 false 77 ); 78 zipW.close(); 79 80 // find the computer name 81 let lines = await net(["config", "workstation"], true); 82 let COMPUTER_NAME; 83 for (let l of lines.split("\n")) { 84 if (l.startsWith("Computer name")) { 85 COMPUTER_NAME = l.split("\\\\")[1].trim(); 86 } 87 } 88 ok(COMPUTER_NAME); 89 90 dump( 91 `\n\n-------\nNow in a CMD with Administrator priviledges please run:\nnet share ${SHARE_NAME}=${tmpDir.path.replaceAll( 92 "\\\\", 93 "\\" 94 )} /grant:everyone,READ\n\n-------\n` 95 ); 96 97 info("waiting while you run command"); 98 while (true) { 99 let output = await net(["share"], true); 100 if (output.includes(SHARE_NAME)) { 101 break; 102 } 103 } 104 105 // Map the network share to a X: drive 106 await net(["use", "x:", `\\\\${COMPUTER_NAME}\\${SHARE_NAME}`]); 107 108 // the build script have created the zip we can test on in the current dir. 109 let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); 110 file.initWithPath(`X:\\${XPI_NAME}`); 111 info(file.path); 112 ok(file.exists()); 113 114 let zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( 115 Ci.nsIZipReader 116 ); 117 zipreader.open(file); 118 stream = wrapInputStream(zipreader.getInputStream("data")); 119 120 // Delete the X: drive 121 await net(["use", "x:", "/delete", "/y"]); 122 123 // Checks that we get the expected failure 124 Assert.throws( 125 () => { 126 while (true) { 127 Assert.ok(!!stream.read(1024).length); 128 } 129 }, 130 /NS_ERROR_FAILURE/, 131 "Got fault handler exception" 132 ); 133 134 // This part is optional, but it's good to clean up. 135 dump( 136 `\n\n-------\nNow in a CMD with Administrator priviledges please run:\nnet share ${SHARE_NAME} /delete\n\n-------\n` 137 ); 138 });