01-ip.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "limbo/ip/state.hpp"
  2. #include "test-utils.h"
  3. using namespace limbo;
  4. uint8_t example_raw[]{
  5. 0x45, 0x00, 0x00, 0x38, 0x82, 0xde, 0x00, 0x00, 0x01, 0x11, 0x2c, 0x60,
  6. 0x0a, 0x00, 0x1f, 0x7c, 0xe0, 0x00, 0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9,
  7. 0x00, 0x24, 0xb6, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8. 0x00, 0x00, 0x00, 0x00, 0x04, 0x77, 0x70, 0x61, 0x64, 0x05, 0x6c, 0x6f,
  9. 0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x00, 0x01};
  10. auto example_chunk = Chunk(example_raw, sizeof(example_raw));
  11. uint8_t payload[] = {0x14, 0xe9, 0x14, 0xe9, 0x00, 0x24, 0xb6, 0xaa, 0x00,
  12. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  13. 0x00, 0x00, 0x04, 0x77, 0x70, 0x61, 0x64, 0x05, 0x6c,
  14. 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x00, 0x01};
  15. auto payload_chunk = Chunk(payload, sizeof(payload));
  16. auto src = make_address("10.0.31.124");
  17. auto dst = make_address("224.0.0.251");
  18. using State = ip::State<void, ip::Opts::calculate_checksum>;
  19. using Context = typename State::Context;
  20. using Packet = typename State::Packet;
  21. TEST_CASE("send", "[ip]") {
  22. char buff[sizeof(example_raw)];
  23. auto buff_chunk = Chunk(buff, sizeof(example_raw));
  24. auto ctx = Context{src, dst, 1, ip::Proto::udp, 0, 0, nullptr};
  25. auto state = State();
  26. state.init(0x82DD);
  27. SECTION("no enough space") {
  28. auto zero_chunk = Chunk(buff, 0);
  29. auto result = Packet::send(state, ctx, zero_chunk, payload_chunk);
  30. REQUIRE(!result);
  31. CHECK(result.state() == &state);
  32. CHECK(result.error_code() == (uint32_t)Errc::no_enough_space);
  33. }
  34. auto result = Packet::send(state, ctx, buff_chunk, payload_chunk);
  35. REQUIRE(result);
  36. auto res_buff = result.consumed();
  37. CHECK((void *)res_buff.data() == (void *)buff);
  38. CHECK(res_buff.size() == example_chunk.size());
  39. CHECK(res_buff == example_chunk);
  40. }
  41. TEST_CASE("send (no checksum)", "[ip]") {
  42. using State = ip::State<void>;
  43. using Context = typename State::Context;
  44. using Packet = typename State::Packet;
  45. uint8_t example_modified[sizeof(example_raw)];
  46. memcpy(example_modified, example_raw, sizeof(example_raw));
  47. auto example_chunk = Chunk(example_modified, sizeof(example_modified));
  48. /* zero checksum */
  49. example_modified[10] = 0;
  50. example_modified[11] = 0;
  51. char buff[sizeof(example_raw)];
  52. auto buff_chunk = Chunk(buff, sizeof(example_raw));
  53. auto ctx = Context{src, dst, 1, ip::Proto::udp, 0, 0, nullptr};
  54. auto state = State();
  55. state.init(0x82DD);
  56. auto result = Packet::send(state, ctx, buff_chunk, payload_chunk);
  57. REQUIRE(result);
  58. auto res_buff = result.consumed();
  59. CHECK((void *)res_buff.data() == (void *)buff);
  60. CHECK(res_buff.size() == example_chunk.size());
  61. CHECK(res_buff == example_chunk);
  62. }
  63. TEST_CASE("recv/success", "[ip]") {
  64. auto state = State();
  65. auto result = state.recv(example_chunk, nullptr);
  66. CHECK(result);
  67. CHECK(result.consumed() == example_chunk);
  68. CHECK(result.state() == &state);
  69. auto &packet = state.get_parsed();
  70. CHECK(packet.ver == 4);
  71. CHECK(packet.header_length == 5);
  72. CHECK(packet.tos == 0);
  73. CHECK(packet.length == example_chunk.size());
  74. CHECK(packet.id == 0x82DE);
  75. CHECK(packet.flag == 0);
  76. CHECK(packet.frag_offset == 0);
  77. CHECK(packet.ttl == 1);
  78. CHECK(packet.proto == (uint8_t)ip::Proto::udp);
  79. CHECK(packet.checksum == 0x2C60);
  80. CHECK(packet.source == src);
  81. CHECK(packet.destination == dst);
  82. CHECK(packet.options == Chunk(example_raw, 0));
  83. CHECK(packet.payload == payload_chunk);
  84. CHECK(packet.packet_start == example_chunk.data());
  85. }
  86. TEST_CASE("recv/incomplete", "[ip]") {
  87. auto state = State();
  88. auto empty_chunk = Chunk(example_raw, 0);
  89. SECTION("less then header sz") {
  90. auto result = state.recv(Chunk(example_raw, 18), nullptr);
  91. CHECK(result);
  92. CHECK(result.consumed() == empty_chunk);
  93. CHECK(result.demand() == 2);
  94. }
  95. SECTION("incomplete payload") {
  96. auto chunk = Chunk(example_raw, example_chunk.size() - 1);
  97. auto result = state.recv(chunk, nullptr);
  98. CHECK(result);
  99. CHECK(result.consumed() == empty_chunk);
  100. CHECK(result.demand() == 1);
  101. }
  102. }
  103. TEST_CASE("recv/options", "[ip]") {
  104. uint8_t example_raw[]{
  105. 0x46, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x01, 0x11, 0x84, 0x8c,
  106. 0x0a, 0x00, 0x1f, 0x7c, 0xe0, 0x00, 0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9,
  107. };
  108. auto example_chunk = Chunk(example_raw, sizeof(example_raw));
  109. auto state = State();
  110. SECTION("success") {
  111. auto result = state.recv(example_chunk, nullptr);
  112. CHECK(result);
  113. CHECK(result.consumed() == example_chunk);
  114. auto &packet = state.get_parsed();
  115. CHECK(packet.ver == 4);
  116. CHECK(packet.header_length == 6);
  117. CHECK(packet.options == Chunk(example_raw + 20, 4));
  118. }
  119. SECTION("incomplete") {
  120. auto chunk = Chunk(example_raw, 21);
  121. auto result = state.recv(chunk, nullptr);
  122. auto empty_chunk = Chunk(example_raw, 0);
  123. CHECK(result);
  124. CHECK(result.consumed() == empty_chunk);
  125. CHECK(result.demand() == 3);
  126. }
  127. }
  128. TEST_CASE("recv/failures", "[ip]") {
  129. using State = ip::State<void, ip::Opts::validate_checksum>;
  130. auto state = State();
  131. uint8_t raw[sizeof(example_raw)];
  132. memcpy(raw, example_raw, sizeof(raw));
  133. auto raw_chunk = Chunk(raw, sizeof(raw));
  134. SECTION("wrong checksum") {
  135. ++raw[20 - 9];
  136. auto result = state.recv(raw_chunk, nullptr);
  137. CHECK(!result);
  138. CHECK(result.state() == &state);
  139. CHECK(result.error_code() == (uint32_t)Errc::ip_checksum_mismatch);
  140. CHECK(result.consumed() == raw_chunk);
  141. SECTION("ignore checksum") {
  142. using State = ip::State<void>;
  143. auto state = State();
  144. auto result = state.recv(raw_chunk, nullptr);
  145. CHECK(result);
  146. }
  147. }
  148. SECTION("wrong version") {
  149. raw[0] = 0;
  150. auto result = state.recv(raw_chunk, nullptr);
  151. CHECK(!result);
  152. CHECK(result.state() == &state);
  153. CHECK(result.error_code() == (uint32_t)Errc::ip_unsupported_version);
  154. CHECK(result.consumed() == Chunk(raw, 1));
  155. }
  156. SECTION("wrong header_sz") {
  157. raw[0] = 0x41;
  158. auto result = state.recv(raw_chunk, nullptr);
  159. CHECK(!result);
  160. CHECK(result.state() == &state);
  161. CHECK(result.error_code() == (uint32_t)Errc::ip_header_missized);
  162. CHECK(result.consumed() == Chunk(raw, 1));
  163. }
  164. }
  165. TEST_CASE("send & recv with checksum", "[ip]") {
  166. using O = ip::Opts;
  167. constexpr auto opts = O::calculate_checksum | O::validate_checksum;
  168. using State = ip::State<void, opts>;
  169. using Context = typename State::Context;
  170. using Packet = typename State::Packet;
  171. char buff[sizeof(example_raw)];
  172. auto buff_chunk = Chunk(buff, sizeof(example_raw));
  173. auto ctx = Context{src, dst, 1, ip::Proto::udp, 0, 0, nullptr};
  174. auto state = State();
  175. state.init(0x82DD);
  176. auto result = Packet::send(state, ctx, buff_chunk, payload_chunk);
  177. REQUIRE(result);
  178. auto res_buff = result.consumed();
  179. result = state.recv(res_buff, nullptr);
  180. CHECK(result);
  181. CHECK(result.state() == &state);
  182. CHECK(!result.demand());
  183. }