Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions Tests/SocketTests/NetworkStreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace NFUnitTestSocketTests
{
[TestClass]
public class NetworkStreamTests
{
[Setup]
public void SetupConnectToEthernetTests()
{
// Comment next line to run the tests on a real hardware
Assert.SkipTest("Skipping tests using nanoCLR Win32 in a pipeline");
}

[TestMethod]
public void Ctor_NullSocket_ThrowsArgumentNullException()
{
Assert.ThrowsException(typeof(ArgumentNullException), () =>
{
_ = new NetworkStream(null);
});

Assert.ThrowsException(typeof(ArgumentNullException), () =>
{
_ = new NetworkStream(null, true);
});
}

[TestMethod]
public void Ctor_UnconnectedStreamSocket_ThrowsIOException()
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try
{
Assert.ThrowsException(typeof(IOException), () =>
{
_ = new NetworkStream(socket);
});

Assert.ThrowsException(typeof(IOException), () =>
{
_ = new NetworkStream(socket, false);
});
}
finally
{
socket.Close();
}
}

[TestMethod]
public void Ctor_ConnectedDgramSocket_ThrowsIOException()
{
SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);

try
{
testSockets.Startup(0, 0);

// a connected datagram socket has a remote endpoint, so this exercises the socket type check
testSockets.socketClient.Connect(testSockets.epServer);

Assert.ThrowsException(typeof(IOException), () =>
{
_ = new NetworkStream(testSockets.socketClient);
});

Assert.ThrowsException(typeof(IOException), () =>
{
_ = new NetworkStream(testSockets.socketClient, false);
});
}
finally
{
testSockets.TearDown();
}
}

[TestMethod]
public void Ctor_ConnectedStreamSocket_Succeeds()
{
SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
Socket acceptedSocket = null;

try
{
testSockets.Startup(0, 0);
testSockets.socketServer.Listen(1);
testSockets.socketClient.Connect(testSockets.epServer);
acceptedSocket = testSockets.socketServer.Accept();

NetworkStream clientStream = new NetworkStream(testSockets.socketClient);
NetworkStream serverStream = new NetworkStream(acceptedSocket, true);

clientStream.Write(testSockets.bufSend, 0, testSockets.bufSend.Length);

int bytesRead = serverStream.Read(testSockets.bufReceive, 0, testSockets.bufReceive.Length);

testSockets.AssertDataReceived(bytesRead);

clientStream.Dispose();

// server stream owns the accepted socket, so disposing it closes the socket
serverStream.Dispose();
acceptedSocket = null;
}
finally
{
acceptedSocket?.Close();
testSockets.TearDown();
}
}
}
}
1 change: 1 addition & 0 deletions Tests/SocketTests/SocketTests.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="CompanionClient.cs" />
<Compile Include="NetworkStreamTests.cs" />
<Compile Include="SocketOptionsTests.cs" />
<Compile Include="SocketPair.cs" />
<Compile Include="SocketExceptionsTests.cs" />
Expand Down
7 changes: 2 additions & 5 deletions nanoFramework.System.Net/Security/SslStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ public class SslStream : NetworkStream
/// Initializes a new instance of the SslStream class using the specified Socket.
/// </summary>
/// <param name="socket">A valid socket that currently has a TCP connection.</param>
/// <exception cref="ArgumentNullException"><paramref name="socket"/> is <see langword="null"/>.</exception>
/// <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>
/// <remarks>
/// The SslStream maintains the lifetime of the socket. When the SslStream object is disposed,
/// the underlying TCP socket will be closed.
/// </remarks>
public SslStream(Socket socket)
: base(socket, false)
{
if (SocketType.Stream != (SocketType)_socketType)
{
throw new NotSupportedException();
}

_sslContext = -1;
_isServer = false;

Expand Down
Loading