Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: tests/standalone/src/io/FileTest.dart

Issue 10252020: test rename overhaul: step 12 - standalone (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Dart test program for testing file I/O.
6
7 #import("dart:io");
8 #import("dart:isolate");
9
10 class MyListOfOneElement implements List {
11 int _value;
12 MyListOfOneElement(this._value);
13 int get length() => 1;
14 operator [](int index) => _value;
15 }
16
17 class FileTest {
18 static Directory tempDirectory;
19 static int numLiveAsyncTests = 0;
20
21 static void asyncTestStarted() { ++numLiveAsyncTests; }
22 static void asyncTestDone(String name) {
23 --numLiveAsyncTests;
24 if (numLiveAsyncTests == 0) {
25 deleteTempDirectory();
26 }
27 }
28
29 static void createTempDirectory(Function doNext) {
30 tempDirectory = new Directory('');
31 tempDirectory.onError = (e) {
32 Expect.fail("Failed creating temporary directory");
33 };
34 tempDirectory.createTemp(doNext);
35 }
36
37 static void deleteTempDirectory() {
38 tempDirectory.deleteRecursivelySync();
39 }
40
41 // Test for file read functionality.
42 static void testReadStream() {
43 // Read a file and check part of it's contents.
44 String filename = getFilename("bin/file_test.cc");
45 File file = new File(filename);
46 InputStream input = file.openInputStream();
47 input.onData = () {
48 List<int> buffer = new List<int>(42);
49 int bytesRead = input.readInto(buffer, 0, 12);
50 Expect.equals(12, bytesRead);
51 bytesRead = input.readInto(buffer, 12, 30);
52 input.close();
53 Expect.equals(30, bytesRead);
54 Expect.equals(47, buffer[0]); // represents '/' in the file.
55 Expect.equals(47, buffer[1]); // represents '/' in the file.
56 Expect.equals(32, buffer[2]); // represents ' ' in the file.
57 Expect.equals(67, buffer[3]); // represents 'C' in the file.
58 Expect.equals(111, buffer[4]); // represents 'o' in the file.
59 Expect.equals(112, buffer[5]); // represents 'p' in the file.
60 Expect.equals(121, buffer[6]); // represents 'y' in the file.
61 Expect.equals(114, buffer[7]); // represents 'r' in the file.
62 Expect.equals(105, buffer[8]); // represents 'i' in the file.
63 Expect.equals(103, buffer[9]); // represents 'g' in the file.
64 Expect.equals(104, buffer[10]); // represents 'h' in the file.
65 Expect.equals(116, buffer[11]); // represents 't' in the file.
66 };
67 }
68
69 // Test for file read and write functionality.
70 static void testReadWriteStream() {
71 asyncTestStarted();
72
73 // Read a file.
74 String inFilename = getFilename("tests/vm/data/fixed_length_file");
75 File file;
76 InputStream input;
77 int bytesRead;
78
79 // Test reading all using readInto.
80 file = new File(inFilename);
81 input = file.openInputStream();
82 input.onData = () {
83 List<int> buffer1 = new List<int>(42);
84 bytesRead = input.readInto(buffer1, 0, 42);
85 Expect.equals(42, bytesRead);
86 Expect.isTrue(input.closed);
87
88 // Test reading all using readInto and read.
89 file = new File(inFilename);
90 input = file.openInputStream();
91 input.onData = () {
92 bytesRead = input.readInto(buffer1, 0, 21);
93 Expect.equals(21, bytesRead);
94 buffer1 = input.read();
95 Expect.equals(21, buffer1.length);
96 Expect.isTrue(input.closed);
97
98 // Test reading all using read and readInto.
99 file = new File(inFilename);
100 input = file.openInputStream();
101 input.onData = () {
102 buffer1 = input.read(21);
103 Expect.equals(21, buffer1.length);
104 bytesRead = input.readInto(buffer1, 0, 21);
105 Expect.equals(21, bytesRead);
106 Expect.isTrue(input.closed);
107
108 // Test reading all using read.
109 file = new File(inFilename);
110 input = file.openInputStream();
111 input.onData = () {
112 buffer1 = input.read();
113 Expect.equals(42, buffer1.length);
114 Expect.isTrue(input.closed);
115
116 // Write the contents of the file just read into another file.
117 String outFilename = tempDirectory.path + "/out_read_write_stream";
118 file = new File(outFilename);
119 OutputStream output = file.openOutputStream();
120 bool writeDone = output.writeFrom(buffer1, 0, 42);
121 Expect.equals(false, writeDone);
122 output.onNoPendingWrites = () {
123 output.close();
124 output.onClosed = () {
125 // Now read the contents of the file just written.
126 List<int> buffer2 = new List<int>(42);
127 file = new File(outFilename);
128 input = file.openInputStream();
129 input.onData = () {
130 bytesRead = input.readInto(buffer2, 0, 42);
131 Expect.equals(42, bytesRead);
132 // Now compare the two buffers to check if they are identical.
133 for (int i = 0; i < buffer1.length; i++) {
134 Expect.equals(buffer1[i], buffer2[i]);
135 }
136 };
137 input.onClosed = () {
138 // Delete the output file.
139 file.deleteSync();
140 Expect.isFalse(file.existsSync());
141 asyncTestDone("testReadWriteStream");
142 };
143 };
144 };
145 };
146 };
147 };
148 };
149 }
150
151 static void testRead() {
152 // Read a file and check part of it's contents.
153 String filename = getFilename("bin/file_test.cc");
154 File file = new File(filename);
155 file.onError = (e) {
156 Expect.fail("No errors expected : $e");
157 };
158 file.open(FileMode.READ, (RandomAccessFile file) {
159 List<int> buffer = new List<int>(10);
160 file.readList(buffer, 0, 5, (bytes_read) {
161 Expect.equals(5, bytes_read);
162 file.readList(buffer, 5, 5, (bytes_read) {
163 Expect.equals(5, bytes_read);
164 Expect.equals(47, buffer[0]); // represents '/' in the file.
165 Expect.equals(47, buffer[1]); // represents '/' in the file.
166 Expect.equals(32, buffer[2]); // represents ' ' in the file.
167 Expect.equals(67, buffer[3]); // represents 'C' in the file.
168 Expect.equals(111, buffer[4]); // represents 'o' in the file.
169 Expect.equals(112, buffer[5]); // represents 'p' in the file.
170 Expect.equals(121, buffer[6]); // represents 'y' in the file.
171 Expect.equals(114, buffer[7]); // represents 'r' in the file.
172 Expect.equals(105, buffer[8]); // represents 'i' in the file.
173 Expect.equals(103, buffer[9]); // represents 'g' in the file.
174 file.close(() => null);
175 });
176 });
177 });
178 }
179
180 static void testReadSync() {
181 // Read a file and check part of it's contents.
182 String filename = getFilename("bin/file_test.cc");
183 RandomAccessFile file = (new File(filename)).openSync();
184 List<int> buffer = new List<int>(42);
185 int bytes_read = 0;
186 bytes_read = file.readListSync(buffer, 0, 12);
187 Expect.equals(12, bytes_read);
188 bytes_read = file.readListSync(buffer, 12, 30);
189 Expect.equals(30, bytes_read);
190 Expect.equals(47, buffer[0]); // represents '/' in the file.
191 Expect.equals(47, buffer[1]); // represents '/' in the file.
192 Expect.equals(32, buffer[2]); // represents ' ' in the file.
193 Expect.equals(67, buffer[3]); // represents 'C' in the file.
194 Expect.equals(111, buffer[4]); // represents 'o' in the file.
195 Expect.equals(112, buffer[5]); // represents 'p' in the file.
196 Expect.equals(121, buffer[6]); // represents 'y' in the file.
197 Expect.equals(114, buffer[7]); // represents 'r' in the file.
198 Expect.equals(105, buffer[8]); // represents 'i' in the file.
199 Expect.equals(103, buffer[9]); // represents 'g' in the file.
200 Expect.equals(104, buffer[10]); // represents 'h' in the file.
201 Expect.equals(116, buffer[11]); // represents 't' in the file.
202 }
203
204 // Test for file read and write functionality.
205 static void testReadWrite() {
206 // Read a file.
207 String inFilename = getFilename("tests/vm/data/fixed_length_file");
208 final File file = new File(inFilename);
209 file.onError = (e) {
210 Expect.fail("No errors expected : $e");
211 };
212 file.open(FileMode.READ, (RandomAccessFile openedFile) {
213 openedFile.onError = (s) {
214 Expect.fail("No errors expected : $s");
215 };
216 List<int> buffer1 = new List<int>(42);
217 openedFile.readList(buffer1, 0, 42, (bytes_read) {
218 Expect.equals(42, bytes_read);
219 openedFile.close(() {
220 // Write the contents of the file just read into another file.
221 String outFilename = tempDirectory.path + "/out_read_write";
222 final File file2 = new File(outFilename);
223 file2.onError = (e) {
224 Expect.fail("No errors expected : $e");
225 };
226 file2.create(() {
227 file2.fullPath((s) {
228 Expect.isTrue(new File(s).existsSync());
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
230 Expect.fail("Not a full path");
231 }
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) {
233 openedFile2.onError = (s) {
234 Expect.fail("No errors expected : $s");
235 };
236 openedFile2.writeList(buffer1, 0, bytes_read);
237 openedFile2.onNoPendingWrites = () {
238 openedFile2.close(() {
239 List<int> buffer2 = new List<int>(bytes_read);
240 final File file3 = new File(outFilename);
241 file3.onError = (e) {
242 Expect.fail("No errors expected : $e");
243 };
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) {
245 openedFile3.onError = (s) {
246 Expect.fail("No errors expected : $s");
247 };
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) {
249 Expect.equals(42, bytes_read);
250 openedFile3.close(() {
251 // Now compare the two buffers to check if they
252 // are identical.
253 Expect.equals(buffer1.length, buffer2.length);
254 for (int i = 0; i < buffer1.length; i++) {
255 Expect.equals(buffer1[i], buffer2[i]);
256 }
257 // Delete the output file.
258 final file4 = file3;
259 file4.delete(() {
260 file4.exists((exists) {
261 Expect.isFalse(exists);
262 asyncTestDone("testReadWrite");
263 });
264 });
265 });
266 });
267 });
268 });
269 };
270 });
271 });
272 });
273 });
274 });
275 });
276 asyncTestStarted();
277 }
278
279 static void testWriteAppend() {
280 String content = "foobar";
281 String filename = tempDirectory.path + "/write_append";
282 File file = new File(filename);
283 file.createSync();
284 Expect.isTrue(new File(filename).existsSync());
285 List<int> buffer = content.charCodes();
286 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
287 openedFile.writeListSync(buffer, 0, buffer.length);
288 openedFile.closeSync();
289 // Reopen the file in write mode to ensure that we overwrite the content.
290 openedFile = (new File(filename)).openSync(FileMode.WRITE);
291 openedFile.writeListSync(buffer, 0, buffer.length);
292 Expect.equals(content.length, openedFile.lengthSync());
293 openedFile.closeSync();
294 // Open the file in append mode and ensure that we do not overwrite
295 // the existing content.
296 openedFile = (new File(filename)).openSync(FileMode.APPEND);
297 openedFile.writeListSync(buffer, 0, buffer.length);
298 Expect.equals(content.length * 2, openedFile.lengthSync());
299 openedFile.closeSync();
300 file.deleteSync();
301 }
302
303 static void testOutputStreamWriteAppend() {
304 String content = "foobar";
305 String filename = tempDirectory.path + "/outstream_write_append";
306 File file = new File(filename);
307 file.createSync();
308 List<int> buffer = content.charCodes();
309 OutputStream outStream = file.openOutputStream();
310 outStream.write(buffer);
311 outStream.onNoPendingWrites = () {
312 outStream.close();
313 File file2 = new File(filename);
314 OutputStream appendingOutput =
315 file2.openOutputStream(FileMode.APPEND);
316 appendingOutput.write(buffer);
317 appendingOutput.onNoPendingWrites = () {
318 appendingOutput.close();
319 File file3 = new File(filename);
320 file3.open(FileMode.READ, (RandomAccessFile openedFile) {
321 openedFile.length((int length) {
322 Expect.equals(content.length * 2, length);
323 openedFile.close(() {
324 file3.delete(() {
325 asyncTestDone("testOutputStreamWriteAppend");
326 });
327 });
328 });
329 });
330 };
331 };
332 asyncTestStarted();
333 }
334
335 // Test for file read and write functionality.
336 static void testOutputStreamWriteString() {
337 String content = "foobar";
338 String filename = tempDirectory.path + "/outstream_write_string";
339 File file = new File(filename);
340 file.createSync();
341 List<int> buffer = content.charCodes();
342 OutputStream outStream = file.openOutputStream();
343 outStream.writeString("abcdABCD");
344 outStream.writeString("abcdABCD", Encoding.UTF_8);
345 outStream.writeString("abcdABCD", Encoding.ISO_8859_1);
346 outStream.writeString("abcdABCD", Encoding.ASCII);
347 outStream.writeString("æøå", Encoding.UTF_8);
348 outStream.onNoPendingWrites = () {
349 outStream.close();
350 outStream.onClosed = () {
351 RandomAccessFile raf = file.openSync();
352 Expect.equals(38, raf.lengthSync());
353 };
354 };
355 asyncTestStarted();
356 }
357
358
359 static void testReadWriteSync() {
360 // Read a file.
361 String inFilename = getFilename("tests/vm/data/fixed_length_file");
362 RandomAccessFile file = (new File(inFilename)).openSync();
363 List<int> buffer1 = new List<int>(42);
364 int bytes_read = 0;
365 int bytes_written = 0;
366 bytes_read = file.readListSync(buffer1, 0, 42);
367 Expect.equals(42, bytes_read);
368 file.closeSync();
369 // Write the contents of the file just read into another file.
370 String outFilename = tempDirectory.path + "/out_read_write_sync";
371 File outFile = new File(outFilename);
372 outFile.createSync();
373 String path = outFile.fullPathSync();
374 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
375 Expect.fail("Not a full path");
376 }
377 Expect.isTrue(new File(path).existsSync());
378 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE);
379 openedFile.writeListSync(buffer1, 0, bytes_read);
380 openedFile.closeSync();
381 // Now read the contents of the file just written.
382 List<int> buffer2 = new List<int>(bytes_read);
383 openedFile = (new File(outFilename)).openSync();
384 bytes_read = openedFile.readListSync(buffer2, 0, 42);
385 Expect.equals(42, bytes_read);
386 openedFile.closeSync();
387 // Now compare the two buffers to check if they are identical.
388 Expect.equals(buffer1.length, buffer2.length);
389 for (int i = 0; i < buffer1.length; i++) {
390 Expect.equals(buffer1[i], buffer2[i]);
391 }
392 // Delete the output file.
393 outFile.deleteSync();
394 Expect.isFalse(outFile.existsSync());
395 }
396
397 static void testReadEmptyFileSync() {
398 String fileName = tempDirectory.path + "/empty_file_sync";
399 File file = new File(fileName);
400 file.createSync();
401 RandomAccessFile openedFile = file.openSync();
402 Expect.equals(-1, openedFile.readByteSync());
403 openedFile.closeSync();
404 file.deleteSync();
405 }
406
407 static void testReadEmptyFile() {
408 String fileName = tempDirectory.path + "/empty_file";
409 File file = new File(fileName);
410 file.onError = (e) {
411 Expect.fail("No errors expected : $e");
412 };
413 asyncTestStarted();
414 file.create(() {
415 file.open(FileMode.READ, (RandomAccessFile openedFile) {
416 openedFile.readByte((int byte) {
417 Expect.equals(-1, byte);
418 });
419 openedFile.onError = (e) {
420 Expect.isTrue(e is FileIOException);
421 openedFile.close(() {
422 file.delete(() {
423 asyncTestDone("testReadEmptyFile");
424 });
425 });
426 };
427 });
428 });
429 }
430
431 // Test for file write of different types of lists.
432 static void testWriteVariousLists() {
433 asyncTestStarted();
434 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
435 final File file = new File(fileName);
436 file.onError = (e) => Expect.fail("No errors expected : $e");
437 file.create(() {
438 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
439 // Write bytes from 0 to 7.
440 openedFile.writeList([0], 0, 1);
441 openedFile.writeList(const [1], 0, 1);
442 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
443 var x = 12345678901234567890123456789012345678901234567890;
444 var y = 12345678901234567890123456789012345678901234567893;
445 openedFile.writeList([y - x], 0, 1);
446 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
447 openedFile.writeList(const [261], 0, 1);
448 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
449 x = 12345678901234567890123456789012345678901234567890;
450 y = 12345678901234567890123456789012345678901234568153;
451 openedFile.writeList([y - x], 0, 1);
452
453 openedFile.onError = (e) => Expect.fail("No errors expected : $e");
454 openedFile.onNoPendingWrites = () {
455 openedFile.close(() {
456 // Check the written bytes.
457 final File file2 = new File(fileName);
458 var openedFile2 = file2.openSync();
459 var length = openedFile2.lengthSync();
460 Expect.equals(8, length);
461 List data = new List(length);
462 openedFile2.readListSync(data, 0, length);
463 for (var i = 0; i < data.length; i++) {
464 Expect.equals(i, data[i]);
465 }
466 openedFile2.closeSync();
467 file2.deleteSync();
468 asyncTestDone("testWriteVariousLists");
469 });
470 };
471 });
472 });
473 }
474
475 static void testDirectory() {
476 asyncTestStarted();
477
478 // Port to verify that the test completes.
479 var port = new ReceivePort();
480 port.receive((message, replyTo) {
481 port.close();
482 Expect.equals(1, message);
483 asyncTestDone("testDirectory");
484 });
485
486 var tempDir = tempDirectory.path;
487 var file = new File("${tempDir}/testDirectory");
488 var errors = 0;
489 file.directory((d) => Expect.fail("non-existing file"));
490 file.onError = (e) {
491 file.onError = (e) => Expect.fail("no error expected");
492 file.create(() {
493 file.directory((Directory d) {
494 d.onError = (s) => Expect.fail("no error expected");
495 d.exists((exists) {
496 Expect.isTrue(exists);
497 Expect.isTrue(d.path.endsWith(tempDir));
498 file.delete(() {
499 var file_dir = new File(".");
500 file_dir.directory((d) => Expect.fail("non-existing file"));
501 file_dir.onError = (e) {
502 var file_dir = new File(tempDir);
503 file_dir.directory((d) => Expect.fail("non-existing file"));
504 file_dir.onError = (e) => port.toSendPort().send(1);
505 };
506 });
507 });
508 });
509 });
510 };
511 }
512
513 static void testDirectorySync() {
514 var tempDir = tempDirectory.path;
515 var file = new File("${tempDir}/testDirectorySync");
516 // Non-existing file should throw exception.
517 Expect.throws(file.directorySync, (e) { return e is FileIOException; });
518 file.createSync();
519 // Check that the path of the returned directory is the temp directory.
520 Directory d = file.directorySync();
521 Expect.isTrue(d.existsSync());
522 Expect.isTrue(d.path.endsWith(tempDir));
523 file.deleteSync();
524 // Directories should throw exception.
525 var file_dir = new File(".");
526 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
527 file_dir = new File(tempDir);
528 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
529 }
530
531 // Test for file length functionality.
532 static void testLength() {
533 String filename = getFilename("tests/vm/data/fixed_length_file");
534 File file = new File(filename);
535 RandomAccessFile openedFile = file.openSync();
536 openedFile.onError = (e) => Expect.fail("No errors expected");
537 file.onError = (e) => Expect.fail("No errors expected");
538 openedFile.length((length) {
539 Expect.equals(42, length);
540 openedFile.close(() => null);
541 });
542 file.length((length) {
543 Expect.equals(42, length);
544 });
545 }
546
547 static void testLengthSync() {
548 String filename = getFilename("tests/vm/data/fixed_length_file");
549 File file = new File(filename);
550 RandomAccessFile openedFile = file.openSync();
551 Expect.equals(42, file.lengthSync());
552 Expect.equals(42, openedFile.lengthSync());
553 openedFile.closeSync();
554 }
555
556 // Test for file position functionality.
557 static void testPosition() {
558 String filename = getFilename("tests/vm/data/fixed_length_file");
559 RandomAccessFile input = (new File(filename)).openSync();
560 input.onError = (e) => Expect.fail("No errors expected");
561 input.position((position) {
562 Expect.equals(0, position);
563 List<int> buffer = new List<int>(100);
564 input.readList(buffer, 0, 12, (bytes_read) {
565 input.position((position) {
566 Expect.equals(12, position);
567 input.readList(buffer, 12, 6, (bytes_read) {
568 input.position((position) {
569 Expect.equals(18, position);
570 input.setPosition(8, () {
571 input.position((position) {
572 Expect.equals(8, position);
573 input.close(() => null);
574 });
575 });
576 });
577 });
578 });
579 });
580 });
581 }
582
583 static void testPositionSync() {
584 String filename = getFilename("tests/vm/data/fixed_length_file");
585 RandomAccessFile input = (new File(filename)).openSync();
586 Expect.equals(0, input.positionSync());
587 List<int> buffer = new List<int>(100);
588 input.readListSync(buffer, 0, 12);
589 Expect.equals(12, input.positionSync());
590 input.readListSync(buffer, 12, 6);
591 Expect.equals(18, input.positionSync());
592 input.setPositionSync(8);
593 Expect.equals(8, input.positionSync());
594 input.closeSync();
595 }
596
597 static void testTruncate() {
598 File file = new File(tempDirectory.path + "/out_truncate");
599 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
600 file.onError = (e) => Expect.fail("No errors expected: $e");
601 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
602 openedFile.writeList(buffer, 0, 10);
603 openedFile.onNoPendingWrites = () {
604 openedFile.length((length) {
605 Expect.equals(10, length);
606 openedFile.truncate(5, () {
607 openedFile.length((length) {
608 Expect.equals(5, length);
609 openedFile.close(() {
610 file.delete(() {
611 file.exists((exists) {
612 Expect.isFalse(exists);
613 asyncTestDone("testTruncate");
614 });
615 });
616 });
617 });
618 });
619 });
620 };
621 });
622 asyncTestStarted();
623 }
624
625 static void testTruncateSync() {
626 File file = new File(tempDirectory.path + "/out_truncate_sync");
627 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
628 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
629 openedFile.writeListSync(buffer, 0, 10);
630 Expect.equals(10, openedFile.lengthSync());
631 openedFile.truncateSync(5);
632 Expect.equals(5, openedFile.lengthSync());
633 openedFile.closeSync();
634 file.deleteSync();
635 Expect.isFalse(file.existsSync());
636 }
637
638 // Tests exception handling after file was closed.
639 static void testCloseException() {
640 bool exceptionCaught = false;
641 bool wrongExceptionCaught = false;
642 File input = new File(tempDirectory.path + "/out_close_exception");
643 RandomAccessFile openedFile = input.openSync(FileMode.WRITE);
644 openedFile.closeSync();
645 try {
646 openedFile.readByteSync();
647 } catch (FileIOException ex) {
648 exceptionCaught = true;
649 } catch (Exception ex) {
650 wrongExceptionCaught = true;
651 }
652 Expect.equals(true, exceptionCaught);
653 Expect.equals(true, !wrongExceptionCaught);
654 exceptionCaught = false;
655 try {
656 openedFile.writeByteSync(1);
657 } catch (FileIOException ex) {
658 exceptionCaught = true;
659 } catch (Exception ex) {
660 wrongExceptionCaught = true;
661 }
662 Expect.equals(true, exceptionCaught);
663 Expect.equals(true, !wrongExceptionCaught);
664 exceptionCaught = false;
665 try {
666 openedFile.writeStringSync("Test");
667 } catch (FileIOException ex) {
668 exceptionCaught = true;
669 } catch (Exception ex) {
670 wrongExceptionCaught = true;
671 }
672 Expect.equals(true, exceptionCaught);
673 Expect.equals(true, !wrongExceptionCaught);
674 exceptionCaught = false;
675 try {
676 List<int> buffer = new List<int>(100);
677 openedFile.readListSync(buffer, 0, 10);
678 } catch (FileIOException ex) {
679 exceptionCaught = true;
680 } catch (Exception ex) {
681 wrongExceptionCaught = true;
682 }
683 Expect.equals(true, exceptionCaught);
684 Expect.equals(true, !wrongExceptionCaught);
685 exceptionCaught = false;
686 try {
687 List<int> buffer = new List<int>(100);
688 openedFile.writeListSync(buffer, 0, 10);
689 } catch (FileIOException ex) {
690 exceptionCaught = true;
691 } catch (Exception ex) {
692 wrongExceptionCaught = true;
693 }
694 Expect.equals(true, exceptionCaught);
695 Expect.equals(true, !wrongExceptionCaught);
696 exceptionCaught = false;
697 try {
698 openedFile.positionSync();
699 } catch (FileIOException ex) {
700 exceptionCaught = true;
701 } catch (Exception ex) {
702 wrongExceptionCaught = true;
703 }
704 Expect.equals(true, exceptionCaught);
705 Expect.equals(true, !wrongExceptionCaught);
706 exceptionCaught = false;
707 try {
708 openedFile.lengthSync();
709 } catch (FileIOException ex) {
710 exceptionCaught = true;
711 } catch (Exception ex) {
712 wrongExceptionCaught = true;
713 }
714 Expect.equals(true, exceptionCaught);
715 Expect.equals(true, !wrongExceptionCaught);
716 exceptionCaught = false;
717 try {
718 openedFile.flushSync();
719 } catch (FileIOException ex) {
720 exceptionCaught = true;
721 } catch (Exception ex) {
722 wrongExceptionCaught = true;
723 }
724 Expect.equals(true, exceptionCaught);
725 Expect.equals(true, !wrongExceptionCaught);
726 input.deleteSync();
727 }
728
729 // Tests stream exception handling after file was closed.
730 static void testCloseExceptionStream() {
731 asyncTestStarted();
732 List<int> buffer = new List<int>(42);
733 File file = new File(tempDirectory.path + "/out_close_exception_stream");
734 file.createSync();
735 InputStream input = file.openInputStream();
736 input.onClosed = () {
737 Expect.isTrue(input.closed);
738 Expect.isNull(input.readInto(buffer, 0, 12));
739 OutputStream output = file.openOutputStream();
740 output.close();
741 Expect.throws(() => output.writeFrom(buffer, 0, 12));
742 output.onClosed = () {
743 file.deleteSync();
744 asyncTestDone("testCloseExceptionStream");
745 };
746 };
747 }
748
749 // Tests buffer out of bounds exception.
750 static void testBufferOutOfBoundsException() {
751 bool exceptionCaught = false;
752 bool wrongExceptionCaught = false;
753 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
754 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
755 try {
756 List<int> buffer = new List<int>(10);
757 openedFile.readListSync(buffer, 0, 12);
758 } catch (IndexOutOfRangeException ex) {
759 exceptionCaught = true;
760 } catch (Exception ex) {
761 wrongExceptionCaught = true;
762 }
763 Expect.equals(true, exceptionCaught);
764 Expect.equals(true, !wrongExceptionCaught);
765 exceptionCaught = false;
766 try {
767 List<int> buffer = new List<int>(10);
768 openedFile.readListSync(buffer, 6, 6);
769 } catch (IndexOutOfRangeException ex) {
770 exceptionCaught = true;
771 } catch (Exception ex) {
772 wrongExceptionCaught = true;
773 }
774 Expect.equals(true, exceptionCaught);
775 Expect.equals(true, !wrongExceptionCaught);
776 exceptionCaught = false;
777 try {
778 List<int> buffer = new List<int>(10);
779 openedFile.readListSync(buffer, -1, 1);
780 } catch (IndexOutOfRangeException ex) {
781 exceptionCaught = true;
782 } catch (Exception ex) {
783 wrongExceptionCaught = true;
784 }
785 Expect.equals(true, exceptionCaught);
786 Expect.equals(true, !wrongExceptionCaught);
787 exceptionCaught = false;
788 try {
789 List<int> buffer = new List<int>(10);
790 openedFile.readListSync(buffer, 0, -1);
791 } catch (IndexOutOfRangeException ex) {
792 exceptionCaught = true;
793 } catch (Exception ex) {
794 wrongExceptionCaught = true;
795 }
796 Expect.equals(true, exceptionCaught);
797 Expect.equals(true, !wrongExceptionCaught);
798 exceptionCaught = false;
799 try {
800 List<int> buffer = new List<int>(10);
801 openedFile.writeListSync(buffer, 0, 12);
802 } catch (IndexOutOfRangeException ex) {
803 exceptionCaught = true;
804 } catch (Exception ex) {
805 wrongExceptionCaught = true;
806 }
807 Expect.equals(true, exceptionCaught);
808 Expect.equals(true, !wrongExceptionCaught);
809 exceptionCaught = false;
810 try {
811 List<int> buffer = new List<int>(10);
812 openedFile.writeListSync(buffer, 6, 6);
813 } catch (IndexOutOfRangeException ex) {
814 exceptionCaught = true;
815 } catch (Exception ex) {
816 wrongExceptionCaught = true;
817 }
818 Expect.equals(true, exceptionCaught);
819 Expect.equals(true, !wrongExceptionCaught);
820 exceptionCaught = false;
821 try {
822 List<int> buffer = new List<int>(10);
823 openedFile.writeListSync(buffer, -1, 1);
824 } catch (IndexOutOfRangeException ex) {
825 exceptionCaught = true;
826 } catch (Exception ex) {
827 wrongExceptionCaught = true;
828 }
829 Expect.equals(true, exceptionCaught);
830 Expect.equals(true, !wrongExceptionCaught);
831 exceptionCaught = false;
832 try {
833 List<int> buffer = new List<int>(10);
834 openedFile.writeListSync(buffer, 0, -1);
835 } catch (IndexOutOfRangeException ex) {
836 exceptionCaught = true;
837 } catch (Exception ex) {
838 wrongExceptionCaught = true;
839 }
840 Expect.equals(true, exceptionCaught);
841 Expect.equals(true, !wrongExceptionCaught);
842 openedFile.closeSync();
843 file.deleteSync();
844 }
845
846 static void testOpenDirectoryAsFile() {
847 var f = new File('.');
848 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file'));
849 f.onError = (e) => null;
850 }
851
852 static void testOpenDirectoryAsFileSync() {
853 var f = new File('.');
854 try {
855 f.openSync();
856 Expect.fail("Expected exception opening directory as file");
857 } catch (var e) {
858 Expect.isTrue(e is FileIOException);
859 }
860 }
861
862 static void testReadAsBytes() {
863 var port = new ReceivePort();
864 port.receive((result, replyTo) {
865 port.close();
866 Expect.equals(42, result);
867 });
868 var name = getFilename("tests/vm/data/fixed_length_file");
869 var f = new File(name);
870 f.readAsBytes((bytes) {
871 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
872 port.toSendPort().send(bytes.length);
873 });
874 f.onError = (e) => Expect.fail("No errors expected: $e");
875 }
876
877 static void testReadAsBytesSync() {
878 var name = getFilename("tests/vm/data/fixed_length_file");
879 var bytes = new File(name).readAsBytesSync();
880 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
881 Expect.equals(bytes.length, 42);
882 }
883
884 static void testReadAsText() {
885 var port = new ReceivePort();
886 port.receive((result, replyTo) {
887 port.close();
888 Expect.equals(1, result);
889 });
890 var name = getFilename("tests/vm/data/fixed_length_file");
891 var f = new File(name);
892 f.readAsText(Encoding.UTF_8, (text) {
893 Expect.isTrue(text.endsWith("42 bytes."));
894 Expect.equals(42, text.length);
895 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat");
896 var f = new File(name);
897 f.onError = (e) => Expect.fail("No errors expected: $e");
898 f.readAsText(Encoding.UTF_8, (text) {
899 Expect.equals(6, text.length);
900 var expected = [955, 120, 46, 32, 120, 10];
901 Expect.listEquals(expected, text.charCodes());
902 f.readAsText(Encoding.ISO_8859_1, (text) {
903 Expect.equals(7, text.length);
904 var expected = [206, 187, 120, 46, 32, 120, 10];
905 Expect.listEquals(expected, text.charCodes());
906 f.onError = (e) {
907 port.toSendPort().send(1);
908 };
909 f.readAsText(Encoding.ASCII, (text) {
910 Expect.fail("Non-ascii char should cause error");
911 });
912 });
913 });
914 });
915 f.onError = (e) {
916 Expect.fail("No errors expected: $e");
917 };
918 }
919
920 static void testReadAsTextSync() {
921 var name = getFilename("tests/vm/data/fixed_length_file");
922 var text = new File(name).readAsTextSync();
923 Expect.isTrue(text.endsWith("42 bytes."));
924 Expect.equals(42, text.length);
925 name = getDataFilename("tests/standalone/src/io/read_as_text.dat");
926 text = new File(name).readAsTextSync();
927 Expect.equals(6, text.length);
928 var expected = [955, 120, 46, 32, 120, 10];
929 Expect.listEquals(expected, text.charCodes());
930 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); });
931 text = new File(name).readAsTextSync(Encoding.ISO_8859_1);
932 expected = [206, 187, 120, 46, 32, 120, 10];
933 Expect.equals(7, text.length);
934 Expect.listEquals(expected, text.charCodes());
935 }
936
937 static void testReadAsLines() {
938 var port = new ReceivePort();
939 port.receive((result, replyTo) {
940 port.close();
941 Expect.equals(42, result);
942 });
943 var name = getFilename("tests/vm/data/fixed_length_file");
944 var f = new File(name);
945 f.readAsLines(Encoding.UTF_8, (lines) {
946 Expect.equals(1, lines.length);
947 var line = lines[0];
948 Expect.isTrue(line.endsWith("42 bytes."));
949 port.toSendPort().send(line.length);
950 });
951 f.onError = (e) => Expect.fail("No errors expected: $e");
952 }
953
954 static void testReadAsLinesSync() {
955 var name = getFilename("tests/vm/data/fixed_length_file");
956 var lines = new File(name).readAsLinesSync();
957 Expect.equals(1, lines.length);
958 var line = lines[0];
959 Expect.isTrue(line.endsWith("42 bytes."));
960 Expect.equals(42, line.length);
961 name = getDataFilename("tests/standalone/src/io/readline_test1.dat");
962 lines = new File(name).readAsLinesSync();
963 Expect.equals(10, lines.length);
964 }
965
966
967 static void testReadAsErrors() {
968 var port = new ReceivePort();
969 port.receive((message, _) {
970 port.close();
971 Expect.equals(1, message);
972 });
973 var f = new File('.');
974 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
975 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
976 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
977 f.readAsBytes((bytes) => Expect.fail("no bytes expected"));
978 f.onError = (e) {
979 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected"));
980 f.onError = (e) {
981 f.readAsLines(Encoding.UTF_8,
982 (lines) => Expect.fail("no lines expected"));
983 f.onError = (e) => port.toSendPort().send(1);
984 };
985 };
986 }
987
988 // Test that opens the same file for writing then for appending to test
989 // that the file is not truncated when opened for appending.
990 static void testAppend() {
991 var file = new File('${tempDirectory.path}/out_append');
992 file.open(FileMode.WRITE, (openedFile) {
993 openedFile.writeString("asdf");
994 openedFile.onNoPendingWrites = () {
995 openedFile.close(() {
996 file.open(FileMode.APPEND, (openedFile) {
997 openedFile.length((length) {
998 Expect.equals(4, length);
999 openedFile.writeString("asdf");
1000 openedFile.onNoPendingWrites = () {
1001 openedFile.length((length) {
1002 Expect.equals(8, length);
1003 openedFile.close(() {
1004 file.delete(() {
1005 file.exists((exists) {
1006 Expect.isFalse(exists);
1007 asyncTestDone("testAppend");
1008 });
1009 });
1010 });
1011 });
1012 };
1013 });
1014 });
1015 });
1016 };
1017 });
1018 asyncTestStarted();
1019 }
1020
1021 static void testAppendSync() {
1022 var file = new File('${tempDirectory.path}/out_append_sync');
1023 var openedFile = file.openSync(FileMode.WRITE);
1024 openedFile.writeStringSync("asdf");
1025 Expect.equals(4, openedFile.lengthSync());
1026 openedFile.closeSync();
1027 openedFile = file.openSync(FileMode.WRITE);
1028 openedFile.setPositionSync(4);
1029 openedFile.writeStringSync("asdf");
1030 Expect.equals(8, openedFile.lengthSync());
1031 openedFile.closeSync();
1032 file.deleteSync();
1033 Expect.isFalse(file.existsSync());
1034 }
1035
1036 // Helper method to be able to run the test from the runtime
1037 // directory, or the top directory.
1038 static String getFilename(String path) =>
1039 new File(path).existsSync() ? path : 'runtime/' + path;
1040
1041 static String getDataFilename(String path) =>
1042 new File(path).existsSync() ? path : '../' + path;
1043
1044 // Main test entrypoint.
1045 static testMain() {
1046 testRead();
1047 testReadSync();
1048 testReadStream();
1049 testLength();
1050 testLengthSync();
1051 testPosition();
1052 testPositionSync();
1053 testOpenDirectoryAsFile();
1054 testOpenDirectoryAsFileSync();
1055 testReadAsBytes();
1056 testReadAsBytesSync();
1057 testReadAsText();
1058 testReadAsTextSync();
1059 testReadAsLines();
1060 testReadAsLinesSync();
1061 testReadAsErrors();
1062
1063 createTempDirectory(() {
1064 testReadWrite();
1065 testReadWriteSync();
1066 testReadWriteStream();
1067 testReadEmptyFileSync();
1068 testReadEmptyFile();
1069 testTruncate();
1070 testTruncateSync();
1071 testCloseException();
1072 testCloseExceptionStream();
1073 testBufferOutOfBoundsException();
1074 testAppend();
1075 testAppendSync();
1076 testWriteAppend();
1077 testOutputStreamWriteAppend();
1078 testOutputStreamWriteString();
1079 testWriteVariousLists();
1080 testDirectory();
1081 testDirectorySync();
1082 });
1083 }
1084 }
1085
1086 main() {
1087 FileTest.testMain();
1088 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698