test-crypto.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.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. int
  60. main (int argc, char * argv[])
  61. {
  62. g_test_init (&argc, &argv, NULL);
  63. g_test_add_func ("/crypto/hexlify", test_hexlify);
  64. g_test_add_func ("/crypto/unhexlify", test_unhexlify);
  65. g_test_add ("/crypto/decrypt/003/wrong-password", Dump, NULL,
  66. dump_003_set_up, test_decrypt_003_wrong_password, dump_003_tear_down);
  67. return g_test_run ();
  68. }