Exemplo n.º 1
0
        public void Constructor_SetsDefaultsCorrectly()
        {
            var hostEntry = new HostEntry();

            Assert.Equal(HostEntry.LocalHostIpAddress, hostEntry.IpAddress);
            Assert.True(hostEntry.IsEnabled);
        }
        public IHostsConfigurer AddHostEntry(Action<IHostEntryConfigurer> configurer)
        {
            var entry = new HostEntry();
            configurer(new HostEntryConfigurer(entry));
            HostsFile.AddHostEntry(entry);
            Logger.Info(entry.ContructAddMessage);

            return this;
        }
Exemplo n.º 3
0
        public IHostsConfigurer AddHostEntry(Action <IHostEntryConfigurer> configurer)
        {
            var entry = new HostEntry();

            configurer(new HostEntryConfigurer(entry));
            HostsFile.AddHostEntry(entry);
            Logger.Info(entry.ContructAddMessage);

            return(this);
        }
        public void AddHostEntry_ShouldNotAddDuplicateEntriesWithTheSameHostNameAndIp()
        {
            var sut = new HostsFile();
            var hostEntry1 = new HostEntry() { HostName = "mysite.co.uk", IsEnabled = true, IpAddress = "192.168.2.1"};
            var hostEntry2 = new HostEntry() { HostName = "mysite.co.uk", IsEnabled = true, IpAddress = "192.168.2.1" };

            sut.AddHostEntry(hostEntry1);
            sut.AddHostEntry(hostEntry2);

            Assert.Equal(1, sut.AllHostEntries().Count());
            Assert.True(sut.AllHostEntries().Last().IsEnabled);
        }
        public void ToString_ReturnsCorrectValueWhenEnabledWithNoDescription()
        {
            var hostEntry = new HostEntry();
            hostEntry.IpAddress = "127.0.0.1";
            hostEntry.IsEnabled = true;
            hostEntry.Description = null;
            hostEntry.HostName = "mysite.co.uk";

            var actual = hostEntry.ToString();
            var expected = "127.0.0.1       mysite.co.uk";

            Assert.Equal(expected, actual);
        }
        public void ToString_ReturnsCorrectValueWhenDisabledWithDescription()
        {
            var hostEntry = new HostEntry();
            hostEntry.IpAddress = "127.0.0.1";
            hostEntry.IsEnabled = false;
            hostEntry.Description = "Some description";
            hostEntry.HostName = "mysite.co.uk";

            var actual = hostEntry.ToString();
            var expected = "# 127.0.0.1       mysite.co.uk\t# Some description";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 7
0
        public void TryParse_ParsesCorrectlyWhenValueIsCommentedOutAndHasDescription()
        {
            HostEntry hostEntry = default(HostEntry);
            var       value     = "#127.0.0.1       mysite.co.uk # This is a description";

            var actual = HostEntry.TryParse(value, out hostEntry);

            Assert.True(actual);
            Assert.Equal("127.0.0.1", hostEntry.IpAddress);
            Assert.Equal("mysite.co.uk", hostEntry.HostName);
            Assert.Equal("This is a description", hostEntry.Description);
            Assert.False(hostEntry.IsEnabled);
        }
Exemplo n.º 8
0
        public void TryParse_ParsesCorrectlyWhenValueIsAnUncommentedOutEntryWithNoDescription()
        {
            HostEntry hostEntry = default(HostEntry);
            var       value     = "127.0.0.1       mysite.co.uk";

            var actual = HostEntry.TryParse(value, out hostEntry);

            Assert.True(actual);
            Assert.Equal("127.0.0.1", hostEntry.IpAddress);
            Assert.Equal("mysite.co.uk", hostEntry.HostName);
            Assert.Equal("", hostEntry.Description);
            Assert.True(hostEntry.IsEnabled);
        }
Exemplo n.º 9
0
        public void ToString_ReturnsCorrectValueWhenEnabledWithDescription()
        {
            var hostEntry = new HostEntry();

            hostEntry.IpAddress   = "127.0.0.1";
            hostEntry.IsEnabled   = true;
            hostEntry.Description = "Some description";
            hostEntry.HostName    = "mysite.co.uk";

            var actual   = hostEntry.ToString();
            var expected = "127.0.0.1       mysite.co.uk\t# Some description";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 10
0
        public void RemoveHostEntry_RemovesEntryFromHostFile()
        {
            var sut   = new HostsFile();
            var entry = new HostEntry()
            {
                HostName = "mysite.co.uk"
            };

            sut.AddHostEntry(entry);

            sut.RemoveHostEntry(entry);

            Assert.Empty(sut.AllHostEntries());
        }
Exemplo n.º 11
0
        public void ToString_ReturnsCorrectValueWhenDisabledWithNoDescription()
        {
            var hostEntry = new HostEntry();

            hostEntry.IpAddress   = "127.0.0.1";
            hostEntry.IsEnabled   = false;
            hostEntry.Description = null;
            hostEntry.HostName    = "mysite.co.uk";

            var actual   = hostEntry.ToString();
            var expected = "# 127.0.0.1       mysite.co.uk";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 12
0
        public void AddHostEntry_ShouldNotAddDuplicateEntriesWithTheSameHostNameAndIp()
        {
            var sut        = new HostsFile();
            var hostEntry1 = new HostEntry()
            {
                HostName = "mysite.co.uk", IsEnabled = true, IpAddress = "192.168.2.1"
            };
            var hostEntry2 = new HostEntry()
            {
                HostName = "mysite.co.uk", IsEnabled = true, IpAddress = "192.168.2.1"
            };

            sut.AddHostEntry(hostEntry1);
            sut.AddHostEntry(hostEntry2);

            Assert.Equal(1, sut.AllHostEntries().Count());
            Assert.True(sut.AllHostEntries().Last().IsEnabled);
        }
Exemplo n.º 13
0
        public HostEntry AddHostEntry(HostEntry hostEntry)
        {
            var found = FindHostEntry(hostEntry.HostName);

            foreach (HostEntry entry in FindHostEntry(hostEntry.HostName))
            {
                entry.IsEnabled = false;
            }

            var duplicateEntry = found.FirstOrDefault(x => x.IpAddress.Equals(hostEntry.IpAddress));

            if (duplicateEntry == null)
            {
                _hostEntries.Add(hostEntry);
            }
            else
            {
                duplicateEntry.IsEnabled = true;
            }

            return hostEntry;
        }
Exemplo n.º 14
0
        public HostEntry AddHostEntry(HostEntry hostEntry)
        {
            var found = FindHostEntry(hostEntry.HostName);

            foreach (HostEntry entry in FindHostEntry(hostEntry.HostName))
            {
                entry.IsEnabled = false;
            }

            var duplicateEntry = found.FirstOrDefault(x => x.IpAddress.Equals(hostEntry.IpAddress));

            if (duplicateEntry == null)
            {
                _hostEntries.Add(hostEntry);
            }
            else
            {
                duplicateEntry.IsEnabled = true;
            }

            return(hostEntry);
        }
Exemplo n.º 15
0
        public static bool TryParse(string value, out HostEntry hostEntry)
        {
            const string matchPattern = @"^(?<IpAddress>[0-9a-f.:]+)\s+(?<HostName>[^\s#]+)(?<Description>.*)$";

            hostEntry = new HostEntry();

            var regex = new Regex(matchPattern);
            var match = regex.Match(value.TrimStart('#'));

            if (match.Success)
            {
                hostEntry = new HostEntry()
                {
                    IpAddress   = match.Groups["IpAddress"].Value,
                    HostName    = match.Groups["HostName"].Value,
                    Description = match.Groups["Description"].Value.Replace("#", string.Empty).Trim(),
                    IsEnabled   = !value.StartsWith("#")
                };
            }

            return(match.Success);
        }
Exemplo n.º 16
0
        public static bool TryParse(string value, out HostEntry hostEntry)
        {
            const string matchPattern = @"^(?<IpAddress>[0-9a-f.:]+)\s+(?<HostName>[^\s#]+)(?<Description>.*)$";

            hostEntry = new HostEntry();

            var regex = new Regex(matchPattern);
            var match = regex.Match(value.TrimStart('#'));

            if (match.Success)
            {
               hostEntry = new HostEntry()
               {
                   IpAddress = match.Groups["IpAddress"].Value,
                   HostName = match.Groups["HostName"].Value,
                   Description = match.Groups["Description"].Value.Replace("#", string.Empty).Trim(),
                   IsEnabled = !value.StartsWith("#")
               };
            }

            return match.Success;
        }
Exemplo n.º 17
0
        public static HostsFile Load(Stream stream)
        {
            var hostsFile = new HostsFile();

            var reader = new StreamReader(stream);

            while (reader.Peek() >= 0)
            {
                string line = reader.ReadLine();

                HostEntry hostEntry;

                if (HostEntry.TryParse(line, out hostEntry))
                {
                    hostsFile.AddHostEntry(hostEntry);
                }
                else
                {
                    hostsFile.AddComment(line.TrimStart('#'));
                }
            }

            return(hostsFile);
        }
 public void Constructor_SetsDefaultsCorrectly()
 {
     var hostEntry = new HostEntry();
     Assert.Equal(HostEntry.LocalHostIpAddress, hostEntry.IpAddress);
     Assert.True(hostEntry.IsEnabled);
 }
        public void RemoveHostEntry_RemovesEntryFromHostFile()
        {
            var sut = new HostsFile();
            var entry = new HostEntry(){HostName = "mysite.co.uk"};
            sut.AddHostEntry(entry);

            sut.RemoveHostEntry(entry);

            Assert.Empty(sut.AllHostEntries());
        }
Exemplo n.º 20
0
 public void RemoveHostEntry(HostEntry entry)
 {
     _hostEntries.Remove(entry);
 }
 public HostEntryConfigurer(HostEntry hostEntry)
 {
     _hostEntry = hostEntry;
 }
Exemplo n.º 22
0
 public void RemoveHostEntry(HostEntry entry)
 {
     _hostEntries.Remove(entry);
 }
 public HostEntryConfigurer(HostEntry hostEntry)
 {
     _hostEntry = hostEntry;
 }