commit
38e86d4a07
23 files changed
+3219
No files matched your search
@@ -0,0 +1,12 @@
|
||||
# Unit tests for EchoionProtocol. Uses a tiny header-only harness (check.hpp),
|
||||
# no external test framework required.
|
||||
|
||||
file(GLOB ECHOIONPROTOCOL_TEST_SOURCES CONFIGURE_DEPENDS
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp")
|
||||
|
||||
add_executable(echoion_protocol_tests ${ECHOIONPROTOCOL_TEST_SOURCES})
|
||||
target_link_libraries(echoion_protocol_tests PRIVATE EchoionProtocol)
|
||||
target_include_directories(echoion_protocol_tests PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
add_test(NAME echoion_protocol_tests COMMAND echoion_protocol_tests)
|
||||
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
// Minimal dependency-free test harness. Tests register cases with TEST_CASE and
|
||||
// assert with CHECK / CHECK_EQ; main() runs them and reports a non-zero exit on
|
||||
// any failure so ctest treats it as a failing test.
|
||||
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace echotest
|
||||
{
|
||||
struct Case {
|
||||
std::string name;
|
||||
std::function<void()> fn;
|
||||
};
|
||||
|
||||
inline std::vector<Case>& registry()
|
||||
{
|
||||
static std::vector<Case> cases;
|
||||
return cases;
|
||||
}
|
||||
|
||||
inline int& failures()
|
||||
{
|
||||
static int f = 0;
|
||||
return f;
|
||||
}
|
||||
|
||||
struct Registrar {
|
||||
Registrar(const std::string& name, std::function<void()> fn)
|
||||
{
|
||||
registry().push_back({name, std::move(fn)});
|
||||
}
|
||||
};
|
||||
|
||||
inline int run()
|
||||
{
|
||||
int failed = 0;
|
||||
for (auto& c : registry()) {
|
||||
int before = failures();
|
||||
try {
|
||||
c.fn();
|
||||
} catch (const std::exception& e) {
|
||||
std::printf(" [THROW] %s: %s\n", c.name.c_str(), e.what());
|
||||
++failures();
|
||||
}
|
||||
if (failures() > before) {
|
||||
std::printf("[FAIL] %s\n", c.name.c_str());
|
||||
++failed;
|
||||
} else {
|
||||
std::printf("[ok] %s\n", c.name.c_str());
|
||||
}
|
||||
}
|
||||
std::printf("\n%zu cases, %d failed\n", registry().size(), failed);
|
||||
return failed == 0 ? 0 : 1;
|
||||
}
|
||||
} // namespace echotest
|
||||
|
||||
#define TEST_CASE(name) \
|
||||
static void name(); \
|
||||
static echotest::Registrar reg_##name(#name, name); \
|
||||
static void name()
|
||||
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::printf(" CHECK failed: %s (%s:%d)\n", #cond, __FILE__, \
|
||||
__LINE__); \
|
||||
++echotest::failures(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_EQ(a, b) \
|
||||
do { \
|
||||
if (!((a) == (b))) { \
|
||||
std::printf(" CHECK_EQ failed: %s == %s (%s:%d)\n", #a, #b, \
|
||||
__FILE__, __LINE__); \
|
||||
++echotest::failures(); \
|
||||
} \
|
||||
} while (0)
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "check.hpp"
|
||||
#include "echoion/protocol/Command.hpp"
|
||||
|
||||
using namespace Echoion::Protocol;
|
||||
|
||||
TEST_CASE(escape_roundtrip)
|
||||
{
|
||||
std::string raw = "hello world|pipe/slash\ttab\nnl\\back";
|
||||
auto esc = tsEscape(raw);
|
||||
// Spaces and specials must be escaped (no raw space/pipe left).
|
||||
CHECK(esc.find(' ') == std::string::npos);
|
||||
CHECK(esc.find('|') == std::string::npos);
|
||||
CHECK_EQ(tsUnescape(esc), raw);
|
||||
}
|
||||
|
||||
TEST_CASE(escape_known)
|
||||
{
|
||||
CHECK_EQ(tsEscape("a b"), std::string("a\\sb"));
|
||||
CHECK_EQ(tsEscape("a|b"), std::string("a\\pb"));
|
||||
CHECK_EQ(tsEscape("a/b"), std::string("a\\/b"));
|
||||
CHECK_EQ(tsUnescape("a\\sb"), std::string("a b"));
|
||||
}
|
||||
|
||||
TEST_CASE(build_command)
|
||||
{
|
||||
CommandParams p;
|
||||
p["client_nickname"] = "My Bot";
|
||||
p["cid"] = "5";
|
||||
auto cmd = buildCommand("clientmove", p);
|
||||
// Params are ordered by key (map): cid before client_nickname.
|
||||
CHECK_EQ(cmd, std::string("clientmove cid=5 client_nickname=My\\sBot"));
|
||||
}
|
||||
|
||||
TEST_CASE(parse_notify)
|
||||
{
|
||||
auto pc = parseCommand(
|
||||
"notifytextmessage targetmode=2 msg=hello\\sworld invokerid=7 "
|
||||
"invokername=Alice");
|
||||
CHECK_EQ(pc.name, std::string("notifytextmessage"));
|
||||
CHECK_EQ(pc.params["targetmode"], std::string("2"));
|
||||
CHECK_EQ(pc.params["msg"], std::string("hello world"));
|
||||
CHECK_EQ(pc.params["invokername"], std::string("Alice"));
|
||||
}
|
||||
|
||||
TEST_CASE(parse_pipe_groups)
|
||||
{
|
||||
auto pc = parseCommand("channellist cid=1 channel_name=Default|cid=2 "
|
||||
"channel_name=Talk");
|
||||
CHECK_EQ(pc.name, std::string("channellist"));
|
||||
CHECK_EQ(pc.groups.size(), size_t(2));
|
||||
CHECK_EQ(pc.groups[0]["cid"], std::string("1"));
|
||||
CHECK_EQ(pc.groups[0]["channel_name"], std::string("Default"));
|
||||
CHECK_EQ(pc.groups[1]["cid"], std::string("2"));
|
||||
CHECK_EQ(pc.groups[1]["channel_name"], std::string("Talk"));
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "check.hpp"
|
||||
#include "echoion/protocol/Crypto.hpp"
|
||||
#include "echoion/protocol/Util.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace Echoion::Protocol;
|
||||
using Crypto::ByteView;
|
||||
|
||||
namespace
|
||||
{
|
||||
Crypto::Bytes hex(const std::string& h) { return Util::fromHex(h); }
|
||||
std::string hx(ByteView b) { return Util::toHex(b); }
|
||||
std::string hx(const std::vector<uint8_t>& b)
|
||||
{
|
||||
return Util::toHex(ByteView(b.data(), b.size()));
|
||||
}
|
||||
template <size_t N> std::string hx(const std::array<uint8_t, N>& b)
|
||||
{
|
||||
return Util::toHex(ByteView(b.data(), b.size()));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// RFC 4493 AES-128-CMAC known-answer vectors.
|
||||
TEST_CASE(cmac_rfc4493)
|
||||
{
|
||||
auto key = hex("2b7e151628aed2a6abf7158809cf4f3c");
|
||||
|
||||
// Example 1: empty message.
|
||||
CHECK_EQ(hx(Crypto::cmac(key, {})),
|
||||
std::string("bb1d6929e95937287fa37d129b756746"));
|
||||
|
||||
// Example 2: 16-byte message.
|
||||
auto m2 = hex("6bc1bee22e409f96e93d7e117393172a");
|
||||
CHECK_EQ(hx(Crypto::cmac(key, m2)),
|
||||
std::string("070a16b46b4d4144f79bdd9dd04a287c"));
|
||||
|
||||
// Example 3: 40-byte message.
|
||||
auto m3 = hex("6bc1bee22e409f96e93d7e117393172a"
|
||||
"ae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
"30c81c46a35ce411");
|
||||
CHECK_EQ(hx(Crypto::cmac(key, m3)),
|
||||
std::string("dfa66747de9ae63030ca32611497c827"));
|
||||
|
||||
// Example 4: 64-byte message.
|
||||
auto m4 = hex("6bc1bee22e409f96e93d7e117393172a"
|
||||
"ae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
"30c81c46a35ce411e5fbc1191a0a52ef"
|
||||
"f69f2445df4f9b17ad2b417be66c3710");
|
||||
CHECK_EQ(hx(Crypto::cmac(key, m4)),
|
||||
std::string("51f0bebf7e3b9d92fc49741779363cfe"));
|
||||
}
|
||||
|
||||
// EAX specification test vectors (Bellare/Rogaway/Wagner). The EAX output is
|
||||
// ciphertext||tag where tag is the full 16-byte value; TeamSpeak truncates the
|
||||
// MAC to 8 bytes, so we compare against the first 8 tag bytes.
|
||||
TEST_CASE(eax_spec_vectors)
|
||||
{
|
||||
// Empty message.
|
||||
{
|
||||
auto key = hex("233952dee4d5ed5f9b9c6d6ff80ff478");
|
||||
auto nonce = hex("62ec67f9c3a4a407fcb2a8c49031a8b3");
|
||||
auto hdr = hex("6bfb914fd07eae6b");
|
||||
auto res = Crypto::eaxEncrypt(key, nonce, hdr, {});
|
||||
CHECK(res.ciphertext.empty());
|
||||
CHECK_EQ(hx(res.mac), std::string("e037830e8389f27b"));
|
||||
// Round-trip.
|
||||
auto dec = Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, res.mac);
|
||||
CHECK(dec.has_value());
|
||||
CHECK(dec && dec->empty());
|
||||
}
|
||||
|
||||
// 2-byte message F7FB.
|
||||
{
|
||||
auto key = hex("91945d3f4dcbee0bf45ef52255f095a4");
|
||||
auto nonce = hex("becaf043b0a23d843194ba972c66debd");
|
||||
auto hdr = hex("fa3bfd4806eb53fa");
|
||||
auto msg = hex("f7fb");
|
||||
auto res = Crypto::eaxEncrypt(key, nonce, hdr, msg);
|
||||
CHECK_EQ(hx(res.ciphertext), std::string("19dd"));
|
||||
CHECK_EQ(hx(res.mac), std::string("5c4c9331049d0bda"));
|
||||
auto dec = Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, res.mac);
|
||||
CHECK(dec.has_value());
|
||||
CHECK(dec && hx(*dec) == std::string("f7fb"));
|
||||
// Tamper → reject.
|
||||
auto bad = res.mac;
|
||||
bad[0] ^= 0x01;
|
||||
CHECK(!Crypto::eaxDecrypt(key, nonce, hdr, res.ciphertext, bad));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(sha_and_base64)
|
||||
{
|
||||
// sha1("abc") = a9993e364706816aba3e25717850c26c9cd0d89d
|
||||
std::string abc = "abc";
|
||||
auto s1 = Crypto::sha1(
|
||||
ByteView(reinterpret_cast<const uint8_t*>(abc.data()), abc.size()));
|
||||
CHECK_EQ(hx(s1),
|
||||
std::string("a9993e364706816aba3e25717850c26c9cd0d89d"));
|
||||
|
||||
// base64 round-trip + known value.
|
||||
CHECK_EQ(Util::base64Encode(ByteView(
|
||||
reinterpret_cast<const uint8_t*>("foobar"), 6)),
|
||||
std::string("Zm9vYmFy"));
|
||||
auto dec = Util::base64Decode("Zm9vYmFy");
|
||||
CHECK_EQ(std::string(dec.begin(), dec.end()), std::string("foobar"));
|
||||
}
|
||||
|
||||
// deriveKeyNonce: verify the packetId XOR into key[0..1] and the deterministic
|
||||
// structure. Uses a fixed 20-byte ivStruct.
|
||||
TEST_CASE(derive_key_nonce)
|
||||
{
|
||||
auto iv = hex("000102030405060708090a0b0c0d0e0f10111213"); // 20 bytes
|
||||
auto a = Crypto::deriveKeyNonce(false, 0, 0, 2, iv);
|
||||
auto b = Crypto::deriveKeyNonce(false, 0x0102, 0, 2, iv);
|
||||
// key[0] ^= pid>>8, key[1] ^= pid&0xff; nonce unchanged.
|
||||
CHECK_EQ(a.key[0] ^ 0x01, b.key[0]);
|
||||
CHECK_EQ(a.key[1] ^ 0x02, b.key[1]);
|
||||
CHECK_EQ(hx(a.nonce), hx(b.nonce));
|
||||
// Direction changes the derivation.
|
||||
auto c = Crypto::deriveKeyNonce(true, 0, 0, 2, iv);
|
||||
CHECK(hx(a.key) != hx(c.key));
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "check.hpp"
|
||||
#include "echoion/protocol/Crypto.hpp"
|
||||
#include "echoion/protocol/Identity.hpp"
|
||||
#include "echoion/protocol/Util.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
using namespace Echoion::Protocol;
|
||||
|
||||
TEST_CASE(identity_generate_uid_and_level)
|
||||
{
|
||||
auto id = Identity::generate(8);
|
||||
|
||||
// uid must equal base64(sha1(ascii(publicKeyString))).
|
||||
auto h = Crypto::sha1(Crypto::ByteView(
|
||||
reinterpret_cast<const uint8_t*>(id.publicKeyString().data()),
|
||||
id.publicKeyString().size()));
|
||||
CHECK_EQ(id.uid(),
|
||||
Util::base64Encode(Crypto::ByteView(h.data(), h.size())));
|
||||
|
||||
// publicKeyString is valid base64 of a DER SEQUENCE (starts with 0x30).
|
||||
auto der = Util::base64Decode(id.publicKeyString());
|
||||
CHECK(!der.empty());
|
||||
CHECK_EQ(der[0], uint8_t(0x30));
|
||||
|
||||
// Security level actually reached the requested threshold.
|
||||
CHECK(id.securityLevel() >= 8);
|
||||
}
|
||||
|
||||
TEST_CASE(identity_sign_verifiable_length)
|
||||
{
|
||||
auto id = Identity::generate(0);
|
||||
std::string msg = "hello";
|
||||
auto sig = id.sign(Crypto::ByteView(
|
||||
reinterpret_cast<const uint8_t*>(msg.data()), msg.size()));
|
||||
// DER ECDSA signature over P-256 is ~70-72 bytes; never empty.
|
||||
CHECK(!sig.empty());
|
||||
CHECK(sig[0] == 0x30); // DER SEQUENCE
|
||||
}
|
||||
|
||||
TEST_CASE(identity_save_load_roundtrip)
|
||||
{
|
||||
auto id = Identity::generate(0);
|
||||
std::string path = "/tmp/echoion_identity_test.txt";
|
||||
id.save(path);
|
||||
auto loaded = Identity::load(path);
|
||||
CHECK_EQ(loaded.publicKeyString(), id.publicKeyString());
|
||||
CHECK_EQ(loaded.uid(), id.uid());
|
||||
CHECK_EQ(loaded.keyOffset(), id.keyOffset());
|
||||
std::remove(path.c_str());
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "check.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
return echotest::run();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "check.hpp"
|
||||
#include "echoion/protocol/QuickLz.hpp"
|
||||
|
||||
using namespace Echoion::Protocol;
|
||||
|
||||
// QuickLZ level-1 "stored" (uncompressed) block: header byte with level=1
|
||||
// (bits 2-3 = 01 -> 0x04), compressed bit (bit0) = 0, short size form
|
||||
// (bit1 = 0) -> 3-byte header: [flags, compressedSize, decompressedSize].
|
||||
TEST_CASE(qlz_stored_block)
|
||||
{
|
||||
Bytes payload = {'h', 'e', 'l', 'l', 'o'};
|
||||
Bytes buf;
|
||||
buf.push_back(0x04); // flags: level 1, not compressed, short header
|
||||
buf.push_back(static_cast<uint8_t>(3 + payload.size())); // compressed size
|
||||
buf.push_back(static_cast<uint8_t>(payload.size())); // decompressed size
|
||||
buf.insert(buf.end(), payload.begin(), payload.end());
|
||||
|
||||
CHECK_EQ(qlzDecompressedSize(buf), uint32_t(payload.size()));
|
||||
CHECK_EQ(qlzCompressedSize(buf), uint32_t(3 + payload.size()));
|
||||
|
||||
auto out = qlzDecompress(buf);
|
||||
CHECK_EQ(out.size(), payload.size());
|
||||
CHECK(out == payload);
|
||||
}
|
||||
|
||||
TEST_CASE(qlz_rejects_bad_level)
|
||||
{
|
||||
Bytes buf = {0x00, 0x03, 0x00}; // level 0 -> unsupported
|
||||
bool threw = false;
|
||||
try {
|
||||
qlzDecompress(buf);
|
||||
} catch (const std::exception&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
Reference in new issue
Block a user