30-customization.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "limbo/ip/state.hpp"
  2. #include "limbo/stack.hpp"
  3. #include "limbo/udp/state.hpp"
  4. #include "test-utils.h"
  5. using namespace limbo;
  6. uint8_t payload[] = {0x14, 0xe9, 0x14, 0xe9, 0x00, 0x24, 0xb6, 0xaa, 0x00,
  7. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8. 0x00, 0x00, 0x04, 0x77, 0x70, 0x61, 0x64, 0x05, 0x6c,
  9. 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x00, 0x01};
  10. auto payload_chunk = Chunk(payload, sizeof(payload));
  11. auto src = make_address("10.0.31.124");
  12. auto dst = make_address("224.0.0.251");
  13. using IP = ip::State<void, ip::Opts::calculate_checksum>;
  14. using UDP = udp::State<IP>;
  15. using MyStack = Stack<Layer<UDP>, Layer<IP>>;
  16. static uint32_t copy_counter = 0;
  17. namespace limbo {
  18. template <> struct customization<IP> {
  19. static void *copy(void *dest, const void *src, std::size_t count) {
  20. ++copy_counter;
  21. return customization<void>::copy(dest, src, count);
  22. }
  23. static void copy(Chunk &dest, const Chunk source) {
  24. ++copy_counter;
  25. return customization<void>::copy(dest, source);
  26. }
  27. };
  28. } // namespace limbo
  29. TEST_CASE("send ip", "[customization]") {
  30. using Context = typename IP::Context;
  31. using Packet = typename IP::Packet;
  32. char buff[100];
  33. auto buff_chunk = Chunk(buff, sizeof(buff));
  34. auto ctx = Context{src, dst, 1, ip::Proto::udp, 0, 0, nullptr};
  35. auto state = IP();
  36. copy_counter = 0;
  37. state.init(0x82DD);
  38. auto result = Packet::send(state, ctx, buff_chunk, payload_chunk);
  39. REQUIRE(result);
  40. CHECK(copy_counter == 2);
  41. }
  42. TEST_CASE("send udp", "[customization]") {
  43. copy_counter = 0;
  44. auto stack = MyStack();
  45. auto &ip_state = stack.get<1, 0>();
  46. auto &udp_state = stack.get<0, 0>();
  47. ip_state.init(0x04);
  48. auto ip_ctx = IP::Context{src, dst, 6, ip::Proto::udp, 0, 0, nullptr};
  49. auto udp_ctx = UDP::Context{7, 8, &ip_ctx};
  50. unsigned char buff[100];
  51. auto buff_chunk = Chunk(buff, sizeof(buff));
  52. auto result = stack.send(buff_chunk, udp_ctx, payload_chunk);
  53. REQUIRE(result);
  54. CHECK(copy_counter == 4);
  55. }