12-udp-receiver.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "limbo/ip/state.hpp"
  2. #include "limbo/stack.hpp"
  3. #include "limbo/udp/receiver.hpp"
  4. #include "test-utils.h"
  5. #include <type_traits>
  6. using namespace limbo;
  7. using IP = ip::State<void, ip::Opts::calculate_checksum>;
  8. using UDP = udp::State<IP>;
  9. using Receiver = udp::Receiver<UDP>;
  10. using MyStack = Stack<Layer<Receiver>, 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-receiver]") {
  21. auto stack = MyStack();
  22. auto &ip_state = stack.get<2, 0>();
  23. auto &udp_recv = stack.get<0, 0>();
  24. ip_state.init(0x04);
  25. udp_recv.init({src, 7}, 6);
  26. unsigned char buff[sizeof(example_raw)];
  27. auto buff_chunk = Chunk(buff, sizeof(buff));
  28. auto result = udp_recv.send(stack, buff_chunk, {dst, 8}, 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-receiver]") {
  36. auto stack = MyStack();
  37. auto &udp_recv = stack.get<0, 0>();
  38. udp_recv.init({dst, 8}, 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_recv);
  43. auto &p = udp_recv.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 to wrong port", "[udp-receiver]") {
  52. auto stack = MyStack();
  53. auto &udp_recv = stack.get<0, 0>();
  54. auto &udp_state = stack.get<1, 0>();
  55. /* dst and src are NOT swapped =>
  56. * listening port 7, but receving upon port 8 */
  57. udp_recv.init({src, 7}, 6);
  58. auto result = stack.recv(example_chunk, nullptr);
  59. REQUIRE(result);
  60. REQUIRE(result.consumed() == example_chunk);
  61. REQUIRE(result.state() == &udp_state);
  62. auto &p = udp_state.get_parsed();
  63. CHECK(p.source_port == 7);
  64. CHECK(p.dest_port == 8);
  65. CHECK(p.length == payload_chunk.size() + 8);
  66. CHECK(p.payload == payload_chunk);
  67. CHECK(p.container->source == src);
  68. CHECK(p.container->destination == dst);
  69. }