nodestore.c 7.9 KB

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