Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-secret-items.c: Test secret items
3 :
4 : Copyright (C) 2013 Red Hat Inc
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 "test-service.h"
26 :
27 : #include "gkd-secret-types.h"
28 :
29 : #include "egg/egg-testing.h"
30 :
31 : #include <glib.h>
32 : #include <glib/gstdio.h>
33 : #include <gio/gio.h>
34 :
35 : #include <fcntl.h>
36 :
37 : typedef struct {
38 : TestService service;
39 : } Test;
40 :
41 : static void
42 1 : setup (Test *test,
43 : gconstpointer unused)
44 : {
45 : GVariant *retval;
46 1 : GError *error = NULL;
47 :
48 1 : test_service_setup (&test->service);
49 :
50 : /* Unlock the test collection */
51 1 : retval = g_dbus_connection_call_sync (test->service.connection,
52 1 : test->service.bus_name,
53 : SECRET_SERVICE_PATH,
54 : INTERNAL_SERVICE_INTERFACE,
55 : "UnlockWithMasterPassword",
56 : g_variant_new ("(o@(oayays))",
57 : "/org/freedesktop/secrets/collection/test",
58 : test_service_build_secret (&test->service, "booo")),
59 : G_VARIANT_TYPE ("()"),
60 : G_DBUS_CALL_FLAGS_NO_AUTO_START,
61 : -1, NULL, &error);
62 1 : g_assert_no_error (error);
63 1 : g_variant_unref (retval);
64 1 : }
65 :
66 : static void
67 1 : teardown (Test *test,
68 : gconstpointer unused)
69 : {
70 1 : test_service_teardown (&test->service);
71 1 : }
72 :
73 : static GVariant *
74 1 : get_all_properties (Test *test,
75 : const gchar *path,
76 : const gchar *interface)
77 : {
78 : GVariant *retval;
79 : GVariant *props;
80 1 : GError *error = NULL;
81 :
82 1 : retval = g_dbus_connection_call_sync (test->service.connection,
83 1 : test->service.bus_name,
84 : path, "org.freedesktop.DBus.Properties",
85 : "GetAll", g_variant_new ("(s)", interface),
86 : G_VARIANT_TYPE ("(a{sv})"),
87 : G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL, &error);
88 1 : g_assert_no_error (error);
89 :
90 1 : g_variant_get (retval, "(@a{sv})", &props);
91 1 : g_variant_unref (retval);
92 :
93 1 : return props;
94 : }
95 :
96 : static void
97 0 : set_property (Test *test,
98 : const gchar *path,
99 : const gchar *interface,
100 : const gchar *property,
101 : GVariant *value)
102 : {
103 : GVariant *retval;
104 0 : GError *error = NULL;
105 :
106 0 : retval = g_dbus_connection_call_sync (test->service.connection,
107 0 : test->service.bus_name,
108 : path, "org.freedesktop.DBus.Properties",
109 : "Set", g_variant_new ("(ssv)", interface, property, value),
110 : G_VARIANT_TYPE ("()"),
111 : G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL, &error);
112 0 : g_assert_no_error (error);
113 0 : g_variant_unref (retval);
114 0 : }
115 :
116 : static void
117 1 : test_created_modified_properties (Test *test,
118 : gconstpointer unused)
119 : {
120 : GVariantBuilder builder;
121 1 : GError *error = NULL;
122 : GVariant *retval;
123 : gchar *item;
124 : gchar *prompt;
125 : guint64 created;
126 : guint64 modified;
127 : GVariant *props;
128 :
129 1 : g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
130 1 : g_variant_builder_add (&builder, "{sv}", SECRET_ITEM_INTERFACE ".Label", g_variant_new_string ("The Label"));
131 1 : props = g_variant_builder_end (&builder);
132 :
133 1 : retval = g_dbus_connection_call_sync (test->service.connection,
134 1 : test->service.bus_name,
135 : "/org/freedesktop/secrets/collection/test",
136 : SECRET_COLLECTION_INTERFACE,
137 : "CreateItem",
138 : g_variant_new ("(@a{sv}@(oayays)b)", props,
139 : test_service_build_secret (&test->service, "the secret"), TRUE),
140 : G_VARIANT_TYPE ("(oo)"),
141 : G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL, &error);
142 1 : g_assert_no_error (error);
143 :
144 1 : g_variant_get (retval, "(oo)", &item, &prompt);
145 1 : g_assert_cmpstr (prompt, ==, "/");
146 1 : g_assert_cmpstr (item, !=, "/");
147 1 : g_variant_unref (retval);
148 1 : g_free (prompt);
149 :
150 1 : props = get_all_properties (test, item, SECRET_ITEM_INTERFACE);
151 1 : if (!g_variant_lookup (props, "Created", "t", &created))
152 0 : g_assert_not_reached ();
153 1 : if (!g_variant_lookup (props, "Modified", "t", &modified))
154 0 : g_assert_not_reached ();
155 1 : g_variant_unref (props);
156 :
157 : /* Created and modified within the last 10 seconds */
158 1 : g_assert_cmpuint (created, >, (g_get_real_time () / G_TIME_SPAN_SECOND) - 10);
159 1 : g_assert_cmpuint (modified, >, (g_get_real_time () / G_TIME_SPAN_SECOND) - 10);
160 1 : g_assert_cmpuint (created, ==, modified);
161 :
162 1 : if (!g_test_thorough ()) {
163 1 : g_free (item);
164 1 : return;
165 : }
166 :
167 : /* Unfortunately have to wait 1.25 seconds here */
168 0 : g_usleep (G_TIME_SPAN_SECOND + (G_TIME_SPAN_SECOND / 4));
169 :
170 : /* Now modify the item */
171 0 : set_property (test, item, SECRET_ITEM_INTERFACE, "Label", g_variant_new_string ("New Label"));
172 :
173 : /* Check the properties again */
174 0 : props = get_all_properties (test, item, SECRET_ITEM_INTERFACE);
175 0 : if (!g_variant_lookup (props, "Created", "t", &created))
176 0 : g_assert_not_reached ();
177 0 : if (!g_variant_lookup (props, "Modified", "t", &modified))
178 0 : g_assert_not_reached ();
179 0 : g_variant_unref (props);
180 :
181 : /* Modified should have changed */
182 0 : g_assert_cmpuint (modified, >, (g_get_real_time () / G_TIME_SPAN_SECOND) - 10);
183 0 : g_assert_cmpuint (created, !=, modified);
184 :
185 0 : g_free (item);
186 : }
187 :
188 : int
189 1 : main (int argc, char **argv)
190 : {
191 : #if !GLIB_CHECK_VERSION(2,35,0)
192 : g_type_init ();
193 : #endif
194 1 : g_test_init (&argc, &argv, NULL);
195 :
196 1 : g_test_add ("/secret-item/created-modified-properties", Test, NULL,
197 : setup, test_created_modified_properties, teardown);
198 :
199 1 : return egg_tests_run_with_loop ();
200 : }
|