main.c 5.3 KB

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