tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_safeoutputstream.js (1963B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 "use strict";
      6 
      7 function write_atomic(file, str) {
      8  var stream = Cc[
      9    "@mozilla.org/network/atomic-file-output-stream;1"
     10  ].createInstance(Ci.nsIFileOutputStream);
     11  stream.init(file, -1, -1, 0);
     12  do {
     13    var written = stream.write(str, str.length);
     14    if (written == str.length) {
     15      break;
     16    }
     17    str = str.substring(written);
     18  } while (1);
     19  stream.QueryInterface(Ci.nsISafeOutputStream).finish();
     20  stream.close();
     21 }
     22 
     23 function write(file, str) {
     24  var stream = Cc[
     25    "@mozilla.org/network/safe-file-output-stream;1"
     26  ].createInstance(Ci.nsIFileOutputStream);
     27  stream.init(file, -1, -1, 0);
     28  do {
     29    var written = stream.write(str, str.length);
     30    if (written == str.length) {
     31      break;
     32    }
     33    str = str.substring(written);
     34  } while (1);
     35  stream.QueryInterface(Ci.nsISafeOutputStream).finish();
     36  stream.close();
     37 }
     38 
     39 function checkFile(file, str) {
     40  var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
     41    Ci.nsIFileInputStream
     42  );
     43  stream.init(file, -1, -1, 0);
     44 
     45  var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
     46    Ci.nsIScriptableInputStream
     47  );
     48  scriptStream.init(stream);
     49 
     50  Assert.equal(scriptStream.read(scriptStream.available()), str);
     51  scriptStream.close();
     52 }
     53 
     54 function run_test() {
     55  var filename = "\u0913";
     56  var file = Services.dirsvc.get("TmpD", Ci.nsIFile);
     57  file.append(filename);
     58 
     59  write(file, "First write");
     60  checkFile(file, "First write");
     61 
     62  write(file, "Second write");
     63  checkFile(file, "Second write");
     64 
     65  write_atomic(file, "First write: Atomic");
     66  checkFile(file, "First write: Atomic");
     67 
     68  write_atomic(file, "Second write: Atomic");
     69  checkFile(file, "Second write: Atomic");
     70 }