1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <config.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <gtest/gtest.h>
#include <exceptions/exceptions.h>
#include <dns/tests/unittest_util.h>
#include <dns/rcode.h>
#include <util/io/buffer.h>
#include <dns/message.h>
#include <nsas/nameserver_address_store.h>
#include <cache/resolver_cache.h>
// IMPORTANT: We shouldn't directly use ASIO definitions in this test.
// In particular, we must not include asio.hpp in this file.
// The asiolink module is primarily intended to be a wrapper that hide the
// details of the underlying implementations. We need to test the wrapper
// level behaviors. In addition, some compilers reject to compile this file
// if we include asio.hpp unless we specify a special compiler option.
// If we need to test something at the level of underlying ASIO and need
// their definition, that test should go to asiolink/internal/tests.
#include <resolve/recursive_query.h>
#include <asiolink/io_socket.h>
#include <asiolink/io_service.h>
#include <asiolink/io_message.h>
#include <asiolink/io_error.h>
#include <asiolink/dns_lookup.h>
#include <asiolink/simple_callback.h>
using isc::UnitTestUtil;
using namespace std;
using namespace asiolink;
using namespace isc::dns;
using namespace isc::util::io;
namespace {
const char* const TEST_SERVER_PORT = "53535";
const char* const TEST_CLIENT_PORT = "53536";
const char* const TEST_IPV6_ADDR = "::1";
const char* const TEST_IPV4_ADDR = "127.0.0.1";
// This data is intended to be valid as a DNS/TCP-like message: the first
// two octets encode the length of the rest of the data. This is crucial
// for the tests below.
const uint8_t test_data[] = {0, 4, 1, 2, 3, 4};
// This function returns an addrinfo structure for use by tests, using
// different addresses and ports depending on whether we're testing
// IPv4 or v6, TCP or UDP, and client or server operation.
struct addrinfo*
resolveAddress(const int family, const int protocol, const bool client) {
const char* const addr = (family == AF_INET6) ?
TEST_IPV6_ADDR : TEST_IPV4_ADDR;
const char* const port = client ? TEST_CLIENT_PORT : TEST_SERVER_PORT;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = (protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
hints.ai_protocol = protocol;
hints.ai_flags = AI_NUMERICSERV;
struct addrinfo* res;
const int error = getaddrinfo(addr, port, &hints, &res);
if (error != 0) {
isc_throw(IOError, "getaddrinfo failed: " << gai_strerror(error));
}
return (res);
}
// This fixture is a framework for various types of network operations
// using the ASIO interfaces. Each test case creates an IOService object,
// opens a local "client" socket for testing, sends data via the local socket
// to the service that would run in the IOService object.
// A mock callback function (an ASIOCallBack object) is registered with the
// IOService object, so the test code should be able to examine the data
// received on the server side. It then checks the received data matches
// expected parameters.
// If initialization parameters of the IOService should be modified, the test
// case can do it using the setDNSService() method.
// Note: the set of tests in RecursiveQueryTest use actual network services and may
// involve undesirable side effects such as blocking.
class RecursiveQueryTest : public ::testing::Test {
protected:
RecursiveQueryTest();
~RecursiveQueryTest() {
if (res_ != NULL) {
freeaddrinfo(res_);
}
if (sock_ != -1) {
close(sock_);
}
delete dns_service_;
delete callback_;
delete io_service_;
}
// Send a test UDP packet to a mock server
void sendUDP(const int family) {
res_ = resolveAddress(family, IPPROTO_UDP, false);
sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
if (sock_ < 0) {
isc_throw(IOError, "failed to open test socket");
}
const int cc = sendto(sock_, test_data, sizeof(test_data), 0,
res_->ai_addr, res_->ai_addrlen);
if (cc != sizeof(test_data)) {
isc_throw(IOError, "unexpected sendto result: " << cc);
}
io_service_->run();
}
// Send a test TCP packet to a mock server
void sendTCP(const int family) {
res_ = resolveAddress(family, IPPROTO_TCP, false);
sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
if (sock_ < 0) {
isc_throw(IOError, "failed to open test socket");
}
if (connect(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
isc_throw(IOError, "failed to connect to the test server");
}
const int cc = send(sock_, test_data, sizeof(test_data), 0);
if (cc != sizeof(test_data)) {
isc_throw(IOError, "unexpected send result: " << cc);
}
io_service_->run();
}
// Receive a UDP packet from a mock server; used for testing
// recursive lookup. The caller must place a RecursiveQuery
// on the IO Service queue before running this routine.
void recvUDP(const int family, void* buffer, size_t& size) {
res_ = resolveAddress(family, IPPROTO_UDP, true);
sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
if (sock_ < 0) {
isc_throw(IOError, "failed to open test socket");
}
if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
isc_throw(IOError, "bind failed: " << strerror(errno));
}
// The IO service queue should have a RecursiveQuery object scheduled
// to run at this point. This call will cause it to begin an
// async send, then return.
io_service_->run_one();
// ... and this one will block until the send has completed
io_service_->run_one();
// Now we attempt to recv() whatever was sent.
// XXX: there's no guarantee the receiving socket can immediately get
// the packet. Normally we can perform blocking recv to wait for it,
// but in theory it's even possible that the packet is lost.
// In order to prevent the test from hanging in such a worst case
// we add an ad hoc timeout.
const struct timeval timeo = { 10, 0 };
int recv_options = 0;
if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo,
sizeof(timeo))) {
if (errno == ENOPROTOOPT) {
// Workaround for Solaris: it doesn't accept SO_RCVTIMEO
// with the error of ENOPROTOOPT. Since this is a workaround
// for rare error cases anyway, we simply switch to the
// "don't wait" mode. If we still find an error in recv()
// can happen often we'll consider a more complete solution.
recv_options = MSG_DONTWAIT;
} else {
isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
}
}
const int ret = recv(sock_, buffer, size, recv_options);
if (ret < 0) {
isc_throw(IOError, "recvfrom failed: " << strerror(errno));
}
// Pass the message size back via the size parameter
size = ret;
}
// Set up an IO Service queue using the specified address
void setDNSService(const char& address) {
delete dns_service_;
dns_service_ = NULL;
delete io_service_;
io_service_ = new IOService();
callback_ = new ASIOCallBack(this);
dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, address, callback_, NULL, NULL);
}
// Set up an IO Service queue using the "any" address, on IPv4 if
// 'use_ipv4' is true and on IPv6 if 'use_ipv6' is true.
void setDNSService(const bool use_ipv4, const bool use_ipv6) {
delete dns_service_;
dns_service_ = NULL;
delete io_service_;
io_service_ = new IOService();
callback_ = new ASIOCallBack(this);
dns_service_ = new DNSService(*io_service_, *TEST_SERVER_PORT, use_ipv4, use_ipv6, callback_,
NULL, NULL);
}
// Set up empty DNS Service
// Set up an IO Service queue without any addresses
void setDNSService() {
delete dns_service_;
dns_service_ = NULL;
delete io_service_;
io_service_ = new IOService();
callback_ = new ASIOCallBack(this);
dns_service_ = new DNSService(*io_service_, callback_, NULL, NULL);
}
// Run a simple server test, on either IPv4 or IPv6, and over either
// UDP or TCP. Calls the sendUDP() or sendTCP() methods, which will
// start the IO Service queue. The UDPServer or TCPServer that was
// created by setIOService() will receive the test packet and issue a
// callback, which enables us to check that the data it received
// matches what we sent.
void doTest(const int family, const int protocol) {
if (protocol == IPPROTO_UDP) {
sendUDP(family);
} else {
sendTCP(family);
}
// There doesn't seem to be an effective test for the validity of
// 'native'.
// One thing we are sure is it must be different from our local socket.
EXPECT_NE(sock_, callback_native_);
EXPECT_EQ(protocol, callback_protocol_);
EXPECT_EQ(family == AF_INET6 ? TEST_IPV6_ADDR : TEST_IPV4_ADDR,
callback_address_);
const uint8_t* expected_data =
protocol == IPPROTO_UDP ? test_data : test_data + 2;
const size_t expected_datasize =
protocol == IPPROTO_UDP ? sizeof(test_data) :
sizeof(test_data) - 2;
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, &callback_data_[0],
callback_data_.size(),
expected_data, expected_datasize);
}
protected:
// This is a nonfunctional mockup of a DNSServer object. Its purpose
// is to resume after a recursive query or other asynchronous call
// has completed.
class MockServer : public DNSServer {
public:
explicit MockServer(IOService& io_service,
SimpleCallback* checkin = NULL,
DNSLookup* lookup = NULL,
DNSAnswer* answer = NULL) :
io_(io_service),
done_(false),
message_(new Message(Message::PARSE)),
answer_message_(new Message(Message::RENDER)),
respbuf_(new OutputBuffer(0)),
checkin_(checkin), lookup_(lookup), answer_(answer)
{}
void operator()(asio::error_code ec = asio::error_code(),
size_t length = 0)
{}
void resume(const bool) {
// should never be called in our tests
}
DNSServer* clone() {
MockServer* s = new MockServer(*this);
return (s);
}
inline void asyncLookup() {
if (lookup_) {
(*lookup_)(*io_message_, message_, answer_message_,
respbuf_, this);
}
}
protected:
IOService& io_;
bool done_;
private:
// Currently unused; these will be used for testing
// asynchronous lookup calls via the asyncLookup() method
boost::shared_ptr<asiolink::IOMessage> io_message_;
isc::dns::MessagePtr message_;
isc::dns::MessagePtr answer_message_;
isc::util::io::OutputBufferPtr respbuf_;
// Callback functions provided by the caller
const SimpleCallback* checkin_;
const DNSLookup* lookup_;
const DNSAnswer* answer_;
};
// This version of mock server just stops the io_service when it is resumed
class MockServerStop : public MockServer {
public:
explicit MockServerStop(IOService& io_service, bool* done) :
MockServer(io_service),
done_(done)
{}
void resume(const bool done) {
*done_ = done;
io_.stop();
}
DNSServer* clone() {
return (new MockServerStop(*this));
}
private:
bool* done_;
};
class MockResolver : public isc::resolve::ResolverInterface {
void resolve(const QuestionPtr& question,
const ResolverInterface::CallbackPtr& callback) {
}
};
// This version of mock server just stops the io_service when it is resumed
// the second time. (Used in the clientTimeout test, where resume
// is called initially with the error answer, and later when the
// lookup times out, it is called without an answer to send back)
class MockServerStop2 : public MockServer {
public:
explicit MockServerStop2(IOService& io_service,
bool* done1, bool* done2) :
MockServer(io_service),
done1_(done1),
done2_(done2),
stopped_once_(false)
{}
void resume(const bool done) {
if (stopped_once_) {
*done2_ = done;
io_.stop();
} else {
*done1_ = done;
stopped_once_ = true;
}
}
DNSServer* clone() {
return (new MockServerStop2(*this));
}
private:
bool* done1_;
bool* done2_;
bool stopped_once_;
};
private:
class ASIOCallBack : public SimpleCallback {
public:
ASIOCallBack(RecursiveQueryTest* test_obj) : test_obj_(test_obj) {}
void operator()(const IOMessage& io_message) const {
test_obj_->callBack(io_message);
}
private:
RecursiveQueryTest* test_obj_;
};
void callBack(const IOMessage& io_message) {
callback_protocol_ = io_message.getSocket().getProtocol();
callback_native_ = io_message.getSocket().getNative();
callback_address_ =
io_message.getRemoteEndpoint().getAddress().toText();
callback_data_.assign(
static_cast<const uint8_t*>(io_message.getData()),
static_cast<const uint8_t*>(io_message.getData()) +
io_message.getDataSize());
io_service_->stop();
}
protected:
// We use a pointer for io_service_, because for some tests we
// need to recreate a new one within one onstance of this class
IOService* io_service_;
DNSService* dns_service_;
isc::nsas::NameserverAddressStore* nsas_;
isc::cache::ResolverCache cache_;
ASIOCallBack* callback_;
int callback_protocol_;
int callback_native_;
string callback_address_;
vector<uint8_t> callback_data_;
int sock_;
struct addrinfo* res_;
};
RecursiveQueryTest::RecursiveQueryTest() :
dns_service_(NULL), callback_(NULL), callback_protocol_(0),
callback_native_(-1), sock_(-1), res_(NULL)
{
io_service_ = new IOService();
setDNSService(true, true);
boost::shared_ptr<MockResolver>mock_resolver(new MockResolver());
nsas_ = new isc::nsas::NameserverAddressStore(mock_resolver);
}
TEST_F(RecursiveQueryTest, v6UDPSend) {
doTest(AF_INET6, IPPROTO_UDP);
}
TEST_F(RecursiveQueryTest, v6TCPSend) {
doTest(AF_INET6, IPPROTO_TCP);
}
TEST_F(RecursiveQueryTest, v4UDPSend) {
doTest(AF_INET, IPPROTO_UDP);
}
TEST_F(RecursiveQueryTest, v4TCPSend) {
doTest(AF_INET, IPPROTO_TCP);
}
TEST_F(RecursiveQueryTest, v6UDPSendSpecific) {
// Explicitly set a specific address to be bound to the socket.
// The subsequent test does not directly ensures the underlying socket
// is bound to the expected address, but the success of the tests should
// reasonably suggest it works as intended.
// Specifying an address also implicitly means the service runs in a
// single address-family mode. In tests using TCP we can confirm that
// by trying to make a connection and seeing a failure. In UDP, it'd be
// more complicated because we need to use a connected socket and catch
// an error on a subsequent read operation. We could do it, but for
// simplicity we only tests the easier cases for now.
setDNSService(*TEST_IPV6_ADDR);
doTest(AF_INET6, IPPROTO_UDP);
}
TEST_F(RecursiveQueryTest, v6TCPSendSpecific) {
setDNSService(*TEST_IPV6_ADDR);
doTest(AF_INET6, IPPROTO_TCP);
EXPECT_THROW(sendTCP(AF_INET), IOError);
}
TEST_F(RecursiveQueryTest, v4UDPSendSpecific) {
setDNSService(*TEST_IPV4_ADDR);
doTest(AF_INET, IPPROTO_UDP);
}
TEST_F(RecursiveQueryTest, v4TCPSendSpecific) {
setDNSService(*TEST_IPV4_ADDR);
doTest(AF_INET, IPPROTO_TCP);
EXPECT_THROW(sendTCP(AF_INET6), IOError);
}
TEST_F(RecursiveQueryTest, v6AddServer) {
setDNSService();
dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV6_ADDR);
doTest(AF_INET6, IPPROTO_TCP);
EXPECT_THROW(sendTCP(AF_INET), IOError);
}
TEST_F(RecursiveQueryTest, v4AddServer) {
setDNSService();
dns_service_->addServer(*TEST_SERVER_PORT, TEST_IPV4_ADDR);
doTest(AF_INET, IPPROTO_TCP);
EXPECT_THROW(sendTCP(AF_INET6), IOError);
}
TEST_F(RecursiveQueryTest, clearServers) {
setDNSService();
dns_service_->clearServers();
EXPECT_THROW(sendTCP(AF_INET), IOError);
EXPECT_THROW(sendTCP(AF_INET6), IOError);
}
TEST_F(RecursiveQueryTest, v6TCPOnly) {
// Open only IPv6 TCP socket. A subsequent attempt of establishing an
// IPv4/TCP connection should fail. See above for why we only test this
// for TCP.
setDNSService(false, true);
EXPECT_THROW(sendTCP(AF_INET), IOError);
}
TEST_F(RecursiveQueryTest, v4TCPOnly) {
setDNSService(true, false);
EXPECT_THROW(sendTCP(AF_INET6), IOError);
}
vector<pair<string, uint16_t> >
singleAddress(const string &address, uint16_t port) {
vector<pair<string, uint16_t> > result;
result.push_back(pair<string, uint16_t>(address, port));
return (result);
}
TEST_F(RecursiveQueryTest, recursiveSetupV4) {
setDNSService(true, false);
uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port)));
}
TEST_F(RecursiveQueryTest, recursiveSetupV6) {
setDNSService(false, true);
uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
EXPECT_NO_THROW(RecursiveQuery(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV6_ADDR, port),
singleAddress(TEST_IPV6_ADDR,port)));
}
// XXX:
// This is very inadequate unit testing. It should be generalized into
// a routine that can do this with variable address family, address, and
// port, and with the various callbacks defined in such a way as to ensure
// full code coverage including error cases.
TEST_F(RecursiveQueryTest, forwarderSend) {
setDNSService(true, false);
// Note: We use the test prot plus one to ensure we aren't binding
// to the same port as the actual server
uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
MockServer server(*io_service_);
RecursiveQuery rq(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port));
Question q(Name("example.com"), RRClass::IN(), RRType::TXT());
OutputBufferPtr buffer(new OutputBuffer(0));
MessagePtr answer(new Message(Message::RENDER));
rq.resolve(q, answer, buffer, &server);
char data[4096];
size_t size = sizeof(data);
ASSERT_NO_THROW(recvUDP(AF_INET, data, size));
Message m(Message::PARSE);
InputBuffer ibuf(data, size);
// Make sure we can parse the message that was sent
EXPECT_NO_THROW(m.parseHeader(ibuf));
EXPECT_NO_THROW(m.fromWire(ibuf));
// Check that the question sent matches the one we wanted
QuestionPtr q2 = *m.beginQuestion();
EXPECT_EQ(q.getName(), q2->getName());
EXPECT_EQ(q.getType(), q2->getType());
EXPECT_EQ(q.getClass(), q2->getClass());
}
int
createTestSocket()
{
struct addrinfo* res_ = resolveAddress(AF_INET, IPPROTO_UDP, true);
int sock_ = socket(res_->ai_family, res_->ai_socktype, res_->ai_protocol);
if (sock_ < 0) {
isc_throw(IOError, "failed to open test socket");
}
if (bind(sock_, res_->ai_addr, res_->ai_addrlen) < 0) {
isc_throw(IOError, "failed to bind test socket");
}
return sock_;
}
int
setSocketTimeout(int sock_, size_t tv_sec, size_t tv_usec) {
const struct timeval timeo = { tv_sec, tv_usec };
int recv_options = 0;
if (setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo))) {
if (errno == ENOPROTOOPT) { // see RecursiveQueryTest::recvUDP()
recv_options = MSG_DONTWAIT;
} else {
isc_throw(IOError, "set RCVTIMEO failed: " << strerror(errno));
}
}
return recv_options;
}
// try to read from the socket max time
// *num is incremented for every succesfull read
// returns true if it can read max times, false otherwise
bool tryRead(int sock_, int recv_options, size_t max, int* num) {
size_t i = 0;
do {
char inbuff[512];
if (recv(sock_, inbuff, sizeof(inbuff), recv_options) < 0) {
return false;
} else {
++i;
++*num;
}
} while (i < max);
return true;
}
// Test it tries the correct amount of times before giving up
TEST_F(RecursiveQueryTest, forwardQueryTimeout) {
// Prepare the service (we do not use the common setup, we do not answer
setDNSService();
// Prepare the socket
sock_ = createTestSocket();
// Prepare the server
bool done(true);
MockServerStop server(*io_service_, &done);
// Do the answer
const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
RecursiveQuery query(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port),
10, 4000, 3000, 2);
Question question(Name("example.net"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
MessagePtr answer(new Message(Message::RENDER));
query.resolve(question, answer, buffer, &server);
// Run the test
io_service_->run();
// Read up to 3 packets. Use some ad hoc timeout to prevent an infinite
// block (see also recvUDP()).
int recv_options = setSocketTimeout(sock_, 10, 0);
int num = 0;
bool read_success = tryRead(sock_, recv_options, 3, &num);
// The query should 'succeed' with an error response
EXPECT_TRUE(done);
EXPECT_EQ(3, num);
EXPECT_TRUE(read_success);
}
// If we set client timeout to lower than querytimeout, we should
// get a failure answer, but still see retries
// (no actual answer is given here yet)
TEST_F(RecursiveQueryTest, forwardClientTimeout) {
// Prepare the service (we do not use the common setup, we do not answer
setDNSService();
sock_ = createTestSocket();
// Prepare the server
bool done1(true);
MockServerStop server(*io_service_, &done1);
MessagePtr answer(new Message(Message::RENDER));
// Do the answer
const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
// Set it up to retry twice before client timeout fires
// Since the lookup timer has not fired, it should retry
// four times
RecursiveQuery query(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port),
200, 480, 4000, 4);
Question question(Name("example.net"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
query.resolve(question, answer, buffer, &server);
// Run the test
io_service_->run();
// we know it'll fail, so make it a shorter timeout
int recv_options = setSocketTimeout(sock_, 1, 0);
// Try to read 4 times
int num = 0;
bool read_success = tryRead(sock_, recv_options, 4, &num);
// The query should fail
EXPECT_TRUE(done1);
EXPECT_EQ(3, num);
EXPECT_FALSE(read_success);
}
// If we set lookup timeout to lower than querytimeout*retries, we should
// fail before the full amount of retries
TEST_F(RecursiveQueryTest, forwardLookupTimeout) {
// Prepare the service (we do not use the common setup, we do not answer
setDNSService();
// Prepare the socket
sock_ = createTestSocket();
// Prepare the server
bool done(true);
MockServerStop server(*io_service_, &done);
MessagePtr answer(new Message(Message::RENDER));
// Do the answer
const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
// Set up the test so that it will retry 5 times, but the lookup
// timeout will fire after only 3 normal timeouts
RecursiveQuery query(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port),
200, 4000, 480, 5);
Question question(Name("example.net"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
query.resolve(question, answer, buffer, &server);
// Run the test
io_service_->run();
int recv_options = setSocketTimeout(sock_, 1, 0);
// Try to read 5 times, should stop after 3 reads
int num = 0;
bool read_success = tryRead(sock_, recv_options, 5, &num);
// The query should fail and respond with an error
EXPECT_TRUE(done);
EXPECT_EQ(3, num);
EXPECT_FALSE(read_success);
}
// Set everything very low and see if this doesn't cause weird
// behaviour
TEST_F(RecursiveQueryTest, lowtimeouts) {
// Prepare the service (we do not use the common setup, we do not answer
setDNSService();
// Prepare the socket
sock_ = createTestSocket();
// Prepare the server
bool done(true);
MockServerStop server(*io_service_, &done);
MessagePtr answer(new Message(Message::RENDER));
// Do the answer
const uint16_t port = boost::lexical_cast<uint16_t>(TEST_CLIENT_PORT);
// Set up the test so that it will retry 5 times, but the lookup
// timeout will fire after only 3 normal timeouts
RecursiveQuery query(*dns_service_,
*nsas_, cache_,
singleAddress(TEST_IPV4_ADDR, port),
singleAddress(TEST_IPV4_ADDR, port),
1, 1, 1, 1);
Question question(Name("example.net"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
query.resolve(question, answer, buffer, &server);
// Run the test
io_service_->run();
int recv_options = setSocketTimeout(sock_, 1, 0);
// Try to read 5 times, should stop after 3 reads
int num = 0;
bool read_success = tryRead(sock_, recv_options, 5, &num);
// The query should fail and respond with an error
EXPECT_TRUE(done);
EXPECT_EQ(1, num);
EXPECT_FALSE(read_success);
}
// as mentioned above, we need a more better framework for this,
// in addition to that, this sends out queries into the world
// (which we should catch somehow and fake replies for)
// for the skeleton code, it shouldn't be too much of a problem
// Ok so even we don't all have access to the DNS world right now,
// so disabling these tests too.
TEST_F(RecursiveQueryTest, DISABLED_recursiveSendOk) {
setDNSService(true, false);
bool done;
MockServerStop server(*io_service_, &done);
vector<pair<string, uint16_t> > empty_vector;
RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
empty_vector, 10000, 0);
Question q(Name("www.isc.org"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
MessagePtr answer(new Message(Message::RENDER));
rq.resolve(q, answer, buffer, &server);
io_service_->run();
// Check that the answer we got matches the one we wanted
EXPECT_EQ(Rcode::NOERROR(), answer->getRcode());
ASSERT_EQ(1, answer->getRRCount(Message::SECTION_ANSWER));
RRsetPtr a = *answer->beginSection(Message::SECTION_ANSWER);
EXPECT_EQ(q.getName(), a->getName());
EXPECT_EQ(q.getType(), a->getType());
EXPECT_EQ(q.getClass(), a->getClass());
EXPECT_EQ(1, a->getRdataCount());
}
// see comments at previous test
TEST_F(RecursiveQueryTest, DISABLED_recursiveSendNXDOMAIN) {
setDNSService(true, false);
bool done;
MockServerStop server(*io_service_, &done);
vector<pair<string, uint16_t> > empty_vector;
RecursiveQuery rq(*dns_service_, *nsas_, cache_, empty_vector,
empty_vector, 10000, 0);
Question q(Name("wwwdoesnotexist.isc.org"), RRClass::IN(), RRType::A());
OutputBufferPtr buffer(new OutputBuffer(0));
MessagePtr answer(new Message(Message::RENDER));
rq.resolve(q, answer, buffer, &server);
io_service_->run();
// Check that the answer we got matches the one we wanted
EXPECT_EQ(Rcode::NXDOMAIN(), answer->getRcode());
EXPECT_EQ(0, answer->getRRCount(Message::SECTION_ANSWER));
}
// TODO: add tests that check whether the cache is updated on succesfull
// responses, and not updated on failures.
}
|