test-text-streams.js 5.4 KB

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