Branch data Line data Source code
1 : : /* libsecret - GLib wrapper for Secret Service
2 : : *
3 : : * Copyright 2012 Red Hat Inc.
4 : : *
5 : : * This program is free software: you can redistribute it and/or modify
6 : : * it under the terms of the GNU Lesser General Public License as published
7 : : * by the Free Software Foundation; either version 2 of the licence or (at
8 : : * your option) any later version.
9 : : *
10 : : * See the included COPYING file for more information.
11 : : */
12 : :
13 : :
14 : : #include "config.h"
15 : :
16 : : #undef G_DISABLE_ASSERT
17 : :
18 : : #include "secret-value.h"
19 : : #include "secret-private.h"
20 : :
21 : : #include "egg/egg-testing.h"
22 : : #include "egg/egg-secure-memory.h"
23 : :
24 : : #include <glib.h>
25 : :
26 : : #include <errno.h>
27 : : #include <stdlib.h>
28 : :
29 : 2 : EGG_SECURE_DECLARE (test_value);
30 : :
31 : : static void
32 : 1 : test_new (void)
33 : : {
34 : : SecretValue *value;
35 : : gsize length;
36 : :
37 : 1 : value = secret_value_new ("blahblah", 4, "text/plain");
38 : :
39 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
40 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
41 : :
42 [ - + ]: 1 : g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
43 : :
44 : 1 : secret_value_unref (value);
45 : 1 : }
46 : :
47 : : static void
48 : 1 : test_new_terminated (void)
49 : : {
50 : : SecretValue *value;
51 : : gsize length;
52 : :
53 : 1 : value = secret_value_new ("blah", -1, "text/plain");
54 : :
55 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
56 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
57 : :
58 [ - + ]: 1 : g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
59 : :
60 : 1 : secret_value_unref (value);
61 : 1 : }
62 : :
63 : : static void
64 : 1 : test_new_full (void)
65 : : {
66 : : SecretValue *value;
67 : 1 : gchar *data = g_strdup ("blah");
68 : : gsize length;
69 : :
70 : 1 : value = secret_value_new_full (data, 4, "text/plain", g_free);
71 : :
72 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
73 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
74 : :
75 : : /* No copy done here */
76 [ - + ]: 1 : g_assert_true (secret_value_get (value, NULL) == data);
77 : :
78 : 1 : secret_value_unref (value);
79 : 1 : }
80 : :
81 : : static void
82 : 1 : test_new_full_terminated (void)
83 : : {
84 : : SecretValue *value;
85 : 1 : gchar *data = g_strdup ("blah");
86 : : gsize length;
87 : :
88 : 1 : value = secret_value_new_full (data, -1, "text/plain", g_free);
89 : :
90 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
91 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
92 : :
93 : : /* No copy done here */
94 [ - + ]: 1 : g_assert_true (secret_value_get (value, NULL) == data);
95 : :
96 : 1 : secret_value_unref (value);
97 : 1 : }
98 : :
99 : : static void
100 : 1 : test_new_empty (void)
101 : : {
102 : : SecretValue *value;
103 : : const gchar *password;
104 : : gsize length;
105 : :
106 : 1 : value = secret_value_new (NULL, 0, "text/plain");
107 [ - + ]: 1 : g_assert_nonnull (value);
108 : 1 : password = secret_value_get (value, &length);
109 [ - + ]: 1 : g_assert_cmpuint (length, ==, 0);
110 [ - + ]: 1 : g_assert_cmpstr (password, ==, "");
111 : 1 : secret_value_unref (value);
112 : :
113 : 1 : value = secret_value_new ("", 0, "text/plain");
114 [ - + ]: 1 : g_assert_nonnull (value);
115 : 1 : password = secret_value_get (value, &length);
116 [ - + ]: 1 : g_assert_cmpuint (length, ==, 0);
117 [ - + ]: 1 : g_assert_cmpstr (password, ==, "");
118 : 1 : secret_value_unref (value);
119 : 1 : }
120 : :
121 : : static void
122 : 1 : test_ref_unref (void)
123 : : {
124 : : SecretValue *value;
125 : : SecretValue *value2;
126 : : gsize length;
127 : :
128 : 1 : value = secret_value_new ("blah", 4, "text/plain");
129 : 1 : value2 = secret_value_ref(value);
130 : 1 : secret_value_unref (value);
131 : :
132 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
133 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
134 : :
135 : 1 : secret_value_unref (value2);
136 : 1 : }
137 : :
138 : : static void
139 : 1 : test_boxed (void)
140 : : {
141 : : SecretValue *value;
142 : : SecretValue *value2;
143 : : gsize length;
144 : :
145 : 1 : value = secret_value_new ("blah", 4, "text/plain");
146 : 1 : value2 = g_boxed_copy (SECRET_TYPE_VALUE, value);
147 : 1 : g_boxed_free (SECRET_TYPE_VALUE, value);
148 : :
149 [ - + ]: 1 : g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
150 [ - + ]: 1 : g_assert_cmpuint (length, ==, 4);
151 : :
152 : 1 : g_boxed_free (SECRET_TYPE_VALUE, value2);
153 : 1 : }
154 : :
155 : : static void
156 : 1 : test_to_password (void)
157 : : {
158 : : SecretValue *value;
159 : : gchar *password;
160 : :
161 : 1 : value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
162 : : "text/plain", egg_secure_free);
163 : :
164 : 1 : password = _secret_value_unref_to_password (value);
165 [ - + ]: 1 : g_assert_cmpstr (password, ==, "blah");
166 : :
167 : 1 : egg_secure_free (password);
168 : 1 : }
169 : :
170 : : static void
171 : 1 : test_to_password_bad_destroy (void)
172 : : {
173 : : SecretValue *value;
174 : : gchar *password;
175 : :
176 : 1 : value = secret_value_new_full (g_strdup ("blah"), -1,
177 : : "text/plain", g_free);
178 : :
179 : 1 : password = _secret_value_unref_to_password (value);
180 [ - + ]: 1 : g_assert_cmpstr (password, ==, "blah");
181 : :
182 : 1 : egg_secure_free (password);
183 : 1 : }
184 : :
185 : : static void
186 : 1 : test_to_password_bad_content (void)
187 : : {
188 : : SecretValue *value;
189 : : gchar *password;
190 : :
191 : 1 : value = secret_value_new_full (g_strdup ("w\xFFooowhee"), -1,
192 : : "application/octet-stream", g_free);
193 : :
194 : 1 : password = _secret_value_unref_to_password (value);
195 [ - + ]: 1 : g_assert_cmpstr (password, ==, NULL);
196 : 1 : }
197 : :
198 : : static void
199 : 1 : test_to_password_extra_ref (void)
200 : : {
201 : : SecretValue *value;
202 : : gchar *password;
203 : :
204 : 1 : value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
205 : : "text/plain", egg_secure_free);
206 : 1 : secret_value_ref (value);
207 : :
208 : 1 : password = _secret_value_unref_to_password (value);
209 [ - + ]: 1 : g_assert_cmpstr (password, ==, "blah");
210 : :
211 : 1 : egg_secure_free (password);
212 : 1 : secret_value_unref (value);
213 : 1 : }
214 : :
215 : : int
216 : 1 : main (int argc, char **argv)
217 : : {
218 : 1 : g_test_init (&argc, &argv, NULL);
219 : 1 : g_set_prgname ("test-value");
220 : :
221 : 1 : g_test_add_func ("/value/new", test_new);
222 : 1 : g_test_add_func ("/value/new-terminated", test_new_terminated);
223 : 1 : g_test_add_func ("/value/new-full", test_new_full);
224 : 1 : g_test_add_func ("/value/new-full-terminated", test_new_full_terminated);
225 : 1 : g_test_add_func ("/value/new-empty", test_new_empty);
226 : 1 : g_test_add_func ("/value/ref-unref", test_ref_unref);
227 : 1 : g_test_add_func ("/value/boxed", test_boxed);
228 : 1 : g_test_add_func ("/value/to-password", test_to_password);
229 : 1 : g_test_add_func ("/value/to-password-bad-destroy", test_to_password_bad_destroy);
230 : 1 : g_test_add_func ("/value/to-password-bad-content", test_to_password_bad_content);
231 : 1 : g_test_add_func ("/value/to-password-extra-ref", test_to_password_extra_ref);
232 : :
233 : 1 : return egg_tests_run_with_loop ();
234 : : }
|