Skip to content

Commit

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

// Is TYPE?
if (token.StartsWith("TYPE"))
{
type = (DnsType)ushort.Parse(token.Substring(4), CultureInfo.InvariantCulture);
continue;
}
if (Enum.TryParse<DnsType>(token, out DnsType t))
{
type = t;
Expand Down
4 changes: 4 additions & 0 deletions src/ResourceRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ public void Write(TextWriter writer)
writer.Write(Class);
writer.Write(' ');

if (!Enum.IsDefined(typeof(DnsType), Type))
{
writer.Write("TYPE");
}
writer.Write(Type);
writer.Write(' ');

Expand Down
11 changes: 11 additions & 0 deletions test/MasterReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public void ReadResourceWithUnknownClass()
Assert.IsInstanceOfType(resource, typeof(ARecord));
}

[TestMethod]
public void ReadResourceWithUnknownType()
{
var reader = new MasterReader(new StringReader("me CH TYPE1234"));
var resource = reader.ReadResourceRecord();
Assert.AreEqual("me", resource.Name);
Assert.AreEqual(Class.CH, resource.Class);
Assert.AreEqual(1234, (int)resource.Type);
Assert.IsInstanceOfType(resource, typeof(UnknownRecord));
}

[TestMethod]
public void ReadResourceWithComment()
{
Expand Down
10 changes: 10 additions & 0 deletions test/ResourceRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,17 @@ public void Stringing_UnknownClass()
Type = DnsType.A
};
Assert.AreEqual("x.emanon.org CLASS1234 A", a.ToString());
}

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

0 comments on commit a7e8327

Please sign in to comment.