json.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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();
  34. ks_json_add_item_to_array(params.channels, BLADE_CHANNEL_MARSHAL(&(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(&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_add_string_to_object(params, "tag", "hi");
  43. swclt_cmd_t *cmd = CREATE_BLADE_EXECUTE_CMD(
  44. NULL,
  45. "responder_a",
  46. "test.protocol",
  47. "test.method",
  48. &params);
  49. REQUIRE(!params);
  50. char *desc;
  51. REQUIRE(!swclt_cmd_print(cmd, NULL, &desc));
  52. ks_log(KS_LOG_INFO, "Created command: %s", desc);
  53. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "responder_nodeid", ""), "responder_a"));
  54. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "protocol", ""), "test.protocol"));
  55. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "method", ""), "test.method"));
  56. REQUIRE(!strcmp(ks_json_get_object_string(ks_json_get_object_item(cmd->json, "params"), "tag", ""), "hi"));
  57. ks_pool_free(&desc);
  58. swclt_cmd_destroy(&cmd);
  59. }
  60. static void test_blade_execute_with_id(ks_pool_t *pool)
  61. {
  62. ks_json_t *params = ks_json_create_object();
  63. ks_json_add_string_to_object(params, "tag", "hi");
  64. swclt_cmd_t *cmd = CREATE_BLADE_EXECUTE_CMD(
  65. "a6786101-4c6e-4d1a-ac22-1c5dcbbd3c48",
  66. "responder_a",
  67. "test.protocol",
  68. "test.method",
  69. &params);
  70. REQUIRE(!params);
  71. char *desc;
  72. REQUIRE(!swclt_cmd_print(cmd, NULL, &desc));
  73. ks_log(KS_LOG_INFO, "Created command: %s", desc);
  74. REQUIRE(!strcmp(cmd->id_str, "a6786101-4c6e-4d1a-ac22-1c5dcbbd3c48"));
  75. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "responder_nodeid", ""), "responder_a"));
  76. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "protocol", ""), "test.protocol"));
  77. REQUIRE(!strcmp(ks_json_get_object_string(cmd->json, "method", ""), "test.method"));
  78. REQUIRE(!strcmp(ks_json_get_object_string(ks_json_get_object_item(cmd->json, "params"), "tag", ""), "hi"));
  79. ks_pool_free(&desc);
  80. swclt_cmd_destroy(&cmd);
  81. }
  82. static void test_blade_channel(ks_pool_t *pool)
  83. {
  84. ks_json_t *obj = BLADE_CHANNEL_MARSHAL(&(blade_channel_t){"bobo", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC});
  85. ks_json_delete(&obj);
  86. }
  87. void test_json(ks_pool_t *pool)
  88. {
  89. test_blade_channel(pool);
  90. test_blade_execute(pool);
  91. test_blade_execute_with_id(pool);
  92. }