]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-ednscookie_cc.cc
rec: mention rust compiler in compiling docs
[thirdparty/pdns.git] / pdns / test-ednscookie_cc.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #ifndef BOOST_TEST_DYN_LINK
23 #define BOOST_TEST_DYN_LINK
24 #endif
25
26 #define BOOST_TEST_NO_MAIN
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include "ednscookies.hh"
32
33 #include <boost/test/unit_test.hpp>
34
35 BOOST_AUTO_TEST_SUITE(test_ednscookie)
36 BOOST_AUTO_TEST_CASE(test_getEDNSCookiesOptFromString)
37 {
38 string cookie("");
39 EDNSCookiesOpt eco(cookie);
40 // Length 0
41 BOOST_CHECK(!eco.isWellFormed());
42
43 // Too short
44 cookie = "\x12\x34\x56\x78\x90\xab\xcd";
45 BOOST_CHECK(!eco.makeFromString(cookie));
46
47 // Correct length client cookie
48 cookie = "\x12\x34\x56\x78\x90\xab\xcd\xef";
49 BOOST_CHECK(eco.makeFromString(cookie));
50
51 // Too short server cookie
52 cookie = "\x12\x34\x56\x78\x90\xab\xcd\xef\x01";
53 BOOST_CHECK(!eco.makeFromString(cookie));
54
55 cookie = "\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd";
56 BOOST_CHECK(!eco.makeFromString(cookie));
57
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));
62
63 cookie += cookie; // size 32
64 BOOST_CHECK(eco.makeFromString(cookie));
65
66 cookie += "\x12\x34\x56\x78\x90\xab\xcd\xef"; // size 40 (the max)
67 BOOST_CHECK(eco.makeFromString(cookie));
68
69 // Cookie total size too long
70 cookie += "\x01";
71 BOOST_CHECK(!eco.makeFromString(cookie));
72 }
73
74 BOOST_AUTO_TEST_CASE(test_ctor)
75 {
76 string cookie("");
77 auto eco = EDNSCookiesOpt(cookie);
78 BOOST_CHECK(!eco.isWellFormed());
79
80 eco = EDNSCookiesOpt("\x12\x34\x56\x78\x90\xab\xcd\xef");
81 BOOST_CHECK(eco.isWellFormed());
82 BOOST_CHECK_EQUAL(8U, eco.makeOptString().length());
83 }
84
85 #ifdef HAVE_CRYPTO_SHORTHASH
86 BOOST_AUTO_TEST_CASE(test_createEDNSServerCookie)
87 {
88 auto eco = EDNSCookiesOpt("\x12\x34\x56\x78\x90\xab\xcd\xef");
89 ComboAddress remote("192.0.2.2");
90
91 BOOST_CHECK(eco.isWellFormed());
92
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));
98
99 secret = "blablablablablab";
100 BOOST_CHECK(eco.makeServerCookie(secret, remote));
101 BOOST_CHECK(eco.isWellFormed());
102 BOOST_CHECK(eco.isValid(secret, remote));
103
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));
108 }
109 #endif
110
111 BOOST_AUTO_TEST_SUITE_END()