main.c 5.4 KB

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