nodestore.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2018-2019 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. #include "signalwire-client-c/internal/nodestore.h"
  24. ks_uuid_t g_route_nodeid_1, g_route_nodeid_2, g_sessionid;
  25. blade_netcast_rqu_t __netcast_protocol_provider_add_request(ks_pool_t *pool, const char *protocol, ks_uuid_t nodeid, const char *channel)
  26. {
  27. blade_netcast_rqu_t request;
  28. request.command = BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD;
  29. request.certified_only = KS_TRUE;
  30. request.netcaster_nodeid = ks_uuid_null_str(pool);
  31. /* Fill in the params too */
  32. blade_netcast_protocol_provider_add_param_t params = {0};
  33. params.protocol = protocol;
  34. params.nodeid = ks_uuid_str(pool, &nodeid);
  35. params.channels = ks_json_pcreate_array_inline(pool, 1,
  36. BLADE_CHANNEL_MARSHAL(pool, &(blade_channel_t){channel, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC}));
  37. /* Marshal it into its parent request */
  38. request.params = BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_MARSHAL(pool, &params);
  39. return request;
  40. }
  41. blade_netcast_rqu_t __netcast_route_add_request(ks_pool_t *pool, const char *nodeid, ks_bool_t certified)
  42. {
  43. blade_netcast_rqu_t request;
  44. request.command = BLADE_NETCAST_CMD_ROUTE_ADD;
  45. request.certified_only = KS_TRUE;
  46. request.netcaster_nodeid = ks_uuid_null_str(pool);
  47. /* Fill in the params too */
  48. blade_netcast_route_add_param_t params;
  49. params.certified = certified;
  50. params.nodeid = nodeid;
  51. /* Marshal it into its parent request */
  52. request.params = BLADE_NETCAST_ROUTE_ADD_PARAM_MARSHAL(pool, &params);
  53. return request;
  54. }
  55. blade_netcast_rqu_t __netcast_route_remove_request(ks_pool_t *pool, const char *nodeid)
  56. {
  57. blade_netcast_rqu_t request;
  58. request.command = BLADE_NETCAST_CMD_ROUTE_REMOVE;
  59. request.certified_only = KS_TRUE;
  60. request.netcaster_nodeid = ks_uuid_null_str(pool);
  61. /* Fill in the params too */
  62. blade_netcast_route_remove_param_t params;
  63. params.nodeid = nodeid;
  64. params.certified = KS_TRUE;
  65. /* Marshal it into its parent request */
  66. request.params = BLADE_NETCAST_ROUTE_REMOVE_PARAM_MARSHAL(pool, &params);
  67. return request;
  68. }
  69. blade_connect_rpl_t __connect_reply(ks_pool_t *pool)
  70. {
  71. ks_json_t *routes = ks_json_pcreate_array(pool);
  72. ks_json_t *protocols = ks_json_pcreate_array(pool);
  73. ks_json_t *subscriptions = ks_json_pcreate_array(pool);
  74. /* Add a couple routes */
  75. ks_json_add_item_to_array(routes, BLADE_NODE_MARSHAL(pool, &(blade_node_t){ks_uuid_str(pool, &g_route_nodeid_1)}));
  76. ks_json_add_item_to_array(routes, BLADE_NODE_MARSHAL(pool, &(blade_node_t){ks_uuid_str(pool, &g_route_nodeid_2)}));
  77. /* Add a couple protocols each with one provider and one channel */
  78. ks_json_add_item_to_array(protocols,
  79. BLADE_PROTOCOL_MARSHAL(pool, &(blade_protocol_t){
  80. "bobo_protocol_1", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC,
  81. ks_json_pcreate_array_inline(pool, 1, BLADE_PROVIDER_MARSHAL(pool, &(blade_provider_t){ks_uuid_str(pool, &g_route_nodeid_1), ks_json_pcreate_array(pool)})),
  82. ks_json_pcreate_array_inline(pool, 1, BLADE_CHANNEL_MARSHAL(pool, &(blade_channel_t){"bobo_channel_1", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC}))
  83. }));
  84. ks_json_add_item_to_array(protocols,
  85. BLADE_PROTOCOL_MARSHAL(pool, &(blade_protocol_t){
  86. "bobo_protocol_2", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC,
  87. ks_json_pcreate_array_inline(pool, 1, BLADE_PROVIDER_MARSHAL(pool, &(blade_provider_t){ks_uuid_str(pool, &g_route_nodeid_2), ks_json_pcreate_array(pool)})),
  88. ks_json_pcreate_array_inline(pool, 1, BLADE_CHANNEL_MARSHAL(pool, &(blade_channel_t){"bobo_channel_2", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC}))
  89. }));
  90. /* Have the second node be subscribed to the first */
  91. ks_json_add_item_to_array(subscriptions,
  92. BLADE_SUBSCRIPTION_MARSHAL(pool, &(blade_subscription_t){
  93. "bobo_protocol_1",
  94. "bobo_channel_1",
  95. ks_json_pcreate_array_inline(pool, 1,
  96. ks_json_pcreate_uuid(pool, g_route_nodeid_2))
  97. }));
  98. /* Now compose it altogether in a connect result */
  99. blade_connect_rpl_t reply = (blade_connect_rpl_t){
  100. KS_FALSE,
  101. *ks_uuid(&g_sessionid),
  102. ks_uuid_str(pool, &g_route_nodeid_1),
  103. ks_uuid_null_str(pool),
  104. NULL,
  105. routes,
  106. protocols,
  107. subscriptions,
  108. ks_json_pcreate_array(pool)
  109. };
  110. return reply;
  111. }
  112. void test_nodestore_update(ks_pool_t *pool)
  113. {
  114. blade_connect_rpl_t connect_rpl = __connect_reply(pool);
  115. swclt_store_t store;
  116. swclt_store_ctx_t *store_ctx;
  117. ks_uuid_t new_route_nodeid;
  118. const char *new_route_nodeid_str;
  119. ks_uuid(&new_route_nodeid);
  120. new_route_nodeid_str = ks_uuid_str(pool, &new_route_nodeid);
  121. REQUIRE(!swclt_store_create(&store));
  122. REQUIRE(!swclt_store_populate(store, &connect_rpl));
  123. REQUIRE(!swclt_store_get(store, &store_ctx));
  124. /* The store should properly render the types */
  125. REQUIRE(ks_hash_count(store_ctx->protocols) == 2);
  126. REQUIRE(ks_hash_count(store_ctx->subscriptions) == 1);
  127. REQUIRE(ks_hash_count(store_ctx->routes) == 2);
  128. REQUIRE(ks_hash_count(store_ctx->authorities) == 0);
  129. /* Now update with a new node */
  130. blade_netcast_rqu_t netcast_rqu = __netcast_route_add_request(pool, new_route_nodeid_str, KS_TRUE);
  131. REQUIRE(!swclt_store_update(store, &netcast_rqu));
  132. REQUIRE(ks_hash_count(store_ctx->routes) == 3);
  133. REQUIRE(((blade_node_t *)ks_hash_search(store_ctx->routes, new_route_nodeid_str, KS_UNLOCKED))->certified == KS_TRUE);
  134. ks_json_delete(&netcast_rqu.params);
  135. /* And a new provider with a new provider + channel */
  136. netcast_rqu = __netcast_protocol_provider_add_request(pool, "bobo_protocol_new", new_route_nodeid, "bobo_channel_new");
  137. REQUIRE(!swclt_store_update(store, &netcast_rqu));
  138. REQUIRE(ks_hash_count(store_ctx->protocols) == 3);
  139. {
  140. blade_protocol_t *protocol = (blade_protocol_t *)ks_hash_search(store_ctx->protocols, "bobo_protocol_new", KS_UNLOCKED);
  141. REQUIRE(!strcmp(protocol->name, "bobo_protocol_new"));
  142. /* Should have one channel in it called something silly */
  143. REQUIRE(ks_json_get_array_size(protocol->channels) == 1);
  144. {
  145. blade_channel_t *channel;
  146. REQUIRE(!BLADE_CHANNEL_PARSE(pool, ks_json_get_array_item(protocol->channels, 0), &channel));
  147. REQUIRE(!strcmp(channel->name, "bobo_channel_new"));
  148. BLADE_CHANNEL_DESTROY(&channel);
  149. }
  150. }
  151. ks_json_delete(&netcast_rqu.params);
  152. /* And remove it, should also remove the protocol */
  153. netcast_rqu = __netcast_route_remove_request(pool, new_route_nodeid_str);
  154. REQUIRE(!swclt_store_update(store, &netcast_rqu));
  155. REQUIRE(ks_hash_count(store_ctx->routes) == 2);
  156. REQUIRE(ks_hash_count(store_ctx->protocols) == 2);
  157. ks_json_delete(&netcast_rqu.params);
  158. swclt_store_put(&store_ctx);
  159. ks_handle_destroy(&store);
  160. }
  161. void test_nodestore_protocol_select(ks_pool_t *pool)
  162. {
  163. }
  164. void test_nodestore(ks_pool_t *pool)
  165. {
  166. test_nodestore_update(pool);
  167. test_nodestore_protocol_select(pool);
  168. }