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