2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef BOOST_TEST_DYN_LINK
23 #define BOOST_TEST_DYN_LINK
26 #define BOOST_TEST_NO_MAIN
31 #include "ednscookies.hh"
33 #include <boost/test/unit_test.hpp>
35 BOOST_AUTO_TEST_SUITE(test_ednscookie
)
36 BOOST_AUTO_TEST_CASE(test_getEDNSCookiesOptFromString
)
39 EDNSCookiesOpt
eco(cookie
);
41 BOOST_CHECK(!eco
.isWellFormed());
44 cookie
= "\x12\x34\x56\x78\x90\xab\xcd";
45 BOOST_CHECK(!eco
.makeFromString(cookie
));
47 // Correct length client cookie
48 cookie
= "\x12\x34\x56\x78\x90\xab\xcd\xef";
49 BOOST_CHECK(eco
.makeFromString(cookie
));
51 // Too short server cookie
52 cookie
= "\x12\x34\x56\x78\x90\xab\xcd\xef\x01";
53 BOOST_CHECK(!eco
.makeFromString(cookie
));
55 cookie
= "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd";
56 BOOST_CHECK(!eco
.makeFromString(cookie
));
58 // Have server cookie of correct length
59 cookie
= "\x12\x34\x56\x78\x90\xab\xcd\xef";
60 cookie
+= cookie
; // size 16
61 BOOST_CHECK(eco
.makeFromString(cookie
));
63 cookie
+= cookie
; // size 32
64 BOOST_CHECK(eco
.makeFromString(cookie
));
66 cookie
+= "\x12\x34\x56\x78\x90\xab\xcd\xef"; // size 40 (the max)
67 BOOST_CHECK(eco
.makeFromString(cookie
));
69 // Cookie total size too long
71 BOOST_CHECK(!eco
.makeFromString(cookie
));
74 BOOST_AUTO_TEST_CASE(test_ctor
)
77 auto eco
= EDNSCookiesOpt(cookie
);
78 BOOST_CHECK(!eco
.isWellFormed());
80 eco
= EDNSCookiesOpt("\x12\x34\x56\x78\x90\xab\xcd\xef");
81 BOOST_CHECK(eco
.isWellFormed());
82 BOOST_CHECK_EQUAL(8U, eco
.makeOptString().length());
85 #ifdef HAVE_CRYPTO_SHORTHASH
86 BOOST_AUTO_TEST_CASE(test_createEDNSServerCookie
)
88 auto eco
= EDNSCookiesOpt("\x12\x34\x56\x78\x90\xab\xcd\xef");
89 ComboAddress
remote("192.0.2.2");
91 BOOST_CHECK(eco
.isWellFormed());
93 // wrong keysize (not 128 bits)
94 string secret
= "blablablabla";
95 BOOST_CHECK(!eco
.makeServerCookie(secret
, remote
));
96 BOOST_CHECK(eco
.isWellFormed());
97 BOOST_CHECK(!eco
.isValid(secret
, remote
));
99 secret
= "blablablablablab";
100 BOOST_CHECK(eco
.makeServerCookie(secret
, remote
));
101 BOOST_CHECK(eco
.isWellFormed());
102 BOOST_CHECK(eco
.isValid(secret
, remote
));
104 EDNSCookiesOpt
eco2(eco
.makeOptString());
105 BOOST_CHECK(!eco2
.isValid(secret
, ComboAddress("192.0.2.1")));
106 BOOST_CHECK(!eco2
.isValid("blablablablabla1", remote
));
107 BOOST_CHECK(eco2
.isValid(secret
, remote
));
111 BOOST_AUTO_TEST_SUITE_END()