| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitauth.h" |
| 9 |
#include "cockpitws.h" |
| 10 |
|
| 11 |
#include "common/cockpitconf.h" |
| 12 |
#include "cockpiterror.h" |
| 13 |
#include "cockpitwebrequest-private.h" |
| 14 |
|
| 15 |
#include "websocket.h" |
| 16 |
|
| 17 |
#include "testlib/cockpittest.h" |
| 18 |
#include "testlib/mock-auth.h" |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
/* Mock override these from other files */ |
| 23 |
extern const gchar *cockpit_config_file; |
| 24 |
extern const gchar *cockpit_ws_max_startups; |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
CockpitAuth *auth; |
| 28 |
} Test; |
| 29 |
|
| 30 |
static void |
| 31 |
setup (Test *test, |
| 32 |
gconstpointer data) |
| 33 |
{ |
| 34 |
test->auth = cockpit_auth_new (FALSE, COCKPIT_AUTH_NONE); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
teardown (Test *test, |
| 39 |
gconstpointer data) |
| 40 |
{ |
| 41 |
g_object_unref (test->auth); |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
setup_normal (Test *test, |
| 46 |
gconstpointer data) |
| 47 |
{ |
| 48 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit.conf"; |
| 49 |
test->auth = cockpit_auth_new (FALSE, COCKPIT_AUTH_NONE); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
setup_alt_config (Test *test, |
| 54 |
gconstpointer data) |
| 55 |
{ |
| 56 |
cockpit_config_file = SRCDIR "/src/common/mock-config/cockpit/cockpit-alt.conf"; |
| 57 |
test->auth = cockpit_auth_new (FALSE, COCKPIT_AUTH_NONE); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
teardown_normal (Test *test, |
| 62 |
gconstpointer data) |
| 63 |
{ |
| 64 |
cockpit_assert_expected (); |
| 65 |
g_object_unref (test->auth); |
| 66 |
cockpit_conf_cleanup (); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
on_ready_get_result (GObject *source, |
| 71 |
GAsyncResult *result, |
| 72 |
gpointer user_data) |
| 73 |
{ |
| 74 |
GAsyncResult **retval = user_data; |
| 75 |
g_assert (retval != NULL); |
| 76 |
g_assert (*retval == NULL); |
| 77 |
*retval = g_object_ref (result); |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
test_application (Test *test, |
| 82 |
gconstpointer data) |
| 83 |
{ |
| 84 |
gchar *application = NULL; |
| 85 |
gboolean is_host = FALSE; |
| 86 |
|
| 87 |
application = cockpit_auth_parse_application ("/", &is_host); |
| 88 |
g_assert_cmpstr ("cockpit", ==, application); |
| 89 |
g_assert_false (is_host); |
| 90 |
g_clear_pointer (&application, g_free); |
| 91 |
|
| 92 |
application = cockpit_auth_parse_application ("/=", &is_host); |
| 93 |
g_assert_cmpstr ("cockpit", ==, application); |
| 94 |
g_assert_false (is_host); |
| 95 |
g_clear_pointer (&application, g_free); |
| 96 |
|
| 97 |
application = cockpit_auth_parse_application ("/other/other", &is_host); |
| 98 |
g_assert_cmpstr ("cockpit", ==, application); |
| 99 |
g_assert_false (is_host); |
| 100 |
g_clear_pointer (&application, g_free); |
| 101 |
|
| 102 |
application = cockpit_auth_parse_application ("/=other/other", &is_host); |
| 103 |
g_assert_true (is_host); |
| 104 |
g_assert_cmpstr ("cockpit+=other", ==, application); |
| 105 |
g_clear_pointer (&application, g_free); |
| 106 |
|
| 107 |
application = cockpit_auth_parse_application ("/=other", &is_host); |
| 108 |
g_assert_true (is_host); |
| 109 |
g_assert_cmpstr ("cockpit+=other", ==, application); |
| 110 |
g_clear_pointer (&application, g_free); |
| 111 |
|
| 112 |
application = cockpit_auth_parse_application ("/=other/", &is_host); |
| 113 |
g_assert_true (is_host); |
| 114 |
g_assert_cmpstr ("cockpit+=other", ==, application); |
| 115 |
g_clear_pointer (&application, g_free); |
| 116 |
|
| 117 |
application = cockpit_auth_parse_application ("/cockpit", &is_host); |
| 118 |
g_assert_cmpstr ("cockpit", ==, application); |
| 119 |
g_assert_false (is_host); |
| 120 |
g_clear_pointer (&application, g_free); |
| 121 |
|
| 122 |
application = cockpit_auth_parse_application ("/cockpit/login", &is_host); |
| 123 |
g_assert_cmpstr ("cockpit", ==, application); |
| 124 |
g_assert_false (is_host); |
| 125 |
g_clear_pointer (&application, g_free); |
| 126 |
|
| 127 |
application = cockpit_auth_parse_application ("/cockpit+application", &is_host); |
| 128 |
g_assert_cmpstr ("cockpit+application", ==, application); |
| 129 |
g_assert_false (is_host); |
| 130 |
g_clear_pointer (&application, g_free); |
| 131 |
|
| 132 |
application = cockpit_auth_parse_application ("/cockpit+application/", &is_host); |
| 133 |
g_assert_false (is_host); |
| 134 |
g_assert_cmpstr ("cockpit+application", ==, application); |
| 135 |
g_clear_pointer (&application, g_free); |
| 136 |
|
| 137 |
application = cockpit_auth_parse_application ("/cockpit+application/other/other", &is_host); |
| 138 |
g_assert_false (is_host); |
| 139 |
g_assert_cmpstr ("cockpit+application", ==, application); |
| 140 |
g_clear_pointer (&application, g_free); |
| 141 |
} |
| 142 |
|
| 143 |
typedef struct { |
| 144 |
const gchar *superuser_mode; |
| 145 |
gboolean expect_stored_password; |
| 146 |
} UserpassFixture; |
| 147 |
|
| 148 |
static const UserpassFixture fixture_superuser_any = { |
| 149 |
.superuser_mode = "any", |
| 150 |
.expect_stored_password = TRUE |
| 151 |
}; |
| 152 |
|
| 153 |
static void |
| 154 |
test_userpass_cookie_check (Test *test, |
| 155 |
gconstpointer data) |
| 156 |
{ |
| 157 |
const UserpassFixture *fix = data; |
| 158 |
GAsyncResult *result = NULL; |
| 159 |
CockpitWebService *service; |
| 160 |
CockpitWebService *prev_service; |
| 161 |
CockpitCreds *creds; |
| 162 |
CockpitCreds *prev_creds; |
| 163 |
JsonObject *response = NULL; |
| 164 |
GError *error = NULL; |
| 165 |
GHashTable *headers; |
| 166 |
|
| 167 |
headers = mock_auth_basic_header ("me", "this is the password"); |
| 168 |
if (fix && fix->superuser_mode) |
| 169 |
g_hash_table_insert (headers, g_strdup ("X-Superuser"), g_strdup (fix->superuser_mode)); |
| 170 |
cockpit_auth_login_async (test->auth, |
| 171 |
WebRequest(.path = "/cockpit/", .headers = headers), |
| 172 |
on_ready_get_result, &result); |
| 173 |
g_hash_table_unref (headers); |
| 174 |
|
| 175 |
while (result == NULL) |
| 176 |
g_main_context_iteration (NULL, TRUE); |
| 177 |
|
| 178 |
headers = web_socket_util_new_headers (); |
| 179 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 180 |
g_assert_no_error (error); |
| 181 |
|
| 182 |
/* Get the service */ |
| 183 |
mock_auth_include_cookie_as_if_client (headers, headers, "cockpit"); |
| 184 |
service = cockpit_auth_check_cookie (test->auth, |
| 185 |
WebRequest(.path="/cockpit", .headers=headers)); |
| 186 |
|
| 187 |
g_object_unref (result); |
| 188 |
g_assert (service != NULL); |
| 189 |
g_assert (response != NULL); |
| 190 |
|
| 191 |
creds = cockpit_web_service_get_creds (service); |
| 192 |
g_assert_cmpstr ("me", ==, cockpit_creds_get_user (creds)); |
| 193 |
g_assert_cmpstr ("cockpit", ==, cockpit_creds_get_application (creds)); |
| 194 |
if (fix && fix->expect_stored_password) |
| 195 |
g_assert_cmpstr ("this is the password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 196 |
else |
| 197 |
g_assert_null (cockpit_creds_get_password (creds)); |
| 198 |
|
| 199 |
prev_service = service; |
| 200 |
g_object_unref (service); |
| 201 |
service = NULL; |
| 202 |
|
| 203 |
prev_creds = creds; |
| 204 |
creds = NULL; |
| 205 |
|
| 206 |
mock_auth_include_cookie_as_if_client (headers, headers, "cockpit"); |
| 207 |
|
| 208 |
service = cockpit_auth_check_cookie (test->auth, |
| 209 |
WebRequest(.path="/cockpit", .headers=headers)); |
| 210 |
g_assert (prev_service == service); |
| 211 |
|
| 212 |
creds = cockpit_web_service_get_creds (service); |
| 213 |
g_assert (prev_creds == creds); |
| 214 |
|
| 215 |
g_assert_cmpstr ("me", ==, cockpit_creds_get_user (creds)); |
| 216 |
if (fix && fix->expect_stored_password) |
| 217 |
g_assert_cmpstr ("this is the password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 218 |
else |
| 219 |
g_assert_null (cockpit_creds_get_password (creds)); |
| 220 |
|
| 221 |
g_hash_table_destroy (headers); |
| 222 |
g_object_unref (service); |
| 223 |
json_object_unref (response); |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
test_userpass_bad (Test *test, |
| 228 |
gconstpointer data) |
| 229 |
{ |
| 230 |
GAsyncResult *result = NULL; |
| 231 |
GError *error = NULL; |
| 232 |
JsonObject *response; |
| 233 |
GHashTable *headers; |
| 234 |
|
| 235 |
headers = mock_auth_basic_header ("me", "bad"); |
| 236 |
cockpit_auth_login_async (test->auth, |
| 237 |
WebRequest(.path="/cockpit", .headers=headers), |
| 238 |
on_ready_get_result, &result); |
| 239 |
g_hash_table_unref (headers); |
| 240 |
|
| 241 |
while (result == NULL) |
| 242 |
g_main_context_iteration (NULL, TRUE); |
| 243 |
|
| 244 |
headers = web_socket_util_new_headers (); |
| 245 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 246 |
g_object_unref (result); |
| 247 |
|
| 248 |
g_assert (response != NULL); |
| 249 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, "authentication-failed"); |
| 250 |
g_clear_pointer (&response, json_object_unref); |
| 251 |
|
| 252 |
g_assert_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED); |
| 253 |
g_clear_error (&error); |
| 254 |
|
| 255 |
g_hash_table_destroy (headers); |
| 256 |
} |
| 257 |
|
| 258 |
static void |
| 259 |
test_userpass_emptypass (Test *test, |
| 260 |
gconstpointer data) |
| 261 |
{ |
| 262 |
GAsyncResult *result = NULL; |
| 263 |
JsonObject *response; |
| 264 |
GError *error = NULL; |
| 265 |
GHashTable *headers; |
| 266 |
|
| 267 |
headers = mock_auth_basic_header ("aaaaaa", ""); |
| 268 |
cockpit_auth_login_async (test->auth, |
| 269 |
WebRequest(.path="/cockpit", .headers=headers), |
| 270 |
on_ready_get_result, &result); |
| 271 |
g_hash_table_unref (headers); |
| 272 |
|
| 273 |
while (result == NULL) |
| 274 |
g_main_context_iteration (NULL, TRUE); |
| 275 |
|
| 276 |
headers = web_socket_util_new_headers (); |
| 277 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 278 |
g_object_unref (result); |
| 279 |
|
| 280 |
g_assert (response != NULL); |
| 281 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, "authentication-failed"); |
| 282 |
g_clear_pointer (&response, json_object_unref); |
| 283 |
|
| 284 |
g_assert_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED); |
| 285 |
g_clear_error (&error); |
| 286 |
|
| 287 |
g_hash_table_destroy (headers); |
| 288 |
} |
| 289 |
|
| 290 |
static void |
| 291 |
test_headers_bad (Test *test, |
| 292 |
gconstpointer data) |
| 293 |
{ |
| 294 |
GHashTable *headers; |
| 295 |
|
| 296 |
headers = web_socket_util_new_headers (); |
| 297 |
|
| 298 |
/* Bad version */ |
| 299 |
g_hash_table_insert (headers, g_strdup ("Cookie"), g_strdup ("CockpitAuth=v=1;k=blah")); |
| 300 |
if (cockpit_auth_check_cookie (test->auth, |
| 301 |
WebRequest(.path="/cockpit", .headers=headers))) |
| 302 |
g_assert_not_reached (); |
| 303 |
|
| 304 |
/* Bad hash */ |
| 305 |
g_hash_table_remove_all (headers); |
| 306 |
g_hash_table_insert (headers, g_strdup ("Cookie"), g_strdup ("CockpitAuth=v=2;k=blah")); |
| 307 |
if (cockpit_auth_check_cookie (test->auth, |
| 308 |
WebRequest(.path="/cockpit", .headers=headers))) |
| 309 |
g_assert_not_reached (); |
| 310 |
|
| 311 |
/* Bad encoding */ |
| 312 |
g_hash_table_remove_all (headers); |
| 313 |
g_hash_table_insert (headers, g_strdup ("Cookie"), g_strdup ("cockpit=d")); |
| 314 |
if (cockpit_auth_check_cookie (test->auth, |
| 315 |
WebRequest(.path="/cockpit", .headers=headers))) |
| 316 |
g_assert_not_reached (); |
| 317 |
|
| 318 |
g_hash_table_destroy (headers); |
| 319 |
} |
| 320 |
|
| 321 |
static gboolean |
| 322 |
on_timeout_set_flag (gpointer data) |
| 323 |
{ |
| 324 |
gboolean *flag = data; |
| 325 |
g_assert (*flag == FALSE); |
| 326 |
*flag = TRUE; |
| 327 |
return FALSE; |
| 328 |
} |
| 329 |
|
| 330 |
static gboolean |
| 331 |
on_idling_set_flag (CockpitAuth *auth, |
| 332 |
gpointer data) |
| 333 |
{ |
| 334 |
gboolean *flag = data; |
| 335 |
*flag = TRUE; |
| 336 |
return FALSE; |
| 337 |
} |
| 338 |
|
| 339 |
static void |
| 340 |
test_idle_timeout (Test *test, |
| 341 |
gconstpointer data) |
| 342 |
{ |
| 343 |
GAsyncResult *result = NULL; |
| 344 |
CockpitWebService *service; |
| 345 |
JsonObject *login_response; |
| 346 |
GError *error = NULL; |
| 347 |
GHashTable *headers; |
| 348 |
gboolean flag = FALSE; |
| 349 |
gboolean idling = FALSE; |
| 350 |
|
| 351 |
/* The idle timeout is one second */ |
| 352 |
g_assert (cockpit_ws_service_idle == 1); |
| 353 |
|
| 354 |
headers = mock_auth_basic_header ("me", "this is the password"); |
| 355 |
cockpit_auth_login_async (test->auth, |
| 356 |
WebRequest(.path="/cockpit", .headers=headers), |
| 357 |
on_ready_get_result, &result); |
| 358 |
g_hash_table_unref (headers); |
| 359 |
|
| 360 |
while (result == NULL) |
| 361 |
g_main_context_iteration (NULL, TRUE); |
| 362 |
|
| 363 |
headers = web_socket_util_new_headers (); |
| 364 |
login_response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 365 |
g_assert (login_response != NULL); |
| 366 |
json_object_unref (login_response); |
| 367 |
g_object_unref (result); |
| 368 |
g_assert_no_error (error); |
| 369 |
|
| 370 |
/* Logged in ... the webservice is idle though */ |
| 371 |
mock_auth_include_cookie_as_if_client (headers, headers, "cockpit"); |
| 372 |
service = cockpit_auth_check_cookie (test->auth, WebRequest(.path="/cockpit", .headers=headers)); |
| 373 |
g_assert (service != NULL); |
| 374 |
g_assert (cockpit_web_service_get_idling (service)); |
| 375 |
g_object_unref (service); |
| 376 |
|
| 377 |
g_signal_connect (test->auth, "idling", G_CALLBACK (on_idling_set_flag), &idling); |
| 378 |
|
| 379 |
/* Now wait for 2 seconds, and the service should be gone */ |
| 380 |
g_timeout_add_seconds (2, on_timeout_set_flag, &flag); |
| 381 |
while (!flag) |
| 382 |
g_main_context_iteration (NULL, TRUE); |
| 383 |
|
| 384 |
/* Timeout, no longer logged in */ |
| 385 |
service = cockpit_auth_check_cookie (test->auth, WebRequest(.path="/cockpit", .headers=headers)); |
| 386 |
g_assert (service == NULL); |
| 387 |
|
| 388 |
/* Now wait for 3 seconds, and the auth should have said its idling */ |
| 389 |
flag = FALSE; |
| 390 |
g_timeout_add_seconds (3, on_timeout_set_flag, &flag); |
| 391 |
while (!flag) |
| 392 |
g_main_context_iteration (NULL, TRUE); |
| 393 |
|
| 394 |
g_assert (idling == TRUE); |
| 395 |
g_hash_table_destroy (headers); |
| 396 |
} |
| 397 |
|
| 398 |
static void |
| 399 |
test_process_timeout (Test *test, |
| 400 |
gconstpointer data) |
| 401 |
{ |
| 402 |
gboolean idling = FALSE; |
| 403 |
|
| 404 |
g_signal_connect (test->auth, "idling", G_CALLBACK (on_idling_set_flag), &idling); |
| 405 |
|
| 406 |
while (!idling) |
| 407 |
g_main_context_iteration (NULL, TRUE); |
| 408 |
} |
| 409 |
|
| 410 |
static void |
| 411 |
test_max_startups (Test *test, |
| 412 |
gconstpointer data) |
| 413 |
{ |
| 414 |
GAsyncResult *result1 = NULL; |
| 415 |
GAsyncResult *result2 = NULL; |
| 416 |
GAsyncResult *result3 = NULL; |
| 417 |
|
| 418 |
JsonObject *response; |
| 419 |
|
| 420 |
GHashTable *headers_slow; |
| 421 |
GHashTable *headers_fail; |
| 422 |
|
| 423 |
GError *error1 = NULL; |
| 424 |
GError *error2 = NULL; |
| 425 |
GError *error3 = NULL; |
| 426 |
|
| 427 |
cockpit_expect_message ("Request dropped; too many startup connections: 2"); |
| 428 |
|
| 429 |
headers_slow = web_socket_util_new_headers (); |
| 430 |
headers_fail = web_socket_util_new_headers (); |
| 431 |
g_hash_table_insert (headers_slow, g_strdup ("Authorization"), g_strdup ("testscheme failslow")); |
| 432 |
g_hash_table_insert (headers_fail, g_strdup ("Authorization"), g_strdup ("testscheme fail")); |
| 433 |
|
| 434 |
/* Slow request that takes a while to complete */ |
| 435 |
cockpit_auth_login_async (test->auth, |
| 436 |
WebRequest(.path="/cockpit", .headers=headers_slow), |
| 437 |
on_ready_get_result, &result1); |
| 438 |
|
| 439 |
/* Request that gets dropped */ |
| 440 |
cockpit_auth_login_async (test->auth, |
| 441 |
WebRequest(.path="/cockpit", .headers=headers_fail), |
| 442 |
on_ready_get_result, &result2); |
| 443 |
while (result2 == NULL) |
| 444 |
g_main_context_iteration (NULL, TRUE); |
| 445 |
response = cockpit_auth_login_finish (test->auth, result2, NULL, NULL, &error2); |
| 446 |
g_object_unref (result2); |
| 447 |
g_assert (response == NULL); |
| 448 |
g_assert_cmpstr ("Connection closed by host", ==, error2->message); |
| 449 |
|
| 450 |
/* Wait for first request to finish */ |
| 451 |
while (result1 == NULL) |
| 452 |
g_main_context_iteration (NULL, TRUE); |
| 453 |
response = cockpit_auth_login_finish (test->auth, result1, NULL, NULL, &error1); |
| 454 |
g_object_unref (result1); |
| 455 |
g_assert (response != NULL); |
| 456 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, "authentication-failed"); |
| 457 |
g_assert_cmpstr ("Authentication failed", ==, error1->message); |
| 458 |
g_clear_pointer (&response, json_object_unref); |
| 459 |
|
| 460 |
/* Now that first is finished we can successfully run another one */ |
| 461 |
g_hash_table_insert (headers_fail, g_strdup ("Authorization"), g_strdup ("testscheme fail")); |
| 462 |
cockpit_auth_login_async (test->auth, |
| 463 |
WebRequest(.path="/cockpit", .headers=headers_fail), |
| 464 |
on_ready_get_result, &result3); |
| 465 |
while (result3 == NULL) |
| 466 |
g_main_context_iteration (NULL, TRUE); |
| 467 |
response = cockpit_auth_login_finish (test->auth, result3, NULL, NULL, &error3); |
| 468 |
g_object_unref (result3); |
| 469 |
g_assert (response != NULL); |
| 470 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, "authentication-failed"); |
| 471 |
g_clear_pointer (&response, json_object_unref); |
| 472 |
g_assert_cmpstr ("Authentication failed", ==, error3->message); |
| 473 |
|
| 474 |
g_clear_error (&error1); |
| 475 |
g_clear_error (&error2); |
| 476 |
g_clear_error (&error3); |
| 477 |
|
| 478 |
g_hash_table_destroy (headers_fail); |
| 479 |
g_hash_table_destroy (headers_slow); |
| 480 |
} |
| 481 |
|
| 482 |
typedef struct { |
| 483 |
const gchar *header; |
| 484 |
const gchar *error_message; |
| 485 |
const gchar *warning; |
| 486 |
const gchar *path; |
| 487 |
const gchar *init_problem; |
| 488 |
int error_code; |
| 489 |
} ErrorFixture; |
| 490 |
|
| 491 |
typedef struct { |
| 492 |
const gchar *data; |
| 493 |
const gchar *warning; |
| 494 |
const gchar *header; |
| 495 |
const gchar *path; |
| 496 |
const gchar *user; |
| 497 |
const gchar *application; |
| 498 |
const gchar *cookie_name; |
| 499 |
gboolean expect_stored_password; |
| 500 |
} SuccessFixture; |
| 501 |
|
| 502 |
static void |
| 503 |
test_custom_fail (Test *test, |
| 504 |
gconstpointer data) |
| 505 |
{ |
| 506 |
GAsyncResult *result = NULL; |
| 507 |
JsonObject *response; |
| 508 |
GError *error = NULL; |
| 509 |
GHashTable *headers; |
| 510 |
const ErrorFixture *fix = data; |
| 511 |
const gchar *path = fix->path ? fix->path : "/cockpit"; |
| 512 |
|
| 513 |
if (fix->warning) |
| 514 |
cockpit_expect_warning (fix->warning); |
| 515 |
|
| 516 |
headers = web_socket_util_new_headers (); |
| 517 |
g_hash_table_insert (headers, g_strdup ("Authorization"), g_strdup (fix->header)); |
| 518 |
|
| 519 |
cockpit_auth_login_async (test->auth, WebRequest(.path=path, .headers=headers), on_ready_get_result, &result); |
| 520 |
g_hash_table_unref (headers); |
| 521 |
|
| 522 |
while (result == NULL) |
| 523 |
g_main_context_iteration (NULL, TRUE); |
| 524 |
|
| 525 |
headers = web_socket_util_new_headers (); |
| 526 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 527 |
g_object_unref (result); |
| 528 |
|
| 529 |
if (fix->init_problem) |
| 530 |
{ |
| 531 |
g_assert (response != NULL); |
| 532 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, fix->init_problem); |
| 533 |
g_clear_pointer (&response, json_object_unref); |
| 534 |
} |
| 535 |
else |
| 536 |
{ |
| 537 |
g_assert (response == NULL); |
| 538 |
} |
| 539 |
|
| 540 |
if (fix->error_code) |
| 541 |
g_assert_error (error, COCKPIT_ERROR, fix->error_code); |
| 542 |
else |
| 543 |
g_assert (error != NULL); |
| 544 |
|
| 545 |
g_assert_cmpstr (fix->error_message, ==, error->message); |
| 546 |
g_clear_error (&error); |
| 547 |
|
| 548 |
g_hash_table_destroy (headers); |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
test_custom_timeout (Test *test, |
| 553 |
gconstpointer data) |
| 554 |
{ |
| 555 |
cockpit_expect_message ("*session timed out*"); |
| 556 |
test_custom_fail (test, data); |
| 557 |
} |
| 558 |
|
| 559 |
static void |
| 560 |
test_bad_command (Test *test, |
| 561 |
gconstpointer data) |
| 562 |
{ |
| 563 |
cockpit_expect_possible_log ("cockpit-protocol", G_LOG_LEVEL_WARNING, |
| 564 |
"*couldn't recv*"); |
| 565 |
cockpit_expect_possible_log ("cockpit-ws", G_LOG_LEVEL_WARNING, |
| 566 |
"*Auth pipe closed: internal-error*"); |
| 567 |
cockpit_expect_possible_log ("cockpit-ws", G_LOG_LEVEL_WARNING, |
| 568 |
"*Auth pipe closed: not-found*"); |
| 569 |
cockpit_expect_possible_log ("cockpit-ws", G_LOG_LEVEL_WARNING, |
| 570 |
"*Auth pipe closed: terminated*"); |
| 571 |
cockpit_expect_possible_log ("cockpit-ws", G_LOG_LEVEL_WARNING, |
| 572 |
"*couldn't write: Connection refused*"); |
| 573 |
cockpit_expect_possible_log ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 574 |
"*couldn't write: Connection refused*"); |
| 575 |
cockpit_expect_possible_log ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 576 |
"*couldn't send: Connection refused*"); |
| 577 |
test_custom_fail (test, data); |
| 578 |
} |
| 579 |
|
| 580 |
static void |
| 581 |
test_custom_success (Test *test, |
| 582 |
gconstpointer data) |
| 583 |
{ |
| 584 |
GAsyncResult *result = NULL; |
| 585 |
CockpitWebService *service; |
| 586 |
JsonObject *response; |
| 587 |
CockpitCreds *creds; |
| 588 |
GError *error = NULL; |
| 589 |
GHashTable *headers; |
| 590 |
JsonObject *login_data; |
| 591 |
const SuccessFixture *fix = data; |
| 592 |
const gchar *path = fix->path ? fix->path : "/cockpit"; |
| 593 |
const gchar *application = fix->application ? fix->application : "cockpit"; |
| 594 |
|
| 595 |
if (fix->warning) |
| 596 |
cockpit_expect_warning (fix->warning); |
| 597 |
|
| 598 |
headers = web_socket_util_new_headers (); |
| 599 |
g_hash_table_insert (headers, g_strdup ("Authorization"), g_strdup (fix->header)); |
| 600 |
cockpit_auth_login_async (test->auth, |
| 601 |
WebRequest(.path=path, .headers=headers), |
| 602 |
on_ready_get_result, &result); |
| 603 |
g_hash_table_unref (headers); |
| 604 |
|
| 605 |
while (result == NULL) |
| 606 |
g_main_context_iteration (NULL, TRUE); |
| 607 |
|
| 608 |
headers = web_socket_util_new_headers (); |
| 609 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 610 |
g_object_unref (result); |
| 611 |
g_assert_no_error (error); |
| 612 |
g_assert (response != NULL); |
| 613 |
json_object_unref (response); |
| 614 |
|
| 615 |
mock_auth_include_cookie_as_if_client (headers, headers, |
| 616 |
fix->cookie_name ? fix->cookie_name : "cockpit"); |
| 617 |
service = cockpit_auth_check_cookie (test->auth, WebRequest(.path=path, .headers=headers)); |
| 618 |
creds = cockpit_web_service_get_creds (service); |
| 619 |
g_assert_cmpstr (application, ==, cockpit_creds_get_application (creds)); |
| 620 |
if (fix->expect_stored_password) |
| 621 |
g_assert_cmpstr ("this is the machine password", ==, g_bytes_get_data (cockpit_creds_get_password (creds), NULL)); |
| 622 |
else |
| 623 |
g_assert_null (cockpit_creds_get_password (creds)); |
| 624 |
|
| 625 |
login_data = cockpit_creds_get_login_data (creds); |
| 626 |
if (fix->data) |
| 627 |
g_assert_cmpstr (json_object_get_string_member (login_data, "login"), |
| 628 |
==, fix->data); |
| 629 |
else |
| 630 |
g_assert_null (login_data); |
| 631 |
|
| 632 |
g_hash_table_destroy (headers); |
| 633 |
g_object_unref (service); |
| 634 |
} |
| 635 |
|
| 636 |
static const SuccessFixture fixture_ssh_basic = { |
| 637 |
.warning = NULL, |
| 638 |
.data = NULL, |
| 639 |
.header = "Basic bWU6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=" |
| 640 |
}; |
| 641 |
|
| 642 |
static const SuccessFixture fixture_ssh_not_authorized = { |
| 643 |
.warning = NULL, |
| 644 |
.data = NULL, |
| 645 |
.header = "Basic bWU6dGhpcyBpcyB0aGUgcGFzc3dvcmQ=", |
| 646 |
}; |
| 647 |
|
| 648 |
static const SuccessFixture fixture_ssh_remote_basic = { |
| 649 |
.warning = NULL, |
| 650 |
.data = NULL, |
| 651 |
.header = "Basic cmVtb3RlLXVzZXI6dGhpcyBpcyB0aGUgbWFjaGluZSBwYXNzd29yZA==", |
| 652 |
.path = "/cockpit+=machine", |
| 653 |
.user = "remote-user", |
| 654 |
.application = "cockpit+=machine", |
| 655 |
.cookie_name = "machine-cockpit+machine", |
| 656 |
.expect_stored_password = TRUE |
| 657 |
}; |
| 658 |
|
| 659 |
static const SuccessFixture fixture_ssh_no_data = { |
| 660 |
.warning = NULL, |
| 661 |
.data = NULL, |
| 662 |
.header = "testsshscheme success" |
| 663 |
}; |
| 664 |
|
| 665 |
static const SuccessFixture fixture_ssh_remote_switched = { |
| 666 |
.data = NULL, |
| 667 |
.header = "testscheme ssh-remote-switch", |
| 668 |
.path = "/cockpit+=machine", |
| 669 |
.application = "cockpit+=machine", |
| 670 |
.cookie_name = "machine-cockpit+machine" |
| 671 |
}; |
| 672 |
|
| 673 |
static const SuccessFixture fixture_ssh_alt_default = { |
| 674 |
.data = NULL, |
| 675 |
.header = "testsshscheme ssh-alt-default", |
| 676 |
}; |
| 677 |
|
| 678 |
static const SuccessFixture fixture_ssh_alt = { |
| 679 |
.data = NULL, |
| 680 |
.path = "/cockpit+=machine", |
| 681 |
.application = "cockpit+=machine", |
| 682 |
.header = "testsshscheme ssh-alt-machine", |
| 683 |
.cookie_name = "machine-cockpit+machine" |
| 684 |
}; |
| 685 |
|
| 686 |
static const ErrorFixture fixture_bad_conversation = { |
| 687 |
.header = "X-Conversation conversation-id xxx", |
| 688 |
.error_message = "Invalid conversation token", |
| 689 |
}; |
| 690 |
|
| 691 |
static const ErrorFixture fixture_ssh_basic_failed = { |
| 692 |
.error_message = "Authentication failed", |
| 693 |
.header = "Basic dXNlcjp0aGlzIGlzIHRoZSBwYXNzd29yZA==", |
| 694 |
.init_problem = "authentication-failed", |
| 695 |
}; |
| 696 |
|
| 697 |
static const ErrorFixture fixture_ssh_remote_basic_failed = { |
| 698 |
.error_message = "Authentication failed", |
| 699 |
.header = "Basic d3Jvbmc6dGhpcyBpcyB0aGUgbWFjaGluZSBwYXNzd29yZA==", |
| 700 |
.path = "/cockpit+=machine", |
| 701 |
.init_problem = "authentication-failed", |
| 702 |
}; |
| 703 |
|
| 704 |
static const ErrorFixture fixture_ssh_not_supported = { |
| 705 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 706 |
.error_message = "Authentication failed: authentication-not-supported", |
| 707 |
.header = "testsshscheme not-supported", |
| 708 |
.init_problem = "authentication-not-supported", |
| 709 |
}; |
| 710 |
|
| 711 |
static const ErrorFixture fixture_ssh_auth_failed = { |
| 712 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 713 |
.error_message = "Authentication failed", |
| 714 |
.header = "testsshscheme ssh-fail", |
| 715 |
.init_problem = "authentication-failed", |
| 716 |
}; |
| 717 |
|
| 718 |
static const ErrorFixture fixture_ssh_auth_with_error = { |
| 719 |
.error_code = COCKPIT_ERROR_FAILED, |
| 720 |
.error_message = "Authentication failed: unknown: detail for error", |
| 721 |
.header = "testsshscheme with-error", |
| 722 |
.init_problem = "unknown", |
| 723 |
}; |
| 724 |
|
| 725 |
static const SuccessFixture fixture_no_cookie = { |
| 726 |
.warning = NULL, |
| 727 |
.data = NULL, |
| 728 |
.header = "testscheme no-cookie" |
| 729 |
}; |
| 730 |
|
| 731 |
static const SuccessFixture fixture_no_data = { |
| 732 |
.warning = NULL, |
| 733 |
.data = NULL, |
| 734 |
.header = "testscheme success" |
| 735 |
}; |
| 736 |
|
| 737 |
static const SuccessFixture fixture_data_then_success = { |
| 738 |
.warning = NULL, |
| 739 |
.data = "data", |
| 740 |
.header = "testscheme data-then-success" |
| 741 |
}; |
| 742 |
|
| 743 |
static const ErrorFixture fixture_bad_command = { |
| 744 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 745 |
.error_message = "Authentication not available", |
| 746 |
.header = "badcommand bad", |
| 747 |
}; |
| 748 |
|
| 749 |
static const ErrorFixture fixture_auth_failed = { |
| 750 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 751 |
.error_message = "Authentication failed", |
| 752 |
.header = "testscheme fail", |
| 753 |
.init_problem = "authentication-failed", |
| 754 |
}; |
| 755 |
|
| 756 |
static const ErrorFixture fixture_auth_denied = { |
| 757 |
.error_code = COCKPIT_ERROR_PERMISSION_DENIED, |
| 758 |
.error_message = "Permission denied", |
| 759 |
.header = "testscheme denied", |
| 760 |
.init_problem = "access-denied", |
| 761 |
}; |
| 762 |
|
| 763 |
static const ErrorFixture fixture_auth_with_error = { |
| 764 |
.error_code = COCKPIT_ERROR_FAILED, |
| 765 |
.error_message = "Authentication failed: unknown: detail for error", |
| 766 |
.header = "testscheme with-error", |
| 767 |
.init_problem = "unknown", |
| 768 |
}; |
| 769 |
|
| 770 |
static const ErrorFixture fixture_auth_none = { |
| 771 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 772 |
.error_message = "Authentication disabled", |
| 773 |
.header = "none invalid", |
| 774 |
}; |
| 775 |
|
| 776 |
static const ErrorFixture fixture_auth_timeout = { |
| 777 |
.error_message = "Authentication failed: Timeout", |
| 778 |
.header = "timeout-scheme too-slow", |
| 779 |
}; |
| 780 |
|
| 781 |
static const ErrorFixture fixture_non_ascii = { |
| 782 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 783 |
.error_message = "Authentication required", |
| 784 |
.header = "testscheme süccëss", |
| 785 |
}; |
| 786 |
|
| 787 |
static const ErrorFixture fixture_tls_cert = { |
| 788 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 789 |
.error_message = "Authentication required", |
| 790 |
.header = "tls-cert e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
| 791 |
}; |
| 792 |
|
| 793 |
|
| 794 |
typedef struct { |
| 795 |
const gchar **headers; |
| 796 |
const gchar **prompts; |
| 797 |
const gchar *error_message; |
| 798 |
const gchar *message; |
| 799 |
int error_code; |
| 800 |
const gchar *init_problem; |
| 801 |
int pause; |
| 802 |
} ErrorMultiFixture; |
| 803 |
|
| 804 |
typedef struct { |
| 805 |
const gchar **headers; |
| 806 |
const gchar **prompts; |
| 807 |
} SuccessMultiFixture; |
| 808 |
|
| 809 |
static inline gchar * |
| 810 |
str_skip (gchar *v, |
| 811 |
gchar c) |
| 812 |
{ |
| 813 |
while (v[0] == c) |
| 814 |
v++; |
| 815 |
return v; |
| 816 |
} |
| 817 |
|
| 818 |
static gboolean |
| 819 |
parse_login_reply_challenge (GHashTable *headers, |
| 820 |
gchar **out_id, |
| 821 |
gchar **out_prompt) |
| 822 |
{ |
| 823 |
gchar *original = NULL; |
| 824 |
gchar *line; |
| 825 |
gchar *next; |
| 826 |
gchar *id = NULL; |
| 827 |
gchar *prompt = NULL; |
| 828 |
gboolean ret = FALSE; |
| 829 |
gpointer key = NULL; |
| 830 |
gsize length; |
| 831 |
|
| 832 |
if (!g_hash_table_lookup_extended (headers, "WWW-Authenticate", &key, (gpointer *)&original)) |
| 833 |
goto out; |
| 834 |
|
| 835 |
line = original; |
| 836 |
|
| 837 |
// Check challenge type |
| 838 |
line = str_skip (line, ' '); |
| 839 |
if (g_ascii_strncasecmp (line, "X-Conversation ", strlen("X-Conversation ")) != 0) |
| 840 |
goto out; |
| 841 |
|
| 842 |
next = strchr (line, ' '); |
| 843 |
if (!next) |
| 844 |
goto out; |
| 845 |
|
| 846 |
// Get id |
| 847 |
line = next; |
| 848 |
line = str_skip (line, ' '); |
| 849 |
next = strchr (line, ' '); |
| 850 |
if (!next) |
| 851 |
goto out; |
| 852 |
id = g_strndup (line, next - line); |
| 853 |
|
| 854 |
// Rest should be the base64 prompt |
| 855 |
next = str_skip (next, ' '); |
| 856 |
prompt = g_strdup (next); |
| 857 |
if (g_base64_decode_inplace (prompt, &length) == NULL) |
| 858 |
goto out; |
| 859 |
prompt[length] = '\0'; |
| 860 |
ret = TRUE; |
| 861 |
|
| 862 |
out: |
| 863 |
if (ret) |
| 864 |
{ |
| 865 |
*out_id = id; |
| 866 |
*out_prompt = prompt; |
| 867 |
} |
| 868 |
else |
| 869 |
{ |
| 870 |
g_warning ("Got invalid WWW-Authenticate header: %s", original); |
| 871 |
g_free (id); |
| 872 |
g_free (prompt); |
| 873 |
} |
| 874 |
|
| 875 |
return ret; |
| 876 |
} |
| 877 |
|
| 878 |
static void |
| 879 |
test_multi_step_success (Test *test, |
| 880 |
gconstpointer data) |
| 881 |
{ |
| 882 |
CockpitWebService *service; |
| 883 |
CockpitCreds *creds; |
| 884 |
GHashTable *headers = NULL; |
| 885 |
gint spot = 0; |
| 886 |
gchar *id = NULL; |
| 887 |
|
| 888 |
const SuccessMultiFixture *fix = data; |
| 889 |
|
| 890 |
for (spot = 0; fix->headers[spot]; spot++) |
| 891 |
{ |
| 892 |
GAsyncResult *result = NULL; |
| 893 |
JsonObject *response = NULL; |
| 894 |
GError *error = NULL; |
| 895 |
const gchar *header = fix->headers[spot]; |
| 896 |
const gchar *expect_prompt = fix->prompts[spot]; |
| 897 |
gchar *out = NULL; |
| 898 |
gchar *prompt = NULL; |
| 899 |
|
| 900 |
headers = web_socket_util_new_headers (); |
| 901 |
if (id) |
| 902 |
{ |
| 903 |
g_assert (id != NULL); |
| 904 |
out = g_base64_encode ((guint8 *)header, strlen (header)); |
| 905 |
g_hash_table_insert (headers, g_strdup ("Authorization"), |
| 906 |
g_strdup_printf ("X-Conversation %s %s", id, out)); |
| 907 |
g_free (id); |
| 908 |
g_free (out); |
| 909 |
out = NULL; |
| 910 |
id = NULL; |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
g_hash_table_insert (headers, g_strdup ("Authorization"), |
| 915 |
g_strdup (header)); |
| 916 |
} |
| 917 |
cockpit_auth_login_async (test->auth, |
| 918 |
WebRequest(.path="/cockpit/", .headers=headers), |
| 919 |
on_ready_get_result, &result); |
| 920 |
g_hash_table_unref (headers); |
| 921 |
|
| 922 |
while (result == NULL) |
| 923 |
g_main_context_iteration (NULL, TRUE); |
| 924 |
|
| 925 |
headers = web_socket_util_new_headers (); |
| 926 |
g_assert (headers); |
| 927 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 928 |
g_object_unref (result); |
| 929 |
|
| 930 |
/* Confirm we got the right prompt */ |
| 931 |
if (expect_prompt) |
| 932 |
{ |
| 933 |
g_assert (prompt == NULL); |
| 934 |
g_assert (id == NULL); |
| 935 |
|
| 936 |
g_assert (parse_login_reply_challenge (headers, &id, &prompt)); |
| 937 |
g_assert_cmpstr (expect_prompt, ==, prompt); |
| 938 |
g_assert (id != NULL); |
| 939 |
g_free (prompt); |
| 940 |
prompt = NULL; |
| 941 |
|
| 942 |
g_assert_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED); |
| 943 |
g_clear_error (&error); |
| 944 |
|
| 945 |
g_hash_table_unref (headers); |
| 946 |
} |
| 947 |
else |
| 948 |
{ |
| 949 |
g_assert_no_error (error); |
| 950 |
} |
| 951 |
|
| 952 |
if (response) |
| 953 |
json_object_unref (response); |
| 954 |
} |
| 955 |
|
| 956 |
mock_auth_include_cookie_as_if_client (headers, headers, "cockpit"); |
| 957 |
service = cockpit_auth_check_cookie (test->auth, WebRequest(.path="/cockpit", .headers=headers)); |
| 958 |
creds = cockpit_web_service_get_creds (service); |
| 959 |
g_assert_cmpstr ("cockpit", ==, cockpit_creds_get_application (creds)); |
| 960 |
g_assert_null (cockpit_creds_get_password (creds)); |
| 961 |
g_hash_table_destroy (headers); |
| 962 |
g_object_unref (service); |
| 963 |
g_free (id); |
| 964 |
} |
| 965 |
|
| 966 |
static void |
| 967 |
test_multi_step_fail (Test *test, |
| 968 |
gconstpointer data) |
| 969 |
{ |
| 970 |
GHashTable *headers = NULL; |
| 971 |
gint spot = 0; |
| 972 |
g_autofree gchar *id = NULL; |
| 973 |
gchar *prompt = NULL; |
| 974 |
const ErrorMultiFixture *fix = data; |
| 975 |
|
| 976 |
if (fix->message) |
| 977 |
cockpit_expect_message (fix->message); |
| 978 |
|
| 979 |
for (spot = 0; fix->headers[spot]; spot++) |
| 980 |
{ |
| 981 |
GAsyncResult *result = NULL; |
| 982 |
JsonObject *response = NULL; |
| 983 |
GError *error = NULL; |
| 984 |
const gchar *header = fix->headers[spot]; |
| 985 |
const gchar *expect_prompt = fix->prompts[spot]; |
| 986 |
gchar *out = NULL; |
| 987 |
gboolean ready_for_next = TRUE; |
| 988 |
|
| 989 |
headers = web_socket_util_new_headers (); |
| 990 |
if (id) |
| 991 |
{ |
| 992 |
g_assert (id != NULL); |
| 993 |
out = g_base64_encode ((guint8 *)header, strlen (header)); |
| 994 |
g_hash_table_insert (headers, g_strdup ("Authorization"), |
| 995 |
g_strdup_printf ("X-Conversation %s %s", id, out)); |
| 996 |
g_free (id); |
| 997 |
g_free (out); |
| 998 |
out = NULL; |
| 999 |
id = NULL; |
| 1000 |
} |
| 1001 |
else |
| 1002 |
{ |
| 1003 |
g_hash_table_insert (headers, g_strdup ("Authorization"), |
| 1004 |
g_strdup (header)); |
| 1005 |
} |
| 1006 |
cockpit_auth_login_async (test->auth, |
| 1007 |
WebRequest(.path="/cockpit/", .headers=headers), |
| 1008 |
on_ready_get_result, &result); |
| 1009 |
g_hash_table_unref (headers); |
| 1010 |
|
| 1011 |
while (result == NULL) |
| 1012 |
g_main_context_iteration (NULL, TRUE); |
| 1013 |
|
| 1014 |
headers = web_socket_util_new_headers (); |
| 1015 |
response = cockpit_auth_login_finish (test->auth, result, NULL, headers, &error); |
| 1016 |
g_object_unref (result); |
| 1017 |
g_assert (error != NULL); |
| 1018 |
|
| 1019 |
/* Confirm we got the right prompt */ |
| 1020 |
if (expect_prompt) |
| 1021 |
{ |
| 1022 |
g_assert (prompt == NULL); |
| 1023 |
g_assert (id == NULL); |
| 1024 |
|
| 1025 |
g_assert (parse_login_reply_challenge (headers, &id, &prompt)); |
| 1026 |
g_assert_cmpstr (expect_prompt, ==, prompt); |
| 1027 |
g_assert (id != NULL); |
| 1028 |
g_free (prompt); |
| 1029 |
prompt = NULL; |
| 1030 |
if (fix->pause) |
| 1031 |
{ |
| 1032 |
ready_for_next = FALSE; |
| 1033 |
g_timeout_add_seconds (fix->pause, on_timeout_set_flag, &ready_for_next); |
| 1034 |
} |
| 1035 |
|
| 1036 |
while (ready_for_next == FALSE) |
| 1037 |
g_main_context_iteration (NULL, TRUE); |
| 1038 |
|
| 1039 |
if (response) |
| 1040 |
json_object_unref (response); |
| 1041 |
g_hash_table_unref (headers); |
| 1042 |
|
| 1043 |
g_assert_error (error, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED); |
| 1044 |
g_clear_error (&error); |
| 1045 |
} |
| 1046 |
else |
| 1047 |
{ |
| 1048 |
if (fix->init_problem) |
| 1049 |
{ |
| 1050 |
g_assert (response != NULL); |
| 1051 |
g_assert_cmpstr (json_object_get_string_member (response, "problem"), ==, fix->init_problem); |
| 1052 |
g_clear_pointer (&response, json_object_unref); |
| 1053 |
} |
| 1054 |
else |
| 1055 |
{ |
| 1056 |
g_assert (response == NULL); |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (fix->error_code) |
| 1060 |
g_assert_error (error, COCKPIT_ERROR, fix->error_code); |
| 1061 |
else |
| 1062 |
g_assert (error != NULL); |
| 1063 |
|
| 1064 |
g_assert_cmpstr (fix->error_message, ==, error->message); |
| 1065 |
g_clear_error (&error); |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
if (headers) |
| 1071 |
g_hash_table_destroy (headers); |
| 1072 |
} |
| 1073 |
|
| 1074 |
const gchar *two_steps[3] = { "testscheme two-step", "two", NULL }; |
| 1075 |
const gchar *two_prompts[2] = { "type two", NULL }; |
| 1076 |
const gchar *three_steps[4] = { "testscheme three-step", "two", "three", NULL }; |
| 1077 |
const gchar *three_steps_ssh[4] = { "testsshscheme three-step", "two", "three", NULL }; |
| 1078 |
const gchar *three_prompts[3] = { "type two", "type three", NULL }; |
| 1079 |
|
| 1080 |
static const SuccessMultiFixture fixture_two_steps = { |
| 1081 |
.headers = two_steps, |
| 1082 |
.prompts = two_prompts, |
| 1083 |
}; |
| 1084 |
|
| 1085 |
static const SuccessMultiFixture fixture_three_steps = { |
| 1086 |
.headers = three_steps, |
| 1087 |
.prompts = three_prompts, |
| 1088 |
}; |
| 1089 |
|
| 1090 |
static const SuccessMultiFixture fixture_ssh_three_steps = { |
| 1091 |
.headers = three_steps_ssh, |
| 1092 |
.prompts = three_prompts, |
| 1093 |
}; |
| 1094 |
|
| 1095 |
const gchar *two_steps_ssh_wrong[3] = { "testsshscheme two-step", "bad", NULL }; |
| 1096 |
const gchar *two_steps_wrong[3] = { "testscheme two-step", "bad", NULL }; |
| 1097 |
const gchar *three_steps_wrong[4] = { "testscheme three-step", "two", "bad", NULL }; |
| 1098 |
|
| 1099 |
static const ErrorMultiFixture fixture_fail_three_steps = { |
| 1100 |
.headers = three_steps_wrong, |
| 1101 |
.prompts = three_prompts, |
| 1102 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1103 |
.error_message = "Authentication failed", |
| 1104 |
.init_problem = "authentication-failed", |
| 1105 |
}; |
| 1106 |
|
| 1107 |
static const ErrorMultiFixture fixture_fail_two_steps = { |
| 1108 |
.headers = two_steps_wrong, |
| 1109 |
.prompts = two_prompts, |
| 1110 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1111 |
.error_message = "Authentication failed", |
| 1112 |
.init_problem = "authentication-failed", |
| 1113 |
}; |
| 1114 |
|
| 1115 |
static const ErrorMultiFixture fixture_fail_ssh_two_steps = { |
| 1116 |
.headers = two_steps_ssh_wrong, |
| 1117 |
.prompts = two_prompts, |
| 1118 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1119 |
.error_message = "Authentication failed", |
| 1120 |
.init_problem = "authentication-failed", |
| 1121 |
}; |
| 1122 |
|
| 1123 |
static const ErrorMultiFixture fixture_fail_step_timeout = { |
| 1124 |
.headers = two_steps, |
| 1125 |
.prompts = two_prompts, |
| 1126 |
.error_code = COCKPIT_ERROR_AUTHENTICATION_FAILED, |
| 1127 |
.error_message = "Invalid conversation token", |
| 1128 |
.message = "*session timed out during authentication*", |
| 1129 |
.pause = 3, |
| 1130 |
}; |
| 1131 |
|
| 1132 |
|
| 1133 |
typedef struct { |
| 1134 |
const gchar *str; |
| 1135 |
guint max_startups; |
| 1136 |
guint max_startups_rate; |
| 1137 |
guint max_startups_begin; |
| 1138 |
gboolean warn; |
| 1139 |
} StartupFixture; |
| 1140 |
|
| 1141 |
static void |
| 1142 |
setup_startups (Test *test, |
| 1143 |
gconstpointer data) |
| 1144 |
{ |
| 1145 |
const StartupFixture *fix = data; |
| 1146 |
cockpit_config_file = SRCDIR "does-not-exist"; |
| 1147 |
cockpit_ws_max_startups = fix->str; |
| 1148 |
if (fix->warn) |
| 1149 |
cockpit_expect_warning ("Illegal MaxStartups spec*"); |
| 1150 |
|
| 1151 |
test->auth = cockpit_auth_new (FALSE, COCKPIT_AUTH_NONE); |
| 1152 |
} |
| 1153 |
|
| 1154 |
static void |
| 1155 |
teardown_startups (Test *test, |
| 1156 |
gconstpointer data) |
| 1157 |
{ |
| 1158 |
cockpit_assert_expected (); |
| 1159 |
g_object_unref (test->auth); |
| 1160 |
} |
| 1161 |
|
| 1162 |
static const StartupFixture fixture_normal = { |
| 1163 |
.str = "20:50:200", |
| 1164 |
.max_startups = 200, |
| 1165 |
.max_startups_begin = 20, |
| 1166 |
.max_startups_rate = 50, |
| 1167 |
.warn = FALSE, |
| 1168 |
}; |
| 1169 |
|
| 1170 |
static const StartupFixture fixture_single = { |
| 1171 |
.str = "20", |
| 1172 |
.max_startups = 20, |
| 1173 |
.max_startups_begin = 20, |
| 1174 |
.max_startups_rate = 100, |
| 1175 |
.warn = FALSE, |
| 1176 |
}; |
| 1177 |
|
| 1178 |
static const StartupFixture fixture_double = { |
| 1179 |
.str = "20:50", |
| 1180 |
.max_startups = 20, |
| 1181 |
.max_startups_begin = 20, |
| 1182 |
.max_startups_rate = 100, |
| 1183 |
.warn = FALSE, |
| 1184 |
}; |
| 1185 |
|
| 1186 |
static const StartupFixture fixture_unlimited = { |
| 1187 |
.str = "0", |
| 1188 |
.max_startups = 0, |
| 1189 |
.max_startups_begin = 0, |
| 1190 |
.max_startups_rate = 100, |
| 1191 |
.warn = FALSE, |
| 1192 |
}; |
| 1193 |
|
| 1194 |
static const StartupFixture fixture_bad = { |
| 1195 |
.str = "bad", |
| 1196 |
.max_startups = 10, |
| 1197 |
.max_startups_begin = 10, |
| 1198 |
.max_startups_rate = 100, |
| 1199 |
.warn = TRUE, |
| 1200 |
}; |
| 1201 |
|
| 1202 |
static const StartupFixture fixture_bad_rate = { |
| 1203 |
.str = "20:101:40", |
| 1204 |
.max_startups = 10, |
| 1205 |
.max_startups_begin = 10, |
| 1206 |
.max_startups_rate = 100, |
| 1207 |
.warn = TRUE, |
| 1208 |
}; |
| 1209 |
|
| 1210 |
static const StartupFixture fixture_bad_startups = { |
| 1211 |
.str = "40:101:20", |
| 1212 |
.max_startups = 10, |
| 1213 |
.max_startups_begin = 10, |
| 1214 |
.max_startups_rate = 100, |
| 1215 |
.warn = TRUE, |
| 1216 |
}; |
| 1217 |
|
| 1218 |
static const StartupFixture fixture_bad_negative = { |
| 1219 |
.str = "-40:101:20", |
| 1220 |
.max_startups = 10, |
| 1221 |
.max_startups_begin = 10, |
| 1222 |
.max_startups_rate = 100, |
| 1223 |
.warn = TRUE, |
| 1224 |
}; |
| 1225 |
|
| 1226 |
static const StartupFixture fixture_bad_too_many = { |
| 1227 |
.str = "40:101:20:50:50", |
| 1228 |
.max_startups = 10, |
| 1229 |
.max_startups_begin = 10, |
| 1230 |
.max_startups_rate = 100, |
| 1231 |
.warn = TRUE, |
| 1232 |
}; |
| 1233 |
|
| 1234 |
static void |
| 1235 |
test_max_startups_conf (Test *test, |
| 1236 |
gconstpointer data) |
| 1237 |
{ |
| 1238 |
const StartupFixture *fix = data; |
| 1239 |
g_assert_cmpuint (fix->max_startups_begin, ==, test->auth->max_startups_begin); |
| 1240 |
g_assert_cmpuint (fix->max_startups, ==, test->auth->max_startups); |
| 1241 |
g_assert_cmpuint (fix->max_startups_rate, ==, test->auth->max_startups_rate); |
| 1242 |
} |
| 1243 |
|
| 1244 |
int |
| 1245 |
main (int argc, |
| 1246 |
char *argv[]) |
| 1247 |
{ |
| 1248 |
cockpit_ws_session_program = BUILDDIR "/mock-auth-command"; |
| 1249 |
cockpit_ws_service_idle = 1; |
| 1250 |
|
| 1251 |
cockpit_test_setenv ("COCKPIT_WS_PROCESS_IDLE", "2"); |
| 1252 |
|
| 1253 |
cockpit_test_init (&argc, &argv); |
| 1254 |
|
| 1255 |
g_test_add ("/auth/application", Test, NULL, NULL, test_application, NULL); |
| 1256 |
g_test_add ("/auth/userpass-header-check", Test, NULL, setup, test_userpass_cookie_check, teardown); |
| 1257 |
g_test_add ("/auth/userpass-store-check", Test, &fixture_superuser_any, setup, test_userpass_cookie_check, teardown); |
| 1258 |
g_test_add ("/auth/userpass-bad", Test, NULL, setup, test_userpass_bad, teardown); |
| 1259 |
g_test_add ("/auth/userpass-emptypass", Test, NULL, setup, test_userpass_emptypass, teardown); |
| 1260 |
g_test_add ("/auth/headers-bad", Test, NULL, setup, test_headers_bad, teardown); |
| 1261 |
g_test_add ("/auth/idle-timeout", Test, NULL, setup, test_idle_timeout, teardown); |
| 1262 |
g_test_add ("/auth/process-timeout", Test, NULL, setup, test_process_timeout, teardown); |
| 1263 |
g_test_add ("/auth/bad-coversation", Test, &fixture_bad_conversation, |
| 1264 |
setup_normal, test_custom_fail, teardown_normal); |
| 1265 |
g_test_add ("/auth/custom-success", Test, &fixture_no_data, |
| 1266 |
setup_normal, test_custom_success, teardown_normal); |
| 1267 |
g_test_add ("/auth/custom-no-cookie-success", Test, &fixture_no_cookie, |
| 1268 |
setup_normal, test_custom_success, teardown_normal); |
| 1269 |
g_test_add ("/auth/custom-data-then-success", Test, &fixture_data_then_success, |
| 1270 |
setup_normal, test_custom_success, teardown_normal); |
| 1271 |
g_test_add ("/auth/custom-fail-auth", Test, &fixture_auth_failed, |
| 1272 |
setup_normal, test_custom_fail, teardown_normal); |
| 1273 |
g_test_add ("/auth/custom-denied-auth", Test, &fixture_auth_denied, |
| 1274 |
setup_normal, test_custom_fail, teardown_normal); |
| 1275 |
g_test_add ("/auth/custom-with-error", Test, &fixture_auth_with_error, |
| 1276 |
setup_normal, test_custom_fail, teardown_normal); |
| 1277 |
g_test_add ("/auth/custom-timeout", Test, &fixture_auth_timeout, |
| 1278 |
setup_normal, test_custom_timeout, teardown_normal); |
| 1279 |
g_test_add ("/auth/non-ascii", Test, &fixture_non_ascii, |
| 1280 |
setup_normal, test_custom_fail, teardown_normal); |
| 1281 |
g_test_add ("/auth/bogus-tls-cert", Test, &fixture_tls_cert, |
| 1282 |
setup_normal, test_custom_fail, teardown_normal); |
| 1283 |
|
| 1284 |
g_test_add ("/auth/custom-ssh-basic-success", Test, &fixture_ssh_basic, |
| 1285 |
setup_normal, test_custom_success, teardown_normal); |
| 1286 |
g_test_add ("/auth/custom-ssh-basic-success-not-authorized", Test, &fixture_ssh_not_authorized, |
| 1287 |
setup_normal, test_custom_success, teardown_normal); |
| 1288 |
g_test_add ("/auth/custom-ssh-remote-basic-success", Test, &fixture_ssh_remote_basic, |
| 1289 |
setup_normal, test_custom_success, teardown_normal); |
| 1290 |
g_test_add ("/auth/custom-ssh-remote-switched", Test, &fixture_ssh_remote_switched, |
| 1291 |
setup_normal, test_custom_success, teardown_normal); |
| 1292 |
g_test_add ("/auth/custom-ssh-with-conf-default", Test, &fixture_ssh_alt_default, |
| 1293 |
setup_alt_config, test_custom_success, teardown_normal); |
| 1294 |
g_test_add ("/auth/custom-ssh-with-conf-allow", Test, &fixture_ssh_alt, |
| 1295 |
setup_alt_config, test_custom_success, teardown_normal); |
| 1296 |
g_test_add ("/auth/custom-ssh-success", Test, &fixture_ssh_no_data, |
| 1297 |
setup_normal, test_custom_success, teardown_normal); |
| 1298 |
g_test_add ("/auth/custom-ssh-fail-auth", Test, &fixture_ssh_auth_failed, |
| 1299 |
setup_normal, test_custom_fail, teardown_normal); |
| 1300 |
g_test_add ("/auth/custom-ssh-fail-basic-auth", Test, &fixture_ssh_basic_failed, |
| 1301 |
setup_normal, test_custom_fail, teardown_normal); |
| 1302 |
g_test_add ("/auth/custom-ssh-remote-fail-basic-auth", Test, &fixture_ssh_remote_basic_failed, |
| 1303 |
setup_normal, test_custom_fail, teardown_normal); |
| 1304 |
g_test_add ("/auth/custom-ssh-not-supported", Test, &fixture_ssh_not_supported, |
| 1305 |
setup_normal, test_custom_fail, teardown_normal); |
| 1306 |
g_test_add ("/auth/custom-ssh-with-error", Test, &fixture_ssh_auth_with_error, |
| 1307 |
setup_normal, test_custom_fail, teardown_normal); |
| 1308 |
g_test_add ("/auth/success-ssh-multi-step-three", Test, &fixture_ssh_three_steps, |
| 1309 |
setup_normal, test_multi_step_success, teardown_normal); |
| 1310 |
g_test_add ("/auth/fail-ssh-multi-step-two", Test, &fixture_fail_ssh_two_steps, |
| 1311 |
setup_normal, test_multi_step_fail, teardown_normal); |
| 1312 |
|
| 1313 |
g_test_add ("/auth/none", Test, &fixture_auth_none, |
| 1314 |
setup_normal, test_custom_fail, teardown_normal); |
| 1315 |
g_test_add ("/auth/bad-command", Test, &fixture_bad_command, |
| 1316 |
setup_normal, test_bad_command, teardown_normal); |
| 1317 |
g_test_add ("/auth/success-multi-step-two", Test, &fixture_two_steps, |
| 1318 |
setup_normal, test_multi_step_success, teardown_normal); |
| 1319 |
g_test_add ("/auth/success-multi-step-three", Test, &fixture_three_steps, |
| 1320 |
setup_normal, test_multi_step_success, teardown_normal); |
| 1321 |
g_test_add ("/auth/fail-multi-step-two", Test, &fixture_fail_two_steps, |
| 1322 |
setup_normal, test_multi_step_fail, teardown_normal); |
| 1323 |
g_test_add ("/auth/fail-multi-step-three", Test, &fixture_fail_three_steps, |
| 1324 |
setup_normal, test_multi_step_fail, teardown_normal); |
| 1325 |
g_test_add ("/auth/fail-multi-step-timeout", Test, &fixture_fail_step_timeout, |
| 1326 |
setup_normal, test_multi_step_fail, teardown_normal); |
| 1327 |
g_test_add ("/auth/max-startups", Test, NULL, |
| 1328 |
setup_normal, test_max_startups, teardown_normal); |
| 1329 |
g_test_add ("/auth/max-startups-normal", Test, &fixture_normal, |
| 1330 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1331 |
g_test_add ("/auth/max-startups-single", Test, &fixture_single, |
| 1332 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1333 |
g_test_add ("/auth/max-startups-double", Test, &fixture_double, |
| 1334 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1335 |
g_test_add ("/auth/max-startups-unlimited", Test, &fixture_unlimited, |
| 1336 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1337 |
g_test_add ("/auth/max-startups-bad", Test, &fixture_bad, |
| 1338 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1339 |
g_test_add ("/auth/max-startups-bad-rate", Test, &fixture_bad_rate, |
| 1340 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1341 |
g_test_add ("/auth/max-startups-bad-startups", Test, &fixture_bad_startups, |
| 1342 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1343 |
g_test_add ("/auth/max-startups-bad-negative", Test, &fixture_bad_negative, |
| 1344 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1345 |
g_test_add ("/auth/max-startups-too-many", Test, &fixture_bad_too_many, |
| 1346 |
setup_startups, test_max_startups_conf, teardown_startups); |
| 1347 |
return g_test_run (); |
| 1348 |
} |