OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_session_id.cpp
Go to the documentation of this file.
1#include "test_common.hpp"
2
5#include <unordered_map>
7
8using namespace openvpn;
9
10TEST(Sessid, Test1)
11{
12 SSLLib::RandomAPI rng;
13
14 // test 1
15 {
16 const SessionID64 sid1(rng);
17 // std::cout << "SID1: " << sid1 << "\n";
18
19 const SessionID64 sid2(sid1.to_string());
20 ASSERT_TRUE(sid1.defined() && sid2.defined()) << "FAIL sid1 or sid2 is undefined";
21 ASSERT_EQ(sid1, sid2);
22
23 const SessionID128 sid3(rng);
24 ASSERT_FALSE(sid1.eq_weak(sid3)) << "FAIL sid1 ~== sid3";
25 ASSERT_FALSE(sid3.eq_weak(sid1)) << "FAIL sid3 ~== sid1";
26
27 for (int i = 1; i <= 4; ++i)
28 {
29 // std::cout << "---- " << i << " ----\n";
30 const TokenEncrypt::Key key(rng);
31 TokenEncryptDecrypt ted(key);
32 const SessionID128 sid3_enc(sid3, ted.encrypt);
33 // std::cout << "SID3 (enc): " << sid3_enc << "\n";
34 const SessionID128 sid3_dec(sid3_enc, ted.decrypt);
35 // std::cout << "SID3 (dec): " << sid3_dec << "\n";
36 }
37 }
38}
39TEST(Sessid, Test2)
40{
41 SSLLib::RandomAPI rng;
42 {
43 const SessionID64 sid1(rng);
44 // std::cout << "SID1: " << sid1 << "\n";
45 const SessionID128 sid2(rng);
46 // std::cout << "SID2: " << sid2 << "\n";
47
48 const SessionID128 sid1_exp(sid1);
49 // std::cout << "SID1_EXP: " << sid1_exp << "\n";
50 const SessionID64 sid2_trunc(sid2);
51 // std::cout << "SID2_TRUNC: " << sid2_trunc << "\n";
52 }
53}
54
55TEST(Sessid, Test3)
56{
57 const SessionID64 ns;
58 ASSERT_FALSE(ns.defined()) << "FAIL default constructed SessionID is defined";
59}
60
61TEST(Sessid, Test4)
62{
63 const SessionID128 x;
64 const SessionID128 a("YmtN7B2edrDRlefk3vQ_YQ..");
65 const SessionID128 b("YmtN7B2edrDRlefk3vQ_YA..");
66 const SessionID64 c("YmtN7B2edrA.");
67 const SessionID128 d(c);
68 /*std::cout << "a: " << a <<
69 std::endl;
70 std::cout << "b: " << b <<
71 std::endl;
72 std::cout << "c: " << c <<
73 std::endl;
74 std::cout << "d: " << d <<
75 std::endl; */
76 ASSERT_FALSE(a == b) << "test4: wrong, not equal";
77 ASSERT_TRUE(a.eq_weak(b)) << "test4/1: wrong, weakly equal";
78 ASSERT_TRUE(a.eq_weak(c)) << "test4/2: wrong, weakly equal";
79 ASSERT_TRUE(b.eq_weak(c)) << "test4/3: wrong, weakly equal";
80
81 std::unordered_map<SessionID128, std::string> map;
82 const std::unordered_map<SessionID128, std::string> &cmap = map;
83 map[a] = "hello";
84 ASSERT_TRUE(b.find_weak(map, true)) << "test4/1: wrong, weak exists";
85 ASSERT_TRUE(d.find_weak(map, true)) << "test4/2: wrong, weak exists";
86 ASSERT_FALSE(a.find_weak(map, true)) << "test4/3: wrong, weak doesn't exist";
87 ASSERT_TRUE(a.find_weak(map, false)) << "test4/4: wrong, weak exists";
88 ASSERT_FALSE(x.find_weak(map, true)) << "test4: wrong, weak doesn't exist";
89 const SessionID128 *s1 = d.find_weak(cmap, true);
90 ASSERT_TRUE(s1) << "test4: can't find s1";
91 // std::cout << "lookup: " << *s1 << ' ' <<
92 // std::endl;
93 const SessionID128 *s2 = x.find_weak(cmap, true);
94 ASSERT_FALSE(s2) << "test4: shouldn't have found s2";
95}
96
97TEST(Sessid, Speed)
98{
99 SSLLib::RandomAPI rng;
100
101 const SessionID128 sid(rng);
102 const TokenEncrypt::Key key(rng);
103 TokenEncryptDecrypt ted(key);
104 for (size_t i = 0; i < 1000; ++i)
105 {
106 const SessionID128 sid_enc(sid, ted.encrypt);
107 const SessionID128 sid_dec(sid_enc, ted.decrypt);
108 ASSERT_EQ(sid, sid_dec);
109 }
110}
111
112struct SessionID : public SessionID128
113{
115 {
116 // dump("default");
117 }
118
120 : SessionID128(rng)
121 {
122 // dump("rng");
123 }
124
126 {
127 // dump("destruct");
128 }
129
130 void dump(const char *prefix) const
131 {
132 std::cout << prefix << " : " << to_string() << '\n';
133 }
134};
135
137{
138 public:
140 : sid(rng)
141 {
142 }
143
144 const SessionID &get_token() const
145 {
146 return sid;
147 }
148
149 private:
151};
152
153std::string test(Session *session)
154{
155 const std::string &nam = "myname";
156 const SessionID &sid = session ? session->get_token() : SessionID();
157 return "Name: " + nam + " SessID: " + sid.to_string();
158}
159
160TEST(Sessid, Refscope1)
161{
162 FakeSecureRand fake_rng(0x42);
163 Session sess(fake_rng);
164 EXPECT_EQ("Name: myname SessID: QkNERUZHSElKS0xNTk9QUQ..", test(&sess));
165 EXPECT_EQ("Name: myname SessID: AAAAAAAAAAAAAAAAAAAAAA..", test(nullptr));
166}
167
168#ifndef ITER
169#define ITER 1000
170#endif
171
172static void tryit(RandomAPI &rng, TokenEncryptDecrypt &encdec)
173{
174 std::uint8_t data1[TokenEncrypt::Key::SIZE];
175 std::uint8_t data2[TokenEncrypt::Key::SIZE];
176 std::uint8_t data3[TokenEncrypt::Key::SIZE];
177
178 rng.rand_bytes(data1, sizeof(data1));
179 encdec.encrypt(data2, data1, TokenEncrypt::Key::SIZE);
180 encdec.decrypt(data3, data2, TokenEncrypt::Key::SIZE);
181 ASSERT_TRUE(::memcmp(data1, data3, TokenEncrypt::Key::SIZE) == 0);
182}
183
185{
186 const StrongRandomAPI::Ptr rng(new SSLLib::RandomAPI());
187 const TokenEncrypt::Key key(*rng);
188 TokenEncryptDecrypt encdec(key);
189
190 for (size_t i = 0; i < ITER; ++i)
191 tryit(*rng, encdec);
192}
const SessionID & get_token() const
Session(StrongRandomAPI &rng)
SessionID sid
Abstract base class for random number generators.
Definition randapi.hpp:39
virtual void rand_bytes(unsigned char *buf, size_t size)=0
Fill a buffer with random bytes.
std::string to_string() const
Definition sess_id.hpp:115
bool defined() const
Definition sess_id.hpp:94
bool eq_weak(const SessionIDType< S > &other) const
Definition sess_id.hpp:137
const SessionIDType * find_weak(const UNORDERED_MAP &m, const bool conflict) const
Definition sess_id.hpp:163
Abstract base class for cryptographically strong random number generators.
Definition randapi.hpp:226
static constexpr size_t SIZE
SessionID(StrongRandomAPI &rng)
void dump(const char *prefix) const
void test()
Definition test_rc.cpp:80
TEST(Sessid, Test1)
#define ITER
static void tryit(RandomAPI &rng, TokenEncryptDecrypt &encdec)