Exemple #1
0
        public void Roundtrip_NoOptions()
        {
            var a = new OPTRecord();
            var b = (OPTRecord) new ResourceRecord().Read(a.ToByteArray());

            Assert.AreEqual(a.Name, b.Name);
            Assert.AreEqual(a.Class, b.Class);
            Assert.AreEqual(a.Type, b.Type);
            Assert.AreEqual(a.TTL, b.TTL);
        }
        byte[] BuildRequest(Message request)
        {
            // Always have a query ID.
            if (request.Id == 0)
            {
                request.Id = this.NextQueryId();
            }

            // Add an OPT if not already present.
            var opt = request.AdditionalRecords.OfType <OPTRecord>().FirstOrDefault();

            if (opt == null)
            {
                opt = new OPTRecord();
                request.AdditionalRecords.Add(opt);
            }

            // Keep the connection alive.
            if (!opt.Options.Any(o => o.Type == EdnsOptionType.Keepalive))
            {
                var keepalive = new EdnsKeepaliveOption
                {
                    Timeout = TimeSpan.FromMinutes(2)
                };
                opt.Options.Add(keepalive);
            }
            ;

            // Always use padding. Must be the last transform.
            if (!opt.Options.Any(o => o.Type == EdnsOptionType.Padding))
            {
                var paddingOption = new EdnsPaddingOption();
                opt.Options.Add(paddingOption);
                var need = BlockLength - ((request.Length() + 2) % BlockLength);
                if (need > 0)
                {
                    paddingOption.Padding = new byte[need];
                    rng.NextBytes(paddingOption.Padding);
                }
            }
            ;

            using (var tcpRequest = new MemoryStream())
            {
                tcpRequest.WriteByte(0);   // two byte length prefix
                tcpRequest.WriteByte(0);
                request.Write(tcpRequest); // udpRequest
                var length = (ushort)(tcpRequest.Length - 2);
                tcpRequest.Position = 0;
                tcpRequest.WriteByte((byte)(length >> 8));
                tcpRequest.WriteByte((byte)(length));
                return(tcpRequest.ToArray());
            }
        }
Exemple #3
0
        public void Defaults()
        {
            var opt = new OPTRecord();

            Assert.AreEqual("", opt.Name);
            Assert.AreEqual(1280, opt.RequestorPayloadSize);
            Assert.AreEqual((ushort)opt.Class, opt.RequestorPayloadSize);
            Assert.AreEqual(0, opt.Opcode8);
            Assert.AreEqual(0, opt.Version);
            Assert.AreEqual(false, opt.DO);
            Assert.AreEqual(0, opt.Options.Count);
        }
Exemple #4
0
        public void Equality()
        {
            var a = new OPTRecord
            {
            };
            var b = new OPTRecord
            {
                RequestorPayloadSize = 512
            };

            Assert.IsTrue(a.Equals(a));
            Assert.IsFalse(a.Equals(b));
            Assert.IsFalse(a.Equals(null));
        }
Exemple #5
0
        public void Roundtrip_Empty()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsKeepaliveOption();

            Assert.AreEqual(EdnsOptionType.Keepalive, expected.Type);
            Assert.AreEqual(false, expected.Timeout.HasValue);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsKeepaliveOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            Assert.AreEqual(expected.Timeout.HasValue, actual.Timeout.HasValue);
        }
Exemple #6
0
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsNSIDOption
            {
                Id = new byte[] { 1, 2, 3, 4 }
            };

            Assert.AreEqual(EdnsOptionType.NSID, expected.Type);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsNSIDOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            CollectionAssert.AreEqual(expected.Id, actual.Id);
        }
Exemple #7
0
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsDAUOption
            {
                Algorithms = { SecurityAlgorithm.ED25519, SecurityAlgorithm.ECCGOST }
            };

            Assert.AreEqual(EdnsOptionType.DAU, expected.Type);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsDAUOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            CollectionAssert.AreEqual(expected.Algorithms, actual.Algorithms);
        }
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsN3UOption
            {
                Algorithms = { DigestType.GostR34_11_94, DigestType.Sha512 }
            };

            Assert.AreEqual(EdnsOptionType.N3U, expected.Type);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsN3UOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            CollectionAssert.AreEqual(expected.Algorithms, actual.Algorithms);
        }
Exemple #9
0
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsKeepaliveOption
            {
                Timeout = TimeSpan.FromSeconds(3)
            };

            Assert.AreEqual(EdnsOptionType.Keepalive, expected.Type);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsKeepaliveOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            Assert.AreEqual(expected.Timeout.Value, actual.Timeout.Value);
        }
Exemple #10
0
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new UnknownEdnsOption
            {
                Type = EdnsOptionType.ExperimentalMin,
                Data = new byte[] { 10, 11, 12 }
            };

            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (UnknownEdnsOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            CollectionAssert.AreEqual(expected.Data, actual.Data);
        }
        public void Roundtrip()
        {
            var opt1     = new OPTRecord();
            var expected = new EdnsPaddingOption
            {
                Padding = new byte[] { 0, 0, 0 }
            };

            Assert.AreEqual(EdnsOptionType.Padding, expected.Type);
            opt1.Options.Add(expected);

            var opt2   = (OPTRecord) new ResourceRecord().Read(opt1.ToByteArray());
            var actual = (EdnsPaddingOption)opt2.Options[0];

            Assert.AreEqual(expected.Type, actual.Type);
            CollectionAssert.AreEqual(expected.Padding, actual.Padding);
        }
Exemple #12
0
        public void Roundtrip()
        {
            var a = new OPTRecord
            {
                RequestorPayloadSize = 512,
                Opcode8 = 2,
                Version = 3,
                DO      = true
            };
            var b = (OPTRecord) new ResourceRecord().Read(a.ToByteArray());

            Assert.AreEqual(a.Name, b.Name);
            Assert.AreEqual(a.Class, b.Class);
            Assert.AreEqual(a.Type, b.Type);
            Assert.AreEqual(a.TTL, b.TTL);
            Assert.AreEqual(a.RequestorPayloadSize, b.RequestorPayloadSize);
            Assert.AreEqual(a.Opcode8, b.Opcode8);
            Assert.AreEqual(a.Version, b.Version);
            Assert.AreEqual(a.DO, b.DO);
        }