tracker-0.16.2/tests/libtracker-miner/tracker-password-provider-test.c

No issues found

  1 /*
  2  * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <stdlib.h>
 23 
 24 #include <libtracker-miner/tracker-miner.h>
 25 
 26 #define SERVICE_NAME  "TestService"
 27 #define TEST_USERNAME "test-user"
 28 #define TEST_PASSWORD "s3cr3t"
 29 
 30 static TrackerPasswordProvider *provider;
 31 
 32 static void
 33 test_password_provider_setting (void)
 34 {
 35 	GError *error = NULL;
 36 	gboolean success;
 37 
 38 	g_print ("Storing password '%s' for user '%s'\n",
 39 	         TEST_PASSWORD,
 40 	         TEST_USERNAME);
 41 
 42 	success = tracker_password_provider_store_password (provider,
 43 	                                                    SERVICE_NAME,
 44 	                                                    "This is the test service",
 45 	                                                    TEST_USERNAME,
 46 	                                                    TEST_PASSWORD,
 47 	                                                    &error);
 48 
 49 	g_assert_cmpint (success, ==, TRUE);
 50 }
 51 
 52 static void
 53 test_password_provider_getting (void)
 54 {
 55 	gchar *username = NULL;
 56 	gchar *password = NULL;
 57 	gboolean success;
 58 	GError *error = NULL;
 59 
 60 	password = tracker_password_provider_get_password (provider,
 61 	                                                   SERVICE_NAME,
 62 	                                                   &username,
 63 	                                                   &error);
 64 
 65 	g_assert_no_error (error);
 66 	g_assert_cmpstr (username, ==, TEST_USERNAME);
 67 	g_assert_cmpstr (password, ==, TEST_PASSWORD);
 68 
 69 	g_print ("Found password is '%s' for username '%s'\n", 
 70 	         password,
 71 	         username);
 72 
 73 	g_free (username);
 74 
 75 	success = tracker_password_provider_unlock_password (password);
 76 	g_assert_cmpint (success, ==, TRUE);
 77 
 78 	/* Also test without getting the username */
 79 	password = tracker_password_provider_get_password (provider,
 80 	                                                   SERVICE_NAME,
 81 	                                                   NULL,
 82 	                                                   &error);
 83 
 84 	g_assert_cmpstr (password, ==, TEST_PASSWORD);
 85 
 86 	g_print ("Found password is '%s' for NULL username\n", password);
 87 
 88 	success = tracker_password_provider_unlock_password (password);
 89 	g_assert_cmpint (success, ==, TRUE);
 90 }
 91 
 92 static gboolean
 93 test_log_failure_cb (const gchar    *log_domain,
 94                      GLogLevelFlags  log_level,
 95                      const gchar    *message,
 96                      gpointer        user_data)
 97 {
 98 	/* Don't abort, we expect failure */
 99 	return FALSE;
100 }
101 
102 int 
103 main (int argc, char **argv)
104 {
105 	const gchar *current_dir;
106 	gint retval;
107 
108 	g_test_init (&argc, &argv, NULL);
109 
110 	/* Set test environment up */
111 	current_dir = g_get_current_dir ();
112 	g_setenv ("XDG_CONFIG_HOME", current_dir, TRUE);
113 
114 	g_test_add_func ("/libtracker-miner/tracker-password-provider/setting",
115 	                 test_password_provider_setting);
116 	g_test_add_func ("/libtracker-miner/tracker-password-provider/getting",
117 	                 test_password_provider_getting);
118 
119 	g_test_log_set_fatal_handler (test_log_failure_cb, NULL);
120 	provider = tracker_password_provider_get ();
121 	g_print ("Not aborting here because we expect no filename to exist yet\n");
122 	g_assert (provider);
123 
124 	/* g_object_unref (provider); */
125 
126 	retval = g_test_run ();
127 
128         /* clean up */
129         g_print ("Removing temporary data\n");
130         g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
131 
132         return retval;
133 }