confdb_test_agent.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright (c) 2008, 2009 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45. #include <sys/un.h>
  46. #include <syslog.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/confdb.h>
  49. #include "common_test_agent.h"
  50. #define OK_STR "OK"
  51. #define FAIL_STR "FAIL"
  52. #define NOT_SUPPORTED_STR "NOT_SUPPORTED"
  53. #define INCDEC_VALUE 45
  54. confdb_callbacks_t callbacks = {
  55. .confdb_key_change_notify_fn = NULL,
  56. .confdb_object_create_change_notify_fn = NULL,
  57. .confdb_object_delete_change_notify_fn = NULL
  58. };
  59. typedef enum {
  60. NTF_OBJECT_CREATED,
  61. NTF_OBJECT_DELETED,
  62. NTF_KEY_CREATED,
  63. NTF_KEY_REPLACED,
  64. NTF_KEY_DELETED,
  65. NTF_NONE,
  66. } ntf_callback_type_t;
  67. static ntf_callback_type_t callback_type;
  68. static char ntf_object_name[256];
  69. static size_t ntf_object_name_len;
  70. static char ntf_key_name[256];
  71. static size_t ntf_key_name_len;
  72. static char ntf_key_value[256];
  73. static size_t ntf_key_value_len;
  74. static void ta_key_change_notify (
  75. confdb_handle_t handle,
  76. confdb_change_type_t change_type,
  77. hdb_handle_t parent_object_handle,
  78. hdb_handle_t object_handle,
  79. const void *object_name,
  80. size_t object_name_len,
  81. const void *key_name,
  82. size_t key_name_len,
  83. const void *key_value,
  84. size_t key_value_len)
  85. {
  86. switch (change_type) {
  87. case OBJECT_KEY_CREATED:
  88. callback_type = NTF_KEY_CREATED;
  89. break;
  90. case OBJECT_KEY_DELETED:
  91. callback_type = NTF_KEY_DELETED;
  92. break;
  93. case OBJECT_KEY_REPLACED:
  94. callback_type = NTF_KEY_REPLACED;
  95. break;
  96. default:
  97. assert (0);
  98. break;
  99. }
  100. ntf_object_name_len = object_name_len;
  101. memcpy (ntf_object_name, object_name, object_name_len);
  102. ntf_key_name_len = key_name_len;
  103. memcpy (ntf_key_name, key_name, key_name_len);
  104. ntf_key_value_len = key_value_len;
  105. memcpy (ntf_key_value, key_value, key_value_len);
  106. }
  107. static void ta_object_create_notify (
  108. confdb_handle_t handle,
  109. hdb_handle_t parent_object_handle,
  110. hdb_handle_t object_handle,
  111. const void *name_pt,
  112. size_t name_len)
  113. {
  114. callback_type = NTF_OBJECT_CREATED;
  115. ntf_object_name_len = name_len;
  116. memcpy (ntf_object_name, name_pt, name_len);
  117. }
  118. static void ta_object_delete_notify (
  119. confdb_handle_t handle,
  120. hdb_handle_t parent_object_handle,
  121. const void *name_pt,
  122. size_t name_len)
  123. {
  124. callback_type = NTF_OBJECT_DELETED;
  125. ntf_object_name_len = name_len;
  126. memcpy (ntf_object_name, name_pt, name_len);
  127. }
  128. confdb_callbacks_t valid_callbacks = {
  129. .confdb_key_change_notify_fn = ta_key_change_notify,
  130. .confdb_object_create_change_notify_fn = ta_object_create_notify,
  131. .confdb_object_delete_change_notify_fn = ta_object_delete_notify
  132. };
  133. static void set_get_test (int sock)
  134. {
  135. confdb_handle_t handle;
  136. char response[100];
  137. int res;
  138. hdb_handle_t object_handle;
  139. confdb_value_types_t type;
  140. char key_value[256];
  141. char key2_value[256];
  142. size_t value_len;
  143. size_t value2_len;
  144. syslog (LOG_ERR, "%s START", __func__);
  145. snprintf (response, 100, "%s", OK_STR);
  146. res = confdb_initialize (&handle, &callbacks);
  147. if (res != CS_OK) {
  148. syslog (LOG_ERR, "Could not initialize confdb error %d", res);
  149. goto send_response;
  150. }
  151. /* Add a scratch object and put 2 keys into it */
  152. res = confdb_object_create (handle, OBJECT_PARENT_HANDLE,
  153. "testconfdb", strlen("testconfdb"), &object_handle);
  154. if (res != CS_OK) {
  155. syslog (LOG_ERR, "error creating 'testconfdb' object: %d", res);
  156. goto send_response;
  157. }
  158. res = confdb_key_create (handle, object_handle,
  159. "testkey", strlen ("testkey"),
  160. "one", strlen ("one"));
  161. if (res != CS_OK) {
  162. syslog (LOG_ERR, "error creating 'testconfdb' key 1: %d", res);
  163. goto send_response;
  164. }
  165. res = confdb_key_replace (handle, object_handle,
  166. "testkey", strlen ("testkey"),
  167. "one", strlen ("one"),
  168. "newone", strlen ("newone"));
  169. if (res != CS_OK) {
  170. syslog (LOG_ERR, "error replace 'testconfdb' key 2: %d", res);
  171. goto send_response;
  172. }
  173. res = confdb_key_get_typed (handle, object_handle,
  174. "testkey", key_value, &value_len, &type);
  175. if (res != CS_OK) {
  176. syslog (LOG_ERR, "Could not get \"testkey\" key: %d", res);
  177. goto send_response;
  178. }
  179. if (strcmp (key_value, "newone") != 0) {
  180. syslog (LOG_ERR, "Key not set correctly");
  181. goto send_response;
  182. }
  183. if (type != CONFDB_VALUETYPE_ANY) {
  184. syslog (LOG_ERR, "Key type not set correctly");
  185. goto send_response;
  186. }
  187. res = confdb_key_get (handle, object_handle,
  188. "testkey", strlen ("testkey"), key2_value, &value2_len);
  189. if (res != CS_OK) {
  190. syslog (LOG_ERR, "Could not get \"testkey\" key: %d", res);
  191. goto send_response;
  192. }
  193. if (value2_len != value_len) {
  194. syslog (LOG_ERR, "value length from confdb_key_get:%u and confdb_key_get_typed:%u differ.",
  195. (uint32_t)value_len, (uint32_t)value2_len);
  196. goto send_response;
  197. }
  198. res = confdb_key_delete (handle, object_handle,
  199. "testkey", strlen ("testkey"), key2_value, value2_len);
  200. if (res != CS_OK) {
  201. syslog (LOG_ERR, "Could not get \"testkey\" key: %d", res);
  202. goto send_response;
  203. }
  204. /* Remove it.
  205. Check that it doesn't exist when the full tree dump runs next */
  206. res = confdb_object_destroy(handle, object_handle);
  207. if (res != CS_OK) {
  208. syslog (LOG_ERR, "error destroying 'testconfdb' object: %d", res);
  209. goto send_response;
  210. }
  211. snprintf (response, 100, "%s", OK_STR);
  212. send_response:
  213. syslog (LOG_ERR, "%s %s", __func__, response);
  214. send (sock, response, strlen (response) + 1, 0);
  215. confdb_finalize (handle);
  216. }
  217. static void increment_decrement_test (int sock)
  218. {
  219. char response[100];
  220. int res;
  221. uint32_t incdec_value;
  222. hdb_handle_t object_handle;
  223. confdb_handle_t handle;
  224. confdb_handle_t par_handle;
  225. snprintf (response, 100, "%s", FAIL_STR);
  226. res = confdb_initialize (&handle, &callbacks);
  227. if (res != CS_OK) {
  228. syslog (LOG_ERR, "Could not initialize confdb error %d", res);
  229. goto send_response;
  230. }
  231. /* Add a scratch object and put 1 keys into it */
  232. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE,
  233. "testconfdb", strlen("testconfdb"), &object_handle);
  234. if (res != CS_OK) {
  235. syslog (LOG_ERR, "error creating 'testconfdb' object: %d", res);
  236. goto send_response;
  237. }
  238. res = confdb_object_parent_get (handle, object_handle, &par_handle);
  239. if (res != CS_OK) {
  240. syslog (LOG_ERR, "error getting parent of 'testconfdb' object: %d", res);
  241. goto send_response;
  242. }
  243. if (par_handle != OBJECT_PARENT_HANDLE) {
  244. syslog (LOG_ERR, "wrong parent handle");
  245. goto send_response;
  246. }
  247. incdec_value = INCDEC_VALUE;
  248. res = confdb_key_create_typed (handle, object_handle, "incdec",
  249. &incdec_value, sizeof(incdec_value), CONFDB_VALUETYPE_UINT32);
  250. if (res != CS_OK) {
  251. syslog (LOG_ERR, "error creating 'testconfdb' key 4: %d\n", res);
  252. goto send_response;
  253. }
  254. res = confdb_key_increment(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  255. if (res != CS_OK) {
  256. syslog (LOG_ERR, "error incrementing 'testconfdb' key 4: %d\n", res);
  257. goto send_response;
  258. }
  259. if (incdec_value == INCDEC_VALUE + 1) {
  260. syslog (LOG_INFO, "incremented value = %d\n", incdec_value);
  261. }
  262. else {
  263. syslog (LOG_ERR, "ERROR: incremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE+1);
  264. goto send_response;
  265. }
  266. res = confdb_key_decrement(handle, object_handle, "incdec", strlen("incdec"), &incdec_value);
  267. if (res != CS_OK) {
  268. syslog (LOG_ERR, "error decrementing 'testconfdb' key 4: %d\n", res);
  269. goto send_response;
  270. }
  271. if (incdec_value == INCDEC_VALUE) {
  272. syslog (LOG_ERR, "decremented value = %d\n", incdec_value);
  273. }
  274. else {
  275. syslog (LOG_ERR, "ERROR: decremented value = %d (should be %d)\n", incdec_value, INCDEC_VALUE);
  276. goto send_response;
  277. }
  278. /* Remove it.
  279. Check that it doesn't exist when the full tree dump runs next */
  280. res = confdb_object_destroy(handle, object_handle);
  281. if (res != CS_OK) {
  282. syslog (LOG_ERR, "error destroying 'testconfdb' object: %d\n", res);
  283. goto send_response;
  284. }
  285. snprintf (response, 100, "%s", OK_STR);
  286. send_response:
  287. confdb_finalize (handle);
  288. send (sock, response, strlen (response) + 1, 0);
  289. }
  290. static void object_find_test (int sock)
  291. {
  292. char response[100];
  293. confdb_handle_t handle;
  294. int result;
  295. hdb_handle_t totem_handle;
  296. char key_value[256];
  297. size_t value_len;
  298. snprintf (response, 100, "%s", FAIL_STR);
  299. result = confdb_initialize (&handle, &callbacks);
  300. if (result != CS_OK) {
  301. syslog (LOG_ERR, "Could not initialize confdb error %d\n", result);
  302. goto send_response;
  303. }
  304. /* Find "totem" and dump bits of it again, to test the direct APIs */
  305. result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
  306. if (result != CS_OK) {
  307. syslog (LOG_ERR, "Could not start object_find %d\n", result);
  308. goto send_response;
  309. }
  310. result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
  311. if (result != CS_OK) {
  312. syslog (LOG_ERR, "Could not object_find \"totem\": %d\n", result);
  313. goto send_response;
  314. }
  315. result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
  316. if (result != CS_OK) {
  317. syslog (LOG_ERR, "Could not get \"version\" key: %d\n", result);
  318. goto send_response;
  319. }
  320. result = confdb_object_find_destroy (handle, OBJECT_PARENT_HANDLE);
  321. if (result != CS_OK) {
  322. syslog (LOG_ERR, "Could not destroy find object %d\n", result);
  323. goto send_response;
  324. }
  325. snprintf (response, 100, "%s", OK_STR);
  326. send_response:
  327. confdb_finalize (handle);
  328. send (sock, response, strlen (response) + 1, 0);
  329. }
  330. static void notification_test (int sock)
  331. {
  332. char response[100];
  333. confdb_handle_t handle;
  334. int res;
  335. hdb_handle_t object_handle;
  336. hdb_handle_t new_object_handle;
  337. uint16_t incdec_value;
  338. uint32_t incdec_value_out;
  339. snprintf (response, 100, "%s", FAIL_STR);
  340. res = confdb_initialize (&handle, &valid_callbacks);
  341. if (res != CS_OK) {
  342. syslog (LOG_ERR, "Could not initialize confdb error %d\n", res);
  343. goto send_response;
  344. }
  345. /* Add a base scratch object (we don't want to track the parent object) */
  346. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE,
  347. "testconfdb", strlen("testconfdb"), &object_handle);
  348. if (res != CS_OK) {
  349. syslog (LOG_ERR, "error creating 'testconfdb' object: %d", res);
  350. goto send_response;
  351. }
  352. res = confdb_track_changes (handle, object_handle, 1 /*OBJECT_TRACK_DEPTH_RECURSIVE*/);
  353. if (res != CS_OK) {
  354. syslog (LOG_ERR, "can't track changes on object: %d", res);
  355. goto send_response;
  356. }
  357. /* Test 'object created' notification
  358. */
  359. callback_type = NTF_NONE;
  360. res = confdb_object_create(handle, object_handle,
  361. "duck", strlen("duck"), &new_object_handle);
  362. if (res != CS_OK) {
  363. syslog (LOG_ERR, "error creating 'duck' object: %d", res);
  364. goto send_response;
  365. }
  366. confdb_dispatch (handle, CS_DISPATCH_ALL);
  367. if (callback_type != NTF_OBJECT_CREATED) {
  368. syslog (LOG_ERR, "no notification received for the creation of 'duck'");
  369. goto send_response;
  370. }
  371. if (strcmp ("duck", ntf_object_name) != 0) {
  372. syslog (LOG_ERR, "expected notification for 'duck' but got %s", ntf_object_name);
  373. goto send_response;
  374. }
  375. /* Test 'key created' notification
  376. */
  377. callback_type = NTF_NONE;
  378. incdec_value = INCDEC_VALUE;
  379. res = confdb_key_create_typed (handle, new_object_handle, "incdec",
  380. &incdec_value, sizeof(incdec_value), CONFDB_VALUETYPE_UINT16);
  381. if (res != CS_OK) {
  382. syslog (LOG_ERR, "error creating 'testconfdb' key 4: %d\n", res);
  383. goto send_response;
  384. }
  385. confdb_dispatch (handle, CS_DISPATCH_ALL);
  386. if (callback_type != NTF_KEY_CREATED) {
  387. syslog (LOG_ERR, "no notification received for the creation of key 'incdec'");
  388. goto send_response;
  389. }
  390. if (strcmp ("incdec", ntf_key_name) != 0) {
  391. syslog (LOG_ERR, "expected notification for 'incdec' but got %s", ntf_key_name);
  392. goto send_response;
  393. }
  394. /* Test 'key replaced' notification
  395. */
  396. callback_type = NTF_NONE;
  397. res = confdb_key_increment(handle, new_object_handle, "incdec", strlen("incdec"), &incdec_value_out);
  398. if (res != CS_OK) {
  399. syslog (LOG_ERR, "error incrementing 'testconfdb' key 4: %d\n", res);
  400. goto send_response;
  401. }
  402. confdb_dispatch (handle, CS_DISPATCH_ALL);
  403. if (callback_type != NTF_KEY_REPLACED) {
  404. syslog (LOG_ERR, "no notification received for the incrementing of key 'incdec'");
  405. goto send_response;
  406. }
  407. if (strcmp ("incdec", ntf_key_name) != 0) {
  408. syslog (LOG_ERR, "expected notification for 'incdec' but got %s", ntf_key_name);
  409. goto send_response;
  410. }
  411. /* Test 'key destroyed' notification
  412. */
  413. callback_type = NTF_NONE;
  414. res = confdb_key_delete (handle, new_object_handle,
  415. "incdec", strlen ("incdec"), ntf_key_value, ntf_key_value_len);
  416. if (res != CS_OK) {
  417. syslog (LOG_ERR, "Could not delete \"incdec\" key: %d", res);
  418. goto send_response;
  419. }
  420. confdb_dispatch (handle, CS_DISPATCH_ALL);
  421. if (callback_type != NTF_KEY_DELETED) {
  422. syslog (LOG_ERR, "no notification received for the deletion of key 'incdec'");
  423. goto send_response;
  424. }
  425. if (strcmp ("incdec", ntf_key_name) != 0) {
  426. syslog (LOG_ERR, "expected notification for 'incdec' but got %s", ntf_key_name);
  427. goto send_response;
  428. }
  429. /* Test 'object destroyed' notification
  430. */
  431. callback_type = NTF_NONE;
  432. res = confdb_object_destroy(handle, new_object_handle);
  433. if (res != CS_OK) {
  434. syslog (LOG_ERR, "error destroying 'testconfdb' object: %d", res);
  435. goto send_response;
  436. }
  437. confdb_dispatch (handle, CS_DISPATCH_ALL);
  438. if (callback_type != NTF_OBJECT_DELETED) {
  439. syslog (LOG_ERR, "no notification received for the deletion of 'duck'");
  440. goto send_response;
  441. }
  442. if (strcmp ("duck", ntf_object_name) != 0) {
  443. syslog (LOG_ERR, "expected notification for 'duck' but got %s", ntf_object_name);
  444. goto send_response;
  445. }
  446. confdb_stop_track_changes (handle);
  447. confdb_object_destroy(handle, object_handle);
  448. snprintf (response, 100, "%s", OK_STR);
  449. send_response:
  450. send (sock, response, strlen (response) + 1, 0);
  451. confdb_finalize (handle);
  452. }
  453. static void do_command (int sock, char* func, char*args[], int num_args)
  454. {
  455. char response[100];
  456. if (parse_debug)
  457. syslog (LOG_DEBUG,"RPC:%s() called.", func);
  458. if (strcmp ("set_get_test", func) == 0) {
  459. set_get_test (sock);
  460. } else if (strcmp ("increment_decrement_test", func) == 0) {
  461. increment_decrement_test (sock);
  462. } else if (strcmp ("object_find_test", func) == 0) {
  463. object_find_test (sock);
  464. } else if (strcmp ("notification_test", func) == 0) {
  465. notification_test (sock);
  466. } else {
  467. syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
  468. snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
  469. send (sock, response, strlen (response) + 1, 0);
  470. }
  471. }
  472. int main (int argc, char *argv[])
  473. {
  474. int ret;
  475. openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
  476. syslog (LOG_ERR, "confdb_test_agent STARTING");
  477. parse_debug = 1;
  478. ret = test_agent_run (9035, do_command);
  479. syslog (LOG_ERR, "confdb_test_agent EXITING");
  480. return ret;
  481. }