michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const byteStreams = require("sdk/io/byte-streams"); michael@0: const file = require("sdk/io/file"); michael@0: const { pathFor } = require("sdk/system"); michael@0: const { Loader } = require("sdk/test/loader"); michael@0: michael@0: const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used."); michael@0: michael@0: // This should match the constant of the same name in byte-streams.js. michael@0: const BUFFER_BYTE_LEN = 0x8000; michael@0: michael@0: exports.testWriteRead = function (assert) { michael@0: let fname = dataFileFilename(); michael@0: michael@0: // Write a small string less than the stream's buffer size... michael@0: let str = "exports.testWriteRead data!"; michael@0: let stream = open(assert, fname, true); michael@0: assert.ok(!stream.closed, "stream.closed after open should be false"); michael@0: stream.write(str); michael@0: stream.close(); michael@0: assert.ok(stream.closed, "Stream should be closed after stream.close"); michael@0: assert.throws(function () stream.write("This shouldn't be written!"), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.write after close should raise error"); michael@0: michael@0: // ... and read it. michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(), str, michael@0: "stream.read should return string written"); michael@0: assert.equal(stream.read(), "", michael@0: "stream.read at EOS should return empty string"); michael@0: stream.close(); michael@0: assert.ok(stream.closed, "Stream should be closed after stream.close"); michael@0: assert.throws(function () stream.read(), michael@0: STREAM_CLOSED_ERROR, michael@0: "stream.read after close should raise error"); michael@0: michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: // Write a big string many times the size of the stream's buffer and read it. michael@0: exports.testWriteReadBig = function (assert) { michael@0: let str = ""; michael@0: let bufLen = BUFFER_BYTE_LEN; michael@0: let fileSize = bufLen * 10; michael@0: for (let i = 0; i < fileSize; i++) michael@0: str += i % 10; michael@0: let fname = dataFileFilename(); michael@0: let stream = open(assert, fname, true); michael@0: stream.write(str); michael@0: stream.close(); michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(), str, michael@0: "stream.read should return string written"); michael@0: stream.close(); michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: // The same, but write and read in chunks. michael@0: exports.testWriteReadChunks = function (assert) { michael@0: let str = ""; michael@0: let bufLen = BUFFER_BYTE_LEN; michael@0: let fileSize = bufLen * 10; michael@0: for (let i = 0; i < fileSize; i++) michael@0: str += i % 10; michael@0: let fname = dataFileFilename(); michael@0: let stream = open(assert, fname, true); michael@0: let i = 0; michael@0: while (i < str.length) { michael@0: // Use a chunk length that spans buffers. michael@0: let chunk = str.substr(i, bufLen + 1); michael@0: stream.write(chunk); michael@0: i += bufLen + 1; michael@0: } michael@0: stream.close(); michael@0: stream = open(assert, fname); michael@0: let readStr = ""; michael@0: bufLen = BUFFER_BYTE_LEN; michael@0: let readLen = bufLen + 1; michael@0: do { michael@0: var frag = stream.read(readLen); michael@0: readStr += frag; michael@0: } while (frag); michael@0: stream.close(); michael@0: assert.equal(readStr, str, michael@0: "stream.write and read in chunks should work as expected"); michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: exports.testReadLengths = function (assert) { michael@0: let fname = dataFileFilename(); michael@0: let str = "exports.testReadLengths data!"; michael@0: let stream = open(assert, fname, true); michael@0: stream.write(str); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(str.length * 1000), str, michael@0: "stream.read with big byte length should return string " + michael@0: "written"); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(0), "", michael@0: "string.read with zero byte length should return empty " + michael@0: "string"); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(-1), "", michael@0: "string.read with negative byte length should return " + michael@0: "empty string"); michael@0: stream.close(); michael@0: michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: exports.testTruncate = function (assert) { michael@0: let fname = dataFileFilename(); michael@0: let str = "exports.testReadLengths data!"; michael@0: let stream = open(assert, fname, true); michael@0: stream.write(str); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(), str, michael@0: "stream.read should return string written"); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname, true); michael@0: stream.close(); michael@0: michael@0: stream = open(assert, fname); michael@0: assert.equal(stream.read(), "", michael@0: "stream.read after truncate should be empty"); michael@0: stream.close(); michael@0: michael@0: file.remove(fname); michael@0: }; michael@0: michael@0: exports.testUnload = function (assert) { michael@0: let loader = Loader(module); michael@0: let file = loader.require("sdk/io/file"); michael@0: michael@0: let filename = dataFileFilename("temp-b"); michael@0: let stream = file.open(filename, "wb"); michael@0: michael@0: loader.unload(); michael@0: assert.ok(stream.closed, "Stream should be closed after module unload"); michael@0: }; michael@0: michael@0: // Returns the name of a file that should be used to test writing and reading. michael@0: function dataFileFilename() { michael@0: return file.join(pathFor("ProfD"), "test-byte-streams-data"); michael@0: } michael@0: michael@0: // Opens and returns the given file and ensures it's of the correct class. michael@0: function open(assert, filename, forWriting) { michael@0: let stream = file.open(filename, forWriting ? "wb" : "b"); michael@0: let klass = forWriting ? "ByteWriter" : "ByteReader"; michael@0: assert.ok(stream instanceof byteStreams[klass], michael@0: "Opened stream should be a " + klass); michael@0: return stream; michael@0: } michael@0: michael@0: require('sdk/test').run(exports);