property_user_read.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #include <CUnit/CUnit.h>
  2. #include <CUnit/Basic.h>
  3. #include "mqtt_protocol.h"
  4. #include "property_mosq.h"
  5. #include "packet_mosq.h"
  6. static void generate_full_proplist(mosquitto_property **proplist)
  7. {
  8. int rc;
  9. /* This isn't a valid proplist for sending, because it contains every
  10. * property. Very useful for testing though. */
  11. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, 1);
  12. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  13. if(rc != MOSQ_ERR_SUCCESS) return;
  14. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 3600);
  15. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  16. if(rc != MOSQ_ERR_SUCCESS) return;
  17. rc = mosquitto_property_add_string(proplist, MQTT_PROP_CONTENT_TYPE, "application/json");
  18. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  19. if(rc != MOSQ_ERR_SUCCESS) return;
  20. rc = mosquitto_property_add_string(proplist, MQTT_PROP_RESPONSE_TOPIC, "response/topic");
  21. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  22. if(rc != MOSQ_ERR_SUCCESS) return;
  23. rc = mosquitto_property_add_binary(proplist, MQTT_PROP_CORRELATION_DATA, "correlation-data", strlen("correlation-data"));
  24. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  25. if(rc != MOSQ_ERR_SUCCESS) return;
  26. rc = mosquitto_property_add_varint(proplist, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, 63);
  27. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  28. if(rc != MOSQ_ERR_SUCCESS) return;
  29. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_SESSION_EXPIRY_INTERVAL, 86400);
  30. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  31. if(rc != MOSQ_ERR_SUCCESS) return;
  32. rc = mosquitto_property_add_string(proplist, MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER, "mosquitto-test");
  33. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  34. if(rc != MOSQ_ERR_SUCCESS) return;
  35. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_SERVER_KEEP_ALIVE, 180);
  36. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  37. if(rc != MOSQ_ERR_SUCCESS) return;
  38. rc = mosquitto_property_add_string(proplist, MQTT_PROP_AUTHENTICATION_METHOD, "basic");
  39. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  40. if(rc != MOSQ_ERR_SUCCESS) return;
  41. rc = mosquitto_property_add_binary(proplist, MQTT_PROP_AUTHENTICATION_DATA, "password", strlen("password"));
  42. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  43. if(rc != MOSQ_ERR_SUCCESS) return;
  44. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, 1);
  45. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  46. if(rc != MOSQ_ERR_SUCCESS) return;
  47. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_WILL_DELAY_INTERVAL, 1800);
  48. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  49. if(rc != MOSQ_ERR_SUCCESS) return;
  50. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_REQUEST_RESPONSE_INFORMATION, 1);
  51. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  52. if(rc != MOSQ_ERR_SUCCESS) return;
  53. rc = mosquitto_property_add_string(proplist, MQTT_PROP_RESPONSE_INFORMATION, "response");
  54. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  55. if(rc != MOSQ_ERR_SUCCESS) return;
  56. rc = mosquitto_property_add_string(proplist, MQTT_PROP_SERVER_REFERENCE, "localhost");
  57. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  58. if(rc != MOSQ_ERR_SUCCESS) return;
  59. rc = mosquitto_property_add_string(proplist, MQTT_PROP_REASON_STRING, "reason");
  60. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  61. if(rc != MOSQ_ERR_SUCCESS) return;
  62. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_RECEIVE_MAXIMUM, 1024);
  63. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  64. if(rc != MOSQ_ERR_SUCCESS) return;
  65. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, 64);
  66. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  67. if(rc != MOSQ_ERR_SUCCESS) return;
  68. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_TOPIC_ALIAS, 15);
  69. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  70. if(rc != MOSQ_ERR_SUCCESS) return;
  71. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_MAXIMUM_QOS, 0);
  72. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  73. if(rc != MOSQ_ERR_SUCCESS) return;
  74. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_RETAIN_AVAILABLE, 0);
  75. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  76. if(rc != MOSQ_ERR_SUCCESS) return;
  77. rc = mosquitto_property_add_string_pair(proplist, MQTT_PROP_USER_PROPERTY, "user-agent", "mosquitto/test");
  78. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  79. if(rc != MOSQ_ERR_SUCCESS) return;
  80. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_MAXIMUM_PACKET_SIZE, 200000000);
  81. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  82. if(rc != MOSQ_ERR_SUCCESS) return;
  83. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_WILDCARD_SUB_AVAILABLE, 0);
  84. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  85. if(rc != MOSQ_ERR_SUCCESS) return;
  86. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE, 0);
  87. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  88. if(rc != MOSQ_ERR_SUCCESS) return;
  89. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
  90. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  91. }
  92. static void generate_partial_proplist(mosquitto_property **proplist)
  93. {
  94. int rc;
  95. // BYTE MISSING: MQTT_PROP_PAYLOAD_FORMAT_INDICATOR
  96. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 3600);
  97. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  98. // STRING MISSING: MQTT_PROP_CONTENT_TYPE
  99. rc = mosquitto_property_add_string(proplist, MQTT_PROP_RESPONSE_TOPIC, "response/topic");
  100. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  101. // BINARY MISSING: MQTT_PROP_CORRELATION_DATA
  102. // VARINT MISSING: MQTT_PROP_SUBSCRIPTION_IDENTIFIER
  103. // INT32 MISSING: MQTT_PROP_SESSION_EXPIRY_INTERVAL
  104. rc = mosquitto_property_add_string(proplist, MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER, "mosquitto-test");
  105. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  106. // INT16 MISSING: MQTT_PROP_SERVER_KEEP_ALIVE
  107. rc = mosquitto_property_add_string(proplist, MQTT_PROP_AUTHENTICATION_METHOD, "basic");
  108. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  109. rc = mosquitto_property_add_binary(proplist, MQTT_PROP_AUTHENTICATION_DATA, "password", strlen("password"));
  110. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  111. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, 1);
  112. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  113. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_WILL_DELAY_INTERVAL, 1800);
  114. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  115. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_REQUEST_RESPONSE_INFORMATION, 1);
  116. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  117. rc = mosquitto_property_add_string(proplist, MQTT_PROP_RESPONSE_INFORMATION, "response");
  118. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  119. rc = mosquitto_property_add_string(proplist, MQTT_PROP_SERVER_REFERENCE, "localhost");
  120. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  121. rc = mosquitto_property_add_string(proplist, MQTT_PROP_REASON_STRING, "reason");
  122. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  123. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_RECEIVE_MAXIMUM, 1024);
  124. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  125. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, 64);
  126. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  127. rc = mosquitto_property_add_int16(proplist, MQTT_PROP_TOPIC_ALIAS, 15);
  128. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  129. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_MAXIMUM_QOS, 0);
  130. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  131. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_RETAIN_AVAILABLE, 0);
  132. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  133. // STRING PAIR MISSING: MQTT_PROP_USER_PROPERTY
  134. rc = mosquitto_property_add_int32(proplist, MQTT_PROP_MAXIMUM_PACKET_SIZE, 200000000);
  135. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  136. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_WILDCARD_SUB_AVAILABLE, 0);
  137. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  138. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE, 0);
  139. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  140. rc = mosquitto_property_add_byte(proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
  141. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  142. }
  143. /* ========================================================================
  144. * SINGLE READ
  145. * ======================================================================== */
  146. static void read_byte_helper(const mosquitto_property *proplist, int identifier, uint8_t expected_value)
  147. {
  148. const mosquitto_property *prop;
  149. uint8_t value;
  150. prop = mosquitto_property_read_byte(proplist, identifier, &value, false);
  151. CU_ASSERT_PTR_NOT_NULL(prop);
  152. CU_ASSERT_EQUAL(value, expected_value);
  153. }
  154. static void read_int16_helper(const mosquitto_property *proplist, int identifier, uint16_t expected_value)
  155. {
  156. const mosquitto_property *prop;
  157. uint16_t value;
  158. prop = mosquitto_property_read_int16(proplist, identifier, &value, false);
  159. CU_ASSERT_PTR_NOT_NULL(prop);
  160. CU_ASSERT_EQUAL(value, expected_value);
  161. }
  162. static void read_int32_helper(const mosquitto_property *proplist, int identifier, uint32_t expected_value)
  163. {
  164. const mosquitto_property *prop;
  165. uint32_t value;
  166. prop = mosquitto_property_read_int32(proplist, identifier, &value, false);
  167. CU_ASSERT_PTR_NOT_NULL(prop);
  168. CU_ASSERT_EQUAL(value, expected_value);
  169. }
  170. static void read_varint_helper(const mosquitto_property *proplist, int identifier, uint32_t expected_value)
  171. {
  172. const mosquitto_property *prop;
  173. uint32_t value;
  174. prop = mosquitto_property_read_varint(proplist, identifier, &value, false);
  175. CU_ASSERT_PTR_NOT_NULL(prop);
  176. CU_ASSERT_EQUAL(value, expected_value);
  177. }
  178. static void read_binary_helper(const mosquitto_property *proplist, int identifier, void *expected_value, uint16_t expected_length)
  179. {
  180. const mosquitto_property *prop;
  181. void *value = NULL;
  182. uint16_t length;
  183. prop = mosquitto_property_read_binary(proplist, identifier, &value, &length, false);
  184. CU_ASSERT_PTR_NOT_NULL(prop);
  185. CU_ASSERT_EQUAL(length, expected_length);
  186. CU_ASSERT_PTR_NOT_NULL(value);
  187. if(value){
  188. CU_ASSERT_NSTRING_EQUAL(value, expected_value, expected_length);
  189. }
  190. free(value);
  191. }
  192. static void read_string_helper(const mosquitto_property *proplist, int identifier, char *expected_value)
  193. {
  194. const mosquitto_property *prop;
  195. char *value = NULL;
  196. prop = mosquitto_property_read_string(proplist, identifier, &value, false);
  197. CU_ASSERT_PTR_NOT_NULL(prop);
  198. CU_ASSERT_PTR_NOT_NULL(value);
  199. if(value){
  200. CU_ASSERT_STRING_EQUAL(value, expected_value);
  201. }
  202. free(value);
  203. }
  204. static void read_string_pair_helper(const mosquitto_property *proplist, int identifier, char *expected_key, char *expected_value)
  205. {
  206. const mosquitto_property *prop;
  207. char *key = NULL, *value = NULL;
  208. prop = mosquitto_property_read_string_pair(proplist, identifier, &key, &value, false);
  209. CU_ASSERT_PTR_NOT_NULL(prop);
  210. CU_ASSERT_PTR_NOT_NULL(key);
  211. if(key){
  212. CU_ASSERT_STRING_EQUAL(key, expected_key);
  213. }
  214. CU_ASSERT_PTR_NOT_NULL(value);
  215. if(value){
  216. CU_ASSERT_STRING_EQUAL(value, expected_value);
  217. }
  218. free(key);
  219. free(value);
  220. }
  221. static void TEST_read_single_byte(void)
  222. {
  223. int rc;
  224. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  225. generate_full_proplist(&proplist);
  226. if(!proplist) return;
  227. read_byte_helper(proplist, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, 1);
  228. read_byte_helper(proplist, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, 1);
  229. read_byte_helper(proplist, MQTT_PROP_REQUEST_RESPONSE_INFORMATION, 1);
  230. read_byte_helper(proplist, MQTT_PROP_MAXIMUM_QOS, 0);
  231. read_byte_helper(proplist, MQTT_PROP_RETAIN_AVAILABLE, 0);
  232. read_byte_helper(proplist, MQTT_PROP_WILDCARD_SUB_AVAILABLE, 0);
  233. read_byte_helper(proplist, MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE, 0);
  234. read_byte_helper(proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
  235. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  236. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  237. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  238. read_byte_helper(proplist_copy, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, 1);
  239. read_byte_helper(proplist_copy, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, 1);
  240. read_byte_helper(proplist_copy, MQTT_PROP_REQUEST_RESPONSE_INFORMATION, 1);
  241. read_byte_helper(proplist_copy, MQTT_PROP_MAXIMUM_QOS, 0);
  242. read_byte_helper(proplist_copy, MQTT_PROP_RETAIN_AVAILABLE, 0);
  243. read_byte_helper(proplist_copy, MQTT_PROP_WILDCARD_SUB_AVAILABLE, 0);
  244. read_byte_helper(proplist_copy, MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE, 0);
  245. read_byte_helper(proplist_copy, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
  246. mosquitto_property_free_all(&proplist);
  247. mosquitto_property_free_all(&proplist_copy);
  248. }
  249. static void TEST_read_single_int16(void)
  250. {
  251. int rc;
  252. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  253. generate_full_proplist(&proplist);
  254. if(!proplist) return;
  255. read_int16_helper(proplist, MQTT_PROP_SERVER_KEEP_ALIVE, 180);
  256. read_int16_helper(proplist, MQTT_PROP_RECEIVE_MAXIMUM, 1024);
  257. read_int16_helper(proplist, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, 64);
  258. read_int16_helper(proplist, MQTT_PROP_TOPIC_ALIAS, 15);
  259. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  260. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  261. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  262. read_int16_helper(proplist_copy, MQTT_PROP_SERVER_KEEP_ALIVE, 180);
  263. read_int16_helper(proplist_copy, MQTT_PROP_RECEIVE_MAXIMUM, 1024);
  264. read_int16_helper(proplist_copy, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, 64);
  265. read_int16_helper(proplist_copy, MQTT_PROP_TOPIC_ALIAS, 15);
  266. mosquitto_property_free_all(&proplist);
  267. mosquitto_property_free_all(&proplist_copy);
  268. }
  269. static void TEST_read_single_int32(void)
  270. {
  271. int rc;
  272. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  273. generate_full_proplist(&proplist);
  274. if(!proplist) return;
  275. read_int32_helper(proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 3600);
  276. read_int32_helper(proplist, MQTT_PROP_SESSION_EXPIRY_INTERVAL, 86400);
  277. read_int32_helper(proplist, MQTT_PROP_WILL_DELAY_INTERVAL, 1800);
  278. read_int32_helper(proplist, MQTT_PROP_MAXIMUM_PACKET_SIZE, 200000000);
  279. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  280. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  281. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  282. read_int32_helper(proplist_copy, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 3600);
  283. read_int32_helper(proplist_copy, MQTT_PROP_SESSION_EXPIRY_INTERVAL, 86400);
  284. read_int32_helper(proplist_copy, MQTT_PROP_WILL_DELAY_INTERVAL, 1800);
  285. read_int32_helper(proplist_copy, MQTT_PROP_MAXIMUM_PACKET_SIZE, 200000000);
  286. mosquitto_property_free_all(&proplist);
  287. mosquitto_property_free_all(&proplist_copy);
  288. }
  289. static void TEST_read_single_varint(void)
  290. {
  291. int rc;
  292. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  293. generate_full_proplist(&proplist);
  294. if(!proplist) return;
  295. read_varint_helper(proplist, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, 63);
  296. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  297. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  298. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  299. read_varint_helper(proplist_copy, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, 63);
  300. mosquitto_property_free_all(&proplist);
  301. mosquitto_property_free_all(&proplist_copy);
  302. }
  303. static void TEST_read_single_binary(void)
  304. {
  305. int rc;
  306. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  307. generate_full_proplist(&proplist);
  308. if(!proplist) return;
  309. read_binary_helper(proplist, MQTT_PROP_CORRELATION_DATA, "correlation-data", strlen("correlation-data"));
  310. read_binary_helper(proplist, MQTT_PROP_AUTHENTICATION_DATA, "password", strlen("password"));
  311. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  312. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  313. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  314. if(proplist_copy){
  315. read_binary_helper(proplist_copy, MQTT_PROP_CORRELATION_DATA, "correlation-data", strlen("correlation-data"));
  316. read_binary_helper(proplist_copy, MQTT_PROP_AUTHENTICATION_DATA, "password", strlen("password"));
  317. }
  318. mosquitto_property_free_all(&proplist);
  319. mosquitto_property_free_all(&proplist_copy);
  320. }
  321. static void TEST_read_single_string(void)
  322. {
  323. int rc;
  324. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  325. generate_full_proplist(&proplist);
  326. if(!proplist) return;
  327. read_string_helper(proplist, MQTT_PROP_CONTENT_TYPE, "application/json");
  328. read_string_helper(proplist, MQTT_PROP_RESPONSE_TOPIC, "response/topic");
  329. read_string_helper(proplist, MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER, "mosquitto-test");
  330. read_string_helper(proplist, MQTT_PROP_AUTHENTICATION_METHOD, "basic");
  331. read_string_helper(proplist, MQTT_PROP_RESPONSE_INFORMATION, "response");
  332. read_string_helper(proplist, MQTT_PROP_SERVER_REFERENCE, "localhost");
  333. read_string_helper(proplist, MQTT_PROP_REASON_STRING, "reason");
  334. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  335. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  336. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  337. if(proplist_copy){
  338. read_string_helper(proplist_copy, MQTT_PROP_CONTENT_TYPE, "application/json");
  339. read_string_helper(proplist_copy, MQTT_PROP_RESPONSE_TOPIC, "response/topic");
  340. read_string_helper(proplist_copy, MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER, "mosquitto-test");
  341. read_string_helper(proplist_copy, MQTT_PROP_AUTHENTICATION_METHOD, "basic");
  342. read_string_helper(proplist_copy, MQTT_PROP_RESPONSE_INFORMATION, "response");
  343. read_string_helper(proplist_copy, MQTT_PROP_SERVER_REFERENCE, "localhost");
  344. read_string_helper(proplist_copy, MQTT_PROP_REASON_STRING, "reason");
  345. }
  346. mosquitto_property_free_all(&proplist);
  347. mosquitto_property_free_all(&proplist_copy);
  348. }
  349. static void TEST_read_single_string_pair(void)
  350. {
  351. int rc;
  352. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  353. generate_full_proplist(&proplist);
  354. if(!proplist) return;
  355. read_string_pair_helper(proplist, MQTT_PROP_USER_PROPERTY, "user-agent", "mosquitto/test");
  356. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  357. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  358. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  359. if(proplist_copy){
  360. read_string_pair_helper(proplist_copy, MQTT_PROP_USER_PROPERTY, "user-agent", "mosquitto/test");
  361. }
  362. mosquitto_property_free_all(&proplist);
  363. mosquitto_property_free_all(&proplist_copy);
  364. }
  365. /* ========================================================================
  366. * MISSING READ
  367. * ======================================================================== */
  368. static void missing_read_helper(mosquitto_property *proplist)
  369. {
  370. const mosquitto_property *prop;
  371. uint8_t byte_value;
  372. uint16_t int16_value;
  373. uint32_t int32_value;
  374. char *key, *value;
  375. uint16_t length;
  376. /* MISSING */
  377. prop = mosquitto_property_read_byte(proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, &byte_value, false);
  378. CU_ASSERT_PTR_NULL(prop);
  379. /* NOT MISSING */
  380. prop = mosquitto_property_read_int32(proplist, MQTT_PROP_WILL_DELAY_INTERVAL, &int32_value, false);
  381. CU_ASSERT_PTR_NOT_NULL(prop);
  382. CU_ASSERT_EQUAL(int32_value, 1800);
  383. /* MISSING */
  384. value = NULL;
  385. prop = mosquitto_property_read_string(proplist, MQTT_PROP_CONTENT_TYPE, &value, false);
  386. CU_ASSERT_PTR_NULL(prop);
  387. /* NOT MISSING */
  388. value = NULL;
  389. prop = mosquitto_property_read_string(proplist, MQTT_PROP_RESPONSE_TOPIC, &value, false);
  390. CU_ASSERT_PTR_NOT_NULL(prop);
  391. CU_ASSERT_PTR_NOT_NULL(value);
  392. if(value){
  393. CU_ASSERT_STRING_EQUAL(value, "response/topic");
  394. free(value);
  395. }
  396. /* MISSING */
  397. prop = mosquitto_property_read_binary(proplist, MQTT_PROP_CORRELATION_DATA, (void **)&value, &length, false);
  398. CU_ASSERT_PTR_NULL(prop);
  399. /* NOT MISSING */
  400. prop = mosquitto_property_read_byte(proplist, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, &byte_value, false);
  401. CU_ASSERT_PTR_NOT_NULL(prop);
  402. CU_ASSERT_EQUAL(byte_value, 1);
  403. /* MISSING */
  404. prop = mosquitto_property_read_varint(proplist, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, &int32_value, false);
  405. CU_ASSERT_PTR_NULL(prop);
  406. /* NOT MISSING */
  407. value = NULL;
  408. prop = mosquitto_property_read_string(proplist, MQTT_PROP_SERVER_REFERENCE, &value, false);
  409. CU_ASSERT_PTR_NOT_NULL(prop);
  410. CU_ASSERT_PTR_NOT_NULL(value);
  411. if(value){
  412. CU_ASSERT_STRING_EQUAL(value, "localhost");
  413. free(value);
  414. }
  415. /* MISSING */
  416. prop = mosquitto_property_read_int32(proplist, MQTT_PROP_SESSION_EXPIRY_INTERVAL, &int32_value, false);
  417. CU_ASSERT_PTR_NULL(prop);
  418. /* NOT MISSING */
  419. value = NULL;
  420. prop = mosquitto_property_read_binary(proplist, MQTT_PROP_AUTHENTICATION_DATA, (void **)&value, &length, false);
  421. CU_ASSERT_PTR_NOT_NULL(prop);
  422. CU_ASSERT_PTR_NOT_NULL(value);
  423. if(value){
  424. CU_ASSERT_NSTRING_EQUAL(value, "password", strlen("password"));
  425. CU_ASSERT_EQUAL(length, strlen("password"));
  426. free(value);
  427. }
  428. /* MISSING */
  429. prop = mosquitto_property_read_int16(proplist, MQTT_PROP_SERVER_KEEP_ALIVE, &int16_value, false);
  430. CU_ASSERT_PTR_NULL(prop);
  431. /* NOT MISSING */
  432. prop = mosquitto_property_read_int16(proplist, MQTT_PROP_RECEIVE_MAXIMUM, &int16_value, false);
  433. CU_ASSERT_PTR_NOT_NULL(prop);
  434. CU_ASSERT_EQUAL(int16_value, 1024);
  435. /* MISSING */
  436. prop = mosquitto_property_read_string_pair(proplist, MQTT_PROP_USER_PROPERTY, &key, &value, false);
  437. CU_ASSERT_PTR_NULL(prop);
  438. }
  439. static void TEST_read_missing(void)
  440. {
  441. mosquitto_property *proplist = NULL, *proplist_copy = NULL;
  442. int rc;
  443. generate_partial_proplist(&proplist);
  444. if(!proplist) return;
  445. missing_read_helper(proplist);
  446. rc = mosquitto_property_copy_all(&proplist_copy, proplist);
  447. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  448. CU_ASSERT_PTR_NOT_NULL(proplist_copy);
  449. if(proplist_copy){
  450. missing_read_helper(proplist_copy);
  451. }
  452. mosquitto_property_free_all(&proplist);
  453. mosquitto_property_free_all(&proplist_copy);
  454. }
  455. /* ========================================================================
  456. * STRING TO PROPERTY INFO
  457. * ======================================================================== */
  458. static void string_to_property_info_helper(const char *str, int rc_expected, int identifier_expected, int type_expected)
  459. {
  460. int rc;
  461. int identifier, type;
  462. rc = mosquitto_string_to_property_info(str, &identifier, &type);
  463. CU_ASSERT_EQUAL(rc, rc_expected);
  464. if(rc == MOSQ_ERR_SUCCESS){
  465. CU_ASSERT_EQUAL(identifier, identifier_expected);
  466. CU_ASSERT_EQUAL(type, type_expected);
  467. }
  468. }
  469. static void TEST_string_to_property_info(void)
  470. {
  471. string_to_property_info_helper("payload-format-indicator", MOSQ_ERR_SUCCESS, MQTT_PROP_PAYLOAD_FORMAT_INDICATOR, MQTT_PROP_TYPE_BYTE);
  472. string_to_property_info_helper("message-expiry-interval", MOSQ_ERR_SUCCESS, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, MQTT_PROP_TYPE_INT32);
  473. string_to_property_info_helper("content-type", MOSQ_ERR_SUCCESS, MQTT_PROP_CONTENT_TYPE, MQTT_PROP_TYPE_STRING);
  474. string_to_property_info_helper("response-topic", MOSQ_ERR_SUCCESS, MQTT_PROP_RESPONSE_TOPIC, MQTT_PROP_TYPE_STRING);
  475. string_to_property_info_helper("correlation-data", MOSQ_ERR_SUCCESS, MQTT_PROP_CORRELATION_DATA, MQTT_PROP_TYPE_BINARY);
  476. string_to_property_info_helper("subscription-identifier", MOSQ_ERR_SUCCESS, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, MQTT_PROP_TYPE_VARINT);
  477. string_to_property_info_helper("session-expiry-interval", MOSQ_ERR_SUCCESS, MQTT_PROP_SESSION_EXPIRY_INTERVAL, MQTT_PROP_TYPE_INT32);
  478. string_to_property_info_helper("assigned-client-identifier", MOSQ_ERR_SUCCESS, MQTT_PROP_ASSIGNED_CLIENT_IDENTIFIER, MQTT_PROP_TYPE_STRING);
  479. string_to_property_info_helper("server-keep-alive", MOSQ_ERR_SUCCESS, MQTT_PROP_SERVER_KEEP_ALIVE, MQTT_PROP_TYPE_INT16);
  480. string_to_property_info_helper("authentication-method", MOSQ_ERR_SUCCESS, MQTT_PROP_AUTHENTICATION_METHOD, MQTT_PROP_TYPE_STRING);
  481. string_to_property_info_helper("authentication-data", MOSQ_ERR_SUCCESS, MQTT_PROP_AUTHENTICATION_DATA, MQTT_PROP_TYPE_BINARY);
  482. string_to_property_info_helper("request-problem-information", MOSQ_ERR_SUCCESS, MQTT_PROP_REQUEST_PROBLEM_INFORMATION, MQTT_PROP_TYPE_BYTE);
  483. string_to_property_info_helper("will-delay-interval", MOSQ_ERR_SUCCESS, MQTT_PROP_WILL_DELAY_INTERVAL, MQTT_PROP_TYPE_INT32);
  484. string_to_property_info_helper("request-response-information", MOSQ_ERR_SUCCESS, MQTT_PROP_REQUEST_RESPONSE_INFORMATION, MQTT_PROP_TYPE_BYTE);
  485. string_to_property_info_helper("response-information", MOSQ_ERR_SUCCESS, MQTT_PROP_RESPONSE_INFORMATION, MQTT_PROP_TYPE_STRING);
  486. string_to_property_info_helper("server-reference", MOSQ_ERR_SUCCESS, MQTT_PROP_SERVER_REFERENCE, MQTT_PROP_TYPE_STRING);
  487. string_to_property_info_helper("reason-string", MOSQ_ERR_SUCCESS, MQTT_PROP_REASON_STRING, MQTT_PROP_TYPE_STRING);
  488. string_to_property_info_helper("receive-maximum", MOSQ_ERR_SUCCESS, MQTT_PROP_RECEIVE_MAXIMUM, MQTT_PROP_TYPE_INT16);
  489. string_to_property_info_helper("topic-alias-maximum", MOSQ_ERR_SUCCESS, MQTT_PROP_TOPIC_ALIAS_MAXIMUM, MQTT_PROP_TYPE_INT16);
  490. string_to_property_info_helper("topic-alias", MOSQ_ERR_SUCCESS, MQTT_PROP_TOPIC_ALIAS, MQTT_PROP_TYPE_INT16);
  491. string_to_property_info_helper("maximum-qos", MOSQ_ERR_SUCCESS, MQTT_PROP_MAXIMUM_QOS, MQTT_PROP_TYPE_BYTE);
  492. string_to_property_info_helper("retain-available", MOSQ_ERR_SUCCESS, MQTT_PROP_RETAIN_AVAILABLE, MQTT_PROP_TYPE_BYTE);
  493. string_to_property_info_helper("user-property", MOSQ_ERR_SUCCESS, MQTT_PROP_USER_PROPERTY, MQTT_PROP_TYPE_STRING_PAIR);
  494. string_to_property_info_helper("maximum-packet-size", MOSQ_ERR_SUCCESS, MQTT_PROP_MAXIMUM_PACKET_SIZE, MQTT_PROP_TYPE_INT32);
  495. string_to_property_info_helper("wildcard-subscription-available", MOSQ_ERR_SUCCESS, MQTT_PROP_WILDCARD_SUB_AVAILABLE, MQTT_PROP_TYPE_BYTE);
  496. string_to_property_info_helper("subscription-identifier-available", MOSQ_ERR_SUCCESS, MQTT_PROP_SUBSCRIPTION_ID_AVAILABLE, MQTT_PROP_TYPE_BYTE);
  497. string_to_property_info_helper("shared-subscription-available", MOSQ_ERR_SUCCESS, MQTT_PROP_SHARED_SUB_AVAILABLE, MQTT_PROP_TYPE_BYTE);
  498. string_to_property_info_helper("payload-format-indicator1", MOSQ_ERR_INVAL, 0, 0);
  499. string_to_property_info_helper("payload", MOSQ_ERR_INVAL, 0, 0);
  500. string_to_property_info_helper("", MOSQ_ERR_INVAL, 0, 0);
  501. string_to_property_info_helper(NULL, MOSQ_ERR_INVAL, 0, 0);
  502. }
  503. /* ========================================================================
  504. * TEST SUITE SETUP
  505. * ======================================================================== */
  506. int init_property_user_read_tests(void)
  507. {
  508. CU_pSuite test_suite = NULL;
  509. test_suite = CU_add_suite("Property user read", NULL, NULL);
  510. if(!test_suite){
  511. printf("Error adding CUnit Property user read test suite.\n");
  512. return 1;
  513. }
  514. if(0
  515. || !CU_add_test(test_suite, "Read single byte", TEST_read_single_byte)
  516. || !CU_add_test(test_suite, "Read single int16", TEST_read_single_int16)
  517. || !CU_add_test(test_suite, "Read single int32", TEST_read_single_int32)
  518. || !CU_add_test(test_suite, "Read single varint", TEST_read_single_varint)
  519. || !CU_add_test(test_suite, "Read single binary", TEST_read_single_binary)
  520. || !CU_add_test(test_suite, "Read single string", TEST_read_single_string)
  521. || !CU_add_test(test_suite, "Read single string pair", TEST_read_single_string_pair)
  522. || !CU_add_test(test_suite, "Read missing", TEST_read_missing)
  523. || !CU_add_test(test_suite, "String to property info", TEST_string_to_property_info)
  524. ){
  525. printf("Error adding Property Add CUnit tests.\n");
  526. return 1;
  527. }
  528. return 0;
  529. }