persist_read_test.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* Tests for persistence.
  2. *
  3. * FIXME - these need to be aggressive about finding failures, at the moment
  4. * they are just confirming that good behaviour works. */
  5. #include <CUnit/CUnit.h>
  6. #include <CUnit/Basic.h>
  7. #define WITH_BROKER
  8. #define WITH_PERSISTENCE
  9. #include "mosquitto_broker_internal.h"
  10. #include "persist.h"
  11. #include "property_mosq.h"
  12. char *last_sub = NULL;
  13. int last_qos;
  14. uint32_t last_identifier;
  15. struct mosquitto_db db;
  16. static void TEST_persistence_disabled(void)
  17. {
  18. struct mosquitto__config config;
  19. int rc;
  20. memset(&db, 0, sizeof(struct mosquitto_db));
  21. memset(&config, 0, sizeof(struct mosquitto__config));
  22. db.config = &config;
  23. rc = persist__restore();
  24. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  25. }
  26. static void TEST_empty_file(void)
  27. {
  28. struct mosquitto__config config;
  29. int rc;
  30. memset(&db, 0, sizeof(struct mosquitto_db));
  31. memset(&config, 0, sizeof(struct mosquitto__config));
  32. db.config = &config;
  33. config.persistence = true;
  34. config.persistence_filepath = "files/persist_read/empty.test-db";
  35. rc = persist__restore();
  36. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  37. }
  38. static void TEST_corrupt_header(void)
  39. {
  40. struct mosquitto__config config;
  41. int rc;
  42. memset(&db, 0, sizeof(struct mosquitto_db));
  43. memset(&config, 0, sizeof(struct mosquitto__config));
  44. db.config = &config;
  45. config.persistence = true;
  46. config.persistence_filepath = "files/persist_read/corrupt-header-short.test-db";
  47. rc = persist__restore();
  48. CU_ASSERT_EQUAL(rc, 1);
  49. config.persistence_filepath = "files/persist_read/corrupt-header-long.test-db";
  50. rc = persist__restore();
  51. CU_ASSERT_EQUAL(rc, 1);
  52. }
  53. static void TEST_unsupported_version(void)
  54. {
  55. struct mosquitto__config config;
  56. int rc;
  57. memset(&db, 0, sizeof(struct mosquitto_db));
  58. memset(&config, 0, sizeof(struct mosquitto__config));
  59. db.config = &config;
  60. config.persistence = true;
  61. config.persistence_filepath = "files/persist_read/unsupported-version.test-db";
  62. rc = persist__restore();
  63. CU_ASSERT_EQUAL(rc, 1);
  64. }
  65. static void TEST_v3_config_ok(void)
  66. {
  67. struct mosquitto__config config;
  68. int rc;
  69. memset(&db, 0, sizeof(struct mosquitto_db));
  70. memset(&config, 0, sizeof(struct mosquitto__config));
  71. db.config = &config;
  72. config.persistence = true;
  73. config.persistence_filepath = "files/persist_read/v3-cfg.test-db";
  74. rc = persist__restore();
  75. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  76. CU_ASSERT_EQUAL(db.last_db_id, 0x7856341200000000);
  77. }
  78. static void TEST_v4_config_ok(void)
  79. {
  80. struct mosquitto__config config;
  81. int rc;
  82. memset(&db, 0, sizeof(struct mosquitto_db));
  83. memset(&config, 0, sizeof(struct mosquitto__config));
  84. db.config = &config;
  85. config.persistence = true;
  86. config.persistence_filepath = "files/persist_read/v4-cfg.test-db";
  87. rc = persist__restore();
  88. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  89. CU_ASSERT_EQUAL(db.last_db_id, 0x7856341200000000);
  90. }
  91. static void TEST_v3_config_truncated(void)
  92. {
  93. struct mosquitto__config config;
  94. int rc;
  95. memset(&db, 0, sizeof(struct mosquitto_db));
  96. memset(&config, 0, sizeof(struct mosquitto__config));
  97. db.config = &config;
  98. config.persistence = true;
  99. config.persistence_filepath = "files/persist_read/v3-cfg-truncated.test-db";
  100. rc = persist__restore();
  101. CU_ASSERT_EQUAL(rc, 1);
  102. CU_ASSERT_EQUAL(db.last_db_id, 0);
  103. }
  104. static void TEST_v3_config_bad_dbid(void)
  105. {
  106. struct mosquitto__config config;
  107. int rc;
  108. memset(&db, 0, sizeof(struct mosquitto_db));
  109. memset(&config, 0, sizeof(struct mosquitto__config));
  110. db.config = &config;
  111. config.persistence = true;
  112. config.persistence_filepath = "files/persist_read/v3-cfg-bad-dbid.test-db";
  113. rc = persist__restore();
  114. CU_ASSERT_EQUAL(rc, 1);
  115. CU_ASSERT_EQUAL(db.last_db_id, 0);
  116. }
  117. static void TEST_v3_bad_chunk(void)
  118. {
  119. struct mosquitto__config config;
  120. int rc;
  121. memset(&db, 0, sizeof(struct mosquitto_db));
  122. memset(&config, 0, sizeof(struct mosquitto__config));
  123. db.config = &config;
  124. config.persistence = true;
  125. config.persistence_filepath = "files/persist_read/v3-bad-chunk.test-db";
  126. rc = persist__restore();
  127. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  128. CU_ASSERT_EQUAL(db.last_db_id, 0x17);
  129. }
  130. static void TEST_v3_message_store(void)
  131. {
  132. struct mosquitto__config config;
  133. int rc;
  134. memset(&db, 0, sizeof(struct mosquitto_db));
  135. memset(&config, 0, sizeof(struct mosquitto__config));
  136. db.config = &config;
  137. config.persistence = true;
  138. config.persistence_filepath = "files/persist_read/v3-message-store.test-db";
  139. rc = persist__restore();
  140. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  141. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  142. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  143. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  144. if(db.msg_store){
  145. CU_ASSERT_EQUAL(db.msg_store->db_id, 1);
  146. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  147. CU_ASSERT_EQUAL(db.msg_store->source_mid, 2);
  148. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  149. CU_ASSERT_EQUAL(db.msg_store->qos, 2);
  150. CU_ASSERT_EQUAL(db.msg_store->retain, 1);
  151. CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic);
  152. if(db.msg_store->topic){
  153. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  154. }
  155. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  156. if(db.msg_store->payloadlen == 7){
  157. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  158. }
  159. }
  160. }
  161. static void TEST_v3_client(void)
  162. {
  163. struct mosquitto__config config;
  164. struct mosquitto *context;
  165. int rc;
  166. memset(&db, 0, sizeof(struct mosquitto_db));
  167. memset(&config, 0, sizeof(struct mosquitto__config));
  168. db.config = &config;
  169. config.persistence = true;
  170. config.persistence_filepath = "files/persist_read/v3-client.test-db";
  171. rc = persist__restore();
  172. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  173. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  174. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  175. CU_ASSERT_PTR_NOT_NULL(context);
  176. if(context){
  177. CU_ASSERT_PTR_NULL(context->msgs_in.inflight);
  178. CU_ASSERT_PTR_NULL(context->msgs_out.inflight);
  179. CU_ASSERT_EQUAL(context->last_mid, 0x5287);
  180. }
  181. }
  182. static void TEST_v3_client_message(void)
  183. {
  184. struct mosquitto__config config;
  185. struct mosquitto *context;
  186. int rc;
  187. memset(&db, 0, sizeof(struct mosquitto_db));
  188. memset(&config, 0, sizeof(struct mosquitto__config));
  189. db.config = &config;
  190. config.persistence = true;
  191. config.persistence_filepath = "files/persist_read/v3-client-message.test-db";
  192. config.max_inflight_messages = 20;
  193. rc = persist__restore();
  194. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  195. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  196. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  197. CU_ASSERT_PTR_NOT_NULL(context);
  198. if(context){
  199. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight);
  200. if(context->msgs_out.inflight){
  201. CU_ASSERT_PTR_NULL(context->msgs_out.inflight->next);
  202. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight->store);
  203. if(context->msgs_out.inflight->store){
  204. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->ref_count, 1);
  205. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->source_id, "source_id");
  206. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->source_mid, 2);
  207. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->mid, 0);
  208. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->qos, 2);
  209. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->retain, 1);
  210. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight->store->topic);
  211. if(context->msgs_out.inflight->store->topic){
  212. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->topic, "topic");
  213. }
  214. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->payloadlen, 7);
  215. if(context->msgs_out.inflight->store->payloadlen == 7){
  216. CU_ASSERT_NSTRING_EQUAL(context->msgs_out.inflight->store->payload, "payload", 7);
  217. }
  218. }
  219. CU_ASSERT_EQUAL(context->msgs_out.inflight->mid, 0x73);
  220. CU_ASSERT_EQUAL(context->msgs_out.inflight->qos, 1);
  221. CU_ASSERT_EQUAL(context->msgs_out.inflight->retain, 0);
  222. CU_ASSERT_EQUAL(context->msgs_out.inflight->direction, mosq_md_out);
  223. CU_ASSERT_EQUAL(context->msgs_out.inflight->state, mosq_ms_wait_for_puback);
  224. CU_ASSERT_EQUAL(context->msgs_out.inflight->dup, 0);
  225. CU_ASSERT_PTR_NULL(context->msgs_out.inflight->properties);
  226. }
  227. }
  228. }
  229. static void TEST_v3_retain(void)
  230. {
  231. struct mosquitto__config config;
  232. int rc;
  233. memset(&db, 0, sizeof(struct mosquitto_db));
  234. memset(&config, 0, sizeof(struct mosquitto__config));
  235. db.config = &config;
  236. retain__init();
  237. config.persistence = true;
  238. config.persistence_filepath = "files/persist_read/v3-retain.test-db";
  239. rc = persist__restore();
  240. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  241. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  242. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  243. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  244. if(db.msg_store){
  245. CU_ASSERT_EQUAL(db.msg_store->db_id, 0x54);
  246. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  247. CU_ASSERT_EQUAL(db.msg_store->source_mid, 2);
  248. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  249. CU_ASSERT_EQUAL(db.msg_store->qos, 2);
  250. CU_ASSERT_EQUAL(db.msg_store->retain, 1);
  251. CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic);
  252. if(db.msg_store->topic){
  253. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  254. }
  255. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  256. if(db.msg_store->payloadlen == 7){
  257. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  258. }
  259. }
  260. CU_ASSERT_PTR_NOT_NULL(db.retains);
  261. if(db.retains){
  262. CU_ASSERT_STRING_EQUAL(db.retains->topic, "");
  263. CU_ASSERT_PTR_NOT_NULL(db.retains->children);
  264. if(db.retains->children){
  265. CU_ASSERT_STRING_EQUAL(db.retains->children->topic, "");
  266. CU_ASSERT_PTR_NOT_NULL(db.retains->children->children);
  267. if(db.retains->children->children){
  268. CU_ASSERT_STRING_EQUAL(db.retains->children->children->topic, "topic");
  269. }
  270. }
  271. }
  272. }
  273. static void TEST_v3_sub(void)
  274. {
  275. struct mosquitto__config config;
  276. struct mosquitto *context;
  277. int rc;
  278. last_sub = NULL;
  279. last_qos = -1;
  280. memset(&db, 0, sizeof(struct mosquitto_db));
  281. memset(&config, 0, sizeof(struct mosquitto__config));
  282. db.config = &config;
  283. config.persistence = true;
  284. config.persistence_filepath = "files/persist_read/v3-sub.test-db";
  285. rc = persist__restore();
  286. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  287. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  288. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  289. CU_ASSERT_PTR_NOT_NULL(context);
  290. if(context){
  291. CU_ASSERT_PTR_NOT_NULL(last_sub);
  292. if(last_sub){
  293. CU_ASSERT_STRING_EQUAL(last_sub, "subscription")
  294. free(last_sub);
  295. }
  296. CU_ASSERT_EQUAL(last_qos, 1);
  297. }
  298. }
  299. static void TEST_v4_message_store(void)
  300. {
  301. struct mosquitto__config config;
  302. int rc;
  303. memset(&db, 0, sizeof(struct mosquitto_db));
  304. memset(&config, 0, sizeof(struct mosquitto__config));
  305. db.config = &config;
  306. config.persistence = true;
  307. config.persistence_filepath = "files/persist_read/v4-message-store.test-db";
  308. rc = persist__restore();
  309. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  310. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  311. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  312. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  313. if(db.msg_store){
  314. CU_ASSERT_EQUAL(db.msg_store->db_id, 0xFEDCBA9876543210);
  315. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  316. CU_ASSERT_EQUAL(db.msg_store->source_mid, 0x88);
  317. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  318. CU_ASSERT_EQUAL(db.msg_store->qos, 1);
  319. CU_ASSERT_EQUAL(db.msg_store->retain, 0);
  320. CU_ASSERT_PTR_NOT_NULL(db.msg_store->topic);
  321. if(db.msg_store->topic){
  322. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  323. }
  324. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  325. if(db.msg_store->payloadlen == 7){
  326. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  327. }
  328. }
  329. }
  330. static void TEST_v6_config_ok(void)
  331. {
  332. struct mosquitto__config config;
  333. int rc;
  334. memset(&db, 0, sizeof(struct mosquitto_db));
  335. memset(&config, 0, sizeof(struct mosquitto__config));
  336. db.config = &config;
  337. config.persistence = true;
  338. config.persistence_filepath = "files/persist_read/v6-cfg.test-db";
  339. rc = persist__restore();
  340. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  341. CU_ASSERT_EQUAL(db.last_db_id, 0x7856341200000000);
  342. }
  343. static void TEST_v5_config_truncated(void)
  344. {
  345. struct mosquitto__config config;
  346. int rc;
  347. memset(&db, 0, sizeof(struct mosquitto_db));
  348. memset(&config, 0, sizeof(struct mosquitto__config));
  349. db.config = &config;
  350. config.persistence = true;
  351. config.persistence_filepath = "files/persist_read/v5-cfg-truncated.test-db";
  352. rc = persist__restore();
  353. CU_ASSERT_EQUAL(rc, 1);
  354. CU_ASSERT_EQUAL(db.last_db_id, 0);
  355. }
  356. static void TEST_v5_bad_chunk(void)
  357. {
  358. struct mosquitto__config config;
  359. int rc;
  360. memset(&db, 0, sizeof(struct mosquitto_db));
  361. memset(&config, 0, sizeof(struct mosquitto__config));
  362. db.config = &config;
  363. config.persistence = true;
  364. config.persistence_filepath = "files/persist_read/v5-bad-chunk.test-db";
  365. rc = persist__restore();
  366. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  367. CU_ASSERT_EQUAL(db.last_db_id, 0x17);
  368. }
  369. static void TEST_v6_message_store(void)
  370. {
  371. struct mosquitto__config config;
  372. int rc;
  373. memset(&db, 0, sizeof(struct mosquitto_db));
  374. memset(&config, 0, sizeof(struct mosquitto__config));
  375. db.config = &config;
  376. config.persistence = true;
  377. config.persistence_filepath = "files/persist_read/v6-message-store.test-db";
  378. rc = persist__restore();
  379. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  380. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  381. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  382. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  383. if(db.msg_store){
  384. CU_ASSERT_EQUAL(db.msg_store->db_id, 1);
  385. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  386. CU_ASSERT_EQUAL(db.msg_store->source_mid, 2);
  387. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  388. CU_ASSERT_EQUAL(db.msg_store->qos, 2);
  389. CU_ASSERT_EQUAL(db.msg_store->retain, 1);
  390. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  391. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  392. if(db.msg_store->payloadlen == 7){
  393. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  394. }
  395. CU_ASSERT_PTR_NULL(db.msg_store->properties);
  396. }
  397. }
  398. static void TEST_v6_message_store_props(void)
  399. {
  400. struct mosquitto__config config;
  401. struct mosquitto__listener listener;
  402. int rc;
  403. memset(&db, 0, sizeof(struct mosquitto_db));
  404. memset(&config, 0, sizeof(struct mosquitto__config));
  405. memset(&listener, 0, sizeof(struct mosquitto__listener));
  406. db.config = &config;
  407. listener.port = 1883;
  408. config.listeners = &listener;
  409. config.listener_count = 1;
  410. config.persistence = true;
  411. config.persistence_filepath = "files/persist_read/v6-message-store-props.test-db";
  412. rc = persist__restore();
  413. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  414. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  415. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  416. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  417. if(db.msg_store){
  418. CU_ASSERT_EQUAL(db.msg_store->db_id, 1);
  419. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  420. CU_ASSERT_EQUAL(db.msg_store->source_mid, 2);
  421. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  422. CU_ASSERT_EQUAL(db.msg_store->qos, 2);
  423. CU_ASSERT_EQUAL(db.msg_store->retain, 1);
  424. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  425. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  426. if(db.msg_store->payloadlen == 7){
  427. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  428. }
  429. CU_ASSERT_PTR_NOT_NULL(db.msg_store->properties);
  430. if(db.msg_store->properties){
  431. CU_ASSERT_EQUAL(db.msg_store->properties->identifier, 1);
  432. CU_ASSERT_EQUAL(db.msg_store->properties->value.i8, 1);
  433. }
  434. CU_ASSERT_PTR_NOT_NULL(db.msg_store->source_listener);
  435. }
  436. }
  437. static void TEST_v5_client(void)
  438. {
  439. struct mosquitto__config config;
  440. struct mosquitto *context;
  441. int rc;
  442. memset(&db, 0, sizeof(struct mosquitto_db));
  443. memset(&config, 0, sizeof(struct mosquitto__config));
  444. db.config = &config;
  445. config.persistence = true;
  446. config.persistence_filepath = "files/persist_read/v5-client.test-db";
  447. rc = persist__restore();
  448. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  449. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  450. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  451. CU_ASSERT_PTR_NOT_NULL(context);
  452. if(context){
  453. CU_ASSERT_PTR_NULL(context->msgs_in.inflight);
  454. CU_ASSERT_PTR_NULL(context->msgs_out.inflight);
  455. CU_ASSERT_EQUAL(context->last_mid, 0x5287);
  456. }
  457. }
  458. static void TEST_v6_client(void)
  459. {
  460. struct mosquitto__config config;
  461. struct mosquitto *context;
  462. struct mosquitto__listener listener;
  463. int rc;
  464. memset(&db, 0, sizeof(struct mosquitto_db));
  465. memset(&config, 0, sizeof(struct mosquitto__config));
  466. memset(&listener, 0, sizeof(struct mosquitto__listener));
  467. db.config = &config;
  468. listener.port = 1883;
  469. config.per_listener_settings = true;
  470. config.listeners = &listener;
  471. config.listener_count = 1;
  472. config.persistence = true;
  473. config.persistence_filepath = "files/persist_read/v6-client.test-db";
  474. rc = persist__restore();
  475. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  476. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  477. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  478. CU_ASSERT_PTR_NOT_NULL(context);
  479. if(context){
  480. CU_ASSERT_PTR_NULL(context->msgs_in.inflight);
  481. CU_ASSERT_PTR_NULL(context->msgs_out.inflight);
  482. CU_ASSERT_EQUAL(context->last_mid, 0x5287);
  483. CU_ASSERT_EQUAL(context->listener, &listener);
  484. CU_ASSERT_PTR_NOT_NULL(context->username);
  485. if(context->username){
  486. CU_ASSERT_STRING_EQUAL(context->username, "usrname");
  487. }
  488. }
  489. }
  490. static void TEST_v6_client_message(void)
  491. {
  492. struct mosquitto__config config;
  493. struct mosquitto *context;
  494. int rc;
  495. memset(&db, 0, sizeof(struct mosquitto_db));
  496. memset(&config, 0, sizeof(struct mosquitto__config));
  497. db.config = &config;
  498. config.persistence = true;
  499. config.persistence_filepath = "files/persist_read/v6-client-message.test-db";
  500. rc = persist__restore();
  501. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  502. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  503. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  504. CU_ASSERT_PTR_NOT_NULL(context);
  505. if(context){
  506. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight);
  507. if(context->msgs_out.inflight){
  508. CU_ASSERT_PTR_NULL(context->msgs_out.inflight->next);
  509. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight->store);
  510. if(context->msgs_out.inflight->store){
  511. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->ref_count, 1);
  512. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->source_id, "source_id");
  513. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->source_mid, 2);
  514. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->mid, 0);
  515. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->qos, 2);
  516. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->retain, 1);
  517. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->topic, "topic");
  518. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->payloadlen, 7);
  519. if(context->msgs_out.inflight->store->payloadlen == 7){
  520. CU_ASSERT_NSTRING_EQUAL(context->msgs_out.inflight->store->payload, "payload", 7);
  521. }
  522. }
  523. CU_ASSERT_EQUAL(context->msgs_out.inflight->mid, 0x73);
  524. CU_ASSERT_EQUAL(context->msgs_out.inflight->qos, 1);
  525. CU_ASSERT_EQUAL(context->msgs_out.inflight->retain, 0);
  526. CU_ASSERT_EQUAL(context->msgs_out.inflight->direction, mosq_md_out);
  527. CU_ASSERT_EQUAL(context->msgs_out.inflight->state, mosq_ms_wait_for_puback);
  528. CU_ASSERT_EQUAL(context->msgs_out.inflight->dup, 0);
  529. CU_ASSERT_PTR_NULL(context->msgs_out.inflight->properties);
  530. }
  531. }
  532. }
  533. static void TEST_v6_client_message_props(void)
  534. {
  535. struct mosquitto__config config;
  536. struct mosquitto *context;
  537. int rc;
  538. memset(&db, 0, sizeof(struct mosquitto_db));
  539. memset(&config, 0, sizeof(struct mosquitto__config));
  540. db.config = &config;
  541. config.persistence = true;
  542. config.persistence_filepath = "files/persist_read/v6-client-message-props.test-db";
  543. rc = persist__restore();
  544. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  545. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  546. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  547. CU_ASSERT_PTR_NOT_NULL(context);
  548. if(context){
  549. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight);
  550. if(context->msgs_out.inflight){
  551. CU_ASSERT_PTR_NULL(context->msgs_out.inflight->next);
  552. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight->store);
  553. if(context->msgs_out.inflight->store){
  554. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->ref_count, 1);
  555. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->source_id, "source_id");
  556. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->source_mid, 2);
  557. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->mid, 0);
  558. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->qos, 2);
  559. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->retain, 1);
  560. CU_ASSERT_STRING_EQUAL(context->msgs_out.inflight->store->topic, "topic");
  561. CU_ASSERT_EQUAL(context->msgs_out.inflight->store->payloadlen, 7);
  562. if(context->msgs_out.inflight->store->payloadlen == 7){
  563. CU_ASSERT_NSTRING_EQUAL(context->msgs_out.inflight->store->payload, "payload", 7);
  564. }
  565. }
  566. CU_ASSERT_EQUAL(context->msgs_out.inflight->mid, 0x73);
  567. CU_ASSERT_EQUAL(context->msgs_out.inflight->qos, 1);
  568. CU_ASSERT_EQUAL(context->msgs_out.inflight->retain, 0);
  569. CU_ASSERT_EQUAL(context->msgs_out.inflight->direction, mosq_md_out);
  570. CU_ASSERT_EQUAL(context->msgs_out.inflight->state, mosq_ms_wait_for_puback);
  571. CU_ASSERT_EQUAL(context->msgs_out.inflight->dup, 0);
  572. CU_ASSERT_PTR_NOT_NULL(context->msgs_out.inflight->properties);
  573. if(context->msgs_out.inflight->properties){
  574. CU_ASSERT_EQUAL(context->msgs_out.inflight->properties->identifier, 1);
  575. CU_ASSERT_EQUAL(context->msgs_out.inflight->properties->value.i8, 1);
  576. }
  577. }
  578. }
  579. }
  580. static void TEST_v6_retain(void)
  581. {
  582. struct mosquitto__config config;
  583. int rc;
  584. memset(&db, 0, sizeof(struct mosquitto_db));
  585. memset(&config, 0, sizeof(struct mosquitto__config));
  586. db.config = &config;
  587. config.persistence = true;
  588. config.persistence_filepath = "files/persist_read/v6-retain.test-db";
  589. retain__init();
  590. rc = persist__restore();
  591. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  592. CU_ASSERT_EQUAL(db.msg_store_count, 1);
  593. CU_ASSERT_EQUAL(db.msg_store_bytes, 7);
  594. CU_ASSERT_PTR_NOT_NULL(db.msg_store);
  595. if(db.msg_store){
  596. CU_ASSERT_EQUAL(db.msg_store->db_id, 0x54);
  597. CU_ASSERT_STRING_EQUAL(db.msg_store->source_id, "source_id");
  598. CU_ASSERT_EQUAL(db.msg_store->source_mid, 2);
  599. CU_ASSERT_EQUAL(db.msg_store->mid, 0);
  600. CU_ASSERT_EQUAL(db.msg_store->qos, 2);
  601. CU_ASSERT_EQUAL(db.msg_store->retain, 1);
  602. CU_ASSERT_STRING_EQUAL(db.msg_store->topic, "topic");
  603. CU_ASSERT_EQUAL(db.msg_store->payloadlen, 7);
  604. if(db.msg_store->payloadlen == 7){
  605. CU_ASSERT_NSTRING_EQUAL(db.msg_store->payload, "payload", 7);
  606. }
  607. }
  608. CU_ASSERT_PTR_NOT_NULL(db.retains);
  609. if(db.retains){
  610. CU_ASSERT_STRING_EQUAL(db.retains->topic, "");
  611. CU_ASSERT_PTR_NOT_NULL(db.retains->children);
  612. if(db.retains->children){
  613. CU_ASSERT_STRING_EQUAL(db.retains->children->topic, "");
  614. CU_ASSERT_PTR_NOT_NULL(db.retains->children->children);
  615. if(db.retains->children->children){
  616. CU_ASSERT_STRING_EQUAL(db.retains->children->children->topic, "topic");
  617. }
  618. }
  619. }
  620. }
  621. static void TEST_v6_sub(void)
  622. {
  623. struct mosquitto__config config;
  624. struct mosquitto *context;
  625. int rc;
  626. last_sub = NULL;
  627. last_qos = -1;
  628. memset(&db, 0, sizeof(struct mosquitto_db));
  629. memset(&config, 0, sizeof(struct mosquitto__config));
  630. db.config = &config;
  631. config.persistence = true;
  632. config.persistence_filepath = "files/persist_read/v6-sub.test-db";
  633. rc = persist__restore();
  634. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  635. CU_ASSERT_PTR_NOT_NULL(db.contexts_by_id);
  636. HASH_FIND(hh_id, db.contexts_by_id, "client-id", strlen("client-id"), context);
  637. CU_ASSERT_PTR_NOT_NULL(context);
  638. if(context){
  639. CU_ASSERT_PTR_NOT_NULL(last_sub);
  640. if(last_sub){
  641. CU_ASSERT_STRING_EQUAL(last_sub, "subscription")
  642. free(last_sub);
  643. }
  644. CU_ASSERT_EQUAL(last_qos, 1);
  645. CU_ASSERT_EQUAL(last_identifier, 0x7623);
  646. }
  647. }
  648. /* ========================================================================
  649. * TEST SUITE SETUP
  650. * ======================================================================== */
  651. int init_persist_read_tests(void)
  652. {
  653. CU_pSuite test_suite = NULL;
  654. test_suite = CU_add_suite("Persist read", NULL, NULL);
  655. if(!test_suite){
  656. printf("Error adding CUnit persist read test suite.\n");
  657. return 1;
  658. }
  659. if(0
  660. || !CU_add_test(test_suite, "Persistence disabled", TEST_persistence_disabled)
  661. || !CU_add_test(test_suite, "Empty file", TEST_empty_file)
  662. || !CU_add_test(test_suite, "Corrupt header", TEST_corrupt_header)
  663. || !CU_add_test(test_suite, "Unsupported version", TEST_unsupported_version)
  664. || !CU_add_test(test_suite, "v3 config ok", TEST_v3_config_ok)
  665. || !CU_add_test(test_suite, "v3 config bad truncated", TEST_v3_config_truncated)
  666. || !CU_add_test(test_suite, "v3 config bad dbid", TEST_v3_config_bad_dbid)
  667. || !CU_add_test(test_suite, "v3 bad chunk", TEST_v3_bad_chunk)
  668. || !CU_add_test(test_suite, "v3 message store", TEST_v3_message_store)
  669. || !CU_add_test(test_suite, "v3 client", TEST_v3_client)
  670. || !CU_add_test(test_suite, "v3 client message", TEST_v3_client_message)
  671. || !CU_add_test(test_suite, "v3 retain", TEST_v3_retain)
  672. || !CU_add_test(test_suite, "v3 sub", TEST_v3_sub)
  673. || !CU_add_test(test_suite, "v4 config ok", TEST_v4_config_ok)
  674. || !CU_add_test(test_suite, "v4 message store", TEST_v4_message_store)
  675. || !CU_add_test(test_suite, "v5 client", TEST_v5_client)
  676. || !CU_add_test(test_suite, "v5 config bad truncated", TEST_v5_config_truncated)
  677. || !CU_add_test(test_suite, "v5 bad chunk", TEST_v5_bad_chunk)
  678. || !CU_add_test(test_suite, "v6 config ok", TEST_v6_config_ok)
  679. || !CU_add_test(test_suite, "v6 message store", TEST_v6_message_store)
  680. || !CU_add_test(test_suite, "v6 message store+props", TEST_v6_message_store_props)
  681. || !CU_add_test(test_suite, "v6 client", TEST_v6_client)
  682. || !CU_add_test(test_suite, "v6 client message", TEST_v6_client_message)
  683. || !CU_add_test(test_suite, "v6 client message+props", TEST_v6_client_message_props)
  684. || !CU_add_test(test_suite, "v6 retain", TEST_v6_retain)
  685. || !CU_add_test(test_suite, "v6 sub", TEST_v6_sub)
  686. ){
  687. printf("Error adding persist CUnit tests.\n");
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. int main(int argc, char *argv[])
  693. {
  694. unsigned int fails;
  695. UNUSED(argc);
  696. UNUSED(argv);
  697. if(CU_initialize_registry() != CUE_SUCCESS){
  698. printf("Error initializing CUnit registry.\n");
  699. return 1;
  700. }
  701. if(0
  702. || init_persist_read_tests()
  703. ){
  704. CU_cleanup_registry();
  705. return 1;
  706. }
  707. CU_basic_set_mode(CU_BRM_VERBOSE);
  708. CU_basic_run_tests();
  709. fails = CU_get_number_of_failures();
  710. CU_cleanup_registry();
  711. return (int)fails;
  712. }