test-byte-streams.js 5.0 KB

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