Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions DNS/Protocol/ResourceRecords/TextResourceRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,22 @@ public TextResourceRecord(Domain domain, string attributeName, string attributeV

public IList<CharacterString> TextData { get; }

public KeyValuePair<string, string> Attribute {
get {
string text = ToStringTextData();
Match match = PATTERN_TXT_RECORD.Match(text);

if (match.Success) {
string attributeName = (match.Groups[1].Length > 0) ?
Unescape(Trim(match.Groups[1].ToString())) : null;
string attributeValue = Unescape(match.Groups[2].ToString());
return new KeyValuePair<string, string>(attributeName, attributeValue);
} else {
return new KeyValuePair<string, string>(null, Unescape(text));
}
}
public IEnumerable<KeyValuePair<string, string>> Attributes {
get =>
this.TextData.Select(chrStr =>
{
var text = chrStr.ToString();
Match match = PATTERN_TXT_RECORD.Match(text);

if (match.Success) {
string attributeName = (match.Groups[1].Length > 0) ?
Unescape(Trim(match.Groups[1].ToString())) : null;
string attributeValue = Unescape(match.Groups[2].ToString());
return new KeyValuePair<string, string>(attributeName, attributeValue);
} else {
return new KeyValuePair<string, string>(null, Unescape(text));
}
});
}

public string ToStringTextData() {
Expand All @@ -81,7 +83,8 @@ public string ToStringTextData(Encoding encoding) {
}

public override string ToString() {
return Stringify().Add("TextData", (object) ToStringTextData()).ToString();
var list = String.Join(" ", TextData.Select(c => c.ToString()));
return Stringify().Add("TextData", (object) list).ToString();
}
}
}
5 changes: 3 additions & 2 deletions Tests/Protocol/ResourceRecords/TextResourceRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Xunit;
using DNS.Protocol.ResourceRecords;
using DNS.Protocol;
using System.Linq;

namespace DNS.Tests.Protocol.ResourceRecords {

Expand All @@ -21,7 +22,7 @@ public class TextResourceRecordTest {
[InlineData("abc` =123 ", "abc ", "123 ")]
public void Rfc1464Examples(string internalForm, string expAttributeName, string expAttributeValue) {
TextResourceRecord record = new TextResourceRecord(new ArrayTextResourceRecord(internalForm));
KeyValuePair<string, string> attribute = record.Attribute;
KeyValuePair<string, string> attribute = record.Attributes.First();
Assert.Equal(expAttributeName, attribute.Key);
Assert.Equal(expAttributeValue, attribute.Value);
}
Expand All @@ -32,7 +33,7 @@ public void Rfc1464Examples(string internalForm, string expAttributeName, string
[InlineData("", "", null, "")]
public void NegativeExamples(string input, string expTxtData, string expAttributeName, string expAttributeValue) {
TextResourceRecord record = new TextResourceRecord(new ArrayTextResourceRecord(input));
KeyValuePair<string, string> attribute = record.Attribute;
KeyValuePair<string, string> attribute = record.Attributes.First();

Assert.Equal(expTxtData, record.ToStringTextData());
Assert.Equal(expAttributeName, attribute.Key);
Expand Down