subs_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Tests for subscription adding/removing
  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 "memory_mosq.h"
  11. struct mosquitto_db db;
  12. static void hier_quick_check(struct mosquitto__subhier **sub, struct mosquitto *context, const char *topic)
  13. {
  14. if(sub != NULL){
  15. CU_ASSERT_EQUAL((*sub)->topic_len, strlen(topic));
  16. CU_ASSERT_PTR_NOT_NULL((*sub)->topic);
  17. if((*sub)->topic){
  18. CU_ASSERT_STRING_EQUAL((*sub)->topic, topic);
  19. }
  20. if(context){
  21. CU_ASSERT_PTR_NOT_NULL((*sub)->subs);
  22. if((*sub)->subs){
  23. CU_ASSERT_PTR_EQUAL((*sub)->subs->context, context);
  24. CU_ASSERT_PTR_NULL((*sub)->subs->next);
  25. }
  26. }else{
  27. CU_ASSERT_PTR_NULL((*sub)->subs);
  28. }
  29. (*sub) = (*sub)->children;
  30. }
  31. }
  32. static void TEST_sub_add_single(void)
  33. {
  34. struct mosquitto__config config;
  35. struct mosquitto__listener listener;
  36. struct mosquitto context;
  37. struct mosquitto__subhier *sub;
  38. int rc;
  39. memset(&db, 0, sizeof(struct mosquitto_db));
  40. memset(&config, 0, sizeof(struct mosquitto__config));
  41. memset(&listener, 0, sizeof(struct mosquitto__listener));
  42. memset(&context, 0, sizeof(struct mosquitto));
  43. context.id = "client";
  44. db.config = &config;
  45. listener.port = 1883;
  46. config.listeners = &listener;
  47. config.listener_count = 1;
  48. db__open(&config);
  49. rc = sub__add(&context, "a/b/c/d/e", 0, 0, 0, &db.subs);
  50. CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
  51. CU_ASSERT_PTR_NOT_NULL(db.subs);
  52. if(db.subs){
  53. sub = db.subs;
  54. hier_quick_check(&sub, NULL, "");
  55. hier_quick_check(&sub, NULL, "");
  56. hier_quick_check(&sub, NULL, "a");
  57. hier_quick_check(&sub, NULL, "b");
  58. hier_quick_check(&sub, NULL, "c");
  59. hier_quick_check(&sub, NULL, "d");
  60. hier_quick_check(&sub, &context, "e");
  61. CU_ASSERT_PTR_NULL(sub);
  62. }
  63. mosquitto__free(context.subs);
  64. db__close();
  65. }
  66. /* ========================================================================
  67. * TEST SUITE SETUP
  68. * ======================================================================== */
  69. int main(int argc, char *argv[])
  70. {
  71. CU_pSuite test_suite = NULL;
  72. unsigned int fails;
  73. UNUSED(argc);
  74. UNUSED(argv);
  75. if(CU_initialize_registry() != CUE_SUCCESS){
  76. printf("Error initializing CUnit registry.\n");
  77. return 1;
  78. }
  79. test_suite = CU_add_suite("Subs", NULL, NULL);
  80. if(!test_suite){
  81. printf("Error adding CUnit Subs test suite.\n");
  82. CU_cleanup_registry();
  83. return 1;
  84. }
  85. if(0
  86. || !CU_add_test(test_suite, "Sub add single", TEST_sub_add_single)
  87. ){
  88. printf("Error adding Subs CUnit tests.\n");
  89. CU_cleanup_registry();
  90. return 1;
  91. }
  92. CU_basic_set_mode(CU_BRM_VERBOSE);
  93. CU_basic_run_tests();
  94. fails = CU_get_number_of_failures();
  95. CU_cleanup_registry();
  96. return (int)fails;
  97. }