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

Side by Side Diff: tests/standalone/src/io/FileErrorTest.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 error handling in file I/O.
6
7 #import("dart:io");
8 #import("dart:isolate");
9
10 Directory tempDir() {
11 var d = new Directory('');
12 d.createTempSync();
13 return d;
14 }
15
16
17 bool checkNonExistentFileException(e, str) {
18 Expect.isTrue(e is FileIOException);
19 Expect.isTrue(e.osError != null);
20 Expect.isTrue(e.toString().indexOf(str) != -1);
21 if (Platform.operatingSystem() == "linux") {
22 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
23 } else if (Platform.operatingSystem() == "macos") {
24 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
25 } else if (Platform.operatingSystem() == "windows") {
26 Expect.isTrue(
27 e.toString().indexOf(
28 "The system cannot find the file specified") != -1);
29 }
30 // File not not found has error code 2 on all supported platforms.
31 Expect.equals(2, e.osError.errorCode);
32
33 return true;
34 }
35
36
37 bool checkOpenNonExistentFileException(e) {
38 return checkNonExistentFileException(e, "Cannot open file");
39 }
40
41
42 bool checkDeleteNonExistentFileException(e) {
43 return checkNonExistentFileException(e, "Cannot delete file");
44 }
45
46
47 bool checkLengthNonExistentFileException(e) {
48 return checkNonExistentFileException(e, "Cannot retrieve length of file");
49 }
50
51
52 void testOpenNonExistent() {
53 Directory temp = tempDir();
54 ReceivePort p = new ReceivePort();
55 p.receive((x, y) {
56 p.close();
57 temp.deleteRecursivelySync();
58 });
59 var file = new File("${temp.path}/nonExistentFile");
60
61 // Non-existing file should throw exception.
62 Expect.throws(() => file.openSync(),
63 (e) => checkOpenNonExistentFileException(e));
64
65 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code"));
66 file.onError = (e) {
67 checkOpenNonExistentFileException(e);
68 p.toSendPort().send(null);
69 };
70 }
71
72
73 void testDeleteNonExistent() {
74 Directory temp = tempDir();
75 ReceivePort p = new ReceivePort();
76 p.receive((x, y) {
77 p.close();
78 temp.deleteRecursivelySync();
79 });
80 var file = new File("${temp.path}/nonExistentFile");
81
82 // Non-existing file should throw exception.
83 Expect.throws(() => file.deleteSync(),
84 (e) => checkDeleteNonExistentFileException(e));
85
86 file.delete(() => Expect.fail("Unreachable code"));
87 file.onError = (e) {
88 checkDeleteNonExistentFileException(e);
89 p.toSendPort().send(null);
90 };
91 }
92
93
94 void testLengthNonExistent() {
95 Directory temp = tempDir();
96 ReceivePort p = new ReceivePort();
97 p.receive((x, y) {
98 p.close();
99 temp.deleteRecursivelySync();
100 });
101 var file = new File("${temp.path}/nonExistentFile");
102
103 // Non-existing file should throw exception.
104 Expect.throws(() => file.lengthSync(),
105 (e) => checkLengthNonExistentFileException(e));
106
107 file.length((len) => Expect.fail("Unreachable code"));
108 file.onError = (e) {
109 checkLengthNonExistentFileException(e);
110 p.toSendPort().send(null);
111 };
112 }
113
114
115 bool checkCreateInNonExistentDirectoryException(e) {
116 Expect.isTrue(e is FileIOException);
117 Expect.isTrue(e.osError != null);
118 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1);
119 if (Platform.operatingSystem() == "linux") {
120 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
121 Expect.equals(2, e.osError.errorCode);
122 } else if (Platform.operatingSystem() == "macos") {
123 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
124 Expect.equals(2, e.osError.errorCode);
125 } else if (Platform.operatingSystem() == "windows") {
126 Expect.isTrue(
127 e.toString().indexOf(
128 "The system cannot find the path specified") != -1);
129 Expect.equals(3, e.osError.errorCode);
130 }
131
132 return true;
133 }
134
135 void testCreateInNonExistentDirectory() {
136 Directory temp = tempDir();
137 ReceivePort p = new ReceivePort();
138 p.receive((x, y) {
139 p.close();
140 temp.deleteRecursivelySync();
141 });
142 var file = new File("${temp.path}/nonExistentDirectory/newFile");
143
144 // Create in non-existent directory should throw exception.
145 Expect.throws(() => file.createSync(),
146 (e) => checkCreateInNonExistentDirectoryException(e));
147
148 file.create(() => Expect.fail("Unreachable code"));
149 file.onError = (e) {
150 checkCreateInNonExistentDirectoryException(e);
151 p.toSendPort().send(null);
152 };
153 }
154
155 bool checkFullPathOnNonExistentDirectoryException(e) {
156 Expect.isTrue(e is FileIOException);
157 Expect.isTrue(e.osError != null);
158 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1);
159 if (Platform.operatingSystem() == "linux") {
160 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
161 } else if (Platform.operatingSystem() == "macos") {
162 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
163 } else if (Platform.operatingSystem() == "windows") {
164 Expect.isTrue(
165 e.toString().indexOf(
166 "The system cannot find the file specified") != -1);
167 }
168 // File not not found has error code 2 on all supported platforms.
169 Expect.equals(2, e.osError.errorCode);
170
171 return true;
172 }
173
174 void testFullPathOnNonExistentDirectory() {
175 Directory temp = tempDir();
176 ReceivePort p = new ReceivePort();
177 p.receive((x, y) {
178 p.close();
179 temp.deleteRecursivelySync();
180 });
181 var file = new File("${temp.path}/nonExistentDirectory");
182
183 // Full path non-existent directory should throw exception.
184 Expect.throws(() => file.fullPathSync(),
185 (e) => checkFullPathOnNonExistentDirectoryException(e));
186
187 file.fullPath((path) => Expect.fail("Unreachable code $path"));
188 file.onError = (e) {
189 checkFullPathOnNonExistentDirectoryException(e);
190 p.toSendPort().send(null);
191 };
192 }
193
194 bool checkDirectoryInNonExistentDirectoryException(e) {
195 Expect.isTrue(e is FileIOException);
196 Expect.isTrue(e.osError != null);
197 Expect.isTrue(
198 e.toString().indexOf("Cannot retrieve directory for file") != -1);
199 if (Platform.operatingSystem() == "linux") {
200 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
201 } else if (Platform.operatingSystem() == "macos") {
202 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
203 } else if (Platform.operatingSystem() == "windows") {
204 Expect.isTrue(
205 e.toString().indexOf(
206 "The system cannot find the file specified") != -1);
207 }
208 // File not not found has error code 2 on all supported platforms.
209 Expect.equals(2, e.osError.errorCode);
210
211 return true;
212 }
213
214 void testDirectoryInNonExistentDirectory() {
215 Directory temp = tempDir();
216 ReceivePort p = new ReceivePort();
217 p.receive((x, y) {
218 p.close();
219 temp.deleteRecursivelySync();
220 });
221 var file = new File("${temp.path}/nonExistentDirectory/newFile");
222
223 // Create in non-existent directory should throw exception.
224 Expect.throws(() => file.directorySync(),
225 (e) => checkDirectoryInNonExistentDirectoryException(e));
226
227 file.directory((directory) => Expect.fail("Unreachable code"));
228 file.onError = (e) {
229 checkDirectoryInNonExistentDirectoryException(e);
230 p.toSendPort().send(null);
231 };
232 }
233
234 void testReadAsBytesNonExistent() {
235 Directory temp = tempDir();
236 ReceivePort p = new ReceivePort();
237 p.receive((x, y) {
238 p.close();
239 temp.deleteRecursivelySync();
240 });
241 var file = new File("${temp.path}/nonExistentFile3");
242
243 // Non-existing file should throw exception.
244 Expect.throws(() => file.readAsBytesSync(),
245 (e) => checkOpenNonExistentFileException(e));
246
247 file.readAsBytes((data) => Expect.fail("Unreachable code"));
248 file.onError = (e) {
249 checkOpenNonExistentFileException(e);
250 p.toSendPort().send(null);
251 };
252 }
253
254 void testReadAsTextNonExistent() {
255 Directory temp = tempDir();
256 ReceivePort p = new ReceivePort();
257 p.receive((x, y) {
258 p.close();
259 temp.deleteRecursivelySync();
260 });
261 var file = new File("${temp.path}/nonExistentFile4");
262
263 // Non-existing file should throw exception.
264 Expect.throws(() => file.readAsTextSync(),
265 (e) => checkOpenNonExistentFileException(e));
266
267 file.readAsText(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
268 file.onError = (e) {
269 checkOpenNonExistentFileException(e);
270 p.toSendPort().send(null);
271 };
272 }
273
274 testReadAsLinesNonExistent() {
275 Directory temp = tempDir();
276 ReceivePort p = new ReceivePort();
277 p.receive((x, y) {
278 p.close();
279 temp.deleteRecursivelySync();
280 });
281 var file = new File("${temp.path}/nonExistentFile5");
282
283 // Non-existing file should throw exception.
284 Expect.throws(() => file.readAsLinesSync(),
285 (e) => checkOpenNonExistentFileException(e));
286
287 file.readAsLines(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
288 file.onError = (e) {
289 checkOpenNonExistentFileException(e);
290 p.toSendPort().send(null);
291 };
292 }
293
294 bool checkWriteReadOnlyFileException(e) {
295 Expect.isTrue(e is FileIOException);
296 Expect.isTrue(e.osError != null);
297 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode);
298 return true;
299 }
300
301
302 // Create a test file in a temporary directory. Setup a port to signal
303 // when the temporary directory should be deleted. Pass the file and
304 // the port to the callback argument.
305 createTestFile(callback) {
306 Directory temp = tempDir();
307 ReceivePort p = new ReceivePort();
308 p.receive((x, y) {
309 p.close();
310 temp.deleteRecursivelySync();
311 });
312
313 var file = new File("${temp.path}/test_file");
314 file.createSync();
315 callback(file, p.toSendPort());
316 }
317
318
319 testWriteByteToReadOnlyFile() {
320 createTestFile((file, port) {
321 var openedFile = file.openSync(FileMode.READ);
322
323 // Writing to read only file should throw an exception.
324 Expect.throws(() => openedFile.writeByteSync(0),
325 (e) => checkWriteReadOnlyFileException(e));
326
327 openedFile.writeByte(0);
328 openedFile.onError = (e) {
329 checkWriteReadOnlyFileException(e);
330 openedFile.close(() => port.send(null));
331 };
332 });
333 }
334
335 testWriteListToReadOnlyFile() {
336 createTestFile((file, port) {
337 var openedFile = file.openSync(FileMode.READ);
338
339 List data = [0, 1, 2, 3];
340 // Writing to read only file should throw an exception.
341 Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
342 (e) => checkWriteReadOnlyFileException(e));
343
344 openedFile.writeList(data, 0, data.length);
345 openedFile.onError = (e) {
346 checkWriteReadOnlyFileException(e);
347 openedFile.close(() => port.send(null));
348 };
349 });
350 }
351
352 testTruncateReadOnlyFile() {
353 createTestFile((file, port) {
354 var openedFile = file.openSync(FileMode.WRITE);
355 openedFile.writeByteSync(0);
356 openedFile.closeSync();
357 openedFile = file.openSync(FileMode.READ);
358
359 // Truncating read only file should throw an exception.
360 Expect.throws(() => openedFile.truncateSync(0),
361 (e) => checkWriteReadOnlyFileException(e));
362
363 openedFile.truncate(0, () => Expect.fail("Unreachable code"));
364 openedFile.onError = (e) {
365 checkWriteReadOnlyFileException(e);
366 openedFile.close(() => port.send(null));
367 };
368 });
369 }
370
371 bool checkFileClosedException(e) {
372 Expect.isTrue(e is FileIOException);
373 Expect.isTrue(e.toString().indexOf("File closed") != -1);
374 Expect.isTrue(e.osError == null);
375 return true;
376 }
377
378 testOperateOnClosedFile() {
379 createTestFile((file, port) {
380 var openedFile = file.openSync(FileMode.READ);
381 openedFile.closeSync();
382
383 List data = [0, 1, 2, 3];
384 Expect.throws(() => openedFile.readByteSync(),
385 (e) => checkFileClosedException(e));
386 Expect.throws(() => openedFile.writeByteSync(0),
387 (e) => checkFileClosedException(e));
388 Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
389 (e) => checkFileClosedException(e));
390 Expect.throws(() => openedFile.readListSync(data, 0, data.length),
391 (e) => checkFileClosedException(e));
392 Expect.throws(() => openedFile.writeStringSync("Hello"),
393 (e) => checkFileClosedException(e));
394 Expect.throws(() => openedFile.positionSync(),
395 (e) => checkFileClosedException(e));
396 Expect.throws(() => openedFile.setPositionSync(0),
397 (e) => checkFileClosedException(e));
398 Expect.throws(() => openedFile.truncateSync(0),
399 (e) => checkFileClosedException(e));
400 Expect.throws(() => openedFile.lengthSync(),
401 (e) => checkFileClosedException(e));
402 Expect.throws(() => openedFile.flushSync(),
403 (e) => checkFileClosedException(e));
404
405 var errorCount = 0;
406 openedFile.readByte((byte) => Expect.fail("Unreachable code"));
407 errorCount++;
408 openedFile.writeByte(0);
409 errorCount++;
410 openedFile.readList(
411 data, 0, data.length, (bytesRead) => Expect.fail("Unreachable code"));
412 errorCount++;
413 openedFile.writeList(data, 0, data.length);
414 errorCount++;
415 openedFile.writeString("Hello");
416 errorCount++;
417 openedFile.position((position) => Expect.fail("Unreachable code"));
418 errorCount++;
419 openedFile.setPosition(0, () => Expect.fail("Unreachable code"));
420 errorCount++;
421 openedFile.truncate(0, () => Expect.fail("Unreachable code"));
422 errorCount++;
423 openedFile.length((length) => Expect.fail("Unreachable code"));
424 errorCount++;
425 openedFile.flush(() => Expect.fail("Unreachable code"));
426 errorCount++;
427
428 openedFile.onError = (e) {
429 checkFileClosedException(e);
430 if (--errorCount == 0) {
431 port.send(null);
432 }
433 };
434 });
435 }
436
437 testRepeatedlyCloseFile() {
438 createTestFile((file, port) {
439 var openedFile = file.openSync();
440 openedFile.close(() {
441 openedFile.onError = (e) {
442 Expect.isTrue(e is FileIOException);
443 port.send(null);
444 };
445 openedFile.close(() => null);
446 });
447 });
448 }
449
450 main() {
451 testOpenNonExistent();
452 testDeleteNonExistent();
453 testLengthNonExistent();
454 testCreateInNonExistentDirectory();
455 testFullPathOnNonExistentDirectory();
456 testDirectoryInNonExistentDirectory();
457 testReadAsBytesNonExistent();
458 testReadAsTextNonExistent();
459 testReadAsLinesNonExistent();
460 testWriteByteToReadOnlyFile();
461 testWriteListToReadOnlyFile();
462 testTruncateReadOnlyFile();
463 testOperateOnClosedFile();
464 testRepeatedlyCloseFile();
465 }
OLDNEW
« no previous file with comments | « tests/standalone/src/io/EchoServerTest.dart ('k') | tests/standalone/src/io/FileInputStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698