예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        public MFTestResults SocketExceptionTest17_SocketError()
        {
            /// <summary>
            /// 1. Causes a SocketError error
            /// This currently succeeds but will need re-writing if 17577 is addressed
            /// </summary>
            ///
            bool isCorrectCatch = false;

            try
            {
                SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Stream);
            }
            catch (SocketException)
            {
                isCorrectCatch = true;
            }

            return(isCorrectCatch ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
 public MFTestResults SocketsEnums11_SocketOptionName_MulticastTimeToLive_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing MulticastTimeToLive with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
         SocketOptionName.MulticastTimeToLive, intData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #13
0
        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;
        }
예제 #14
0
        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);
        }
예제 #15
0
 public MFTestResults SocketsEnums11_SocketOptionName_DontFragment_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing DontFragment with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
         SocketOptionName.DontFragment, boolData, true);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #16
0
 public MFTestResults SocketsEnums11_SocketOptionName_UseLoopback_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing UseLoopback with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
         SocketOptionName.UseLoopback, boolData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #17
0
        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);
        }
예제 #18
0
 public MFTestResults SocketsEnums11_SocketOptionName_SendTimeout_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing SendTimeout with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.SendTimeout, intData, true);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #19
0
        public MFTestResults SocketsEnums11_SocketOptionName_TypeOfService_TCP()
        {
            //don't run this on emulator since its not supported.
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
                return MFTestResults.Skip;

            SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
            Log.Comment("Testing TypeOfService with TCP");
            bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
                SocketOptionName.TypeOfService, byteArrData, false);
            testSockets.TearDown();
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #20
0
 public MFTestResults SocketsEnums11_SocketOptionName_ReuseAddress_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing ReuseAddress with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.ReuseAddress, boolData, true);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #21
0
 public MFTestResults SocketsEnums11_SocketOptionName_ReceiveLowWater_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing ReceiveLowWater with TCP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.ReceiveLowWater, intData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #22
0
 public MFTestResults SocketsEnums11_SocketOptionName_PacketInformation_TCP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
     Log.Comment("Testing PacketInformation with TCP");
     bool testResult = TestSocketOptionGetOnly(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.PacketInformation, byteArrData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #23
0
        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);
        }
예제 #24
0
 public MFTestResults SocketsEnums11_SocketOptionName_AcceptConnection_UDP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
     Log.Comment("Testing AcceptConnection with UDP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.AcceptConnection, boolData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #25
0
        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;
        }
예제 #26
0
        public MFTestResults SocketsEnums11_SocketOptionName_DropSourceMembership_TCP()
        {
            NetworkInterface[] inter = NetworkInterface.GetAllNetworkInterfaces();
            IPAddress localAddress = SocketTools.ParseAddress(inter[0].IPAddress);
            byte[] local = localAddress.GetAddressBytes();
            byte[] multicastOpt = new byte[] {    224,      100,        1,        1,
                                                          local[0], local[1], local[2], local[3]};

            SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
            Log.Comment("Testing DropSourceMembership with TCP");
            bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
                SocketOptionName.DropSourceMembership, multicastOpt, false);
            testSockets.TearDown();
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #27
0
        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);
        }
예제 #28
0
        public MFTestResults SocketsEnums13_SON_Broadcast()
        {
            /// <summary>
            /// 1. Starts a UDP client broadcasting 
            /// 2. Verifies that Broadcast is correct before and after
            /// </summary>
            ///
            bool testResult = false;
            try
            {
                SocketPair testSockets1 = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
                int clientPort1 = SocketTools.nextPort;
                int serverPort1 = SocketTools.nextPort;
                int tempPort1 = SocketTools.nextPort;
                IPEndPoint epBroadcast1 = new IPEndPoint(
                    SocketTools.DottedDecimalToIp(
                    (byte)255, (byte)255, (byte)255, (byte)255), tempPort1);

                Log.Comment("SetSocketOption socket, broadcast, false");
                testSockets1.socketClient.SetSocketOption(
                    SocketOptionLevel.Socket, SocketOptionName.Broadcast, false);

                try
                {
                    Log.Comment("SendTo epBroadcast1");
                    testSockets1.socketClient.SendTo(
                        new byte[] { 0x01, 0x02, 0x03 }, epBroadcast1);
                    Log.Comment("Error, this should never get displayed.  An exception should have been thrown.");
                }
                catch (SocketException e)
                {
                    Log.Comment("Error code: " + e.ErrorCode);
                    Log.Comment("Correctly threw exception trying to broadcast from socket that has had broadcast turned off.");
                    testResult = true;
                }
                catch (Exception e)
                {
                    Log.Comment("Incorrect exception thrown: " + e.Message);
                }
                finally
                {
                    Log.Comment("Tear down the socket");
                    testSockets1.TearDown();
                }
                //--//

                SocketPair testSockets2 = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
                int clientPort2 = SocketTools.nextPort;
                int serverPort2 = SocketTools.nextPort;
                int tempPort2 = SocketTools.nextPort;
                IPEndPoint epBroadcast2 = new IPEndPoint(
                    SocketTools.DottedDecimalToIp(
                    (byte)255, (byte)255, (byte)255, (byte)255), tempPort2);

                Log.Comment("SetSocketOption socket, broadcast, true");
                testSockets2.socketClient.SetSocketOption(
                    SocketOptionLevel.Socket,SocketOptionName.Broadcast, true);

                try
                {
                    Log.Comment("SendTo epBroadcast1");
                    testSockets2.socketClient.SendTo(
                        new byte[] { 0x01, 0x02, 0x03 }, epBroadcast2);
                }
                catch (SocketException e)
                {
                    Log.Comment("Error code: " + e.ErrorCode);
                    if (e.ErrorCode == 10047)
                    {
                        Log.Comment("Some drivers do not like this option being set.  Allow them to throw this exception");
                        testResult &= true;
                    }
                    else
                    {
                        Log.Comment("Incorrectly threw exception trying to broadcast from socket that has had broadcast turned on.");
                        testResult = false;
                    }
                }
                catch (Exception e)
                {
                    Log.Comment("Incorrect exception thrown: " + e.Message);
                }
                finally
                {
                    Log.Comment("Tear down the socket");
                    testSockets2.TearDown();
                }
            }
            catch (SocketException e)
            {
                Log.Comment("SocketException ErrorCode: " + e.ErrorCode);
                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
                {
                    Log.Comment("Fail for any other error codes that we don't know about");
                    testResult = false;
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                testResult = false;
            }

            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #29
0
        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);
        }
예제 #30
0
        private bool TestSocketOption(SocketPair testSockets, SocketOptionLevel optionLevel, 
             SocketOptionName optionName, Object data, bool isSupported)
        {
            try
            {
                string typeStr = data.GetType().ToString();
                switch (typeStr)
                {
                    case "System.Boolean":
                        Log.Comment("First set");
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            (bool)data);
                        Log.Comment("First get"); 
                        if (((int)testSockets.socketClient.GetSocketOption(optionLevel, 
                            optionName) == 1) != (bool)data)
                            throw new Exception("Got wrong data after first Set");
                        Log.Comment("Second set"); 
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            !(bool)data);
                        Log.Comment("Second get");
                        if (((int)testSockets.socketClient.GetSocketOption(optionLevel, 
                            optionName) == 1) != !(bool)data)
                            throw new Exception("Got wrong data after second Set");
                        break;
                    case "System.Int32":
                        Log.Comment("First set");
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            (int)data);
                        Log.Comment("First get");
                        if ((int)testSockets.socketClient.GetSocketOption(optionLevel, 
                            optionName) != (int)data)
                            throw new Exception("Got wrong data after first Set");
                        Log.Comment("Second set");
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            (int)data - 1);
                        Log.Comment("Second get");
                        if ((int)testSockets.socketClient.GetSocketOption(optionLevel, 
                            optionName) != (int)data - 1)
                            throw new Exception("Got wrong data after second Set");
                        break;
                    case "System.Byte[]":
                        Log.Comment("First set");
                        byte[] result = new byte[((byte[])data).Length];
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            (byte[])data);
                        testSockets.socketClient.GetSocketOption(optionLevel, optionName, 
                            result) ;
                        Log.Comment("First get");
                        if (!SocketTools.ArrayEquals(result, (byte[])data))
                            throw new Exception("Got wrong data after first Set");

                        //Decrement first byte of data
                        ((byte[])data)[0]--;

                        Log.Comment("Second set");
                        testSockets.socketClient.SetSocketOption(optionLevel, optionName, 
                            (byte[])data); 
                        testSockets.socketClient.GetSocketOption(optionLevel, optionName, 
                            result);
                        Log.Comment("Second get");
                        if (!SocketTools.ArrayEquals(result, (byte[])data))
                            throw new Exception("Got wrong data after second Set");
                        break;
                    default:
                        throw new Exception("Test Error, cannot set socket option with that type");
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException"))
                {
                    Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode);
                    if (!isSupported && (((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption ||
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressFamilyNotSupported ||
                        ((SocketException)e).ErrorCode == (int)SocketError.InvalidArgument ||
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressNotAvailable))
                    {
                        Log.Comment("Non-supported option graceful fail");
                        return true;
                    }
                    else if (isSupported && ((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption)
                    {
                        Log.Comment(
                            "Supported option graceful fail, this option needs implementation");
                        return true;
                    }
                    else if (!isSupported && (((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption ||
                        ((SocketException)e).ErrorCode == (int)SocketError.InvalidArgument ||
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressNotAvailable))
                    {
                        Log.Comment("Non-supported option failed with bad errorcode, " +
                            "should be 10042 or 10022");
                    }
                }
                Log.Comment("SocketOption FAILED");
                return false;
            }

            if (!isSupported)
            {
                Log.Comment("This option is not supported.  Should throw an exception");
                return true;
            }

            Log.Comment("SocketOption succeeded");
            return true;
        }
예제 #31
0
        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);
        }
예제 #32
0
        public MFTestResults SocketsEnums11_SocketOptionName_MulticastInterface_TCP()
        {
            NetworkInterface[] inter = NetworkInterface.GetAllNetworkInterfaces();
            IPAddress localAddress = SocketTools.ParseAddress(inter[0].IPAddress);

            SocketPair testSockets = new SocketPair(ProtocolType.Tcp, SocketType.Stream);
            Log.Comment("Testing MulticastInterface with TCP");
            bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
                SocketOptionName.MulticastInterface, localAddress.GetAddressBytes(), false);
            testSockets.TearDown();
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #33
0
        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);
        }
예제 #34
0
        public MFTestResults SocketsEnums11_SocketOptionName_Debug_UDP()
        {
            //don't run this on emulator since its not supported.
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
                return MFTestResults.Skip;

            SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
            Log.Comment("Testing Debug with UDP");
            bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
                SocketOptionName.Debug, boolData, false);
            testSockets.TearDown();
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #35
0
        public MFTestResults SocketsEnums11_SocketOptionName_AddSourceMembership_UDP()
        {
            //don't run this on emulator since its not supported.
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
                return MFTestResults.Skip;

            NetworkInterface[] inter = NetworkInterface.GetAllNetworkInterfaces();
            IPAddress localAddress = SocketTools.ParseAddress(inter[0].IPAddress);
            byte[] local = localAddress.GetAddressBytes();
            byte[] multicastOpt = new byte[] {    224,      100,        1,        1,
                                                          local[0], local[1], local[2], local[3]};

            SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
            Log.Comment("Testing AddSourceMembership with UDP");
            bool testResult = TestSocketOption(testSockets, SocketOptionLevel.IP, 
                SocketOptionName.AddSourceMembership, multicastOpt, false);
            testSockets.TearDown();
            return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
예제 #36
0
        private bool TestSocketOptionSetOnly(SocketPair testSockets, 
            SocketOptionLevel optionLevel,
             SocketOptionName optionName, Object data, bool isSupported)
        {
            try
            {
                Log.Comment("Option is only valid for Set");
                string typeStr = data.GetType().ToString();
                switch (typeStr)
                {
                    case "System.Boolean":
                        testSockets.socketClient.SetSocketOption(optionLevel, 
                            optionName, (bool)data);
                        break;
                    case "System.Int32":
                        testSockets.socketClient.SetSocketOption(optionLevel, 
                            optionName, (int)data);
                        break;
                    case "System.Byte[]":
                        testSockets.socketClient.SetSocketOption(optionLevel, 
                            optionName, (byte[])data);
                        break;
                    default:
                        throw new Exception("Test Error, cannot set socket option with that type");
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                if (e.GetType() == Type.GetType("System.Net.Sockets.SocketException"))
                {
                    Log.Comment("ErrorCode: " + ((SocketException)e).ErrorCode);
                    if (!isSupported && (((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption || 
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressFamilyNotSupported || 
                        ((SocketException)e).ErrorCode == (int)SocketError.InvalidArgument || 
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressNotAvailable))
                    {
                        Log.Comment("Non-supported option graceful fail");
                        return true;
                    }
                    else if (isSupported && ((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption)
                    {
                        Log.Comment(
                            "Supported option graceful fail, this option needs implementation");
                        return true;
                    }
                    else if (!isSupported && (((SocketException)e).ErrorCode == (int)SocketError.ProtocolOption || 
                        ((SocketException)e).ErrorCode == (int)SocketError.InvalidArgument || 
                        ((SocketException)e).ErrorCode == (int)SocketError.AddressNotAvailable))
                    {
                        Log.Comment("Non-supported option failed with bad errorcode, " +
                            "should be 10042 or 10022");
                    }
                }
                Log.Comment("SocketOption FAILED");
                return false;
            }

            if (!isSupported)
            {
                Log.Comment("This option is not supported.  Should throw an exception");
                return true;
            }

            Log.Comment("SocketOption succeeded");
            return true;
        }
예제 #37
0
 public MFTestResults SocketsEnums11_SocketOptionName_Broadcast_UDP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
     Log.Comment("Testing Broadcast with UDP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.Broadcast, boolData, true);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }
예제 #38
0
 public MFTestResults SocketsEnums11_SocketOptionName_SendLowWater_UDP()
 {
     SocketPair testSockets = new SocketPair(ProtocolType.Udp, SocketType.Dgram);
     Log.Comment("Testing SendLowWater with UDP");
     bool testResult = TestSocketOption(testSockets, SocketOptionLevel.Socket, 
         SocketOptionName.SendLowWater, intData, false);
     testSockets.TearDown();
     return (testResult ? MFTestResults.Pass : MFTestResults.Fail);
 }