test_storagestream.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. var Cc = Components.classes;
  7. var Ci = Components.interfaces;
  8. var Cr = Components.results;
  9. function run_test()
  10. {
  11. test1();
  12. test2();
  13. test3();
  14. test4();
  15. }
  16. /**
  17. * Checks that getting an input stream from a storage stream which has never had
  18. * anything written to it throws a not-initialized exception.
  19. */
  20. function test1()
  21. {
  22. var ss = Cc["@mozilla.org/storagestream;1"]
  23. .createInstance(Ci.nsIStorageStream);
  24. ss.init(1024, 1024, null);
  25. var out = ss.getOutputStream(0);
  26. var inp2 = ss.newInputStream(0);
  27. do_check_eq(inp2.available(), 0);
  28. do_check_true(inp2.isNonBlocking());
  29. var sis =
  30. Cc["@mozilla.org/scriptableinputstream;1"]
  31. .createInstance(Ci.nsIScriptableInputStream);
  32. sis.init(inp2);
  33. var threw = false;
  34. try {
  35. sis.read(1);
  36. } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) {
  37. threw = true;
  38. }
  39. do_check_true(threw);
  40. }
  41. /**
  42. * Checks that getting an input stream from a storage stream to which 0 bytes of
  43. * data have been explicitly written doesn't throw an exception.
  44. */
  45. function test2()
  46. {
  47. var ss = Cc["@mozilla.org/storagestream;1"]
  48. .createInstance(Ci.nsIStorageStream);
  49. ss.init(1024, 1024, null);
  50. var out = ss.getOutputStream(0);
  51. out.write("", 0);
  52. try
  53. {
  54. var inp2 = ss.newInputStream(0);
  55. }
  56. catch (e)
  57. {
  58. do_throw("shouldn't throw exception when new input stream created");
  59. }
  60. }
  61. /**
  62. * Checks that reading any non-zero amount of data from a storage stream
  63. * which has had 0 bytes written to it explicitly works correctly.
  64. */
  65. function test3()
  66. {
  67. var ss = Cc["@mozilla.org/storagestream;1"]
  68. .createInstance(Ci.nsIStorageStream);
  69. ss.init(1024, 1024, null);
  70. var out = ss.getOutputStream(0);
  71. out.write("", 0);
  72. try
  73. {
  74. var inp = ss.newInputStream(0);
  75. }
  76. catch (e)
  77. {
  78. do_throw("newInputStream(0) shouldn't throw if write() is called: " + e);
  79. }
  80. do_check_true(inp.isNonBlocking(), "next test expects a non-blocking stream");
  81. try
  82. {
  83. var threw = false;
  84. var bis = BIS(inp);
  85. var dummy = bis.readByteArray(5);
  86. }
  87. catch (e)
  88. {
  89. if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK)
  90. do_throw("wrong error thrown: " + e);
  91. threw = true;
  92. }
  93. do_check_true(threw,
  94. "should have thrown (nsStorageInputStream is nonblocking)");
  95. }
  96. /**
  97. * Basic functionality test for storagestream: write data to it, get an input
  98. * stream, and read the data back to see that it matches.
  99. */
  100. function test4()
  101. {
  102. var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74];
  103. var ss = Cc["@mozilla.org/storagestream;1"]
  104. .createInstance(Ci.nsIStorageStream);
  105. ss.init(1024, 1024, null);
  106. var outStream = ss.getOutputStream(0);
  107. var bos = Cc["@mozilla.org/binaryoutputstream;1"]
  108. .createInstance(Ci.nsIBinaryOutputStream);
  109. bos.setOutputStream(outStream);
  110. bos.writeByteArray(bytes, bytes.length);
  111. bos.close();
  112. var inp = ss.newInputStream(0);
  113. var bis = BIS(inp);
  114. var count = 0;
  115. while (count < bytes.length)
  116. {
  117. var data = bis.read8(1);
  118. do_check_eq(data, bytes[count++]);
  119. }
  120. var threw = false;
  121. try
  122. {
  123. data = bis.read8(1);
  124. }
  125. catch (e)
  126. {
  127. if (e.result != Cr.NS_ERROR_FAILURE)
  128. do_throw("wrong error thrown: " + e);
  129. threw = true;
  130. }
  131. if (!threw)
  132. do_throw("should have thrown but instead returned: '" + data + "'");
  133. }
  134. function BIS(input)
  135. {
  136. var bis = Cc["@mozilla.org/binaryinputstream;1"]
  137. .createInstance(Ci.nsIBinaryInputStream);
  138. bis.setInputStream(input);
  139. return bis;
  140. }