1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/unit/test_seek_multiplex.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Ci = Components.interfaces; 1.9 +const Cr = Components.results; 1.10 +const CC = Components.Constructor; 1.11 +const Cc = Components.classes; 1.12 + 1.13 +// The string we use as data. 1.14 +const data = "0123456789"; 1.15 +// Number of streams in the multiplex stream. 1.16 +const count = 10; 1.17 + 1.18 +function test_multiplex_streams() { 1.19 + var MultiplexStream = CC("@mozilla.org/io/multiplex-input-stream;1", 1.20 + "nsIMultiplexInputStream"); 1.21 + do_check_eq(1, 1); 1.22 + 1.23 + var BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1", 1.24 + "nsIBinaryInputStream"); 1.25 + var BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1", 1.26 + "nsIBinaryOutputStream", 1.27 + "setOutputStream"); 1.28 + var multiplex = new MultiplexStream(); 1.29 + for (var i = 0; i < count; ++i) { 1.30 + let s = Cc["@mozilla.org/io/string-input-stream;1"] 1.31 + .createInstance(Ci.nsIStringInputStream); 1.32 + s.setData(data, data.length); 1.33 + 1.34 + multiplex.appendStream(s); 1.35 + } 1.36 + var seekable = multiplex.QueryInterface(Ci.nsISeekableStream); 1.37 + var sis = Cc["@mozilla.org/scriptableinputstream;1"] 1.38 + .createInstance(Ci.nsIScriptableInputStream); 1.39 + sis.init(seekable); 1.40 + // Read some data. 1.41 + var readData = sis.read(20); 1.42 + do_check_eq(readData, data + data); 1.43 + // -- Tests for NS_SEEK_SET 1.44 + // Seek to a non-zero, non-stream-boundary offset. 1.45 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 2); 1.46 + do_check_eq(seekable.tell(), 2); 1.47 + do_check_eq(seekable.available(), 98); 1.48 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 9); 1.49 + do_check_eq(seekable.tell(), 9); 1.50 + do_check_eq(seekable.available(), 91); 1.51 + // Seek across stream boundary. 1.52 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 35); 1.53 + do_check_eq(seekable.tell(), 35); 1.54 + do_check_eq(seekable.available(), 65); 1.55 + readData = sis.read(5); 1.56 + do_check_eq(readData, data.slice(5)); 1.57 + do_check_eq(seekable.available(), 60); 1.58 + // Seek at stream boundaries. 1.59 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 40); 1.60 + do_check_eq(seekable.tell(), 40); 1.61 + do_check_eq(seekable.available(), 60); 1.62 + readData = sis.read(10); 1.63 + do_check_eq(readData, data); 1.64 + do_check_eq(seekable.tell(), 50); 1.65 + do_check_eq(seekable.available(), 50); 1.66 + // Rewind and read across streams. 1.67 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 39); 1.68 + do_check_eq(seekable.tell(), 39); 1.69 + do_check_eq(seekable.available(), 61); 1.70 + readData = sis.read(11); 1.71 + do_check_eq(readData, data.slice(9) + data); 1.72 + do_check_eq(seekable.tell(), 50); 1.73 + do_check_eq(seekable.available(), 50); 1.74 + // Rewind to the beginning. 1.75 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); 1.76 + do_check_eq(seekable.tell(), 0); 1.77 + do_check_eq(seekable.available(), 100); 1.78 + // Seek to some random location 1.79 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 50); 1.80 + // -- Tests for NS_SEEK_CUR 1.81 + // Positive seek. 1.82 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 15); 1.83 + do_check_eq(seekable.tell(), 65); 1.84 + do_check_eq(seekable.available(), 35); 1.85 + readData = sis.read(10); 1.86 + do_check_eq(readData, data.slice(5) + data.slice(0, 5)); 1.87 + do_check_eq(seekable.tell(), 75); 1.88 + do_check_eq(seekable.available(), 25); 1.89 + // Negative seek. 1.90 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15); 1.91 + do_check_eq(seekable.tell(), 60); 1.92 + do_check_eq(seekable.available(), 40); 1.93 + readData = sis.read(10); 1.94 + do_check_eq(readData, data); 1.95 + do_check_eq(seekable.tell(), 70); 1.96 + do_check_eq(seekable.available(), 30); 1.97 + 1.98 + // -- Tests for NS_SEEK_END 1.99 + // Normal read. 1.100 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -5); 1.101 + do_check_eq(seekable.tell(), data.length * count - 5); 1.102 + readData = sis.read(5); 1.103 + do_check_eq(readData, data.slice(5)); 1.104 + do_check_eq(seekable.tell(), data.length * count); 1.105 + // Read across streams. 1.106 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, -15); 1.107 + do_check_eq(seekable.tell(), data.length * count - 15); 1.108 + readData = sis.read(15); 1.109 + do_check_eq(readData, data.slice(5) + data); 1.110 + do_check_eq(seekable.tell(), data.length * count); 1.111 + 1.112 + // -- Try to do various edge cases 1.113 + // Forward seek from the end, should throw. 1.114 + var caught = false; 1.115 + try { 1.116 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_END, 15); 1.117 + } catch(e) { 1.118 + caught = true; 1.119 + } 1.120 + do_check_eq(caught, true); 1.121 + do_check_eq(seekable.tell(), data.length * count); 1.122 + // Backward seek from the beginning, should be clamped. 1.123 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); 1.124 + do_check_eq(seekable.tell(), 0); 1.125 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -15); 1.126 + do_check_eq(seekable.tell(), 0); 1.127 + // Seek too far: should be clamped. 1.128 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); 1.129 + do_check_eq(seekable.tell(), 0); 1.130 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, 3 * data.length * count); 1.131 + do_check_eq(seekable.tell(), 100); 1.132 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, data.length * count); 1.133 + do_check_eq(seekable.tell(), 100); 1.134 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_CUR, -2 * data.length * count); 1.135 + do_check_eq(seekable.tell(), 0); 1.136 +} 1.137 + 1.138 +function test_multiplex_bug797871() { 1.139 + 1.140 + var data = "1234567890123456789012345678901234567890"; 1.141 + 1.142 + var MultiplexStream = CC("@mozilla.org/io/multiplex-input-stream;1", 1.143 + "nsIMultiplexInputStream"); 1.144 + do_check_eq(1, 1); 1.145 + 1.146 + var BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1", 1.147 + "nsIBinaryInputStream"); 1.148 + var BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1", 1.149 + "nsIBinaryOutputStream", 1.150 + "setOutputStream"); 1.151 + var multiplex = new MultiplexStream(); 1.152 + let s = Cc["@mozilla.org/io/string-input-stream;1"] 1.153 + .createInstance(Ci.nsIStringInputStream); 1.154 + s.setData(data, data.length); 1.155 + 1.156 + multiplex.appendStream(s); 1.157 + 1.158 + var seekable = multiplex.QueryInterface(Ci.nsISeekableStream); 1.159 + var sis = Cc["@mozilla.org/scriptableinputstream;1"] 1.160 + .createInstance(Ci.nsIScriptableInputStream); 1.161 + sis.init(seekable); 1.162 + 1.163 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 8); 1.164 + do_check_eq(seekable.tell(), 8); 1.165 + readData = sis.read(2); 1.166 + do_check_eq(seekable.tell(), 10); 1.167 + 1.168 + seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, 20); 1.169 + do_check_eq(seekable.tell(), 20); 1.170 +} 1.171 + 1.172 +function run_test() { 1.173 + test_multiplex_streams(); 1.174 + test_multiplex_bug797871(); 1.175 +} 1.176 +