summaryrefslogtreecommitdiff
path: root/tests/libtracker-miner/tracker-password-provider-test.c
blob: 3c58b268f789c1a2bf0e6fc4c9951c00eba1a3a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "config.h"

#include <stdlib.h>

#include <libtracker-miner/tracker-miner.h>

#define SERVICE_NAME  "TestService"
#define TEST_USERNAME "test-user"
#define TEST_PASSWORD "s3cr3t"

static TrackerPasswordProvider *provider;

static void
test_password_provider_setting (void)
{
	GError *error = NULL;
	gboolean success;

	g_print ("Storing password '%s' for user '%s'\n",
	         TEST_PASSWORD,
	         TEST_USERNAME);

	success = tracker_password_provider_store_password (provider,
	                                                    SERVICE_NAME,
	                                                    "This is the test service",
	                                                    TEST_USERNAME,
	                                                    TEST_PASSWORD,
	                                                    &error);

	g_assert_cmpint (success, ==, TRUE);
}

static void
test_password_provider_getting (void)
{
	gchar *username = NULL;
	gchar *password = NULL;
	gboolean success;
	GError *error = NULL;

	password = tracker_password_provider_get_password (provider,
	                                                   SERVICE_NAME,
	                                                   &username,
	                                                   &error);

	g_assert_cmpstr (username, ==, TEST_USERNAME);
	g_assert_cmpstr (password, ==, TEST_PASSWORD);

	g_print ("Found password is '%s' for username '%s'\n", 
	         password,
	         username);

	g_free (username);

	success = tracker_password_provider_unlock_password (password);
	g_assert_cmpint (success, ==, TRUE);

	/* Also test without getting the username */
	password = tracker_password_provider_get_password (provider,
	                                                   SERVICE_NAME,
	                                                   NULL,
	                                                   &error);

	g_assert_cmpstr (password, ==, TEST_PASSWORD);

	g_print ("Found password is '%s' for NULL username\n", password);

	success = tracker_password_provider_unlock_password (password);
	g_assert_cmpint (success, ==, TRUE);
}

static gboolean
test_log_failure_cb (const gchar    *log_domain,
                     GLogLevelFlags  log_level,
                     const gchar    *message,
                     gpointer        user_data)
{
	/* Don't abort, we expect failure */
	return FALSE;
}

int 
main (int argc, char **argv)
{
	const gchar *current_dir;
	gint retval;

	g_type_init ();

	g_thread_init (NULL);
	g_test_init (&argc, &argv, NULL);

	/* Set test environment up */
	current_dir = g_get_current_dir ();
	g_setenv ("XDG_CONFIG_HOME", current_dir, TRUE);

	g_test_add_func ("/libtracker-miner/tracker-password-provider/setting",
	                 test_password_provider_setting);
	g_test_add_func ("/libtracker-miner/tracker-password-provider/getting",
	                 test_password_provider_getting);

	g_test_log_set_fatal_handler (test_log_failure_cb, NULL);
	provider = tracker_password_provider_get ();
	g_print ("Not aborting here because we expect no filename to exist yet\n");
	g_assert (provider);

	/* g_object_unref (provider); */

	retval = g_test_run ();

        /* clean up */
        g_print ("Removing temporary data\n");
        g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);

        return retval;
}