File: test-gkd-ssh-agent-interaction.c

package info (click to toggle)
gnome-keyring 3.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,404 kB
  • sloc: ansic: 77,137; sh: 4,856; makefile: 1,334; xml: 420; python: 331; sed: 16
file content (196 lines) | stat: -rw-r--r-- 5,483 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * gnome-keyring
 *
 * Copyright (C) 2018 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.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daiki Ueno
 */

#include "config.h"

#include "gkd-ssh-agent-private.h"
#include "gkd-ssh-agent-interaction.h"
#include "daemon/login/gkd-login-password.h"
#include "egg/egg-testing.h"

#include <glib.h>
#include <glib/gstdio.h>
#include <gcr/gcr-base.h>

typedef struct {
	const gchar *prompter_name;
	GTlsPassword *password;
} Test;

static void
setup (Test *test, gboolean login_available)
{
	GTlsPassword *password;

	password = g_tls_password_new (G_TLS_PASSWORD_NONE, "");
	test->password = g_object_new (GKD_TYPE_LOGIN_PASSWORD,
				       "base", password,
				       "login-available", login_available,
				       "description", "ssh-key",
				       NULL);
	g_object_unref (password);

	test->prompter_name = gcr_mock_prompter_start ();
}

static void
setup_no_login (Test *test, gconstpointer unused)
{
	setup (test, FALSE);
}

static void
setup_login (Test *test, gconstpointer unused)
{
	setup (test, TRUE);
}

static void
teardown (Test *test, gconstpointer unused)
{
	gcr_mock_prompter_stop ();

	g_object_unref (test->password);
}

static void
on_async_result (GObject *source_object,
		 GAsyncResult *result,
		 gpointer user_data)
{
        GAsyncResult **ret = user_data;
        *ret = g_object_ref (result);
	egg_test_wait_stop ();
}

static void
test_ask_password_no_login (Test *test, gconstpointer unused)
{
	GTlsInteraction *interaction;
	GAsyncResult *result = NULL;
	GError *error = NULL;
	const guchar *value;
	gsize length;
	GTlsInteractionResult ret;

	interaction = gkd_ssh_agent_interaction_new (test->prompter_name);
	gcr_mock_prompter_expect_password_ok ("password", NULL);
	g_tls_interaction_ask_password_async (interaction,
					      test->password,
					      NULL,
					      on_async_result,
					      &result);
	g_assert (result == NULL);

	egg_test_wait ();

	g_assert (result != NULL);

	ret = g_tls_interaction_ask_password_finish (interaction, result, &error);
	g_assert_cmpint (ret, ==, G_TLS_INTERACTION_HANDLED);
	g_assert_no_error (error);

	value = g_tls_password_get_value (test->password, &length);
	g_assert_cmpmem ("password", 8, value, length);
	g_assert_false (gkd_login_password_get_store_password (GKD_LOGIN_PASSWORD (test->password)));
	g_object_unref (interaction);
	g_object_unref (result);
}

static void
test_ask_password_login (Test *test, gconstpointer unused)
{
	GTlsInteraction *interaction;
	GAsyncResult *result = NULL;
	GError *error = NULL;
	const guchar *value;
	gsize length;
	GTlsInteractionResult ret;

	interaction = gkd_ssh_agent_interaction_new (test->prompter_name);
	gcr_mock_prompter_expect_password_ok ("password", "choice-chosen", TRUE, NULL);
	g_tls_interaction_ask_password_async (interaction,
					      test->password,
					      NULL,
					      on_async_result,
					      &result);
	g_assert (result == NULL);

	egg_test_wait ();

	ret = g_tls_interaction_ask_password_finish (interaction, result, &error);
	g_assert_cmpint (ret, ==, G_TLS_INTERACTION_HANDLED);
	g_assert_no_error (error);

	value = g_tls_password_get_value (test->password, &length);
	g_assert_cmpmem ("password", 8, value, length);
	g_assert_true (gkd_login_password_get_store_password (GKD_LOGIN_PASSWORD (test->password)));
	g_object_unref (interaction);
	g_object_unref (result);
}

static void
test_ask_password_cancel (Test *test, gconstpointer unused)
{
	GTlsInteraction *interaction;
	GAsyncResult *result = NULL;
	GError *error = NULL;
	const guchar *value;
	gsize length;
	GTlsInteractionResult ret;

	interaction = gkd_ssh_agent_interaction_new (test->prompter_name);
	gcr_mock_prompter_expect_password_cancel ();
	g_tls_interaction_ask_password_async (interaction,
					      test->password,
					      NULL,
					      on_async_result,
					      &result);
	g_assert (result == NULL);

	egg_test_wait ();

	g_assert (result != NULL);


	ret = g_tls_interaction_ask_password_finish (interaction, result, &error);
	g_assert_cmpint (ret, ==, G_TLS_INTERACTION_FAILED);
	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);

	value = g_tls_password_get_value (test->password, &length);
	g_assert_cmpmem ("", 0, value, length);
	g_assert_false (gkd_login_password_get_store_password (GKD_LOGIN_PASSWORD (test->password)));
	g_object_unref (interaction);
	g_object_unref (result);
}

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/ssh-agent/interaction/ask_password_no_login", Test, NULL, setup_no_login, test_ask_password_no_login, teardown);
	g_test_add ("/ssh-agent/interaction/ask_password_login", Test, NULL, setup_login, test_ask_password_login, teardown);
	g_test_add ("/ssh-agent/interaction/ask_password_cancel", Test, NULL, setup_no_login, test_ask_password_cancel, teardown);

	return egg_tests_run_with_loop ();
}