forked from Psiphon-Inc/psicash-lib-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.cpp
53 lines (45 loc) · 1.51 KB
/
test_helpers.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
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include "test_helpers.hpp"
// Adapted from https://stackoverflow.com/a/478960
int exec(const char* cmd, std::string& output) {
output.clear();
std::array<char, 128> buffer;
bool closed = false;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), [&closed](FILE* p){if (!closed) pclose(p);});
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
output += buffer.data();
}
int result = pclose(pipe.get());
closed = true;
// pclose will be called again as the shared_ptr deleter, but that's okay
return result;
}
bool AuthTokenSetsEqual(const psicash::AuthTokens& at1, const psicash::AuthTokens& at2) {
if (at1.size() != at2.size()) {
return false;
}
for (const auto& map_it1 : at1) {
const auto& val_it2 = at2.find(map_it1.first);
if (val_it2 == at2.end()) {
return false;
}
if (val_it2->second.id != map_it1.second.id) {
return false;
}
if (val_it2->second.server_time_expiry.has_value() != map_it1.second.server_time_expiry.has_value()) {
return false;
}
if (val_it2->second.server_time_expiry.has_value()
&& !(*(val_it2->second.server_time_expiry) == *(map_it1.second.server_time_expiry))) {
return false;
}
}
return true;
}