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

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

Issue 9839053: Add error handling to random access file (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r5810 Created 8 years, 9 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
« no previous file with comments | « runtime/bin/file_win.cc ('k') | tests/standalone/src/io/FileTest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 // 4 //
5 // Dart test program for testing error handling in file I/O. 5 // Dart test program for testing error handling in file I/O.
6 6
7 #import("dart:io"); 7 #import("dart:io");
8 #import("dart:isolate"); 8 #import("dart:isolate");
9 9
10 String tempDir() { 10 Directory tempDir() {
11 var d = new Directory(''); 11 var d = new Directory('');
12 d.createTempSync(); 12 d.createTempSync();
13 return d.path; 13 return d;
14 } 14 }
15 15
16 16
17 bool checkOpenNonExistentFileException(e) { 17 bool checkOpenNonExistentFileException(e) {
18 Expect.isTrue(e is FileIOException); 18 Expect.isTrue(e is FileIOException);
19 Expect.isTrue(e.osError != null); 19 Expect.isTrue(e.osError != null);
20 Expect.isTrue(e.toString().indexOf("Cannot open file") != -1); 20 Expect.isTrue(e.toString().indexOf("Cannot open file") != -1);
21 Platform platform = new Platform(); 21 Platform platform = new Platform();
22 if (platform.operatingSystem() == "linux") { 22 if (platform.operatingSystem() == "linux") {
23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); 23 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
24 } else if (platform.operatingSystem() == "macos") { 24 } else if (platform.operatingSystem() == "macos") {
25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1); 25 Expect.isTrue(e.toString().indexOf("No such file or directory") != -1);
26 } else if (platform.operatingSystem() == "windows") { 26 } else if (platform.operatingSystem() == "windows") {
27 Expect.isTrue( 27 Expect.isTrue(
28 e.toString().indexOf( 28 e.toString().indexOf(
29 "The system cannot find the file specified") != -1); 29 "The system cannot find the file specified") != -1);
30 } 30 }
31 // File not not found has error code 2 on all supported platforms. 31 // File not not found has error code 2 on all supported platforms.
32 Expect.equals(2, e.osError.errorCode); 32 Expect.equals(2, e.osError.errorCode);
33 33
34 return true; 34 return true;
35 } 35 }
36 36
37 void testOpenNonExistent() { 37 void testOpenNonExistent() {
38 var file = new File("${tempDir()}/nonExistentFile"); 38 var file = new File("${tempDir().path}/nonExistentFile");
39 39
40 // Non-existing file should throw exception. 40 // Non-existing file should throw exception.
41 Expect.throws(() => file.openSync(), 41 Expect.throws(() => file.openSync(),
42 (e) => checkOpenNonExistentFileException(e)); 42 (e) => checkOpenNonExistentFileException(e));
43 43
44 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code")); 44 file.open(FileMode.READ, (raf) => Expect.fail("Unreachable code"));
45 file.onError = (e) => checkOpenNonExistentFileException(e); 45 file.onError = (e) => checkOpenNonExistentFileException(e);
46 } 46 }
47 47
48 bool checkDeleteNonExistentFileException(e) { 48 bool checkDeleteNonExistentFileException(e) {
(...skipping 10 matching lines...) Expand all
59 e.toString().indexOf( 59 e.toString().indexOf(
60 "The system cannot find the file specified") != -1); 60 "The system cannot find the file specified") != -1);
61 } 61 }
62 // File not not found has error code 2 on all supported platforms. 62 // File not not found has error code 2 on all supported platforms.
63 Expect.equals(2, e.osError.errorCode); 63 Expect.equals(2, e.osError.errorCode);
64 64
65 return true; 65 return true;
66 } 66 }
67 67
68 void testDeleteNonExistent() { 68 void testDeleteNonExistent() {
69 var file = new File("${tempDir()}/nonExistentFile"); 69 var file = new File("${tempDir().path}/nonExistentFile");
70 70
71 // Non-existing file should throw exception. 71 // Non-existing file should throw exception.
72 Expect.throws(() => file.deleteSync(), 72 Expect.throws(() => file.deleteSync(),
73 (e) => checkDeleteNonExistentFileException(e)); 73 (e) => checkDeleteNonExistentFileException(e));
74 74
75 file.delete(() => Expect.fail("Unreachable code")); 75 file.delete(() => Expect.fail("Unreachable code"));
76 file.onError = (e) => checkDeleteNonExistentFileException(e); 76 file.onError = (e) => checkDeleteNonExistentFileException(e);
77 } 77 }
78 78
79 bool checkCreateInNonExistentDirectoryException(e) { 79 bool checkCreateInNonExistentDirectoryException(e) {
(...skipping 11 matching lines...) Expand all
91 Expect.isTrue( 91 Expect.isTrue(
92 e.toString().indexOf( 92 e.toString().indexOf(
93 "The system cannot find the path specified") != -1); 93 "The system cannot find the path specified") != -1);
94 Expect.equals(3, e.osError.errorCode); 94 Expect.equals(3, e.osError.errorCode);
95 } 95 }
96 96
97 return true; 97 return true;
98 } 98 }
99 99
100 void testCreateInNonExistentDirectory() { 100 void testCreateInNonExistentDirectory() {
101 var file = new File("${tempDir()}/nonExistentDirectory/newFile"); 101 var file = new File("${tempDir().path}/nonExistentDirectory/newFile");
102 102
103 // Create in non-existent directory should throw exception. 103 // Create in non-existent directory should throw exception.
104 Expect.throws(() => file.createSync(), 104 Expect.throws(() => file.createSync(),
105 (e) => checkCreateInNonExistentDirectoryException(e)); 105 (e) => checkCreateInNonExistentDirectoryException(e));
106 106
107 file.create(() => Expect.fail("Unreachable code")); 107 file.create(() => Expect.fail("Unreachable code"));
108 file.onError = (e) => checkCreateInNonExistentDirectoryException(e); 108 file.onError = (e) => checkCreateInNonExistentDirectoryException(e);
109 } 109 }
110 110
111 bool checkFullPathOnNonExistentDirectoryException(e) { 111 bool checkFullPathOnNonExistentDirectoryException(e) {
(...skipping 10 matching lines...) Expand all
122 e.toString().indexOf( 122 e.toString().indexOf(
123 "The system cannot find the file specified") != -1); 123 "The system cannot find the file specified") != -1);
124 } 124 }
125 // File not not found has error code 2 on all supported platforms. 125 // File not not found has error code 2 on all supported platforms.
126 Expect.equals(2, e.osError.errorCode); 126 Expect.equals(2, e.osError.errorCode);
127 127
128 return true; 128 return true;
129 } 129 }
130 130
131 void testFullPathOnNonExistentDirectory() { 131 void testFullPathOnNonExistentDirectory() {
132 var file = new File("${tempDir()}/nonExistentDirectory"); 132 var file = new File("${tempDir().path}/nonExistentDirectory");
133 133
134 // Full path non-existent directory should throw exception. 134 // Full path non-existent directory should throw exception.
135 Expect.throws(() => file.fullPathSync(), 135 Expect.throws(() => file.fullPathSync(),
136 (e) => checkFullPathOnNonExistentDirectoryException(e)); 136 (e) => checkFullPathOnNonExistentDirectoryException(e));
137 137
138 file.fullPath((path) => Expect.fail("Unreachable code $path")); 138 file.fullPath((path) => Expect.fail("Unreachable code $path"));
139 file.onError = (e) => checkFullPathOnNonExistentDirectoryException(e); 139 file.onError = (e) => checkFullPathOnNonExistentDirectoryException(e);
140 } 140 }
141 141
142 bool checkDirectoryInNonExistentDirectoryException(e) { 142 bool checkDirectoryInNonExistentDirectoryException(e) {
(...skipping 11 matching lines...) Expand all
154 e.toString().indexOf( 154 e.toString().indexOf(
155 "The system cannot find the file specified") != -1); 155 "The system cannot find the file specified") != -1);
156 } 156 }
157 // File not not found has error code 2 on all supported platforms. 157 // File not not found has error code 2 on all supported platforms.
158 Expect.equals(2, e.osError.errorCode); 158 Expect.equals(2, e.osError.errorCode);
159 159
160 return true; 160 return true;
161 } 161 }
162 162
163 void testDirectoryInNonExistentDirectory() { 163 void testDirectoryInNonExistentDirectory() {
164 var file = new File("${tempDir()}/nonExistentDirectory/newFile"); 164 var file = new File("${tempDir().path}/nonExistentDirectory/newFile");
165 165
166 // Create in non-existent directory should throw exception. 166 // Create in non-existent directory should throw exception.
167 Expect.throws(() => file.directorySync(), 167 Expect.throws(() => file.directorySync(),
168 (e) => checkDirectoryInNonExistentDirectoryException(e)); 168 (e) => checkDirectoryInNonExistentDirectoryException(e));
169 169
170 file.directory((directory) => Expect.fail("Unreachable code")); 170 file.directory((directory) => Expect.fail("Unreachable code"));
171 file.onError = (e) => checkDirectoryInNonExistentDirectoryException(e); 171 file.onError = (e) => checkDirectoryInNonExistentDirectoryException(e);
172 } 172 }
173 173
174 void testReadAsBytesNonExistent() { 174 void testReadAsBytesNonExistent() {
175 var file = new File("${tempDir()}/nonExistentFile3"); 175 var file = new File("${tempDir().path}/nonExistentFile3");
176 176
177 // Non-existing file should throw exception. 177 // Non-existing file should throw exception.
178 Expect.throws(() => file.readAsBytesSync(), 178 Expect.throws(() => file.readAsBytesSync(),
179 (e) => checkOpenNonExistentFileException(e)); 179 (e) => checkOpenNonExistentFileException(e));
180 180
181 // TODO(sgjesse): Handle error for file.readAsBytes as well. 181 file.readAsBytes((data) => Expect.fail("Unreachable code"));
182 file.onError = (e) => checkOpenNonExistentFileException(e);
182 } 183 }
183 184
184 void testReadAsTextNonExistent() { 185 void testReadAsTextNonExistent() {
185 var file = new File("${tempDir()}/nonExistentFile4"); 186 var file = new File("${tempDir().path}/nonExistentFile4");
186 187
187 // Non-existing file should throw exception. 188 // Non-existing file should throw exception.
188 Expect.throws(() => file.readAsTextSync(), 189 Expect.throws(() => file.readAsTextSync(),
189 (e) => checkOpenNonExistentFileException(e)); 190 (e) => checkOpenNonExistentFileException(e));
190 191
191 // TODO(sgjesse): Handle error for file.readAsText as well. 192 file.readAsText(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
193 file.onError = (e) => checkOpenNonExistentFileException(e);
192 } 194 }
193 195
194 void testReadAsLinesNonExistent() { 196 void testReadAsLinesNonExistent() {
195 var file = new File("${tempDir()}/nonExistentFile5"); 197 var file = new File("${tempDir().path}/nonExistentFile5");
196 198
197 // Non-existing file should throw exception. 199 // Non-existing file should throw exception.
198 Expect.throws(() => file.readAsLinesSync(), 200 Expect.throws(() => file.readAsLinesSync(),
199 (e) => checkOpenNonExistentFileException(e)); 201 (e) => checkOpenNonExistentFileException(e));
200 202
201 // TODO(sgjesse): Handle error for file.readAsLines as well. 203 file.readAsLines(Encoding.ASCII, (data) => Expect.fail("Unreachable code"));
204 file.onError = (e) => checkOpenNonExistentFileException(e);
205 }
206
207 bool checkWriteReadOnlyFileException(e) {
208 Expect.isTrue(e is FileIOException);
209 Expect.isTrue(e.osError != null);
210 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode);
211 return true;
212 }
213
214 testWriteByteToReadOnlyFile() {
215 Directory temp = tempDir();
216 ReceivePort p = new ReceivePort();
217 p.receive((x,y) {
218 p.close();
219 temp.deleteRecursivelySync();
220 });
221
222 var file = new File("${temp.path}/test_file");
223 file.createSync();
224 var openedFile = file.openSync(FileMode.READ);
225
226 // Writing to read only file should throw an exception.
227 Expect.throws(() => openedFile.writeByteSync(0),
228 (e) => checkWriteReadOnlyFileException(e));
229
230 openedFile.writeByte(0);
231 openedFile.onError = (e) {
232 checkWriteReadOnlyFileException(e);
233 p.toSendPort().send(null);
234 };
235 }
236
237 testWriteListToReadOnlyFile() {
238 Directory temp = tempDir();
239 ReceivePort p = new ReceivePort();
240 p.receive((x,y) {
241 p.close();
242 temp.deleteRecursivelySync();
243 });
244
245 var file = new File("${temp.path}/test_file");
246 file.createSync();
247 var openedFile = file.openSync(FileMode.READ);
248
249 List data = [0, 1, 2, 3];
250 // Writing to read only file should throw an exception.
251 Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
252 (e) => checkWriteReadOnlyFileException(e));
253
254 openedFile.writeList(data, 0, data.length);
255 openedFile.onError = (e) {
256 checkWriteReadOnlyFileException(e);
257 p.toSendPort().send(null);
258 };
259 }
260
261 testTruncateReadOnlyFile() {
262 Directory temp = tempDir();
263 ReceivePort p = new ReceivePort();
264 p.receive((x,y) {
265 p.close();
266 temp.deleteRecursivelySync();
267 });
268
269 var file = new File("${temp.path}/test_file");
270 file.createSync();
271 var openedFile = file.openSync(FileMode.READ);
272
273 // Truncating read only file should throw an exception.
274 Expect.throws(() => openedFile.truncateSync(0),
275 (e) => checkWriteReadOnlyFileException(e));
276
277 openedFile.truncate(0, () => Expect.fail("Unreachable code"));
278 openedFile.onError = (e) {
279 checkWriteReadOnlyFileException(e);
280 p.toSendPort().send(null);
281 };
282 }
283
284 bool checkFileClosedException(e) {
285 Expect.isTrue(e is FileIOException);
286 Expect.isTrue(e.toString().indexOf("File closed") != -1);
287 Expect.isTrue(e.osError == null);
288 return true;
289 }
290
291 testOperateOnClosedFile() {
292 Directory temp = tempDir();
293 ReceivePort p = new ReceivePort();
294 p.receive((x,y) {
295 p.close();
296 temp.deleteRecursivelySync();
297 });
298
299 var file = new File("${tempDir().path}/test_file");
300 file.createSync();
301 var openedFile = file.openSync(FileMode.READ);
302 openedFile.closeSync();
303
304 List data = [0, 1, 2, 3];
305 Expect.throws(() => openedFile.readByteSync(),
306 (e) => checkFileClosedException(e));
307 Expect.throws(() => openedFile.writeByteSync(0),
308 (e) => checkFileClosedException(e));
309 Expect.throws(() => openedFile.writeListSync(data, 0, data.length),
310 (e) => checkFileClosedException(e));
311 Expect.throws(() => openedFile.readListSync(data, 0, data.length),
312 (e) => checkFileClosedException(e));
313 Expect.throws(() => openedFile.writeStringSync("Hello"),
314 (e) => checkFileClosedException(e));
315 Expect.throws(() => openedFile.positionSync(),
316 (e) => checkFileClosedException(e));
317 Expect.throws(() => openedFile.setPositionSync(0),
318 (e) => checkFileClosedException(e));
319 Expect.throws(() => openedFile.truncateSync(0),
320 (e) => checkFileClosedException(e));
321 Expect.throws(() => openedFile.lengthSync(),
322 (e) => checkFileClosedException(e));
323 Expect.throws(() => openedFile.flushSync(),
324 (e) => checkFileClosedException(e));
325
326 var errorCount = 0;
327 openedFile.readByte((byte) => Expect.fail("Unreachable code"));
328 errorCount++;
329 openedFile.writeByte(0);
330 errorCount++;
331 openedFile.readList(
332 data, 0, data.length, (bytesRead) => Expect.fail("Unreachable code"));
333 errorCount++;
334 openedFile.writeList(data, 0, data.length);
335 errorCount++;
336 openedFile.writeString("Hello");
337 errorCount++;
338 openedFile.position((position) => Expect.fail("Unreachable code"));
339 errorCount++;
340 openedFile.setPosition(0, () => Expect.fail("Unreachable code"));
341 errorCount++;
342 openedFile.truncate(0, () => Expect.fail("Unreachable code"));
343 errorCount++;
344 openedFile.length((length) => Expect.fail("Unreachable code"));
345 errorCount++;
346 openedFile.flush(() => Expect.fail("Unreachable code"));
347 errorCount++;
348
349 openedFile.onError = (e) {
350 checkFileClosedException(e);
351 if (--errorCount == 0) {
352 p.toSendPort().send(null);
353 }
354 };
202 } 355 }
203 356
204 main() { 357 main() {
205 testOpenNonExistent(); 358 testOpenNonExistent();
206 testDeleteNonExistent(); 359 testDeleteNonExistent();
207 testCreateInNonExistentDirectory(); 360 testCreateInNonExistentDirectory();
208 testFullPathOnNonExistentDirectory(); 361 testFullPathOnNonExistentDirectory();
209 testDirectoryInNonExistentDirectory(); 362 testDirectoryInNonExistentDirectory();
210 testReadAsBytesNonExistent(); 363 testReadAsBytesNonExistent();
211 testReadAsTextNonExistent(); 364 testReadAsTextNonExistent();
212 testReadAsLinesNonExistent(); 365 testReadAsLinesNonExistent();
366 testWriteByteToReadOnlyFile();
367 testWriteListToReadOnlyFile();
368 testTruncateReadOnlyFile();
369 testOperateOnClosedFile();
213 } 370 }
OLDNEW
« no previous file with comments | « runtime/bin/file_win.cc ('k') | tests/standalone/src/io/FileTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698