test_rxerr.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2016-2019 UAVCAN Team
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Contributors: https://github.com/UAVCAN/libcanard/contributors
  23. */
  24. #include <catch.hpp>
  25. #include <canard.h>
  26. //Independent implementation of ID layout from https://uavcan.org/Specification/4._CAN_bus_transport_layer/
  27. #define CONSTRUCT_PRIO(prio) (((prio) & 0x1F) << 24)
  28. #define CONSTRUCT_MTID(mtid) (((mtid) & 0xFFFF) << 8)
  29. #define CONSTRUCT_SID(sid) (((sid) & 0x7F))
  30. #define CONSTRUCT_DISC(disc) (((disc) & 0x3FFF) << 10)
  31. #define CONSTRUCT_DISC_MTID(mtid) (((mtid) & 0x3) << 8)
  32. #define CONSTRUCT_STID(stid) (((stid) & 0xFF) << 16)
  33. #define CONSTRUCT_DID(did) (((did) & 0x7F) << 8)
  34. #define CONSTRUCT_REQUEST(request) (((request) & 0x1) << 15)
  35. #define CONSTRUCT_SNM_BIT (1 << 7)
  36. #define CONSTRUCT_ANON_MSG_ID(prio, disc, mtid, sid) (CONSTRUCT_PRIO(prio) | \
  37. CONSTRUCT_DISC_MTID(mtid) | \
  38. CONSTRUCT_DISC(disc) | \
  39. CONSTRUCT_SID(sid) \
  40. CANARD_CAN_FRAME_EFF)
  41. #define CONSTRUCT_MSG_ID(prio, mtid, sid) (CONSTRUCT_PRIO(prio) | \
  42. CONSTRUCT_MTID(mtid) | \
  43. CONSTRUCT_SID(sid) | \
  44. CANARD_CAN_FRAME_EFF)
  45. #define CONSTRUCT_SVC_ID(prio, stid, request, did, sid) (CONSTRUCT_PRIO(prio) | \
  46. CONSTRUCT_STID(stid) | \
  47. CONSTRUCT_REQUEST(request) | \
  48. CONSTRUCT_DID(did) | \
  49. CONSTRUCT_SID(sid) | \
  50. CONSTRUCT_SNM_BIT | \
  51. CANARD_CAN_FRAME_EFF)
  52. #define CONSTRUCT_START(start) (((start) & 0x1) << 7)
  53. #define CONSTRUCT_END(end) (((end) & 0x1) << 6)
  54. #define CONSTRUCT_TOGGLE(toggle) (((toggle) & 0x1) << 5)
  55. #define CONSTRUCT_TID(tid) (((tid) & 0x1F))
  56. #define CONSTRUCT_TAIL_BYTE(start, end, toggle, tid) (CONSTRUCT_START(start) | \
  57. CONSTRUCT_END(end) | \
  58. CONSTRUCT_TOGGLE(toggle) | \
  59. CONSTRUCT_TID(tid))
  60. static bool g_should_accept = true;
  61. /**
  62. * This callback is invoked by the library when a new message or request or response is received.
  63. */
  64. static void onTransferReceived(CanardInstance* ins,
  65. CanardRxTransfer* transfer)
  66. {
  67. (void)ins;
  68. (void)transfer;
  69. }
  70. static bool shouldAcceptTransfer(const CanardInstance* ins,
  71. uint64_t* out_data_type_signature,
  72. uint16_t data_type_id,
  73. CanardTransferType transfer_type,
  74. uint8_t source_node_id)
  75. {
  76. (void)ins;
  77. (void)out_data_type_signature;
  78. (void)data_type_id;
  79. (void)transfer_type;
  80. (void)source_node_id;
  81. return g_should_accept;
  82. }
  83. TEST_CASE("canardHandleRxFrame incompatible packet handling, Correctness")
  84. {
  85. uint8_t canard_memory_pool[1024];
  86. CanardInstance canard;
  87. CanardCANFrame frame;
  88. int16_t err;
  89. //Setup frame data to be single frame transfer
  90. frame.data[0] = CONSTRUCT_TAIL_BYTE(1, 1, 0, 0);
  91. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  92. onTransferReceived, shouldAcceptTransfer, &canard);
  93. g_should_accept = true;
  94. //Frame with good RTR/ERR/data_len bad EFF
  95. frame.id = 0;
  96. frame.data_len = 1;
  97. err = canardHandleRxFrame(&canard, &frame, 1);
  98. REQUIRE(-CANARD_ERROR_RX_INCOMPATIBLE_PACKET == err);
  99. //Frame with good EFF/ERR/data_len, bad RTR
  100. frame.id = 0 | CANARD_CAN_FRAME_RTR | CANARD_CAN_FRAME_EFF;
  101. frame.data_len = 1;
  102. err = canardHandleRxFrame(&canard, &frame, 1);
  103. REQUIRE(-CANARD_ERROR_RX_INCOMPATIBLE_PACKET == err);
  104. //Frame with good EFF/RTR/data_len, bad ERR
  105. frame.id = 0 | CANARD_CAN_FRAME_ERR | CANARD_CAN_FRAME_EFF;
  106. frame.data_len = 1;
  107. err = canardHandleRxFrame(&canard, &frame, 1);
  108. REQUIRE(-CANARD_ERROR_RX_INCOMPATIBLE_PACKET == err);
  109. //Frame with good EFF/RTR/ERR, bad data_len
  110. frame.id = 0 | CANARD_CAN_FRAME_EFF;
  111. frame.data_len = 0;
  112. err = canardHandleRxFrame(&canard, &frame, 1);
  113. REQUIRE(-CANARD_ERROR_RX_INCOMPATIBLE_PACKET == err);
  114. //Frame with good EFF/RTR/ERR/data_len
  115. frame.id = 0 | CANARD_CAN_FRAME_EFF;
  116. frame.data_len = 1;
  117. err = canardHandleRxFrame(&canard, &frame, 1);
  118. REQUIRE(CANARD_OK == err);
  119. }
  120. TEST_CASE("canardHandleRxFrame wrong address handling, Correctness")
  121. {
  122. uint8_t canard_memory_pool[1024];
  123. CanardInstance canard;
  124. CanardCANFrame frame;
  125. int16_t err;
  126. //Setup frame data to be single frame transfer
  127. frame.data[0] = CONSTRUCT_TAIL_BYTE(1, 1, 0, 0);
  128. //Open canard to accept all transfers with a node ID of 20
  129. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  130. onTransferReceived, shouldAcceptTransfer, &canard);
  131. canardSetLocalNodeID(&canard, 20);
  132. g_should_accept = true;
  133. //Send package with ID 24, should not be wanted
  134. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 24, 0);
  135. frame.data_len = 1;
  136. err = canardHandleRxFrame(&canard, &frame, 1);
  137. REQUIRE(-CANARD_ERROR_RX_WRONG_ADDRESS == err);
  138. //Send package with ID 20, should be OK
  139. frame.id = 0 | CANARD_CAN_FRAME_EFF; //Set EFF
  140. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  141. frame.data_len = 1;
  142. err = canardHandleRxFrame(&canard, &frame, 1);
  143. REQUIRE(CANARD_OK == err);
  144. }
  145. TEST_CASE("canardHandleRxFrame shouldAccept handling, Correctness")
  146. {
  147. uint8_t canard_memory_pool[1024];
  148. CanardInstance canard;
  149. CanardCANFrame frame;
  150. int16_t err;
  151. //Setup frame data to be single frame transfer
  152. frame.data[0] = CONSTRUCT_TAIL_BYTE(1, 1, 0, 0);
  153. //Open canard to accept all transfers with a node ID of 20
  154. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  155. onTransferReceived, shouldAcceptTransfer, &canard);
  156. canardSetLocalNodeID(&canard, 20);
  157. //Send packet, don't accept
  158. g_should_accept = false;
  159. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  160. frame.data_len = 1;
  161. err = canardHandleRxFrame(&canard, &frame, 1);
  162. REQUIRE(-CANARD_ERROR_RX_NOT_WANTED == err);
  163. //Send packet, accept
  164. g_should_accept = true;
  165. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  166. frame.data_len = 1;
  167. err = canardHandleRxFrame(&canard, &frame, 1);
  168. REQUIRE(CANARD_OK == err);
  169. }
  170. TEST_CASE("canardHandleRxFrame no state handling, Correctness")
  171. {
  172. uint8_t canard_memory_pool[1024];
  173. CanardInstance canard;
  174. CanardCANFrame frame;
  175. int16_t err;
  176. g_should_accept = true;
  177. //Open canard to accept all transfers with a node ID of 20
  178. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  179. onTransferReceived, shouldAcceptTransfer, &canard);
  180. canardSetLocalNodeID(&canard, 20);
  181. //Not start or end of packet, should fail
  182. frame.data[0] = CONSTRUCT_TAIL_BYTE(0, 0, 0, 0);
  183. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  184. frame.data_len = 1;
  185. err = canardHandleRxFrame(&canard, &frame, 1);
  186. REQUIRE(-CANARD_ERROR_RX_MISSED_START == err);
  187. //End of packet, should fail
  188. frame.data[0] = CONSTRUCT_TAIL_BYTE(0, 1, 0, 0);
  189. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  190. frame.data_len = 1;
  191. err = canardHandleRxFrame(&canard, &frame, 1);
  192. REQUIRE(-CANARD_ERROR_RX_MISSED_START == err);
  193. //1 Frame packet, should pass
  194. frame.data[0] = CONSTRUCT_TAIL_BYTE(1, 1, 0, 0);
  195. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  196. frame.data_len = 1;
  197. err = canardHandleRxFrame(&canard, &frame, 1);
  198. REQUIRE(CANARD_OK == err);
  199. //Send a start packet, should pass
  200. frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  201. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  202. frame.data_len = 8; //Data length MUST be full packet
  203. err = canardHandleRxFrame(&canard, &frame, 1);
  204. REQUIRE(CANARD_OK == err);
  205. //Send a middle packet, from the same ID, but don't toggle
  206. frame.data[0] = CONSTRUCT_TAIL_BYTE(0, 0, 0, 1);
  207. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  208. frame.data_len = 1;
  209. err = canardHandleRxFrame(&canard, &frame, 1);
  210. REQUIRE(-CANARD_ERROR_RX_WRONG_TOGGLE == err);
  211. //Send a middle packet, toggle, but use wrong ID
  212. frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 2);
  213. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  214. frame.data_len = 8;
  215. err = canardHandleRxFrame(&canard, &frame, 1);
  216. REQUIRE(-CANARD_ERROR_RX_UNEXPECTED_TID == err);
  217. //Send a middle packet, toggle, and use correct ID
  218. frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 1);
  219. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  220. frame.data_len = 8;
  221. err = canardHandleRxFrame(&canard, &frame, 1);
  222. REQUIRE(CANARD_OK == err);
  223. }
  224. TEST_CASE("canardHandleRxFrame missed start handling, Correctness")
  225. {
  226. uint8_t canard_memory_pool[1024];
  227. CanardInstance canard;
  228. CanardCANFrame frame;
  229. int16_t err;
  230. g_should_accept = true;
  231. //Open canard to accept all transfers with a node ID of 20
  232. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  233. onTransferReceived, shouldAcceptTransfer, &canard);
  234. canardSetLocalNodeID(&canard, 20);
  235. //Send a start packet, should pass
  236. frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  237. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  238. frame.data_len = 8; //Data length MUST be full packet
  239. err = canardHandleRxFrame(&canard, &frame, 1);
  240. REQUIRE(CANARD_OK == err);
  241. //Send a middle packet, toggle, and use correct ID - but timeout
  242. frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 1);
  243. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  244. frame.data_len = 8;
  245. err = canardHandleRxFrame(&canard, &frame, 4000000);
  246. REQUIRE(-CANARD_ERROR_RX_MISSED_START == err);
  247. //Send a start packet, should pass
  248. frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  249. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  250. frame.data_len = 8; //Data length MUST be full packet
  251. err = canardHandleRxFrame(&canard, &frame, 1);
  252. REQUIRE(CANARD_OK == err);
  253. //Send a middle packet, toggle, and use correct ID - but timestamp 0
  254. frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 1);
  255. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  256. frame.data_len = 8;
  257. err = canardHandleRxFrame(&canard, &frame, 0);
  258. REQUIRE(-CANARD_ERROR_RX_MISSED_START == err);
  259. //Send a start packet, should pass
  260. frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  261. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  262. frame.data_len = 8; //Data length MUST be full packet
  263. err = canardHandleRxFrame(&canard, &frame, 1);
  264. REQUIRE(CANARD_OK == err);
  265. //Send a middle packet, toggle, and use an incorrect TID
  266. frame.data[7] = CONSTRUCT_TAIL_BYTE(0, 0, 1, 3);
  267. frame.data[7] = 0 | 3 | (1<<5);
  268. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  269. frame.data_len = 8;
  270. err = canardHandleRxFrame(&canard, &frame, 0);
  271. REQUIRE(-CANARD_ERROR_RX_MISSED_START == err);
  272. }
  273. TEST_CASE("canardHandleRxFrame short frame handling, Correctness")
  274. {
  275. uint8_t canard_memory_pool[1024];
  276. CanardInstance canard;
  277. CanardCANFrame frame;
  278. int16_t err;
  279. g_should_accept = true;
  280. //Open canard to accept all transfers with a node ID of 20
  281. canardInit(&canard, canard_memory_pool, sizeof(canard_memory_pool),
  282. onTransferReceived, shouldAcceptTransfer, &canard);
  283. canardSetLocalNodeID(&canard, 20);
  284. //Send a start packet which is short, should fail
  285. frame.data[1] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  286. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  287. frame.data_len = 2; //Data length MUST be full packet
  288. err = canardHandleRxFrame(&canard, &frame, 1);
  289. REQUIRE(-CANARD_ERROR_RX_SHORT_FRAME == err);
  290. //Send a start packet which is short, should fail
  291. frame.data[2] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  292. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  293. frame.data_len = 3; //Data length MUST be full packet
  294. err = canardHandleRxFrame(&canard, &frame, 1);
  295. REQUIRE(-CANARD_ERROR_RX_SHORT_FRAME == err);
  296. }
  297. TEST_CASE("canardHandleRxFrame OOM handling, Correctness")
  298. {
  299. uint8_t dummy_buf;
  300. CanardInstance canard;
  301. CanardCANFrame frame;
  302. int16_t err;
  303. g_should_accept = true;
  304. //Open canard to accept all transfers with a node ID of 20
  305. canardInit(&canard, &dummy_buf, 0,
  306. onTransferReceived, shouldAcceptTransfer, &canard);
  307. canardSetLocalNodeID(&canard, 20);
  308. //Send a start packet - we shouldn't have any memory
  309. frame.data[7] = CONSTRUCT_TAIL_BYTE(1, 0, 0, 1);
  310. frame.id = CONSTRUCT_SVC_ID(0, 0, 1, 20, 0);
  311. frame.data_len = 8; //Data length MUST be full packet
  312. err = canardHandleRxFrame(&canard, &frame, 1);
  313. REQUIRE(-CANARD_ERROR_OUT_OF_MEMORY == err);
  314. }