Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
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));
 }