~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to daemon/prompt/tests/unit-test-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* unit-test-util.c: Test gkd-prompt-util.c
 
3
 
 
4
   Copyright (C) 2009 Stefan Walter
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
20
 
 
21
   Author: Stef Walter <stef@memberwebs.com>
 
22
*/
 
23
 
 
24
#include "run-auto-test.h"
 
25
 
 
26
#include "gkd-prompt-util.h"
 
27
 
 
28
#include <egg/egg-dh.h>
 
29
#include <egg/egg-libgcrypt.h>
 
30
#include <egg/egg-secure-memory.h>
 
31
 
 
32
#include <gcrypt.h>
 
33
 
 
34
static GKeyFile *key_file = NULL;
 
35
 
 
36
DEFINE_SETUP(prompt_util)
 
37
{
 
38
        egg_libgcrypt_initialize ();
 
39
        key_file = g_key_file_new ();
 
40
}
 
41
 
 
42
DEFINE_TEARDOWN(prompt_util)
 
43
{
 
44
        g_key_file_free (key_file);
 
45
        key_file = NULL;
 
46
}
 
47
 
 
48
DEFINE_TEST(encode_decode_mpi)
 
49
{
 
50
        gcry_mpi_t mpi, check;
 
51
 
 
52
        mpi = gcry_mpi_new (512);
 
53
        gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM);
 
54
 
 
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 ();
 
58
 
 
59
        g_assert (gcry_mpi_cmp (mpi, check) == 0);
 
60
        gcry_mpi_release (mpi);
 
61
        gcry_mpi_release (check);
 
62
}
 
63
 
 
64
DEFINE_TEST(decode_nonexistant_mpi)
 
65
{
 
66
        gcry_mpi_t mpi;
 
67
 
 
68
        if (gkd_prompt_util_decode_mpi (key_file, "nonexist", "nope", &mpi))
 
69
                g_assert_not_reached ();
 
70
}
 
71
 
 
72
DEFINE_TEST(encode_decode_hex)
 
73
{
 
74
        gchar buffer[32];
 
75
        gpointer check;
 
76
        gsize n_check;
 
77
 
 
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);
 
81
        g_assert (check);
 
82
        g_assert (n_check == 32);
 
83
        g_assert (memcmp (buffer, check, 32) == 0);
 
84
 
 
85
        g_free (check);
 
86
}
 
87
 
 
88
DEFINE_TEST(decode_nonexistant_hex)
 
89
{
 
90
        gsize n_data;
 
91
 
 
92
        if (gkd_prompt_util_decode_hex (key_file, "nonexist", "nope", &n_data))
 
93
                g_assert_not_reached ();
 
94
}
 
95
 
 
96
static void
 
97
do_encrypt_decrypt_text (const gchar *text)
 
98
{
 
99
        gpointer key, enc;
 
100
        gsize n_key, n_enc;
 
101
        guchar iv[16];
 
102
        gchar *check;
 
103
 
 
104
        g_test_message ("prompt encrypt/decrypt text: %s", text);
 
105
 
 
106
        /* Test making a key */
 
107
        n_key = 16;
 
108
        key = egg_secure_alloc (n_key);
 
109
        gcry_randomize (key, n_key, GCRY_WEAK_RANDOM);
 
110
 
 
111
        gcry_create_nonce (iv, 16);
 
112
        enc = gkd_prompt_util_encrypt_text (key, n_key, iv, 16, text, &n_enc);
 
113
 
 
114
        g_assert (enc);
 
115
        /* Always greater due to null term */
 
116
        g_assert (n_enc > strlen (text));
 
117
        g_assert (n_enc % 16 == 0);
 
118
 
 
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);
 
122
        g_free (enc);
 
123
 
 
124
        g_assert (check);
 
125
        g_assert (strlen (check) < n_enc);
 
126
        g_assert_cmpstr (check, ==, text);
 
127
}
 
128
 
 
129
DEFINE_TEST(encrypt_decrypt_text)
 
130
{
 
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");
 
136
}