示例#1
0
        private static bool RunTest(Test test, Diversion injectionDiversion)
        {
            byte[] buffer = new byte[MaxPacket];

            string validationError = string.Empty;

            if(!Diversion.ValidateFilter(test.Filter, DivertLayer.Network, ref validationError))
            {
                System.Console.WriteLine("Error in filter string {0}", validationError);
                return false;
            }

            Address address = new Address();
            address.Direction = DivertDirection.Outbound;

            if(Diversion.EvaluateFilter(test.Filter, DivertLayer.Network, test.TestPacketData, (uint)test.TestPacketData.Length, address) != test.Match)
            {
                System.Console.WriteLine("Error - Filter {0} does not match the packet.", test.Filter);
                return false;
            }

            Diversion testDiversion1 = null;

            Diversion testDiversion2 = null;

            try
            {
                // (1) Open a WinDivert handle to the given filter
                testDiversion1 = Diversion.Open(test.Filter, DivertLayer.Network, 0, 0);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(string.Format("Failed to open testDiversion1 with error {0}\n", e.Message));
                return false;
            }

            if (!testDiversion1.Handle.Valid)
            {
                System.Console.WriteLine(string.Format("Failed to open testDiversion1 with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                return false;
            }

            if(!test.Match)
            {
                testDiversion2 = testDiversion1;

                try
                {
                    // Catch non-matching packets
                    testDiversion1 = Diversion.Open("true", DivertLayer.Network, 33, 0);
                }
                catch (System.Exception e)
                {
                    System.Console.WriteLine(string.Format("Failed to open testDiversion2 with error {0}\n", e.Message));
                    testDiversion1.Close();
                    return false;
                }

                if (!testDiversion2.Handle.Valid)
                {
                    System.Console.WriteLine(string.Format("Failed to open testDiversion2 with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                    testDiversion1.Close();
                    return false;
                }
            }

            // (2) Inject the packet
            uint injectLength = 0;
            if(!injectionDiversion.Send(test.TestPacketData, (uint)test.TestPacketData.Length, address, ref injectLength))
            {
                System.Console.WriteLine(string.Format("Error injecting test packet with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

                if (testDiversion2 != null)
                {
                    testDiversion2.Close();
                }
                testDiversion1.Close();

                return false;
            }

            // (3) Wait for the packet to arrive
            DivertAsyncResult result = new DivertAsyncResult();
            uint recvLength = 0;
            if(!testDiversion1.ReceiveAsync(buffer, address, ref recvLength, result))
            {
                if(!result.NoError)
                {
                    System.Console.WriteLine(string.Format("Async packet reception failed with error {0}.\n", result.ErrorCode));

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return false;
                }

                if(!result.Get(250))
                {
                    System.Console.WriteLine(string.Format("Async packet read failed with error {0}.\n", result.ErrorCode));

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return false;
                }

                recvLength = result.Length;
            }

            if(address.Direction == DivertDirection.Outbound)
            {
                testDiversion1.CalculateChecksums(buffer, recvLength, 0);
            }

            // (4) Verify that the packet is the same
            if(recvLength != test.TestPacketData.Length)
            {
                System.Console.WriteLine("Received packet does not match test packet.\n");

                if (testDiversion2 != null)
                {
                    testDiversion2.Close();
                }
                testDiversion1.Close();

                return false;
            }

            for(int i = 0; i < test.TestPacketData.Length; ++i)
            {
                if(buffer[i] != test.TestPacketData[i])
                {
                    System.Console.WriteLine("Received packet does not match test packet.\n");

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return false;
                }
            }

            // (5) Clean-up
            if (testDiversion1 != null)
            {
                testDiversion1.Close();
            }
                        
            if (testDiversion2 != null)
            {
                testDiversion2.Close();
            }

            return true;
        }
示例#2
0
        private static bool RunTest(Test test, Diversion injectionDiversion)
        {
            byte[] buffer = new byte[MaxPacket];

            string validationError = string.Empty;

            if (!Diversion.ValidateFilter(test.Filter, DivertLayer.Network, ref validationError))
            {
                System.Console.WriteLine("Error in filter string {0}", validationError);
                return(false);
            }

            Address address = new Address();

            address.Direction = DivertDirection.Outbound;

            if (Diversion.EvaluateFilter(test.Filter, DivertLayer.Network, test.TestPacketData, (uint)test.TestPacketData.Length, address) != test.Match)
            {
                System.Console.WriteLine("Error - Filter {0} does not match the packet.", test.Filter);
                return(false);
            }

            Diversion testDiversion1 = null;

            Diversion testDiversion2 = null;

            try
            {
                // (1) Open a WinDivert handle to the given filter
                testDiversion1 = Diversion.Open(test.Filter, DivertLayer.Network, 0, 0);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(string.Format("Failed to open testDiversion1 with error {0}\n", e.Message));
                return(false);
            }

            if (!testDiversion1.Handle.Valid)
            {
                System.Console.WriteLine(string.Format("Failed to open testDiversion1 with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                return(false);
            }

            if (!test.Match)
            {
                testDiversion2 = testDiversion1;

                try
                {
                    // Catch non-matching packets
                    testDiversion1 = Diversion.Open("true", DivertLayer.Network, 33, 0);
                }
                catch (System.Exception e)
                {
                    System.Console.WriteLine(string.Format("Failed to open testDiversion2 with error {0}\n", e.Message));
                    testDiversion1.Close();
                    return(false);
                }

                if (!testDiversion2.Handle.Valid)
                {
                    System.Console.WriteLine(string.Format("Failed to open testDiversion2 with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
                    testDiversion1.Close();
                    return(false);
                }
            }

            // (2) Inject the packet
            uint injectLength = 0;

            if (!injectionDiversion.Send(test.TestPacketData, (uint)test.TestPacketData.Length, address, ref injectLength))
            {
                System.Console.WriteLine(string.Format("Error injecting test packet with error {0}.\n", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

                if (testDiversion2 != null)
                {
                    testDiversion2.Close();
                }
                testDiversion1.Close();

                return(false);
            }

            // (3) Wait for the packet to arrive
            DivertAsyncResult result = new DivertAsyncResult();
            uint recvLength          = 0;

            if (!testDiversion1.ReceiveAsync(buffer, address, ref recvLength, result))
            {
                if (!result.NoError)
                {
                    System.Console.WriteLine(string.Format("Async packet reception failed with error {0}.\n", result.ErrorCode));

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return(false);
                }

                if (!result.Get(250))
                {
                    System.Console.WriteLine(string.Format("Async packet read failed with error {0}.\n", result.ErrorCode));

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return(false);
                }

                recvLength = result.Length;
            }

            if (address.Direction == DivertDirection.Outbound)
            {
                testDiversion1.CalculateChecksums(buffer, recvLength, 0);
            }

            // (4) Verify that the packet is the same
            if (recvLength != test.TestPacketData.Length)
            {
                System.Console.WriteLine("Received packet does not match test packet.\n");

                if (testDiversion2 != null)
                {
                    testDiversion2.Close();
                }
                testDiversion1.Close();

                return(false);
            }

            for (int i = 0; i < test.TestPacketData.Length; ++i)
            {
                if (buffer[i] != test.TestPacketData[i])
                {
                    System.Console.WriteLine("Received packet does not match test packet.\n");

                    if (testDiversion2 != null)
                    {
                        testDiversion2.Close();
                    }
                    testDiversion1.Close();

                    return(false);
                }
            }

            // (5) Clean-up
            if (testDiversion1 != null)
            {
                testDiversion1.Close();
            }

            if (testDiversion2 != null)
            {
                testDiversion2.Close();
            }

            return(true);
        }