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