added test

This commit is contained in:
Erik Frojdh 2019-04-03 19:59:37 +02:00
parent 69a11f8950
commit 7256a1e422
3 changed files with 66 additions and 18 deletions

View File

@ -16,10 +16,19 @@ class IpAddr {
IpAddr(const char *address);
std::string str() const;
std::string hex() const;
constexpr bool operator==(const IpAddr &other) const noexcept { return addr_ == other.addr_; }
constexpr bool operator!=(const IpAddr &other) const noexcept { return addr_ != other.addr_; }
constexpr bool operator==(const uint32_t other) const noexcept { return addr_ == other; }
constexpr bool operator!=(const uint32_t other) const noexcept { return addr_ != other; }
constexpr bool operator==(const IpAddr &other) const noexcept {
return addr_ == other.addr_;
}
constexpr bool operator!=(const IpAddr &other) const noexcept {
return addr_ != other.addr_;
}
constexpr bool operator==(const uint32_t other) const noexcept {
return addr_ == other;
}
constexpr bool operator!=(const uint32_t other) const noexcept {
return addr_ != other;
}
constexpr uint32_t uint32() const noexcept { return addr_; }
};
class MacAddr {
@ -33,10 +42,19 @@ class MacAddr {
MacAddr(const char *address);
std::string str() const;
std::string hex() const;
constexpr bool operator==(const MacAddr &other) const noexcept { return addr_ == other.addr_; }
constexpr bool operator!=(const MacAddr &other) const noexcept { return addr_ != other.addr_; }
constexpr bool operator==(const uint64_t other) const noexcept { return addr_ == other; }
constexpr bool operator!=(const uint64_t other) const noexcept { return addr_ != other; }
constexpr bool operator==(const MacAddr &other) const noexcept {
return addr_ == other.addr_;
}
constexpr bool operator!=(const MacAddr &other) const noexcept {
return addr_ != other.addr_;
}
constexpr bool operator==(const uint64_t other) const noexcept {
return addr_ == other;
}
constexpr bool operator!=(const uint64_t other) const noexcept {
return addr_ != other;
}
constexpr uint64_t uint64() const noexcept { return addr_; }
};
std::ostream &operator<<(std::ostream &out, const IpAddr &addr);

View File

@ -78,7 +78,8 @@ std::ostream &operator<<(std::ostream &out, const MacAddr &addr) {
}
uint32_t HostnameToIp(const char *hostname) {
struct addrinfo hints, *result;
addrinfo hints;
addrinfo *result = nullptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@ -86,7 +87,7 @@ uint32_t HostnameToIp(const char *hostname) {
freeaddrinfo(result);
throw RuntimeError("Could not convert hostname to ip");
}
uint32_t ip = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
uint32_t ip = ((sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
return ip;
}

View File

@ -5,14 +5,15 @@
#include <vector>
#include "string_utils.h"
#include "sls_detector_exceptions.h"
using namespace sls;
TEST_CASE("Convert mac address using classes") {
TEST_CASE("Convert mac address using classes", "[support]") {
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4", "ee:50:22:46:d9:f4"};
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4",
"ee:50:22:46:d9:f4"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto mac0 = MacAddr(vec_addr[i]);
auto mac1 = MacAddr(vec_ans[i]);
@ -26,10 +27,24 @@ TEST_CASE("Convert mac address using classes") {
}
}
TEST_CASE("Hex representation of MAC", "[support]") {
MacAddr m{346856806822};
CHECK(m.hex() == "0050c246d9a6");
CHECK(m.str() == "00:50:c2:46:d9:a6");
CHECK_FALSE(m == 7);
TEST_CASE("Convert IP using classes ") {
MacAddr m2{"00:50:c2:46:d9:c4"};
CHECK(m2 == 346856806852);
CHECK(m2.hex() == "0050c246d9c4");
CHECK(m2.str() == "00:50:c2:46:d9:c4");
CHECK_FALSE(m2 == 3);
}
TEST_CASE("Convert IP using classes ", "[support]") {
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163", "129.129.202.160"};
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163",
"129.129.202.160"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto ip0 = IpAddr(vec_addr[i]);
@ -45,7 +60,21 @@ TEST_CASE("Convert IP using classes ") {
}
}
TEST_CASE("Strange input gives 0"){
CHECK(IpAddr("hej")== 0);
CHECK(MacAddr("hej")== 0);
TEST_CASE("Strange input gives 0", "[support]") {
CHECK(IpAddr("hej") == 0);
CHECK(MacAddr("hej") == 0);
}
TEST_CASE("Convert to uint for sending over network", "[support]") {
MacAddr addr{346856806822};
uint64_t a = addr.uint64();
CHECK(a == 346856806822);
IpAddr addr2{"129.129.205.242"};
uint32_t b = addr2.uint32();
CHECK(b == 4073554305);
}
TEST_CASE("Hostname lookup failed throws", "[support]"){
CHECK_THROWS_AS(HostnameToIp("pippifax"), RuntimeError);
}