Improve gsettings test coverage
[platform/upstream/glib.git] / gio / tests / gtlsconsoleinteraction.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2011 Collabora, Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Stef Walter <stefw@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <glib/gprintf.h>
27 #include <string.h>
28
29 #ifdef G_OS_WIN32
30 #include <conio.h>
31 #endif
32
33 #include "gtlsconsoleinteraction.h"
34
35 /*
36  * WARNING: This is not the example you're looking for [slow hand wave]. This
37  * is not industrial strength, it's just for testing. It uses embarassing
38  * functions like getpass() and does lazy things with threads.
39  */
40
41 G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INTERACTION);
42
43 #if defined(G_OS_WIN32) || defined(__BIONIC__)
44 /* win32 doesn't have getpass() */
45 #include <stdio.h>
46 #ifndef BUFSIZ
47 #define BUFSIZ 8192
48 #endif
49 static gchar *
50 getpass (const gchar *prompt)
51 {
52   static gchar buf[BUFSIZ];
53   gint i;
54
55   g_printf ("%s", prompt);
56   fflush (stdout);
57
58   for (i = 0; i < BUFSIZ - 1; ++i)
59     {
60 #ifdef __BIONIC__
61       buf[i] = getc (stdin);
62 #else
63       buf[i] = _getch ();
64 #endif
65       if (buf[i] == '\r')
66         break;
67     }
68   buf[i] = '\0';
69
70   g_printf ("\n");
71
72   return &buf[0];
73 }
74 #endif
75
76 static GTlsInteractionResult
77 g_tls_console_interaction_ask_password (GTlsInteraction    *interaction,
78                                         GTlsPassword       *password,
79                                         GCancellable       *cancellable,
80                                         GError            **error)
81 {
82   const gchar *value;
83   gchar *prompt;
84
85   prompt = g_strdup_printf ("Password \"%s\"': ", g_tls_password_get_description (password));
86   value = getpass (prompt);
87   g_free (prompt);
88
89   if (g_cancellable_set_error_if_cancelled (cancellable, error))
90     return G_TLS_INTERACTION_FAILED;
91
92   g_tls_password_set_value (password, (guchar *)value, -1);
93   return G_TLS_INTERACTION_HANDLED;
94 }
95
96 static void
97 ask_password_with_getpass (GTask        *task,
98                            gpointer      object,
99                            gpointer      task_data,
100                            GCancellable *cancellable)
101 {
102   GTlsPassword *password = task_data;
103   GError *error = NULL;
104
105   g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password,
106                                           cancellable, &error);
107   if (error != NULL)
108     g_task_return_error (task, error);
109   else
110     g_task_return_int (task, G_TLS_INTERACTION_HANDLED);
111 }
112
113 static void
114 g_tls_console_interaction_ask_password_async (GTlsInteraction    *interaction,
115                                               GTlsPassword       *password,
116                                               GCancellable       *cancellable,
117                                               GAsyncReadyCallback callback,
118                                               gpointer            user_data)
119 {
120   GTask *task;
121
122   task = g_task_new (interaction, cancellable, callback, user_data);
123   g_task_set_task_data (task, g_object_ref (password), g_object_unref);
124   g_task_run_in_thread (task, ask_password_with_getpass);
125   g_object_unref (task);
126 }
127
128 static GTlsInteractionResult
129 g_tls_console_interaction_ask_password_finish (GTlsInteraction    *interaction,
130                                                GAsyncResult       *result,
131                                                GError            **error)
132 {
133   GTlsInteractionResult ret;
134
135   g_return_val_if_fail (g_task_is_valid (result, interaction),
136                         G_TLS_INTERACTION_FAILED);
137
138   ret = g_task_propagate_int (G_TASK (result), error);
139   if (ret == (GTlsInteractionResult)-1)
140     return G_TLS_INTERACTION_FAILED;
141   else
142     return ret;
143 }
144
145 static void
146 g_tls_console_interaction_init (GTlsConsoleInteraction *interaction)
147 {
148
149 }
150
151 static void
152 g_tls_console_interaction_class_init (GTlsConsoleInteractionClass *klass)
153 {
154   GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
155   interaction_class->ask_password = g_tls_console_interaction_ask_password;
156   interaction_class->ask_password_async = g_tls_console_interaction_ask_password_async;
157   interaction_class->ask_password_finish = g_tls_console_interaction_ask_password_finish;
158 }
159
160 GTlsInteraction *
161 g_tls_console_interaction_new (void)
162 {
163   return g_object_new (G_TYPE_TLS_CONSOLE_INTERACTION, NULL);
164 }