Skip to content

Commit 2af90bd

Browse files
authored
NetworkStream now rejects non-stream sockets (#418)
***NO_CI***
1 parent d35322d commit 2af90bd

4 files changed

Lines changed: 165 additions & 154 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
using System;
8+
using System.IO;
9+
using System.Net;
10+
using System.Net.Sockets;
11+
12+
namespace NFUnitTestSocketTests
13+
{
14+
[TestClass]
15+
public class NetworkStreamTests
16+
{
17+
[Setup]
18+
public void SetupConnectToEthernetTests()
19+
{
20+
// Comment next line to run the tests on a real hardware
21+
Assert.SkipTest("Skipping tests using nanoCLR Win32 in a pipeline");
22+
}
23+
24+
[TestMethod]
25+
public void Ctor_NullSocket_ThrowsArgumentNullException()
26+
{
27+
Assert.ThrowsException(typeof(ArgumentNullException), () =>
28+
{
29+
_ = new NetworkStream(null);
30+
});
31+
32+
Assert.ThrowsException(typeof(ArgumentNullException), () =>
33+
{
34+
_ = new NetworkStream(null, true);
35+
});
36+
}
37+
38+
[TestMethod]
39+
public void Ctor_UnconnectedStreamSocket_ThrowsIOException()
40+
{
41+
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
42+
43+
try
44+
{
45+
Assert.ThrowsException(typeof(IOException), () =>
46+
{
47+
_ = new NetworkStream(socket);
48+
});
49+
50+
Assert.ThrowsException(typeof(IOException), () =>
51+
{
52+
_ = new NetworkStream(socket, false);
53+
});
54+
}
55+
finally
56+
{
57+
socket.Close();
58+
}
59+
}
60+
61+
[TestMethod]
62+
public void Ctor_ConnectedDgramSocket_ThrowsIOException()
63+
{
64+
SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
65+
66+
try
67+
{
68+
testSockets.Startup(0, 0);
69+
70+
// a connected datagram socket has a remote endpoint, so this exercises the socket type check
71+
testSockets.socketClient.Connect(testSockets.epServer);
72+
73+
Assert.ThrowsException(typeof(IOException), () =>
74+
{
75+
_ = new NetworkStream(testSockets.socketClient);
76+
});
77+
78+
Assert.ThrowsException(typeof(IOException), () =>
79+
{
80+
_ = new NetworkStream(testSockets.socketClient, false);
81+
});
82+
}
83+
finally
84+
{
85+
testSockets.TearDown();
86+
}
87+
}
88+
89+
[TestMethod]
90+
public void Ctor_ConnectedStreamSocket_Succeeds()
91+
{
92+
SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
93+
Socket acceptedSocket = null;
94+
95+
try
96+
{
97+
testSockets.Startup(0, 0);
98+
testSockets.socketServer.Listen(1);
99+
testSockets.socketClient.Connect(testSockets.epServer);
100+
acceptedSocket = testSockets.socketServer.Accept();
101+
102+
NetworkStream clientStream = new NetworkStream(testSockets.socketClient);
103+
NetworkStream serverStream = new NetworkStream(acceptedSocket, true);
104+
105+
clientStream.Write(testSockets.bufSend, 0, testSockets.bufSend.Length);
106+
107+
int bytesRead = serverStream.Read(testSockets.bufReceive, 0, testSockets.bufReceive.Length);
108+
109+
testSockets.AssertDataReceived(bytesRead);
110+
111+
clientStream.Dispose();
112+
113+
// server stream owns the accepted socket, so disposing it closes the socket
114+
serverStream.Dispose();
115+
acceptedSocket = null;
116+
}
117+
finally
118+
{
119+
acceptedSocket?.Close();
120+
testSockets.TearDown();
121+
}
122+
}
123+
}
124+
}

‎Tests/SocketTests/SocketTests.nfproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2828
<ItemGroup>
2929
<Compile Include="CompanionClient.cs" />
30+
<Compile Include="NetworkStreamTests.cs" />
3031
<Compile Include="SocketOptionsTests.cs" />
3132
<Compile Include="SocketPair.cs" />
3233
<Compile Include="SocketExceptionsTests.cs" />

‎nanoFramework.System.Net/Security/SslStream.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,15 @@ public class SslStream : NetworkStream
4747
/// Initializes a new instance of the SslStream class using the specified Socket.
4848
/// </summary>
4949
/// <param name="socket">A valid socket that currently has a TCP connection.</param>
50+
/// <exception cref="ArgumentNullException"><paramref name="socket"/> is <see langword="null"/>.</exception>
51+
/// <exception cref="IOException"><paramref name="socket"/> is not connected. -or- The <see cref="Socket.SocketType"/> property of <paramref name="socket"/> is not <see cref="SocketType.Stream"/>.</exception>
5052
/// <remarks>
5153
/// The SslStream maintains the lifetime of the socket. When the SslStream object is disposed,
5254
/// the underlying TCP socket will be closed.
5355
/// </remarks>
5456
public SslStream(Socket socket)
5557
: base(socket, false)
5658
{
57-
if (SocketType.Stream != (SocketType)_socketType)
58-
{
59-
throw new NotSupportedException();
60-
}
61-
6259
_sslContext = -1;
6360
_isServer = false;
6461

0 commit comments

Comments
 (0)