Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2014 Stefan Walter
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring 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 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@gnome.org>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr.h"
25 : :
26 : : #include "egg/egg-testing.h"
27 : : #include "egg/mock-interaction.h"
28 : :
29 : : #include <glib/gstdio.h>
30 : :
31 : : extern const char *gcr_ssh_askpass_executable;
32 : :
33 : : typedef struct {
34 : : GTlsInteraction *interaction;
35 : : GcrSshAskpass *askpass;
36 : : } Test;
37 : :
38 : : static void
39 : 3 : setup (Test *test,
40 : : gconstpointer password)
41 : : {
42 : 3 : test->interaction = mock_interaction_new (password);
43 : 3 : test->askpass = gcr_ssh_askpass_new (test->interaction);
44 : 3 : }
45 : :
46 : : static void
47 : 3 : teardown (Test *test,
48 : : gconstpointer unused)
49 : : {
50 : 3 : g_object_unref (test->interaction);
51 : :
52 : 3 : g_object_add_weak_pointer (G_OBJECT (test->askpass), (gpointer *)&test->askpass);
53 : 3 : g_object_unref (test->askpass);
54 [ - + ]: 3 : g_assert (test->askpass == NULL);
55 : 3 : }
56 : :
57 : : static void
58 : 2 : on_ssh_child (GPid pid,
59 : : gint status,
60 : : gpointer user_data)
61 : : {
62 : 2 : gint *out = user_data;
63 : 2 : *out = status;
64 : 2 : }
65 : :
66 : : static void
67 : 1 : test_ssh_keygen (Test *test,
68 : : gconstpointer password)
69 : : {
70 : :
71 : 1 : GError *error = NULL;
72 : : gint status;
73 : : GPid pid;
74 : :
75 : 1 : gchar *filename = g_build_filename (SRCDIR, "gcr", "fixtures", "pem-rsa-enc.key", NULL);
76 : 1 : gchar *argv[] = { "ssh-keygen", "-y", "-f", filename, NULL };
77 : :
78 [ - + ]: 1 : g_assert_cmpstr (password, ==, "booo");
79 : :
80 [ - + ]: 1 : if (g_chmod (filename, 0600) < 0)
81 : 0 : g_assert_not_reached ();
82 : :
83 : 1 : g_spawn_async (SRCDIR "/gcr/fixtures", argv, NULL,
84 : : G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_CLOEXEC_PIPES,
85 : 1 : gcr_ssh_askpass_child_setup, test->askpass, &pid, &error);
86 : :
87 : 1 : g_free (filename);
88 : :
89 [ - + ]: 1 : if (g_error_matches (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT)) {
90 : 0 : g_test_skip ("ssh-keygen not found");
91 : 0 : return;
92 : : }
93 : :
94 [ - + ]: 1 : g_assert_no_error (error);
95 : :
96 : 1 : status = -1;
97 : 1 : g_child_watch_add (pid, on_ssh_child, &status);
98 : :
99 [ + + ]: 7 : while (status == -1)
100 : 6 : g_main_context_iteration (NULL, TRUE);
101 : :
102 : 1 : g_spawn_check_exit_status (status, &error);
103 [ - + ]: 1 : g_assert_cmpint (status, ==, 0);
104 [ - + ]: 1 : g_assert_no_error (error);
105 : : }
106 : :
107 : : static void
108 : 1 : test_cancelled (Test *test,
109 : : gconstpointer password)
110 : : {
111 : :
112 : 1 : GError *error = NULL;
113 : : gint status;
114 : : GPid pid;
115 : :
116 : 1 : gchar *filename = g_build_filename (SRCDIR, "gcr", "fixtures", "pem-rsa-enc.key", NULL);
117 : 1 : gchar *argv[] = { "ssh-keygen", "-y", "-f", filename, NULL };
118 : :
119 [ - + ]: 1 : g_assert_cmpstr (password, ==, NULL);
120 : :
121 [ - + ]: 1 : if (g_chmod (filename, 0600) < 0)
122 : 0 : g_assert_not_reached ();
123 : :
124 : 1 : g_spawn_async (SRCDIR "/gcr/fixtures", argv, NULL,
125 : : G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_CLOEXEC_PIPES,
126 : 1 : gcr_ssh_askpass_child_setup, test->askpass, &pid, &error);
127 : :
128 : 1 : g_free (filename);
129 : :
130 [ - + ]: 1 : if (g_error_matches (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT)) {
131 : 0 : g_test_skip ("ssh-keygen not found");
132 : 0 : return;
133 : : }
134 : :
135 [ - + ]: 1 : g_assert_no_error (error);
136 : :
137 : 1 : status = -1;
138 : 1 : g_child_watch_add (pid, on_ssh_child, &status);
139 : :
140 [ + + ]: 7 : while (status == -1)
141 : 6 : g_main_context_iteration (NULL, TRUE);
142 : :
143 [ - + ]: 1 : g_assert_cmpint (status, !=, 0);
144 [ - + ]: 1 : g_assert_no_error (error);
145 : : }
146 : :
147 : : static void
148 : 1 : test_properties (Test *test,
149 : : gconstpointer unused)
150 : : {
151 : : GTlsInteraction *interaction;
152 : :
153 : 1 : g_object_get (test->askpass, "interaction", &interaction, NULL);
154 [ - + ]: 1 : g_assert (interaction == test->interaction);
155 : 1 : g_object_unref (interaction);
156 : :
157 [ - + ]: 1 : g_assert (gcr_ssh_askpass_get_interaction (test->askpass) == test->interaction);
158 : 1 : }
159 : :
160 : : int
161 : 1 : main (int argc, char **argv)
162 : : {
163 : 1 : g_test_init (&argc, &argv, NULL);
164 : 1 : g_set_prgname ("test-ssh-askpass");
165 : :
166 : 1 : gcr_ssh_askpass_executable = _GCR_TEST_SSH_ASKPASS_PATH;
167 : :
168 : 1 : g_test_add ("/gcr/ssh-askpass/ssh-keygen", Test, "booo", setup, test_ssh_keygen, teardown);
169 : 1 : g_test_add ("/gcr/ssh-askpass/cancelled", Test, NULL, setup, test_cancelled, teardown);
170 : 1 : g_test_add ("/gcr/ssh-askpass/properties", Test, NULL, setup, test_properties, teardown);
171 : :
172 : 1 : return g_test_run ();
173 : : }
|