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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
|
// Copyright (C) 2010 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 <util/io/sockaddr_util.h>
#include <util/memory_segment_local.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
#include <dns/name.h>
#include <dns/opcode.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <dns/rrttl.h>
#include <dns/rdataclass.h>
#include <dns/tsig.h>
#include <server_common/portconfig.h>
#include <server_common/keyring.h>
#include <datasrc/memory_datasrc.h>
#include <datasrc/client_list.h>
#include <auth/auth_srv.h>
#include <auth/command.h>
#include <auth/common.h>
#include <auth/statistics.h>
#include <auth/datasrc_configurator.h>
#include <util/unittests/mock_socketsession.h>
#include <util/threads/lock.h>
#include <dns/tests/unittest_util.h>
#include <testutils/dnsmessage_test.h>
#include <testutils/srv_test.h>
#include <testutils/mockups.h>
#include <testutils/portconfig.h>
#include <testutils/socket_request.h>
#include <gtest/gtest.h>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/foreach.hpp>
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
using namespace std;
using namespace isc::cc;
using namespace isc::dns;
using namespace isc::util;
using namespace isc::util::io::internal;
using namespace isc::util::unittests;
using namespace isc::dns::rdata;
using namespace isc::data;
using namespace isc::xfr;
using namespace isc::asiodns;
using namespace isc::asiolink;
using namespace isc::testutils;
using namespace isc::server_common::portconfig;
using isc::UnitTestUtil;
using boost::scoped_ptr;
namespace {
const char* const CONFIG_TESTDB =
"{\"database_file\": \"" TEST_DATA_DIR "/example.sqlite3\"}";
// The following file must be non existent and must be non"creatable" (see
// the sqlite3 test).
const char* const BADCONFIG_TESTDB =
"{ \"database_file\": \"" TEST_DATA_DIR "/nodir/notexist\"}";
const char* const STATIC_DSRC_FILE = DSRC_DIR "/static.zone";
// This is a configuration that uses the in-memory data source containing
// a signed example zone.
const char* const CONFIG_INMEMORY_EXAMPLE = TEST_DATA_DIR "/rfc5155-example.zone.signed";
class AuthSrvTest : public SrvTestBase {
protected:
AuthSrvTest() :
dnss_(),
server(xfrout, ddns_forwarder),
// The empty string is expected value of the parameter of
// requestSocket, not the app_name (there's no fallback, it checks
// the empty string is passed).
sock_requestor_(dnss_, address_store_, 53210, ""),
datasrc_configurator_(&server)
{
server.setDNSService(dnss_);
server.setXfrinSession(¬ify_session);
server.createDDNSForwarder();
}
~AuthSrvTest() {
server.destroyDDNSForwarder();
}
virtual void processMessage() {
// If processMessage has been called before, parse_message needs
// to be reset. If it hasn't, there's no harm in doing so
parse_message->clear(Message::PARSE);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
}
// Helper for checking Rcode statistic counters;
// Checks for one specific Rcode statistics counter value
void checkRcodeCounter(const Rcode& rcode, int expected_value) const {
EXPECT_EQ(expected_value, server.getCounter(rcode)) <<
"Expected Rcode count for " << rcode.toText() <<
" " << expected_value << ", was: " <<
server.getCounter(rcode);
}
// Checks whether all Rcode counters are set to zero
void checkAllRcodeCountersZero() const {
for (int i = 0; i < 17; i++) {
checkRcodeCounter(Rcode(i), 0);
}
}
// Checks whether all Rcode counters are set to zero except the given
// rcode (it is checked to be set to 'value')
void checkAllRcodeCountersZeroExcept(const Rcode& rcode, int value) const {
for (int i = 0; i < 17; i++) {
const Rcode rc(i);
if (rc == rcode) {
checkRcodeCounter(Rcode(i), value);
} else {
checkRcodeCounter(Rcode(i), 0);
}
}
}
// Convenience method for tests that expect to return SERVFAIL
// It calls processMessage, checks if there is an answer, and
// check the header for default SERVFAIL data
void processAndCheckSERVFAIL() {
processMessage();
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
// Convenient shortcut of creating a simple request and having the
// server process it.
void createAndSendRequest(RRType req_type, Opcode opcode = Opcode::QUERY(),
const Name& req_name = Name("example.com"),
RRClass req_class = RRClass::IN(),
int protocol = IPPROTO_UDP,
const char* const remote_address =
DEFAULT_REMOTE_ADDRESS,
uint16_t remote_port = DEFAULT_REMOTE_PORT)
{
UnitTestUtil::createRequestMessage(request_message, opcode,
default_qid, req_name,
req_class, req_type);
createRequestPacket(request_message, protocol, NULL,
remote_address, remote_port);
parse_message->clear(Message::PARSE);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
}
MockDNSService dnss_;
MockXfroutClient xfrout;
MockSocketSessionForwarder ddns_forwarder;
AuthSrv server;
vector<uint8_t> response_data;
AddressList address_store_;
TestSocketRequestor sock_requestor_;
DataSourceConfigurator datasrc_configurator_;
};
// A helper function that builds a response to version.bind/TXT/CH that
// should be identical to the response from our builtin (static) data source
// by default. The resulting wire-format data will be stored in 'data'.
void
createBuiltinVersionResponse(const qid_t qid, vector<uint8_t>& data) {
const Name version_name("VERSION.BIND.");
const Name apex_name("BIND.");
Message message(Message::RENDER);
UnitTestUtil::createRequestMessage(message, Opcode::QUERY(),
qid, version_name,
RRClass::CH(), RRType::TXT());
message.setHeaderFlag(Message::HEADERFLAG_QR);
message.setHeaderFlag(Message::HEADERFLAG_AA);
RRsetPtr rrset_version = RRsetPtr(new RRset(version_name, RRClass::CH(),
RRType::TXT(), RRTTL(0)));
rrset_version->addRdata(generic::TXT(PACKAGE_STRING));
message.addRRset(Message::SECTION_ANSWER, rrset_version);
RRsetPtr rrset_version_ns = RRsetPtr(new RRset(apex_name, RRClass::CH(),
RRType::NS(), RRTTL(0)));
rrset_version_ns->addRdata(generic::NS(apex_name));
message.addRRset(Message::SECTION_AUTHORITY, rrset_version_ns);
MessageRenderer renderer;
message.toWire(renderer);
data.clear();
data.assign(static_cast<const uint8_t*>(renderer.getData()),
static_cast<const uint8_t*>(renderer.getData()) +
renderer.getLength());
}
// We did not configure any client lists. Therefore it should be REFUSED
TEST_F(AuthSrvTest, noClientList) {
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("version.bind"),
RRClass::CH(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::REFUSED(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
// Unsupported requests. Should result in NOTIMP.
TEST_F(AuthSrvTest, unsupportedRequest) {
unsupportedRequest();
// unsupportedRequest tries 13 different opcodes
checkAllRcodeCountersZeroExcept(Rcode::NOTIMP(), 13);
}
// Multiple questions. Should result in FORMERR.
TEST_F(AuthSrvTest, multiQuestion) {
multiQuestion();
checkAllRcodeCountersZeroExcept(Rcode::FORMERR(), 1);
}
// Incoming data doesn't even contain the complete header. Must be silently
// dropped.
TEST_F(AuthSrvTest, shortMessage) {
shortMessage();
checkAllRcodeCountersZero();
}
// Response messages. Must be silently dropped, whether it's a valid response
// or malformed or could otherwise cause a protocol error.
TEST_F(AuthSrvTest, response) {
response();
checkAllRcodeCountersZero();
}
// Query with a broken question
TEST_F(AuthSrvTest, shortQuestion) {
shortQuestion();
checkAllRcodeCountersZeroExcept(Rcode::FORMERR(), 1);
}
// Query with a broken answer section
TEST_F(AuthSrvTest, shortAnswer) {
shortAnswer();
checkAllRcodeCountersZeroExcept(Rcode::FORMERR(), 1);
}
// Query with unsupported version of EDNS.
TEST_F(AuthSrvTest, ednsBadVers) {
ednsBadVers();
checkAllRcodeCountersZeroExcept(Rcode::BADVERS(), 1);
}
TEST_F(AuthSrvTest, AXFROverUDP) {
axfrOverUDP();
}
TEST_F(AuthSrvTest, AXFRSuccess) {
EXPECT_FALSE(xfrout.isConnected());
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
// On success, the AXFR query has been passed to a separate process,
// so we shouldn't have to respond.
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(xfrout.isConnected());
checkAllRcodeCountersZero();
}
// Give the server a signed request, but don't give it the key. It will
// not be able to verify it, returning BADKEY
TEST_F(AuthSrvTest, TSIGSignedBadKey) {
TSIGKey key("key:c2VjcmV0Cg==:hmac-sha1");
TSIGContext context(key);
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("version.bind"), RRClass::CH(),
RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP, &context);
// Process the message, but use a different key there
boost::shared_ptr<TSIGKeyRing> keyring(new TSIGKeyRing);
server.setTSIGKeyRing(&keyring);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, TSIGError::BAD_KEY().toRcode(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
// We need to parse the message ourself, or getTSIGRecord won't work
InputBuffer ib(response_obuffer->getData(), response_obuffer->getLength());
Message m(Message::PARSE);
m.fromWire(ib);
const TSIGRecord* tsig = m.getTSIGRecord();
ASSERT_TRUE(tsig != NULL) <<
"Missing TSIG signature (we should have one even at error)";
EXPECT_EQ(TSIGError::BAD_KEY_CODE, tsig->getRdata().getError());
EXPECT_EQ(0, tsig->getRdata().getMACSize()) <<
"It should be unsigned with this error";
checkAllRcodeCountersZeroExcept(Rcode::NOTAUTH(), 1);
}
// Give the server a signed request, but signed by a different key
// (with the same name). It should return BADSIG
TEST_F(AuthSrvTest, TSIGBadSig) {
TSIGKey key("key:c2VjcmV0Cg==:hmac-sha1");
TSIGContext context(key);
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("version.bind"), RRClass::CH(),
RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP, &context);
// Process the message, but use a different key there
boost::shared_ptr<TSIGKeyRing> keyring(new TSIGKeyRing);
keyring->add(TSIGKey("key:QkFECg==:hmac-sha1"));
server.setTSIGKeyRing(&keyring);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, TSIGError::BAD_SIG().toRcode(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
// We need to parse the message ourself, or getTSIGRecord won't work
InputBuffer ib(response_obuffer->getData(), response_obuffer->getLength());
Message m(Message::PARSE);
m.fromWire(ib);
const TSIGRecord* tsig = m.getTSIGRecord();
ASSERT_TRUE(tsig != NULL) <<
"Missing TSIG signature (we should have one even at error)";
EXPECT_EQ(TSIGError::BAD_SIG_CODE, tsig->getRdata().getError());
EXPECT_EQ(0, tsig->getRdata().getMACSize()) <<
"It should be unsigned with this error";
checkAllRcodeCountersZeroExcept(Rcode::NOTAUTH(), 1);
}
// Give the server a signed unsupported request with a bad signature.
// This checks the server first verifies the signature before anything
// else.
TEST_F(AuthSrvTest, TSIGCheckFirst) {
TSIGKey key("key:c2VjcmV0Cg==:hmac-sha1");
TSIGContext context(key);
// Pass a wrong opcode there. The server shouldn't know what to do
// about it.
UnitTestUtil::createRequestMessage(request_message, Opcode::RESERVED14(),
default_qid, Name("version.bind"),
RRClass::CH(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP, &context);
// Process the message, but use a different key there
boost::shared_ptr<TSIGKeyRing> keyring(new TSIGKeyRing);
keyring->add(TSIGKey("key:QkFECg==:hmac-sha1"));
server.setTSIGKeyRing(&keyring);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, TSIGError::BAD_SIG().toRcode(),
Opcode::RESERVED14().getCode(), QR_FLAG, 0, 0, 0, 0);
// We need to parse the message ourself, or getTSIGRecord won't work
InputBuffer ib(response_obuffer->getData(), response_obuffer->getLength());
Message m(Message::PARSE);
m.fromWire(ib);
const TSIGRecord* tsig = m.getTSIGRecord();
ASSERT_TRUE(tsig != NULL) <<
"Missing TSIG signature (we should have one even at error)";
EXPECT_EQ(TSIGError::BAD_SIG_CODE, tsig->getRdata().getError());
EXPECT_EQ(0, tsig->getRdata().getMACSize()) <<
"It should be unsigned with this error";
// TSIG should have failed, and so the per opcode counter shouldn't be
// incremented.
EXPECT_EQ(0, server.getCounter(Opcode::RESERVED14()));
checkAllRcodeCountersZeroExcept(Rcode::NOTAUTH(), 1);
}
TEST_F(AuthSrvTest, AXFRConnectFail) {
EXPECT_FALSE(xfrout.isConnected()); // check prerequisite
xfrout.disableConnect();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
EXPECT_FALSE(xfrout.isConnected());
}
TEST_F(AuthSrvTest, AXFRSendFail) {
// first send a valid query, making the connection with the xfr process
// open.
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(xfrout.isConnected());
xfrout.disableSend();
parse_message->clear(Message::PARSE);
response_obuffer->clear();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
// The connection should have been closed due to the send failure.
EXPECT_FALSE(xfrout.isConnected());
}
TEST_F(AuthSrvTest, AXFRDisconnectFail) {
// In our usage disconnect() shouldn't fail. But even if it does,
// it should not disrupt service (so processMessage should have caught it)
xfrout.disableSend();
xfrout.disableDisconnect();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
EXPECT_NO_THROW(server.processMessage(*io_message, *parse_message,
*response_obuffer, &dnsserv));
// Since the disconnect failed, we should still be 'connected'
EXPECT_TRUE(xfrout.isConnected());
// XXX: we need to re-enable disconnect. otherwise an exception would be
// thrown via the destructor of the server.
xfrout.enableDisconnect();
}
TEST_F(AuthSrvTest, IXFRConnectFail) {
EXPECT_FALSE(xfrout.isConnected()); // check prerequisite
xfrout.disableConnect();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::IXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
EXPECT_FALSE(xfrout.isConnected());
}
TEST_F(AuthSrvTest, IXFRSendFail) {
// first send a valid query, making the connection with the xfr process
// open.
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::IXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(xfrout.isConnected());
xfrout.disableSend();
parse_message->clear(Message::PARSE);
response_obuffer->clear();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::IXFR());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
// The connection should have been closed due to the send failure.
EXPECT_FALSE(xfrout.isConnected());
}
TEST_F(AuthSrvTest, IXFRDisconnectFail) {
// In our usage disconnect() shouldn't fail, but even if it does,
// procesMessage() should catch it.
xfrout.disableSend();
xfrout.disableDisconnect();
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(),
RRType::IXFR());
createRequestPacket(request_message, IPPROTO_TCP);
EXPECT_NO_THROW(server.processMessage(*io_message, *parse_message,
*response_obuffer, &dnsserv));
EXPECT_TRUE(xfrout.isConnected());
// XXX: we need to re-enable disconnect. otherwise an exception would be
// thrown via the destructor of the server.
xfrout.enableDisconnect();
}
TEST_F(AuthSrvTest, notify) {
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
// An internal command message should have been created and sent to an
// external module. Check them.
EXPECT_EQ("Zonemgr", notify_session.getMessageDest());
EXPECT_EQ("notify",
notify_session.getSentMessage()->get("command")->get(0)->stringValue());
ConstElementPtr notify_args =
notify_session.getSentMessage()->get("command")->get(1);
EXPECT_EQ("example.com.", notify_args->get("zone_name")->stringValue());
EXPECT_EQ(DEFAULT_REMOTE_ADDRESS,
notify_args->get("master")->stringValue());
EXPECT_EQ("IN", notify_args->get("zone_class")->stringValue());
// On success, the server should return a response to the notify.
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
// The question must be identical to that of the received notify
ConstQuestionPtr question = *parse_message->beginQuestion();
EXPECT_EQ(Name("example.com"), question->getName());
EXPECT_EQ(RRClass::IN(), question->getClass());
EXPECT_EQ(RRType::SOA(), question->getType());
checkAllRcodeCountersZeroExcept(Rcode::NOERROR(), 1);
}
TEST_F(AuthSrvTest, notifyForCHClass) {
// Same as the previous test, but for the CH RRClass.
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::CH(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
// Other conditions should be the same, so simply confirm the RR class is
// set correctly.
ConstElementPtr notify_args =
notify_session.getSentMessage()->get("command")->get(1);
EXPECT_EQ("CH", notify_args->get("zone_class")->stringValue());
}
TEST_F(AuthSrvTest, notifyEmptyQuestion) {
request_message.clear(Message::RENDER);
request_message.setOpcode(Opcode::NOTIFY());
request_message.setRcode(Rcode::NOERROR());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
request_message.setQid(default_qid);
request_message.toWire(request_renderer);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
Opcode::NOTIFY().getCode(), QR_FLAG, 0, 0, 0, 0);
}
TEST_F(AuthSrvTest, notifyMultiQuestions) {
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
// add one more SOA question
request_message.addQuestion(Question(Name("example.com"), RRClass::IN(),
RRType::SOA()));
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
Opcode::NOTIFY().getCode(), QR_FLAG, 2, 0, 0, 0);
}
TEST_F(AuthSrvTest, notifyNonSOAQuestion) {
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::FORMERR(),
Opcode::NOTIFY().getCode(), QR_FLAG, 1, 0, 0, 0);
}
TEST_F(AuthSrvTest, notifyWithoutAA) {
// implicitly leave the AA bit off. our implementation will accept it.
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
}
TEST_F(AuthSrvTest, notifyWithErrorRcode) {
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
request_message.setRcode(Rcode::SERVFAIL());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
Opcode::NOTIFY().getCode(), QR_FLAG | AA_FLAG, 1, 0, 0, 0);
}
TEST_F(AuthSrvTest, notifyWithoutSession) {
server.setXfrinSession(NULL);
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
// we simply ignore the notify and let it be resent if an internal error
// happens.
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
}
TEST_F(AuthSrvTest, notifySendFail) {
notify_session.disableSend();
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
}
TEST_F(AuthSrvTest, notifyReceiveFail) {
notify_session.disableReceive();
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
}
TEST_F(AuthSrvTest, notifyWithBogusSessionMessage) {
notify_session.setMessage(Element::fromJSON("{\"foo\": 1}"));
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
}
TEST_F(AuthSrvTest, notifyWithSessionMessageError) {
notify_session.setMessage(
Element::fromJSON("{\"result\": [1, \"FAIL\"]}"));
UnitTestUtil::createRequestMessage(request_message, Opcode::NOTIFY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
}
void
updateDatabase(DataSourceConfigurator& datasrc_configurator,
const char* params)
{
const ConstElementPtr config(Element::fromJSON("{"
"\"IN\": [{"
" \"type\": \"sqlite3\","
" \"params\": " + string(params) +
"}]}"));
datasrc_configurator.reconfigure(config);
}
void
updateInMemory(DataSourceConfigurator& datasrc_configurator,
const char* origin, const char* filename)
{
const ConstElementPtr config(Element::fromJSON("{"
"\"IN\": [{"
" \"type\": \"MasterFiles\","
" \"params\": {"
" \"" + string(origin) + "\": \"" + string(filename) + "\""
" },"
" \"cache-enable\": true"
"}],"
"\"CH\": [{"
" \"type\": \"static\","
" \"params\": \"" + string(STATIC_DSRC_FILE) + "\""
"}]}"));
datasrc_configurator.reconfigure(config);
}
void
updateBuiltin(DataSourceConfigurator& datasrc_configurator) {
const ConstElementPtr config(Element::fromJSON("{"
"\"CH\": [{"
" \"type\": \"static\","
" \"params\": \"" + string(STATIC_DSRC_FILE) + "\""
"}]}"));
datasrc_configurator.reconfigure(config);
}
// Try giving the server a TSIG signed request and see it can anwer signed as
// well
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_TSIGSigned) { // Needs builtin
#else
TEST_F(AuthSrvTest, TSIGSigned) {
#endif
// Prepare key, the client message, etc
updateBuiltin(datasrc_configurator_);
const TSIGKey key("key:c2VjcmV0Cg==:hmac-sha1");
TSIGContext context(key);
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("VERSION.BIND."), RRClass::CH(),
RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP, &context);
// Run the message through the server
boost::shared_ptr<TSIGKeyRing> keyring(new TSIGKeyRing);
keyring->add(key);
server.setTSIGKeyRing(&keyring);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
// What did we get?
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 1, 0);
// We need to parse the message ourself, or getTSIGRecord won't work
InputBuffer ib(response_obuffer->getData(), response_obuffer->getLength());
Message m(Message::PARSE);
m.fromWire(ib);
const TSIGRecord* tsig = m.getTSIGRecord();
ASSERT_TRUE(tsig != NULL) << "Missing TSIG signature";
TSIGError error(context.verify(tsig, response_obuffer->getData(),
response_obuffer->getLength()));
EXPECT_EQ(TSIGError::NOERROR(), error) <<
"The server signed the response, but it doesn't seem to be valid";
checkAllRcodeCountersZeroExcept(Rcode::NOERROR(), 1);
}
// Same test emulating the UDPServer class behavior (defined in libasiolink).
// This is not a good test in that it assumes internal implementation details
// of UDPServer, but we've encountered a regression due to the introduction
// of that class, so we add a test for that case to prevent such a regression
// in future.
// Besides, the generalization of UDPServer is probably too much for the
// authoritative only server in terms of performance, and it's quite likely
// we need to drop it for the authoritative server implementation.
// At that point we can drop this test, too.
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_builtInQueryViaDNSServer) {
#else
TEST_F(AuthSrvTest, builtInQueryViaDNSServer) {
#endif
updateBuiltin(datasrc_configurator_);
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("VERSION.BIND."),
RRClass::CH(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP);
(*server.getDNSLookupProvider())(*io_message, parse_message,
response_message,
response_obuffer, &dnsserv);
(*server.getDNSAnswerProvider())(*io_message, parse_message,
response_message, response_obuffer);
createBuiltinVersionResponse(default_qid, response_data);
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
response_obuffer->getData(),
response_obuffer->getLength(),
&response_data[0], response_data.size());
// After it has been run, the message should be cleared
EXPECT_EQ(0, parse_message->getRRCount(Message::SECTION_QUESTION));
}
// In the following tests we confirm the response data is rendered in
// wire format in the expected way.
// The most primitive check: checking the result of the processMessage()
// method
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_builtInQuery) {
#else
TEST_F(AuthSrvTest, builtInQuery) {
#endif
updateBuiltin(datasrc_configurator_);
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("VERSION.BIND."),
RRClass::CH(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
createBuiltinVersionResponse(default_qid, response_data);
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
response_obuffer->getData(),
response_obuffer->getLength(),
&response_data[0], response_data.size());
checkAllRcodeCountersZeroExcept(Rcode::NOERROR(), 1);
}
// Same type of test as builtInQueryViaDNSServer but for an error response.
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_iqueryViaDNSServer) { // Needs builtin
#else
TEST_F(AuthSrvTest, iqueryViaDNSServer) { // Needs builtin
#endif
updateBuiltin(datasrc_configurator_);
createDataFromFile("iquery_fromWire.wire");
(*server.getDNSLookupProvider())(*io_message, parse_message,
response_message,
response_obuffer, &dnsserv);
(*server.getDNSAnswerProvider())(*io_message, parse_message,
response_message, response_obuffer);
UnitTestUtil::readWireData("iquery_response_fromWire.wire",
response_data);
EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
response_obuffer->getData(),
response_obuffer->getLength(),
&response_data[0], response_data.size());
}
// Install a Sqlite3 data source with testing data.
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_updateConfig) {
#else
TEST_F(AuthSrvTest, updateConfig) {
#endif
updateDatabase(datasrc_configurator_, CONFIG_TESTDB);
// query for existent data in the installed data source. The resulting
// response should have the AA flag on, and have an RR in each answer
// and authority section.
createDataFromFile("examplequery_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(), opcode.getCode(),
QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_datasourceFail) {
#else
TEST_F(AuthSrvTest, datasourceFail) {
#endif
updateDatabase(datasrc_configurator_, CONFIG_TESTDB);
// This query will hit a corrupted entry of the data source (the zoneload
// tool and the data source itself naively accept it). This will result
// in a SERVFAIL response, and the answer and authority sections should
// be empty.
createDataFromFile("badExampleQuery_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_updateConfigFail) {
#else
TEST_F(AuthSrvTest, updateConfigFail) {
#endif
// First, load a valid data source.
updateDatabase(datasrc_configurator_, CONFIG_TESTDB);
// Next, try to update it with a non-existent one. This should fail.
EXPECT_THROW(updateDatabase(datasrc_configurator_, BADCONFIG_TESTDB),
isc::datasrc::DataSourceError);
// The original data source should still exist.
createDataFromFile("examplequery_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}
TEST_F(AuthSrvTest, updateWithInMemoryClient) {
// Test configuring memory data source. Detailed test cases are covered
// in the configuration tests. We only check the AuthSrv interface here.
// Create an empty in-memory
const ConstElementPtr config(Element::fromJSON("{"
"\"IN\": [{"
" \"type\": \"MasterFiles\","
" \"params\": {},"
" \"cache-enable\": true"
"}]}"));
datasrc_configurator_.reconfigure(config);
// after successful configuration, we should have one (with empty zoneset).
// The memory data source is empty, should return REFUSED rcode.
createDataFromFile("examplequery_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::REFUSED(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_queryWithInMemoryClientNoDNSSEC) {
#else
TEST_F(AuthSrvTest, queryWithInMemoryClientNoDNSSEC) {
#endif
// In this example, we do simple check that query is handled from the
// query handler class, and confirm it returns no error and a non empty
// answer section. Detailed examination on the response content
// for various types of queries are tested in the query tests.
updateInMemory(datasrc_configurator_, "example.", CONFIG_INMEMORY_EXAMPLE);
createDataFromFile("nsec3query_nodnssec_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 2, 1);
}
#ifdef USE_STATIC_LINK
TEST_F(AuthSrvTest, DISABLED_queryWithInMemoryClientDNSSEC) {
#else
TEST_F(AuthSrvTest, queryWithInMemoryClientDNSSEC) {
#endif
// Similar to the previous test, but the query has the DO bit on.
// The response should contain RRSIGs, and should have more RRs than
// the previous case.
updateInMemory(datasrc_configurator_, "example.", CONFIG_INMEMORY_EXAMPLE);
createDataFromFile("nsec3query_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 2, 3, 3);
}
TEST_F(AuthSrvTest,
#ifdef USE_STATIC_LINK
DISABLED_chQueryWithInMemoryClient
#else
chQueryWithInMemoryClient
#endif
)
{
// Set up the in-memory
updateInMemory(datasrc_configurator_, "example.", CONFIG_INMEMORY_EXAMPLE);
// This shouldn't affect the result of class CH query
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("VERSION.BIND."),
RRClass::CH(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 1, 0);
}
// Submit UDP normal query and check query counter
TEST_F(AuthSrvTest, queryCounterUDPNormal) {
// The counter should be initialized to 0.
EXPECT_EQ(0, server.getCounter(AuthCounters::SERVER_UDP_QUERY));
// Create UDP message and process.
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
// After processing UDP query, the counter should be 1.
EXPECT_EQ(1, server.getCounter(AuthCounters::SERVER_UDP_QUERY));
// The counter for opcode Query should also be one
EXPECT_EQ(1, server.getCounter(Opcode::QUERY()));
// The counter for REFUSED responses should also be one, the rest zero
checkAllRcodeCountersZeroExcept(Rcode::REFUSED(), 1);
}
// Submit TCP normal query and check query counter
TEST_F(AuthSrvTest, queryCounterTCPNormal) {
// The counter should be initialized to 0.
EXPECT_EQ(0, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
// Create TCP message and process.
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
createRequestPacket(request_message, IPPROTO_TCP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
// After processing TCP query, the counter should be 1.
EXPECT_EQ(1, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
// The counter for SUCCESS responses should also be one
EXPECT_EQ(1, server.getCounter(Opcode::QUERY()));
// The counter for REFUSED responses should also be one, the rest zero
checkAllRcodeCountersZeroExcept(Rcode::REFUSED(), 1);
}
// Submit TCP AXFR query and check query counter
TEST_F(AuthSrvTest, queryCounterTCPAXFR) {
// The counter should be initialized to 0.
EXPECT_EQ(0, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(), RRType::AXFR());
createRequestPacket(request_message, IPPROTO_TCP);
// On success, the AXFR query has been passed to a separate process,
// so auth itself shouldn't respond.
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
// After processing TCP AXFR query, the counter should be 1.
EXPECT_EQ(1, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
// No rcodes should be incremented
checkAllRcodeCountersZero();
}
// Submit TCP IXFR query and check query counter
TEST_F(AuthSrvTest, queryCounterTCPIXFR) {
// The counter should be initialized to 0.
EXPECT_EQ(0, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
Name("example.com"), RRClass::IN(), RRType::IXFR());
createRequestPacket(request_message, IPPROTO_TCP);
// On success, the IXFR query has been passed to a separate process,
// so auth itself shouldn't respond.
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
// After processing TCP IXFR query, the counter should be 1.
EXPECT_EQ(1, server.getCounter(AuthCounters::SERVER_TCP_QUERY));
}
TEST_F(AuthSrvTest, queryCounterOpcodes) {
for (int i = 0; i < 16; ++i) {
// The counter should be initialized to 0.
EXPECT_EQ(0, server.getCounter(Opcode(i)));
// For each possible opcode, create a request message and send it
UnitTestUtil::createRequestMessage(request_message, Opcode(i),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
createRequestPacket(request_message, IPPROTO_UDP);
// "send" the request N-th times where N is i + 1 for i-th code.
// we intentionally use different values for each code
for (int j = 0; j <= i; ++j) {
parse_message->clear(Message::PARSE);
server.processMessage(*io_message, *parse_message,
*response_obuffer,
&dnsserv);
}
// Confirm the counter.
EXPECT_EQ(i + 1, server.getCounter(Opcode(i)));
}
}
// class for queryCounterUnexpected test
// getProtocol() returns IPPROTO_IP
class DummyUnknownSocket : public IOSocket {
public:
DummyUnknownSocket() {}
virtual int getNative() const { return (0); }
virtual int getProtocol() const { return (IPPROTO_IP); }
};
// function for queryCounterUnexpected test
// returns a reference to a static object of DummyUnknownSocket
IOSocket&
getDummyUnknownSocket() {
static DummyUnknownSocket socket;
return (socket);
}
// Submit unexpected type of query and check it is ignored
TEST_F(AuthSrvTest, queryCounterUnexpected) {
// This code isn't exception safe, but we'd rather keep the code
// simpler and more readable as this is only for tests
// Create UDP query packet.
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
createRequestPacket(request_message, IPPROTO_UDP);
// Modify the message.
delete io_message;
endpoint = IOEndpoint::create(IPPROTO_UDP,
IOAddress(DEFAULT_REMOTE_ADDRESS), 53210);
io_message = new IOMessage(request_renderer.getData(),
request_renderer.getLength(),
getDummyUnknownSocket(), *endpoint);
EXPECT_FALSE(dnsserv.hasAnswer());
}
TEST_F(AuthSrvTest, stop) {
// normal case is covered in command_unittest.cc. we should primarily
// test it here, but the current design of the stop test takes time,
// so we consolidate the cases in the command tests.
// If/when the interval timer has finer granularity we'll probably add
// our own tests here, so we keep this empty test case.
}
TEST_F(AuthSrvTest, listenAddresses) {
isc::testutils::portconfig::listenAddresses(server);
// Check it requests the correct addresses
const char* tokens[] = {
"TCP:127.0.0.1:53210:1",
"UDP:127.0.0.1:53210:2",
"TCP:::1:53210:3",
"UDP:::1:53210:4",
NULL
};
sock_requestor_.checkTokens(tokens, sock_requestor_.given_tokens_,
"Given tokens");
// It returns back to empty set of addresses afterwards, so
// they should be released
sock_requestor_.checkTokens(tokens, sock_requestor_.released_tokens_,
"Released tokens");
}
TEST_F(AuthSrvTest, processNormalQuery_reuseRenderer1) {
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::NS());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_NE(request_message.getRcode(), parse_message->getRcode());
}
TEST_F(AuthSrvTest, processNormalQuery_reuseRenderer2) {
UnitTestUtil::createRequestMessage(request_message, Opcode::QUERY(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
request_message.setHeaderFlag(Message::HEADERFLAG_AA);
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
ConstQuestionPtr question = *parse_message->beginQuestion();
EXPECT_STRNE(question->getType().toText().c_str(),
RRType::NS().toText().c_str());
}
//
// Tests for catching exceptions in various stages of the query processing
//
// These tests work by defining two proxy classes, that act as an in-memory
// client by default, but can throw exceptions at various points.
//
namespace {
/// The possible methods to throw in, either in FakeClient or
/// FakeZoneFinder
enum ThrowWhen {
THROW_NEVER,
THROW_AT_FIND_ZONE,
THROW_AT_GET_ORIGIN,
THROW_AT_GET_CLASS,
THROW_AT_FIND,
THROW_AT_FIND_ALL,
THROW_AT_FIND_NSEC3
};
/// convenience function to check whether and what to throw
void
checkThrow(ThrowWhen method, ThrowWhen throw_at, bool isc_exception) {
if (method == throw_at) {
if (isc_exception) {
isc_throw(isc::Exception, "foo");
} else {
throw std::exception();
}
}
}
/// \brief proxy class for the ZoneFinder returned by the Client
/// proxied by FakeClient
///
/// See the documentation for FakeClient for more information,
/// all methods simply check whether they should throw, and if not, call
/// their proxied equivalent.
class FakeZoneFinder : public isc::datasrc::ZoneFinder {
public:
FakeZoneFinder(isc::datasrc::ZoneFinderPtr zone_finder,
ThrowWhen throw_when, bool isc_exception,
ConstRRsetPtr fake_rrset) :
real_zone_finder_(zone_finder),
throw_when_(throw_when),
isc_exception_(isc_exception),
fake_rrset_(fake_rrset)
{}
virtual isc::dns::Name
getOrigin() const {
checkThrow(THROW_AT_GET_ORIGIN, throw_when_, isc_exception_);
return (real_zone_finder_->getOrigin());
}
virtual isc::dns::RRClass
getClass() const {
checkThrow(THROW_AT_GET_CLASS, throw_when_, isc_exception_);
return (real_zone_finder_->getClass());
}
virtual isc::datasrc::ZoneFinderContextPtr
find(const isc::dns::Name& name,
const isc::dns::RRType& type,
isc::datasrc::ZoneFinder::FindOptions options)
{
using namespace isc::datasrc;
checkThrow(THROW_AT_FIND, throw_when_, isc_exception_);
// If faked RRset was specified on construction and it matches the
// query, return it instead of searching the real data source.
if (fake_rrset_ && fake_rrset_->getName() == name &&
fake_rrset_->getType() == type)
{
return (ZoneFinderContextPtr(new ZoneFinder::Context(
*this, options,
ResultContext(SUCCESS,
fake_rrset_))));
}
return (real_zone_finder_->find(name, type, options));
}
virtual isc::datasrc::ZoneFinderContextPtr
findAll(const isc::dns::Name& name,
std::vector<isc::dns::ConstRRsetPtr> &target,
const FindOptions options = FIND_DEFAULT)
{
checkThrow(THROW_AT_FIND_ALL, throw_when_, isc_exception_);
return (real_zone_finder_->findAll(name, target, options));
}
virtual FindNSEC3Result
findNSEC3(const isc::dns::Name& name, bool recursive) {
checkThrow(THROW_AT_FIND_NSEC3, throw_when_, isc_exception_);
return (real_zone_finder_->findNSEC3(name, recursive));
}
private:
isc::datasrc::ZoneFinderPtr real_zone_finder_;
ThrowWhen throw_when_;
bool isc_exception_;
ConstRRsetPtr fake_rrset_;
};
/// \brief Proxy FakeClient that can throw exceptions at specified times
///
/// Currently it is used as an 'InMemoryClient' using setInMemoryClient,
/// but it is in effect a general datasource client.
class FakeClient : public isc::datasrc::DataSourceClient {
public:
/// \brief Create a proxy memory client
///
/// \param real_client The real (in-memory) client to proxy
/// \param throw_when if set to any value other than never, that is
/// the method that will throw an exception (either in this
/// class or the related FakeZoneFinder)
/// \param isc_exception if true, throw isc::Exception, otherwise,
/// throw std::exception
/// \param fake_rrset If non NULL, it will be used as an answer to
/// find() for that name and type.
FakeClient(const DataSourceClient* real_client,
ThrowWhen throw_when, bool isc_exception,
ConstRRsetPtr fake_rrset = ConstRRsetPtr()) :
real_client_ptr_(real_client),
throw_when_(throw_when),
isc_exception_(isc_exception),
fake_rrset_(fake_rrset)
{}
/// \brief proxy call for findZone
///
/// if this instance was constructed with throw_when set to find_zone,
/// this method will throw. Otherwise, it will return a FakeZoneFinder
/// instance which will throw at the method specified at the
/// construction of this instance.
virtual FindResult
findZone(const isc::dns::Name& name) const {
checkThrow(THROW_AT_FIND_ZONE, throw_when_, isc_exception_);
const FindResult result =
real_client_ptr_->findZone(name);
return (FindResult(result.code, isc::datasrc::ZoneFinderPtr(
new FakeZoneFinder(result.zone_finder,
throw_when_,
isc_exception_,
fake_rrset_))));
}
isc::datasrc::ZoneUpdaterPtr
getUpdater(const isc::dns::Name&, bool, bool) const {
isc_throw(isc::NotImplemented,
"Update attempt on in fake data source");
}
std::pair<isc::datasrc::ZoneJournalReader::Result,
isc::datasrc::ZoneJournalReaderPtr>
getJournalReader(const isc::dns::Name&, uint32_t, uint32_t) const {
isc_throw(isc::NotImplemented, "Journaling isn't supported for "
"fake data source");
}
private:
const DataSourceClient* real_client_ptr_;
ThrowWhen throw_when_;
bool isc_exception_;
ConstRRsetPtr fake_rrset_;
};
class FakeList : public isc::datasrc::ConfigurableClientList {
public:
/// \brief Creates a fake list for the given in-memory client
///
/// It will create a FakeClient for each client in the original list,
/// with the given arguments, which is used when searching for the
/// corresponding data source.
FakeList(const boost::shared_ptr<isc::datasrc::ConfigurableClientList>
real_list, ThrowWhen throw_when, bool isc_exception,
ConstRRsetPtr fake_rrset = ConstRRsetPtr()) :
ConfigurableClientList(RRClass::IN()),
real_(real_list)
{
BOOST_FOREACH(const DataSourceInfo& info, real_->getDataSources()) {
const isc::datasrc::DataSourceClientPtr
client(new FakeClient(info.data_src_client_ != NULL ?
info.data_src_client_ :
info.getCacheClient(),
throw_when, isc_exception, fake_rrset));
clients_.push_back(client);
data_sources_.push_back(
DataSourceInfo(client.get(),
isc::datasrc::DataSourceClientContainerPtr(),
false, RRClass::IN(), mem_sgmt_));
}
}
private:
const boost::shared_ptr<isc::datasrc::ConfigurableClientList> real_;
vector<isc::datasrc::DataSourceClientPtr> clients_;
MemorySegmentLocal mem_sgmt_;
};
} // end anonymous namespace for throwing proxy classes
// Test for the tests
//
// Set the proxies to never throw, this should have the same result as
// queryWithInMemoryClientNoDNSSEC, and serves to test the two proxy classes
TEST_F(AuthSrvTest,
#ifdef USE_STATIC_LINK
DISABLED_queryWithInMemoryClientProxy
#else
queryWithInMemoryClientProxy
#endif
)
{
// Set real inmem client to proxy
updateInMemory(datasrc_configurator_, "example.", CONFIG_INMEMORY_EXAMPLE);
{
isc::util::thread::Mutex::Locker locker(server.getClientListMutex());
boost::shared_ptr<isc::datasrc::ConfigurableClientList>
list(new FakeList(server.getClientList(RRClass::IN()), THROW_NEVER,
false));
server.setClientList(RRClass::IN(), list);
}
createDataFromFile("nsec3query_nodnssec_fromWire.wire");
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 2, 1);
}
// Convenience function for the rest of the tests, set up a proxy
// to throw in the given method
// If isc_exception is true, it will throw isc::Exception, otherwise
// it will throw std::exception
// If non null rrset is given, it will be passed to the proxy so it can
// return some faked response.
void
setupThrow(AuthSrv* server, DataSourceConfigurator& datasrc_configurator,
ThrowWhen throw_when, bool isc_exception,
ConstRRsetPtr rrset = ConstRRsetPtr())
{
updateInMemory(datasrc_configurator, "example.", CONFIG_INMEMORY_EXAMPLE);
isc::util::thread::Mutex::Locker locker(server->getClientListMutex());
boost::shared_ptr<isc::datasrc::ConfigurableClientList>
list(new FakeList(server->getClientList(RRClass::IN()), throw_when,
isc_exception, rrset));
server->setClientList(RRClass::IN(), list);
}
TEST_F(AuthSrvTest,
#ifdef USE_STATIC_LINK
DISABLED_queryWithThrowingProxyServfails
#else
queryWithThrowingProxyServfails
#endif
)
{
// Test the common cases, all of which should simply return SERVFAIL
// Use THROW_NEVER as end marker
ThrowWhen throws[] = { THROW_AT_FIND_ZONE,
THROW_AT_GET_ORIGIN,
THROW_AT_FIND,
THROW_AT_FIND_NSEC3,
THROW_NEVER };
UnitTestUtil::createDNSSECRequestMessage(request_message, opcode,
default_qid, Name("foo.example."),
RRClass::IN(), RRType::TXT());
for (ThrowWhen* when(throws); *when != THROW_NEVER; ++when) {
createRequestPacket(request_message, IPPROTO_UDP);
setupThrow(&server, datasrc_configurator_, *when, true);
processAndCheckSERVFAIL();
// To be sure, check same for non-isc-exceptions
createRequestPacket(request_message, IPPROTO_UDP);
setupThrow(&server, datasrc_configurator_, *when, false);
processAndCheckSERVFAIL();
}
}
// Throw isc::Exception in getClass(). (Currently?) getClass is not called
// in the processMessage path, so this should result in a normal answer
TEST_F(AuthSrvTest,
#ifdef USE_STATIC_LINK
DISABLED_queryWithInMemoryClientProxyGetClass
#else
queryWithInMemoryClientProxyGetClass
#endif
)
{
createDataFromFile("nsec3query_nodnssec_fromWire.wire");
setupThrow(&server, datasrc_configurator_, THROW_AT_GET_CLASS, true);
// getClass is not called so it should just answer
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOERROR(),
opcode.getCode(), QR_FLAG | AA_FLAG, 1, 1, 2, 1);
}
TEST_F(AuthSrvTest,
#ifdef USE_STATIC_LINK
DISABLED_queryWithThrowingInToWire
#else
queryWithThrowingInToWire
#endif
)
{
// Set up a faked data source. It will return an empty RRset for the
// query.
ConstRRsetPtr empty_rrset(new RRset(Name("foo.example"),
RRClass::IN(), RRType::TXT(),
RRTTL(0)));
setupThrow(&server, datasrc_configurator_, THROW_NEVER, true, empty_rrset);
// Repeat the query processing two times. Due to the faked RRset,
// toWire() should throw, and it should result in SERVFAIL.
OutputBufferPtr orig_buffer;
for (int i = 0; i < 2; ++i) {
UnitTestUtil::createDNSSECRequestMessage(request_message, opcode,
default_qid,
Name("foo.example."),
RRClass::IN(), RRType::TXT());
createRequestPacket(request_message, IPPROTO_UDP);
server.processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
// Make a backup of the original buffer for latest tests and replace
// it with a new one
if (!orig_buffer) {
orig_buffer = response_obuffer;
response_obuffer.reset(new OutputBuffer(0));
}
request_message.clear(Message::RENDER);
parse_message->clear(Message::PARSE);
}
// Now check if the original buffer is intact
parse_message->clear(Message::PARSE);
InputBuffer ibuffer(orig_buffer->getData(), orig_buffer->getLength());
parse_message->fromWire(ibuffer);
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
opcode.getCode(), QR_FLAG, 1, 0, 0, 0);
}
//
// DDNS related tests
//
// Helper subroutine to check if the given socket address has the expected
// address and port. It depends on specific output of getnameinfo() (while
// there can be multiple textual representation of the same address) but
// in practice it should be reliable.
void
checkAddrPort(const struct sockaddr& actual_sa,
const string& expected_addr, uint16_t expected_port)
{
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
const int error = getnameinfo(&actual_sa, getSALength(actual_sa), hbuf,
sizeof(hbuf), sbuf, sizeof(sbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
if (error != 0) {
isc_throw(isc::Unexpected, "getnameinfo failed: " <<
gai_strerror(error));
}
EXPECT_EQ(expected_addr, hbuf);
EXPECT_EQ(boost::lexical_cast<string>(expected_port), sbuf);
}
TEST_F(AuthSrvTest, DDNSForward) {
EXPECT_FALSE(ddns_forwarder.isConnected());
// Repeat sending an update request 4 times, differing some network
// parameters: UDP/IPv4, TCP/IPv4, UDP/IPv6, TCP/IPv6, in this order.
// By doing that we can also confirm the forwarder connection will be
// established exactly once, and kept established.
for (size_t i = 0; i < 4; ++i) {
// Use different names for some different cases
const Name zone_name = Name(i < 2 ? "example.com" : "example.org");
const socklen_t family = (i < 2) ? AF_INET : AF_INET6;
const char* const remote_addr =
(family == AF_INET) ? "192.0.2.1" : "2001:db8::1";
const uint16_t remote_port =
(family == AF_INET) ? 53214 : 53216;
const int protocol = ((i % 2) == 0) ? IPPROTO_UDP : IPPROTO_TCP;
createAndSendRequest(RRType::SOA(), Opcode::UPDATE(), zone_name,
RRClass::IN(), protocol, remote_addr,
remote_port);
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
// Examine the pushed data (note: currently "local end" has a dummy
// value equal to remote)
EXPECT_EQ(family, ddns_forwarder.getPushedFamily());
const int expected_type =
(protocol == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
EXPECT_EQ(expected_type, ddns_forwarder.getPushedType());
EXPECT_EQ(protocol, ddns_forwarder.getPushedProtocol());
checkAddrPort(ddns_forwarder.getPushedRemoteend(),
remote_addr, remote_port);
checkAddrPort(ddns_forwarder.getPushedLocalend(),
remote_addr, remote_port);
EXPECT_EQ(io_message->getDataSize(),
ddns_forwarder.getPushedData().size());
EXPECT_EQ(0, memcmp(io_message->getData(),
&ddns_forwarder.getPushedData()[0],
ddns_forwarder.getPushedData().size()));
}
}
TEST_F(AuthSrvTest, DDNSForwardConnectFail) {
// make connect attempt fail. It should result in SERVFAIL. Note that
// the question (zone) section should be cleared for opcode of update.
ddns_forwarder.disableConnect();
createAndSendRequest(RRType::SOA(), Opcode::UPDATE());
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
Opcode::UPDATE().getCode(), QR_FLAG, 0, 0, 0, 0);
EXPECT_FALSE(ddns_forwarder.isConnected());
// Now make connect okay again. Despite the previous failure the new
// connection should now be established.
ddns_forwarder.enableConnect();
createAndSendRequest(RRType::SOA(), Opcode::UPDATE());
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
}
TEST_F(AuthSrvTest, DDNSForwardPushFail) {
// Make first request succeed, which will establish the connection.
EXPECT_FALSE(ddns_forwarder.isConnected());
createAndSendRequest(RRType::SOA(), Opcode::UPDATE());
EXPECT_TRUE(ddns_forwarder.isConnected());
// make connect attempt fail. It should result in SERVFAIL. The
// connection should be closed. Use IPv6 address for varying log output.
ddns_forwarder.disablePush();
createAndSendRequest(RRType::SOA(), Opcode::UPDATE(), Name("example.com"),
RRClass::IN(), IPPROTO_UDP, "2001:db8::2");
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::SERVFAIL(),
Opcode::UPDATE().getCode(), QR_FLAG, 0, 0, 0, 0);
EXPECT_FALSE(ddns_forwarder.isConnected());
// Allow push again. Connection will be reopened, and the request will
// be forwarded successfully.
ddns_forwarder.enablePush();
createAndSendRequest(RRType::SOA(), Opcode::UPDATE());
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
}
TEST_F(AuthSrvTest, DDNSForwardClose) {
scoped_ptr<AuthSrv> tmp_server(new AuthSrv(xfrout, ddns_forwarder));
tmp_server->createDDNSForwarder();
UnitTestUtil::createRequestMessage(request_message, Opcode::UPDATE(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
createRequestPacket(request_message, IPPROTO_UDP);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
// Destroy the server. The forwarder should close the connection.
tmp_server.reset();
EXPECT_FALSE(ddns_forwarder.isConnected());
}
namespace {
// Send a basic command without arguments, and check the response has
// result code 0
void sendSimpleCommand(AuthSrv& server, const std::string& command) {
ConstElementPtr response = execAuthServerCommand(server, command,
ConstElementPtr());
int command_result = -1;
isc::config::parseAnswer(command_result, response);
EXPECT_EQ(0, command_result);
}
} // end anonymous namespace
TEST_F(AuthSrvTest, DDNSForwardCreateDestroy) {
// Test that AuthSrv returns NOTIMP before ddns forwarder is created,
// that the ddns_forwarder is connected when the 'start_ddns_forwarder'
// command has been sent, and that it is no longer connected and auth
// returns NOTIMP after the stop_ddns_forwarding command is sent.
scoped_ptr<AuthSrv> tmp_server(new AuthSrv(xfrout, ddns_forwarder));
// Prepare update message to send
UnitTestUtil::createRequestMessage(request_message, Opcode::UPDATE(),
default_qid, Name("example.com"),
RRClass::IN(), RRType::SOA());
createRequestPacket(request_message, IPPROTO_UDP);
// before creating forwarder. isConnected() should be false and
// rcode to UPDATE should be NOTIMP
parse_message->clear(Message::PARSE);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(ddns_forwarder.isConnected());
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOTIMP(),
Opcode::UPDATE().getCode(), QR_FLAG, 0, 0, 0, 0);
// now create forwarder
sendSimpleCommand(*tmp_server, "start_ddns_forwarder");
// our mock does not respond, and since auth is supposed to send it on,
// there should now be no result when an UPDATE is sent
parse_message->clear(Message::PARSE);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
// If we send a start again, the connection should be recreated,
// visible because isConnected() reports false until an actual message
// has been forwarded
sendSimpleCommand(*tmp_server, "start_ddns_forwarder");
EXPECT_FALSE(ddns_forwarder.isConnected());
parse_message->clear(Message::PARSE);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(dnsserv.hasAnswer());
EXPECT_TRUE(ddns_forwarder.isConnected());
// Now tell it to stop forwarder, should respond with NOTIMP again
sendSimpleCommand(*tmp_server, "stop_ddns_forwarder");
parse_message->clear(Message::PARSE);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(ddns_forwarder.isConnected());
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOTIMP(),
Opcode::UPDATE().getCode(), QR_FLAG, 0, 0, 0, 0);
// Sending stop again should make no difference
sendSimpleCommand(*tmp_server, "stop_ddns_forwarder");
parse_message->clear(Message::PARSE);
tmp_server->processMessage(*io_message, *parse_message, *response_obuffer,
&dnsserv);
EXPECT_FALSE(ddns_forwarder.isConnected());
EXPECT_TRUE(dnsserv.hasAnswer());
headerCheck(*parse_message, default_qid, Rcode::NOTIMP(),
Opcode::UPDATE().getCode(), QR_FLAG, 0, 0, 0, 0);
}
// Check the client list accessors
TEST_F(AuthSrvTest, clientList) {
// We need to lock the mutex to make the (get|set)ClientList happy.
// There's a debug-build only check in them to make sure everything
// locks them and we call them directly here.
isc::util::thread::Mutex::Locker locker(server.getClientListMutex());
// The lists don't exist. Therefore, the list of RRClasses is empty.
// We also have no IN list.
EXPECT_TRUE(server.getClientListClasses().empty());
EXPECT_EQ(boost::shared_ptr<const isc::datasrc::ClientList>(),
server.getClientList(RRClass::IN()));
// Put something in.
const boost::shared_ptr<isc::datasrc::ConfigurableClientList>
list(new isc::datasrc::ConfigurableClientList(RRClass::IN()));
const boost::shared_ptr<isc::datasrc::ConfigurableClientList>
list2(new isc::datasrc::ConfigurableClientList(RRClass::CH()));
server.setClientList(RRClass::IN(), list);
server.setClientList(RRClass::CH(), list2);
// There are two things in the list and they are IN and CH
vector<RRClass> classes(server.getClientListClasses());
ASSERT_EQ(2, classes.size());
EXPECT_EQ(RRClass::IN(), classes[0]);
EXPECT_EQ(RRClass::CH(), classes[1]);
// And the lists can be retrieved.
EXPECT_EQ(list, server.getClientList(RRClass::IN()));
EXPECT_EQ(list2, server.getClientList(RRClass::CH()));
// Remove one of them
server.setClientList(RRClass::CH(),
boost::shared_ptr<isc::datasrc::ConfigurableClientList>());
// This really got deleted, including the class.
classes = server.getClientListClasses();
ASSERT_EQ(1, classes.size());
EXPECT_EQ(RRClass::IN(), classes[0]);
EXPECT_EQ(list, server.getClientList(RRClass::IN()));
}
// We just test the mutex can be locked (exactly once).
TEST_F(AuthSrvTest, mutex) {
isc::util::thread::Mutex::Locker l1(server.getClientListMutex());
// TODO: Once we have non-debug build, this one will not work, since
// we currently use the fact that we can't lock twice from the same
// thread. In the non-debug mode, this would deadlock.
// Skip then.
EXPECT_THROW({
isc::util::thread::Mutex::Locker l2(server.getClientListMutex());
}, isc::InvalidOperation);
}
}
|