Skip to content

Commit df6a8b5

Browse files
authored
Fix NetworkStream write core (#419)
1 parent d8aa5d6 commit df6a8b5

3 files changed

Lines changed: 131 additions & 1 deletion

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using nanoFramework.TestFramework;
5+
using System;
6+
using System.Net;
7+
using System.Net.Sockets;
8+
9+
namespace NFUnitTestSocketTests
10+
{
11+
[TestClass]
12+
public class NetworkStreamTests
13+
{
14+
private const int ServerPort = 7010;
15+
16+
[Setup]
17+
public void SetupNetworkStreamTests()
18+
{
19+
// Comment next line to run the tests on a real hardware
20+
Assert.SkipTest("Skipping tests using nanoCLR Win32 in a pipeline");
21+
}
22+
23+
[TestMethod]
24+
public void NetworkStream_Write_ByteArray_SendsData()
25+
{
26+
byte[] sent = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
27+
28+
RunLoopbackTest((stream, peer) =>
29+
{
30+
stream.Write(sent, 0, sent.Length);
31+
32+
AssertReceived(peer, sent);
33+
});
34+
}
35+
36+
[TestMethod]
37+
public void NetworkStream_Write_ByteArrayWithOffset_SendsData()
38+
{
39+
byte[] buffer = new byte[] { 0xFF, 0x11, 0x22, 0x33, 0xFF };
40+
41+
RunLoopbackTest((stream, peer) =>
42+
{
43+
stream.Write(buffer, 1, 3);
44+
45+
AssertReceived(peer, new byte[] { 0x11, 0x22, 0x33 });
46+
});
47+
}
48+
49+
[TestMethod]
50+
public void NetworkStream_Write_Span_SendsData()
51+
{
52+
byte[] sent = new byte[] { 0xAB, 0xCD, 0xEF };
53+
54+
RunLoopbackTest((stream, peer) =>
55+
{
56+
stream.Write(new ReadOnlySpan<byte>(sent));
57+
58+
AssertReceived(peer, sent);
59+
});
60+
}
61+
62+
[TestMethod]
63+
public void NetworkStream_Write_ZeroLength_DoesNotThrow()
64+
{
65+
RunLoopbackTest((stream, peer) =>
66+
{
67+
stream.Write(new byte[0], 0, 0);
68+
});
69+
}
70+
71+
private delegate void LoopbackTestAction(NetworkStream stream, Socket peer);
72+
73+
private static void RunLoopbackTest(LoopbackTestAction test)
74+
{
75+
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
76+
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
77+
Socket peer = null;
78+
NetworkStream stream = null;
79+
80+
try
81+
{
82+
listener.Bind(new IPEndPoint(IPAddress.Loopback, ServerPort));
83+
listener.Listen(1);
84+
85+
client.Connect(new IPEndPoint(IPAddress.Loopback, ServerPort));
86+
peer = listener.Accept();
87+
88+
stream = new NetworkStream(client, true);
89+
90+
test(stream, peer);
91+
}
92+
finally
93+
{
94+
if (stream != null)
95+
{
96+
// stream owns the client socket
97+
stream.Close();
98+
}
99+
else
100+
{
101+
client.Close();
102+
}
103+
104+
peer?.Close();
105+
listener.Close();
106+
}
107+
}
108+
109+
private static void AssertReceived(Socket peer, byte[] expected)
110+
{
111+
byte[] received = new byte[expected.Length];
112+
int totalRead = 0;
113+
114+
while (totalRead < expected.Length)
115+
{
116+
int read = peer.Receive(received, totalRead, expected.Length - totalRead, SocketFlags.None);
117+
118+
Assert.IsTrue(read > 0, "Connection closed before all data was received");
119+
120+
totalRead += read;
121+
}
122+
123+
for (int i = 0; i < expected.Length; i++)
124+
{
125+
Assert.AreEqual(expected[i], received[i], $"Data mismatch at index {i}");
126+
}
127+
}
128+
}
129+
}

‎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/Sockets/NetworkStream.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ private void WriteCore(byte[] buffer, int offset, int count)
487487
throw new NotSupportedException();
488488
}
489489

490-
if (0 != count)
490+
if (bytesSent != count)
491491
{
492492
throw new IOException();
493493
}

0 commit comments

Comments
 (0)