strawberry/tests/test_fleet.cpp

72 lines
2.7 KiB
C++

#include <cstdio>
#include <cstdlib>
#include <string>
#include "strawberry/fleet.hpp"
using strawberry::fleet::kNodeUidHexLen;
using strawberry::fleet::kNodeUnknown;
using strawberry::fleet::node_uid_from;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
std::exit(1); \
} \
} while (0)
static bool is_lower_hex(const std::string& s) {
for (char c : s)
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;
return true;
}
int main() {
// a. determinism
CHECK(node_uid_from("", "abc", "") == node_uid_from("", "abc", ""));
// b. precedence
const std::string all = node_uid_from("op", "mid", "host");
CHECK(all == node_uid_from("op", "", ""));
CHECK(all == node_uid_from("op", "other", "otherhost"));
CHECK(all != node_uid_from("", "mid", "host"));
const std::string mh = node_uid_from("", "mid", "host");
CHECK(mh == node_uid_from("", "mid", ""));
CHECK(mh == node_uid_from("", "mid", "otherhost"));
CHECK(mh != node_uid_from("", "", "host"));
// c. shape
const std::string r = node_uid_from("", "abc", "");
CHECK(r.size() == kNodeUidHexLen);
CHECK(is_lower_hex(r));
// d. sentinel
CHECK(node_uid_from("", "", "") == kNodeUnknown);
CHECK(node_uid_from(" ", "\n", "\t") == "node_unknown");
CHECK(node_uid_from(" ", "", "") == "node_unknown");
CHECK(node_uid_from("", "\n", "") == "node_unknown");
CHECK(node_uid_from("", "", "\t") == "node_unknown");
// e. whitespace-insensitivity
CHECK(node_uid_from("", " abc \n", "") == node_uid_from("", "abc", ""));
// f. no raw leak
const std::string mid = "b9d1e2f3a4c5d6e7f8091a2b3c4d5e6f";
const std::string opid = "operator-node-identifier-long-0123456789";
const std::string host = "some-long-hostname.example.internal";
CHECK(node_uid_from("", mid, "").find(mid) == std::string::npos);
CHECK(node_uid_from(opid, "", "").find(opid) == std::string::npos);
CHECK(node_uid_from("", "", host).find(host) == std::string::npos);
// g. domain separation
const std::string x_op = node_uid_from("x", "", "");
const std::string x_mid = node_uid_from("", "x", "");
const std::string x_host = node_uid_from("", "", "x");
CHECK(x_op != x_mid);
CHECK(x_mid != x_host);
CHECK(x_op != x_host);
std::puts("test_fleet: all checks passed");
return 0;
}