public void DelegatesToFirewallManager()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                tcpPortManager.RemoveFirewallRules("fred");
                FirewallManager.Received(1).RemoveAllFirewallRules("fred");
            }
            public void DelegatesToFirewallManager()
            {
                var tcpPortManager   = new LocalTcpPortManager(FirewallManager, NetShRunner);
                var firewallRuleSpec = new FirewallRuleSpec();

                tcpPortManager.CreateOutboundFirewallRule("fred", firewallRuleSpec);
                FirewallManager.Received(1).CreateOutboundFirewallRule("fred", firewallRuleSpec);
            }
            public void RemovesFirewallRulesEvenWithNoPort()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                tcpPortManager.ReleaseLocalPort(null, "userName");

                NetShRunner.DidNotReceive().DeleteRule(Arg.Any <int>());
                FirewallManager.Received().ClosePort(Arg.Is("userName"));
            }
            public void ThrowsWardenExceptionIfNetShFails()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                NetShRunner.AddRule(Arg.Any <int>(), Arg.Any <string>()).ReturnsForAnyArgs(false);

                var exception = Record.Exception(() => tcpPortManager.ReserveLocalPort(8888, "userName"));

                Assert.IsType <Exception>(exception);
            }
            public void ZeroPortReturnsNewPort()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                NetShRunner.AddRule(Arg.Any <int>(), Arg.Any <string>()).ReturnsForAnyArgs(true);

                var port = tcpPortManager.ReserveLocalPort(0, "userName");

                Assert.True(port > 0);
            }
            public void CallsNetShRunnerFirewallManagerToReleasePort()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                NetShRunner.DeleteRule(Arg.Any <int>()).ReturnsForAnyArgs(true);

                tcpPortManager.ReleaseLocalPort(8888, "userName");

                NetShRunner.Received().DeleteRule(Arg.Is(8888));
                FirewallManager.Received().ClosePort(Arg.Is("userName"));
            }
            public void ThrowWardenExecptionIfFirewallManagerFails()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                NetShRunner.AddRule(Arg.Any <int>(), Arg.Any <string>()).ReturnsForAnyArgs(true);
                FirewallManager.When(x => x.OpenPort(Arg.Any <int>(), Arg.Any <string>())).Do(x => { throw new Exception(); });

                var exception = Record.Exception(() => tcpPortManager.ReserveLocalPort(8888, "userName"));

                Assert.IsType <Exception>(exception);
            }
            public void CallsNetShRunnerFirewallManagerAndReturnsPort()
            {
                var tcpPortManager = new LocalTcpPortManager(FirewallManager, NetShRunner);

                NetShRunner.AddRule(Arg.Any <int>(), Arg.Any <string>()).ReturnsForAnyArgs(true);

                var port = tcpPortManager.ReserveLocalPort(8888, "userName");

                NetShRunner.Received().AddRule(Arg.Is(8888), Arg.Is("userName"));
                FirewallManager.Received().OpenPort(Arg.Is(8888), Arg.Is("userName"));
                Assert.Equal(8888, port);
            }
            public void ThrowsArgumentNullExceptionIfUserNameIsNull()
            {
                var tcpPortManager = new LocalTcpPortManager(null, null);

                Assert.Throws <ArgumentNullException>(() => tcpPortManager.ReleaseLocalPort(100, null));
            }