Rename the library subdirectory to libsecret
[platform/upstream/libsecret.git] / libsecret / tests / test-value.c
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 #include "secret-value.h"
17 #include "secret-private.h"
18
19 #include "egg/egg-testing.h"
20 #include "egg/egg-secure-memory.h"
21
22 #include <glib.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26
27 EGG_SECURE_DECLARE (test_value);
28
29 static void
30 test_new (void)
31 {
32         SecretValue *value;
33         gsize length;
34
35         value = secret_value_new ("blahblah", 4, "text/plain");
36
37         g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
38         g_assert_cmpuint (length, ==, 4);
39
40         g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
41
42         secret_value_unref (value);
43 }
44
45 static void
46 test_new_terminated (void)
47 {
48         SecretValue *value;
49         gsize length;
50
51         value = secret_value_new ("blah", -1, "text/plain");
52
53         g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
54         g_assert_cmpuint (length, ==, 4);
55
56         g_assert_cmpstr (secret_value_get_content_type (value), ==, "text/plain");
57
58         secret_value_unref (value);
59 }
60
61 static void
62 test_new_full (void)
63 {
64         SecretValue *value;
65         gchar *data = g_strdup ("blah");
66         gsize length;
67
68         value = secret_value_new_full (data, 4, "text/plain", g_free);
69
70         g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
71         g_assert_cmpuint (length, ==, 4);
72
73         /* No copy done here */
74         g_assert (secret_value_get (value, NULL) == data);
75
76         secret_value_unref (value);
77 }
78
79 static void
80 test_new_full_terminated (void)
81 {
82         SecretValue *value;
83         gchar *data = g_strdup ("blah");
84         gsize length;
85
86         value = secret_value_new_full (data, -1, "text/plain", g_free);
87
88         g_assert_cmpstr (secret_value_get (value, &length), ==, "blah");
89         g_assert_cmpuint (length, ==, 4);
90
91         /* No copy done here */
92         g_assert (secret_value_get (value, NULL) == data);
93
94         secret_value_unref (value);
95 }
96
97 static void
98 test_ref_unref (void)
99 {
100         SecretValue *value;
101         SecretValue *value2;
102         gsize length;
103
104         value = secret_value_new ("blah", 4, "text/plain");
105         value2 = secret_value_ref(value);
106         secret_value_unref (value);
107
108         g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
109         g_assert_cmpuint (length, ==, 4);
110
111         secret_value_unref (value2);
112 }
113
114 static void
115 test_boxed (void)
116 {
117         SecretValue *value;
118         SecretValue *value2;
119         gsize length;
120
121         value = secret_value_new ("blah", 4, "text/plain");
122         value2 = g_boxed_copy (SECRET_TYPE_VALUE, value);
123         g_boxed_free (SECRET_TYPE_VALUE, value);
124
125         g_assert_cmpstr (secret_value_get (value2, &length), ==, "blah");
126         g_assert_cmpuint (length, ==, 4);
127
128         g_boxed_free (SECRET_TYPE_VALUE, value2);
129 }
130
131 static void
132 test_to_password (void)
133 {
134         SecretValue *value;
135         gchar *password;
136
137         value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
138                                         "text/plain", egg_secure_free);
139
140         password = _secret_value_unref_to_password (value);
141         g_assert_cmpstr (password, ==, "blah");
142
143         egg_secure_free (password);
144 }
145
146 static void
147 test_to_password_bad_destroy (void)
148 {
149         SecretValue *value;
150         gchar *password;
151
152         value = secret_value_new_full (g_strdup ("blah"), -1,
153                                         "text/plain", g_free);
154
155         password = _secret_value_unref_to_password (value);
156         g_assert_cmpstr (password, ==, "blah");
157
158         egg_secure_free (password);
159 }
160
161 static void
162 test_to_password_bad_content (void)
163 {
164         SecretValue *value;
165         gchar *password;
166
167         value = secret_value_new_full (g_strdup ("w\xFFooowhee"), -1,
168                                         "application/octet-stream", g_free);
169
170         password = _secret_value_unref_to_password (value);
171         g_assert_cmpstr (password, ==, NULL);
172 }
173
174 static void
175 test_to_password_extra_ref (void)
176 {
177         SecretValue *value;
178         gchar *password;
179
180         value = secret_value_new_full (egg_secure_strdup ("blah"), -1,
181                                         "text/plain", egg_secure_free);
182         secret_value_ref (value);
183
184         password = _secret_value_unref_to_password (value);
185         g_assert_cmpstr (password, ==, "blah");
186
187         egg_secure_free (password);
188         secret_value_unref (value);
189 }
190
191 int
192 main (int argc, char **argv)
193 {
194         g_test_init (&argc, &argv, NULL);
195         g_set_prgname ("test-value");
196         g_type_init ();
197
198         g_test_add_func ("/value/new", test_new);
199         g_test_add_func ("/value/new-terminated", test_new_terminated);
200         g_test_add_func ("/value/new-full", test_new_full);
201         g_test_add_func ("/value/new-full-terminated", test_new_full_terminated);
202         g_test_add_func ("/value/ref-unref", test_ref_unref);
203         g_test_add_func ("/value/boxed", test_boxed);
204         g_test_add_func ("/value/to-password", test_to_password);
205         g_test_add_func ("/value/to-password-bad-destroy", test_to_password_bad_destroy);
206         g_test_add_func ("/value/to-password-bad-content", test_to_password_bad_content);
207         g_test_add_func ("/value/to-password-extra-ref", test_to_password_extra_ref);
208
209         return egg_tests_run_with_loop ();
210 }