test_json.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. This file is part of GNUnet
  3. (C) 2015, 2016 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 json/test_json.c
  18. * @brief Tests for JSON conversion functions
  19. * @author Christian Grothoff <christian@grothoff.org>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_json_lib.h"
  24. /**
  25. * Test absolute time conversion from/to JSON.
  26. *
  27. * @return 0 on success
  28. */
  29. static int
  30. test_abs_time ()
  31. {
  32. json_t *j;
  33. struct GNUNET_TIME_Absolute a1;
  34. struct GNUNET_TIME_Absolute a2;
  35. struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_absolute_time (NULL,
  36. &a2),
  37. GNUNET_JSON_spec_end ()};
  38. struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL,
  39. &a2),
  40. GNUNET_JSON_spec_end ()};
  41. a1 = GNUNET_TIME_absolute_get ();
  42. GNUNET_TIME_round_abs (&a1);
  43. j = GNUNET_JSON_from_time_abs (a1);
  44. GNUNET_assert (NULL != j);
  45. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL));
  46. GNUNET_assert (a1.abs_value_us == a2.abs_value_us);
  47. json_decref (j);
  48. a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
  49. j = GNUNET_JSON_from_time_abs (a1);
  50. GNUNET_assert (NULL != j);
  51. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL));
  52. GNUNET_assert (a1.abs_value_us == a2.abs_value_us);
  53. json_decref (j);
  54. return 0;
  55. }
  56. /**
  57. * Test relative time conversion from/to JSON.
  58. *
  59. * @return 0 on success
  60. */
  61. static int
  62. test_rel_time ()
  63. {
  64. json_t *j;
  65. struct GNUNET_TIME_Relative r1;
  66. struct GNUNET_TIME_Relative r2;
  67. struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL,
  68. &r2),
  69. GNUNET_JSON_spec_end ()};
  70. struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL,
  71. &r2),
  72. GNUNET_JSON_spec_end ()};
  73. r1 = GNUNET_TIME_UNIT_SECONDS;
  74. j = GNUNET_JSON_from_time_rel (r1);
  75. GNUNET_assert (NULL != j);
  76. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL));
  77. GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
  78. json_decref (j);
  79. r1 = GNUNET_TIME_UNIT_FOREVER_REL;
  80. j = GNUNET_JSON_from_time_rel (r1);
  81. GNUNET_assert (NULL != j);
  82. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL));
  83. GNUNET_assert (r1.rel_value_us == r2.rel_value_us);
  84. json_decref (j);
  85. return 0;
  86. }
  87. /**
  88. * Test raw (binary) conversion from/to JSON.
  89. *
  90. * @return 0 on success
  91. */
  92. static int
  93. test_raw ()
  94. {
  95. char blob[256];
  96. unsigned int i;
  97. json_t *j;
  98. for (i = 0; i <= 256; i++)
  99. {
  100. char blob2[256];
  101. struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL,
  102. blob2,
  103. i),
  104. GNUNET_JSON_spec_end ()};
  105. memset (blob, i, i);
  106. j = GNUNET_JSON_from_data (blob, i);
  107. GNUNET_assert (NULL != j);
  108. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL));
  109. GNUNET_assert (0 == memcmp (blob, blob2, i));
  110. }
  111. return 0;
  112. }
  113. /**
  114. * Test rsa conversions from/to JSON.
  115. *
  116. * @return 0 on success
  117. */
  118. static int
  119. test_rsa ()
  120. {
  121. struct GNUNET_CRYPTO_RsaPublicKey *pub;
  122. struct GNUNET_CRYPTO_RsaPublicKey *pub2;
  123. struct GNUNET_JSON_Specification pspec[] =
  124. {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()};
  125. struct GNUNET_CRYPTO_RsaSignature *sig;
  126. struct GNUNET_CRYPTO_RsaSignature *sig2;
  127. struct GNUNET_JSON_Specification sspec[] =
  128. {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()};
  129. struct GNUNET_CRYPTO_RsaPrivateKey *priv;
  130. struct GNUNET_HashCode msg;
  131. json_t *jp;
  132. json_t *js;
  133. priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
  134. pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
  135. memset (&msg, 42, sizeof (msg));
  136. sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg);
  137. GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub)));
  138. GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig)));
  139. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL));
  140. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL));
  141. GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2));
  142. GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2));
  143. GNUNET_CRYPTO_rsa_signature_free (sig);
  144. GNUNET_CRYPTO_rsa_signature_free (sig2);
  145. GNUNET_CRYPTO_rsa_private_key_free (priv);
  146. GNUNET_CRYPTO_rsa_public_key_free (pub);
  147. GNUNET_CRYPTO_rsa_public_key_free (pub2);
  148. return 0;
  149. }
  150. /**
  151. * Test rsa conversions from/to JSON.
  152. *
  153. * @return 0 on success
  154. */
  155. static int
  156. test_boolean ()
  157. {
  158. int b1;
  159. int b2;
  160. json_t *json;
  161. struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1",
  162. &b1),
  163. GNUNET_JSON_spec_boolean ("b2",
  164. &b2),
  165. GNUNET_JSON_spec_end ()};
  166. json = json_object ();
  167. json_object_set_new (json, "b1", json_true ());
  168. json_object_set_new (json, "b2", json_false ());
  169. GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL));
  170. GNUNET_assert (GNUNET_YES == b1);
  171. GNUNET_assert (GNUNET_NO == b2);
  172. json_object_set_new (json, "b1", json_integer (42));
  173. GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL));
  174. return 0;
  175. }
  176. int
  177. main (int argc, const char *const argv[])
  178. {
  179. GNUNET_log_setup ("test-json", "WARNING", NULL);
  180. if (0 != test_abs_time ())
  181. return 1;
  182. if (0 != test_rel_time ())
  183. return 1;
  184. if (0 != test_raw ())
  185. return 1;
  186. if (0 != test_rsa ())
  187. return 1;
  188. if (0 != test_boolean ())
  189. return 1;
  190. /* FIXME: test EdDSA signature conversion... */
  191. return 0;
  192. }
  193. /* end of test_json.c */