Пример #1
0
 public TcpTestServer()
 {
     _testport              = TestPort.Create();
     _testserver            = new TcpServer(IPAddress.Loopback.ToString(), _testport);
     _testserver.Connected += Connected;
     _testserver.Start();
 }
Пример #2
0
        public virtual void Setup()
        {
            _origout = Console.Out;
            Console.SetOut(_stdout);
            _origerror = Console.Error;
            Console.SetError(_stderror);

            _clientPort = TestPort.Create();
            _serverPort = TestPort.Create();

            _clientCert = new TestCert("client.localhost.nunit");
            _serverCert = new TestCert("server.localhost.nunit");
        }
Пример #3
0
        public void TestCommandRun()
        {
            string exeFile = typeof(SslTunnel.Server.Commands).Assembly.Location;

            Assert.IsTrue(File.Exists(exeFile));
            File.Copy(exeFile, Path.ChangeExtension(exeFile, ".nunittest.exe"), true);
            exeFile = Path.ChangeExtension(exeFile, ".nunittest.exe");
            int testPort = TestPort.Create();

            using (TextWriter config = File.CreateText(exeFile + ".config"))
            {
                config.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                config.WriteLine("<configuration>");
                config.WriteLine("  <configSections>");
                config.WriteLine("	  <section name=\"TunnelConfig\" type=\"CSharpTest.Net.SslTunnel.Config, SslTunnel.Library\" />");
                config.WriteLine("  </configSections>");
                config.WriteLine("  <TunnelConfig>");
                config.WriteLine("	  <listener ip=\"127.0.0.1\" port=\"{0}\" serverCertFile=\"{1}\">", testPort, _serverCert.CertificateFile);
                config.WriteLine("	    <target ip=\"google.com\" port=\"80\" />");
                config.WriteLine("	  </listener>");
                config.WriteLine("  </TunnelConfig>");
                config.WriteLine("</configuration>");
            }

            ServicePointManager.ServerCertificateValidationCallback =
                delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                if (certificate.GetPublicKeyString() == _serverCert.Certificate.GetPublicKeyString())
                {
                    return(true);
                }
                return(sslPolicyErrors == SslPolicyErrors.None);
            };
            try
            {
                try
                {
                    using (TcpClient client = new TcpClient("127.0.0.1", testPort))
                    {
                        client.Connect();
                        client.Stream.WriteByte(0);
                    }

                    Assert.Fail("Connection should fail before we call Run()");
                }
                catch (SocketException e)
                {
                    Assert.AreEqual(SocketError.ConnectionRefused, e.SocketErrorCode);
                }

                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationBase   = Path.GetDirectoryName(exeFile);
                setup.ApplicationName   = Path.GetFileName(exeFile);
                setup.ConfigurationFile = exeFile + ".config";
                AppDomain      domain    = AppDomain.CreateDomain("SslTunnel", AppDomain.CurrentDomain.Evidence, setup);
                BlockingReader blockRead = new BlockingReader(Environment.NewLine);
                Thread         thread    = null;
                try
                {
                    thread = new Thread(
                        delegate()
                    {
                        SetConsoleInput domainConsole = (SetConsoleInput)domain.CreateInstanceAndUnwrap(typeof(SetConsoleInput).Assembly.FullName, typeof(SetConsoleInput).FullName);
                        domainConsole.SetInput(blockRead);
                        domainConsole.SetOutput(_stdout);
                        domainConsole.SetError(_stderror);
                        domain.ExecuteAssembly(
                            exeFile,
#if NET20 || NET35
                            AppDomain.CurrentDomain.Evidence,
#endif
                            new string[] { "run" });
                    });
                    thread.Name = setup.ApplicationName;
                    thread.Start();

                    while (thread.IsAlive && Respose.Contains("Press [Enter] to quit...") == false && Respose.Contains("127.0.0.1") == false)
                    {
                        Thread.Sleep(100);
                    }

                    WebClient client   = new WebClient();
                    string    response = client.DownloadString("https://127.0.0.1:" + testPort);
                    Assert.IsTrue(response.Contains("<html"));
                    Assert.IsTrue(response.Contains("google.com"));
                }
                finally
                {
                    try
                    {
                        if (thread != null)
                        {
                            blockRead.WaitHandle.Set();
                            Assert.IsTrue(thread.Join(TimeSpan.FromMinutes(1)));
                        }
                        AppDomain.Unload(domain);
                        File.Delete(exeFile + ".config");
                        File.Delete(exeFile);
                    }
                    catch (Exception e) { Trace.TraceWarning(e.ToString()); }
                }
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = null;
            }
        }
 public override void Setup()
 {
     base.Setup();
     _mixInPort = TestPort.Create();
 }