OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_validatecreds.cpp
Go to the documentation of this file.
1// Test Unicode::is_valid_utf8(), validate_auth_cred(),
2// and AuthCreds::is_valid(). Throws exception on failure.
3#include "test_common.hpp"
4
5#include <iostream>
8
11
12using namespace openvpn;
13
14static bool verbose = false;
15
16void validate(const ValidateCreds::Type type, const bool expected_result, const std::string &cred, const bool strict)
17{
18 if (verbose)
19 OPENVPN_LOG("VALIDATE '" << cred << "' expected res=" << expected_result);
20 const bool actual_result = ValidateCreds::is_valid(type, cred, strict);
21 EXPECT_EQ(actual_result, expected_result);
22}
23
24TEST(misc, creds1)
25{
26 validate(ValidateCreds::USERNAME, true, "foobar", true);
27 validate(ValidateCreds::PASSWORD, true, "xxx\nyyy", false);
28 validate(ValidateCreds::USERNAME, false, "foo\nbar", true);
30 true,
31 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
32 true);
34 false,
35 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
36 true);
37 validate(ValidateCreds::USERNAME, false, "hello\x07there", true);
38 validate(ValidateCreds::USERNAME, true, "Привет", true);
39 validate(ValidateCreds::USERNAME, false, "\xFF\xFF\xFF\xFF", true);
40}
41
42void validate_creds(std::string username, std::string password, const bool expected_result, const bool strict)
43{
44 if (verbose)
45 OPENVPN_LOG("VALIDATE username='" << username << "' password='" << password << "' expected res=" << expected_result);
46 SafeString password_safe(password);
47 const AuthCreds ac(std::move(username), std::move(password_safe), "");
48 const bool actual_result = ac.is_valid(strict);
49 EXPECT_EQ(actual_result, expected_result);
50}
51
52TEST(misc, creds2)
53{
54 validate_creds("foo", "bar", true, true);
55 validate_creds("", "bar", false, true);
56 validate_creds("foo", "", true, true);
57 validate_creds("Привет", "trouble", true, true);
58 validate_creds("Привет", "", true, true);
59 validate_creds("foo\nbar", "zoo", false, true);
60 validate_creds("hello\x07there", "pass", false, true);
61 validate_creds("হ্যালো", "హలో", true, true);
62 validate_creds("yyy", "\xFF\xFF\xFF\xFF", false, true);
63}
64
65void validate_utf8(const std::string &str, const size_t max_len_flags, const bool expected_result)
66{
67 if (verbose)
68 OPENVPN_LOG("VALIDATE UTF8 '" << str << "' expected res=" << expected_result);
69 const bool actual_result = Unicode::is_valid_utf8(str, max_len_flags);
70 EXPECT_EQ(actual_result, expected_result);
71}
72
73TEST(misc, creds3)
74{
75 validate_utf8("", 0, true);
76 validate_utf8("test", 0, true);
77 validate_utf8("Привет", 0, true);
78 validate_utf8("Привет", 6, true);
79 validate_utf8("Привет", 5, false);
80 validate_utf8("hello\x07there", 0, true);
81 validate_utf8("hello\x07there", Unicode::UTF8_NO_CTRL, false);
82 validate_utf8("\xFF\xFF\xFF\xFF", 0, false);
83 validate_utf8("hello there", 0, true);
84 validate_utf8("hello there", Unicode::UTF8_NO_SPACE, false);
85}
bool is_valid(const bool strict) const
Definition authcreds.hpp:64
A string-like type that clears the buffer contents on delete.
Definition safestr.hpp:27
#define OPENVPN_LOG(args)
bool is_valid_utf8(const STRING &str, const size_t max_len_flags=0)
Definition unicode.hpp:75
static bool is_valid(const Type type, const STRING &cred, const bool strict)
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
void validate_creds(std::string username, std::string password, const bool expected_result, const bool strict)
void validate_utf8(const std::string &str, const size_t max_len_flags, const bool expected_result)
static bool verbose
void validate(const ValidateCreds::Type type, const bool expected_result, const std::string &cred, const bool strict)
TEST(misc, creds1)