addon-sdk/source/test/test-text-streams.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 file = require("sdk/io/file");
michael@0 6 const { pathFor } = require("sdk/system");
michael@0 7 const { Loader } = require("sdk/test/loader");
michael@0 8
michael@0 9 const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used.");
michael@0 10
michael@0 11 // This should match the constant of the same name in text-streams.js.
michael@0 12 const BUFFER_BYTE_LEN = 0x8000;
michael@0 13
michael@0 14 exports.testWriteRead = function (assert) {
michael@0 15 let fname = dataFileFilename();
michael@0 16
michael@0 17 // Write a small string less than the stream's buffer size...
michael@0 18 let str = "exports.testWriteRead data!";
michael@0 19 let stream = file.open(fname, "w");
michael@0 20 assert.ok(!stream.closed, "stream.closed after open should be false");
michael@0 21 stream.write(str);
michael@0 22 stream.close();
michael@0 23 assert.ok(stream.closed, "stream.closed after close should be true");
michael@0 24 assert.throws(function () stream.close(),
michael@0 25 STREAM_CLOSED_ERROR,
michael@0 26 "stream.close after already closed should raise error");
michael@0 27 assert.throws(function () stream.write("This shouldn't be written!"),
michael@0 28 STREAM_CLOSED_ERROR,
michael@0 29 "stream.write after close should raise error");
michael@0 30
michael@0 31 // ... and read it.
michael@0 32 stream = file.open(fname);
michael@0 33 assert.ok(!stream.closed, "stream.closed after open should be false");
michael@0 34 assert.equal(stream.read(), str,
michael@0 35 "stream.read should return string written");
michael@0 36 assert.equal(stream.read(), "",
michael@0 37 "stream.read at EOS should return empty string");
michael@0 38 stream.close();
michael@0 39 assert.ok(stream.closed, "stream.closed after close should be true");
michael@0 40 assert.throws(function () stream.close(),
michael@0 41 STREAM_CLOSED_ERROR,
michael@0 42 "stream.close after already closed should raise error");
michael@0 43 assert.throws(function () stream.read(),
michael@0 44 STREAM_CLOSED_ERROR,
michael@0 45 "stream.read after close should raise error");
michael@0 46
michael@0 47 // Write a big string many times the size of the stream's buffer and read it.
michael@0 48 // Since it comes after the previous test, this also ensures that the file is
michael@0 49 // truncated when it's opened for writing.
michael@0 50 str = "";
michael@0 51 let bufLen = BUFFER_BYTE_LEN;
michael@0 52 let fileSize = bufLen * 10;
michael@0 53 for (let i = 0; i < fileSize; i++)
michael@0 54 str += i % 10;
michael@0 55 stream = file.open(fname, "w");
michael@0 56 stream.write(str);
michael@0 57 stream.close();
michael@0 58 stream = file.open(fname);
michael@0 59 assert.equal(stream.read(), str,
michael@0 60 "stream.read should return string written");
michael@0 61 stream.close();
michael@0 62
michael@0 63 // The same, but write and read in chunks.
michael@0 64 stream = file.open(fname, "w");
michael@0 65 let i = 0;
michael@0 66 while (i < str.length) {
michael@0 67 // Use a chunk length that spans buffers.
michael@0 68 let chunk = str.substr(i, bufLen + 1);
michael@0 69 stream.write(chunk);
michael@0 70 i += bufLen + 1;
michael@0 71 }
michael@0 72 stream.close();
michael@0 73 stream = file.open(fname);
michael@0 74 let readStr = "";
michael@0 75 bufLen = BUFFER_BYTE_LEN;
michael@0 76 let readLen = bufLen + 1;
michael@0 77 do {
michael@0 78 var frag = stream.read(readLen);
michael@0 79 readStr += frag;
michael@0 80 } while (frag);
michael@0 81 stream.close();
michael@0 82 assert.equal(readStr, str,
michael@0 83 "stream.write and read in chunks should work as expected");
michael@0 84
michael@0 85 // Read the same file, passing in strange numbers of bytes to read.
michael@0 86 stream = file.open(fname);
michael@0 87 assert.equal(stream.read(fileSize * 100), str,
michael@0 88 "stream.read with big byte length should return string " +
michael@0 89 "written");
michael@0 90 stream.close();
michael@0 91
michael@0 92 stream = file.open(fname);
michael@0 93 assert.equal(stream.read(0), "",
michael@0 94 "string.read with zero byte length should return empty " +
michael@0 95 "string");
michael@0 96 stream.close();
michael@0 97
michael@0 98 stream = file.open(fname);
michael@0 99 assert.equal(stream.read(-1), "",
michael@0 100 "string.read with negative byte length should return " +
michael@0 101 "empty string");
michael@0 102 stream.close();
michael@0 103
michael@0 104 file.remove(fname);
michael@0 105 };
michael@0 106
michael@0 107 exports.testWriteAsync = function (assert, done) {
michael@0 108 let fname = dataFileFilename();
michael@0 109 let str = "exports.testWriteAsync data!";
michael@0 110 let stream = file.open(fname, "w");
michael@0 111 assert.ok(!stream.closed, "stream.closed after open should be false");
michael@0 112
michael@0 113 // Write.
michael@0 114 stream.writeAsync(str, function (err) {
michael@0 115 assert.equal(this, stream, "|this| should be the stream object");
michael@0 116 assert.equal(err, undefined,
michael@0 117 "stream.writeAsync should not cause error");
michael@0 118 assert.ok(stream.closed, "stream.closed after write should be true");
michael@0 119 assert.throws(function () stream.close(),
michael@0 120 STREAM_CLOSED_ERROR,
michael@0 121 "stream.close after already closed should raise error");
michael@0 122 assert.throws(function () stream.writeAsync("This shouldn't work!"),
michael@0 123 STREAM_CLOSED_ERROR,
michael@0 124 "stream.writeAsync after close should raise error");
michael@0 125
michael@0 126 // Read.
michael@0 127 stream = file.open(fname, "r");
michael@0 128 assert.ok(!stream.closed, "stream.closed after open should be false");
michael@0 129 let readStr = stream.read();
michael@0 130 assert.equal(readStr, str,
michael@0 131 "string.read should yield string written");
michael@0 132 stream.close();
michael@0 133 file.remove(fname);
michael@0 134 done();
michael@0 135 });
michael@0 136 };
michael@0 137
michael@0 138 exports.testUnload = function (assert) {
michael@0 139 let loader = Loader(module);
michael@0 140 let file = loader.require("sdk/io/file");
michael@0 141
michael@0 142 let filename = dataFileFilename("temp");
michael@0 143 let stream = file.open(filename, "w");
michael@0 144
michael@0 145 loader.unload();
michael@0 146 assert.ok(stream.closed, "stream should be closed after module unload");
michael@0 147 };
michael@0 148
michael@0 149 // Returns the name of a file that should be used to test writing and reading.
michael@0 150 function dataFileFilename() {
michael@0 151 return file.join(pathFor("ProfD"), "test-text-streams-data");
michael@0 152 }
michael@0 153
michael@0 154 require('sdk/test').run(exports);

mercurial