]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-ipcrypt_cc.cc
Merge pull request #9073 from pieterlexis/runtime-dirs-virtual-hosting
[thirdparty/pdns.git] / pdns / test-ipcrypt_cc.cc
1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_NO_MAIN
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 #include <boost/test/unit_test.hpp>
7 #include "ipcipher.hh"
8 #include "misc.hh"
9
10 using namespace boost;
11
12 BOOST_AUTO_TEST_SUITE(test_ipcrypt_hh)
13
14 BOOST_AUTO_TEST_CASE(test_ipcrypt4)
15 {
16 ComboAddress ca("127.0.0.1");
17 std::string key="0123456789ABCDEF";
18 auto encrypted = encryptCA(ca, key);
19
20 auto decrypted = decryptCA(encrypted, key);
21 BOOST_CHECK_EQUAL(ca.toString(), decrypted.toString());
22 }
23
24 BOOST_AUTO_TEST_CASE(test_ipcrypt4_vector)
25 {
26 vector<pair<string,string>> tests{ // test vector from https://github.com/veorq/ipcrypt
27 {{"127.0.0.1"},{"114.62.227.59"}},
28 {{"8.8.8.8"}, {"46.48.51.50"}},
29 {{"1.2.3.4"}, {"171.238.15.199"}}};
30
31 std::string key="some 16-byte key";
32
33 for(const auto& p : tests) {
34 auto encrypted = encryptCA(ComboAddress(p.first), key);
35 BOOST_CHECK_EQUAL(encrypted.toString(), p.second);
36 auto decrypted = decryptCA(encrypted, key);
37 BOOST_CHECK_EQUAL(decrypted.toString(), p.first);
38 }
39
40 // test from Frank Denis' test.cc
41 ComboAddress ip("192.168.69.42"), out, dec;
42 string key2;
43 for(int n=0; n<16; ++n)
44 key2.append(1, (char)n+1);
45
46 for (unsigned int i = 0; i < 100000000UL; i++) {
47 out=encryptCA(ip, key2);
48 // dec=decryptCA(out, key2);
49 // BOOST_CHECK(ip==dec);
50 ip=out;
51 }
52
53 ComboAddress expected("93.155.197.186");
54
55 BOOST_CHECK_EQUAL(ip.toString(), expected.toString());
56 }
57
58
59 BOOST_AUTO_TEST_CASE(test_ipcrypt6)
60 {
61 ComboAddress ca("::1");
62 std::string key="0123456789ABCDEF";
63 auto encrypted = encryptCA(ca, key);
64
65 auto decrypted = decryptCA(encrypted, key);
66 BOOST_CHECK_EQUAL(ca.toString(), decrypted.toString());
67 }
68
69
70 BOOST_AUTO_TEST_SUITE_END()