public void Instantiation_FromInvalidLine_ShouldInitializeLineWithoutChange()
        {
            const string invalid = "192.168.1.256\t #Comment";
            var          entry   = new HostsEntry(invalid);

            Assert.Equal(invalid, entry.Line);
        }
        public void Instantiation_ShouldInitializeAddressString()
        {
            var entry = new HostsEntry(TestAddress, TestHostName, TestComment);

            Assert.NotNull(entry.AddressString);
            Assert.Equal(TestAddress.ToString(), entry.AddressString);
        }
Esempio n. 3
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.DataGridView.CellFormatting"/> event.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">The value of the
        /// <see cref="P:System.Windows.Forms.DataGridViewCellFormattingEventArgs.ColumnIndex"/>
        /// property of <paramref name="e"/> is greater than the number of
        /// columns in the control minus one.-or-The value of the
        /// <see cref="P:System.Windows.Forms.DataGridViewCellFormattingEventArgs.RowIndex"/>
        /// property of <paramref name="e"/> is greater than the number of
        /// rows in the control minus one.</exception>
        protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
        {
            var        viewObject = this.Rows[e.RowIndex].DataBoundItem as ObjectView <HostsEntry>;
            HostsEntry entry      = viewObject != null ? viewObject.Object : null;

            if (entry != null)
            {
                if (!entry.Enabled && entry.Valid)
                {
                    e.CellStyle.BackColor = Color.LightGray;
                }
                else if (!entry.Enabled)
                {
                    e.CellStyle.BackColor = Color.Gray;
                    e.CellStyle.ForeColor = Color.White;
                }
                else if (!entry.Valid)
                {
                    e.CellStyle.BackColor = Color.LightPink;
                }
                else
                {
                    e.CellStyle.BackColor = Color.White;
                }
            }

            base.OnCellFormatting(e);
        }
        public void ToString_ShouldReturnLine()
        {
            var entry = new HostsEntry(TestLineSpaceDelimited);

            var result = entry.ToString();

            Assert.Equal(entry.Line, result);
        }
        public void Instantiation_WithVariousInvalidLines_ShouldReturnExpectedAddress(string line, string ip)
        {
            var expected = IPAddress.Parse(ip);

            var entry = new HostsEntry(line);

            Assert.Equal(expected, entry.Address);
        }
        public void ToDtoCreatesADto()
        {
            HostsEntry    model = GetTestModel();
            HostsEntryDto dto   = HostsEntryConverter.ToDto(new[] { model });

            Assert.Equal(model.Host, dto.Host);
            Assert.Equal(model.IpAddress, IPAddress.Parse(dto.IpAddresses[0]));
        }
        public void ToDtosCreatesDtos()
        {
            HostsEntry model = GetTestModel();

            HostsEntryDto[] dtos = HostsEntryConverter.ToDtos(new[] { model, model });
            Assert.Equal(model.Host, dtos[0].Host);
            Assert.Equal(model.IpAddress, IPAddress.Parse(dtos[0].IpAddresses[0]));
            Assert.Equal(model.IpAddress, IPAddress.Parse(dtos[0].IpAddresses[1]));
        }
Esempio n. 8
0
        internal HostsFileEntries(string[] hostEntries, bool truncate)
        {
            if (hostEntries == null)
            {
                hostEntries = new string[0];
            }

            this.hosts = new Dictionary <string, HostsEntry>(hostEntries.Length);

            if (truncate)
            {
                this.hostsFileLines = new List <string>();
                foreach (var line in hostEntries)
                {
                    if (line.StartsWith("#", StringComparison.OrdinalIgnoreCase))
                    {
                        this.hostsFileLines.Add(line);
                    }
                    else
                    {
                        break;
                    }
                }

                this.hostsFileLines.Add(string.Empty);
                this.SetHostEntry("localhost", "127.0.0.1");
                return;
            }

            this.hostsFileLines = new List <string>(hostEntries);
            var lineNum = 0;

            foreach (var line in this.hostsFileLines)
            {
                var match = this.hostsEntryRegex.Match(line);
                if (match.Success)
                {
                    var hostsEntry    = new HostsEntry(lineNum, match.Groups["HostName"].Value, match.Groups["Tail"].Value);
                    var hostsEntryKey = hostsEntry.HostName.ToLower(CultureInfo.InvariantCulture);
                    if (!this.hosts.ContainsKey(hostsEntryKey))
                    {
                        this.hosts[hostsEntryKey] = hostsEntry;
                    }
                }

                lineNum++;
            }
        }
Esempio n. 9
0
        public void SetHostEntry(string hostName, string ipAddress, string comment)
        {
            string hostsKey  = hostName.ToLower(CultureInfo.InvariantCulture);
            string tail      = string.IsNullOrEmpty(comment) ? null : ("\t# " + comment);
            string hostsLine = PadIPAddress(ipAddress) + Separator + hostName;

            if (this.hosts.ContainsKey(hostsKey))
            {
                HostsEntry hostEntry = this.hosts[hostsKey];
                this.hostsFileLines[hostEntry.LineNumber] = hostsLine + (tail ?? hostEntry.Tail);
            }
            else
            {
                this.hostsFileLines.Add(hostsLine + tail);
                this.hosts[hostsKey] = new HostsEntry(this.hostsFileLines.Count - 1, hostName, tail);
            }
        }
        public void Instantiation_WithVariousInvalidLines_ShouldReturnExpectedLine(string line)
        {
            var entry = new HostsEntry(line);

            Assert.Equal(line, entry.Line);
        }
        internal HostsFileEntries(string[] hostEntries, bool truncate)
        {
            if (hostEntries == null)
            {
                hostEntries = new string[0];
            }

            this.hosts = new Dictionary<string, HostsEntry>(hostEntries.Length);

            if (truncate)
            {
                this.hostsFileLines = new List<string>();
                foreach (var line in hostEntries)
                {
                    if (line.StartsWith("#", StringComparison.OrdinalIgnoreCase))
                    {
                        this.hostsFileLines.Add(line);
                    }
                    else
                    {
                        break;
                    }
                }

                this.hostsFileLines.Add(string.Empty);
                this.SetHostEntry("localhost", "127.0.0.1");
                return;
            }

            this.hostsFileLines = new List<string>(hostEntries);
            var lineNum = 0;
            foreach (var line in this.hostsFileLines)
            {
                var match = this.hostsEntryRegex.Match(line);
                if (match.Success)
                {
                    var hostsEntry = new HostsEntry(lineNum, match.Groups["HostName"].Value, match.Groups["Tail"].Value);
                    var hostsEntryKey = hostsEntry.HostName.ToLower(CultureInfo.InvariantCulture);
                    if (!this.hosts.ContainsKey(hostsEntryKey))
                    {
                        this.hosts[hostsEntryKey] = hostsEntry;
                    }
                }

                lineNum++;
            }
        }
        public void Instantiation_FromLine_ShouldInitializeComment()
        {
            var entry = new HostsEntry(TestLineTabDelimited);

            Assert.Equal(" " + TestComment, entry.Comment);
        }
        public void Instantiation_FromLine_ShouldInitializeLine()
        {
            var entry = new HostsEntry(TestLineTabDelimited);

            Assert.Equal(TestLineTabDelimited, entry.Line);
        }
        public void Instantiation_ShouldInitializeLine()
        {
            var entry = new HostsEntry(TestAddress, TestHostName, TestComment);

            Assert.Equal("192.168.1.1\twww.example.com # Example Comment", entry.Line);
        }
        public void Instantiation_FromLine_ShouldInitializeAddressString()
        {
            var entry = new HostsEntry(TestLineTabDelimited);

            Assert.Equal(TestAddress.ToString(), entry.AddressString);
        }
 private string uiHostsEntry(HostsEntry e) => String.Format("{0,-12} -> {1}", e.domain, e.ip);
        public void Instantiation_ShouldInitializeComment()
        {
            var entry = new HostsEntry(TestAddress, TestHostName, TestComment);

            Assert.Equal(TestComment, entry.Comment);
        }
        public void Instantiation_FromLineWithEmptyComment_ShouldInitializeCommentToEmptyString()
        {
            var entry = new HostsEntry($"{TestAddress}\t{TestHostName} #");

            Assert.Equal(string.Empty, entry.Comment);
        }
        public void Instantiation_FromLineWithSpaceDelimiter_ShouldInitializeAddress()
        {
            var entry = new HostsEntry(TestLineSpaceDelimited);

            Assert.Equal(TestAddress, entry.Address);
        }
        public void Instantiation_WithVariousInvalidLines_ShouldReturnExpectedComment(string line, string expected)
        {
            var entry = new HostsEntry(line);

            Assert.Equal(expected, entry.Comment);
        }
        public void Instantiation_FromLineWithSpaceDelimiter_ShouldInitializeHostName()
        {
            var entry = new HostsEntry(TestLineSpaceDelimited);

            Assert.Equal(TestHostName, entry.HostName);
        }
        public void Instantiation_FromLineWithNoSpaceAroundComment_ShouldInitializeComment()
        {
            var entry = new HostsEntry(TestLineNoSpaceAroundComment);

            Assert.Equal(TestComment, entry.Comment);
        }