1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Copyright (C) 2001-2004 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
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 GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
28 #include <libedataserverui/e-passwords.h>
29 #include <libgnome/gnome-util.h>
31 #include "e2k-context.h"
32 #include "e2k-global-catalog.h"
35 #include "test-utils.h"
37 extern const char *test_program_name;
41 * @prompt: prompt string
43 * Prints @prompt followed by ": " and waits for the user to type
44 * a password (with echoing disabled).
46 * Return value: the password (or %NULL if stdin is not a tty).
49 test_ask_password (const char *prompt)
56 if (tcgetattr (STDIN_FILENO, &t) != 0)
59 old_lflag = t.c_lflag;
60 t.c_lflag = (t.c_lflag | ICANON | ECHONL) & ~ECHO;
61 tcsetattr (STDIN_FILENO, TCSANOW, &t);
63 fprintf (stderr, "%s: ", prompt);
66 /* For some reason, fgets can return EINTR on
67 * Linux if ECHO is false...
70 password = fgets (buf, sizeof (buf), stdin);
71 while (password == NULL && errno == EINTR);
73 t.c_lflag = old_lflag;
74 tcsetattr (STDIN_FILENO, TCSANOW, &t);
78 return g_strndup (password, strcspn (password, "\n"));
83 * @user: username to get the password for
84 * @host: Exchange (or global catalog) server name
86 * Tries to get a password for @user on @host, by looking it up in
87 * the Evolution password database or by prompting the user.
89 * Return value: the password, or %NULL if it could not be determined.
92 test_get_password (const char *user, const char *host)
94 static char *password = NULL;
103 key = g_strdup_printf ("exchange://%s@%s", user, host);
104 password = e_passwords_get_password ("Exchange", key);
110 prompt = g_strdup_printf ("Password for %s@%s",
113 prompt = g_strdup_printf ("Password for %s", user);
115 password = test_ask_password (prompt);
124 * @uri: an Exchange HTTP/HTTPS URI
126 * Creates an %E2kContext based on @uri. If @uri does not contain a
127 * username, the user's local username will be used. If it does not
128 * contain a password, test_get_password() will be called to get one.
130 * Return value: the new %E2kContext (always; if an error occurs,
131 * test_get_context() will exit the program).
134 test_get_context (const char *uri)
139 ctx = e2k_context_new (uri);
141 fprintf (stderr, "Could not parse %s as URI\n", uri);
145 euri = e2k_uri_new (uri);
147 euri->user = g_strdup (g_get_user_name ());
149 euri->passwd = g_strdup (test_get_password (euri->user, euri->host));
151 e2k_context_set_auth (ctx, euri->user, euri->domain,
152 euri->authmech, euri->passwd);
160 * @server: the global catalog server to contact
162 * Creates an %E2kGlobalCatalog for the server @server.
163 * test_get_password() will be called to get a password.
165 * Return value: the new %E2kGlobalCatalog (always; if an error occurs,
166 * test_get_gc() will exit the program).
169 test_get_gc (const char *server)
171 E2kGlobalCatalog *gc;
172 const char *password;
175 if (strchr (server, '@')) {
176 user = g_strdup (server);
177 p = strchr (user, '@');
181 user = g_strdup (g_get_user_name ());
183 password = test_get_password (user, server);
184 gc = e2k_global_catalog_new (server, -1, user, NULL, password);
186 fprintf (stderr, "Could not create GC\n");
194 static char **global_argv;
195 static int global_argc;
196 static GMainLoop *loop;
203 * test-utils.o includes a main() function that calls various
204 * initialization routines, starts the main loop, and then calls
205 * test_main(). So test_main() is the entry point for a
206 * test-utils-using program.
212 * Cleanly quits a test program.
217 g_main_loop_quit (loop);
221 * test_abort_if_http_error:
222 * @status: an HTTP status code
224 * Checks if @status is an HTTP or libsoup error, and if so, prints
225 * the error message and exits.
228 test_abort_if_http_error (E2kHTTPStatus status)
230 if (E2K_HTTP_STATUS_IS_TRANSPORT_ERROR (status)) {
231 fprintf (stderr, "\n%s\n", soup_status_get_phrase (status));
233 } else if (!E2K_HTTP_STATUS_IS_SUCCESSFUL (status)) {
234 fprintf (stderr, "\n%d\n", status);
240 idle_run (gpointer data)
242 test_main (global_argc, global_argv);
247 main (int argc, char **argv)
249 gnome_program_init (test_program_name, VERSION, LIBGNOME_MODULE,
255 loop = g_main_loop_new (NULL, TRUE);
256 g_idle_add (idle_run, NULL);
257 g_main_loop_run (loop);