11-udp-connection.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "limbo/errc.h"
  2. #include "limbo/ip/state.hpp"
  3. #include "limbo/stack.hpp"
  4. #include "limbo/udp/connection.hpp"
  5. #include "test-utils.h"
  6. using namespace limbo;
  7. using IP = ip::State<void, ip::Opts::calculate_checksum>;
  8. using UDP = udp::State<IP>;
  9. using Connection = udp::Connection<UDP>;
  10. using MyStack = Stack<Layer<Connection>, Layer<UDP>, Layer<IP>>;
  11. unsigned char example_raw[20 + 8 + 5] = {
  12. /* hdr */ 0x45, 0x00, 0x00, 0x21, 0x00, 0x05, 0x00, 0x00,
  13. /* ttl */ 0x06, 0x11, 0xaa, 0x50, 0x0a, 0x00, 0x1f, 0x7c,
  14. /* dst */ 0xe0, 0x00, 0x00, 0xfb, 0x00, 0x07, 0x00, 0x08,
  15. /* len */ 0x00, 0x0D, 0x00, 0x00, 'h', 'e', 'l', 'l', 'o'};
  16. auto example_chunk = Chunk(example_raw, sizeof(example_raw));
  17. auto payload_chunk = Chunk((void *)(example_raw + 28), 5);
  18. auto src = make_address("10.0.31.124");
  19. auto dst = make_address("224.0.0.251");
  20. TEST_CASE("send", "[udp-connection]") {
  21. auto stack = MyStack();
  22. auto &ip_state = stack.get<2, 0>();
  23. auto &udp_conn = stack.get<0, 0>();
  24. ip_state.init(0x04);
  25. udp_conn.init({src, 7}, {dst, 8}, 6);
  26. unsigned char buff[sizeof(example_raw)];
  27. auto buff_chunk = Chunk(buff, sizeof(buff));
  28. auto result = udp_conn.send(stack, buff_chunk, payload_chunk);
  29. REQUIRE(result);
  30. auto res_buff = result.consumed();
  31. CHECK((void *)res_buff.data() == (void *)buff);
  32. CHECK(res_buff.size() == example_chunk.size());
  33. CHECK(res_buff == example_chunk);
  34. }
  35. TEST_CASE("receive", "[udp-connection]") {
  36. auto stack = MyStack();
  37. auto &udp_conn = stack.get<0, 0>();
  38. udp_conn.init({dst, 8}, {src, 7}, 6); /* dst and src are swapped */
  39. auto result = stack.recv(example_chunk, nullptr);
  40. REQUIRE(result);
  41. REQUIRE(result.consumed() == example_chunk);
  42. REQUIRE(result.state() == &udp_conn);
  43. auto &p = udp_conn.get_parsed();
  44. CHECK(p.source_port == 7);
  45. CHECK(p.dest_port == 8);
  46. CHECK(p.length == payload_chunk.size() + 8);
  47. CHECK(p.payload == payload_chunk);
  48. CHECK(p.container->source == src);
  49. CHECK(p.container->destination == dst);
  50. }
  51. TEST_CASE("receive from wrong source", "[udp-connection]") {
  52. auto stack = MyStack();
  53. auto &udp_conn = stack.get<0, 0>();
  54. auto &udp_state = stack.get<1, 0>();
  55. udp_conn.init({src, 7}, {dst, 8}, 6); /* dst and src are NOT swapped */
  56. auto result = stack.recv(example_chunk, nullptr);
  57. REQUIRE(result);
  58. REQUIRE(result.consumed() == example_chunk);
  59. REQUIRE(result.state() == &udp_state);
  60. auto &p = udp_state.get_parsed();
  61. CHECK(p.source_port == 7);
  62. CHECK(p.dest_port == 8);
  63. CHECK(p.length == payload_chunk.size() + 8);
  64. CHECK(p.payload == payload_chunk);
  65. CHECK(p.container->source == src);
  66. CHECK(p.container->destination == dst);
  67. }