1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "limbo/errc.h"
- #include "limbo/ip/state.hpp"
- #include "limbo/stack.hpp"
- #include "limbo/udp/connection.hpp"
- #include "test-utils.h"
- using namespace limbo;
- using IP = ip::State<void, ip::Opts::calculate_checksum>;
- using UDP = udp::State<IP>;
- using Connection = udp::Connection<UDP>;
- using MyStack = Stack<Layer<Connection>, Layer<UDP>, Layer<IP>>;
- unsigned char example_raw[20 + 8 + 5] = {
- /* hdr */ 0x45, 0x00, 0x00, 0x21, 0x00, 0x05, 0x00, 0x00,
- /* ttl */ 0x06, 0x11, 0xaa, 0x50, 0x0a, 0x00, 0x1f, 0x7c,
- /* dst */ 0xe0, 0x00, 0x00, 0xfb, 0x00, 0x07, 0x00, 0x08,
- /* len */ 0x00, 0x0D, 0x00, 0x00, 'h', 'e', 'l', 'l', 'o'};
- auto example_chunk = Chunk(example_raw, sizeof(example_raw));
- auto payload_chunk = Chunk((void *)(example_raw + 28), 5);
- auto src = make_address("10.0.31.124");
- auto dst = make_address("224.0.0.251");
- TEST_CASE("send", "[udp-connection]") {
- auto stack = MyStack();
- auto &ip_state = stack.get<2, 0>();
- auto &udp_conn = stack.get<0, 0>();
- ip_state.init(0x04);
- udp_conn.init({src, 7}, {dst, 8}, 6);
- unsigned char buff[sizeof(example_raw)];
- auto buff_chunk = Chunk(buff, sizeof(buff));
- auto result = udp_conn.send(stack, buff_chunk, payload_chunk);
- REQUIRE(result);
- auto res_buff = result.consumed();
- CHECK((void *)res_buff.data() == (void *)buff);
- CHECK(res_buff.size() == example_chunk.size());
- CHECK(res_buff == example_chunk);
- }
- TEST_CASE("receive", "[udp-connection]") {
- auto stack = MyStack();
- auto &udp_conn = stack.get<0, 0>();
- udp_conn.init({dst, 8}, {src, 7}, 6); /* dst and src are swapped */
- auto result = stack.recv(example_chunk, nullptr);
- REQUIRE(result);
- REQUIRE(result.consumed() == example_chunk);
- REQUIRE(result.state() == &udp_conn);
- auto &p = udp_conn.get_parsed();
- CHECK(p.source_port == 7);
- CHECK(p.dest_port == 8);
- CHECK(p.length == payload_chunk.size() + 8);
- CHECK(p.payload == payload_chunk);
- CHECK(p.container->source == src);
- CHECK(p.container->destination == dst);
- }
- TEST_CASE("receive from wrong source", "[udp-connection]") {
- auto stack = MyStack();
- auto &udp_conn = stack.get<0, 0>();
- auto &udp_state = stack.get<1, 0>();
- udp_conn.init({src, 7}, {dst, 8}, 6); /* dst and src are NOT swapped */
- auto result = stack.recv(example_chunk, nullptr);
- REQUIRE(result);
- REQUIRE(result.consumed() == example_chunk);
- REQUIRE(result.state() == &udp_state);
- auto &p = udp_state.get_parsed();
- CHECK(p.source_port == 7);
- CHECK(p.dest_port == 8);
- CHECK(p.length == payload_chunk.size() + 8);
- CHECK(p.payload == payload_chunk);
- CHECK(p.container->source == src);
- CHECK(p.container->destination == dst);
- }
|