2
0

session.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static SWCLT_HSTATE g_last_state_change;
  24. static ks_cond_t *g_cond;
  25. static void __on_sess_hmon_event(swclt_sess_t sess, swclt_hstate_change_t *state_change_info, const char *cb_data)
  26. {
  27. REQUIRE(!strcmp(cb_data, "bobo"));
  28. ks_cond_lock(g_cond);
  29. g_last_state_change = state_change_info->new_state;
  30. ks_cond_broadcast(g_cond);
  31. ks_cond_unlock(g_cond);
  32. }
  33. typedef void(*swclt_hstate_change_cb_t)(swclt_handle_base_t *ctx, swclt_hstate_change_t *state_change_request);
  34. void test_session(ks_pool_t *pool)
  35. {
  36. swclt_sess_t sess;
  37. swclt_sess_ctx_t *sess_ctx;
  38. swclt_hmon_t hmon;
  39. REQUIRE(!ks_cond_create(&g_cond, NULL));
  40. /* Load the config we expect session to load */
  41. REQUIRE(swclt_config_get_private_key_path(g_certified_config));
  42. REQUIRE(swclt_config_get_client_cert_path(g_certified_config));
  43. REQUIRE(swclt_config_get_cert_chain_path(g_certified_config));
  44. REQUIRE(!swclt_sess_create(&sess, g_target_ident_str, g_certified_config));
  45. /* Register a monitor to get to know when session comes online successfully */
  46. REQUIRE(!swclt_hmon_register(&hmon, sess, __on_sess_hmon_event, "bobo"));
  47. {
  48. ks_handle_t next = 0;
  49. uint32_t count = 0;
  50. while (KS_STATUS_SUCCESS == ks_handle_enum_type(SWCLT_HTYPE_HMON, &next))
  51. count++;
  52. REQUIRE(count == 1);
  53. }
  54. ks_cond_lock(g_cond);
  55. /* Now take the session onine */
  56. REQUIRE(!swclt_sess_connect(sess));
  57. ks_cond_wait(g_cond);
  58. /* Should be online */
  59. REQUIRE(g_last_state_change == SWCLT_HSTATE_ONLINE);
  60. /* Now disconnect the session */
  61. REQUIRE(!swclt_sess_disconnect(sess));
  62. /* Should be offline now */
  63. ks_cond_wait(g_cond);
  64. REQUIRE(g_last_state_change == SWCLT_HSTATE_OFFLINE);
  65. /* Now delete the monitor */
  66. ks_handle_destroy(&hmon);
  67. ks_cond_unlock(g_cond);
  68. /* Go online again */
  69. REQUIRE(!swclt_sess_connect(sess));
  70. /* Wait for its low level handle state to be back to OK */
  71. while (swclt_hstate_check(sess, NULL))
  72. ks_sleep(1000);
  73. /* Ok we're done */
  74. ks_cond_destroy(&g_cond);
  75. ks_handle_destroy(&sess);
  76. }