json.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 blade_netcast_rqu_t __netcast_protocol_provider_add_request(ks_pool_t *pool, const char *protocol, ks_uuid_t nodeid, const char *channel)
  24. {
  25. blade_netcast_rqu_t request;
  26. request.command = BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD;
  27. request.certified_only = KS_TRUE;
  28. request.netcaster_nodeid = ks_uuid_null_thr_str();
  29. /* Fill in the params too */
  30. blade_netcast_protocol_provider_add_param_t params = {0};
  31. params.protocol = protocol;
  32. params.nodeid = ks_uuid_str(NULL, &nodeid);
  33. params.channels = ks_json_create_array_inline(1,
  34. BLADE_CHANNEL_MARSHAL(NULL, &(blade_channel_t){channel, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC}));
  35. /* Marshal it into its parent request */
  36. request.params = BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_MARSHAL(NULL, &params);
  37. return request;
  38. }
  39. static void test_blade_execute(ks_pool_t *pool)
  40. {
  41. ks_json_t *params = ks_json_create_object();
  42. ks_json_padd_string_to_object(NULL, params, "tag", "hi");
  43. swclt_cmd_t cmd = CREATE_BLADE_EXECUTE_CMD(
  44. "responder_a",
  45. "test.protocol",
  46. "test.method",
  47. &params);
  48. REQUIRE(!params);
  49. const ks_json_t *obj;
  50. REQUIRE(swclt_cmd_error(cmd, &obj));
  51. REQUIRE(swclt_cmd_result(cmd, &obj));
  52. REQUIRE(!swclt_cmd_request(cmd, &obj));
  53. char *desc;
  54. REQUIRE(!swclt_cmd_print(cmd, NULL, &desc));
  55. ks_log(KS_LOG_INFO, "Created command: %s", desc);
  56. REQUIRE(!strcmp(ks_json_get_object_cstr(obj, "responder_nodeid"), "responder_a"));
  57. REQUIRE(!strcmp(ks_json_get_object_cstr(obj, "protocol"), "test.protocol"));
  58. REQUIRE(!strcmp(ks_json_get_object_cstr(obj, "method"), "test.method"));
  59. REQUIRE(!strcmp(ks_json_lookup_cstr(obj, 2, "params", "tag"), "hi"));
  60. ks_pool_free(&desc);
  61. ks_handle_destroy(&cmd);
  62. }
  63. static void test_blade_channel(ks_pool_t *pool)
  64. {
  65. ks_json_t *obj = BLADE_CHANNEL_MARSHAL(NULL, &(blade_channel_t){"bobo", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC});
  66. ks_json_delete(&obj);
  67. }
  68. void test_json(ks_pool_t *pool)
  69. {
  70. test_blade_channel(pool);
  71. test_blade_execute(pool);
  72. }