public MFTestResults SocketsEnums8_SocketFlags_Peek() { /// <summary> /// 1. Sends TCP data using the Peek flag /// 2. Verifies that the correct exception is thrown /// Further testing would require harness alteration /// </summary> /// bool isAnyCatch = false; try { try { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Receive(testSockets.bufReceive, SocketFlags.Peek); Log.Comment("Checking Peek data"); testSockets.AssertDataReceived(cBytes); int cBytesAgain = sock.Receive(testSockets.bufReceive); if(cBytesAgain < cBytes) throw new Exception( "Peek returns more bytes than the successive read" ); } testSockets.AssertDataReceived(cBytes); testSockets.TearDown(); testSockets = null; } catch (SocketException) { isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isAnyCatch ? MFTestResults.Fail : MFTestResults.Pass); }
public MFTestResults SocketExceptionTest20_OperationNotSupported() { /// <summary> /// 1. Causes a OperationNotSupported error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { sock.Receive(testSockets.bufReceive, SocketFlags.DontRoute); } isCorrectCatch = true; } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } if (!isAnyCatch) { Log.Comment("No exception caught"); } testSockets.TearDown(); testSockets = null; return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest11_AccessDenied() { /// <summary> /// 1. Causes a AccessDenied error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = serverPort; testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false); testSockets.Startup(clientPort, serverPort); IPEndPoint epBroadcast = new IPEndPoint(SocketTools.DottedDecimalToIp((byte)255, (byte)255, (byte)255, (byte)255), tempPort); EndPoint serverEndPoint = epBroadcast.Create(epBroadcast.Serialize()); testSockets.socketClient.SendTo(testSockets.bufSend, serverEndPoint); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest14_AddressNotAvailable() { /// <summary> /// 1. Causes a AddressNotAvailable error /// Due to loopback this method causes an InvalidArgument /// SocketException erroneously /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = clientPort; testSockets.Startup(clientPort, serverPort); testSockets.socketClient.Bind(new IPEndPoint(new IPAddress(SocketTools.DottedDecimalToIp((byte)192, (byte)168, (byte)192, (byte)168)), tempPort)); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest6_IsConnected() { /// <summary> /// 1. Causes a IsConnected error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); testSockets.socketClient.Connect(testSockets.epServer); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest12_NotConnected() { /// <summary> /// 1. Causes a NotConnected error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); Socket socketTemp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketTemp.Bind(testSockets.socketServer.RemoteEndPoint); socketTemp.Send(new byte[2]); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest13_InvalidArgument() { /// <summary> /// 1. Causes a InvalidArgument error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = clientPort; testSockets.Startup(clientPort, serverPort); testSockets.socketServer.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.Broadcast, true); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest18_Fault() { /// <summary> /// 1. Causes a Fault error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new byte[] { (byte)0 }); testSockets.Startup(0, 0); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest6_TCPRecieve() { /// <summary> /// 1. Starts a server socket listening for TCP /// 2. Starts a client socket connected to the server /// 3. Verifies that data can be correctly sent and recieved using /// each prototype of Recieve method /// 4. Verifies that exceptions are correctly thrown for Recieve calls that have /// bad Offset or Size parameters /// </summary> /// bool testResult = true; SocketPair testSockets = null; try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, testSockets.bufSend.Length, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array, Int, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } bool subResult = false; try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, testSockets.bufSend.Length + 2, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (System.IndexOutOfRangeException) { Log.Comment("IndexOutOfRangeException Successfully Caught"); subResult = true; } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array, Int, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; subResult = true; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } if (!subResult) Log.Comment("Erroneously succeeded with bad Int-Size parameter"); testResult &= subResult; } try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, 0, testSockets.bufSend.Length, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array, Int-Offset," + "Int-Size, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } subResult = false; try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, 2, testSockets.bufSend.Length, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (System.IndexOutOfRangeException) { Log.Comment("IndexOutOfRangeException Successfully Caught"); subResult = true; } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array," + " Int-Offset, Int-Size, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; subResult = true; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } if (!subResult) Log.Comment("Erroneously succeeded with bad Int-Offset parameter"); testResult &= subResult; } try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend, 1, testSockets.bufSend.Length - 1, SocketFlags.None); int throwAway; using (Socket sock = testSockets.socketServer.Accept()) { throwAway = sock.Available; cBytes = sock.Receive(testSockets.bufReceive, 1, 2, SocketFlags.None); throwAway = testSockets.socketClient.Send(testSockets.bufSend, 0, 1, SocketFlags.None); throwAway = sock.Available; cBytes += sock.Receive(testSockets.bufReceive, 0, 1, SocketFlags.None); } testSockets.AssertDataReceived(cBytes); } catch (Exception e) { Log.Comment("Caught exception in Recieve with Byte Array, " + "Int-Offset, Int-Size, SocketFlags: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest5_BasicRAW() { /// <summary> /// 1. Starts a server socket listening for Raw /// 2. Starts a client socket connected to the server /// 3. Verifies that data can be correctly sent and recieved /// </summary> /// bool testResult = true; SocketPair testSockets = null; try { testSockets = new SocketPair(ProtocolType.Raw, SocketType.Raw); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); if (cBytes != testSockets.bufSend.Length) throw new Exception("Send failed, wrong length"); using (Socket sock = testSockets.socketServer.Accept()) { if (sock.LocalEndPoint == null) throw new Exception("LocalEndPoint is null"); //Fix? if (testSockets.socketServer.LocalEndPoint.ToString() != sock.LocalEndPoint.ToString()) throw new Exception("LocalEndPoint is incorrect"); if (sock.RemoteEndPoint == null) throw new Exception("RemoteEndPoint is null"); if (testSockets.socketClient.LocalEndPoint.ToString() != sock.RemoteEndPoint.ToString()) throw new Exception("RemoteEndPoint is incorrect"); cBytes = sock.Available; if (cBytes != testSockets.bufSend.Length) throw new Exception("Send failed, wrong length"); cBytes = sock.Receive(testSockets.bufReceive); } testSockets.AssertDataReceived(cBytes); } catch (SocketException e) { Log.Comment("Caught exception: " + e.Message); Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest3_SocketOption() { /// <summary> /// 1. Creates a pair of UDP sockets /// 3. Verifies that SocketOptions can be correctly set and got /// </summary> /// bool testResult = true; SocketPair testSockets = null; try { testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); int iSet = 0x1024; int iGet; testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, iSet); iGet = (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer); if (iSet != iGet) throw new Exception("Socket option flag ints differ"); testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer, iSet); iGet = (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer); if (iSet != iGet) throw new Exception("Socket option flag ints differ"); bool fSet = true; bool fGet; try { testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, fSet); fGet = ((int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReuseAddress) == 0x1); if (fSet != fGet) throw new Exception("Socket option flag bools differ"); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) throw; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest2_BasicUDP() { /// <summary> /// 1. Starts a server socket listening for UDP /// 2. Starts a client socket sending UDP packets /// 3. Verifies that data can be correctly sent and recieved /// </summary> /// bool testResult = true; SocketPair testSockets = null; try { testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); int cBytes = testSockets.socketClient.SendTo( testSockets.bufSend, testSockets.epServer); if (cBytes != testSockets.bufSend.Length) throw new Exception("Send failed, wrong length"); EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0); cBytes = testSockets.socketServer.ReceiveFrom(testSockets.bufReceive, ref epFrom); if (testSockets.epClient.Address.ToString() != ((IPEndPoint)epFrom).Address.ToString()) throw new Exception("Bad address"); } catch (SocketException e) { if (e.ErrorCode == 10047) { Log.Comment("Address family not supported by protocol family."); Log.Comment("This exception is thrown by the device driver that doesnt' implement Loopback for UDP"); } else { testResult = false; Log.Comment("Fail for any other error codes that we don't know about"); } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest1_TCP_NoDelay() { ///<summary> ///1. Starts a server socket listening for TCP ///2. Starts a client socket connected to the server ///3. Verifies that data can be correctly sent and recieved ///4. Verifies NoDelay is functional ///</summary> ///skip this test for the emulator since setting socket options is not supported. if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3) return MFTestResults.Skip; MFTestResults testResult = MFTestResults.Pass; bool noDelay = false; int bytesReceivedNoDelay = 0; int bytesReceivedDelay = 0; do { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, noDelay); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); Log.Comment("send lots of small packets."); int bytesSent = 0; for (int i = 0; i < 140; ++i) { bytesSent += testSockets.socketClient.Send(testSockets.bufSend); } using (Socket sock = testSockets.socketServer.Accept()) { byte[] recBytes = new byte[486*testSockets.bufSend.Length]; int bytesReceived = sock.Available; bytesReceived = sock.Receive(recBytes); if (noDelay) bytesReceivedNoDelay = bytesReceived; else bytesReceivedDelay = bytesReceived; Log.Comment("BytesSent: " + bytesSent + " BytesReceived: " + bytesReceived); } } catch (SocketException e) { Log.Comment("Caught exception: " + e.Message); Log.Comment("ErrorCode: " + e.ErrorCode.ToString()); testResult = MFTestResults.Fail; } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); testResult = MFTestResults.Fail; } finally { testSockets.TearDown(); } noDelay = !noDelay; } while (noDelay); if (bytesReceivedNoDelay < bytesReceivedDelay) { testResult = MFTestResults.Fail; Log.Comment("We've received more bytes with nodelay enabled."); } return testResult; }
public MFTestResults SocketExceptionTest19_ProtocolOption() { /// <summary> /// 1. Causes a ProtocolOption error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); testSockets.socketClient.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.Linger); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } if (!isAnyCatch) { Log.Comment("No exception caught"); } testSockets.TearDown(); testSockets = null; return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest8_TCPRecieveFrom() { /// <summary> /// 1. Starts a server socket listening for TCP /// 2. Starts a client socket connected to the server /// 3. Verifies that data can be correctly sent and Recieved using /// each prototype of RecieveFrom method /// 4. Verifies that exceptions are correctly thrown for RecieveFrom calls that have /// bad Offset or Size parameters /// </summary> /// MFTestResults testResult = MFTestResults.Pass; SocketPair testSockets = null; try { testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); EndPoint remoteEndPoint = testSockets.epClient.Create(testSockets.epClient.Serialize()); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.ReceiveFrom(testSockets.bufReceive, 0, testSockets.bufSend.Length, SocketFlags.None, ref remoteEndPoint); } testSockets.AssertDataReceived(cBytes); testSockets.TearDown(); } catch (Exception e) { Log.Comment("Caught exception in RecieveFrom with Byte Array," + " Int, SocketFlags: " + e.Message); testResult = MFTestResults.Fail; } finally { if (testSockets != null) { testSockets.TearDown(); testSockets = null; } } return testResult; }
public MFTestResults SocketExceptionTest12_NotConnected() { /// <summary> /// 1. Causes a NotConnected error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); Socket socketTemp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketTemp.Bind(testSockets.socketServer.RemoteEndPoint); socketTemp.Send(new byte[2]); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest13_InvalidArgument() { /// <summary> /// 1. Causes a InvalidArgument error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = clientPort; testSockets.Startup(clientPort, serverPort); testSockets.socketServer.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.Broadcast, true); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketsEnums15_SON_SendBuffer() { /// <summary> /// 1. Sends a message of size SendBuffer /// 2. Verifies that it transferred correctly /// 1. Sends a message of size SendBuffer+1 /// 2. Verifies that the correct exception is thrown /// </summary> /// bool testResult = true; byte[] buf = new byte[4]; int number; try { int sendBufferValue1 = 1024; int sendBufferValue2 = 512; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sendBufferValue1); testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer, buf); number = (int)buf[0] + (int)(buf[1]<<8) + (int)(buf[2]<<16) + (int)(buf[3]<<24); if(number != sendBufferValue1) throw new System.Exception("ReceiveBuffer option was not set correctly"); testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sendBufferValue2); testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.SendBuffer, buf); number = (int)buf[0] + (int)(buf[1]<<8) + (int)(buf[2]<<16) + (int)(buf[3]<<24); if(number != sendBufferValue2) throw new System.Exception("ReceiveBuffer option was not set correctly"); Log.Comment("ReceiveBuffer " + number.ToString() ); testSockets.Startup(0, 0); testSockets.bufSend = new byte[number]; testSockets.bufReceive = new byte[number]; testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); if (cBytes != testSockets.bufSend.Length) throw new System.Exception("Send failed, wrong length"); using (Socket sock = testSockets.socketServer.Accept()) { if (sock.LocalEndPoint == null) throw new System.Exception("LocalEndPoint is null"); if (testSockets.socketServer.LocalEndPoint.ToString() != sock.LocalEndPoint.ToString()) throw new System.Exception("LocalEndPoint is incorrect"); if (sock.RemoteEndPoint == null) throw new System.Exception("RemoteEndPoint is null"); if (testSockets.socketClient.LocalEndPoint.ToString() != sock.RemoteEndPoint.ToString()) throw new System.Exception("RemoteEndPoint is incorrect"); //wait a second to ensure the socket is available System.Threading.Thread.Sleep(1000); cBytes = sock.Available; if (cBytes != testSockets.bufSend.Length) throw new System.Exception("Send failed, wrong length"); cBytes = sock.Receive(testSockets.bufReceive); } testSockets.AssertDataReceived(cBytes); testSockets.TearDown(); testSockets = null; } catch (System.Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketsEnums12_SON_AcceptConnection() { /// <summary> /// 1. Starts a server socket listening for TCP /// 2. Verifies that AcceptConnection is correct before and after /// </summary> /// bool testResult = true; try { byte[] buf = new byte[4]; int number; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); testSockets.Startup(0, 0); testSockets.socketServer.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.AcceptConnection, buf ); number = (int)buf[0] + (int)(buf[1]<<8) + (int)(buf[2]<<16) + (int)(buf[3]<<24); testResult &= (number == 0); testSockets.socketServer.Listen(1); testSockets.socketServer.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.AcceptConnection, buf ); number = (int)buf[0] + (int)(buf[1]<<8) + (int)(buf[2]<<16) + (int)(buf[3]<<24); testResult &= (number != 0); testSockets.TearDown(); testSockets = null; } catch (System.Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketsEnums9_SocketOptionLevel() { /// <summary> /// 1. Tests that Set and GetSocketOption can be called using each member of /// SocketOptionLevel /// /// TODO: Fix Docs for (...Byte[]) /// </summary> /// bool isAnyCatch = false; try { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, false); if (0 != (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Tcp, SocketOptionName.NoDelay)) throw new System.Exception("SocketOptionLevel.Tcp failed"); testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, true); if (0 == (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger)) throw new System.Exception("SocketOptionLevel.Socket failed"); testSockets.socketClient.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DontFragment, true); if (0 == (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.IP, SocketOptionName.DontFragment)) throw new System.Exception("SocketOptionLevel.IP failed"); if ((int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger) != 1) throw new System.Exception( "GetSocketOption with Level and Name got wrong data"); byte[] lingerBytes = new byte[] { 0, 0, 0, 0 }; testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger, lingerBytes); if (!SocketTools.ArrayEquals(lingerBytes, new byte[] { 1, 0, 0, 0 })) throw new System.Exception("GetSocketOption with Level," + " Name and ByteArray got wrong data"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); testSockets.socketClient.Send(testSockets.bufSend); int cBytes = 0; using (Socket sock = testSockets.socketServer.Accept()) { Thread.Sleep(500); cBytes = sock.Available; cBytes = sock.Receive(testSockets.bufReceive); } testSockets.AssertDataReceived(cBytes); } catch (SocketException e) { isAnyCatch = true; Log.Comment("System.Exception caught: " + e.Message + " " + e.ErrorCode); } finally { testSockets.TearDown(); } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("System.Exception caught: " + e.Message); } return (!isAnyCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest20_OperationNotSupported() { /// <summary> /// 1. Causes a OperationNotSupported error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { sock.Receive(testSockets.bufReceive, SocketFlags.DontRoute); } isCorrectCatch = true; } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } if (!isAnyCatch) { Log.Comment("No exception caught"); } testSockets.TearDown(); testSockets = null; return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest11_AccessDenied() { /// <summary> /// 1. Causes a AccessDenied error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = serverPort; testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false); testSockets.Startup(clientPort, serverPort); IPEndPoint epBroadcast = new IPEndPoint(SocketTools.DottedDecimalToIp((byte)255, (byte)255, (byte)255, (byte)255), tempPort); EndPoint serverEndPoint = epBroadcast.Create(epBroadcast.Serialize()); testSockets.socketClient.SendTo(testSockets.bufSend, serverEndPoint); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest13_DPWSOptions() { /// <summary> /// 1. Starts a server socket listening for UDP /// 2. Starts a client socket sending UDP packets /// 3. Verifies that data can be correctly sent and recieved /// </summary> /// //don't run on the emulator since this isn't supported if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3) return MFTestResults.Skip; bool testResult = true; try { SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram); NetworkInterface[] inter = NetworkInterface.GetAllNetworkInterfaces(); IPAddress localAddress = SocketTools.ParseAddress(inter[0].IPAddress); try { byte[] local = localAddress.GetAddressBytes(); byte[] multicastOpt = new byte[] { 224, 100, 1, 1, local[0], local[1], local[2], local[3]}; testSockets.socketClient.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOpt); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { byte [] bytes = localAddress.GetAddressBytes(); // given the address change it to what SetSocketOption needs testSockets.socketClient.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.MulticastInterface, bytes); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DontFragment, true); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, false); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.ExclusiveAddressUse, true); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { int error = (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Error); Log.Comment("Current error: " + error.ToString()); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { SocketType type = (SocketType)( (int)testSockets.socketClient.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type)); Log.Comment("Current error: " + type.ToString()); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { int error = (int)testSockets.socketClient.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error); Log.Comment("Current error: " + error.ToString()); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { SocketType type = (SocketType)( (int)testSockets.socketClient.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type)); Log.Comment("Current error: " + type.ToString()); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { byte[] local = localAddress.GetAddressBytes(); byte[] multicastOpt = new byte[] { 224, 100, 1, 1, local[0], local[1], local[2], local[3]}; testSockets.socketClient.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, multicastOpt); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } try { testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 0x1024); } catch (SocketException se) { if (se.ErrorCode != (int)SocketError.ProtocolOption) { Log.Comment("Caught exception: " + se.Message); if (se.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + se.ErrorCode); testResult = false; } } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); int cBytes = testSockets.socketClient.SendTo(testSockets.bufSend, testSockets.epServer); if (cBytes != testSockets.bufSend.Length) throw new Exception("Send failed, wrong length"); EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0); cBytes = testSockets.socketServer.ReceiveFrom( testSockets.bufReceive, ref epFrom); if (testSockets.epClient.Address.ToString() != ((IPEndPoint)epFrom).Address.ToString()) throw new Exception("Bad address"); testSockets.TearDown(); testSockets = null; } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest10_TCPPoll() { /// <summary> /// 1. Starts a server socket listening for TCP /// 2. Starts a client socket connected to the server /// 3. Transfers some data /// 4. Verifies that Poll returns correct results after each individual /// function is called. /// </summary> /// bool testResult = true; try { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); Log.Comment("Listen called"); testSockets.socketClient.Connect(testSockets.epServer); Log.Comment("Connect called"); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); Log.Comment("Send called"); testResult = (testSockets.socketClient.Poll(1000, SelectMode.SelectWrite) == true); testResult = (testSockets.socketServer.Poll(1000, SelectMode.SelectRead) == true); using (Socket sock = testSockets.socketServer.Accept()) { Log.Comment("Accept called"); testResult = (testSockets.socketClient.Poll(1000, SelectMode.SelectWrite) == true); cBytes = sock.Available; Log.Comment("Available bytes assigned"); testResult = (testSockets.socketClient.Poll(1000, SelectMode.SelectWrite) == true); cBytes = sock.Receive(testSockets.bufReceive); Log.Comment("Recieve called"); testResult = (testSockets.socketClient.Poll(1000, SelectMode.SelectWrite) == true); } testSockets.AssertDataReceived(cBytes); testSockets.TearDown(); testSockets = null; } catch (Exception e) { Log.Comment("Caught exception: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); testResult = false; } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest14_AddressNotAvailable() { /// <summary> /// 1. Causes a AddressNotAvailable error /// Due to loopback this method causes an InvalidArgument /// SocketException erroneously /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { int clientPort = SocketTools.nextPort; int serverPort = SocketTools.nextPort; int tempPort = clientPort; testSockets.Startup(clientPort, serverPort); testSockets.socketClient.Bind(new IPEndPoint(new IPAddress(SocketTools.DottedDecimalToIp((byte)192, (byte)168, (byte)192, (byte)168)), tempPort)); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketTest11_SetSocketOptionBasic() { /// <summary> /// 1. Call SetSocketOption with each of its signatures /// </summary> /// bool isAnyCatch = false; try { SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); // check the bool testSockets.socketClient.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger, true); bool linger = (0 != (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger)); if (!linger) throw new Exception("Linger was not enabled"); int lingerTime = 20; testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerTime); int lingerResult = (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger); if (lingerResult != lingerTime) throw new Exception("Linger time was not set correctly"); // check the bool again for false testSockets.socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, false); bool notLinger = (0 == (int)testSockets.socketClient.GetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Linger)); if (!notLinger) throw new Exception("Linger was not disabled"); // proceed to bind Log.Comment("Testing with port 0"); testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); int cBytes = testSockets.socketClient.Send(testSockets.bufSend); using (Socket sock = testSockets.socketServer.Accept()) { cBytes = sock.Receive(testSockets.bufReceive); testSockets.AssertDataReceived(cBytes); } testSockets.TearDown(); testSockets = null; } catch (Exception e) { isAnyCatch = true; Log.Comment("Exception caught: " + e.Message); if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException")) Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode); } return (!isAnyCatch ? MFTestResults.Pass : MFTestResults.Fail); }
public MFTestResults SocketExceptionTest6_IsConnected() { /// <summary> /// 1. Causes a IsConnected error /// </summary> /// bool isCorrectCatch = false; bool isAnyCatch = false; SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream); try { try { testSockets.Startup(0, 0); testSockets.socketServer.Listen(1); testSockets.socketClient.Connect(testSockets.epServer); testSockets.socketClient.Connect(testSockets.epServer); } catch (SocketException) { isCorrectCatch = true; isAnyCatch = true; } } catch (System.Exception e) { isAnyCatch = true; Log.Comment("Incorrect exception caught: " + e.Message); } finally { testSockets.TearDown(); } if (!isAnyCatch) { Log.Comment("No exception caught"); } return (isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail); }