Branch data Line data Source code
1 : : /*
2 : : * gnome-keyring
3 : : *
4 : : * Copyright (C) 2018 Red Hat, Inc.
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU Lesser General Public License as
8 : : * published by the Free Software Foundation; either version 2.1 of
9 : : * the License, or (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful, but
12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this program; if not, see
18 : : * <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Daiki Ueno
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "gcr-ssh-agent-preload.h"
26 : : #include "egg/egg-testing.h"
27 : :
28 : : #include <glib/gstdio.h>
29 : : #include <unistd.h>
30 : :
31 : : typedef struct {
32 : : gchar *directory;
33 : : GcrSshAgentPreload *preload;
34 : : } Test;
35 : :
36 : : static void
37 : 4 : setup (Test *test, gconstpointer unused)
38 : : {
39 : 4 : test->directory = egg_tests_create_scratch_directory (NULL, NULL);
40 : :
41 : 4 : egg_tests_copy_scratch_file (test->directory, SRCDIR "/gcr/fixtures/ssh-agent/id_rsa_plain");
42 : 4 : egg_tests_copy_scratch_file (test->directory, SRCDIR "/gcr/fixtures/ssh-agent/id_rsa_plain.pub");
43 : :
44 : 4 : test->preload = gcr_ssh_agent_preload_new (test->directory);
45 : 4 : }
46 : :
47 : : static void
48 : 4 : teardown (Test *test, gconstpointer unused)
49 : : {
50 : 4 : g_object_unref (test->preload);
51 : :
52 : 4 : egg_tests_remove_scratch_directory (test->directory);
53 : 4 : g_free (test->directory);
54 : 4 : }
55 : :
56 : : static void
57 : 1 : test_list (Test *test, gconstpointer unused)
58 : : {
59 : : GList *keys;
60 : :
61 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
62 [ - + ]: 1 : g_assert_cmpint (1, ==, g_list_length (keys));
63 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
64 : 1 : }
65 : :
66 : : static void
67 : 1 : test_added (Test *test, gconstpointer unused)
68 : : {
69 : : GList *keys;
70 : :
71 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
72 [ - + ]: 1 : g_assert_cmpint (1, ==, g_list_length (keys));
73 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
74 : :
75 : : /* Mtime must change so wait between tests */
76 : 1 : sleep (1);
77 : :
78 : 1 : egg_tests_copy_scratch_file (test->directory, SRCDIR "/gcr/fixtures/ssh-agent/id_ecdsa_plain");
79 : 1 : egg_tests_copy_scratch_file (test->directory, SRCDIR "/gcr/fixtures/ssh-agent/id_ecdsa_plain.pub");
80 : :
81 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
82 [ - + ]: 1 : g_assert_cmpint (2, ==, g_list_length (keys));
83 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
84 : 1 : }
85 : :
86 : : static void
87 : 1 : test_removed (Test *test, gconstpointer unused)
88 : : {
89 : : GList *keys;
90 : : gchar *path;
91 : :
92 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
93 [ - + ]: 1 : g_assert_cmpint (1, ==, g_list_length (keys));
94 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
95 : :
96 : : /* Mtime must change so wait between tests */
97 : 1 : sleep (1);
98 : :
99 : 1 : path = g_build_filename (test->directory, "id_rsa_plain.pub", NULL);
100 : 1 : g_unlink (path);
101 : 1 : g_free (path);
102 : :
103 : 1 : path = g_build_filename (test->directory, "id_rsa_plain", NULL);
104 : 1 : g_unlink (path);
105 : 1 : g_free (path);
106 : :
107 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
108 [ - + ]: 1 : g_assert_cmpint (0, ==, g_list_length (keys));
109 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
110 : 1 : }
111 : :
112 : : static void
113 : 1 : test_changed (Test *test, gconstpointer unused)
114 : : {
115 : : GList *keys;
116 : : gchar *path;
117 : : gchar *contents;
118 : : gsize length;
119 : : GError *error;
120 : : gchar *p;
121 : : gboolean ret;
122 : :
123 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
124 [ - + ]: 1 : g_assert_cmpint (1, ==, g_list_length (keys));
125 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
126 : :
127 : : /* Mtime must change so wait between tests */
128 : 1 : sleep (1);
129 : :
130 : 1 : path = g_build_filename (test->directory, "id_rsa_plain.pub", NULL);
131 : 1 : error = NULL;
132 : 1 : ret = g_file_get_contents (path, &contents, &length, &error);
133 [ - + ]: 1 : g_assert_true (ret);
134 [ - + ]: 1 : g_assert_no_error (error);
135 : :
136 : : #define COMMENT "comment"
137 : 1 : contents = g_realloc (contents, length + strlen (COMMENT) + 1);
138 : 1 : p = strchr (contents, '\n');
139 [ - + ]: 1 : g_assert_nonnull (p);
140 : 1 : memcpy (p, " " COMMENT "\n", strlen (COMMENT) + 2);
141 : 1 : error = NULL;
142 : 1 : ret = g_file_set_contents (path, contents, length + strlen (COMMENT), &error);
143 [ - + ]: 1 : g_assert_true (ret);
144 [ - + ]: 1 : g_assert_no_error (error);
145 : 1 : g_free (path);
146 : 1 : g_free (contents);
147 : :
148 : 1 : keys = gcr_ssh_agent_preload_get_keys (test->preload);
149 [ - + ]: 1 : g_assert_cmpint (1, ==, g_list_length (keys));
150 [ - + ]: 1 : g_assert_cmpstr (COMMENT, ==, ((GcrSshAgentKeyInfo *)keys->data)->comment);
151 : 1 : g_list_free_full (keys, (GDestroyNotify)gcr_ssh_agent_key_info_free);
152 : : #undef COMMENT
153 : 1 : }
154 : :
155 : : int
156 : 1 : main (int argc, char **argv)
157 : : {
158 : 1 : g_test_init (&argc, &argv, NULL);
159 : :
160 : 1 : g_test_add ("/ssh-agent/preload/list", Test, NULL, setup, test_list, teardown);
161 : 1 : g_test_add ("/ssh-agent/preload/added", Test, NULL, setup, test_added, teardown);
162 : 1 : g_test_add ("/ssh-agent/preload/removed", Test, NULL, setup, test_removed, teardown);
163 : 1 : g_test_add ("/ssh-agent/preload/changed", Test, NULL, setup, test_changed, teardown);
164 : :
165 : 1 : return g_test_run ();
166 : : }
|