test-crypto.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <glib.h>
  2. #include <gio/gio.h>
  3. #include <string.h>
  4. #include <json-glib/json-glib.h>
  5. #include "iridium-crypto.h"
  6. #include "iridium-standard-file.h"
  7. typedef struct {
  8. JsonParser *parser;
  9. } Dump;
  10. static void
  11. dump_003_set_up (Dump *dump, gconstpointer user_data)
  12. {
  13. GBytes *data;
  14. GError *error = NULL;
  15. data = g_resources_lookup_data ("/net/bloerg/Test/dumps/003.encrypted.json",
  16. G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
  17. g_assert_nonnull (data);
  18. dump->parser = json_parser_new_immutable ();
  19. g_assert (json_parser_load_from_data (dump->parser, g_bytes_get_data (data, NULL),
  20. g_bytes_get_size(data), &error));
  21. g_assert_null (error);
  22. g_bytes_unref (data);
  23. }
  24. static void
  25. dump_003_tear_down (Dump *dump, gconstpointer user_data)
  26. {
  27. g_object_unref (dump->parser);
  28. }
  29. static void
  30. test_hexlify (void)
  31. {
  32. guint8 data[] = { 'A', '1', 'x', ';' };
  33. gchar *hexlified = iridium_crypto_hexlify (data, 4);
  34. g_assert_cmpstr (hexlified, ==, "4131783b");
  35. g_free (hexlified);
  36. }
  37. static void
  38. test_unhexlify (void)
  39. {
  40. gchar *hexlified = "466f6f62617221";
  41. guint8 *data = iridium_crypto_unhexlify (hexlified, strlen (hexlified));
  42. g_assert_cmpstr ((gchar *) data, ==, "Foobar!");
  43. g_free (data);
  44. }
  45. static void
  46. test_decrypt_003_wrong_password (Dump *dump, gconstpointer user_data)
  47. {
  48. IridiumAuthParams *params;
  49. GList *items;
  50. GError *error = NULL;
  51. params = iridium_crypto_auth_params_new ();
  52. iridium_crypto_derive_keys (params, "foo");
  53. items = iridium_standard_file_get_items (dump->parser, params, &error);
  54. g_assert_null (items);
  55. g_assert_nonnull (error);
  56. g_assert (error->code == IRIDIUM_CRYPTO_ERROR_DECRYPT_ENCRYPTION_KEY);
  57. iridium_crypto_auth_params_free (params);
  58. }
  59. static void
  60. test_decrypt_003_correct_password (Dump *dump, gconstpointer user_data)
  61. {
  62. IridiumAuthParams *params;
  63. JsonNode *root;
  64. JsonNode *nonce_node;
  65. GList *items;
  66. GError *error = NULL;
  67. params = iridium_crypto_auth_params_new ();
  68. root = json_parser_get_root (dump->parser);
  69. g_assert_nonnull (root);
  70. nonce_node = json_path_query ("$.auth_params.pw_nonce", root, &error);
  71. g_assert_nonnull (nonce_node);
  72. g_assert_null (error);
  73. g_free (params->salt);
  74. params->salt = g_strdup(json_node_get_string(
  75. json_array_get_element(json_node_get_array(nonce_node), 0)));
  76. json_node_unref (nonce_node);
  77. iridium_crypto_derive_keys (params, "test123");
  78. items = iridium_standard_file_get_items (dump->parser, params, &error);
  79. g_assert_nonnull (items);
  80. g_assert_null (error);
  81. iridium_crypto_auth_params_free (params);
  82. }
  83. int
  84. main (int argc, char * argv[])
  85. {
  86. g_test_init (&argc, &argv, NULL);
  87. g_test_add_func ("/crypto/hexlify", test_hexlify);
  88. g_test_add_func ("/crypto/unhexlify", test_unhexlify);
  89. g_test_add ("/crypto/decrypt/003/wrong-password", Dump, NULL,
  90. dump_003_set_up, test_decrypt_003_wrong_password, dump_003_tear_down);
  91. g_test_add ("/crypto/decrypt/003/correct-password", Dump, NULL,
  92. dump_003_set_up, test_decrypt_003_correct_password, dump_003_tear_down);
  93. return g_test_run ();
  94. }