Beispiel #1
0
        public PropertyBag EditEntries(PropertyBag bag)
        {
            return(CatchCommonExceptions(() =>
            {
                EditEntriesRequest request = new EditEntriesRequest(bag);

                HostsFile hostsFile = GetHostsFile();

                IEnumerable <HostEntry> hostEntries = hostsFile.Entries;

                for (int i = 0; i < request.ChangedEntries.Count; i++)
                {
                    HostEntry originalEntry = request.OriginalEntries[i];
                    HostEntry changedEntry = request.ChangedEntries[i];

                    HostEntry hostEntry = FindHostEntry(originalEntry, hostEntries);

                    hostEntry.Address = changedEntry.Address;
                    hostEntry.Hostname = changedEntry.Hostname;
                    hostEntry.Comment = changedEntry.Comment;
                    hostEntry.Enabled = changedEntry.Enabled;
                }

                hostsFile.Save();

                return new EditEntriesResponse().ToPropertyBag();
            }));
        }
Beispiel #2
0
        public void Save_AppliesDeletionReorderingDisabling()
        {
            StringResource resource = new StringResource(Resources.ComplexHostsFile_Before);

            HostsFile file = new HostsFile(resource);

            var entry1 = file.Entries.Where(c => c.Hostname == "host1.localhost").First();
            var entry2 = file.Entries.Where(c => c.Hostname == "host2.localhost").First();
            var entry3 = file.Entries.Where(c => c.Hostname == "host3.localhost").First();
            var entry4 = file.Entries.Where(c => c.Hostname == "host4.localhost").First();
            var entry5 = file.Entries.Where(c => c.Hostname == "host5.localhost").First();
            var entry6 = new HostEntry("host6.localhost", "127.0.0.1", "comment 6");

            entry1.Enabled = false;
            entry2.Enabled = true;
            entry3.Enabled = false;

            entry3.SwapLine(entry5); // swap two with a deleted in between
            entry6.SwapLine(entry2); // new swapped with existing

            file.DeleteEntry(entry4);
            file.AddEntry(entry6);

            file.Save();

            string actualOutput = resource.ToString();

            Assert.AreEqual(Resources.ComplexHostsFile_Expected, actualOutput);
        }
Beispiel #3
0
        public void AddHostsFileEntry(IList <HostsFileEntryBase> hostsFileMapEntries)
        {
            var hostsFile = new HostsFile();

            hostsFile.Load(HostsFile.GetDefaultHostsFilePath());

            var addedHosts = false;

            foreach (var commandHostsFileMapEntry in hostsFileMapEntries)
            {
                if (hostsFile.Entries.Any(e => e.RawLine == commandHostsFileMapEntry.ToString()))
                {
                    continue;
                }

                addedHosts = true;
                hostsFile.Add(commandHostsFileMapEntry);
            }

            if (addedHosts)
            {
                hostsFileMapEntries.Insert(0, new HostsFileComment("Added by DIMMY"));
                hostsFileMapEntries.Add(new HostsFileComment("End of DIMMY section "));
            }

            hostsFile.Save(HostsFile.GetDefaultHostsFilePath());
        }
Beispiel #4
0
        protected override void ProcessRecord()
        {
            var hostFile = new HostsFile();

            hostFile.RemoveHost(HostName);
            hostFile.Save();
        }
Beispiel #5
0
        protected override void ProcessRecord()
        {
            var hostFile = new HostsFile();

            hostFile.AddHost(HostName, IpAddress, Comment);
            hostFile.Save();
        }
        public void Save_WhenHasUnsavedChanges_ShouldClearHasUnsavedChanges()
        {
            var hosts = new HostsFile(_mockHandler.Object)
            {
                HasUnsavedChanges = true
            };

            hosts.Save();

            Assert.False(hosts.HasUnsavedChanges, "Expected HasUnsavedChanges to be false, but it was true.");
        }
        public void Save_WhenHasUnsavedChanges_ShouldSaveFileUsingFileHandler()
        {
            var hosts = new HostsFile(_mockHandler.Object)
            {
                HasUnsavedChanges = true
            };

            hosts.Save();

            _mockHandler.Verify(mock => mock.Write(It.IsAny <IEnumerable <string> >()), Times.Once);
        }
Beispiel #8
0
            public void will_set_IsDirty_to_false()
            {
                var hostFilePath = Path.Combine(Path.GetTempPath(), "hosts");

                var hostFile = new HostsFile();

                hostFile.Set("127.0.0.1", "test.com");

                Assert.True(hostFile.IsDirty);

                XHostConfig.Instance.HostsFilePath = hostFilePath;
                hostFile.Save();

                Assert.False(hostFile.IsDirty);
            }
Beispiel #9
0
        public void Save_AppliesEnabled()
        {
            StringResource resource = new StringResource(Resources.SampleHostsFile);

            HostsFile file = new HostsFile(resource);

            file.Entries.Where(c => c.Hostname == "host1.localhost").First().Enabled = false;
            file.Entries.Where(c => c.Hostname == "host2.localhost").First().Enabled = true;

            file.Save();

            string actualOutput = resource.ToString();

            Assert.AreEqual(Resources.SampleHostsFile_Disable, actualOutput);
        }
Beispiel #10
0
        public void Save_AppliesDeletion()
        {
            StringResource resource = new StringResource(Resources.SampleHostsFile);

            HostsFile file = new HostsFile(resource);

            var entryA = file.Entries.Where(c => c.Hostname == "host1.localhost").First();

            file.DeleteEntry(entryA);

            file.Save();

            string actualOutput = resource.ToString();

            Assert.AreEqual(Resources.SampleHostsFile_Delete, actualOutput);
        }
Beispiel #11
0
        public void Save_AppliesReordering()
        {
            StringResource resource = new StringResource(Resources.SampleHostsFile);

            HostsFile file = new HostsFile(resource);

            var entryA = file.Entries.Where(c => c.Hostname == "host1.localhost").First();
            var entryB = file.Entries.Where(c => c.Hostname == "host2.localhost").First();

            entryA.SwapLine(entryB);

            file.Save();

            string actualOutput = resource.ToString();

            Assert.AreEqual(Resources.SampleHostsFile_Reorder, actualOutput);
        }
Beispiel #12
0
        public PropertyBag AddEntries(PropertyBag bag)
        {
            return(CatchCommonExceptions(() =>
            {
                AddEntriesRequest request = new AddEntriesRequest(bag);

                IList <HostEntry> hostEntries = request.Entries;

                HostsFile hostsFile = GetHostsFile();

                foreach (HostEntry hostEntry in hostEntries)
                {
                    hostsFile.AddEntry(hostEntry);
                }

                hostsFile.Save();

                return new AddEntriesResponse().ToPropertyBag();
            }));
        }
Beispiel #13
0
        public PropertyBag DeleteEntries(PropertyBag bag)
        {
            return(CatchCommonExceptions(() =>
            {
                DeleteEntriesRequest request = new DeleteEntriesRequest(bag);

                HostsFile hostsFile = GetHostsFile();

                IEnumerable <HostEntry> hostEntries = hostsFile.Entries;

                foreach (HostEntry remoteEntry in request.Entries)
                {
                    HostEntry localEntry = FindHostEntry(remoteEntry, hostEntries);

                    hostsFile.DeleteEntry(localEntry);
                }

                hostsFile.Save();

                return new DeleteEntriesResponse().ToPropertyBag();
            }));
        }
Beispiel #14
0
            public void will_set_IsDirty_to_false()
            {
                var hostFilePath = Path.Combine(Path.GetTempPath(), "hosts");

                var hostFile = new HostsFile();
                hostFile.Set("127.0.0.1", "test.com");

                Assert.True(hostFile.IsDirty);

                XHostConfig.Instance.HostsFilePath = hostFilePath;
                hostFile.Save();

                Assert.False(hostFile.IsDirty);
            }