Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const byteStreams = require("sdk/io/byte-streams"); |
michael@0 | 6 | const file = require("sdk/io/file"); |
michael@0 | 7 | const { pathFor } = require("sdk/system"); |
michael@0 | 8 | const { Loader } = require("sdk/test/loader"); |
michael@0 | 9 | |
michael@0 | 10 | const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used."); |
michael@0 | 11 | |
michael@0 | 12 | // This should match the constant of the same name in byte-streams.js. |
michael@0 | 13 | const BUFFER_BYTE_LEN = 0x8000; |
michael@0 | 14 | |
michael@0 | 15 | exports.testWriteRead = function (assert) { |
michael@0 | 16 | let fname = dataFileFilename(); |
michael@0 | 17 | |
michael@0 | 18 | // Write a small string less than the stream's buffer size... |
michael@0 | 19 | let str = "exports.testWriteRead data!"; |
michael@0 | 20 | let stream = open(assert, fname, true); |
michael@0 | 21 | assert.ok(!stream.closed, "stream.closed after open should be false"); |
michael@0 | 22 | stream.write(str); |
michael@0 | 23 | stream.close(); |
michael@0 | 24 | assert.ok(stream.closed, "Stream should be closed after stream.close"); |
michael@0 | 25 | assert.throws(function () stream.write("This shouldn't be written!"), |
michael@0 | 26 | STREAM_CLOSED_ERROR, |
michael@0 | 27 | "stream.write after close should raise error"); |
michael@0 | 28 | |
michael@0 | 29 | // ... and read it. |
michael@0 | 30 | stream = open(assert, fname); |
michael@0 | 31 | assert.equal(stream.read(), str, |
michael@0 | 32 | "stream.read should return string written"); |
michael@0 | 33 | assert.equal(stream.read(), "", |
michael@0 | 34 | "stream.read at EOS should return empty string"); |
michael@0 | 35 | stream.close(); |
michael@0 | 36 | assert.ok(stream.closed, "Stream should be closed after stream.close"); |
michael@0 | 37 | assert.throws(function () stream.read(), |
michael@0 | 38 | STREAM_CLOSED_ERROR, |
michael@0 | 39 | "stream.read after close should raise error"); |
michael@0 | 40 | |
michael@0 | 41 | file.remove(fname); |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | // Write a big string many times the size of the stream's buffer and read it. |
michael@0 | 45 | exports.testWriteReadBig = function (assert) { |
michael@0 | 46 | let str = ""; |
michael@0 | 47 | let bufLen = BUFFER_BYTE_LEN; |
michael@0 | 48 | let fileSize = bufLen * 10; |
michael@0 | 49 | for (let i = 0; i < fileSize; i++) |
michael@0 | 50 | str += i % 10; |
michael@0 | 51 | let fname = dataFileFilename(); |
michael@0 | 52 | let stream = open(assert, fname, true); |
michael@0 | 53 | stream.write(str); |
michael@0 | 54 | stream.close(); |
michael@0 | 55 | stream = open(assert, fname); |
michael@0 | 56 | assert.equal(stream.read(), str, |
michael@0 | 57 | "stream.read should return string written"); |
michael@0 | 58 | stream.close(); |
michael@0 | 59 | file.remove(fname); |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | // The same, but write and read in chunks. |
michael@0 | 63 | exports.testWriteReadChunks = function (assert) { |
michael@0 | 64 | let str = ""; |
michael@0 | 65 | let bufLen = BUFFER_BYTE_LEN; |
michael@0 | 66 | let fileSize = bufLen * 10; |
michael@0 | 67 | for (let i = 0; i < fileSize; i++) |
michael@0 | 68 | str += i % 10; |
michael@0 | 69 | let fname = dataFileFilename(); |
michael@0 | 70 | let stream = open(assert, fname, true); |
michael@0 | 71 | let i = 0; |
michael@0 | 72 | while (i < str.length) { |
michael@0 | 73 | // Use a chunk length that spans buffers. |
michael@0 | 74 | let chunk = str.substr(i, bufLen + 1); |
michael@0 | 75 | stream.write(chunk); |
michael@0 | 76 | i += bufLen + 1; |
michael@0 | 77 | } |
michael@0 | 78 | stream.close(); |
michael@0 | 79 | stream = open(assert, fname); |
michael@0 | 80 | let readStr = ""; |
michael@0 | 81 | bufLen = BUFFER_BYTE_LEN; |
michael@0 | 82 | let readLen = bufLen + 1; |
michael@0 | 83 | do { |
michael@0 | 84 | var frag = stream.read(readLen); |
michael@0 | 85 | readStr += frag; |
michael@0 | 86 | } while (frag); |
michael@0 | 87 | stream.close(); |
michael@0 | 88 | assert.equal(readStr, str, |
michael@0 | 89 | "stream.write and read in chunks should work as expected"); |
michael@0 | 90 | file.remove(fname); |
michael@0 | 91 | }; |
michael@0 | 92 | |
michael@0 | 93 | exports.testReadLengths = function (assert) { |
michael@0 | 94 | let fname = dataFileFilename(); |
michael@0 | 95 | let str = "exports.testReadLengths data!"; |
michael@0 | 96 | let stream = open(assert, fname, true); |
michael@0 | 97 | stream.write(str); |
michael@0 | 98 | stream.close(); |
michael@0 | 99 | |
michael@0 | 100 | stream = open(assert, fname); |
michael@0 | 101 | assert.equal(stream.read(str.length * 1000), str, |
michael@0 | 102 | "stream.read with big byte length should return string " + |
michael@0 | 103 | "written"); |
michael@0 | 104 | stream.close(); |
michael@0 | 105 | |
michael@0 | 106 | stream = open(assert, fname); |
michael@0 | 107 | assert.equal(stream.read(0), "", |
michael@0 | 108 | "string.read with zero byte length should return empty " + |
michael@0 | 109 | "string"); |
michael@0 | 110 | stream.close(); |
michael@0 | 111 | |
michael@0 | 112 | stream = open(assert, fname); |
michael@0 | 113 | assert.equal(stream.read(-1), "", |
michael@0 | 114 | "string.read with negative byte length should return " + |
michael@0 | 115 | "empty string"); |
michael@0 | 116 | stream.close(); |
michael@0 | 117 | |
michael@0 | 118 | file.remove(fname); |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | exports.testTruncate = function (assert) { |
michael@0 | 122 | let fname = dataFileFilename(); |
michael@0 | 123 | let str = "exports.testReadLengths data!"; |
michael@0 | 124 | let stream = open(assert, fname, true); |
michael@0 | 125 | stream.write(str); |
michael@0 | 126 | stream.close(); |
michael@0 | 127 | |
michael@0 | 128 | stream = open(assert, fname); |
michael@0 | 129 | assert.equal(stream.read(), str, |
michael@0 | 130 | "stream.read should return string written"); |
michael@0 | 131 | stream.close(); |
michael@0 | 132 | |
michael@0 | 133 | stream = open(assert, fname, true); |
michael@0 | 134 | stream.close(); |
michael@0 | 135 | |
michael@0 | 136 | stream = open(assert, fname); |
michael@0 | 137 | assert.equal(stream.read(), "", |
michael@0 | 138 | "stream.read after truncate should be empty"); |
michael@0 | 139 | stream.close(); |
michael@0 | 140 | |
michael@0 | 141 | file.remove(fname); |
michael@0 | 142 | }; |
michael@0 | 143 | |
michael@0 | 144 | exports.testUnload = function (assert) { |
michael@0 | 145 | let loader = Loader(module); |
michael@0 | 146 | let file = loader.require("sdk/io/file"); |
michael@0 | 147 | |
michael@0 | 148 | let filename = dataFileFilename("temp-b"); |
michael@0 | 149 | let stream = file.open(filename, "wb"); |
michael@0 | 150 | |
michael@0 | 151 | loader.unload(); |
michael@0 | 152 | assert.ok(stream.closed, "Stream should be closed after module unload"); |
michael@0 | 153 | }; |
michael@0 | 154 | |
michael@0 | 155 | // Returns the name of a file that should be used to test writing and reading. |
michael@0 | 156 | function dataFileFilename() { |
michael@0 | 157 | return file.join(pathFor("ProfD"), "test-byte-streams-data"); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Opens and returns the given file and ensures it's of the correct class. |
michael@0 | 161 | function open(assert, filename, forWriting) { |
michael@0 | 162 | let stream = file.open(filename, forWriting ? "wb" : "b"); |
michael@0 | 163 | let klass = forWriting ? "ByteWriter" : "ByteReader"; |
michael@0 | 164 | assert.ok(stream instanceof byteStreams[klass], |
michael@0 | 165 | "Opened stream should be a " + klass); |
michael@0 | 166 | return stream; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | require('sdk/test').run(exports); |