コード例 #1
0
        public void TestGenerateId()
        {
            var randomIds = Enumerable.Range(0, ushort.MaxValue).Select(x => DnsQueryFactory.GenerateId());

            // Check the distribution is between 10% and 100% of the available values
            Assert.InRange(randomIds.Distinct().Count(), ushort.MaxValue / 10, ushort.MaxValue);
        }
コード例 #2
0
        public async Task TestQuery()
        {
            using var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

            var endpoint = GenerateEndPoint();

            // Create a real upstream DNS client to serve the request (todo: mock this)
            using var upstream = new DnsUdpClient(IPAddress.Parse("1.1.1.1"));

            // Create a loopback server
            using var server = new DnsUdpServer(endpoint, upstream);

            // Start recieving
            using var receiveTask = server.Listen(tokenSource.Token);

            // Create a loopback DNS client to talk to the server
            using var client = new DnsUdpClient(endpoint);

            try
            {
                var query = DnsQueryFactory.CreateQuery("google.com");

                // Send a DNS request to the server, verify the results
                var response = await client.Query(query, tokenSource.Token);

                Assert.Equal(query.Id, response.Header.Id);
                Assert.Equal(query.Host, response.Header.Host);
                Assert.True(response.Answers.Count > 0);
            }
            finally
            {
                tokenSource.Cancel();
                await receiveTask;
            }
        }
コード例 #3
0
        public void TestOrAllowedByDefault()
        {
            var dnsFilter = new DnsDomainSuffixFilter("example.com");

            Assert.False(dnsFilter.IsPermitted(DnsQueryFactory.CreateQuery("test.example.com")));
            Assert.True(dnsFilter.IsPermitted(DnsQueryFactory.CreateQuery("test.example.org")));
            Assert.False(dnsFilter.IsPermitted(DnsQueryFactory.CreateQuery("test.example.org.example.com")));
            Assert.True(dnsFilter.IsPermitted(DnsQueryFactory.CreateQuery("test.example.com.example.org")));
        }
コード例 #4
0
        public void TestCreateReverseQueryIPv6()
        {
            var header = DnsQueryFactory.CreateReverseQuery(IPAddress.Parse("2600:9000:2015:3600:1a:36dc:e5c0:93a1"));

            Assert.Equal("1.a.3.9.0.c.5.e.c.d.6.3.a.1.0.0.6.3.5.1.0.2.0.0.0.9.0.0.6.2.ip6.arpa", header.Host);
            Assert.True(header.Id > 0);
            Assert.Equal(DnsQueryClass.IN, header.QueryClass);
            Assert.Equal(DnsQueryType.PTR, header.QueryType);
            Assert.Equal(DnsOperationCode.QUERY, header.OperationCode);
            Assert.Equal(1, header.QuestionCount);
            Assert.True(header.RecusionDesired);
        }
コード例 #5
0
        public void TestCreateReverseQueryIPv4()
        {
            var header = DnsQueryFactory.CreateReverseQuery(IPAddress.Parse("1.2.3.4"));

            Assert.Equal("4.3.2.1.in-addr.arpa", header.Host);
            Assert.True(header.Id > 0);
            Assert.Equal(DnsQueryClass.IN, header.QueryClass);
            Assert.Equal(DnsQueryType.PTR, header.QueryType);
            Assert.Equal(DnsOperationCode.QUERY, header.OperationCode);
            Assert.Equal(1, header.QuestionCount);
            Assert.True(header.RecusionDesired);
        }
コード例 #6
0
        public void TestCreateQuery()
        {
            var header = DnsQueryFactory.CreateQuery("example.com", DnsQueryType.CNAME);

            Assert.Equal("example.com", header.Host);
            Assert.True(header.Id > 0);
            Assert.Equal(DnsQueryClass.IN, header.QueryClass);
            Assert.Equal(DnsQueryType.CNAME, header.QueryType);
            Assert.Equal(DnsOperationCode.QUERY, header.OperationCode);
            Assert.Equal(1, header.QuestionCount);
            Assert.True(header.RecusionDesired);
        }
コード例 #7
0
        public static async Task RunQuery(this IDnsClient client, string host, DnsQueryType queryType, DnsResponseCode expectedResponseCode = DnsResponseCode.NoError)
        {
            var query  = DnsQueryFactory.CreateQuery(host, queryType);
            var answer = await client.Query(query);

            Assert.Equal(host, answer.Header.Host);
            Assert.Equal(query.Id, answer.Header.Id);
            Assert.Equal(expectedResponseCode, answer.Header.ResponseCode);

            if (expectedResponseCode == DnsResponseCode.NoError && !answer.Header.Truncation)
            {
                Assert.True(answer.Answers.Count > 0);
            }
            else
            {
                Assert.Empty(answer.Answers);
            }
        }
コード例 #8
0
 public async Task TestLookupTimeout()
 {
     // Reserved - see https://en.wikipedia.org/wiki/Reserved_IP_addresses
     using var client = new DnsUdpClient(new NullLogger <DnsUdpClient>(), IPAddress.Parse("192.88.99.0"));
     await Assert.ThrowsAsync <DnsClientTimeoutException>(() => client.Query(DnsQueryFactory.CreateQuery("alanedwardes.com"), CancellationToken.None));
 }