30-customization.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. ip_state.init(0x04);
  47. auto ip_ctx = IP::Context{src, dst, 6, ip::Proto::udp, 0, 0, nullptr};
  48. auto udp_ctx = UDP::Context{7, 8, &ip_ctx};
  49. unsigned char buff[100];
  50. auto buff_chunk = Chunk(buff, sizeof(buff));
  51. auto result = stack.send(buff_chunk, udp_ctx, payload_chunk);
  52. REQUIRE(result);
  53. CHECK(copy_counter == 4);
  54. }