01-ip.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, 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, 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. }
  85. TEST_CASE("recv/incomplete", "[ip]") {
  86. auto state = State();
  87. auto empty_chunk = Chunk(example_raw, 0);
  88. SECTION("less then header sz") {
  89. auto result = state.recv(Chunk(example_raw, 18), nullptr);
  90. CHECK(result);
  91. CHECK(result.consumed() == empty_chunk);
  92. CHECK(result.demand() == 2);
  93. }
  94. SECTION("incomplete payload") {
  95. auto chunk = Chunk(example_raw, example_chunk.size() - 1);
  96. auto result = state.recv(chunk, nullptr);
  97. CHECK(result);
  98. CHECK(result.consumed() == empty_chunk);
  99. CHECK(result.demand() == 1);
  100. }
  101. }
  102. TEST_CASE("recv/options", "[ip]") {
  103. uint8_t example_raw[]{
  104. 0x46, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x01, 0x11, 0x84, 0x8c,
  105. 0x0a, 0x00, 0x1f, 0x7c, 0xe0, 0x00, 0x00, 0xfb, 0x14, 0xe9, 0x14, 0xe9,
  106. };
  107. auto example_chunk = Chunk(example_raw, sizeof(example_raw));
  108. auto state = State();
  109. SECTION("success") {
  110. auto result = state.recv(example_chunk, nullptr);
  111. CHECK(result);
  112. CHECK(result.consumed() == example_chunk);
  113. auto &packet = state.get_parsed();
  114. CHECK(packet.ver == 4);
  115. CHECK(packet.header_length == 6);
  116. CHECK(packet.options == Chunk(example_raw + 20, 4));
  117. }
  118. SECTION("incomplete") {
  119. auto chunk = Chunk(example_raw, 21);
  120. auto result = state.recv(chunk, nullptr);
  121. auto empty_chunk = Chunk(example_raw, 0);
  122. CHECK(result);
  123. CHECK(result.consumed() == empty_chunk);
  124. CHECK(result.demand() == 3);
  125. }
  126. }
  127. TEST_CASE("recv/failures", "[ip]") {
  128. using State = ip::State<void, ip::Opts::validate_checksum>;
  129. auto state = State();
  130. uint8_t raw[sizeof(example_raw)];
  131. memcpy(raw, example_raw, sizeof(raw));
  132. auto raw_chunk = Chunk(raw, sizeof(raw));
  133. SECTION("wrong checksum") {
  134. ++raw[20 - 9];
  135. auto result = state.recv(raw_chunk, nullptr);
  136. CHECK(!result);
  137. CHECK(result.state() == &state);
  138. CHECK(result.error_code() == (uint32_t)Errc::ip_checksum_mismatch);
  139. CHECK(result.consumed() == raw_chunk);
  140. SECTION("ignore checksum") {
  141. using State = ip::State<void>;
  142. auto state = State();
  143. auto result = state.recv(raw_chunk, nullptr);
  144. CHECK(result);
  145. }
  146. }
  147. SECTION("wrong version") {
  148. raw[0] = 0;
  149. auto result = state.recv(raw_chunk, nullptr);
  150. CHECK(!result);
  151. CHECK(result.state() == &state);
  152. CHECK(result.error_code() == (uint32_t)Errc::ip_unsupported_version);
  153. CHECK(result.consumed() == Chunk(raw, 1));
  154. }
  155. SECTION("wrong header_sz") {
  156. raw[0] = 0x41;
  157. auto result = state.recv(raw_chunk, nullptr);
  158. CHECK(!result);
  159. CHECK(result.state() == &state);
  160. CHECK(result.error_code() == (uint32_t)Errc::ip_header_missized);
  161. CHECK(result.consumed() == Chunk(raw, 1));
  162. }
  163. }
  164. TEST_CASE("send & recv with checksum", "[ip]") {
  165. using O = ip::Opts;
  166. constexpr auto opts = O::calculate_checksum | O::validate_checksum;
  167. using State = ip::State<void, opts>;
  168. using Context = typename State::Context;
  169. using Packet = typename State::Packet;
  170. char buff[sizeof(example_raw)];
  171. auto buff_chunk = Chunk(buff, sizeof(example_raw));
  172. auto ctx = Context{src, dst, 1, ip::Proto::udp, 0, nullptr};
  173. auto state = State();
  174. state.init(0x82DD);
  175. auto result = Packet::send(state, ctx, buff_chunk, payload_chunk);
  176. REQUIRE(result);
  177. auto res_buff = result.consumed();
  178. result = state.recv(res_buff, nullptr);
  179. CHECK(result);
  180. CHECK(result.state() == &state);
  181. CHECK(!result.demand());
  182. }