-
Notifications
You must be signed in to change notification settings - Fork 174
/
test_unit.cpp
99 lines (77 loc) · 2.53 KB
/
test_unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "gtest/gtest.h"
#include "obfuscate.h"
#include <cstdio>
#include <string>
// Test AY_OBFUSCATE (main test)
TEST(Obfuscate, AY_OBFUSCATE)
{
// Encrypt a string literal with an XOR cipher at compile time and store
// it in test.
auto test = AY_OBFUSCATE("Hello World");
// The string starts out as encrypted
ASSERT_TRUE(test.is_encrypted());
// Manually decrypt the string. This is useful for pre-processing
// especially large strings.
test.decrypt();
ASSERT_TRUE(!test.is_encrypted());
// Output the plain text string to the console (implicitly converts to
// const char*)
puts(test);
// Re-encrypt the string so that it cannot be seen in it's plain text
// form in memory.
test.encrypt();
ASSERT_TRUE(test.is_encrypted());
// The encrypted string will be automatically decrypted if necessary
// when implicitly converting to a const char*
puts(test);
// The string is in a decrypted state
ASSERT_TRUE(!test.is_encrypted());
// Test comparison
ASSERT_TRUE(std::string("Hello World") == (char*)test);
}
// Test AY_OBFUSCATE_KEY
TEST(Obfuscate, AY_OBFUSCATE_KEY)
{
auto test = AY_OBFUSCATE_KEY("Hello World", 0xf8d3481a4bc32d83ull);
puts(test);
// Test comparison
ASSERT_TRUE(std::string("Hello World") == (char*)test);
}
// Test direct API usage
TEST(Obfuscate, API)
{
constexpr auto obfuscator = ay::make_obfuscator("Hello World");
auto test = ay::obfuscated_data<obfuscator.size(), obfuscator.key()>(obfuscator);
puts(test);
// Test comparison
ASSERT_TRUE(std::string("Hello World") == (char*)test);
}
static const char* g_global_static1 = AY_OBFUSCATE("global_static1");
static const char* g_global_static2 = AY_OBFUSCATE("global_static2");
// Test global static const char* variables
TEST(Obfuscate, GlobalConstCharPtr)
{
ASSERT_TRUE(strcmp(g_global_static1, "global_static1") == 0);
puts(g_global_static1);
ASSERT_TRUE(strcmp(g_global_static2, "global_static2") == 0);
puts(g_global_static2);
}
// Test local const char* variables
TEST(Obfuscate, LocalConstCharPtr)
{
const char* local1 = AY_OBFUSCATE("local1");
const char* local2 = AY_OBFUSCATE("local2");
ASSERT_TRUE(strcmp(local1, "local1") == 0);
puts(local1);
ASSERT_TRUE(strcmp(local2, "local2") == 0);
puts(local2);
}
// Test non-null terminated strings
TEST(Obfuscate, NonNullTerminatedStr)
{
constexpr auto obfuscator = ay::obfuscator<10, AY_OBFUSCATE_DEFAULT_KEY>("1234567890");
auto test = ay::obfuscated_data<obfuscator.size(), obfuscator.key()>(obfuscator);
puts(test);
// Test comparison
assert(std::string("1234567890") == (char*)test);
}