internal void Setup(string macAddress, bool multipleIps, bool oneDisabledIp, bool noDns)
        {
            Logger = MockRepository.GenerateMock<ILogger>();

            ExecutableProcessQueue = MockRepository.GenerateMock<IExecutableProcessQueue>();
            ExecutableProcessQueue.Stub(x => x.Enqueue(Arg<string>.Is.Anything, Arg<string>.Is.Anything)).Return(
                ExecutableProcessQueue);

            WmiMacNetworkNameGetter = MockRepository.GenerateMock<IWmiMacNetworkNameGetter>();

            WmiMacNetworkNameGetter.Stub(x => x.Get()).Return(new Dictionary<string, string>
                                                                  {{"Lan1", "FAKEMAC"}, {"Lan2", ""}}).Repeat.Once();
            WmiMacNetworkNameGetter.Stub(x => x.Get()).Return(new Dictionary<string, string>
                                                                  {{"Lan1", "FAKEMAC"}, {"Lan2", "FAKEMAC2"}});

            NetworkInterface = multipleIps
                                   ? GetMeANetworkInterfaceWithAPrimaryAndSecondaryIp(macAddress)
                                   : GetMeANetworkInterface(macAddress);

            if (noDns) NetworkInterface.dns = null;
            if (oneDisabledIp) NetworkInterface.ips[1].enabled = "0";

            new MockRepository().Ordered();
            ExecutableProcessQueue.Replay();
        }
        public void print_serialized_json_string_and_deserialize()
        {
            const string stringWrong = "{\"mac\":\"40:40:92:9e:44:48\",\"dns\":[\"72.3.128.240\",\"72.3.128.241\"],\"label\":\"public\",\"ips\":[{\"ip\":\"98.129.220.138\",\"netmask\":\"255.255.255.0\"}],\"gateway\":\"98.129.220.1\",\"slice\":74532}";
            const string stringCorrt = "{\"mac\":\"40:40:92:9e:44:48\",\"dns\":[\"72.3.128.240\",\"72.3.128.241\"],\"label\":\"public\",\"ips\":[{\"ip\":\"98.129.220.138\",\"netmask\":\"255.255.255.0\"}],\"gateway\":\"98.129.220.1\"}";
            const string stringSomething =
                "{\"label\": \"private\", \"ips\": [{\"netmask\": \"255.255.224.0\", \"ip\": \"10.176.64.48\"}], \"mac\": \"40:40:d0:ed:cb:96\"}";

            var interface1 = new NetworkInterface
                                 {
                                     gateway = "98.129.220.1",
                                     label = "public",
                                     mac = "40:40:92:9e:44:48",
                                     dns = new[] { "72.3.128.240", "72.3.128.241" },
                                     ips =
                                         new[]
                                             {
                                                 new IpTuple {ip = "98.129.220.138", netmask = "255.255.255.0", enabled = "1"},
                                             }
                                 };

            var serialized = _jsonInterface.Serialize(interface1);
            Assert.That(serialized, Is.EqualTo("{\"mac\":\"40:40:92:9e:44:48\",\"dns\":[\"72.3.128.240\",\"72.3.128.241\"],\"label\":\"public\",\"ips\":[{\"ip\":\"98.129.220.138\",\"netmask\":\"255.255.255.0\",\"enabled\":\"1\"}],\"gateway\":\"98.129.220.1\",\"routes\":null}"));

            _jsonInterface.Deserialize(stringCorrt);
            _jsonInterface.Deserialize(stringWrong);
            _jsonInterface.Deserialize(stringSomething);
            _jsonCommand.Deserialize(_updateJson);
        }
        public void Setup()
        {
            xenNetworkInformation = MockRepository.GenerateMock<IXenNetworkInformation>();
            setNetworkInterface = MockRepository.GenerateMock<ISetNetworkInterface>();
            setNetworkRoutes = MockRepository.GenerateMock<ISetNetworkRoutes>();

            networkInterface = new NetworkInterface();
            network = new Network();
            network.Interfaces.Add("fakemac", networkInterface);
            command = new ResetNetwork(setNetworkInterface, xenNetworkInformation, setNetworkRoutes);
            xenNetworkInformation.Stub(x => x.Get()).Return(network);

            result = command.Execute(null);
        }
        public void Setup()
        {
            xenNetworkInformation = MockRepository.GenerateMock<IXenNetworkInformation>();
            setNetworkInterface = MockRepository.GenerateMock<ISetNetworkInterface>();
            setNetworkRoutes = MockRepository.GenerateMock<ISetNetworkRoutes>();

            xenProviderDataInformation = MockRepository.GenerateMock<IXenProviderDataInformation>();
            setProviderData = MockRepository.GenerateMock<ISetProviderData>();
            setHostname = MockRepository.GenerateMock<ISetHostnameAction>();
            _xenStore = MockRepository.GenerateMock<IXenStore>();

            networkInterface = new NetworkInterface();
            network = new Network();
            network.Interfaces.Add("fakemac", networkInterface);

            providerData = new ProviderData();

            command = new ResetNetwork(setNetworkInterface, xenNetworkInformation, setNetworkRoutes, setProviderData, xenProviderDataInformation, setHostname, _xenStore);
        }
        private void SetupIpv6Interface(string interfaceName, NetworkInterface networkInterface)
        {
            if (networkInterface.ip6s == null || networkInterface.ip6s.Length == 0) return;
            var ip6tuple = networkInterface.ip6s[0];
            if (ip6tuple.enabled != "1") return;
            string command = string.Format("interface ipv6 add address interface=\"{0}\" address={1}/{2}",
                interfaceName, ip6tuple.ip, ip6tuple.netmask);
            _executableProcessQueue.Enqueue("netsh", command);

            command = string.Format("interface ipv6 add route prefix=::/0 interface=\"{0}\" nexthop={1} publish=Yes",
                interfaceName, ip6tuple.gateway);
            _executableProcessQueue.Enqueue("netsh", command, new [] { "0" , "1" });
        }
        private void SetupIpv4Interface(string interfaceName, NetworkInterface networkInterface)
        {
            var primaryIpHasBeenAssigned = false;
            for (var i = 0; i != networkInterface.ips.Length; i++)
            {
                if (networkInterface.ips[i].enabled != "1") continue;
                if (!string.IsNullOrEmpty(networkInterface.gateway) && !primaryIpHasBeenAssigned)
                {
                    _executableProcessQueue.Enqueue("netsh",
                                                    String.Format(
                                                        "interface ip add address name=\"{0}\" addr={1} mask={2} gateway={3} gwmetric=2",
                                                        interfaceName, networkInterface.ips[i].ip, networkInterface.ips[i].netmask, networkInterface.gateway));
                    primaryIpHasBeenAssigned = true;
                    continue;
                }

                _executableProcessQueue.Enqueue("netsh", String.Format("interface ip add address name=\"{0}\" addr={1} mask={2}",
                                                                       interfaceName, networkInterface.ips[i].ip, networkInterface.ips[i].netmask));
            }
        }
        private void SetupDns(string interfaceName, NetworkInterface networkInterface)
        {
            // Remove Duplicate DNS entries if any.
            var distinctDNSEntries = networkInterface.dns.Distinct().ToArray();
            var originalDNSEntries = networkInterface.dns;

            if (originalDNSEntries.Length != distinctDNSEntries.Length)
            {
                networkInterface.dns = distinctDNSEntries;

                _logger.Log(string.Format("Removed duplicate DNS entries. Before {0}. After {1}", string.Join(", ", originalDNSEntries.ToArray()), string.Join(", ", distinctDNSEntries.ToArray())));
            }

            for (var i = 0; i != networkInterface.dns.Length; i++)
            {
                _executableProcessQueue.Enqueue("netsh", String.Format("interface ip add dns name=\"{0}\" addr={1} index={2}",
                                                                       interfaceName, networkInterface.dns[i], i + 1));
            }
        }
        private void SetNetworkInterfaceValues(NetworkInterface networkInterface, string interfaceName)
        {
            SetupIpv4Interface(interfaceName, networkInterface);
            SetupIpv6Interface(interfaceName, networkInterface);

            if (networkInterface.dns != null && networkInterface.dns.Length > 0)
            {
                CleanseDnsForSetup(interfaceName);
                SetupDns(interfaceName, networkInterface);
            }

            _executableProcessQueue.Go();

            SetInterfaceName(networkInterface, interfaceName, 0);
        }
 private void SetInterfaceName(NetworkInterface networkInterface, string interfaceName, int count)
 {
     if (interfaceName != networkInterface.label)
         _executableProcessQueue.Enqueue("netsh",
                                         String.Format("interface set interface name=\"{0}\" newname=\"{1}\"",
                                                       interfaceName, networkInterface.label + count));
     try
     {
         _executableProcessQueue.Go();
     }
     catch (UnsuccessfulCommandExecutionException e)
     {
         _logger.Log(string.Format("Failed to setinterface name to {0} retrying", networkInterface.label + count));
         if (count < NO_OF_RETRIES_FOR_SETTING_INTERFACE_NAME)
             SetInterfaceName(networkInterface, interfaceName, ++count);
     }
 }
 public NetworkInterfaceBuilder()
 {
     this.netWorkInterface = new NetworkInterface();
 }
 private void SetupDns(string interfaceName, NetworkInterface networkInterface)
 {
     for (var i = 0; i != networkInterface.dns.Length; i++) {
         _executableProcessQueue.Enqueue("netsh", String.Format("interface ip add dns name=\"{0}\" addr={1} index={2}",
                                                                interfaceName, networkInterface.dns[i], i + 1));
     }
 }
        private void SetNetworkInterfaceValues(NetworkInterface networkInterface, string interfaceName)
        {
            CleanseInterfaceForSetup(interfaceName);
            SetupInterface(interfaceName, networkInterface);

            if (networkInterface.dns != null && networkInterface.dns.Length > 0) {
                CleanseDnsForSetup(interfaceName);
                SetupDns(interfaceName, networkInterface);
            }

            if(interfaceName != networkInterface.label)
                _executableProcessQueue.Enqueue("netsh", String.Format("interface set interface name=\"{0}\" newname=\"{1}\"", interfaceName, networkInterface.label));

            _executableProcessQueue.Go();
        }
        private void SetupInterface(string interfaceName, NetworkInterface networkInterface)
        {
            var primaryIpHasBeenAssigned = false;
            for (var i = 0; i != networkInterface.ips.Length; i++) {
                if (networkInterface.ips[i].enabled != "1") continue;
                if (!string.IsNullOrEmpty(networkInterface.gateway) && !primaryIpHasBeenAssigned) {
                    _executableProcessQueue.Enqueue("netsh",
                                                    String.Format(
                                                        "interface ip add address name=\"{0}\" addr={1} mask={2} gateway={3} gwmetric=2",
                                                        interfaceName, networkInterface.ips[i].ip, networkInterface.ips[i].netmask, networkInterface.gateway));
                    primaryIpHasBeenAssigned = true;
                    continue;
                }

                _executableProcessQueue.Enqueue("netsh", String.Format("interface ip add address name=\"{0}\" addr={1} mask={2}",
                                                                       interfaceName, networkInterface.ips[i].ip, networkInterface.ips[i].netmask));
            }

            var primaryIp6HasBeenAssigned = false;
            for (var i = 0; i != networkInterface.ip6s.Length; i++) {
                if (networkInterface.ip6s[i].enabled != "1") continue;
                if (!string.IsNullOrEmpty(networkInterface.gateway6) && !primaryIp6HasBeenAssigned) {
                    _executableProcessQueue.Enqueue("netsh",
                                                    String.Format(
                                                        "interface ipv6 add address interface=\"{0}\" addrress={1}",
                                                        interfaceName, networkInterface.ips[i].ip));
                    _executableProcessQueue.Enqueue("netsh",
                                                    String.Format(
                                                        // NOTE(jkoelker) Do we need the siteprefixlength here??
                                                        "interface ipv6 add route interface=\"{0}\" prefix=\"::/0\" nexthop=\"{1}\" metric=2",
                                                        interfaceName, networkInterface.gateway6));
                    primaryIp6HasBeenAssigned = true;
                    continue;
                }
                _executableProcessQueue.Enqueue("netsh", String.Format("interface ipv6 add address interface=\"{0}\" address={1}",
                                                                       interfaceName, networkInterface.ip6s[i].ip));
            }
        }
        public void should_remove_duplicate_dns_entries()
        {
            var interface1 = new NetworkInterface
            {
                gateway = "98.129.220.1",
                label = "public",
                mac = "40:40:92:9e:44:48",
                dns = new[] { "72.3.128.240", "72.3.128.240", "72.3.128.241", "72.3.128.241", },
                ips =
                    new[]
                                             {
                                                 new Ipv4Tuple {ip = "98.129.220.138", netmask = "255.255.255.0", enabled = "1"},
                                             },
                ip6s = new[]
                                                {
                                                    new Ipv6Tuple {ip = "2001:4801:787F:202:278E:89D8:FF06:B476", netmask = "96", enabled = "1", gateway = "fe80::def"}
                                                }
            };

            var serialized = _jsonInterface.Serialize(interface1);
            Assert.That(serialized, Is.EqualTo("{\"mac\":\"40:40:92:9e:44:48\",\"dns\":[\"72.3.128.240\",\"72.3.128.240\",\"72.3.128.241\",\"72.3.128.241\"],\"label\":\"public\",\"ips\":[{\"ip\":\"98.129.220.138\",\"netmask\":\"255.255.255.0\",\"enabled\":\"1\"}]," +
                "\"ip6s\":[{\"ip\":\"2001:4801:787F:202:278E:89D8:FF06:B476\",\"netmask\":\"96\",\"gateway\":\"fe80::def\",\"enabled\":\"1\"}]," +
                "\"gateway\":\"98.129.220.1\",\"routes\":null}"));

            var deserialized = _jsonInterface.Deserialize(serialized);

            deserialized.dns = deserialized.dns.Distinct().ToArray();

            var serializedAfterRemovingDuplicateDNSEntries = _jsonInterface.Serialize(deserialized);

            Assert.That(serializedAfterRemovingDuplicateDNSEntries, Is.EqualTo("{\"mac\":\"40:40:92:9e:44:48\",\"dns\":[\"72.3.128.240\",\"72.3.128.241\"],\"label\":\"public\",\"ips\":[{\"ip\":\"98.129.220.138\",\"netmask\":\"255.255.255.0\",\"enabled\":\"1\"}]," +
                "\"ip6s\":[{\"ip\":\"2001:4801:787F:202:278E:89D8:FF06:B476\",\"netmask\":\"96\",\"gateway\":\"fe80::def\",\"enabled\":\"1\"}]," +
                "\"gateway\":\"98.129.220.1\",\"routes\":null}"));
        }
        public void Setup()
        {
            _network = new Network();

            Setup("fakemac");
            SetupRoutesOnInterface(new[]
                                          {
                                              new NetworkRoute
                                                  {
                                                      gateway = "10.1.10.20",
                                                      netmask = "255.255.255.0",
                                                      route = "10.1.10.1"
                                                  },
                                                  new NetworkRoute
                                                  {
                                                      gateway = "10.1.10.20",
                                                      netmask = "255.255.255.0",
                                                      route = "10.1.10.2"
                                                  }

                                          });
            _networkInterface1 = NetworkInterface;
            _networkInterface1.label = "public";
            _network.Interfaces.Add("fakemac", _networkInterface1);

            Setup("fakemac1");
            SetupRoutesOnInterface(new[]
                                          {
                                              new NetworkRoute
                                                  {
                                                      gateway = "10.1.10.20",
                                                      netmask = "255.255.255.0",
                                                      route = "10.1.10.1"
                                                  },
                                                  new NetworkRoute
                                                  {
                                                      gateway = "10.1.10.20",
                                                      netmask = "255.255.255.0",
                                                      route = "10.1.10.3"
                                                  }

                                          });
            _networkInterface2 = NetworkInterface;
            _networkInterface2.gateway = "10.1.1.1";
            _network.Interfaces.Add("fakemac1", _networkInterface2);

            _executableProcessQueue = MockRepository.GenerateMock<IExecutableProcessQueue>();
            _executableProcessQueue.Expect(x => x.Go()).Repeat.Once();

            _registryReader = MockRepository.GenerateMock<IRegistryReader>();
            _registryReader.Stub(x => x.GetValuesFrom(Arg<string>.Is.Anything))
                .Return(new List<string> {
                                             "0.0.0.0,0.0.0.0,172.16.251.2,2",
                                             "1.2.3.4,5.6.7.8,9.10.11.12.13,10"
                                         });

            _logger = MockRepository.GenerateMock<ILogger>();

            ExecutableProcessQueue.Replay();
            _setNetworkRoutes = new SetNetworkRoutes(_executableProcessQueue, _registryReader, _logger);
            _setNetworkRoutes.Execute(_network);
        }