コード例 #1
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
        }
コード例 #2
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
        }
コード例 #3
0
        public void Should_ThrowOnMissingHostName()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Remove-HfHost");
            _powerShell.Commands = psCmd;
            _powerShell.Invoke();


            // END FUNCTION
        }
コード例 #4
0
        public void Should_ThrowOnMissingAddress()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Add-HfHost");
            psCmd.AddParameter("Hostname", "abc.com");
            _powerShell.Commands = psCmd;
            _powerShell.Invoke();

            // END FUNCTION
        }
コード例 #5
0
        public void Should_ThrowOnDuplicate()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Add-HfHost");
            psCmd.AddParameter("Hostname", "abc.com");
            psCmd.AddParameter("Address", "127.0.0.2");

            PSCommand psDup = new PSCommand();

            psDup.AddCommand("Add-HfHost");
            psDup.AddParameter("Hostname", "abc.com");
            psDup.AddParameter("Address", "127.0.0.2");

            try
            {
                _powerShell.Commands = psCmd;
                var result = _powerShell.Invoke <HostFileEntry>();

                mySM.MockFileService.Setup(fs => fs.GetEntries()).Returns(
                    new List <HostFileEntry> {
                    result.First()
                });

                _powerShell.Commands.Clear();

                _powerShell.Commands = psDup;
                _powerShell.Invoke();
            }
            catch (CmdletInvocationException cex)
            {
                // testing for what the cmdlet itself actually threw.
                throw cex.InnerException ?? cex;
            }

            // END FUNCTION
        }
コード例 #6
0
        public void Should_ReturnEmptyForMissingHost()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Get-HfHost");
            psCmd.AddParameter("Hostname", "foo.com");

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

            Assert.AreEqual(0, results.Count);


            // END FUNCTION
        }
コード例 #7
0
        public void Should_AddCorrectHostDataToHostFile()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Add-HfHost");
            psCmd.AddParameter("Hostname", "abc.com");
            psCmd.AddParameter("Address", "127.0.0.2");

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

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

            // END FUNCTION
        }
コード例 #8
0
        public void Should_ReturnHostEntry()
        {
            _powerShell.Commands.Clear();
            var mySM = new MockServiceManager();

            ServiceManager.Provider = () => mySM;

            PSCommand psCmd = new PSCommand();

            psCmd.AddCommand("Add-HfHost");
            psCmd.AddParameter("Hostname", "abc.com");
            psCmd.AddParameter("Address", "127.0.0.2");

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

            Assert.IsNotNull(results);
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("abc.com", results.First().Hostname);
            Assert.AreEqual("127.0.0.2", results.First().Address);


            // END FUNCTION
        }