From: Mikko Rasa Date: Sat, 10 Dec 2022 18:01:33 +0000 (+0200) Subject: Add unit tests for some classes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e9204f3fef08b617e59826edbf81035abd9465c3;p=libs%2Fnet.git Add unit tests for some classes --- diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..ee4c926 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +/test diff --git a/tests/Build b/tests/Build new file mode 100644 index 0000000..971a59d --- /dev/null +++ b/tests/Build @@ -0,0 +1,11 @@ +package "mspnet-tests" +{ + require "mspcore"; + require "mspnet"; + require "msptest"; + + program "test" + { + source "."; + }; +}; diff --git a/tests/http_header.cpp b/tests/http_header.cpp new file mode 100644 index 0000000..e123714 --- /dev/null +++ b/tests/http_header.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; +using namespace Msp; + +class HttpHeaderTests: public Test::RegisteredTest +{ +public: + HttpHeaderTests(); + + static const char *get_name() { return "Http::Header"; } + +private: + void single_value(); + void list_of_values(); + void key_value_list(); + void value_with_attributes(); + void list_with_attributes(); + void quoted_values(); +}; + +HttpHeaderTests::HttpHeaderTests() +{ + add(&HttpHeaderTests::single_value, "Single value"); + add(&HttpHeaderTests::list_of_values, "List"); + add(&HttpHeaderTests::key_value_list, "Key-value list"); + add(&HttpHeaderTests::value_with_attributes, "Value with attributes"); + add(&HttpHeaderTests::list_with_attributes, "List with attributes"); + add(&HttpHeaderTests::quoted_values, "Quoted values"); +} + +void HttpHeaderTests::single_value() +{ + Http::Header header("test", "1;2 , 3 ", Http::Header::SINGLE_VALUE); + EXPECT_EQUAL(header.values.size(), 1); + EXPECT_EQUAL(header.values.front().value, "1;2 , 3"); + EXPECT(header.values.front().parameters.empty()); +} + +void HttpHeaderTests::list_of_values() +{ + Http::Header header("test", "1;2 , 3 ", Http::Header::LIST); + EXPECT_EQUAL(header.values.size(), 3); + EXPECT_EQUAL(header.values[0].value, "1"); + EXPECT_EQUAL(header.values[1].value, "2"); + EXPECT_EQUAL(header.values[2].value, "3"); +} + +void HttpHeaderTests::key_value_list() +{ + Http::Header header("test", "a=1; b = 2 ,c=3 ", Http::Header::KEY_VALUE_LIST); + EXPECT_EQUAL(header.values.size(), 1); + EXPECT_EQUAL(header.values.front().parameters.count("a"), 1); + EXPECT_EQUAL(header.values.front().parameters["a"], "1"); + EXPECT_EQUAL(header.values.front().parameters.count("b"), 1); + EXPECT_EQUAL(header.values.front().parameters["b"], "2"); + EXPECT_EQUAL(header.values.front().parameters.count("c"), 1); + EXPECT_EQUAL(header.values.front().parameters["c"], "3"); +} + +void HttpHeaderTests::value_with_attributes() +{ + Http::Header header("test", "X;a=1, 2 ; b=3 ", Http::Header::VALUE_WITH_ATTRIBUTES); + EXPECT_EQUAL(header.values.size(), 1); + EXPECT_EQUAL(header.values.front().value, "X"); + EXPECT_EQUAL(header.values.front().parameters.count("a"), 1); + EXPECT_EQUAL(header.values.front().parameters["a"], "1, 2"); + EXPECT_EQUAL(header.values.front().parameters.count("b"), 1); + EXPECT_EQUAL(header.values.front().parameters["b"], "3"); +} + +void HttpHeaderTests::list_with_attributes() +{ + Http::Header header("test", "X;a= 1;b= 2 ,Y ; c=3 ", Http::Header::LIST_WITH_ATTRIBUTES); + EXPECT_EQUAL(header.values.size(), 2); + EXPECT_EQUAL(header.values[0].value, "X"); + EXPECT_EQUAL(header.values[0].parameters.count("a"), 1); + EXPECT_EQUAL(header.values[0].parameters["a"], "1"); + EXPECT_EQUAL(header.values[0].parameters.count("b"), 1); + EXPECT_EQUAL(header.values[0].parameters["b"], "2"); + EXPECT_EQUAL(header.values[1].value, "Y"); + EXPECT_EQUAL(header.values[1].parameters.count("c"), 1); + EXPECT_EQUAL(header.values[1].parameters["c"], "3"); +} + +void HttpHeaderTests::quoted_values() +{ + Http::Header header("test", "X;a= 1;b=\" ;2, \",Y ; c=\"3 \" ", Http::Header::LIST_WITH_ATTRIBUTES); + EXPECT_EQUAL(header.values.size(), 2); + EXPECT_EQUAL(header.values[0].value, "X"); + EXPECT_EQUAL(header.values[0].parameters.count("a"), 1); + EXPECT_EQUAL(header.values[0].parameters["a"], "1"); + EXPECT_EQUAL(header.values[0].parameters.count("b"), 1); + EXPECT_EQUAL(header.values[0].parameters["b"], " ;2, "); + EXPECT_EQUAL(header.values[1].value, "Y"); + EXPECT_EQUAL(header.values[1].parameters.count("c"), 1); + EXPECT_EQUAL(header.values[1].parameters["c"], "3 "); +} diff --git a/tests/protocol.cpp b/tests/protocol.cpp new file mode 100644 index 0000000..0f4b7d2 --- /dev/null +++ b/tests/protocol.cpp @@ -0,0 +1,179 @@ +#include +#include + +using namespace std; +using namespace Msp; + +class ProtocolTests: public Test::RegisteredTest +{ +public: + ProtocolTests(); + + static const char *get_name() { return "Protocol"; } + +private: + void hash_match(); + void buffer_overflow(); + void truncated_packet(); + void stub_header(); + + template + void transmit(const P &, P &, size_t); + + void transmit_int(); + void transmit_string(); + void transmit_array(); + void transmit_composite(); +}; + + +class Protocol: public Msp::Net::Protocol +{ +public: + Protocol(); +}; + +struct Packet1 +{ + uint32_t value; +}; + +struct Packet2 +{ + std::string value; +}; + +struct Packet3 +{ + std::vector values; +}; + +struct Packet4 +{ + Packet2 sub1; + std::vector sub2; +}; + +Protocol::Protocol() +{ + add()(&Packet1::value); + add()(&Packet2::value); + add()(&Packet3::values); + add()(&Packet4::sub1)(&Packet4::sub2); +} + +template +class Receiver: public Net::PacketReceiver +{ +private: + T &storage; + +public: + Receiver(T &s): storage(s) { } + + void receive(const T &p) override { storage = p; } +}; + + +ProtocolTests::ProtocolTests() +{ + add(&ProtocolTests::hash_match, "Hash match"); + add(&ProtocolTests::buffer_overflow, "Serialization buffer overflow").expect_throw(); + add(&ProtocolTests::truncated_packet, "Truncated packet").expect_throw(); + add(&ProtocolTests::stub_header, "Stub header"); + add(&ProtocolTests::transmit_int, "Integer transmission"); + add(&ProtocolTests::transmit_string, "String transmission"); + add(&ProtocolTests::transmit_array, "Array transmission"); + add(&ProtocolTests::transmit_composite, "Composite transmission"); +} + +void ProtocolTests::hash_match() +{ + Protocol proto1; + Protocol proto2; + EXPECT_EQUAL(proto1.get_hash(), proto2.get_hash()); +} + +void ProtocolTests::buffer_overflow() +{ + Protocol proto; + Packet1 pkt = { 42 }; + char buffer[7]; + proto.serialize(pkt, buffer, sizeof(buffer)); +} + +void ProtocolTests::truncated_packet() +{ + Protocol proto; + Packet1 pkt = { 42 }; + char buffer[16]; + size_t len = proto.serialize(pkt, buffer, sizeof(buffer)); + Receiver recv(pkt); + proto.dispatch(recv, buffer, len-1); +} + +void ProtocolTests::stub_header() +{ + Protocol proto; + char buffer[3] = { 4, 0, 1 }; + size_t len = proto.get_packet_size(buffer, sizeof(buffer)); + EXPECT_EQUAL(len, 0); +} + +template +void ProtocolTests::transmit(const P &pkt, P &rpkt, size_t expected_length) +{ + Protocol proto; + char buffer[128]; + size_t len = proto.serialize(pkt, buffer, sizeof(buffer)); + EXPECT_EQUAL(len, expected_length); + + size_t rlen = proto.get_packet_size(buffer, sizeof(buffer)); + EXPECT_EQUAL(rlen, len); + + Receiver

recv(rpkt); + size_t dlen = proto.dispatch(recv, buffer, sizeof(buffer)); + EXPECT_EQUAL(dlen, len); +} + +void ProtocolTests::transmit_int() +{ + Packet1 pkt = { 42 }; + Packet1 rpkt; + transmit(pkt, rpkt, 8); + EXPECT_EQUAL(rpkt.value, 42); +} + +void ProtocolTests::transmit_string() +{ + Packet2 pkt = { "Hello" }; + Packet2 rpkt; + transmit(pkt, rpkt, 11); + EXPECT_EQUAL(rpkt.value, "Hello"); +} + +void ProtocolTests::transmit_array() +{ + Packet3 pkt = {{ 2, 3, 5, 7, 11 }}; + Packet3 rpkt; + transmit(pkt, rpkt, 26); + EXPECT_EQUAL(rpkt.values.size(), 5); + for(size_t i=0; i