test-password.c 11.1 KB
Newer Older
Stef Walter's avatar
Stef Walter committed
1
/* libsecret - GLib wrapper for Secret Service
Stef Walter's avatar
Stef Walter committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Copyright 2012 Red Hat Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */

#include "config.h"

Daiki Ueno's avatar
Daiki Ueno committed
17 18
#undef G_DISABLE_ASSERT

Stef Walter's avatar
Stef Walter committed
19
#include "secret-password.h"
20
#include "secret-paths.h"
Stef Walter's avatar
Stef Walter committed
21
#include "secret-private.h"
Stef Walter's avatar
Stef Walter committed
22

23 24
#include "mock-service.h"

Stef Walter's avatar
Stef Walter committed
25 26 27 28 29 30 31
#include "egg/egg-testing.h"

#include <glib.h>

#include <errno.h>
#include <stdlib.h>

Stef Walter's avatar
Stef Walter committed
32 33
static const SecretSchema MOCK_SCHEMA = {
	"org.mock.Schema",
34
	SECRET_SCHEMA_NONE,
Stef Walter's avatar
Stef Walter committed
35
	{
36 37 38
		{ "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
		{ "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
		{ "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
Stef Walter's avatar
Stef Walter committed
39 40 41
	}
};

Stef Walter's avatar
Stef Walter committed
42 43 44 45 46 47 48 49 50
static const SecretSchema NO_NAME_SCHEMA = {
	"unused.Schema.Name",
	SECRET_SCHEMA_DONT_MATCH_NAME,
	{
		{ "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
		{ "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
	}
};

Stef Walter's avatar
Stef Walter committed
51 52 53 54 55 56 57 58 59 60 61
typedef struct {
	GPid pid;
} Test;

static void
setup (Test *test,
       gconstpointer data)
{
	GError *error = NULL;
	const gchar *mock_script = data;

62
	mock_service_start (mock_script, &error);
Stef Walter's avatar
Stef Walter committed
63 64 65 66 67 68 69
	g_assert_no_error (error);
}

static void
teardown (Test *test,
          gconstpointer unused)
{
Stef Walter's avatar
Stef Walter committed
70
	secret_service_disconnect ();
71
	mock_service_stop ();
Stef Walter's avatar
Stef Walter committed
72 73 74 75 76 77 78 79
}

static void
on_complete_get_result (GObject *source,
                        GAsyncResult *result,
                        gpointer user_data)
{
	GAsyncResult **ret = user_data;
80 81
	g_assert_nonnull (ret);
	g_assert_null (*ret);
Stef Walter's avatar
Stef Walter committed
82 83 84 85
	*ret = g_object_ref (result);
	egg_test_wait_stop ();
}

Stef Walter's avatar
Stef Walter committed
86 87 88 89 90 91 92
static void
test_lookup_sync (Test *test,
                  gconstpointer used)
{
	gchar *password;
	GError *error = NULL;

Stef Walter's avatar
Stef Walter committed
93
	password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
94 95 96 97
	                                                    "even", FALSE,
	                                                    "string", "one",
	                                                    "number", 1,
	                                                    NULL);
Stef Walter's avatar
Stef Walter committed
98 99 100 101

	g_assert_no_error (error);
	g_assert_cmpstr (password, ==, "111");

Stef Walter's avatar
Stef Walter committed
102
	secret_password_free (password);
Stef Walter's avatar
Stef Walter committed
103 104 105 106 107 108 109 110 111 112
}

static void
test_lookup_async (Test *test,
                   gconstpointer used)
{
	GAsyncResult *result = NULL;
	GError *error = NULL;
	gchar *password;

Stef Walter's avatar
Stef Walter committed
113
	secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
Stef Walter's avatar
Stef Walter committed
114 115 116 117
	                        "even", FALSE,
	                        "string", "one",
	                        "number", 1,
	                        NULL);
118
	g_assert_null (result);
Stef Walter's avatar
Stef Walter committed
119 120 121

	egg_test_wait ();

122
	password = secret_password_lookup_nonpageable_finish (result, &error);
Stef Walter's avatar
Stef Walter committed
123 124 125 126
	g_assert_no_error (error);
	g_object_unref (result);

	g_assert_cmpstr (password, ==, "111");
Stef Walter's avatar
Stef Walter committed
127
	secret_password_free (password);
Stef Walter's avatar
Stef Walter committed
128 129
}

Stef Walter's avatar
Stef Walter committed
130 131 132 133 134 135 136 137 138 139 140 141
static void
test_lookup_no_name (Test *test,
                     gconstpointer used)
{
	GError *error = NULL;
	gchar *password;

	/* should return null, because nothing with mock schema and 5 */
	password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
	                                        "number", 5,
	                                        NULL);
	g_assert_no_error (error);
142
	g_assert_null (password);
Stef Walter's avatar
Stef Walter committed
143 144 145 146 147 148 149 150 151 152 153 154

	/* should return an item, because we have a prime schema with 5, and flags not to match name */
	password = secret_password_lookup_sync (&NO_NAME_SCHEMA, NULL, &error,
	                                        "number", 5,
	                                        NULL);

	g_assert_no_error (error);
	g_assert_cmpstr (password, ==, "555");

	secret_password_free (password);
}

Stef Walter's avatar
Stef Walter committed
155 156 157 158 159 160 161 162 163
static void
test_store_sync (Test *test,
                  gconstpointer used)
{
	const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
	GError *error = NULL;
	gchar *password;
	gboolean ret;

Stef Walter's avatar
Stef Walter committed
164
	ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
Stef Walter's avatar
Stef Walter committed
165 166 167 168 169
	                                  "Label here", "the password", NULL, &error,
	                                  "even", TRUE,
	                                  "string", "twelve",
	                                  "number", 12,
	                                  NULL);
Stef Walter's avatar
Stef Walter committed
170 171

	g_assert_no_error (error);
172
	g_assert_true (ret);
Stef Walter's avatar
Stef Walter committed
173

Stef Walter's avatar
Stef Walter committed
174
	password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
175 176
	                                                    "string", "twelve",
	                                                    NULL);
Stef Walter's avatar
Stef Walter committed
177 178 179 180

	g_assert_no_error (error);
	g_assert_cmpstr (password, ==, "the password");

Stef Walter's avatar
Stef Walter committed
181
	secret_password_free (password);
Stef Walter's avatar
Stef Walter committed
182 183 184 185 186 187 188 189 190 191 192 193
}

static void
test_store_async (Test *test,
                  gconstpointer used)
{
	const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
	GAsyncResult *result = NULL;
	GError *error = NULL;
	gchar *password;
	gboolean ret;

Stef Walter's avatar
Stef Walter committed
194
	secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
Stef Walter's avatar
Stef Walter committed
195 196 197 198 199
	                       "the password", NULL, on_complete_get_result, &result,
	                       "even", TRUE,
	                       "string", "twelve",
	                       "number", 12,
	                       NULL);
200
	g_assert_null (result);
Stef Walter's avatar
Stef Walter committed
201 202 203

	egg_test_wait ();

Stef Walter's avatar
Stef Walter committed
204
	ret = secret_password_store_finish (result, &error);
Stef Walter's avatar
Stef Walter committed
205
	g_assert_no_error (error);
206
	g_assert_true (ret);
Stef Walter's avatar
Stef Walter committed
207 208
	g_object_unref (result);

Stef Walter's avatar
Stef Walter committed
209
	password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
210 211
	                                                    "string", "twelve",
	                                                    NULL);
Stef Walter's avatar
Stef Walter committed
212 213 214 215

	g_assert_no_error (error);
	g_assert_cmpstr (password, ==, "the password");

Stef Walter's avatar
Stef Walter committed
216
	secret_password_free (password);
Stef Walter's avatar
Stef Walter committed
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static void
test_store_unlock (Test *test,
                   gconstpointer unused)
{
	const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
	GAsyncResult *result = NULL;
	SecretCollection *collection;
	SecretService *service;
	GError *error = NULL;
	gchar *password;
	gboolean ret;
	GList *objects;
	gint count;

	service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
	g_assert_no_error (error);

	/* Check collection state */
	collection = secret_collection_new_for_dbus_path_sync (service, collection_path,
	                                                       SECRET_COLLECTION_NONE, NULL, &error);
	g_assert_no_error (error);
240
	g_assert_false (secret_collection_get_locked (collection));
241 242 243 244 245 246 247 248 249 250 251

	/* Lock it, use async, so collection properties update */
	objects = g_list_append (NULL, collection);
	secret_service_lock (service, objects, NULL, on_complete_get_result, &result);
	egg_test_wait ();
	count = secret_service_lock_finish (service, result, NULL, &error);
	g_assert_cmpint (count, ==, 1);
	g_clear_object (&result);
	g_list_free (objects);

	/* Check collection state */
252
	g_assert_true (secret_collection_get_locked (collection));
253 254 255 256 257 258 259 260

	/* Store the password, use async so collection properties update */
	secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
	                       "the password", NULL, on_complete_get_result, &result,
	                       "even", TRUE,
	                       "string", "twelve",
	                       "number", 12,
	                       NULL);
261
	g_assert_null (result);
262 263 264
	egg_test_wait ();
	ret = secret_password_store_finish (result, &error);
	g_assert_no_error (error);
265
	g_assert_true (ret);
266 267 268
	g_clear_object (&result);

	/* Check collection state */
269
	g_assert_false (secret_collection_get_locked (collection));
270 271 272 273 274 275 276 277 278 279 280 281 282 283


	password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
	                                                    "string", "twelve",
	                                                    NULL);

	g_assert_no_error (error);
	g_assert_cmpstr (password, ==, "the password");

	secret_password_free (password);
	g_object_unref (collection);
	g_object_unref (service);
}

Stef Walter's avatar
Stef Walter committed
284 285 286 287 288 289 290
static void
test_delete_sync (Test *test,
                  gconstpointer used)
{
	GError *error = NULL;
	gboolean ret;

291 292 293 294 295
	ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
	                                  "even", FALSE,
	                                  "string", "one",
	                                  "number", 1,
	                                  NULL);
Stef Walter's avatar
Stef Walter committed
296 297

	g_assert_no_error (error);
298
	g_assert_true (ret);
Stef Walter's avatar
Stef Walter committed
299 300 301 302 303 304 305 306 307 308
}

static void
test_delete_async (Test *test,
                   gconstpointer used)
{
	GError *error = NULL;
	GAsyncResult *result = NULL;
	gboolean ret;

309 310 311 312 313 314
	secret_password_clear (&MOCK_SCHEMA, NULL,
	                       on_complete_get_result, &result,
	                       "even", FALSE,
	                       "string", "one",
	                       "number", 1,
	                       NULL);
Stef Walter's avatar
Stef Walter committed
315

316
	g_assert_null (result);
Stef Walter's avatar
Stef Walter committed
317 318 319

	egg_test_wait ();

320
	ret = secret_password_clear_finish (result, &error);
Stef Walter's avatar
Stef Walter committed
321
	g_assert_no_error (error);
322
	g_assert_true (ret);
Stef Walter's avatar
Stef Walter committed
323 324 325 326

	g_object_unref (result);
}

Stef Walter's avatar
Stef Walter committed
327
static void
328 329
test_clear_no_name (Test *test,
                    gconstpointer used)
Stef Walter's avatar
Stef Walter committed
330
{
331 332
	const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
	SecretService *service;
Stef Walter's avatar
Stef Walter committed
333 334 335 336
	GError *error = NULL;
	gboolean ret;

	/* Shouldn't match anything, because no item with 5 in mock schema */
337 338 339
	ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
	                                  "number", 5,
	                                  NULL);
Stef Walter's avatar
Stef Walter committed
340
	g_assert_no_error (error);
341
	g_assert_false (ret);
Stef Walter's avatar
Stef Walter committed
342

343 344 345 346 347 348 349
	/* We need this collection unlocked for the next test */
	service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
	g_assert_no_error (error);
	secret_service_unlock_dbus_paths_sync (service, paths, NULL, NULL, &error);
	g_assert_no_error (error);
	g_object_unref (service);

Stef Walter's avatar
Stef Walter committed
350
	/* We have an item with 5 in prime schema, but should match anyway becase of flags */
351
	ret = secret_password_clear_sync (&NO_NAME_SCHEMA, NULL, &error,
Stef Walter's avatar
Stef Walter committed
352 353 354 355
	                                  "number", 5,
	                                  NULL);

	g_assert_no_error (error);
356
	g_assert_true (ret);
Stef Walter's avatar
Stef Walter committed
357 358
}

Stef Walter's avatar
Stef Walter committed
359 360 361
static void
test_password_free_null (void)
{
Stef Walter's avatar
Stef Walter committed
362
	secret_password_free (NULL);
Stef Walter's avatar
Stef Walter committed
363 364
}

Stef Walter's avatar
Stef Walter committed
365 366 367 368 369 370
int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);
	g_set_prgname ("test-password");

Stef Walter's avatar
Stef Walter committed
371 372
	g_test_add ("/password/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
	g_test_add ("/password/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
Stef Walter's avatar
Stef Walter committed
373
	g_test_add ("/password/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
Stef Walter's avatar
Stef Walter committed
374 375 376

	g_test_add ("/password/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
	g_test_add ("/password/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
377
	g_test_add ("/password/store-unlock", Test, "mock-service-normal.py", setup, test_store_unlock, teardown);
Stef Walter's avatar
Stef Walter committed
378

Stef Walter's avatar
Stef Walter committed
379 380
	g_test_add ("/password/delete-sync", Test, "mock-service-delete.py", setup, test_delete_sync, teardown);
	g_test_add ("/password/delete-async", Test, "mock-service-delete.py", setup, test_delete_async, teardown);
381
	g_test_add ("/password/clear-no-name", Test, "mock-service-delete.py", setup, test_clear_no_name, teardown);
Stef Walter's avatar
Stef Walter committed
382

Stef Walter's avatar
Stef Walter committed
383 384
	g_test_add_func ("/password/free-null", test_password_free_null);

Stef Walter's avatar
Stef Walter committed
385 386
	return egg_tests_run_with_loop ();
}