main.c 5.5 KB

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