Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-secret.c: Test gkm-secret.c
3 :
4 : Copyright (C) 2007 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 <stef@memberwebs.com>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include <stdlib.h>
26 : #include <stdio.h>
27 : #include <string.h>
28 :
29 : #include "gkm/gkm-secret.h"
30 :
31 : #include "egg/egg-secure-memory.h"
32 :
33 36 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
34 :
35 : static void
36 1 : test_secret (void)
37 : {
38 : GkmSecret *secret;
39 : const gchar *password;
40 : gsize n_password;
41 :
42 1 : secret = gkm_secret_new ((guchar*)"test-pin", 8);
43 1 : g_assert (GKM_IS_SECRET (secret));
44 :
45 1 : password = gkm_secret_get_password (secret, &n_password);
46 1 : g_assert (password);
47 1 : g_assert_cmpuint (n_password, ==, 8);
48 1 : g_assert (memcmp (password, "test-pin", 8) == 0);
49 :
50 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pin", 8));
51 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
52 1 : g_assert (!gkm_secret_equals (secret, NULL, 0));
53 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pin", -1));
54 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
55 :
56 1 : g_object_unref (secret);
57 1 : }
58 :
59 : static void
60 1 : test_secret_from_login (void)
61 : {
62 : GkmSecret *secret;
63 : const gchar *password;
64 : gsize n_password;
65 :
66 1 : secret = gkm_secret_new_from_login ((guchar*)"test-pin", 8);
67 1 : g_assert (GKM_IS_SECRET (secret));
68 1 : password = gkm_secret_get_password (secret, &n_password);
69 1 : g_assert (password);
70 1 : g_assert_cmpuint (n_password, ==, 8);
71 1 : g_assert (memcmp (password, "test-pin", 8) == 0);
72 1 : g_object_unref (secret);
73 :
74 1 : secret = gkm_secret_new_from_login ((guchar*)"test-pin", (CK_ULONG)-1);
75 1 : g_assert (GKM_IS_SECRET (secret));
76 1 : password = gkm_secret_get_password (secret, &n_password);
77 1 : g_assert (password);
78 1 : g_assert_cmpuint (n_password, ==, 8);
79 1 : g_assert (memcmp (password, "test-pin", 8) == 0);
80 1 : g_object_unref (secret);
81 1 : }
82 :
83 : static void
84 1 : test_null_terminated (void)
85 : {
86 : GkmSecret *secret;
87 : const gchar *password;
88 : gsize n_password;
89 :
90 1 : secret = gkm_secret_new ((CK_UTF8CHAR_PTR)"null-terminated", -1);
91 1 : g_assert (GKM_IS_SECRET (secret));
92 :
93 1 : password = gkm_secret_get_password (secret, &n_password);
94 1 : g_assert (password);
95 1 : g_assert_cmpstr (password, ==, "null-terminated");
96 1 : g_assert_cmpuint (n_password, ==, strlen ("null-terminated"));
97 :
98 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated")));
99 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
100 1 : g_assert (!gkm_secret_equals (secret, NULL, 0));
101 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", -1));
102 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
103 :
104 1 : g_object_unref (secret);
105 1 : }
106 :
107 : static void
108 1 : test_always_has_null (void)
109 : {
110 : GkmSecret *secret;
111 : const guchar *memory;
112 : gsize n_memory;
113 :
114 : /* A 4 byte 'binary' secret */
115 1 : secret = gkm_secret_new ((guchar*)"barn", 4);
116 1 : g_assert (GKM_IS_SECRET (secret));
117 :
118 1 : memory = gkm_secret_get (secret, &n_memory);
119 1 : g_assert_cmpuint (n_memory, ==, 4);
120 :
121 : /* But it should be null-terminated anyway */
122 1 : g_assert (memory[4] == 0);
123 :
124 1 : g_object_unref (secret);
125 1 : }
126 :
127 : static void
128 1 : test_null (void)
129 : {
130 : GkmSecret *secret;
131 : const gchar *password;
132 : gsize n_password;
133 :
134 1 : secret = gkm_secret_new (NULL, 0);
135 1 : g_assert (GKM_IS_SECRET (secret));
136 :
137 1 : password = gkm_secret_get_password (secret, &n_password);
138 1 : g_assert (password == NULL);
139 1 : g_assert_cmpuint (n_password, ==, 0);
140 :
141 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated")));
142 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
143 1 : g_assert (gkm_secret_equals (secret, NULL, 0));
144 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", -1));
145 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
146 :
147 1 : g_object_unref (secret);
148 1 : }
149 :
150 : static void
151 1 : test_empty (void)
152 : {
153 : GkmSecret *secret;
154 : const gchar *password;
155 : gsize n_password;
156 :
157 1 : secret = gkm_secret_new ((CK_UTF8CHAR_PTR)"", 0);
158 1 : g_assert (GKM_IS_SECRET (secret));
159 :
160 1 : password = gkm_secret_get_password (secret, &n_password);
161 1 : g_assert_cmpstr (password, ==, "");
162 1 : g_assert_cmpuint (n_password, ==, 0);
163 :
164 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated")));
165 1 : g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9));
166 1 : g_assert (gkm_secret_equals (secret, NULL, 0));
167 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", -1));
168 1 : g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0));
169 :
170 1 : g_object_unref (secret);
171 1 : }
172 :
173 : static void
174 1 : test_equal (void)
175 : {
176 : GkmSecret *one;
177 : GkmSecret *two;
178 :
179 1 : one = two = gkm_secret_new ((CK_UTF8CHAR_PTR)"funny", 5);
180 1 : g_assert (gkm_secret_equal (one, two));
181 :
182 1 : two = gkm_secret_new_from_password ("funny");
183 1 : g_assert (gkm_secret_equal (one, two));
184 :
185 1 : g_object_unref (one);
186 1 : one = gkm_secret_new_from_password ("other");
187 1 : g_assert (!gkm_secret_equal (one, two));
188 :
189 1 : g_object_unref (one);
190 1 : g_object_unref (two);
191 1 : }
192 :
193 : int
194 1 : main (int argc, char **argv)
195 : {
196 : #if !GLIB_CHECK_VERSION(2,35,0)
197 : g_type_init ();
198 : #endif
199 1 : g_test_init (&argc, &argv, NULL);
200 :
201 1 : g_test_add_func ("/gkm/secret/secret", test_secret);
202 1 : g_test_add_func ("/gkm/secret/secret_from_login", test_secret_from_login);
203 1 : g_test_add_func ("/gkm/secret/null_terminated", test_null_terminated);
204 1 : g_test_add_func ("/gkm/secret/always_has_null", test_always_has_null);
205 1 : g_test_add_func ("/gkm/secret/null", test_null);
206 1 : g_test_add_func ("/gkm/secret/empty", test_empty);
207 1 : g_test_add_func ("/gkm/secret/equal", test_equal);
208 :
209 1 : return g_test_run ();
210 : }
|