|
| 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 | +} |
0 commit comments