test_streams.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. var Ci = Components.interfaces;
  5. var Cr = Components.results;
  6. var CC = Components.Constructor;
  7. var Pipe = CC("@mozilla.org/pipe;1",
  8. "nsIPipe",
  9. "init");
  10. var BinaryOutput = CC("@mozilla.org/binaryoutputstream;1",
  11. "nsIBinaryOutputStream",
  12. "setOutputStream");
  13. var BinaryInput = CC("@mozilla.org/binaryinputstream;1",
  14. "nsIBinaryInputStream",
  15. "setInputStream");
  16. /**
  17. * Binary stream tests.
  18. */
  19. function test_binary_streams() {
  20. var p, is, os;
  21. p = new Pipe(false, false, 1024, 1, null);
  22. is = new BinaryInput(p.inputStream);
  23. os = new BinaryOutput(p.outputStream);
  24. const LargeNum = Math.pow(2, 18) + Math.pow(2, 12) + 1;
  25. const HugeNum = Math.pow(2, 62);
  26. const HelloStr = "Hello World";
  27. const HelloArray = Array.map(HelloStr, function(c) {return c.charCodeAt(0)});
  28. var countObj = {};
  29. var msg = {};
  30. var buffer = new ArrayBuffer(HelloArray.length);
  31. // Test reading immediately after writing.
  32. os.writeBoolean(true);
  33. do_check_eq(is.readBoolean(), true);
  34. os.write8(4);
  35. do_check_eq(is.read8(), 4);
  36. os.write16(4);
  37. do_check_eq(is.read16(), 4);
  38. os.write16(1024);
  39. do_check_eq(is.read16(), 1024);
  40. os.write32(7);
  41. do_check_eq(is.read32(), 7);
  42. os.write32(LargeNum);
  43. do_check_eq(is.read32(), LargeNum);
  44. os.write64(LargeNum);
  45. do_check_eq(is.read64(), LargeNum);
  46. os.write64(1024);
  47. do_check_eq(is.read64(), 1024);
  48. os.write64(HugeNum);
  49. do_check_eq(is.read64(), HugeNum);
  50. os.writeFloat(2.5);
  51. do_check_eq(is.readFloat(), 2.5);
  52. // os.writeDouble(Math.SQRT2);
  53. // do_check_eq(is.readDouble(), Math.SQRT2);
  54. os.writeStringZ("Mozilla");
  55. do_check_eq(is.readCString(), "Mozilla");
  56. os.writeWStringZ("Gecko");
  57. do_check_eq(is.readString(), "Gecko");
  58. os.writeBytes(HelloStr, HelloStr.length);
  59. do_check_eq(is.available(), HelloStr.length);
  60. msg = is.readBytes(HelloStr.length);
  61. do_check_eq(msg, HelloStr);
  62. msg = null;
  63. countObj.value = -1;
  64. os.writeByteArray(HelloArray, HelloArray.length);
  65. do_check_eq(is.available(), HelloStr.length);
  66. msg = is.readByteArray(HelloStr.length)
  67. do_check_eq(typeof msg, typeof HelloArray);
  68. do_check_eq(msg.toSource(), HelloArray.toSource());
  69. do_check_eq(is.available(), 0);
  70. os.writeByteArray(HelloArray, HelloArray.length);
  71. do_check_eq(is.readArrayBuffer(buffer.byteLength, buffer), HelloArray.length);
  72. do_check_eq([...(new Uint8Array(buffer))].toSource(), HelloArray.toSource());
  73. do_check_eq(is.available(), 0);
  74. // Test writing in one big chunk.
  75. os.writeBoolean(true);
  76. os.write8(4);
  77. os.write16(4);
  78. os.write16(1024);
  79. os.write32(7);
  80. os.write32(LargeNum);
  81. os.write64(LargeNum);
  82. os.write64(1024);
  83. os.write64(HugeNum);
  84. os.writeFloat(2.5);
  85. // os.writeDouble(Math.SQRT2);
  86. os.writeStringZ("Mozilla");
  87. os.writeWStringZ("Gecko");
  88. os.writeBytes(HelloStr, HelloStr.length);
  89. os.writeByteArray(HelloArray, HelloArray.length);
  90. // Available should not be zero after a long write like this.
  91. do_check_neq(is.available(), 0);
  92. // Test reading in one big chunk.
  93. do_check_eq(is.readBoolean(), true);
  94. do_check_eq(is.read8(), 4);
  95. do_check_eq(is.read16(), 4);
  96. do_check_eq(is.read16(), 1024);
  97. do_check_eq(is.read32(), 7);
  98. do_check_eq(is.read32(), LargeNum);
  99. do_check_eq(is.read64(), LargeNum);
  100. do_check_eq(is.read64(), 1024);
  101. do_check_eq(is.read64(), HugeNum);
  102. do_check_eq(is.readFloat(), 2.5);
  103. // do_check_eq(is.readDouble(), Math.SQRT2);
  104. do_check_eq(is.readCString(), "Mozilla");
  105. do_check_eq(is.readString(), "Gecko");
  106. // Remember, we wrote HelloStr twice - once as a string, and then as an array.
  107. do_check_eq(is.available(), HelloStr.length * 2);
  108. msg = is.readBytes(HelloStr.length);
  109. do_check_eq(msg, HelloStr);
  110. msg = null;
  111. countObj.value = -1;
  112. do_check_eq(is.available(), HelloStr.length);
  113. msg = is.readByteArray(HelloStr.length)
  114. do_check_eq(typeof msg, typeof HelloArray);
  115. do_check_eq(msg.toSource(), HelloArray.toSource());
  116. do_check_eq(is.available(), 0);
  117. // Test for invalid actions.
  118. os.close();
  119. is.close();
  120. try {
  121. os.writeBoolean(false);
  122. do_throw("Not reached!");
  123. } catch (e if (e instanceof Ci.nsIException &&
  124. e.result == Cr.NS_BASE_STREAM_CLOSED)) {
  125. // do nothing
  126. }
  127. try {
  128. is.available();
  129. do_throw("Not reached!");
  130. } catch (e if (e instanceof Ci.nsIException &&
  131. e.result == Cr.NS_BASE_STREAM_CLOSED)) {
  132. // do nothing
  133. }
  134. try {
  135. is.readBoolean();
  136. do_throw("Not reached!");
  137. } catch (e if (e instanceof Ci.nsIException &&
  138. e.result == Cr.NS_ERROR_FAILURE)) {
  139. // do nothing
  140. }
  141. }
  142. function run_test() {
  143. test_binary_streams();
  144. }