main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018 SignalWire, Inc
  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. #include "swclt_test.h"
  23. DECLARE_TEST(hmanager);
  24. DECLARE_TEST(json);
  25. DECLARE_TEST(frame);
  26. DECLARE_TEST(websocket);
  27. DECLARE_TEST(command);
  28. DECLARE_TEST(execute);
  29. DECLARE_TEST(connection);
  30. DECLARE_TEST(session);
  31. DECLARE_TEST(nodestore);
  32. DECLARE_TEST(callback);
  33. DECLARE_TEST(uncert_exp);
  34. DECLARE_TEST(messaging_exp);
  35. DECLARE_TEST(calling_exp);
  36. test_entry_t g_test_methods[] = {
  37. TEST_ENTRY(hmanager),
  38. TEST_ENTRY(json),
  39. TEST_ENTRY(frame),
  40. TEST_ENTRY(websocket),
  41. TEST_ENTRY(command),
  42. TEST_ENTRY(execute),
  43. TEST_ENTRY(connection),
  44. TEST_ENTRY(session),
  45. TEST_ENTRY(nodestore),
  46. TEST_ENTRY(callback),
  47. TEST_ENTRY(uncert_exp),
  48. TEST_ENTRY(messaging_exp),
  49. TEST_ENTRY(calling_exp),
  50. };
  51. static ks_spinlock_t g_log_lock;
  52. const test_entry_t *g_current_test;
  53. swclt_config_t *g_certified_config;
  54. swclt_config_t *g_uncertified_config;
  55. swclt_ident_t g_target_ident;
  56. const char *g_target_ident_str;
  57. static void __set_current_test(const test_entry_t *test_entry)
  58. {
  59. ks_spinlock_acquire(&g_log_lock);
  60. g_current_test = test_entry;
  61. ks_spinlock_release(&g_log_lock);
  62. }
  63. static void __test_logger(const char *file, const char *func, int line, int level, const char *fmt, ...)
  64. {
  65. va_list ap;
  66. char *data;
  67. static char log_line[1024];
  68. va_start(ap, fmt);
  69. ks_vasprintf(&data, fmt, ap);
  70. ks_spinlock_acquire(&g_log_lock);
  71. if (g_current_test)
  72. ks_snprintf(log_line, sizeof(log_line) - 2, "[TEST - %s] %s", g_current_test->name, data);
  73. else
  74. ks_snprintf(log_line, sizeof(log_line) - 2, "%s", data);
  75. if (log_line[strlen(log_line) - 1] != '\n')
  76. strcat(log_line, "\n");
  77. printf("%s", log_line);
  78. #if KS_PLAT_WIN
  79. OutputDebugStringA(log_line);
  80. #endif
  81. fflush(stdout);
  82. free(data);
  83. ks_spinlock_release(&g_log_lock);
  84. va_end(ap);
  85. }
  86. void execute_test(const test_entry_t *entry)
  87. {
  88. __set_current_test(entry);
  89. ks_pool_t *pool;
  90. REQUIRE(!ks_pool_open(&pool));
  91. entry->method(pool);
  92. REQUIRE(!ks_pool_close(&pool));
  93. __set_current_test(NULL);
  94. }
  95. void list_tests()
  96. {
  97. printf("\n");
  98. for (int i = 0; i < sizeof(g_test_methods) / sizeof(test_entry_t); i++) {
  99. printf(" %s\n", g_test_methods[i].name);
  100. }
  101. printf("\n");
  102. }
  103. void execute_named_test(const char *name)
  104. {
  105. for (int i = 0; i < sizeof(g_test_methods) / sizeof(test_entry_t); i++) {
  106. if (strcmp(name, g_test_methods[i].name))
  107. continue;
  108. execute_test(&g_test_methods[i]);
  109. }
  110. }
  111. void test_assertion(const char *assertion, const char *file, int line, const char *tag)
  112. {
  113. ks_abort_fmt("Test: %s failed to assert: %s at: %s:%lu (%s)",
  114. g_current_test->name, assertion, file, line, tag);
  115. }
  116. int main(int argc, char **argv)
  117. {
  118. ks_status_t status;
  119. ks_json_t *certified_config = NULL;
  120. ks_json_t *uncertified_config = NULL;
  121. if (argc == 2 && !strcmp(argv[1], "--list")) {
  122. list_tests();
  123. exit(0);
  124. }
  125. swclt_init(KS_LOG_LEVEL_DEBUG);
  126. ks_global_set_logger(__test_logger);
  127. certified_config = ks_json_pcreate_object(NULL);
  128. uncertified_config = ks_json_pcreate_object(NULL);
  129. ks_json_padd_string_to_object(NULL, certified_config, "private_key_path", "./ca/intermediate/private/controller@freeswitch-upstream.key.pem");
  130. ks_json_padd_string_to_object(NULL, certified_config, "client_cert_path", "./ca/intermediate/certs/controller@freeswitch-upstream.cert.pem");
  131. ks_json_padd_string_to_object(NULL, certified_config, "cert_chain_path", "./ca/intermediate/certs/ca-chain.cert.pem");
  132. swclt_config_create(&g_certified_config);
  133. swclt_config_load_from_json(g_certified_config, certified_config);
  134. ks_json_padd_string_to_object(NULL, uncertified_config, "authentication", "{ \"project\": \"06f784c6-6bd5-47fb-9897-407d66551333\", \"token\": \"PT2eddbccd77832e761d191513df8945d4e1bf70e8f3f74aaa\" }");
  135. swclt_config_create(&g_uncertified_config);
  136. swclt_config_load_from_json(g_uncertified_config, uncertified_config);
  137. ks_json_delete(&certified_config);
  138. ks_json_delete(&uncertified_config);
  139. g_target_ident_str = "blade://switchblade:2100";
  140. if (status = swclt_ident_from_str(&g_target_ident, NULL, g_target_ident_str)) {
  141. goto done;
  142. }
  143. if (argc > 1) {
  144. for (int test_selection = 1; test_selection < argc; test_selection++) {
  145. execute_named_test(argv[test_selection]);
  146. }
  147. } else {
  148. for (int i = 0; i < sizeof(g_test_methods) / sizeof(test_entry_t); i++) {
  149. const char *tail = strrchr(g_test_methods[i].name, '_');
  150. if (tail && !strcmp(tail, "_exp")) continue;
  151. execute_test(&g_test_methods[i]);
  152. }
  153. }
  154. ks_log(KS_LOG_INFO, "ALL TESTS PASS");
  155. done:
  156. swclt_ident_destroy(&g_target_ident);
  157. swclt_config_destroy(&g_uncertified_config);
  158. swclt_config_destroy(&g_certified_config);
  159. if (swclt_shutdown()) {
  160. printf("WARNING Shutdown was ungraceful\n");
  161. ks_debug_break();
  162. }
  163. return status;
  164. }