test_identity_defaults.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file identity/test_identity.c
  18. * @brief testcase for identity service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_identity_service.h"
  24. #include "gnunet_testing_lib.h"
  25. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  26. /**
  27. * Return value from 'main'.
  28. */
  29. static int res;
  30. /**
  31. * Handle to identity service.
  32. */
  33. static struct GNUNET_IDENTITY_Handle *h;
  34. /**
  35. * Handle to identity operation.
  36. */
  37. static struct GNUNET_IDENTITY_Operation *op;
  38. /**
  39. * Handle for task for timeout termination.
  40. */
  41. static struct GNUNET_SCHEDULER_Task *endbadly_task;
  42. #define CHECK(cond) \
  43. do \
  44. { \
  45. if (! (cond)) \
  46. { \
  47. GNUNET_break (0); \
  48. end (); \
  49. return; \
  50. } \
  51. } while (0)
  52. /**
  53. * Clean up all resources used.
  54. */
  55. static void
  56. cleanup (void *cls)
  57. {
  58. (void) cls;
  59. if (NULL != op)
  60. {
  61. GNUNET_IDENTITY_cancel (op);
  62. op = NULL;
  63. }
  64. if (NULL != h)
  65. {
  66. GNUNET_IDENTITY_disconnect (h);
  67. h = NULL;
  68. }
  69. }
  70. /**
  71. * Termiante the testcase (failure).
  72. *
  73. * @param cls NULL
  74. */
  75. static void
  76. endbadly (void *cls)
  77. {
  78. GNUNET_SCHEDULER_shutdown ();
  79. res = 1;
  80. }
  81. /**
  82. * Termiante the testcase.
  83. */
  84. static void
  85. end ()
  86. {
  87. if (NULL != endbadly_task)
  88. {
  89. GNUNET_SCHEDULER_cancel (endbadly_task);
  90. endbadly_task = NULL;
  91. }
  92. GNUNET_SCHEDULER_shutdown ();
  93. }
  94. /**
  95. * Continuation called from successful delete operation.
  96. *
  97. * @param cls NULL
  98. * @param emsg (should also be NULL)
  99. */
  100. static void
  101. delete_cont (void *cls, const char *emsg)
  102. {
  103. op = NULL;
  104. CHECK (NULL == emsg);
  105. res = 0;
  106. end ();
  107. }
  108. /**
  109. * Continuation called from expected-to-fail rename operation.
  110. *
  111. * @param cls NULL
  112. * @param emsg (should also be NULL)
  113. */
  114. static void
  115. get_cb (void *cls,
  116. struct GNUNET_IDENTITY_Ego *ego,
  117. void **ctx,
  118. const char *identifier)
  119. {
  120. op = NULL;
  121. CHECK (NULL != ego);
  122. CHECK (NULL != identifier);
  123. CHECK (0 == strcmp (identifier, "test-id"));
  124. op = GNUNET_IDENTITY_delete (h, "test-id", &delete_cont, NULL);
  125. }
  126. /**
  127. * Called with events about egos.
  128. *
  129. * @param cls NULL
  130. * @param ego ego handle
  131. * @param ego_ctx context for application to store data for this ego
  132. * (during the lifetime of this process, initially NULL)
  133. * @param identifier identifier assigned by the user for this ego,
  134. * NULL if the user just deleted the ego and it
  135. * must thus no longer be used
  136. */
  137. static void
  138. dummy_cb (void *cls,
  139. struct GNUNET_IDENTITY_Ego *ego,
  140. void **ctx,
  141. const char *identifier)
  142. {
  143. (void) cls;
  144. (void) ego;
  145. (void) ctx;
  146. (void) identifier;
  147. }
  148. /**
  149. * Main function of the test, run from scheduler.
  150. *
  151. * @param cls NULL
  152. * @param cfg configuration we use (also to connect to identity service)
  153. * @param peer handle to access more of the peer (not used)
  154. */
  155. static void
  156. run_get (void *cls,
  157. const struct GNUNET_CONFIGURATION_Handle *cfg,
  158. struct GNUNET_TESTING_Peer *peer)
  159. {
  160. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
  161. GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
  162. h = GNUNET_IDENTITY_connect (cfg, &dummy_cb, NULL);
  163. CHECK (NULL != h);
  164. op = GNUNET_IDENTITY_get (h, "test-service", &get_cb, NULL);
  165. }
  166. /**
  167. * Continuation called from successful rename operation.
  168. *
  169. * @param cls NULL
  170. * @param emsg (should also be NULL)
  171. */
  172. static void
  173. success_set_cont (void *cls, const char *emsg)
  174. {
  175. op = NULL;
  176. CHECK (NULL == emsg);
  177. end ();
  178. }
  179. /**
  180. * Called with events about egos.
  181. *
  182. * @param cls NULL
  183. * @param ego ego handle
  184. * @param ego_ctx context for application to store data for this ego
  185. * (during the lifetime of this process, initially NULL)
  186. * @param identifier identifier assigned by the user for this ego,
  187. * NULL if the user just deleted the ego and it
  188. * must thus no longer be used
  189. */
  190. static void
  191. notification_cb (void *cls,
  192. struct GNUNET_IDENTITY_Ego *ego,
  193. void **ctx,
  194. const char *identifier)
  195. {
  196. if (NULL == ego)
  197. return; /* skip first call */
  198. if (NULL == identifier)
  199. return; /* deletion / shutdown */
  200. op = GNUNET_IDENTITY_set (h, "test-service", ego, &success_set_cont, NULL);
  201. }
  202. /**
  203. * Called with events about created ego.
  204. *
  205. * @param cls NULL
  206. * @param pk private key of the ego, or NULL on error
  207. * @param emsg error message
  208. */
  209. static void
  210. create_cb (void *cls,
  211. const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk,
  212. const char *emsg)
  213. {
  214. CHECK (NULL == emsg);
  215. CHECK (NULL != pk);
  216. op = NULL;
  217. }
  218. /**
  219. * Main function of the test, run from scheduler.
  220. *
  221. * @param cls NULL
  222. * @param cfg configuration we use (also to connect to identity service)
  223. * @param peer handle to access more of the peer (not used)
  224. */
  225. static void
  226. run_set (void *cls,
  227. const struct GNUNET_CONFIGURATION_Handle *cfg,
  228. struct GNUNET_TESTING_Peer *peer)
  229. {
  230. endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
  231. GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
  232. h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
  233. CHECK (NULL != h);
  234. op = GNUNET_IDENTITY_create (h, "test-id", &create_cb, NULL);
  235. }
  236. int
  237. main (int argc, char *argv[])
  238. {
  239. GNUNET_DISK_directory_remove ("/tmp/gnunet/test-identity-service");
  240. res = 1;
  241. if (0 != GNUNET_TESTING_service_run ("test-identity-defaults",
  242. "identity",
  243. "test_identity.conf",
  244. &run_set,
  245. NULL))
  246. return 1;
  247. if (0 != GNUNET_TESTING_service_run ("test-identity-defaults",
  248. "identity",
  249. "test_identity.conf",
  250. &run_get,
  251. NULL))
  252. return 1;
  253. GNUNET_DISK_directory_remove ("/tmp/gnunet/test-identity-service");
  254. return res;
  255. }
  256. /* end of test_identity_defaults.c */