| OLD | NEW |
| 1 // Copyright (c) 2011, 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 // Tests socket exceptions. | 5 // Tests socket exceptions. |
| 6 | 6 |
| 7 #import("dart:isolate"); |
| 7 #import("dart:io"); | 8 #import("dart:io"); |
| 8 | 9 |
| 9 class SocketExceptionTest { | 10 class SocketExceptionTest { |
| 10 | 11 |
| 11 static final PORT = 0; | 12 static final PORT = 0; |
| 12 static final HOST = "127.0.0.1"; | 13 static final HOST = "127.0.0.1"; |
| 13 | 14 |
| 14 static void serverSocketExceptionTest() { | 15 static void serverSocketExceptionTest() { |
| 15 bool exceptionCaught = false; | 16 bool exceptionCaught = false; |
| 16 bool wrongExceptionCaught = false; | 17 bool wrongExceptionCaught = false; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 30 } | 31 } |
| 31 | 32 |
| 32 static void clientSocketExceptionTest() { | 33 static void clientSocketExceptionTest() { |
| 33 bool exceptionCaught = false; | 34 bool exceptionCaught = false; |
| 34 bool wrongExceptionCaught = false; | 35 bool wrongExceptionCaught = false; |
| 35 | 36 |
| 36 ServerSocket server = new ServerSocket(HOST, PORT, 10); | 37 ServerSocket server = new ServerSocket(HOST, PORT, 10); |
| 37 Expect.equals(true, server !== null); | 38 Expect.equals(true, server !== null); |
| 38 int port = server.port; | 39 int port = server.port; |
| 39 Socket client = new Socket(HOST, port); | 40 Socket client = new Socket(HOST, port); |
| 40 Expect.equals(true, client !== null); | 41 client.onConnect = () { |
| 41 InputStream input = client.inputStream; | 42 Expect.equals(true, client !== null); |
| 42 OutputStream output = client.outputStream; | 43 InputStream input = client.inputStream; |
| 43 client.close(); | 44 OutputStream output = client.outputStream; |
| 44 try { | |
| 45 client.close(); | 45 client.close(); |
| 46 } catch (SocketIOException ex) { | 46 try { |
| 47 exceptionCaught = true; | 47 client.close(); |
| 48 } catch (Exception ex) { | 48 } catch (SocketIOException ex) { |
| 49 wrongExceptionCaught = true; | 49 exceptionCaught = true; |
| 50 } | 50 } catch (Exception ex) { |
| 51 Expect.equals(false, exceptionCaught); | 51 wrongExceptionCaught = true; |
| 52 Expect.equals(true, !wrongExceptionCaught); | 52 } |
| 53 exceptionCaught = false; | 53 Expect.equals(false, exceptionCaught); |
| 54 try { | 54 Expect.equals(true, !wrongExceptionCaught); |
| 55 client.available(); | 55 exceptionCaught = false; |
| 56 } catch (SocketIOException ex) { | 56 try { |
| 57 exceptionCaught = true; | 57 client.available(); |
| 58 } catch (Exception ex) { | 58 } catch (SocketIOException ex) { |
| 59 wrongExceptionCaught = true; | 59 exceptionCaught = true; |
| 60 } | 60 } catch (Exception ex) { |
| 61 Expect.equals(true, exceptionCaught); | 61 wrongExceptionCaught = true; |
| 62 Expect.equals(true, !wrongExceptionCaught); | 62 } |
| 63 exceptionCaught = false; | 63 Expect.equals(true, exceptionCaught); |
| 64 try { | 64 Expect.equals(true, !wrongExceptionCaught); |
| 65 List<int> buffer = new List<int>(10); | 65 exceptionCaught = false; |
| 66 client.readList(buffer, 0 , 10); | 66 try { |
| 67 } catch (SocketIOException ex) { | 67 List<int> buffer = new List<int>(10); |
| 68 exceptionCaught = true; | 68 client.readList(buffer, 0 , 10); |
| 69 } catch (Exception ex) { | 69 } catch (SocketIOException ex) { |
| 70 wrongExceptionCaught = true; | 70 exceptionCaught = true; |
| 71 } | 71 } catch (Exception ex) { |
| 72 Expect.equals(true, exceptionCaught); | 72 wrongExceptionCaught = true; |
| 73 Expect.equals(true, !wrongExceptionCaught); | 73 } |
| 74 exceptionCaught = false; | 74 Expect.equals(true, exceptionCaught); |
| 75 try { | 75 Expect.equals(true, !wrongExceptionCaught); |
| 76 List<int> buffer = new List<int>(10); | 76 exceptionCaught = false; |
| 77 client.writeList(buffer, 0, 10); | 77 try { |
| 78 } catch (SocketIOException ex) { | 78 List<int> buffer = new List<int>(10); |
| 79 exceptionCaught = true; | 79 client.writeList(buffer, 0, 10); |
| 80 } catch (Exception ex) { | 80 } catch (SocketIOException ex) { |
| 81 wrongExceptionCaught = true; | 81 exceptionCaught = true; |
| 82 } | 82 } catch (Exception ex) { |
| 83 Expect.equals(true, exceptionCaught); | 83 wrongExceptionCaught = true; |
| 84 Expect.equals(true, !wrongExceptionCaught); | 84 } |
| 85 exceptionCaught = false; | 85 Expect.equals(true, exceptionCaught); |
| 86 try { | 86 Expect.equals(true, !wrongExceptionCaught); |
| 87 List<int> buffer = new List<int>(42); | 87 exceptionCaught = false; |
| 88 bool readDone = input.readInto(buffer, 0, 12); | 88 try { |
| 89 } catch (SocketIOException ex) { | 89 List<int> buffer = new List<int>(42); |
| 90 exceptionCaught = true; | 90 bool readDone = input.readInto(buffer, 0, 12); |
| 91 } catch (Exception ex) { | 91 } catch (SocketIOException ex) { |
| 92 wrongExceptionCaught = true; | 92 exceptionCaught = true; |
| 93 } | 93 } catch (Exception ex) { |
| 94 Expect.equals(true, exceptionCaught); | 94 wrongExceptionCaught = true; |
| 95 Expect.equals(true, !wrongExceptionCaught); | 95 } |
| 96 exceptionCaught = false; | 96 Expect.equals(true, exceptionCaught); |
| 97 try { | 97 Expect.equals(true, !wrongExceptionCaught); |
| 98 List<int> buffer = new List<int>(42); | 98 exceptionCaught = false; |
| 99 output.writeFrom(buffer, 0, 12); | 99 try { |
| 100 } catch (SocketIOException ex) { | 100 List<int> buffer = new List<int>(42); |
| 101 exceptionCaught = true; | 101 output.writeFrom(buffer, 0, 12); |
| 102 } catch (Exception ex) { | 102 } catch (SocketIOException ex) { |
| 103 wrongExceptionCaught = true; | 103 exceptionCaught = true; |
| 104 } | 104 } catch (Exception ex) { |
| 105 Expect.equals(true, exceptionCaught); | 105 wrongExceptionCaught = true; |
| 106 Expect.equals(true, !wrongExceptionCaught); | 106 } |
| 107 Expect.equals(true, exceptionCaught); |
| 108 Expect.equals(true, !wrongExceptionCaught); |
| 107 | 109 |
| 108 server.close(); | 110 server.close(); |
| 111 }; |
| 109 } | 112 } |
| 110 | 113 |
| 111 static void indexOutOfRangeExceptionTest() { | 114 static void indexOutOfRangeExceptionTest() { |
| 112 bool exceptionCaught = false; | 115 bool exceptionCaught = false; |
| 113 bool wrongExceptionCaught = false; | 116 bool wrongExceptionCaught = false; |
| 114 | 117 |
| 115 ServerSocket server = new ServerSocket(HOST, PORT, 10); | 118 ServerSocket server = new ServerSocket(HOST, PORT, 10); |
| 116 Expect.equals(true, server !== null); | 119 Expect.equals(true, server !== null); |
| 117 int port = server.port; | 120 int port = server.port; |
| 118 Socket client = new Socket(HOST, port); | 121 Socket client = new Socket(HOST, port); |
| 119 Expect.equals(true, client !== null); | 122 client.onConnect = () { |
| 120 try { | 123 Expect.equals(true, client !== null); |
| 121 List<int> buffer = new List<int>(10); | 124 try { |
| 122 client.readList(buffer, -1, 1); | 125 List<int> buffer = new List<int>(10); |
| 123 } catch (IndexOutOfRangeException ex) { | 126 client.readList(buffer, -1, 1); |
| 124 exceptionCaught = true; | 127 } catch (IndexOutOfRangeException ex) { |
| 125 } catch (Exception ex) { | 128 exceptionCaught = true; |
| 126 wrongExceptionCaught = true; | 129 } catch (Exception ex) { |
| 127 } | 130 wrongExceptionCaught = true; |
| 128 Expect.equals(true, exceptionCaught); | 131 } |
| 129 Expect.equals(true, !wrongExceptionCaught); | 132 Expect.equals(true, exceptionCaught); |
| 130 exceptionCaught = false; | 133 Expect.equals(true, !wrongExceptionCaught); |
| 134 exceptionCaught = false; |
| 131 | 135 |
| 132 try { | 136 try { |
| 133 List<int> buffer = new List<int>(10); | 137 List<int> buffer = new List<int>(10); |
| 134 client.readList(buffer, 0, -1); | 138 client.readList(buffer, 0, -1); |
| 135 } catch (IndexOutOfRangeException ex) { | 139 } catch (IndexOutOfRangeException ex) { |
| 136 exceptionCaught = true; | 140 exceptionCaught = true; |
| 137 } catch (Exception ex) { | 141 } catch (Exception ex) { |
| 138 wrongExceptionCaught = true; | 142 wrongExceptionCaught = true; |
| 139 } | 143 } |
| 140 Expect.equals(true, exceptionCaught); | 144 Expect.equals(true, exceptionCaught); |
| 141 Expect.equals(true, !wrongExceptionCaught); | 145 Expect.equals(true, !wrongExceptionCaught); |
| 142 exceptionCaught = false; | 146 exceptionCaught = false; |
| 143 | 147 |
| 144 try { | 148 try { |
| 145 List<int> buffer = new List<int>(10); | 149 List<int> buffer = new List<int>(10); |
| 146 client.writeList(buffer, -1, 1); | 150 client.writeList(buffer, -1, 1); |
| 147 } catch (IndexOutOfRangeException ex) { | 151 } catch (IndexOutOfRangeException ex) { |
| 148 exceptionCaught = true; | 152 exceptionCaught = true; |
| 149 } catch (Exception ex) { | 153 } catch (Exception ex) { |
| 150 wrongExceptionCaught = true; | 154 wrongExceptionCaught = true; |
| 151 } | 155 } |
| 152 Expect.equals(true, exceptionCaught); | 156 Expect.equals(true, exceptionCaught); |
| 153 Expect.equals(true, !wrongExceptionCaught); | 157 Expect.equals(true, !wrongExceptionCaught); |
| 154 exceptionCaught = false; | 158 exceptionCaught = false; |
| 155 | 159 |
| 156 try { | 160 try { |
| 157 List<int> buffer = new List<int>(10); | 161 List<int> buffer = new List<int>(10); |
| 158 client.writeList(buffer, 0, -1); | 162 client.writeList(buffer, 0, -1); |
| 159 } catch (IndexOutOfRangeException ex) { | 163 } catch (IndexOutOfRangeException ex) { |
| 160 exceptionCaught = true; | 164 exceptionCaught = true; |
| 161 } catch (Exception ex) { | 165 } catch (Exception ex) { |
| 162 wrongExceptionCaught = true; | 166 wrongExceptionCaught = true; |
| 163 } | 167 } |
| 164 Expect.equals(true, exceptionCaught); | 168 Expect.equals(true, exceptionCaught); |
| 165 Expect.equals(true, !wrongExceptionCaught); | 169 Expect.equals(true, !wrongExceptionCaught); |
| 166 | 170 |
| 167 server.close(); | 171 server.close(); |
| 168 client.close(); | 172 client.close(); |
| 173 }; |
| 174 } |
| 175 |
| 176 static void unknownHostTest() { |
| 177 // Port to verify that the test completes. |
| 178 var port = new ReceivePort(); |
| 179 port.receive((message, replyTo) => null); |
| 180 |
| 181 Socket s = new Socket("hede.hule.hest", 1234); |
| 182 s.onError = (e) => port.close(); |
| 183 s.onConnect = () => Expect.fail("Connection completed"); |
| 184 } |
| 185 |
| 186 static void unresponsiveHostTest() { |
| 187 // Port to keep the VM alive until test completes. |
| 188 var port = new ReceivePort(); |
| 189 port.receive((message, replyTo) => null); |
| 190 |
| 191 Socket s = new Socket("127.0.0.1", 65535); |
| 192 s.onError = (e) => port.close(); |
| 193 s.onConnect = () => Expect.fail("Connection completed"); |
| 169 } | 194 } |
| 170 | 195 |
| 171 static void testMain() { | 196 static void testMain() { |
| 172 serverSocketExceptionTest(); | 197 serverSocketExceptionTest(); |
| 173 clientSocketExceptionTest(); | 198 clientSocketExceptionTest(); |
| 174 indexOutOfRangeExceptionTest(); | 199 indexOutOfRangeExceptionTest(); |
| 200 unknownHostTest(); |
| 201 unresponsiveHostTest(); |
| 175 } | 202 } |
| 176 } | 203 } |
| 177 | 204 |
| 178 main() { | 205 main() { |
| 179 SocketExceptionTest.testMain(); | 206 SocketExceptionTest.testMain(); |
| 180 } | 207 } |
| OLD | NEW |