public static void NameResolutionOption_ConvertToByte_Test(bool reorder) { NameResolutionOption preOption = new NameResolutionOption(); NameResolutionOption postOption; preOption.Comment = "Test Comment"; preOption.DnsName = "Dns Name"; preOption.DnsIp4Addr = new IPAddress(new byte[] { 127, 0, 0, 1 }); preOption.DnsIp6Addr = new IPAddress(new byte[] { 0x20, 0x01, 0x0d, 0x0db, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14, 0x28, 0x57, 0xab }); byte[] preOptionByte = preOption.ConvertToByte(reorder, null); using (MemoryStream stream = new MemoryStream(preOptionByte)) { using (BinaryReader binaryReader = new BinaryReader(stream)) { postOption = NameResolutionOption.Parse(binaryReader, reorder, null); } } Assert.IsNotNull(postOption); Assert.AreEqual(preOption.Comment, postOption.Comment); Assert.AreEqual(preOption.DnsName, postOption.DnsName); Assert.AreEqual(preOption.DnsIp4Addr, postOption.DnsIp4Addr); Assert.AreEqual(preOption.DnsIp6Addr, postOption.DnsIp6Addr); }
public static NameResolutionOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException) { CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null"); NameResolutionOption option = new NameResolutionOption(); List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException); if (optionsList.Any()) { foreach (var item in optionsList) { try { switch (item.Key) { case (ushort)NameResolutionOptionCode.CommentCode: option.Comment = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)NameResolutionOptionCode.DnsNameCode: option.DnsName = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)NameResolutionOptionCode.DnsIp4AddrCode: if (item.Value.Length == 4) { option.DnsIp4Addr = new IPAddress(item.Value); } else { throw new ArgumentException(string.Format("[NameResolutionOption.Parse] DnsIp4AddrCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4)); } break; case (ushort)NameResolutionOptionCode.DnsIp6AddrCode: if (item.Value.Length == 16) { option.DnsIp6Addr = new IPAddress(item.Value); } else { throw new ArgumentException(string.Format("[NameResolutionOption.Parse] DnsIp6AddrCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 16)); } break; case (ushort)NameResolutionOptionCode.EndOfOptionsCode: default: break; } } catch (Exception exc) { if (ActionOnException != null) { ActionOnException(exc); } } } } return(option); }