diff options
author | eidheim <eidheim@gmail.com> | 2019-06-25 13:00:58 +0300 |
---|---|---|
committer | eidheim <eidheim@gmail.com> | 2019-06-25 13:00:58 +0300 |
commit | 0a3d94d2b1dda38f47f1aee848b398f415c6eaec (patch) | |
tree | 3740d011ff55c8eccef7b7153fa04e6d51e22dba | |
parent | 79058a047dc1a1c9d64af8ff82a698ebba33ab0b (diff) |
Added tests/assert.hpp
-rw-r--r-- | tests/assert.hpp | 9 | ||||
-rw-r--r-- | tests/crypto_test.cpp | 35 | ||||
-rw-r--r-- | tests/io_test.cpp | 67 | ||||
-rw-r--r-- | tests/parse_test.cpp | 78 |
4 files changed, 98 insertions, 91 deletions
diff --git a/tests/assert.hpp b/tests/assert.hpp new file mode 100644 index 0000000..7d55ec7 --- /dev/null +++ b/tests/assert.hpp @@ -0,0 +1,9 @@ +#ifndef SIMPLE_WEB_ASSERT_HPP +#define SIMPLE_WEB_ASSERT_HPP + +#include <cstdlib> +#include <iostream> + +#define ASSERT(e) ((void)((e) ? ((void)0) : ((void)(std::cerr << "Assertion failed: (" << #e << "), function " << __func__ << ", file " << __FILE__ << ", line " << __LINE__ << ".\n"), std::abort()))) + +#endif /* SIMPLE_WEB_ASSERT_HPP */ diff --git a/tests/crypto_test.cpp b/tests/crypto_test.cpp index 2350882..9775a68 100644 --- a/tests/crypto_test.cpp +++ b/tests/crypto_test.cpp @@ -1,7 +1,6 @@ -#include <cassert> -#include <vector> - +#include "assert.hpp" #include "crypto.hpp" +#include <vector> using namespace std; using namespace SimpleWeb; @@ -35,40 +34,40 @@ const vector<pair<string, string>> sha512_string_tests = { int main() { for(auto &string_test : base64_string_tests) { - assert(Crypto::Base64::encode(string_test.first) == string_test.second); - assert(Crypto::Base64::decode(string_test.second) == string_test.first); + ASSERT(Crypto::Base64::encode(string_test.first) == string_test.second); + ASSERT(Crypto::Base64::decode(string_test.second) == string_test.first); } for(auto &string_test : md5_string_tests) { - assert(Crypto::to_hex_string(Crypto::md5(string_test.first)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::md5(string_test.first)) == string_test.second); stringstream ss(string_test.first); - assert(Crypto::to_hex_string(Crypto::md5(ss)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::md5(ss)) == string_test.second); } for(auto &string_test : sha1_string_tests) { - assert(Crypto::to_hex_string(Crypto::sha1(string_test.first)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha1(string_test.first)) == string_test.second); stringstream ss(string_test.first); - assert(Crypto::to_hex_string(Crypto::sha1(ss)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha1(ss)) == string_test.second); } for(auto &string_test : sha256_string_tests) { - assert(Crypto::to_hex_string(Crypto::sha256(string_test.first)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha256(string_test.first)) == string_test.second); stringstream ss(string_test.first); - assert(Crypto::to_hex_string(Crypto::sha256(ss)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha256(ss)) == string_test.second); } for(auto &string_test : sha512_string_tests) { - assert(Crypto::to_hex_string(Crypto::sha512(string_test.first)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha512(string_test.first)) == string_test.second); stringstream ss(string_test.first); - assert(Crypto::to_hex_string(Crypto::sha512(ss)) == string_test.second); + ASSERT(Crypto::to_hex_string(Crypto::sha512(ss)) == string_test.second); } //Testing iterations - assert(Crypto::to_hex_string(Crypto::sha1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa"); - assert(Crypto::to_hex_string(Crypto::sha1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); + ASSERT(Crypto::to_hex_string(Crypto::sha1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa"); + ASSERT(Crypto::to_hex_string(Crypto::sha1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); stringstream ss("Test"); - assert(Crypto::to_hex_string(Crypto::sha1(ss, 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); + ASSERT(Crypto::to_hex_string(Crypto::sha1(ss, 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); - assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 4096, 128 / 8)) == "f66df50f8aaa11e4d9721e1312ff2e66"); - assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 8192, 512 / 8)) == "a941ccbc34d1ee8ebbd1d34824a419c3dc4eac9cbc7c36ae6c7ca8725e2b618a6ad22241e787af937b0960cf85aa8ea3a258f243e05d3cc9b08af5dd93be046c"); + ASSERT(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 4096, 128 / 8)) == "f66df50f8aaa11e4d9721e1312ff2e66"); + ASSERT(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 8192, 512 / 8)) == "a941ccbc34d1ee8ebbd1d34824a419c3dc4eac9cbc7c36ae6c7ca8725e2b618a6ad22241e787af937b0960cf85aa8ea3a258f243e05d3cc9b08af5dd93be046c"); } diff --git a/tests/io_test.cpp b/tests/io_test.cpp index 25a7c06..2b00b90 100644 --- a/tests/io_test.cpp +++ b/tests/io_test.cpp @@ -1,8 +1,7 @@ +#include "assert.hpp" #include "client_ws.hpp" #include "server_ws.hpp" -#include <cassert> - using namespace std; typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer; @@ -19,21 +18,21 @@ int main() { echo.on_message = [&server_callback_count](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::InMessage> in_message) { auto in_message_str = in_message->string(); - assert(in_message_str == "Hello"); + ASSERT(in_message_str == "Hello"); ++server_callback_count; connection->send(in_message_str, [](const SimpleWeb::error_code &ec) { if(ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); } }); }; echo.on_open = [&server_callback_count](shared_ptr<WsServer::Connection> connection) { ++server_callback_count; - assert(!connection->remote_endpoint_address().empty()); - assert(connection->remote_endpoint_port() > 0); + ASSERT(!connection->remote_endpoint_address().empty()); + ASSERT(connection->remote_endpoint_port() > 0); }; echo.on_close = [&server_callback_count](shared_ptr<WsServer::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { @@ -42,7 +41,7 @@ int main() { echo.on_error = [](shared_ptr<WsServer::Connection> /*connection*/, const SimpleWeb::error_code &ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); }; auto &echo_thrice = server.endpoint["^/echo_thrice/?$"]; @@ -60,7 +59,7 @@ int main() { auto &fragmented_message = server.endpoint["^/fragmented_message/?$"]; fragmented_message.on_message = [&server_callback_count](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::InMessage> in_message) { ++server_callback_count; - assert(in_message->string() == "fragmented message"); + ASSERT(in_message->string() == "fragmented message"); connection->send("fragmented", nullptr, 1); connection->send(" ", nullptr, 0); @@ -90,11 +89,11 @@ int main() { atomic<bool> closed(false); client.on_message = [&](shared_ptr<WsClient::Connection> connection, shared_ptr<WsClient::InMessage> in_message) { - assert(in_message->string() == "Hello"); + ASSERT(in_message->string() == "Hello"); ++client_callback_count; - assert(!closed); + ASSERT(!closed); connection->send_close(1000); }; @@ -102,20 +101,20 @@ int main() { client.on_open = [&](shared_ptr<WsClient::Connection> connection) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); connection->send("Hello"); }; client.on_close = [&](shared_ptr<WsClient::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); closed = true; }; client.on_error = [](shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); }; thread client_thread([&client]() { @@ -128,9 +127,9 @@ int main() { client.stop(); client_thread.join(); - assert(client_callback_count == 3); + ASSERT(client_callback_count == 3); } - assert(server_callback_count == 1200); + ASSERT(server_callback_count == 1200); for(size_t i = 0; i < 400; ++i) { WsClient client("localhost:8080/echo_thrice"); @@ -139,11 +138,11 @@ int main() { atomic<bool> closed(false); client.on_message = [&](shared_ptr<WsClient::Connection> connection, shared_ptr<WsClient::InMessage> in_message) { - assert(in_message->string() == "Hello"); + ASSERT(in_message->string() == "Hello"); ++client_callback_count; - assert(!closed); + ASSERT(!closed); if(client_callback_count == 4) connection->send_close(1000); @@ -152,20 +151,20 @@ int main() { client.on_open = [&](shared_ptr<WsClient::Connection> connection) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); connection->send("Hello"); }; client.on_close = [&](shared_ptr<WsClient::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); closed = true; }; client.on_error = [](shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); }; thread client_thread([&client]() { @@ -178,7 +177,7 @@ int main() { client.stop(); client_thread.join(); - assert(client_callback_count == 5); + ASSERT(client_callback_count == 5); } { @@ -189,11 +188,11 @@ int main() { atomic<bool> closed(false); client.on_message = [&](shared_ptr<WsClient::Connection> connection, shared_ptr<WsClient::InMessage> in_message) { - assert(in_message->string() == "Hello"); + ASSERT(in_message->string() == "Hello"); ++client_callback_count; - assert(!closed); + ASSERT(!closed); if(client_callback_count == 201) connection->send_close(1000); @@ -202,7 +201,7 @@ int main() { client.on_open = [&](shared_ptr<WsClient::Connection> connection) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); for(size_t i = 0; i < 200; ++i) { auto send_stream = make_shared<WsClient::OutMessage>(); @@ -213,13 +212,13 @@ int main() { client.on_close = [&](shared_ptr<WsClient::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); closed = true; }; client.on_error = [](shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); }; thread client_thread([&client]() { @@ -232,8 +231,8 @@ int main() { client.stop(); client_thread.join(); - assert(client_callback_count == 202); - assert(server_callback_count == 202); + ASSERT(client_callback_count == 202); + ASSERT(server_callback_count == 202); } { @@ -244,7 +243,7 @@ int main() { atomic<bool> closed(false); client.on_message = [&](shared_ptr<WsClient::Connection> connection, shared_ptr<WsClient::InMessage> in_message) { - assert(in_message->string() == "fragmented message"); + ASSERT(in_message->string() == "fragmented message"); ++client_callback_count; @@ -254,7 +253,7 @@ int main() { client.on_open = [&](shared_ptr<WsClient::Connection> connection) { ++client_callback_count; - assert(!closed); + ASSERT(!closed); connection->send("fragmented", nullptr, 1); connection->send(" ", nullptr, 0); @@ -262,13 +261,13 @@ int main() { }; client.on_close = [&](shared_ptr<WsClient::Connection> /*connection*/, int /*status*/, const string & /*reason*/) { - assert(!closed); + ASSERT(!closed); closed = true; }; client.on_error = [](shared_ptr<WsClient::Connection> /*connection*/, const SimpleWeb::error_code &ec) { cerr << ec.message() << endl; - assert(false); + ASSERT(false); }; thread client_thread([&client]() { @@ -281,8 +280,8 @@ int main() { client.stop(); client_thread.join(); - assert(client_callback_count == 2); - assert(server_callback_count == 1); + ASSERT(client_callback_count == 2); + ASSERT(server_callback_count == 1); } server.stop(); diff --git a/tests/parse_test.cpp b/tests/parse_test.cpp index 5acc867..3345cdf 100644 --- a/tests/parse_test.cpp +++ b/tests/parse_test.cpp @@ -1,6 +1,6 @@ +#include "assert.hpp" #include "client_ws.hpp" #include "server_ws.hpp" -#include <cassert> #include <iostream> using namespace std; @@ -24,28 +24,28 @@ public: ss << "\r\n"; std::istream stream(&connection->read_buffer); - assert(RequestMessage::parse(stream, connection->method, connection->path, connection->query_string, connection->http_version, connection->header)); + ASSERT(RequestMessage::parse(stream, connection->method, connection->path, connection->query_string, connection->http_version, connection->header)); - assert(connection->method == "GET"); - assert(connection->path == "/test/"); - assert(connection->http_version == "1.1"); + ASSERT(connection->method == "GET"); + ASSERT(connection->path == "/test/"); + ASSERT(connection->http_version == "1.1"); - assert(connection->header.size() == 4); + ASSERT(connection->header.size() == 4); auto header_it = connection->header.find("TestHeader"); - assert(header_it != connection->header.end() && header_it->second == "test"); + ASSERT(header_it != connection->header.end() && header_it->second == "test"); header_it = connection->header.find("TestHeader2"); - assert(header_it != connection->header.end() && header_it->second == "test2"); + ASSERT(header_it != connection->header.end() && header_it->second == "test2"); header_it = connection->header.find("testheader"); - assert(header_it != connection->header.end() && header_it->second == "test"); + ASSERT(header_it != connection->header.end() && header_it->second == "test"); header_it = connection->header.find("testheader2"); - assert(header_it != connection->header.end() && header_it->second == "test2"); + ASSERT(header_it != connection->header.end() && header_it->second == "test2"); auto range = connection->header.equal_range("testheader3"); auto first = range.first; auto second = first; ++second; - assert(range.first != connection->header.end() && range.second != connection->header.end() && + ASSERT(range.first != connection->header.end() && range.second != connection->header.end() && ((first->second == "test3a" && second->second == "test3b") || (first->second == "test3b" && second->second == "test3a"))); } @@ -58,27 +58,27 @@ public: void connect() {} void constructor_parse_test1() { - assert(path == "/test"); - assert(host == "test.org"); - assert(port == 8080); + ASSERT(path == "/test"); + ASSERT(host == "test.org"); + ASSERT(port == 8080); } void constructor_parse_test2() { - assert(path == "/test"); - assert(host == "test.org"); - assert(port == 80); + ASSERT(path == "/test"); + ASSERT(host == "test.org"); + ASSERT(port == 80); } void constructor_parse_test3() { - assert(path == "/"); - assert(host == "test.org"); - assert(port == 80); + ASSERT(path == "/"); + ASSERT(host == "test.org"); + ASSERT(port == 80); } void constructor_parse_test4() { - assert(path == "/"); - assert(host == "test.org"); - assert(port == 8080); + ASSERT(path == "/"); + ASSERT(host == "test.org"); + ASSERT(port == 8080); } void parse_response_header_test() { @@ -93,24 +93,24 @@ public: stream << "TestHeader3:test3b\r\n"; stream << "\r\n"; - assert(ResponseMessage::parse(*connection->in_message, connection->http_version, connection->status_code, connection->header)); + ASSERT(ResponseMessage::parse(*connection->in_message, connection->http_version, connection->status_code, connection->header)); - assert(connection->header.size() == 4); + ASSERT(connection->header.size() == 4); auto header_it = connection->header.find("TestHeader"); - assert(header_it != connection->header.end() && header_it->second == "test"); + ASSERT(header_it != connection->header.end() && header_it->second == "test"); header_it = connection->header.find("TestHeader2"); - assert(header_it != connection->header.end() && header_it->second == "test2"); + ASSERT(header_it != connection->header.end() && header_it->second == "test2"); header_it = connection->header.find("testheader"); - assert(header_it != connection->header.end() && header_it->second == "test"); + ASSERT(header_it != connection->header.end() && header_it->second == "test"); header_it = connection->header.find("testheader2"); - assert(header_it != connection->header.end() && header_it->second == "test2"); + ASSERT(header_it != connection->header.end() && header_it->second == "test2"); auto range = connection->header.equal_range("testheader3"); auto first = range.first; auto second = first; ++second; - assert(range.first != connection->header.end() && range.second != connection->header.end() && + ASSERT(range.first != connection->header.end() && range.second != connection->header.end() && ((first->second == "test3a" && second->second == "test3b") || (first->second == "test3b" && second->second == "test3a"))); @@ -119,17 +119,17 @@ public: }; int main() { - assert(case_insensitive_equal("Test", "tesT")); - assert(case_insensitive_equal("tesT", "test")); - assert(!case_insensitive_equal("test", "tseT")); + ASSERT(case_insensitive_equal("Test", "tesT")); + ASSERT(case_insensitive_equal("tesT", "test")); + ASSERT(!case_insensitive_equal("test", "tseT")); CaseInsensitiveEqual equal; - assert(equal("Test", "tesT")); - assert(equal("tesT", "test")); - assert(!equal("test", "tset")); + ASSERT(equal("Test", "tesT")); + ASSERT(equal("tesT", "test")); + ASSERT(!equal("test", "tset")); CaseInsensitiveHash hash; - assert(hash("Test") == hash("tesT")); - assert(hash("tesT") == hash("test")); - assert(hash("test") != hash("tset")); + ASSERT(hash("Test") == hash("tesT")); + ASSERT(hash("tesT") == hash("test")); + ASSERT(hash("test") != hash("tset")); SocketServerTest serverTest; serverTest.io_service = std::make_shared<io_context>(); |