-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_utils.cpp
More file actions
187 lines (151 loc) · 4.99 KB
/
test_utils.cpp
File metadata and controls
187 lines (151 loc) · 4.99 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "test_utils.h"
// Test configuration constants (to be loaded from an INI file)
bool HTTP_PROXY_TEST_ENABLED;
std::string CURL_LOG_FOLDER;
std::string CERT_AUTH_FILE;
std::string SSL_CERT_FILE;
std::string SSL_KEY_FILE;
std::string SSL_KEY_PWD;
std::string PROXY_SERVER;
std::string PROXY_SERVER_FAKE;
std::mutex g_mtxConsoleMutex;
bool GlobalTestInit(const std::string& strConfFile)
{
CSimpleIniA ini;
ini.LoadFile(strConfFile.c_str());
std::string strTmp;
strTmp = ini.GetValue("tests", "http-proxy", "");
std::transform(strTmp.begin(), strTmp.end(), strTmp.begin(), ::toupper);
HTTP_PROXY_TEST_ENABLED = (strTmp == "YES") ? true : false;
PROXY_SERVER = ini.GetValue("http-proxy", "host", "");
PROXY_SERVER_FAKE = ini.GetValue("http-proxy", "host_invalid", "");
// required when a build is generated with the macro DEBUG_CURL
CURL_LOG_FOLDER = ini.GetValue("local", "curl_logs_folder", "");
CERT_AUTH_FILE = ini.GetValue("local", "ca_file", "");
SSL_CERT_FILE = ini.GetValue("local", "ssl_cert_file", "");
SSL_KEY_FILE = ini.GetValue("local", "ssl_key_file", "");
SSL_KEY_PWD = ini.GetValue("local", "ssl_key_pwd", "");
if (HTTP_PROXY_TEST_ENABLED && (PROXY_SERVER.empty() || PROXY_SERVER_FAKE.empty()))
{
std::clog << "[ERROR] Check your INI file parameters."
" Disable tests that don't have a server/port value."
<< std::endl;
return false;
}
return true;
}
void GlobalTestCleanUp(void)
{
return;
}
void TimeStampTest(std::ostringstream& ssTimestamp)
{
time_t tRawTime;
tm * tmTimeInfo;
time(&tRawTime);
tmTimeInfo = localtime(&tRawTime);
ssTimestamp << (tmTimeInfo->tm_year) + 1900
<< "/" << tmTimeInfo->tm_mon + 1 << "/" << tmTimeInfo->tm_mday << " at "
<< tmTimeInfo->tm_hour << ":" << tmTimeInfo->tm_min << ":" << tmTimeInfo->tm_sec;
}
int TestProgressCallback(void* ptr, double dTotalToDownload, double dNowDownloaded,
double dTotalToUpload, double dNowUploaded)
{
// ensure that the file to be downloaded is not empty
// because that would cause a division by zero error later on
if (dTotalToDownload <= 0.0)
return 0;
// how wide you want the progress meter to be
const int iTotalDots = 20;
double dFractionDownloaded = dNowDownloaded / dTotalToDownload;
// part of the progressmeter that's already "full"
int iDots = round(dFractionDownloaded * iTotalDots);
// create the "meter"
int iDot = 0;
std::cout << static_cast<unsigned>(dFractionDownloaded * 100) << "% [";
// part that's full already
for (; iDot < iDots; iDot++)
std::cout << "=";
// remaining part (spaces)
for (; iDot < iTotalDots; iDot++)
std::cout << " ";
// and back to line begin - do not forget the fflush to avoid output buffering problems!
std::cout << "] \r" << std::flush;
// if you don't return 0, the transfer will be aborted - see the documentation
return 0;
}
bool GetFileTime(const char* const & pszFilePath, time_t& tLastModificationTime)
{
FILE* pFile = fopen(pszFilePath, "rb");
if (pFile != nullptr)
{
struct stat file_info;
#ifndef LINUX
if (fstat(_fileno(pFile), &file_info) == 0)
#else
if (fstat(fileno(pFile), &file_info) == 0)
#endif
{
tLastModificationTime = file_info.st_mtime;
return true;
}
}
return false;
}
#ifdef BOOST
bool AreFilesEqual(const std::string& strlFilePath, const std::string& strrFilePath)
{
io::mapped_file_source f1(strlFilePath);
io::mapped_file_source f2(strrFilePath);
if (f1.size() == f2.size() &&
std::equal(f1.data(), f1.data() + f1.size(), f2.data()))
return true; // The files are equal
else
return false; //The files are not equal
}
#else
#define BUFFER_SIZE 0xFFF // 4KBytes... small files...
bool AreFilesEqual(const std::string& strlFilePath, const std::string& strrFilePath)
{
std::ifstream lFile(strlFilePath.c_str(), std::ifstream::in | std::ifstream::binary);
std::ifstream rFile(strrFilePath.c_str(), std::ifstream::in | std::ifstream::binary);
if (!lFile.is_open() || !rFile.is_open())
{
return false;
}
#ifndef USE_SMART_PTR
char *lBuffer = new char[BUFFER_SIZE]();
char *rBuffer = new char[BUFFER_SIZE]();
#else
std::unique_ptr<char[]> lBuffer(new char[BUFFER_SIZE]);
std::unique_ptr<char[]> rBuffer(new char[BUFFER_SIZE]);
#endif
do
{
#ifndef USE_SMART_PTR
lFile.read(lBuffer, BUFFER_SIZE);
rFile.read(rBuffer, BUFFER_SIZE);
#else
lFile.read(lBuffer.get(), BUFFER_SIZE);
rFile.read(rBuffer.get(), BUFFER_SIZE);
#endif
#ifndef USE_SMART_PTR
if (std::memcmp(lBuffer, rBuffer, BUFFER_SIZE) != 0)
#else
if (std::memcmp(lBuffer.get(), rBuffer.get(), BUFFER_SIZE) != 0)
#endif
{
#ifndef USE_SMART_PTR
delete[] lBuffer;
delete[] rBuffer;
#endif
return false;
}
} while (lFile.good() || rFile.good());
#ifndef USE_SMART_PTR
delete[] lBuffer;
delete[] rBuffer;
#endif
return true;
}
#endif