-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
LocalEndPointTest.cs
284 lines (220 loc) · 10.6 KB
/
LocalEndPointTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Sockets.Tests
{
public abstract class LocalEndPointTest<T> : SocketTestHelperBase<T> where T : SocketHelperBase, new()
{
protected abstract bool IPv6 { get; }
private IPAddress Wildcard => IPv6 ? IPAddress.IPv6Any : IPAddress.Any;
private IPAddress Loopback => IPv6 ? IPAddress.IPv6Loopback : IPAddress.Loopback;
public LocalEndPointTest(ITestOutputHelper output) : base(output) { }
[Fact]
public async Task UdpSocket_WhenBoundToWildcardAddress_LocalEPDoesNotChangeOnSendTo()
{
using (Socket receiver = CreateUdpSocket())
using (Socket sender = CreateUdpSocket())
{
int receiverPort = receiver.BindToAnonymousPort(Wildcard);
Assert.Null(sender.LocalEndPoint);
int senderPortAfterBind = sender.BindToAnonymousPort(Wildcard);
Assert.Equal(Wildcard, GetLocalEPAddress(sender)); // wildcard before sendto
var sendToEP = new IPEndPoint(Loopback, receiverPort);
await SendToAsync(sender, new byte[] { 1, 2, 3 }, sendToEP);
Assert.Equal(Wildcard, GetLocalEPAddress(sender)); // stays as wildcard after sendto
Assert.Equal(senderPortAfterBind, GetLocalEPPort(sender));
byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
receiver.ReceiveFrom(buf, ref receiveFromEP);
Assert.Equal(new byte[] { 1, 2, 3 }, buf);
Assert.Equal(Loopback, ((IPEndPoint)receiveFromEP).Address); // received from specific address
Assert.Equal(senderPortAfterBind, ((IPEndPoint)receiveFromEP).Port);
}
}
[Fact]
public async Task UdpSocket_WhenNotBound_LocalEPChangeToWildcardOnSendTo()
{
using (Socket receiver = CreateUdpSocket())
using (Socket sender = CreateUdpSocket())
{
int receiverPort = receiver.BindToAnonymousPort(Wildcard);
Assert.Null(sender.LocalEndPoint); // null before sendto
var sendToEP = new IPEndPoint(Loopback, receiverPort);
await SendToAsync(sender, new byte[] { 1, 2, 3 }, sendToEP);
Assert.Equal(Wildcard, GetLocalEPAddress(sender)); // changes to wildcard after sendto
byte[] buf = new byte[3];
EndPoint receiveFromEP = new IPEndPoint(Wildcard, 0);
receiver.ReceiveFrom(buf, ref receiveFromEP);
Assert.Equal(new byte[] { 1, 2, 3 }, buf);
Assert.Equal(Loopback, ((IPEndPoint)receiveFromEP).Address); // received from specific address
}
}
[Fact]
public async Task TcpClientSocket_WhenBoundToWildcardAddress_LocalEPChangeToSpecificOnConnnect()
{
using (Socket server = CreateTcpSocket())
using (Socket client = CreateTcpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);
int clientPortAfterBind = client.BindToAnonymousPort(Wildcard);
Assert.Equal(Wildcard, GetLocalEPAddress(client)); // wildcard before connect
server.Listen();
Task<Socket> acceptTask = AcceptAsync(server);
await ConnectAsync(client, new IPEndPoint(Loopback, serverPort));
Assert.Equal(Loopback, GetLocalEPAddress(client)); // changes to specific after connect
Assert.Equal(clientPortAfterBind, GetLocalEPPort(client));
Socket accept = await acceptTask;
Assert.Equal(accept.RemoteEndPoint, client.LocalEndPoint);
}
}
[Fact]
public async Task TcpClientSocket_WhenNotBound_LocalEPChangeToSpecificOnConnnect()
{
using (Socket server = CreateTcpSocket())
using (Socket client = CreateTcpSocket())
{
int serverPort = server.BindToAnonymousPort(Loopback);
server.Listen();
Task<Socket> acceptTask = AcceptAsync(server);
Assert.Null(client.LocalEndPoint); // null before connect
await ConnectAsync(client, new IPEndPoint(Loopback, serverPort));
Assert.Equal(Loopback, GetLocalEPAddress(client)); // changes to specific after connect
Socket accept = await acceptTask;
Assert.Equal(accept.RemoteEndPoint, client.LocalEndPoint);
}
}
[Fact]
public async Task TcpAcceptSocket_WhenServerBoundToWildcardAddress_LocalEPIsSpecific()
{
using (Socket server = CreateTcpSocket())
using (Socket client = CreateTcpSocket())
{
int serverPort = server.BindToAnonymousPort(Wildcard);
Assert.Equal(Wildcard, GetLocalEPAddress(server)); // server -> wildcard before accept
server.Listen();
Task<Socket> acceptTask = AcceptAsync(server);
await ConnectAsync(client, new IPEndPoint(Loopback, serverPort));
Socket accept = await acceptTask;
Assert.Equal(accept.RemoteEndPoint, client.LocalEndPoint);
Assert.Equal(accept.LocalEndPoint, client.RemoteEndPoint);
Assert.Equal(Wildcard, GetLocalEPAddress(server)); // server -> stays as wildcard
Assert.Equal(Loopback, GetLocalEPAddress(accept)); // accept -> specific
Assert.Equal(serverPort, GetLocalEPPort(accept));
}
}
[Fact]
public async Task TcpAcceptSocket_WhenServerBoundToSpecificAddress_LocalEPIsSame()
{
using (Socket server = CreateTcpSocket())
using (Socket client = CreateTcpSocket())
{
int serverPort = server.BindToAnonymousPort(Loopback);
Assert.Equal(Loopback, GetLocalEPAddress(server)); // server -> specific before accept
server.Listen();
Task<Socket> acceptTask = AcceptAsync(server);
await ConnectAsync(client, new IPEndPoint(Loopback, serverPort));
Socket accept = await acceptTask;
Assert.Equal(accept.RemoteEndPoint, client.LocalEndPoint);
Assert.Equal(GetLocalEPAddress(server), GetLocalEPAddress(accept)); // accept -> same address
Assert.Equal(serverPort, GetLocalEPPort(accept));
}
}
[Fact]
public void LocalEndPoint_IsCached()
{
using (Socket socket = CreateTcpSocket())
{
socket.BindToAnonymousPort(Loopback);
EndPoint localEndPointCall1 = socket.LocalEndPoint;
EndPoint localEndPointCall2 = socket.LocalEndPoint;
Assert.Same(localEndPointCall1, localEndPointCall2);
}
}
private Socket CreateUdpSocket()
{
return new Socket(
IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp
);
}
private Socket CreateTcpSocket()
{
return new Socket(
IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp
);
}
private IPAddress GetLocalEPAddress(Socket socket)
{
return ((IPEndPoint)socket.LocalEndPoint).Address;
}
private int GetLocalEPPort(Socket socket)
{
return ((IPEndPoint)socket.LocalEndPoint).Port;
}
}
public abstract class LocalEndPointTestIPv4<T> : LocalEndPointTest<T> where T : SocketHelperBase, new()
{
protected override bool IPv6 => false;
public LocalEndPointTestIPv4(ITestOutputHelper output) : base(output) { }
}
public abstract class LocalEndPointTestIPv6<T> : LocalEndPointTest<T> where T : SocketHelperBase, new()
{
protected override bool IPv6 => true;
public LocalEndPointTestIPv6(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv4", "true")]
public sealed class LocalEndPointTestIPv4Sync : LocalEndPointTestIPv4<SocketHelperArraySync>
{
public LocalEndPointTestIPv4Sync(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv4", "true")]
public sealed class LocalEndPointTestIPv4SyncForceNonBlocking : LocalEndPointTestIPv4<SocketHelperSyncForceNonBlocking>
{
public LocalEndPointTestIPv4SyncForceNonBlocking(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv4", "true")]
public sealed class LocalEndPointTestIPv4Apm : LocalEndPointTestIPv4<SocketHelperApm>
{
public LocalEndPointTestIPv4Apm(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv4", "true")]
public sealed class LocalEndPointTestIPv4Task : LocalEndPointTestIPv4<SocketHelperTask>
{
public LocalEndPointTestIPv4Task(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv4", "true")]
public sealed class LocalEndPointTestIPv4Eap : LocalEndPointTestIPv4<SocketHelperEap>
{
public LocalEndPointTestIPv4Eap(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv6", "true")]
public sealed class LocalEndPointTestIPv6Sync : LocalEndPointTestIPv6<SocketHelperArraySync>
{
public LocalEndPointTestIPv6Sync(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv6", "true")]
public sealed class LocalEndPointTestIPv6SyncForceNonBlocking : LocalEndPointTestIPv6<SocketHelperSyncForceNonBlocking>
{
public LocalEndPointTestIPv6SyncForceNonBlocking(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv6", "true")]
public sealed class LocalEndPointTestIPv6Apm : LocalEndPointTestIPv6<SocketHelperApm>
{
public LocalEndPointTestIPv6Apm(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv6", "true")]
public sealed class LocalEndPointTestIPv6Task : LocalEndPointTestIPv6<SocketHelperTask>
{
public LocalEndPointTestIPv6Task(ITestOutputHelper output) : base(output) { }
}
[Trait("IPv6", "true")]
public sealed class LocalEndPointTestIPv6Eap : LocalEndPointTestIPv6<SocketHelperEap>
{
public LocalEndPointTestIPv6Eap(ITestOutputHelper output) : base(output) { }
}
}