1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* unit-test-util.c: Test gkd-prompt-util.c
4
Copyright (C) 2009 Stefan Walter
6
The Gnome Keyring Library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
9
License, or (at your option) any later version.
11
The Gnome Keyring Library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public
17
License along with the Gnome Library; see the file COPYING.LIB. If not,
18
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
21
Author: Stef Walter <stef@memberwebs.com>
24
#include "run-auto-test.h"
26
#include "gkd-prompt-util.h"
28
#include <egg/egg-dh.h>
29
#include <egg/egg-libgcrypt.h>
30
#include <egg/egg-secure-memory.h>
34
static GKeyFile *key_file = NULL;
36
DEFINE_SETUP(prompt_util)
38
egg_libgcrypt_initialize ();
39
key_file = g_key_file_new ();
42
DEFINE_TEARDOWN(prompt_util)
44
g_key_file_free (key_file);
48
DEFINE_TEST(encode_decode_mpi)
50
gcry_mpi_t mpi, check;
52
mpi = gcry_mpi_new (512);
53
gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM);
55
gkd_prompt_util_encode_mpi (key_file, "section", "field", mpi);
56
if (!gkd_prompt_util_decode_mpi (key_file, "section", "field", &check))
57
g_assert_not_reached ();
59
g_assert (gcry_mpi_cmp (mpi, check) == 0);
60
gcry_mpi_release (mpi);
61
gcry_mpi_release (check);
64
DEFINE_TEST(decode_nonexistant_mpi)
68
if (gkd_prompt_util_decode_mpi (key_file, "nonexist", "nope", &mpi))
69
g_assert_not_reached ();
72
DEFINE_TEST(encode_decode_hex)
78
gcry_create_nonce (buffer, 32);
79
gkd_prompt_util_encode_hex (key_file, "section", "field", buffer, 32);
80
check = gkd_prompt_util_decode_hex (key_file, "section", "field", &n_check);
82
g_assert (n_check == 32);
83
g_assert (memcmp (buffer, check, 32) == 0);
88
DEFINE_TEST(decode_nonexistant_hex)
92
if (gkd_prompt_util_decode_hex (key_file, "nonexist", "nope", &n_data))
93
g_assert_not_reached ();
97
do_encrypt_decrypt_text (const gchar *text)
104
g_test_message ("prompt encrypt/decrypt text: %s", text);
106
/* Test making a key */
108
key = egg_secure_alloc (n_key);
109
gcry_randomize (key, n_key, GCRY_WEAK_RANDOM);
111
gcry_create_nonce (iv, 16);
112
enc = gkd_prompt_util_encrypt_text (key, n_key, iv, 16, text, &n_enc);
115
/* Always greater due to null term */
116
g_assert (n_enc > strlen (text));
117
g_assert (n_enc % 16 == 0);
119
check = gkd_prompt_util_decrypt_text (key, n_key, iv, 16, enc, n_enc);
120
egg_secure_clear (key, n_key);
121
egg_secure_free (key);
125
g_assert (strlen (check) < n_enc);
126
g_assert_cmpstr (check, ==, text);
129
DEFINE_TEST(encrypt_decrypt_text)
131
do_encrypt_decrypt_text ("");
132
do_encrypt_decrypt_text ("blah");
133
do_encrypt_decrypt_text ("0123456789ABCDEF");
134
do_encrypt_decrypt_text ("0123456789ABCDE");
135
do_encrypt_decrypt_text ("0123456789ABCDEF 12345");