Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-object.c: Test GkmObject
3 :
4 : Copyright (C) 2012 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 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stefw@gnome.org>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "mock-module.h"
26 :
27 : #include "gkm/gkm-attributes.h"
28 : #include "gkm/gkm-certificate.h"
29 : #include "gkm/gkm-object.h"
30 : #include "gkm/gkm-module.h"
31 : #include "gkm/gkm-serializable.h"
32 : #include "gkm/gkm-session.h"
33 : #include "gkm/gkm-test.h"
34 :
35 : #include "egg/egg-testing.h"
36 :
37 : #include "pkcs11i.h"
38 :
39 : typedef struct {
40 : GkmModule *module;
41 : GkmSession *session;
42 : GBytes *certificate_data;
43 : GkmCertificate *certificate;
44 : } Test;
45 :
46 : static void
47 6 : setup_basic (Test* test,
48 : gconstpointer unused)
49 : {
50 : gchar *data;
51 : gsize length;
52 :
53 6 : test->module = mock_module_initialize_and_enter ();
54 6 : test->session = mock_module_open_session (TRUE);
55 :
56 6 : if (!g_file_get_contents (SRCDIR "/pkcs11/gkm/fixtures/test-certificate-1.der", &data, &length, NULL))
57 0 : g_assert_not_reached ();
58 :
59 6 : test->certificate_data = g_bytes_new_take (data, length);
60 6 : }
61 :
62 : static void
63 6 : teardown_basic (Test* test,
64 : gconstpointer unused)
65 : {
66 6 : g_bytes_unref (test->certificate_data);
67 6 : mock_module_leave_and_finalize ();
68 6 : }
69 :
70 : static GkmCertificate *
71 6 : create_certificate_object (GkmSession *session,
72 : GBytes *data)
73 : {
74 : GkmCertificate *certificate;
75 :
76 6 : certificate = g_object_new (GKM_TYPE_CERTIFICATE,
77 : "unique", "test-certificate",
78 : "module", gkm_session_get_module (session),
79 : "manager", gkm_session_get_manager (session),
80 : NULL);
81 :
82 6 : if (!gkm_serializable_load (GKM_SERIALIZABLE (certificate), NULL, data))
83 0 : g_assert_not_reached ();
84 :
85 6 : return certificate;
86 : }
87 :
88 : static void
89 6 : setup (Test *test,
90 : gconstpointer unused)
91 : {
92 6 : setup_basic (test, unused);
93 6 : test->certificate = create_certificate_object (test->session, test->certificate_data);
94 6 : }
95 :
96 : static void
97 6 : teardown (Test *test,
98 : gconstpointer unused)
99 : {
100 6 : g_clear_object (&test->certificate);
101 6 : teardown_basic (test, unused);
102 6 : }
103 :
104 : static void
105 1 : test_attribute_check_value (Test* test,
106 : gconstpointer unused)
107 : {
108 : gpointer data;
109 : gsize n_data;
110 :
111 1 : data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
112 : test->session, CKA_CHECK_VALUE, &n_data);
113 :
114 1 : egg_assert_cmpmem (data, n_data, ==, "\x36\x86\x35", 3);
115 1 : g_free (data);
116 1 : }
117 :
118 : static void
119 1 : test_attribute_issuer (Test* test,
120 : gconstpointer unused)
121 : {
122 : gpointer data;
123 : gsize n_data;
124 :
125 1 : data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
126 : test->session, CKA_ISSUER, &n_data);
127 :
128 1 : egg_assert_cmpmem (data, n_data, ==, "\x30\x81\xCF\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x5A\x41\x31\x15\x30\x13\x06\x03\x55\x04\x08\x13\x0C\x57\x65\x73\x74\x65\x72\x6E\x20\x43\x61\x70\x65\x31\x12\x30\x10\x06\x03\x55\x04\x07\x13\x09\x43\x61\x70\x65\x20\x54\x6F\x77\x6E\x31\x1A\x30\x18\x06\x03\x55\x04\x0A\x13\x11\x54\x68\x61\x77\x74\x65\x20\x43\x6F\x6E\x73\x75\x6C\x74\x69\x6E\x67\x31\x28\x30\x26\x06\x03\x55\x04\x0B\x13\x1F\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x69\x6F\x6E\x20\x53\x65\x72\x76\x69\x63\x65\x73\x20\x44\x69\x76\x69\x73\x69\x6F\x6E\x31\x23\x30\x21\x06\x03\x55\x04\x03\x13\x1A\x54\x68\x61\x77\x74\x65\x20\x50\x65\x72\x73\x6F\x6E\x61\x6C\x20\x50\x72\x65\x6D\x69\x75\x6D\x20\x43\x41\x31\x2A\x30\x28\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01\x16\x1B\x70\x65\x72\x73\x6F\x6E\x61\x6C\x2D\x70\x72\x65\x6D\x69\x75\x6D\x40\x74\x68\x61\x77\x74\x65\x2E\x63\x6F\x6D", 210);
129 1 : g_free (data);
130 1 : }
131 :
132 : static void
133 1 : test_attribute_subject (Test* test,
134 : gconstpointer unused)
135 : {
136 : gpointer data;
137 : gsize n_data;
138 :
139 1 : data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
140 : test->session, CKA_SUBJECT, &n_data);
141 :
142 1 : egg_assert_cmpmem (data, n_data, ==, "\x30\x81\xCF\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x5A\x41\x31\x15\x30\x13\x06\x03\x55\x04\x08\x13\x0C\x57\x65\x73\x74\x65\x72\x6E\x20\x43\x61\x70\x65\x31\x12\x30\x10\x06\x03\x55\x04\x07\x13\x09\x43\x61\x70\x65\x20\x54\x6F\x77\x6E\x31\x1A\x30\x18\x06\x03\x55\x04\x0A\x13\x11\x54\x68\x61\x77\x74\x65\x20\x43\x6F\x6E\x73\x75\x6C\x74\x69\x6E\x67\x31\x28\x30\x26\x06\x03\x55\x04\x0B\x13\x1F\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x69\x6F\x6E\x20\x53\x65\x72\x76\x69\x63\x65\x73\x20\x44\x69\x76\x69\x73\x69\x6F\x6E\x31\x23\x30\x21\x06\x03\x55\x04\x03\x13\x1A\x54\x68\x61\x77\x74\x65\x20\x50\x65\x72\x73\x6F\x6E\x61\x6C\x20\x50\x72\x65\x6D\x69\x75\x6D\x20\x43\x41\x31\x2A\x30\x28\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01\x16\x1B\x70\x65\x72\x73\x6F\x6E\x61\x6C\x2D\x70\x72\x65\x6D\x69\x75\x6D\x40\x74\x68\x61\x77\x74\x65\x2E\x63\x6F\x6D", 210);
143 1 : g_free (data);
144 1 : }
145 :
146 : static void
147 1 : test_attribute_serial_number (Test* test,
148 : gconstpointer unused)
149 : {
150 : gpointer data;
151 : gsize n_data;
152 :
153 1 : data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
154 : test->session, CKA_SERIAL_NUMBER, &n_data);
155 :
156 1 : egg_assert_cmpmem (data, n_data, ==, "\x02\x01\x00", 3);
157 1 : g_free (data);
158 1 : }
159 :
160 : static void
161 1 : test_attribute_value (Test* test,
162 : gconstpointer unused)
163 : {
164 : gconstpointer raw;
165 : gpointer data;
166 : gsize n_data, n_raw;
167 :
168 1 : data = gkm_object_get_attribute_data (GKM_OBJECT (test->certificate),
169 : test->session, CKA_VALUE, &n_data);
170 :
171 1 : raw = g_bytes_get_data (test->certificate_data, NULL);
172 1 : n_raw = g_bytes_get_size (test->certificate_data);
173 1 : egg_assert_cmpmem (data, n_data, ==, raw, n_raw);
174 1 : g_free (data);
175 1 : }
176 :
177 : static void
178 1 : test_hash (Test* test,
179 : gconstpointer unused)
180 : {
181 : gpointer hash;
182 : gsize n_hash;
183 :
184 1 : hash = gkm_certificate_hash (test->certificate, GCRY_MD_SHA1, &n_hash);
185 :
186 1 : egg_assert_cmpmem (hash, n_hash, ==, "\x36\x86\x35\x63\xFD\x51\x28\xC7\xBE\xA6\xF0\x05\xCF\xE9\xB4\x36\x68\x08\x6C\xCE", 20);
187 1 : g_free (hash);
188 1 : }
189 :
190 : int
191 1 : main (int argc, char **argv)
192 : {
193 : #if !GLIB_CHECK_VERSION(2,35,0)
194 : g_type_init ();
195 : #endif
196 1 : g_test_init (&argc, &argv, NULL);
197 :
198 1 : g_test_add ("/gkm/certificate/issuer", Test, NULL, setup, test_attribute_issuer, teardown);
199 1 : g_test_add ("/gkm/certificate/subject", Test, NULL, setup, test_attribute_subject, teardown);
200 1 : g_test_add ("/gkm/certificate/check-value", Test, NULL, setup, test_attribute_check_value, teardown);
201 1 : g_test_add ("/gkm/certificate/serial-number", Test, NULL, setup, test_attribute_serial_number, teardown);
202 1 : g_test_add ("/gkm/certificate/value", Test, NULL, setup, test_attribute_value, teardown);
203 1 : g_test_add ("/gkm/certificate/hash", Test, NULL, setup, test_hash, teardown);
204 :
205 1 : return egg_tests_run_in_thread_with_loop ();
206 : }
|