OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 import 'dart:async'; | |
6 import 'dart:io'; | |
7 import 'dart:convert'; | |
8 | |
9 import 'package:async_helper/async_helper.dart'; | |
10 import 'package:expect/expect.dart'; | |
11 | |
12 testBindShared(bool v6Only) { | |
13 asyncStart(); | |
14 ServerSocket.bind( | |
15 'localhost', 0, v6Only: v6Only, shared: true).then((socket) { | |
16 Expect.isTrue(socket.port > 0); | |
17 | |
18 asyncStart(); | |
19 return ServerSocket.bind( | |
20 'localhost', socket.port, v6Only: v6Only, shared: true).then((socket2) { | |
21 Expect.equals(socket.address.address, socket2.address.address); | |
22 Expect.equals(socket.port, socket2.port); | |
23 socket.close().whenComplete(asyncEnd); | |
24 socket2.close().whenComplete(asyncEnd); | |
25 }); | |
26 }); | |
27 } | |
28 | |
29 negTestBindSharedMismatch(bool v6Only) { | |
30 asyncStart(); | |
31 ServerSocket.bind('localhost', 0, v6Only: v6Only).then((ServerSocket socket) { | |
32 Expect.isTrue(socket.port > 0); | |
33 | |
34 asyncStart(); | |
35 return ServerSocket.bind( | |
36 'localhost', socket.port, v6Only: v6Only).catchError((error) { | |
37 Expect.isTrue(error is SocketException); | |
38 Expect.isTrue('$error'.contains('shared flag')); | |
39 socket.close().whenComplete(asyncEnd); | |
40 asyncEnd(); | |
41 }); | |
42 }); | |
43 } | |
44 | |
45 negTestBindV6OnlyMismatch(bool v6Only) { | |
46 asyncStart(); | |
47 ServerSocket.bind( | |
48 'localhost', 0, v6Only: v6Only, shared: true).then((ServerSocket socket) { | |
49 Expect.isTrue(socket.port > 0); | |
50 | |
51 asyncStart(); | |
52 return ServerSocket.bind( | |
53 'localhost', socket.port, v6Only: !v6Only, shared: true) | |
54 .catchError((error) { | |
55 Expect.isTrue(error is SocketException); | |
56 Expect.isTrue('$error'.contains('v6Only flag')); | |
57 socket.close().whenComplete(asyncEnd); | |
58 asyncEnd(); | |
59 }); | |
60 }); | |
61 } | |
62 | |
63 Future testBindDifferentAddresses(InternetAddress addr1, | |
64 InternetAddress addr2, | |
65 bool addr1V6Only, | |
66 bool addr2V6Only) { | |
67 asyncStart(); | |
68 return ServerSocket.bind( | |
69 addr1, 0, v6Only: addr1V6Only, shared: false).then((socket) { | |
70 Expect.isTrue(socket.port > 0); | |
71 | |
72 asyncStart(); | |
73 return ServerSocket.bind( | |
74 addr2, socket.port, v6Only: addr2V6Only, shared: false).then((socket2) { | |
75 Expect.equals(socket.port, socket2.port); | |
76 | |
77 return Future.wait([ | |
78 socket.close().whenComplete(asyncEnd), | |
79 socket2.close().whenComplete(asyncEnd), | |
80 ]); | |
81 }); | |
82 }); | |
83 } | |
84 | |
85 testSocketReferenceInteroperability() { | |
86 asyncStart(); | |
87 ServerSocket.bind('localhost', 0).then((ServerSocket socket) { | |
88 Expect.isTrue(socket.port > 0); | |
89 | |
90 asyncStart(); | |
91 socket.reference.create().then((socket2) { | |
92 bool gotResponseFrom1; | |
93 bool gotResponseFrom2; | |
94 | |
95 Expect.isTrue(socket.port > 0); | |
96 Expect.equals(socket.port, socket2.port); | |
97 | |
98 asyncStart(); | |
99 asyncStart(); | |
100 socket.listen((client) { | |
101 client.drain().whenComplete(asyncEnd); | |
102 client.write('1: hello world'); | |
103 client.close(); | |
104 socket.close().whenComplete(asyncEnd); | |
105 }, onDone: asyncEnd); | |
106 | |
107 asyncStart(); | |
108 asyncStart(); | |
109 socket2.listen((client) { | |
110 client.drain().whenComplete(asyncEnd); | |
111 client.write('2: hello world'); | |
112 client.close(); | |
113 socket2.close().whenComplete(asyncEnd); | |
114 }, onDone: asyncEnd); | |
115 | |
116 var futures = []; | |
117 for (int i = 0; i < 2; i++) { | |
118 asyncStart(); | |
119 futures.add( | |
120 Socket.connect(socket.address, socket.port).then((Socket socket) { | |
121 socket.close().whenComplete(asyncEnd); | |
122 asyncStart(); | |
123 return socket | |
124 .transform(ASCII.decoder).join('').then((String result) { | |
125 if (result == '1: hello world') gotResponseFrom1 = true; | |
126 else if (result == '2: hello world') gotResponseFrom2 = true; | |
127 else throw 'Unexpected result from server: $result'; | |
128 asyncEnd(); | |
129 }); | |
130 })); | |
131 } | |
132 asyncStart(); | |
133 Future.wait(futures).then((_) { | |
134 Expect.isTrue(gotResponseFrom1); | |
135 Expect.isTrue(gotResponseFrom2); | |
136 asyncEnd(); | |
137 }); | |
138 }); | |
139 }); | |
140 } | |
141 | |
142 void main() { | |
143 testBindShared(false); | |
144 testBindShared(true); | |
145 | |
146 negTestBindSharedMismatch(false); | |
147 negTestBindSharedMismatch(true); | |
148 | |
149 negTestBindV6OnlyMismatch(true); | |
150 negTestBindV6OnlyMismatch(false); | |
151 | |
152 asyncStart(); | |
153 testBindDifferentAddresses(InternetAddress.ANY_IP_V6, | |
154 InternetAddress.ANY_IP_V4, | |
155 true, | |
156 false).then((_) { | |
157 testBindDifferentAddresses(InternetAddress.ANY_IP_V4, | |
158 InternetAddress.ANY_IP_V6, | |
159 false, | |
160 true); | |
161 asyncEnd(); | |
162 }); | |
163 | |
164 testSocketReferenceInteroperability(); | |
165 } | |
OLD | NEW |