session.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. static ks_cond_t *g_cond;
  24. static void __on_sess_state_event(swclt_sess_t *sess, void *cb_data)
  25. {
  26. REQUIRE(!strcmp((char *)cb_data, "bobo"));
  27. ks_cond_lock(g_cond);
  28. ks_cond_broadcast(g_cond);
  29. ks_cond_unlock(g_cond);
  30. }
  31. void test_session(ks_pool_t *pool)
  32. {
  33. swclt_sess_t *sess = NULL;
  34. REQUIRE(!ks_cond_create(&g_cond, NULL));
  35. /* Load the config we expect session to load */
  36. REQUIRE(swclt_config_get_private_key_path(g_certified_config));
  37. REQUIRE(swclt_config_get_client_cert_path(g_certified_config));
  38. REQUIRE(swclt_config_get_cert_chain_path(g_certified_config));
  39. REQUIRE(!swclt_sess_create(&sess, g_target_ident_str, g_certified_config));
  40. /* Register a monitor to get to know when session comes online successfully */
  41. REQUIRE(!swclt_sess_set_state_change_cb(sess, __on_sess_state_event, "bobo"));
  42. ks_cond_lock(g_cond);
  43. /* Now take the session onine */
  44. REQUIRE(!swclt_sess_connect(sess));
  45. int i = 5;
  46. while (i-- > 0 && !swclt_sess_connected(sess)) {
  47. ks_cond_timedwait(g_cond, 1000);
  48. }
  49. REQUIRE(swclt_sess_connected(sess));
  50. /* Now disconnect the session */
  51. REQUIRE(!swclt_sess_disconnect(sess));
  52. i = 5;
  53. while (i-- > 0 && swclt_sess_connected(sess)) {
  54. ks_cond_timedwait(g_cond, 1000);
  55. }
  56. REQUIRE(!swclt_sess_connected(sess));
  57. /* Go online again */
  58. REQUIRE(!swclt_sess_connect(sess));
  59. i = 5;
  60. while (i-- > 0 && !swclt_sess_connected(sess)) {
  61. ks_cond_timedwait(g_cond, 1000);
  62. }
  63. REQUIRE(swclt_sess_connected(sess));
  64. ks_cond_unlock(g_cond);
  65. swclt_sess_destroy(&sess);
  66. /* Ok we're done */
  67. ks_cond_destroy(&g_cond);
  68. }