forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSSupport.cs
More file actions
211 lines (175 loc) · 9.89 KB
/
OSSupport.cs
File metadata and controls
211 lines (175 loc) · 9.89 KB
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
// 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;
using Xunit;
namespace System.Net.Sockets.Tests
{
public class OSSupportTest
{
[Fact]
public void SupportsIPv4_MatchesOSSupportsIPv4()
{
#pragma warning disable 0618 // Supports* are obsoleted
Assert.Equal(Socket.SupportsIPv4, Socket.OSSupportsIPv4);
#pragma warning restore
}
[Fact]
public void SupportsIPv6_MatchesOSSupportsIPv6()
{
#pragma warning disable 0618 // Supports* are obsoleted
Assert.Equal(Socket.SupportsIPv6, Socket.OSSupportsIPv6);
#pragma warning restore
}
[Fact]
public void IOControl_FIONREAD_Success()
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, null));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, new byte[0]));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.DataToRead, null, new byte[sizeof(int) - 1]));
byte[] fionreadResult = new byte[sizeof(int)];
Assert.Equal(4, client.IOControl(IOControlCode.DataToRead, null, fionreadResult));
Assert.Equal(client.Available, BitConverter.ToInt32(fionreadResult, 0));
Assert.Equal(0, BitConverter.ToInt32(fionreadResult, 0));
Assert.Equal(4, client.IOControl((int)IOControlCode.DataToRead, null, fionreadResult));
Assert.Equal(client.Available, BitConverter.ToInt32(fionreadResult, 0));
Assert.Equal(0, BitConverter.ToInt32(fionreadResult, 0));
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
client.Connect(listener.LocalEndPoint);
using (Socket server = listener.Accept())
{
server.Send(new byte[] { 42 });
Assert.True(SpinWait.SpinUntil(() => client.Available != 0, 10_000));
Assert.Equal(4, client.IOControl(IOControlCode.DataToRead, null, fionreadResult));
Assert.Equal(client.Available, BitConverter.ToInt32(fionreadResult, 0));
Assert.Equal(1, BitConverter.ToInt32(fionreadResult, 0));
}
}
}
}
[PlatformSpecific(TestPlatforms.AnyUnix)]
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50568", TestPlatforms.Android)]
public void IOControl_SIOCATMARK_Unix_Success()
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, null));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, new byte[0]));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, new byte[sizeof(int) - 1]));
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
client.Connect(listener.LocalEndPoint);
using (Socket server = listener.Accept())
{
byte[] siocatmarkResult = new byte[sizeof(int)];
// Socket connected but no data sent.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(0, BitConverter.ToInt32(siocatmarkResult, 0));
server.Send(new byte[] { 42 }, SocketFlags.None);
server.Send(new byte[] { 43 }, SocketFlags.OutOfBand);
// OOB data recieved, but read pointer not at mark.
Assert.True(SpinWait.SpinUntil(() =>
{
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
return BitConverter.ToInt32(siocatmarkResult, 0) == 0;
}, 10_000));
var received = new byte[1];
Assert.Equal(1, client.Receive(received));
Assert.Equal(42, received[0]);
// OOB data recieved, read pointer at mark.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(1, BitConverter.ToInt32(siocatmarkResult, 0));
Assert.Equal(1, client.Receive(received, SocketFlags.OutOfBand));
Assert.Equal(43, received[0]);
// OOB data read, read pointer at mark.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(PlatformDetection.IsOSXLike ? 0 : 1, BitConverter.ToInt32(siocatmarkResult, 0));
}
}
}
}
[PlatformSpecific(TestPlatforms.Windows)]
[Fact]
public void IOControl_SIOCATMARK_Windows_Success()
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, null));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, new byte[0]));
Assert.Throws<SocketException>(() => client.IOControl(IOControlCode.OobDataRead, null, new byte[sizeof(int) - 1]));
using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
client.Connect(listener.LocalEndPoint);
using (Socket server = listener.Accept())
{
byte[] siocatmarkResult = new byte[sizeof(int)];
// Socket connected but no data sent.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(1, BitConverter.ToInt32(siocatmarkResult, 0));
server.Send(new byte[] { 42 }, SocketFlags.None);
server.Send(new byte[] { 43 }, SocketFlags.OutOfBand);
// OOB data recieved, but read pointer not at mark
Assert.True(SpinWait.SpinUntil(() =>
{
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
return BitConverter.ToInt32(siocatmarkResult, 0) == 0;
}, 10_000));
var received = new byte[1];
Assert.Equal(1, client.Receive(received));
Assert.Equal(42, received[0]);
// OOB data recieved, read pointer at mark.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(0, BitConverter.ToInt32(siocatmarkResult, 0));
Assert.Equal(1, client.Receive(received, SocketFlags.OutOfBand));
Assert.Equal(43, received[0]);
// OOB data read, read pointer at mark.
Assert.Equal(4, client.IOControl(IOControlCode.OobDataRead, null, siocatmarkResult));
Assert.Equal(1, BitConverter.ToInt32(siocatmarkResult, 0));
}
}
}
}
[Fact]
public void IOControl_FIONBIO_Throws()
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
Assert.Throws<InvalidOperationException>(() => client.IOControl(unchecked((int)IOControlCode.NonBlockingIO), null, null));
Assert.Throws<InvalidOperationException>(() => client.IOControl(IOControlCode.NonBlockingIO, null, null));
}
}
[PlatformSpecific(TestPlatforms.AnyUnix)]
[Fact]
public void IOControl_UnknownValues_Unix_Throws()
{
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
foreach (IOControlCode code in Enum.GetValues(typeof(IOControlCode)))
{
switch (code)
{
case IOControlCode.NonBlockingIO:
case IOControlCode.DataToRead:
case IOControlCode.OobDataRead:
// These three codes are currently enabled on Unix.
break;
default:
// The rest should throw PNSE.
Assert.Throws<PlatformNotSupportedException>(() => client.IOControl((int)code, null, null));
Assert.Throws<PlatformNotSupportedException>(() => client.IOControl(code, null, null));
break;
}
}
}
}
}
}