mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-22 17:47:59 +02:00
clean up
This commit is contained in:
@ -4,11 +4,6 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
std::string MacAddrToString(uint64_t mac);
|
||||
uint64_t MacStringToUint(std::string mac);
|
||||
uint32_t IpStringToUint(const char *ipstr);
|
||||
std::string IpToString(uint32_t ip);
|
||||
std::string IpToHexString(uint32_t ip);
|
||||
uint32_t HostnameToIp(const char *hostname);
|
||||
|
||||
class IpAddr {
|
||||
@ -16,7 +11,7 @@ class IpAddr {
|
||||
uint32_t addr_{0};
|
||||
|
||||
public:
|
||||
IpAddr(uint32_t address);
|
||||
constexpr IpAddr(uint32_t address) noexcept: addr_{address} {}
|
||||
IpAddr(const std::string &address);
|
||||
IpAddr(const char *address);
|
||||
std::string str() const;
|
||||
@ -33,7 +28,7 @@ class MacAddr {
|
||||
std::string to_hex(const char delimiter = 0) const;
|
||||
|
||||
public:
|
||||
MacAddr(uint64_t mac);
|
||||
constexpr MacAddr(uint64_t mac) noexcept : addr_{mac} {}
|
||||
MacAddr(std::string mac);
|
||||
MacAddr(const char *address);
|
||||
std::string str() const { return to_hex(':'); }
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
@ -15,7 +16,7 @@
|
||||
|
||||
namespace sls {
|
||||
|
||||
IpAddr::IpAddr(uint32_t address) : addr_{address} {}
|
||||
|
||||
IpAddr::IpAddr(const std::string &address) { inet_pton(AF_INET, address.c_str(), &addr_); }
|
||||
IpAddr::IpAddr(const char *address) { inet_pton(AF_INET, address, &addr_); }
|
||||
std::string IpAddr::str() const {
|
||||
@ -32,18 +33,18 @@ std::string IpAddr::hex() const {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
MacAddr::MacAddr(uint64_t mac) : addr_{mac} {}
|
||||
MacAddr::MacAddr(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
addr_ = 0;
|
||||
} else {
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::strtoul(mac.c_str(), nullptr, 16);
|
||||
}
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
addr_ = std::stol(mac, nullptr, 16);
|
||||
}
|
||||
MacAddr::MacAddr(const char *address) : MacAddr(std::string(address)) {}
|
||||
|
||||
std::string MacAddr::to_hex(const char delimiter) const{
|
||||
std::string MacAddr::to_hex(const char delimiter) const {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((addr_ >> 40) & 0xFF);
|
||||
@ -55,53 +56,9 @@ std::string MacAddr::to_hex(const char delimiter) const{
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
std::ostream &operator<<(std::ostream &out, const IpAddr &addr) { return out << addr.str(); }
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr){
|
||||
return out << addr.str();
|
||||
}
|
||||
|
||||
std::string MacAddrToString(uint64_t mac) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
ss << ((mac >> 40) & 0xFF);
|
||||
for (int i = 32; i >= 0; i -= 8) {
|
||||
ss << ':' << ((mac >> i) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
uint64_t MacStringToUint(std::string mac) {
|
||||
if ((mac.length() != 17) || (mac[2] != ':') || (mac[5] != ':') || (mac[8] != ':') ||
|
||||
(mac[11] != ':') || (mac[14] != ':')) {
|
||||
return 0;
|
||||
}
|
||||
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
|
||||
return std::stol(mac, nullptr, 16);
|
||||
}
|
||||
|
||||
uint32_t IpStringToUint(const char *ipstr) {
|
||||
uint32_t ip{0};
|
||||
inet_pton(AF_INET, ipstr, &ip);
|
||||
return ip;
|
||||
}
|
||||
|
||||
std::string IpToString(uint32_t ip) {
|
||||
char ipstring[INET_ADDRSTRLEN]{};
|
||||
inet_ntop(AF_INET, &ip, ipstring, INET_ADDRSTRLEN);
|
||||
return ipstring;
|
||||
}
|
||||
|
||||
std::string IpToHexString(uint32_t ip) {
|
||||
std::ostringstream ss;
|
||||
ss << std::hex << std::setfill('0') << std::setw(2);
|
||||
for (int i = 0; i != 4; ++i) {
|
||||
ss << ((ip >> i * 8) & 0xFF);
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
std::ostream &operator<<(std::ostream &out, const MacAddr &addr) { return out << addr.str(); }
|
||||
|
||||
uint32_t HostnameToIp(const char *hostname) {
|
||||
struct addrinfo hints, *result;
|
||||
|
@ -7,19 +7,7 @@
|
||||
#include "string_utils.h"
|
||||
|
||||
using namespace sls;
|
||||
TEST_CASE("Convert mac address") {
|
||||
|
||||
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"};
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto mac = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
std::string string_addr = MacAddrToString(mac);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(MacStringToUint(string_addr) == mac);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert mac address using classes") {
|
||||
|
||||
@ -38,19 +26,6 @@ TEST_CASE("Convert mac address using classes") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP") {
|
||||
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"};
|
||||
|
||||
for (int i = 0; i != vec_addr.size(); ++i) {
|
||||
auto ip = vec_addr[i];
|
||||
auto answer = vec_ans[i];
|
||||
|
||||
auto string_addr = IpToString(ip);
|
||||
CHECK(string_addr == answer);
|
||||
CHECK(IpStringToUint(string_addr.c_str()) == ip);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Convert IP using classes ") {
|
||||
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
|
||||
@ -70,19 +45,7 @@ TEST_CASE("Convert IP using classes ") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("IP not valid") {
|
||||
|
||||
CHECK(IpStringToUint("hej") == 0);
|
||||
CHECK(IpStringToUint("mpc2408") == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Convert ip to hex") {
|
||||
std::vector<std::string> ipstrings{"74.125.43.99", "129.129.202.217"};
|
||||
std::vector<std::string> vec_ans{"4a7d2b63", "8181cad9"};
|
||||
for (int i = 0; i != ipstrings.size(); ++i) {
|
||||
uint32_t ip = IpStringToUint(ipstrings[i].c_str());
|
||||
CHECK(IpToHexString(ip) == vec_ans[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// TEST_CASE("Lookup ip")
|
||||
TEST_CASE("Strange input gives 0"){
|
||||
CHECK(IpAddr("hej")== 0);
|
||||
CHECK(MacAddr("hej")== 0);
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#include "MySocketTCP.h"
|
||||
#include "catch.hpp"
|
||||
// #include "multiSlsDetector.h"
|
||||
#include "logger.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@ -23,6 +22,9 @@ TEST_CASE("copy a string") {
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef NDEBUG
|
||||
//This test can only run in release since we assert on the length of the string
|
||||
TEST_CASE("copy a long string"){
|
||||
auto src = "some very very long sting that does not fit";
|
||||
char dst[3];
|
||||
@ -32,7 +34,7 @@ TEST_CASE("copy a long string"){
|
||||
REQUIRE(dst[2]=='\0');
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
TEST_CASE("Concat") {
|
||||
std::vector<std::string> v{"one", "one", "one"};
|
||||
std::vector<std::string> v2{"one", "one", "one"};
|
||||
|
Reference in New Issue
Block a user