forked from Psiphon-Inc/psicash-lib-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.hpp
142 lines (119 loc) · 4.22 KB
/
test_helpers.hpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef PSICASHLIB_TEST_HELPERS_H
#define PSICASHLIB_TEST_HELPERS_H
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <fstream>
#include "gtest/gtest.h"
#include "userdata.hpp"
class TempDir
{
public:
TempDir() {}
protected:
int RandInt()
{
static bool rand_seeded = false;
if (!rand_seeded)
{
std::srand(std::time(nullptr));
rand_seeded = true;
}
return std::rand();
}
std::string GetTempDir()
{
// The first envvar is set by test.sh
std::vector<std::string> env_vars = {"TEST_TEMP_DIR", "TMPDIR", "TMP", "TEMP", "TEMPDIR"};
const char *tmp = nullptr;
for (auto var : env_vars)
{
tmp = getenv(var.c_str());
if (tmp)
{
break;
}
}
if (!tmp)
{
tmp = "/tmp";
}
std::string res = tmp;
res += "/" + std::to_string(RandInt());
#ifdef _MSC_VER
auto rmrf = "rmdir /S /Q \"" + res + "\" > nul 2>&1";
auto mkdirp = "mkdir \"" + res + "\"";
#else
auto rmrf = "rm -rf " + res;
auto mkdirp = "mkdir -p " + res;
#endif
system(rmrf.c_str());
system(mkdirp.c_str());
return res;
}
std::string GetSuffix(bool dev) {
return dev ? ".dev" : ".prod";
}
std::string DatastoreFilepath(const std::string& datastore_root, bool dev) {
return datastore_root + "/psicashdatastore" + GetSuffix(dev);
}
bool Write(const std::string& datastore_root, bool dev, const std::string& s) {
auto ds_file = DatastoreFilepath(datastore_root, dev);
std::ofstream f;
f.open(ds_file, std::ios::out | std::ios::trunc | std::ios::binary);
if (!f.is_open()) {
return false;
}
f << s;
f.close();
return true;
}
bool WriteBadData(const std::string& datastore_root, bool dev)
{
auto ds_file = DatastoreFilepath(datastore_root, dev);
auto make_bad_file = "echo nonsense > " + ds_file;
return Write(datastore_root, dev, "this is bad data");
}
};
int exec(const char* cmd, std::string& output);
// From https://stackoverflow.com/a/17976541
inline std::string trim(const std::string &s)
{
auto wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
auto wsback=std::find_if_not(s.rbegin(),s.rend(),[](int c){return std::isspace(c);}).base();
return (wsback<=wsfront ? std::string() : std::string(wsfront,wsback));
}
// Checks that two vectors have the same set of values, regardless of order.
template<typename T, typename U>
::testing::AssertionResult VectorSetsMatch(const std::vector<T>& expected, const std::vector<U>& actual, std::function<T(const U&)> trans) {
if (expected.size() != actual.size()) {
return ::testing::AssertionFailure() << " actual size (" << actual.size() << ") "
<< "not equal to expected size (" << expected.size() << ")";
}
// Using a copy of expected, so we can remove as we find items, to ensure that the full
// sets match.
std::vector<std::reference_wrapper<const T>> expected_copy;
for (const auto& e : expected) {
expected_copy.push_back(std::cref(e));
}
for (size_t i(0); i < actual.size(); ++i) {
auto transformed_actual = trans(actual[i]);
auto expected_found = std::find_if(
expected_copy.begin(), expected_copy.end(),
[&](const std::reference_wrapper<const T> &e) { return e.get() == transformed_actual; });
if (expected_found == expected_copy.end()) {
// If we ever use this helper function with a type of transformed_actual that
// does not support `<<`, then we'll need to re-think this output.
return ::testing::AssertionFailure() << " actual[" << i << "] (" << transformed_actual << ")"
<< "not found in expected";
}
expected_copy.erase(expected_found);
}
return ::testing::AssertionSuccess();
}
bool AuthTokenSetsEqual(const psicash::AuthTokens& at1, const psicash::AuthTokens& at2);
#endif // PSICASHLIB_TEST_HELPERS_H