test_json.c 6.8 KB

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