0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 12:12:15 +02:00

[test/misc] Convert streq test to unittest

Signed-off-by: Arne Schwabe <arne@openvpn.net>
This commit is contained in:
Arne Schwabe 2019-10-23 15:57:40 +02:00 committed by Frank Lichtenheld
parent 775f1313a2
commit f2cfd225ba
2 changed files with 31 additions and 51 deletions

View File

@ -1,51 +0,0 @@
// TEST : {"cmd": "./go streq"}
#include <iostream>
#include <openvpn/log/logsimple.hpp>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/strneq.hpp>
using namespace openvpn;
void test(const std::string& s1, const std::string& s2, const bool should_eq)
{
const bool neq = crypto::str_neq(s1, s2);
OPENVPN_LOG("'" << s1 << "' cmp '" << s2 << "' " << (neq ? "NEQ" : "EQ"));
if (neq == should_eq)
throw Exception("fail");
}
void test_logic()
{
test("", "", true);
test("x", "", false);
test("", "x", false);
test("foo", "foo", true);
test("foobar", "foo", false);
test("foo", "foobar", false);
}
void test_timing()
{
size_t count = 0;
for (size_t i = 0; i < 1000000000; ++i)
count += crypto::str_neq("foobarxxx", "foobar");
OPENVPN_LOG(count);
}
int main(int /*argc*/, char* /*argv*/[])
{
try {
test_logic();
//test_timing();
}
catch (const std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}

View File

@ -0,0 +1,31 @@
#include "test_common.h"
#include <iostream>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/strneq.hpp>
using namespace openvpn;
void test(const std::string &s1, const std::string &s2, const bool should_eq) {
const bool neq = crypto::str_neq(s1, s2);
//OPENVPN_LOG("'" << s1 << "' cmp '" << s2 << "' " << (neq ? "NEQ" : "EQ"));
ASSERT_NE(neq, should_eq);
}
TEST(misc, streq) {
test("", "", true);
test("x", "", false);
test("", "x", false);
test("foo", "foo", true);
test("foobar", "foo", false);
test("foo", "foobar", false);
}
void test_timing() {
size_t count = 0;
for (size_t i = 0; i < 1000000000; ++i)
count += crypto::str_neq("foobarxxx", "foobar");
OPENVPN_LOG(count);
}