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 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 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 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 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 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 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); }