23#include <gtest/gtest.h>
36 void log(
const std::string &l)
override
38 std::lock_guard<std::mutex> lock(
mutex);
92 out.str(std::string());
170 auto fullpath = UNITTEST_SOURCE_DIR
"/output/" + filename;
171 std::ifstream
f(fullpath);
174 throw std::runtime_error(
"Error opening file " + fullpath);
177 std::istreambuf_iterator<char>());
188 EXPECT_NE(GetTempPathA(MAX_PATH, buf), 0);
189 return std::string(buf) + fn;
207inline std::string
getJoinedString(
const std::vector<T> &r,
const std::string &delim =
"|")
210 std::copy(r.begin(), r.end(), std::ostream_iterator<std::string>(s, delim.c_str()));
224 std::sort(r.begin(), r.end());
236 std::getline(is, l.
data);
240 operator std::string()
const
252 std::stringstream ss{output};
254 std::istream_iterator<detail::line> begin{ss};
255 std::istream_iterator<detail::line> end;
256 std::vector<std::string> lines{begin, end};
259 std::sort(lines.begin(), lines.end());
263 std::copy(lines.begin(), lines.end(), std::ostream_iterator<std::string>(s,
"\n"));
272template <
typename RESOLVABLE,
typename... CTOR_ARGS>
276 using Result = std::pair<const std::string, const unsigned short>;
280 using EndpointType =
typename RESOLVABLE::resolver_type::endpoint_type;
283 std::map<const std::string, EndpointList>
results_;
293 for (
const auto &result : results)
295 EndpointType ep(openvpn_io::ip::make_address(result.first), result.second);
296 endpoints.push_back(ep);
302 : RESOLVABLE(args...)
308 const std::string key(
host +
":" + service);
309 openvpn_io::error_code error = openvpn_io::error::host_not_found;
317 error = openvpn_io::error_code();
318 results = ResultsType::create(ep.cbegin(), ep.cend(),
host, service);
322 this->resolve_callback(error, results);
342 virtual std::string
name()
const override
347 virtual void rand_bytes(
unsigned char *buf,
size_t size)
override
381 template <
typename generator>
385 uint32_t range =
B -
A + 1;
387 uint64_t product = uint64_t{prng()} * uint64_t{range};
389 uint32_t low =
static_cast<uint32_t
>(product);
393 uint32_t threshold = -range % range;
394 while (low < threshold)
396 product = uint64_t{prng()} * uint64_t{range};
397 low =
static_cast<uint32_t
>(product);
400 return A + (product >> 32u);
415#define OVPN_EXPECT_THROW(statement, expected_exception, expected_text) \
419 OPENVPN_THROW_EXCEPTION("OVPN_EXPECT_THROW: no exception was thrown " << __FILE__ << ':' << __LINE__); \
421 catch (const expected_exception &e) \
423 if (std::string(e.what()).find(expected_text) == std::string::npos) \
424 OPENVPN_THROW_EXCEPTION("OVPN_EXPECT_THROW: did not find expected text in exception at " << __FILE__ << ':' << __LINE__ \
425 << ". Got: " << e.what()); \
427#define JY_EXPECT_THROW OVPN_EXPECT_THROW
433#define JY_ASSERT_TRUE(value) \
437 OPENVPN_THROW_EXCEPTION("JY_ASSERT_TRUE: failure at " << __FILE__ << ':' << __LINE__); \
440#define JY_ASSERT_FALSE(value) \
444 OPENVPN_THROW_EXCEPTION("JY_ASSERT_FALSE: failure at " << __FILE__ << ':' << __LINE__); \
447#define JY_ASSERT_EQ(v1, v2) \
451 OPENVPN_THROW_EXCEPTION("JY_ASSERT_EQ: failure at " << __FILE__ << ':' << __LINE__); \
454#define JY_ASSERT_NE(v1, v2) \
458 OPENVPN_THROW_EXCEPTION("JY_ASSERT_NE: failure at " << __FILE__ << ':' << __LINE__); \
461#define JY_ASSERT_LE(v1, v2) \
465 OPENVPN_THROW_EXCEPTION("JY_ASSERT_LE: failure at " << __FILE__ << ':' << __LINE__); \
468#define JY_ASSERT_GE(v1, v2) \
472 OPENVPN_THROW_EXCEPTION("JY_ASSERT_GE: failure at " << __FILE__ << ':' << __LINE__); \
476#define THROW_FMT(...) throw Exception(printfmt(__VA_ARGS__))
std::pair< const std::string, const unsigned short > Result
FakeAsyncResolvable(CTOR_ARGS... args)
void set_results(const std::string &host, const std::string &service, const ResultList &&results)
std::map< const std::string, EndpointList > results_
std::vector< EndpointType > EndpointList
EndpointType init_endpoint() const
std::vector< Result > ResultList
typename RESOLVABLE::results_type ResultsType
void async_resolve_name(const std::string &host, const std::string &service) override
typename RESOLVABLE::resolver_type::endpoint_type EndpointType
void rand_bytes_(unsigned char *buf, size_t size)
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size) override
Fill a buffer with random bytes without throwing exceptions.
virtual void rand_bytes(unsigned char *buf, size_t size) override
Fill a buffer with random bytes.
virtual std::string name() const override
Get the name of the random number generation algorithm.
FakeSecureRand(const unsigned char initial=0)
friend std::istream & operator>>(std::istream &is, line &l)
std::string getOutput() const
std::ostream & getStream()
std::string stopCollecting()
void setPrintOutput(bool doOutput)
const Log::Context::Wrapper & log_wrapper()
bool isStdoutEnabled() const
void log(const std::string &l) override
Log::Context::Wrapper log_wrap
OPENVPN_LOG_CLASS * saved_log
Abstract base class for cryptographically strong random number generators.
thread_local OPENVPN_LOG_CLASS * global_log
#define OPENVPN_LOG_CLASS
The logging interface, simple, logs a string.
Argument to construct a Context in a different thread.
Scoped RAII for the global_log pointer.
std::string getSortedJoinedString(std::vector< T > &r, const std::string &delim="|")
std::string getSortedString(const std::string &output)
void override_logOutput(bool doLogOutput, void(*test_func)())
std::string getExpectedOutput(const std::string &filename)
openvpn::LogOutputCollector * testLog
std::string getJoinedString(const std::vector< T > &r, const std::string &delim="|")
std::string getTempDirPath(const std::string &fn)
const char * expected_output