Skip to content

Commit

Permalink
feat(MasterFile): handle undefined class ("CLASSxxx")
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 18, 2018
1 parent 6511632 commit 98d2c55
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/MasterReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public ResourceRecord ReadResourceRecord()
}

// Is CLASS?
if (token.StartsWith("CLASS"))
{
klass = (Class)ushort.Parse(token.Substring(5), CultureInfo.InvariantCulture);
continue;
}
if (Enum.TryParse<Class>(token, out Class k))
{
klass = k;
Expand Down
7 changes: 7 additions & 0 deletions src/ResourceRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,17 @@ public void Write(TextWriter writer)
writer.Write((int)TTL.TotalSeconds);
writer.Write(' ');
}

if (!Enum.IsDefined(typeof(Class), Class))
{
writer.Write("CLASS");
}
writer.Write(Class);
writer.Write(' ');

writer.Write(Type);
writer.Write(' ');

WriteData(writer);
writer.Write("\r\n");
}
Expand Down
11 changes: 11 additions & 0 deletions test/MasterReaaderTest.cs → test/MasterReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public void ReadResourceWithClassAndTTL()
Assert.IsInstanceOfType(resource, typeof(ARecord));
}

[TestMethod]
public void ReadResourceWithUnknownClass()
{
var reader = new MasterReader(new StringReader("me CLASS1234 A 127.0.0.1"));
var resource = reader.ReadResourceRecord();
Assert.AreEqual("me", resource.Name);
Assert.AreEqual(1234, (int)resource.Class);
Assert.AreEqual(DnsType.A, resource.Type);
Assert.IsInstanceOfType(resource, typeof(ARecord));
}

[TestMethod]
public void ReadResourceWithComment()
{
Expand Down
15 changes: 14 additions & 1 deletion test/ResourceRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,18 @@ public void IsExpired()
Assert.IsFalse(rr.IsExpired(DateTime.Now + TimeSpan.FromSeconds(-3)));
Assert.IsTrue(rr.IsExpired(DateTime.Now + TimeSpan.FromSeconds(3)));
}

[TestMethod]
public void Stringing_UnknownClass()
{
var a = new ResourceRecord
{
Name = "x.emanon.org",
Class = (Class)1234,
Type = DnsType.A
};
Assert.AreEqual("x.emanon.org CLASS1234 A", a.ToString());

}
}
}
}

0 comments on commit 98d2c55

Please sign in to comment.