| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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 // Tests socket exceptions. | |
| 6 | |
| 7 #import("dart:io"); | |
| 8 | |
| 9 class SocketExceptionTest { | |
| 10 | |
| 11 static final PORT = 0; | |
| 12 static final HOST = "127.0.0.1"; | |
| 13 | |
| 14 static void serverSocketExceptionTest() { | |
| 15 bool exceptionCaught = false; | |
| 16 bool wrongExceptionCaught = false; | |
| 17 | |
| 18 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
| 19 Expect.equals(true, server !== null); | |
| 20 server.close(); | |
| 21 try { | |
| 22 server.close(); | |
| 23 } catch (SocketIOException ex) { | |
| 24 exceptionCaught = true; | |
| 25 } catch (Exception ex) { | |
| 26 wrongExceptionCaught = true; | |
| 27 } | |
| 28 Expect.equals(false, exceptionCaught); | |
| 29 Expect.equals(true, !wrongExceptionCaught); | |
| 30 } | |
| 31 | |
| 32 static void clientSocketExceptionTest() { | |
| 33 bool exceptionCaught = false; | |
| 34 bool wrongExceptionCaught = false; | |
| 35 | |
| 36 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
| 37 Expect.equals(true, server !== null); | |
| 38 int port = server.port; | |
| 39 Socket client = new Socket(HOST, port); | |
| 40 Expect.equals(true, client !== null); | |
| 41 InputStream input = client.inputStream; | |
| 42 OutputStream output = client.outputStream; | |
| 43 client.close(); | |
| 44 try { | |
| 45 client.close(); | |
| 46 } catch (SocketIOException ex) { | |
| 47 exceptionCaught = true; | |
| 48 } catch (Exception ex) { | |
| 49 wrongExceptionCaught = true; | |
| 50 } | |
| 51 Expect.equals(false, exceptionCaught); | |
| 52 Expect.equals(true, !wrongExceptionCaught); | |
| 53 exceptionCaught = false; | |
| 54 try { | |
| 55 client.available(); | |
| 56 } catch (SocketIOException ex) { | |
| 57 exceptionCaught = true; | |
| 58 } catch (Exception ex) { | |
| 59 wrongExceptionCaught = true; | |
| 60 } | |
| 61 Expect.equals(true, exceptionCaught); | |
| 62 Expect.equals(true, !wrongExceptionCaught); | |
| 63 exceptionCaught = false; | |
| 64 try { | |
| 65 List<int> buffer = new List<int>(10); | |
| 66 client.readList(buffer, 0 , 10); | |
| 67 } catch (SocketIOException ex) { | |
| 68 exceptionCaught = true; | |
| 69 } catch (Exception ex) { | |
| 70 wrongExceptionCaught = true; | |
| 71 } | |
| 72 Expect.equals(true, exceptionCaught); | |
| 73 Expect.equals(true, !wrongExceptionCaught); | |
| 74 exceptionCaught = false; | |
| 75 try { | |
| 76 List<int> buffer = new List<int>(10); | |
| 77 client.writeList(buffer, 0, 10); | |
| 78 } catch (SocketIOException ex) { | |
| 79 exceptionCaught = true; | |
| 80 } catch (Exception ex) { | |
| 81 wrongExceptionCaught = true; | |
| 82 } | |
| 83 Expect.equals(true, exceptionCaught); | |
| 84 Expect.equals(true, !wrongExceptionCaught); | |
| 85 exceptionCaught = false; | |
| 86 try { | |
| 87 List<int> buffer = new List<int>(42); | |
| 88 bool readDone = input.readInto(buffer, 0, 12); | |
| 89 } catch (SocketIOException ex) { | |
| 90 exceptionCaught = true; | |
| 91 } catch (Exception ex) { | |
| 92 wrongExceptionCaught = true; | |
| 93 } | |
| 94 Expect.equals(true, exceptionCaught); | |
| 95 Expect.equals(true, !wrongExceptionCaught); | |
| 96 exceptionCaught = false; | |
| 97 try { | |
| 98 List<int> buffer = new List<int>(42); | |
| 99 output.writeFrom(buffer, 0, 12); | |
| 100 } catch (SocketIOException ex) { | |
| 101 exceptionCaught = true; | |
| 102 } catch (Exception ex) { | |
| 103 wrongExceptionCaught = true; | |
| 104 } | |
| 105 Expect.equals(true, exceptionCaught); | |
| 106 Expect.equals(true, !wrongExceptionCaught); | |
| 107 | |
| 108 server.close(); | |
| 109 } | |
| 110 | |
| 111 static void indexOutOfRangeExceptionTest() { | |
| 112 bool exceptionCaught = false; | |
| 113 bool wrongExceptionCaught = false; | |
| 114 | |
| 115 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
| 116 Expect.equals(true, server !== null); | |
| 117 int port = server.port; | |
| 118 Socket client = new Socket(HOST, port); | |
| 119 Expect.equals(true, client !== null); | |
| 120 try { | |
| 121 List<int> buffer = new List<int>(10); | |
| 122 client.readList(buffer, -1, 1); | |
| 123 } catch (IndexOutOfRangeException ex) { | |
| 124 exceptionCaught = true; | |
| 125 } catch (Exception ex) { | |
| 126 wrongExceptionCaught = true; | |
| 127 } | |
| 128 Expect.equals(true, exceptionCaught); | |
| 129 Expect.equals(true, !wrongExceptionCaught); | |
| 130 exceptionCaught = false; | |
| 131 | |
| 132 try { | |
| 133 List<int> buffer = new List<int>(10); | |
| 134 client.readList(buffer, 0, -1); | |
| 135 } catch (IndexOutOfRangeException ex) { | |
| 136 exceptionCaught = true; | |
| 137 } catch (Exception ex) { | |
| 138 wrongExceptionCaught = true; | |
| 139 } | |
| 140 Expect.equals(true, exceptionCaught); | |
| 141 Expect.equals(true, !wrongExceptionCaught); | |
| 142 exceptionCaught = false; | |
| 143 | |
| 144 try { | |
| 145 List<int> buffer = new List<int>(10); | |
| 146 client.writeList(buffer, -1, 1); | |
| 147 } catch (IndexOutOfRangeException ex) { | |
| 148 exceptionCaught = true; | |
| 149 } catch (Exception ex) { | |
| 150 wrongExceptionCaught = true; | |
| 151 } | |
| 152 Expect.equals(true, exceptionCaught); | |
| 153 Expect.equals(true, !wrongExceptionCaught); | |
| 154 exceptionCaught = false; | |
| 155 | |
| 156 try { | |
| 157 List<int> buffer = new List<int>(10); | |
| 158 client.writeList(buffer, 0, -1); | |
| 159 } catch (IndexOutOfRangeException ex) { | |
| 160 exceptionCaught = true; | |
| 161 } catch (Exception ex) { | |
| 162 wrongExceptionCaught = true; | |
| 163 } | |
| 164 Expect.equals(true, exceptionCaught); | |
| 165 Expect.equals(true, !wrongExceptionCaught); | |
| 166 | |
| 167 server.close(); | |
| 168 client.close(); | |
| 169 } | |
| 170 | |
| 171 static void testMain() { | |
| 172 serverSocketExceptionTest(); | |
| 173 clientSocketExceptionTest(); | |
| 174 indexOutOfRangeExceptionTest(); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 main() { | |
| 179 SocketExceptionTest.testMain(); | |
| 180 } | |
| OLD | NEW |