Hosts File Utility Part II
Monday, May 04 2009 - software-development
In Part I, here, I talked about creating a utility to help me with constant host entry changes. I have to report that I used it throughout the day today and I am very pleased with how much easier it made my life. I did have one usability issue: I swapped the IP address with the Domain and the result was all IP address matches were being changed, but the domain was ignored. Once I figured out that I was doing it backwards, it worked as expected.
As with everything I write, I used Unit Tests to drive the development. My tests are rudimentary, but they did the job of making sure each class of the utility operated correctly. Once I had all the parts working, putting them together was a snap.
I will caution that some of this code is rather ugly and not what I could call “production” quality. The names of the classes are confusing at best [ how many things can you call host?]. As a general rule, I allow my unit tests to shield me from these types of things and during the second pass, post working, I refactor, rename and generally deal with the ugly stuff.
My project hierarchy
The Program.cs controls everything, the Params.cs class parses the params passed to the Program.cs and determines what action is to be taken, HostEntryParam.cs holds the parsed params (IPAddress and or Domain ), the HostEntry represents a single line in the hosts file. The HostEntries is just a utility class that holds and operates on the collection of HostEntry(s).
Param class Unit Tests
[TestFixture]
public class ParamTests
{
[Test]
public void ParseParamsTest()
{
var hostParams = new Params(new string[2] {"-l", "www.codeverity.com"});
var expectedCount= 2;
Assert.AreEqual(expectedCount, hostParams.Count);
hostParams = new Params(new string[3] { "-l", "www.codeverity.com", "127." });
expectedCount = 3;
Assert.AreEqual(expectedCount, hostParams.Count);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void ParseParamsTestInvalid()
{
var hostParams = new Params(new string[4] { "-l", "www.test.com", "127.", "www.test2.com" });
var expectedCount = 4;
Assert.AreEqual(expectedCount, hostParams.Count);
}
[Test]
public void ParseParamsTestInvalidDefault()
{
var hostParams = new Params(new string[1] { "-x" });
var expectedCount = 1;
Assert.AreEqual(expectedCount, hostParams.Count);
Assert.AreEqual(hostParams.CurrentAction ,Params.HostsAction.List);
}
}
HostEntryParam class Unit Tests
[TestFixture]
public class HostEntryParamTests
{
[Test]
public void HostEntryValidTest()
{
HostEntryParam hostParam = new HostEntryParam("127.0");
Assert.AreEqual(true, hostParam.IsMatch("127.0.0.1"));
hostParam = new HostEntryParam("127.");
Assert.AreEqual(true, hostParam.IsMatch("127.0.0.1"));
hostParam = new HostEntryParam("127.0.0");
Assert.AreEqual(true, hostParam.IsMatch("127.0.0.1"));
hostParam = new HostEntryParam("127.0.0.1");
Assert.AreEqual(true, hostParam.IsMatch("127.0.0.1"));
}
[Test]
public void HostEntryMatchStringTest()
{
HostEntryParam hostParam = new HostEntryParam("168.1.12");
Assert.AreEqual("168.1.12", hostParam.MatchString);
hostParam = new HostEntryParam("168.");
Assert.AreEqual("168.", hostParam.MatchString);
hostParam = new HostEntryParam("168.1.12.4");
Assert.AreEqual("168.1.12.4", hostParam.MatchString);
}
[Test]
public void HostEntryInvalidTest()
{
HostEntryParam hostParam = new HostEntryParam("168.1.12");
Assert.AreEqual(false, hostParam.IsMatch("127.0.0.1"));
Assert.AreEqual(false, hostParam.IsMatch("168.0.0.1"));
Assert.AreEqual(false, hostParam.IsMatch("168.1.4.0"));
Assert.AreEqual(false, hostParam.IsMatch("168.1.122.99"));
Assert.AreEqual(false, hostParam.IsMatch("168.1.122.100"));
}
}
HostEntry class Tests
[TestFixture]
public class HostEntryTests
{
[Test]
public void ReadHostEntryTest()
{
HostEntry entry = new HostEntry("#192.11.2.1 www.codeverity.com ");
Assert.AreEqual(true, entry.IsCommented);
Assert.AreEqual("192.11.2.1", entry.IPAddress);
Assert.AreEqual("www.codeverity.com", entry.Domain);
entry = new HostEntry("#192.11.2.1 www.codeverity.com ");
Assert.AreEqual(true, entry.IsCommented);
Assert.AreEqual("192.11.2.1", entry.IPAddress);
Assert.AreEqual("www.codeverity.com", entry.Domain);
entry = new HostEntry(" 192.11.2.1 www.codeverity.com ");
Assert.AreEqual(false, entry.IsCommented);
Assert.AreEqual("192.11.2.1", entry.IPAddress);
Assert.AreEqual("www.codeverity.com", entry.Domain);
entry = new HostEntry("192.11.2.1 www.codeverity.com");
Assert.AreEqual(false, entry.IsCommented);
Assert.AreEqual("192.11.2.1", entry.IPAddress);
Assert.AreEqual("www.codeverity.com", entry.Domain);
}
}
HostProcessor class Tests
[TestFixture]
public class HostProcessorTests
{
[Test]
public void HostProcessorActionTest()
{
var hostParams = new Params(new string[]{"-l","127.0"});
var hostEntries = new HostEntries();
IList<HostEntryParam> hostEntryParams = hostParams.HostEntryParams;
var processor = new HostProcessor(hostParams, hostEntries, hostEntryParams);
processor.ProcessAction();
}
[Test]
public void HostProcessorUnCommentActionTest()
{
var hostParams = new Params(new string[] { "-c", ”.testme.com" });
var hostEntries = new HostEntries();
var hostEntryParams = hostParams.HostEntryParams;
var processor = new HostProcessor(hostParams, hostEntries, hostEntryParams);
processor.ProcessAction();
}
}
Not much too it really, once you figure out the screwy names I used.
Next post I’ll put up the actual classes and talk about what I thought worked well and what didn’t. I also want to run NDepend on the utility to see what it says is wrong.
