From e821d617802ebbfff756844e4d9720342fc78a4e Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 13 Nov 2026 14:12:49 +0100 Subject: Swarm POC --- modules/swarm/tests/signal.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 modules/swarm/tests/signal.cpp (limited to 'modules/swarm/tests/signal.cpp') diff --git a/modules/swarm/tests/signal.cpp b/modules/swarm/tests/signal.cpp new file mode 100644 index 0000000..fc12193 --- /dev/null +++ b/modules/swarm/tests/signal.cpp @@ -0,0 +1,82 @@ +#include + +#include "../c++/signal.hpp" +#include "../c++/wire.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +SAW_TEST("Signal / an offer round trips"){ + data desc; + desc.get<"type">().set(std::string{sig::offer}); + desc.get<"sdp">().set(std::string{"v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\n"}); + + auto eo_text = encode_message(desc); + SAW_EXPECT(not eo_text.is_error(), "An offer must encode"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "An offer must decode"); + SAW_EXPECT(eo_back.get_value().get<"type">().stl_view() == sig::offer, + "type survives"); + // SDP is multi-line and full of characters JSON has to escape; that it + // comes back byte for byte is the only interesting property here. + SAW_EXPECT(eo_back.get_value().get<"sdp">().stl_view() + == "v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\n", + "SDP must survive escaping unchanged"); +} + +SAW_TEST("Signal / a candidate round trips"){ + data cand; + cand.get<"type">().set(std::string{sig::candidate}); + cand.get<"candidate">().set(std::string{"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"}); + cand.get<"mid">().set(std::string{"0"}); + + auto eo_text = encode_message(cand); + SAW_EXPECT(not eo_text.is_error(), "A candidate must encode"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "A candidate must decode"); + SAW_EXPECT(eo_back.get_value().get<"mid">().stl_view() == "0", "mid survives"); +} + +SAW_TEST("Signal / the header routes without knowing the payload"){ + data desc; + desc.get<"type">().set(std::string{sig::answer}); + desc.get<"sdp">().set(std::string{"v=0"}); + auto eo_text = encode_message(desc); + SAW_EXPECT(not eo_text.is_error(), "encodes"); + + auto eo_hdr = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_hdr.is_error(), "A header read must skip the sdp"); + SAW_EXPECT(eo_hdr.get_value().get<"type">().stl_view() == sig::answer, + "Header reports answer"); +} + +SAW_TEST("Signal / an empty mid is carried rather than dropped"){ + // libdatachannel accepts an unhinted candidate, so the empty string has to + // survive the round trip as an empty string and not become a decode error. + data cand; + cand.get<"type">().set(std::string{sig::candidate}); + cand.get<"candidate">().set(std::string{"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"}); + cand.get<"mid">().set(std::string{""}); + + auto eo_text = encode_message(cand); + SAW_EXPECT(not eo_text.is_error(), "encodes"); + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "An empty mid must still decode"); + SAW_EXPECT(eo_back.get_value().get<"mid">().stl_view().empty(), + "An empty mid stays empty"); +} + +SAW_TEST("Signal / a candidate missing its mid is rejected"){ + // Strictness applies here too: a signalling message that lost a member in + // transit should fail loudly rather than add a candidate with no mid. + auto eo = decode_message( + R"({"type":"candidate","candidate":"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"})"); + SAW_EXPECT(eo.is_error(), "A missing mid must be an error"); +} +} -- cgit v1.3.1