public void Should_ThrowOnUnknownHostEntry()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            var hfe = new HostFileEntry()
            {
                Address  = "127.0.0.2",
                Hostname = "abc.com"
            };

            mySM.SetupExistingHostList(new[] { hfe });

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Set-HfHostAddress");
            psCmd.AddParameter("Hostname", hfe.Hostname + "aa");
            psCmd.AddParameter("Address", hfe.Hostname + "11.11.11.11");

            try
            {
                _powerShell.Commands = psCmd;
                _powerShell.Invoke();
            }
            catch (CmdletInvocationException cex)
            {
                throw cex.InnerException ?? cex;
            }

            // END FUNCTION
        }
        public void Should_UpdateEntry()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            var hfe = new HostFileEntry()
            {
                Address  = "127.0.0.2",
                Hostname = "abc.com"
            };

            mySM.SetupExistingHostList(new [] { hfe });

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Set-HfHostAddress");
            psCmd.AddParameter("Hostname", hfe.Hostname);
            psCmd.AddParameter("Address", "11.11.11.11");

            _powerShell.Commands = psCmd;
            _powerShell.Invoke();

            mySM.MockFileService.Verify(h => h.WriteEntries(
                                            It.Is <IEnumerable <HostFileEntry> >(en => en.Any(writeEntry =>
                                                                                              string.Compare(writeEntry.Hostname, hfe.Hostname) == 0 &&
                                                                                              string.Compare(writeEntry.Address, "11.11.11.11") == 0))));

            // END FUNCTION
        }
예제 #3
0
        public void Should_GetAllHosts()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            var entries = new[]
            {
                new HostFileEntry()
                {
                    Address  = "127.0.0.2",
                    Hostname = "abc.com"
                },
                new HostFileEntry()
                {
                    Address  = "127.0.0.3",
                    Hostname = "def.com"
                },
                new HostFileEntry()
                {
                    Address  = "127.0.0.4",
                    Hostname = "ghi.com"
                }
            };

            mySM.SetupExistingHostList(entries);

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Get-HfHost");
            _powerShell.Commands = psCmd;
            var results = _powerShell.Invoke <HostFileRecord>();

            mySM.MockFileService.Verify(h => h.GetEntries());
            Assert.AreEqual(3, results.Count);
            Assert.IsTrue(results.Any(he =>
                                      string.Compare(he.Hostname, entries[0].Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                      string.Compare(he.Address, entries[0].Address, StringComparison.CurrentCultureIgnoreCase) == 0));
            Assert.IsTrue(results.Any(he =>
                                      string.Compare(he.Hostname, entries[1].Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                      string.Compare(he.Address, entries[1].Address, StringComparison.CurrentCultureIgnoreCase) == 0));
            Assert.IsTrue(results.Any(he =>
                                      string.Compare(he.Hostname, entries[2].Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                      string.Compare(he.Address, entries[2].Address, StringComparison.CurrentCultureIgnoreCase) == 0));

            // END FUNCTION
        }
예제 #4
0
        public void Should_KeepExistingEntries()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            var entries = new[]
            {
                new HostFileEntry()
                {
                    Address  = "127.0.0.2",
                    Hostname = "abc.com"
                },
                new HostFileEntry()
                {
                    Address  = "127.0.0.3",
                    Hostname = "def.com"
                }
            };

            mySM.SetupExistingHostList(entries);

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Add-HfHost");
            psCmd.AddParameter("Hostname", "xyz.com");
            psCmd.AddParameter("Address", "127.0.0.4");

            _powerShell.Commands = psCmd;
            var results = _powerShell.Invoke <HostFileEntry>();

            Assert.IsNotNull(results);
            //Assert.AreEqual(3, results.Count);


            mySM.MockFileService.Verify(h => h.WriteEntries(
                                            It.Is <IEnumerable <HostFileEntry> >(en => en.Any(writeEntry =>
                                                                                              (string.Compare(entries[0].Hostname, writeEntry.Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                                               string.Compare(entries[0].Address, writeEntry.Address, StringComparison.CurrentCultureIgnoreCase) == 0) ||
                                                                                              (string.Compare(entries[1].Hostname, writeEntry.Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                                               string.Compare(entries[1].Address, writeEntry.Address, StringComparison.CurrentCultureIgnoreCase) == 0) ||
                                                                                              (string.Compare("xyz.com", writeEntry.Hostname, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                                               string.Compare("127.0.0.4", writeEntry.Address, StringComparison.CurrentCultureIgnoreCase) == 0)))));


            // END FUNCTION
        }