Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2011 Collabora Ltd
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@collabora.co.uk>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr-record.h"
25 : : #include "gcr/gcr-gnupg-key.h"
26 : : #include "gcr/gcr-gnupg-records.h"
27 : :
28 : : #include "egg/egg-testing.h"
29 : :
30 : : #include <glib.h>
31 : :
32 : : #include <errno.h>
33 : : #include <stdlib.h>
34 : : #include <string.h>
35 : :
36 : : typedef struct {
37 : : GPtrArray *records;
38 : : GPtrArray *pubset;
39 : : GPtrArray *secset;
40 : : GcrGnupgKey *key;
41 : : } Test;
42 : :
43 : : static void
44 : 10 : setup (Test *test, gconstpointer unused)
45 : : {
46 : : GPtrArray *records;
47 : :
48 : 10 : records = g_ptr_array_new_with_free_func (_gcr_record_free);
49 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("pub:f:1024:17:6C7EE1B8621CC013:899817715:1055898235::m:::scESC:", -1));
50 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("fpr:::::::::ECAF7590EB3443B5C7CF3ACB6C7EE1B8621CC013:", -1));
51 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:f::::::::Werner Koch <wk@g10code.com>:\n", -1));
52 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:f::::::::Werner Koch <wk@gnupg.org>:\n", -1));
53 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("sub:f:1536:16:06AD222CADF6A6E1:919537416:1036177416:::::e:\n", -1));
54 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("fpr:::::::::CF8BCC4B18DE08FCD8A1615906AD222CADF6A6E1:\n", -1));
55 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("sub:r:1536:20:5CE086B5B5A18FF4:899817788:1025961788:::::esc:\n", -1));
56 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("fpr:::::::::AB059359A3B81F410FCFF97F5CE086B5B5A18FF4:", -1));
57 : 10 : test->records = records;
58 : :
59 : 10 : test->key = _gcr_gnupg_key_new (records, NULL);
60 : 10 : g_object_add_weak_pointer (G_OBJECT (test->key), (gpointer *)&test->key);
61 : :
62 : 10 : records = g_ptr_array_new_with_free_func (_gcr_record_free);
63 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("pub:u:2048:1:4842D952AFC000FD:1305189489:::u:::scESC:", -1));
64 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:u::::1305189849::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb@example.com>:", -1));
65 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:u::::1305189489::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1@example.com>:", -1));
66 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("sub:u:2048:1:4852132BBED15014:1305189489::::::e:", -1));
67 : 10 : test->pubset = records;
68 : :
69 : 10 : records = g_ptr_array_new_with_free_func (_gcr_record_free);
70 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("sec::2048:1:4842D952AFC000FD:1305189489::::::::::", -1));
71 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:::::::D449F1605254754B0BBFA424FC34E50609103BBB::Test Number 1 (unlimited) <test-number-1@example.com>:", -1));
72 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("uid:::::::D0A8FA7B15DC4BE3F8F03A49C372F2718C78AFC0::Dr. Strangelove <lovingbomb@example.com>:", -1));
73 : 10 : g_ptr_array_add (records, _gcr_record_parse_colons ("ssb::2048:1:4852132BBED15014:1305189489::::::::::", -1));
74 : 10 : test->secset = records;
75 : 10 : }
76 : :
77 : : static void
78 : 10 : teardown (Test *test, gconstpointer unused)
79 : : {
80 : 10 : g_object_unref (test->key);
81 [ - + ]: 10 : egg_test_wait_for_gtask_thread (test->key);
82 [ - + ]: 10 : g_assert (test->key == NULL);
83 : :
84 : 10 : g_ptr_array_unref (test->records);
85 : 10 : g_ptr_array_unref (test->pubset);
86 : 10 : g_ptr_array_unref (test->secset);
87 : 10 : }
88 : :
89 : : static void
90 : 1 : test_label (Test *test, gconstpointer unused)
91 : : {
92 : : gchar *label;
93 : :
94 : 1 : g_object_get (test->key, "label", &label, NULL);
95 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Werner Koch <wk@g10code.com>");
96 : :
97 : 1 : g_free (label);
98 : 1 : }
99 : :
100 : : static void
101 : 1 : test_markup (Test *test, gconstpointer unused)
102 : : {
103 : : gchar *markup;
104 : :
105 : 1 : g_object_get (test->key, "markup", &markup, NULL);
106 [ - + ]: 1 : g_assert_cmpstr (markup, ==, "Werner Koch\n<small>wk@g10code.com</small>");
107 : :
108 : 1 : g_free (markup);
109 : 1 : }
110 : :
111 : : static void
112 : 1 : test_description (Test *test, gconstpointer unused)
113 : : {
114 : : gchar *description;
115 : :
116 : 1 : g_object_get (test->key, "description", &description, NULL);
117 [ - + ]: 1 : g_assert_cmpstr (description, ==, "PGP Key");
118 : :
119 : 1 : g_free (description);
120 : 1 : }
121 : :
122 : : static void
123 : 1 : test_records (Test *test, gconstpointer unused)
124 : : {
125 : : GPtrArray *records;
126 : :
127 : 1 : g_object_get (test->key, "public-records", &records, NULL);
128 [ - + ]: 1 : g_assert (records == test->records);
129 : :
130 : 1 : _gcr_gnupg_key_set_public_records (test->key, records);
131 [ - + ]: 1 : g_assert (records == _gcr_gnupg_key_get_public_records (test->key));
132 : :
133 : 1 : g_ptr_array_unref (records);
134 : 1 : }
135 : :
136 : : static void
137 : 1 : test_keyid (Test *test, gconstpointer unused)
138 : : {
139 : : gchar *keyid;
140 : :
141 : 1 : g_object_get (test->key, "keyid", &keyid, NULL);
142 [ - + ]: 1 : g_assert_cmpstr (keyid, ==, "6C7EE1B8621CC013");
143 : :
144 : 1 : g_free (keyid);
145 : :
146 [ - + ]: 1 : g_assert_cmpstr (_gcr_gnupg_key_get_keyid (test->key), ==, "6C7EE1B8621CC013");
147 : 1 : }
148 : :
149 : : static void
150 : 1 : test_short_keyid (Test *test, gconstpointer unused)
151 : : {
152 : : gchar *keyid;
153 : :
154 : 1 : g_object_get (test->key, "short-keyid", &keyid, NULL);
155 [ - + ]: 1 : g_assert_cmpstr (keyid, ==, "621CC013");
156 : :
157 : 1 : g_free (keyid);
158 : 1 : }
159 : :
160 : : static void
161 : 1 : test_keyid_for_records (Test *test, gconstpointer unused)
162 : : {
163 : : const gchar *keyid;
164 : :
165 : 1 : keyid = _gcr_gnupg_records_get_keyid (test->records);
166 [ - + ]: 1 : g_assert_cmpstr (keyid, ==, "6C7EE1B8621CC013");
167 : 1 : }
168 : :
169 : : static void
170 : 1 : test_with_secret (Test *test, gconstpointer unused)
171 : : {
172 : : GcrGnupgKey *key;
173 : : GPtrArray *secset;
174 : :
175 : 1 : key = _gcr_gnupg_key_new (test->pubset, test->secset);
176 [ - + + - : 1 : g_assert (GCR_IS_GNUPG_KEY (key));
+ - - + ]
177 : :
178 : 1 : g_object_get (key, "secret-records", &secset, NULL);
179 [ - + ]: 1 : g_assert (secset == _gcr_gnupg_key_get_secret_records (key));
180 : 1 : g_object_set (key, "secret-records", secset, NULL);
181 : 1 : g_ptr_array_unref (secset);
182 : :
183 : 1 : g_object_unref (key);
184 : 1 : }
185 : :
186 : : static void
187 : 1 : test_no_change_keyid (Test *test, gconstpointer unused)
188 : : {
189 [ - + ]: 1 : if (g_test_subprocess ()) {
190 : : /* Changing the keyid. This should fail with a warning */
191 : 0 : _gcr_gnupg_key_set_public_records (test->key, test->pubset);
192 : 0 : return;
193 : : }
194 : :
195 : 1 : g_test_trap_subprocess ("/gcr/gnupg-key/no_change_keyid", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
196 : 1 : g_test_trap_assert_failed ();
197 : 1 : g_test_trap_assert_stderr ("*fingerprint is no longer the same:*");
198 : : }
199 : :
200 : : static void
201 : 1 : test_secret_mismatched_keyid (Test *test, gconstpointer unused)
202 : : {
203 [ - + ]: 1 : if (g_test_subprocess ()) {
204 : : /* Different keyid for secret part. This should fail with a warning */
205 : 0 : _gcr_gnupg_key_set_secret_records (test->key, test->secset);
206 : 0 : return;
207 : : }
208 : :
209 : 1 : g_test_trap_subprocess ("/gcr/gnupg-key/secret_mismatched_keyid", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
210 : 1 : g_test_trap_assert_failed ();
211 : 1 : g_test_trap_assert_stderr ("*pub and sec parts are not the same:*");
212 : : }
213 : :
214 : : int
215 : 1 : main (int argc, char **argv)
216 : : {
217 : 1 : g_test_init (&argc, &argv, NULL);
218 : :
219 : 1 : g_test_add ("/gcr/gnupg-key/label", Test, NULL, setup, test_label, teardown);
220 : 1 : g_test_add ("/gcr/gnupg-key/description", Test, NULL, setup, test_description, teardown);
221 : 1 : g_test_add ("/gcr/gnupg-key/markup", Test, NULL, setup, test_markup, teardown);
222 : 1 : g_test_add ("/gcr/gnupg-key/records", Test, NULL, setup, test_records, teardown);
223 : 1 : g_test_add ("/gcr/gnupg-key/keyid", Test, NULL, setup, test_keyid, teardown);
224 : 1 : g_test_add ("/gcr/gnupg-key/short_keyid", Test, NULL, setup, test_short_keyid, teardown);
225 : 1 : g_test_add ("/gcr/gnupg-key/keyid_for_records", Test, NULL, setup, test_keyid_for_records, teardown);
226 : 1 : g_test_add ("/gcr/gnupg-key/with_secret", Test, NULL, setup, test_with_secret, teardown);
227 : :
228 : : /* Valgrind seems to have problems with g_test_trap_fork() */
229 [ + - ]: 1 : if (!egg_testing_on_valgrind ()) {
230 : 1 : g_test_add ("/gcr/gnupg-key/no_change_keyid", Test, NULL, setup, test_no_change_keyid, teardown);
231 : 1 : g_test_add ("/gcr/gnupg-key/secret_mismatched_keyid", Test, NULL, setup, test_secret_mismatched_keyid, teardown);
232 : : }
233 : :
234 : 1 : return g_test_run ();
235 : : }
|