1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Test nsIPartialFileInputStream
// NOTE! These tests often use do_check_true(a == b) rather than
// do_check_eq(a, b) to avoid outputting characters which confuse
// the console
var CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
const PR_RDONLY = 0x1; // see prio.h
// We need the profile directory so the test harness will clean up our test
// files.
do_get_profile();
var binary_test_file_name = "data/image.png";
var text_test_file_name = "test_file_partial_inputstream.js";
// This is a global variable since if it's passed as an argument stack traces
// become unreadable.
var test_file_data;
function run_test()
{
// Binary tests
let binaryFile = do_get_file(binary_test_file_name);
let size = binaryFile.fileSize;
// Want to make sure we're working with a large enough file
dump("**** binary file size is: " + size + " ****\n");
do_check_true(size > 65536);
let binaryStream = new BinaryInputStream(new_file_input_stream(binaryFile));
test_file_data = "";
while ((avail = binaryStream.available()) > 0) {
test_file_data += binaryStream.readBytes(avail);
}
do_check_eq(test_file_data.length, size);
binaryStream.close();
test_binary_portion(0, 10);
test_binary_portion(0, 20000);
test_binary_portion(0, size);
test_binary_portion(20000, 10);
test_binary_portion(20000, 20000);
test_binary_portion(20000, size-20000);
test_binary_portion(size-10, 10);
test_binary_portion(size-20000, 20000);
test_binary_portion(0, 0);
test_binary_portion(20000, 0);
test_binary_portion(size-1, 1);
// Text-file tests
let textFile = do_get_file(binary_test_file_name);
size = textFile.fileSize;
// Want to make sure we're working with a large enough file
dump("**** text file size is: " + size + " ****\n");
do_check_true(size > 7000);
let textStream = new BinaryInputStream(new_file_input_stream(textFile));
test_file_data = "";
while ((avail = textStream.available()) > 0)
test_file_data += textStream.readBytes(avail);
do_check_eq(test_file_data.length, size);
textStream.close();
test_text_portion(0, 100);
test_text_portion(0, size);
test_text_portion(5000, 1000);
test_text_portion(size-10, 10);
test_text_portion(size-5000, 5000);
test_text_portion(10, 0);
test_text_portion(size-1, 1);
// Test auto-closing files
// Test behavior when *not* autoclosing
let tempFile = create_temp_file("01234567890123456789");
let tempInputStream = new_partial_file_input_stream(tempFile, 5, 10);
tempInputStream.QueryInterface(Ci.nsILineInputStream);
do_check_eq(read_line_stream(tempInputStream)[1], "5678901234");
try {
// This fails on some platforms
tempFile.remove(false);
}
catch (ex) {
}
tempInputStream.QueryInterface(Ci.nsISeekableStream);
tempInputStream.seek(SET, 1);
do_check_eq(read_line_stream(tempInputStream)[1], "678901234");
// Test removing the file when autoclosing
tempFile = create_temp_file("01234567890123456789");
tempInputStream = new_partial_file_input_stream(tempFile, 5, 10,
Ci.nsIFileInputStream.CLOSE_ON_EOF |
Ci.nsIFileInputStream.REOPEN_ON_REWIND);
tempInputStream.QueryInterface(Ci.nsILineInputStream);
do_check_eq(read_line_stream(tempInputStream)[1], "5678901234");
tempFile.remove(false);
tempInputStream.QueryInterface(Ci.nsISeekableStream);
try {
// The seek should reopen the file, which should fail.
tempInputStream.seek(SET, 1);
do_check_true(false);
}
catch (ex) {
}
// Test editing the file when autoclosing
tempFile = create_temp_file("01234567890123456789");
tempInputStream = new_partial_file_input_stream(tempFile, 5, 10,
Ci.nsIFileInputStream.CLOSE_ON_EOF |
Ci.nsIFileInputStream.REOPEN_ON_REWIND);
tempInputStream.QueryInterface(Ci.nsILineInputStream);
do_check_eq(read_line_stream(tempInputStream)[1], "5678901234");
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
ostream.init(tempFile, 0x02 | 0x08 | 0x20, // write, create, truncate
0o666, 0);
let newData = "abcdefghijklmnopqrstuvwxyz";
ostream.write(newData, newData.length);
ostream.close();
tempInputStream.QueryInterface(Ci.nsISeekableStream);
tempInputStream.seek(SET, 1);
do_check_eq(read_line_stream(tempInputStream)[1], newData.substr(6,9));
// Test auto-delete and auto-close together
tempFile = create_temp_file("01234567890123456789");
tempInputStream = new_partial_file_input_stream(tempFile, 5, 10,
Ci.nsIFileInputStream.CLOSE_ON_EOF |
Ci.nsIFileInputStream.DELETE_ON_CLOSE);
tempInputStream.QueryInterface(Ci.nsILineInputStream);
do_check_eq(read_line_stream(tempInputStream)[1], "5678901234");
do_check_false(tempFile.exists());
}
function test_binary_portion(start, length) {
let subFile = create_temp_file(test_file_data.substr(start, length));
let streamTests = [
test_4k_read,
test_max_read,
test_seek,
test_seek_then_read,
];
for (var test of streamTests) {
let fileStream = new_file_input_stream(subFile);
let partialStream = new_partial_file_input_stream(do_get_file(binary_test_file_name),
start, length);
test(fileStream, partialStream, length);
fileStream.close();
partialStream.close();
}
}
function test_4k_read(fileStreamA, fileStreamB) {
fileStreamA.QueryInterface(Ci.nsISeekableStream);
fileStreamB.QueryInterface(Ci.nsISeekableStream);
let streamA = new BinaryInputStream(fileStreamA);
let streamB = new BinaryInputStream(fileStreamB);
while(1) {
do_check_eq(fileStreamA.tell(), fileStreamB.tell());
let availA = streamA.available();
let availB = streamB.available();
do_check_eq(availA, availB);
if (availA == 0)
return;
let readSize = availA > 4096 ? 4096 : availA;
do_check_true(streamA.readBytes(readSize) ==
streamB.readBytes(readSize));
}
}
function test_max_read(fileStreamA, fileStreamB) {
fileStreamA.QueryInterface(Ci.nsISeekableStream);
fileStreamB.QueryInterface(Ci.nsISeekableStream);
let streamA = new BinaryInputStream(fileStreamA);
let streamB = new BinaryInputStream(fileStreamB);
while(1) {
do_check_eq(fileStreamA.tell(), fileStreamB.tell());
let availA = streamA.available();
let availB = streamB.available();
do_check_eq(availA, availB);
if (availA == 0)
return;
do_check_true(streamA.readBytes(availA) ==
streamB.readBytes(availB));
}
}
const SET = Ci.nsISeekableStream.NS_SEEK_SET;
const CUR = Ci.nsISeekableStream.NS_SEEK_CUR;
const END = Ci.nsISeekableStream.NS_SEEK_END;
function test_seek(dummy, partialFileStream, size) {
// We can't test the "real" filestream here as our existing file streams
// are very broken and allows searching past the end of the file.
partialFileStream.QueryInterface(Ci.nsISeekableStream);
var tests = [
[SET, 0],
[SET, 5],
[SET, 1000],
[SET, size-10],
[SET, size-5],
[SET, size-1],
[SET, size],
[SET, size+10],
[SET, 0],
[CUR, 5],
[CUR, -5],
[SET, 5000],
[CUR, -100],
[CUR, 200],
[CUR, -5000],
[CUR, 5000],
[CUR, size * 2],
[SET, 1],
[CUR, -1],
[CUR, -1],
[CUR, -1],
[CUR, -1],
[CUR, -1],
[SET, size-1],
[CUR, 1],
[CUR, 1],
[CUR, 1],
[CUR, 1],
[CUR, 1],
[END, 0],
[END, -1],
[END, -5],
[END, -1000],
[END, -size+10],
[END, -size+5],
[END, -size+1],
[END, -size],
[END, -size-10],
[END, 10],
[CUR, 10],
[CUR, 10],
[CUR, 100],
[CUR, 1000],
[END, -1000],
[CUR, 100],
[CUR, 900],
[CUR, 100],
[CUR, 100],
];
let pos = 0;
for (var test of tests) {
let didThrow = false;
try {
partialFileStream.seek(test[0], test[1]);
}
catch (ex) {
didThrow = true;
}
let newPos = test[0] == SET ? test[1] :
test[0] == CUR ? pos + test[1] :
size + test[1];
if (newPos > size || newPos < 0) {
do_check_true(didThrow);
}
else {
do_check_false(didThrow);
pos = newPos;
}
do_check_eq(partialFileStream.tell(), pos);
do_check_eq(partialFileStream.available(), size - pos);
}
}
function test_seek_then_read(fileStreamA, fileStreamB, size) {
// For now we only test seeking inside the file since our existing file
// streams behave very strange when seeking to past the end of the file.
if (size < 20000) {
return;
}
fileStreamA.QueryInterface(Ci.nsISeekableStream);
fileStreamB.QueryInterface(Ci.nsISeekableStream);
let streamA = new BinaryInputStream(fileStreamA);
let streamB = new BinaryInputStream(fileStreamB);
let read = {};
var tests = [
[SET, 0],
[read, 1000],
[read, 1000],
[SET, 5],
[read, 1000],
[read, 5000],
[CUR, 100],
[read, 1000],
[read, 5000],
[CUR, -100],
[read, 1000],
[CUR, -100],
[read, 5000],
[END, -10],
[read, 10],
[END, -100],
[read, 101],
[CUR, -100],
[read, 10],
[SET, 0],
[read, 20000],
[read, 1],
[read, 100],
];
for (var test of tests) {
if (test[0] === read) {
let didThrowA = false;
let didThrowB = false;
let bytesA, bytesB;
try {
bytesA = streamA.readBytes(test[1]);
}
catch (ex) {
didThrowA = true;
}
try {
bytesB = streamB.readBytes(test[1]);
}
catch (ex) {
didThrowB = true;
}
do_check_eq(didThrowA, didThrowB);
do_check_true(bytesA == bytesB);
}
else {
fileStreamA.seek(test[0], test[1]);
fileStreamB.seek(test[0], test[1]);
}
do_check_eq(fileStreamA.tell(), fileStreamB.tell());
do_check_eq(fileStreamA.available(), fileStreamB.available());
}
}
function test_text_portion(start, length) {
let subFile = create_temp_file(test_file_data.substr(start, length));
let streamTests = [
test_readline,
test_seek_then_readline,
];
for (var test of streamTests) {
let fileStream = new_file_input_stream(subFile)
.QueryInterface(Ci.nsILineInputStream);
let partialStream = new_partial_file_input_stream(do_get_file(binary_test_file_name),
start, length)
.QueryInterface(Ci.nsILineInputStream);
test(fileStream, partialStream, length);
fileStream.close();
partialStream.close();
}
}
function test_readline(fileStreamA, fileStreamB)
{
let moreA = true, moreB;
while(moreA) {
let lineA, lineB;
[moreA, lineA] = read_line_stream(fileStreamA);
[moreB, lineB] = read_line_stream(fileStreamB);
do_check_eq(moreA, moreB);
do_check_true(lineA.value == lineB.value);
}
}
function test_seek_then_readline(fileStreamA, fileStreamB, size) {
// For now we only test seeking inside the file since our existing file
// streams behave very strange when seeking to past the end of the file.
if (size < 100) {
return;
}
fileStreamA.QueryInterface(Ci.nsISeekableStream);
fileStreamB.QueryInterface(Ci.nsISeekableStream);
let read = {};
var tests = [
[SET, 0],
[read, 5],
[read, 5],
[SET, 5],
[read, 5],
[read, 15],
[CUR, 100],
[read, 5],
[read, 15],
[CUR, -100],
[read, 5],
[CUR, -100],
[read, 25],
[END, -10],
[read, 1],
[END, -50],
[read, 30],
[read, 1],
[read, 1],
[CUR, -100],
[read, 1],
[SET, 0],
[read, 10000],
[read, 1],
[read, 1],
[SET, 0],
[read, 1],
];
for (var test of tests) {
if (test[0] === read) {
for (let i = 0; i < test[1]; ++i) {
let didThrowA = false;
let didThrowB = false;
let lineA, lineB, moreA, moreB;
try {
[moreA, lineA] = read_line_stream(fileStreamA);
}
catch (ex) {
didThrowA = true;
}
try {
[moreB, lineB] = read_line_stream(fileStreamB);
}
catch (ex) {
didThrowB = true;
}
do_check_eq(didThrowA, didThrowB);
do_check_eq(moreA, moreB);
do_check_true(lineA == lineB);
do_check_eq(fileStreamA.tell(), fileStreamB.tell());
do_check_eq(fileStreamA.available(), fileStreamB.available());
if (!moreA)
break;
}
}
else {
if (!(test[0] == CUR && (test[1] > fileStreamA.available() ||
test[1] < -fileStreamA.tell()))) {
fileStreamA.seek(test[0], test[1]);
fileStreamB.seek(test[0], test[1]);
do_check_eq(fileStreamA.tell(), fileStreamB.tell());
do_check_eq(fileStreamA.available(), fileStreamB.available());
}
}
}
}
function read_line_stream(stream) {
let line = {};
let more = stream.readLine(line);
return [more, line.value];
}
function new_file_input_stream(file) {
var stream =
Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
stream.init(file, PR_RDONLY, 0, 0);
return stream.QueryInterface(Ci.nsIInputStream);
}
function new_partial_file_input_stream(file, start, length, flags) {
var stream =
Cc["@mozilla.org/network/partial-file-input-stream;1"]
.createInstance(Ci.nsIPartialFileInputStream);
stream.init(file, start, length, PR_RDONLY, 0, flags || 0);
return stream.QueryInterface(Ci.nsIInputStream);
}
function create_temp_file(data) {
let file = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("ProfD", Ci.nsIFile);
file.append("fileinputstream-test-file.tmp");
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
createInstance(Ci.nsIFileOutputStream);
ostream.init(file, 0x02 | 0x08 | 0x20, // write, create, truncate
0o666, 0);
do_check_eq(ostream.write(data, data.length), data.length);
ostream.close();
return file;
}
|