Skip to content

test: increase coverage #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- improved class members and test coverage ([#93])
- improved formatting and maintainability ([#92])
- dropped Hunter Package Manager in favor of git submodules in OS builds. ([#90])
- updated ArduinoJson 5.13.2 >> 6.10.1, usage patterns, and documentation/examples. ([#87])
55 changes: 26 additions & 29 deletions src/host/host.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
#include "host/host.h"

Ark::Client::Host::Host() : ip_(), port_(-1) {}

/***/
#include <cstring>
#include <string>

Ark::Client::Host::Host(const Host& other) : port_(other.port_) {
strncpy(this->ip_, other.ip_, sizeof(this->ip_) / sizeof(this->ip_[0]));
}
Ark::Client::Host::Host() : ip_(), port_(-1) {}

/***/
/**/

Ark::Client::Host& Ark::Client::Host::Host::operator=(const Host& other) noexcept {
if (this != &other) {
strncpy(this->ip_, other.ip_, sizeof(this->ip_) / sizeof(this->ip_[0]));
this->port_ = other.port_;
}
return *this;
Ark::Client::Host::Host(
const char* const newIP,
int newPort) : port_(newPort) {
strncpy(this->ip_, newIP, sizeof(this->ip_));
}

/***/
/**/

Ark::Client::Host::Host(const char* const newIP, int newPort) : port_(newPort) {
strncpy(this->ip_, newIP, sizeof(this->ip_) / sizeof(this->ip_[0]));
bool Ark::Client::Host::set(
const char* const newIP,
int newPort) {
strncpy(this->ip_, newIP, sizeof(this->ip_));
this->port_ = newPort;
return
((this->port_ == newPort)
&& (strcmp(this->ip_, newIP) != 0));
}

/***/
/**/

const char* Ark::Client::Host::ip() const noexcept {
std::string Ark::Client::Host::ip() const noexcept {
return this->ip_;
};

/***/
/**/

int Ark::Client::Host::port() const noexcept {
return this->port_;
};

/***/
/**/

std::string Ark::Client::Host::toString() {
char temp[36] = {};
snprintf(temp, sizeof(this->ip_) + sizeof(this->port_), "%s:%d", this->ip_, this->port_);
snprintf(
temp,
sizeof(this->ip_) + sizeof(this->port_),
"%s:%d",
this->ip_, this->port_);
return temp;
}

/***/

bool Ark::Client::Host::set(const char* const newIP, int newPort) {
strncpy(this->ip_, newIP, sizeof(this->ip_) / sizeof(this->ip_[0]));
this->port_ = newPort;
return ((this->port_ == newPort) && (strcmp(this->ip_, newIP) != 0));
}
9 changes: 2 additions & 7 deletions src/include/cpp-client/host/host.h
Original file line number Diff line number Diff line change
@@ -24,21 +24,16 @@ class Host {
char ip_[17];
int port_;

Host(Host&&) = delete;
Host& operator=(Host&&) = delete;
Host(const Host& other);
Host& operator=(const Host& other) noexcept;

public:
Host();
Host(const char* const newIP, int newPort);

bool set(const char* const newIP, int newPort);

const char* ip() const noexcept;
std::string ip() const noexcept;
int port() const noexcept;

std::string toString(); // e.g. "167.114.29.49:4003"
std::string toString();
};
/**/
}; // namespace Client
7 changes: 5 additions & 2 deletions test/api/paths.cpp
Original file line number Diff line number Diff line change
@@ -110,10 +110,13 @@ TEST(paths, test_transactions) { // NOLINT
const auto types = Ark::Client::API::Paths::Transactions::types(testHost);
ASSERT_STREQ("0.0.0.0:4003/api/v2/transactions/types", types.c_str());

const std::map<std::string, std::string> searchBody = {{"id", "dummy"}};
const std::map<std::string, std::string> searchBody = {
{ "id", "dummy" },
{ "key", "value" }
};
const auto search = Ark::Client::API::Paths::Transactions::search(testHost, searchBody, 5, 1);
ASSERT_STREQ("0.0.0.0:4003/api/v2/transactions/search?limit=5&page=1", search.first.c_str());
ASSERT_STREQ("id=dummy", search.second.c_str());
ASSERT_STREQ("id=dummy&key=value", search.second.c_str());

std::string jsonTransaction = "{"
"\"id\":\"5ab523d18ac948da82700a71fc0b3c9e764fc0cba91927cb1aa63354564ad23f\","
2 changes: 1 addition & 1 deletion test/connection/connection.cpp
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ TEST(api, test_connection) { // NOLINT
int version = connection.api.version();
ASSERT_EQ(2, version);

const char* ip = connection.host.ip();
const auto ip = connection.host.ip().c_str();
ASSERT_STREQ("167.114.29.55", ip);

int port = connection.host.port();
33 changes: 26 additions & 7 deletions test/host/host.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@

#include "host/host.h"
#include "gtest/gtest.h"

TEST(api, test_host) { // NOLINT
Ark::Client::Host host("167.114.29.55", 4003);

const auto ip = host.ip();
const auto expectedIp = "167.114.29.55";
ASSERT_STREQ(expectedIp, ip);
const auto expectedPort = 4003;
const auto expectedHost = "167.114.29.55:4003";

int port = host.port();
ASSERT_EQ(4003, port);
// create a test host
Ark::Client::Host host(expectedIp, expectedPort);

// check the host //
// check the IP
const auto ip = host.ip().c_str();
ASSERT_STREQ(expectedIp, ip);
// check the Port
int port = host.port();
ASSERT_EQ(expectedPort, port);
// check the host string
const auto hostString = host.toString().c_str();
const auto expectedHost = "167.114.29.55:4003";
ASSERT_STREQ(expectedHost, hostString);

// test setting the host //
Ark::Client::Host setHost;
setHost.set(expectedIp, expectedPort);
ASSERT_STREQ(setHost.toString().c_str(), host.toString().c_str());

// test the copy constructor //
Ark::Client::Host copiedHost(host);
ASSERT_STREQ(copiedHost.toString().c_str(), host.toString().c_str());

// test the assignment constructor //
Ark::Client::Host assignedHost(host);
ASSERT_STREQ(assignedHost.toString().c_str(), host.toString().c_str());
}
21 changes: 21 additions & 0 deletions test/http/http.cpp
Original file line number Diff line number Diff line change
@@ -47,6 +47,27 @@ TEST(api, test_http_post_body) { // NOLINT

/**/

// Tests invalid POSTing of HTTP body.
TEST(api, test_http_invalid_post_body) { // NOLINT
// Create the HTTP object
const auto http = Ark::Client::makeHTTP();

// Create a malformed Request URL and 'Post' body.
const auto request = "/167.114.29.55:4003/api/v2/wallets/search";
const auto body = "username=baldninja";

// Post the 'request' and 'body' for a response using HTTP
const auto response = http->post(request, body);

// Create a JSON object of the result
DynamicJsonDocument doc(100);
DeserializationError error = deserializeJson(doc, response);
// the empty response should cause deserialization to fail
ASSERT_TRUE(error);
}

/**/

// Tests POSTing of JSON.
TEST(api, test_http_post_json) { // NOLINT
// Create the HTTP object