Branch data Line data Source code
1 : : /* GLib testing framework examples and tests
2 : : *
3 : : * Copyright (C) 2026 Philip Withnall
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Philip Withnall <pwithnall@gnome.org>
21 : : */
22 : :
23 : : #include <locale.h>
24 : : #include <gio/gio.h>
25 : :
26 : : #include <string.h>
27 : : #include <unistd.h>
28 : :
29 : : #include "gdbus-tests.h"
30 : :
31 : : #ifdef G_OS_UNIX
32 : : #include <gio/gunixconnection.h>
33 : : #include <gio/gnetworkingprivate.h>
34 : : #include <gio/gunixsocketaddress.h>
35 : : #include <gio/gunixfdlist.h>
36 : : #endif
37 : :
38 : : #define GIO_COMPILATION 1
39 : : #include "gdbusauthmechanism.h"
40 : : #include "gdbusauthmechanismsha1.h"
41 : :
42 : : /* Vfunc wrappers copied from gdbusauthmechanism.c as they are not public. */
43 : : static gboolean
44 : 15 : dbus_auth_mechanism_is_supported (GDBusAuthMechanism *mechanism)
45 : : {
46 : 15 : return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->is_supported (mechanism);
47 : : }
48 : :
49 : : static GDBusAuthMechanismState
50 : 15 : dbus_auth_mechanism_client_get_state (GDBusAuthMechanism *mechanism)
51 : : {
52 : 15 : return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_state (mechanism);
53 : : }
54 : :
55 : : static gchar *
56 : 15 : dbus_auth_mechanism_client_initiate (GDBusAuthMechanism *mechanism,
57 : : GDBusConnectionFlags conn_flags,
58 : : size_t *out_initial_response_len)
59 : : {
60 : 15 : return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_initiate (mechanism,
61 : : conn_flags,
62 : : out_initial_response_len);
63 : : }
64 : :
65 : : static void
66 : 15 : dbus_auth_mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
67 : : const char *data,
68 : : size_t data_len)
69 : : {
70 : 15 : G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_data_receive (mechanism, data, data_len);
71 : 15 : }
72 : :
73 : : static char *
74 : 15 : dbus_auth_mechanism_client_get_reject_reason (GDBusAuthMechanism *mechanism)
75 : : {
76 : 15 : return G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_get_reject_reason (mechanism);
77 : : }
78 : :
79 : : static void
80 : 15 : dbus_auth_mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
81 : : {
82 : 15 : G_DBUS_AUTH_MECHANISM_GET_CLASS (mechanism)->client_shutdown (mechanism);
83 : 15 : }
84 : :
85 : : static void
86 : 1 : test_server_challenge_validation (void)
87 : : {
88 : : const struct
89 : : {
90 : : const char *server_challenge;
91 : : const char *expected_reject_reason_prefix;
92 : : }
93 : 1 : vectors[] = {
94 : : { "valid_context 123 456", "Problems looking up entry in keyring" },
95 : : { "invalid/context 123 456", "Malformed cookie_context" },
96 : : { "invalid.context 123 456", "Malformed cookie_context" },
97 : : { " 123 456", "Malformed cookie_context" },
98 : : { "😀 123 456", "Malformed cookie_context" },
99 : : { "invalid\ncontext 123 456", "Malformed cookie_context" },
100 : : { "invalid\rcontext 123 456", "Malformed cookie_context" },
101 : : { "invalid\tcontext 123 456", "Malformed cookie_context" },
102 : : { "invalid\\context 123 456", "Malformed cookie_context" },
103 : : { "valid_context 456", "Malformed cookie_id" },
104 : : { "valid_context 123notanumber 456", "Malformed cookie_id" },
105 : : { "valid_context -1 456", "Malformed cookie_id" },
106 : : { "valid_context 4294967296 456", "Malformed cookie_id" },
107 : : { "valid_context 123 ", "Malformed data" },
108 : : { "valid_context ", "Malformed data" },
109 : : };
110 : : GType mechanism_type;
111 : 1 : GDBusConnection *connection = NULL;
112 : :
113 : 1 : g_test_summary ("Test that GDBusAuthMechanismSha1 rejects various malformed server data lines");
114 : :
115 : : /* Briefly connect to the actual bus to ensure the GDBusAuth mechanisms are
116 : : * all registered. */
117 : 1 : session_bus_up ();
118 : :
119 : 1 : connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
120 : 1 : g_assert_nonnull (connection);
121 : 1 : g_clear_object (&connection);
122 : :
123 : 1 : session_bus_down ();
124 : :
125 : : /* Check that we now have the type ID for GDBusAuthMechanismSha1 */
126 : 1 : mechanism_type = g_type_from_name ("GDBusAuthMechanismSha1");
127 : 1 : g_assert_cmpint (mechanism_type, !=, 0);
128 : :
129 : 16 : for (size_t i = 0; i < G_N_ELEMENTS (vectors); i++)
130 : : {
131 : 15 : GDBusAuthMechanism *mechanism = NULL;
132 : 15 : char *data = NULL;
133 : 15 : size_t data_len = 0;
134 : 15 : char *reject_reason = NULL;
135 : :
136 : 15 : mechanism = g_object_new (mechanism_type, NULL);
137 : :
138 : 15 : if (!dbus_auth_mechanism_is_supported (mechanism))
139 : : {
140 : 0 : g_test_skip ("Mechanism not supported");
141 : 0 : g_clear_object (&mechanism);
142 : 0 : return;
143 : : }
144 : :
145 : 15 : data = dbus_auth_mechanism_client_initiate (mechanism,
146 : : G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
147 : : &data_len);
148 : 15 : g_free (data);
149 : :
150 : 15 : dbus_auth_mechanism_client_data_receive (mechanism, vectors[i].server_challenge, strlen (vectors[i].server_challenge));
151 : :
152 : 15 : g_assert_cmpint (dbus_auth_mechanism_client_get_state (mechanism), ==, G_DBUS_AUTH_MECHANISM_STATE_REJECTED);
153 : :
154 : 15 : reject_reason = dbus_auth_mechanism_client_get_reject_reason (mechanism);
155 : 15 : g_assert_true (g_str_has_prefix (reject_reason, vectors[i].expected_reject_reason_prefix));
156 : 15 : g_free (reject_reason);
157 : :
158 : 15 : dbus_auth_mechanism_client_shutdown (mechanism);
159 : :
160 : 15 : g_clear_object (&mechanism);
161 : : }
162 : : }
163 : :
164 : : int
165 : 1 : main (int argc,
166 : : char *argv[])
167 : : {
168 : 1 : setlocale (LC_ALL, "C");
169 : :
170 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
171 : :
172 : 1 : g_test_dbus_unset ();
173 : :
174 : 1 : g_test_add_func ("/gdbus/auth-mechanism-sha1/server-challenge-validation", test_server_challenge_validation);
175 : :
176 : 1 : return g_test_run ();
177 : : }
|