test_seek_multiplex.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Cc = Components.classes;
  8. // The string we use as data.
  9. const data = "0123456789";
  10. // Number of streams in the multiplex stream.
  11. const count = 10;
  12. function test_multiplex_streams() {
  13. var MultiplexStream = CC("@mozilla.org/io/multiplex-input-stream;1",
  14. "nsIMultiplexInputStream");
  15. do_check_eq(1, 1);
  16. var BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1",
  17. "nsIBinaryInputStream");
  18. var BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1",
  19. "nsIBinaryOutputStream",
  20. "setOutputStream");
  21. var multiplex = new MultiplexStream();
  22. for (var i = 0; i < count; ++i) {
  23. let s = Cc["@mozilla.org/io/string-input-stream;1"]
  24. .createInstance(Ci.nsIStringInputStream);
  25. s.setData(data, data.length);
  26. multiplex.appendStream(s);
  27. }
  28. var seekable = multiplex.QueryInterface(Ci.nsISeekableStream);
  29. var sis = Cc["@mozilla.org/scriptableinputstream;1"]
  30. .createInstance(Ci.nsIScriptableInputStream);
  31. sis.init(seekable);
  32. // Read some data.
  33. var readData = sis.read(20);
  34. do_check_eq(readData, data + data);
  35. // -- Tests for NS_SEEK_SET
  36. // Seek to a non-zero, non-stream-boundary offset.
  37. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 2);
  38. do_check_eq(seekable.tell(), 2);
  39. do_check_eq(seekable.available(), 98);
  40. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 9);
  41. do_check_eq(seekable.tell(), 9);
  42. do_check_eq(seekable.available(), 91);
  43. // Seek across stream boundary.
  44. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 35);
  45. do_check_eq(seekable.tell(), 35);
  46. do_check_eq(seekable.available(), 65);
  47. readData = sis.read(5);
  48. do_check_eq(readData, data.slice(5));
  49. do_check_eq(seekable.available(), 60);
  50. // Seek at stream boundaries.
  51. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 40);
  52. do_check_eq(seekable.tell(), 40);
  53. do_check_eq(seekable.available(), 60);
  54. readData = sis.read(10);
  55. do_check_eq(readData, data);
  56. do_check_eq(seekable.tell(), 50);
  57. do_check_eq(seekable.available(), 50);
  58. // Rewind and read across streams.
  59. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 39);
  60. do_check_eq(seekable.tell(), 39);
  61. do_check_eq(seekable.available(), 61);
  62. readData = sis.read(11);
  63. do_check_eq(readData, data.slice(9) + data);
  64. do_check_eq(seekable.tell(), 50);
  65. do_check_eq(seekable.available(), 50);
  66. // Rewind to the beginning.
  67. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
  68. do_check_eq(seekable.tell(), 0);
  69. do_check_eq(seekable.available(), 100);
  70. // Seek to some random location
  71. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 50);
  72. // -- Tests for NS_SEEK_CUR
  73. // Positive seek.
  74. seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 15);
  75. do_check_eq(seekable.tell(), 65);
  76. do_check_eq(seekable.available(), 35);
  77. readData = sis.read(10);
  78. do_check_eq(readData, data.slice(5) + data.slice(0, 5));
  79. do_check_eq(seekable.tell(), 75);
  80. do_check_eq(seekable.available(), 25);
  81. // Negative seek.
  82. seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15);
  83. do_check_eq(seekable.tell(), 60);
  84. do_check_eq(seekable.available(), 40);
  85. readData = sis.read(10);
  86. do_check_eq(readData, data);
  87. do_check_eq(seekable.tell(), 70);
  88. do_check_eq(seekable.available(), 30);
  89. // -- Tests for NS_SEEK_END
  90. // Normal read.
  91. seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -5);
  92. do_check_eq(seekable.tell(), data.length * count - 5);
  93. readData = sis.read(5);
  94. do_check_eq(readData, data.slice(5));
  95. do_check_eq(seekable.tell(), data.length * count);
  96. // Read across streams.
  97. seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -15);
  98. do_check_eq(seekable.tell(), data.length * count - 15);
  99. readData = sis.read(15);
  100. do_check_eq(readData, data.slice(5) + data);
  101. do_check_eq(seekable.tell(), data.length * count);
  102. // -- Try to do various edge cases
  103. // Forward seek from the end, should throw.
  104. var caught = false;
  105. try {
  106. seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, 15);
  107. } catch(e) {
  108. caught = true;
  109. }
  110. do_check_eq(caught, true);
  111. do_check_eq(seekable.tell(), data.length * count);
  112. // Backward seek from the beginning, should be clamped.
  113. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
  114. do_check_eq(seekable.tell(), 0);
  115. seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15);
  116. do_check_eq(seekable.tell(), 0);
  117. // Seek too far: should be clamped.
  118. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
  119. do_check_eq(seekable.tell(), 0);
  120. seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 3 * data.length * count);
  121. do_check_eq(seekable.tell(), 100);
  122. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, data.length * count);
  123. do_check_eq(seekable.tell(), 100);
  124. seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -2 * data.length * count);
  125. do_check_eq(seekable.tell(), 0);
  126. }
  127. function test_multiplex_bug797871() {
  128. var data = "1234567890123456789012345678901234567890";
  129. var MultiplexStream = CC("@mozilla.org/io/multiplex-input-stream;1",
  130. "nsIMultiplexInputStream");
  131. do_check_eq(1, 1);
  132. var BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1",
  133. "nsIBinaryInputStream");
  134. var BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1",
  135. "nsIBinaryOutputStream",
  136. "setOutputStream");
  137. var multiplex = new MultiplexStream();
  138. let s = Cc["@mozilla.org/io/string-input-stream;1"]
  139. .createInstance(Ci.nsIStringInputStream);
  140. s.setData(data, data.length);
  141. multiplex.appendStream(s);
  142. var seekable = multiplex.QueryInterface(Ci.nsISeekableStream);
  143. var sis = Cc["@mozilla.org/scriptableinputstream;1"]
  144. .createInstance(Ci.nsIScriptableInputStream);
  145. sis.init(seekable);
  146. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 8);
  147. do_check_eq(seekable.tell(), 8);
  148. readData = sis.read(2);
  149. do_check_eq(seekable.tell(), 10);
  150. seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 20);
  151. do_check_eq(seekable.tell(), 20);
  152. }
  153. function run_test() {
  154. test_multiplex_streams();
  155. test_multiplex_bug797871();
  156. }