| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright (c) 2014 Red Hat Inc. |
| 4 |
* Author: Stef Walter <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
#include "cockpitauthorize.h" |
| 10 |
#include "testlib/cockpittest.h" |
| 11 |
|
| 12 |
#include <sys/types.h> |
| 13 |
#include <sys/wait.h> |
| 14 |
|
| 15 |
#include <err.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <pwd.h> |
| 18 |
#include <string.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
static const char *expect_message; |
| 24 |
static char *user; |
| 25 |
|
| 26 |
static void |
| 27 |
test_logger (const char *msg) |
| 28 |
{ |
| 29 |
g_assert_nonnull (msg); |
| 30 |
|
| 31 |
if (expect_message) |
| 32 |
{ |
| 33 |
g_assert_nonnull (strstr (msg, expect_message)); |
| 34 |
expect_message = NULL; |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
warnx ("%s", msg); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
setup (char **user_ptr, |
| 44 |
gconstpointer data) |
| 45 |
{ |
| 46 |
struct passwd *pw; |
| 47 |
|
| 48 |
expect_message = NULL; |
| 49 |
|
| 50 |
pw = getpwuid (getuid ()); |
| 51 |
g_assert_nonnull (pw); |
| 52 |
*user_ptr = g_strdup (pw->pw_name); |
| 53 |
user = *user_ptr; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
teardown (char **user_ptr, |
| 58 |
gconstpointer data) |
| 59 |
{ |
| 60 |
if (expect_message) |
| 61 |
g_assert_not_reached (); |
| 62 |
g_free (*user_ptr); |
| 63 |
user = NULL; |
| 64 |
} |
| 65 |
|
| 66 |
typedef struct { |
| 67 |
const char *input; |
| 68 |
const char *expected; |
| 69 |
const char *ret; |
| 70 |
int errn; |
| 71 |
} ChallengeFixture; |
| 72 |
|
| 73 |
static ChallengeFixture type_fixtures[] = { |
| 74 |
{ "valid", "valid", "", 0 }, |
| 75 |
{ ":invalid", NULL, NULL, EINVAL }, |
| 76 |
{ "Basic more-data", "basic", "more-data", 0 }, |
| 77 |
{ "Basic more-data", "basic", "more-data", 0 }, |
| 78 |
{ "valid:test", "valid", "test", 0 }, |
| 79 |
{ "valid1:", "valid1", "", 0 }, |
| 80 |
{ "valid2:test:test", "valid2", "test:test", 0 }, |
| 81 |
{ NULL }, |
| 82 |
}; |
| 83 |
|
| 84 |
static void |
| 85 |
test_type (char **user_ptr, |
| 86 |
gconstpointer data) |
| 87 |
{ |
| 88 |
const ChallengeFixture *fix = data; |
| 89 |
const char *result; |
| 90 |
g_autofree char *type = NULL; |
| 91 |
|
| 92 |
result = cockpit_authorize_type (fix->input, &type); |
| 93 |
if (fix->errn != 0) |
| 94 |
g_assert_cmpint (errno, ==, fix->errn); |
| 95 |
if (fix->ret) |
| 96 |
{ |
| 97 |
g_assert_cmpstr (result, ==, fix->ret); |
| 98 |
g_assert_cmpstr (type, ==, fix->expected); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
g_assert_null (result); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static ChallengeFixture subject_fixtures[] = { |
| 107 |
{ "valid:73637275666679:", "73637275666679", "", 0 }, |
| 108 |
{ "valid:73637275666679:more-data", "73637275666679", "more-data", 0 }, |
| 109 |
{ "valid:scruffy:", "scruffy", "", 0 }, |
| 110 |
{ "X-Conversation conversationtoken more-data", "conversationtoken", "more-data", 0 }, |
| 111 |
{ "X-Conversation conversationtoken more-data", "conversationtoken", "more-data", 0 }, |
| 112 |
{ "invalid:", "73637275666679", NULL, EINVAL }, |
| 113 |
{ "invalid", NULL, NULL, EINVAL }, |
| 114 |
{ NULL }, |
| 115 |
}; |
| 116 |
|
| 117 |
static void |
| 118 |
test_subject (char **user_ptr, |
| 119 |
gconstpointer data) |
| 120 |
{ |
| 121 |
const ChallengeFixture *fix = data; |
| 122 |
const char *result; |
| 123 |
g_autofree char *subject = NULL; |
| 124 |
|
| 125 |
if (fix->ret == NULL) |
| 126 |
expect_message = "\"authorize\" message"; |
| 127 |
|
| 128 |
result = cockpit_authorize_subject (fix->input, &subject); |
| 129 |
if (fix->errn != 0) |
| 130 |
g_assert_cmpint (errno, ==, fix->errn); |
| 131 |
if (fix->ret) |
| 132 |
{ |
| 133 |
g_assert_cmpstr (result, ==, fix->ret); |
| 134 |
g_assert_cmpstr (subject, ==, fix->expected); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
g_assert_null (result); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static ChallengeFixture basic_fixtures[] = { |
| 143 |
{ "Basic c2NydWZmeTp6ZXJvZw==", "scruffy", "zerog", 0 }, |
| 144 |
{ "Basic!c2NydWZmeTp6ZXJvZw==", NULL, NULL, EINVAL }, |
| 145 |
{ "Basic c2NydWZ!!eXplcm9n", NULL, NULL, EINVAL }, |
| 146 |
{ "Basic c2NydWZmeXplcm9n", NULL, NULL, EINVAL }, |
| 147 |
{ "Basic", NULL, "", 0 }, |
| 148 |
{ NULL }, |
| 149 |
}; |
| 150 |
|
| 151 |
static void |
| 152 |
test_parse_basic (char **user_ptr, |
| 153 |
gconstpointer data) |
| 154 |
{ |
| 155 |
const ChallengeFixture *fix = data; |
| 156 |
const char *original = "blah"; |
| 157 |
char *user = (char *)original; |
| 158 |
g_autofree char *password = NULL; |
| 159 |
|
| 160 |
if (fix->ret == NULL) |
| 161 |
expect_message = "invalid"; |
| 162 |
|
| 163 |
password = cockpit_authorize_parse_basic (fix->input, &user); |
| 164 |
if (fix->errn != 0) |
| 165 |
g_assert_cmpint (errno, ==, fix->errn); |
| 166 |
if (fix->ret) |
| 167 |
{ |
| 168 |
g_assert_cmpstr (password, ==, fix->ret); |
| 169 |
if (fix->expected) |
| 170 |
g_assert_cmpstr (user, ==, fix->expected); |
| 171 |
else |
| 172 |
g_assert_null (user); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
g_assert_null (password); |
| 177 |
g_assert_cmpstr (user, ==, "blah"); /* not reassigned */ |
| 178 |
} |
| 179 |
|
| 180 |
if (user != original) |
| 181 |
g_free (user); |
| 182 |
g_clear_pointer (&password, g_free); |
| 183 |
|
| 184 |
if (fix->ret == NULL) |
| 185 |
expect_message = "invalid"; |
| 186 |
|
| 187 |
password = cockpit_authorize_parse_basic (fix->input, NULL); |
| 188 |
if (fix->errn != 0) |
| 189 |
g_assert_cmpint (errno, ==, fix->errn); |
| 190 |
if (fix->ret) |
| 191 |
{ |
| 192 |
g_assert_cmpstr (password, ==, fix->ret); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
g_assert_null (password); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
typedef struct { |
| 201 |
const char *input; |
| 202 |
size_t length; |
| 203 |
const char *ret; |
| 204 |
int errn; |
| 205 |
} NegotiateFixture; |
| 206 |
|
| 207 |
static NegotiateFixture parse_negotiate_fixtures[] = { |
| 208 |
{ "Negotiate c2NydWZmeTp6ZXJvZw==", 13, "scruffy:zerog", 0 }, |
| 209 |
{ "Negotiate!c2NydWZmeTp6ZXJvZw==", 0, NULL, EINVAL }, |
| 210 |
{ "Negotiate c2Nyd!!ZmeTp6ZXJvZw==", 0, NULL, EINVAL }, |
| 211 |
{ "Negotiate", 0, "", 0 }, |
| 212 |
{ NULL }, |
| 213 |
}; |
| 214 |
|
| 215 |
static void |
| 216 |
test_parse_negotiate (char **user_ptr, |
| 217 |
gconstpointer data) |
| 218 |
{ |
| 219 |
const NegotiateFixture *fix = data; |
| 220 |
size_t length = 0xFFFFDD; |
| 221 |
g_autofree void *result = NULL; |
| 222 |
|
| 223 |
if (fix->ret == NULL) |
| 224 |
expect_message = "invalid"; |
| 225 |
|
| 226 |
result = cockpit_authorize_parse_negotiate (fix->input, &length); |
| 227 |
if (fix->errn != 0) |
| 228 |
g_assert_cmpint (errno, ==, fix->errn); |
| 229 |
if (fix->ret) |
| 230 |
{ |
| 231 |
g_assert_cmpuint (fix->length, ==, length); |
| 232 |
g_assert_cmpint (memcmp (result, fix->ret, length), ==, 0); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
g_assert_null (result); |
| 237 |
g_assert_cmpuint (length, ==, 0xFFFFDD); /* not reassigned */ |
| 238 |
} |
| 239 |
|
| 240 |
g_clear_pointer (&result, g_free); |
| 241 |
|
| 242 |
if (fix->ret == NULL) |
| 243 |
expect_message = "invalid"; |
| 244 |
|
| 245 |
result = cockpit_authorize_parse_negotiate (fix->input, NULL); |
| 246 |
if (fix->errn != 0) |
| 247 |
g_assert_cmpint (errno, ==, fix->errn); |
| 248 |
if (fix->ret) |
| 249 |
{ |
| 250 |
g_assert_cmpint (memcmp (result, fix->ret, fix->length), ==, 0); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
g_assert_null (result); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static NegotiateFixture build_negotiate_fixtures[] = { |
| 259 |
{ "scruffy:zerog", 13, "Negotiate c2NydWZmeTp6ZXJvZw==", 0 }, |
| 260 |
{ NULL, 0, "Negotiate", 0, }, |
| 261 |
{ NULL }, |
| 262 |
}; |
| 263 |
|
| 264 |
static void |
| 265 |
test_build_negotiate (char **user_ptr, |
| 266 |
gconstpointer data) |
| 267 |
{ |
| 268 |
const NegotiateFixture *fix = data; |
| 269 |
g_autofree char *result = NULL; |
| 270 |
|
| 271 |
if (fix->ret == NULL) |
| 272 |
expect_message = "invalid"; |
| 273 |
|
| 274 |
result = cockpit_authorize_build_negotiate (fix->input, fix->length); |
| 275 |
if (fix->errn != 0) |
| 276 |
g_assert_cmpint (errno, ==, fix->errn); |
| 277 |
if (fix->ret) |
| 278 |
{ |
| 279 |
g_assert_cmpstr (result, ==, fix->ret); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
g_assert_null (result); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
typedef struct { |
| 288 |
const char *input; |
| 289 |
const char *conversation; |
| 290 |
const char *ret; |
| 291 |
int errn; |
| 292 |
} XConversationFixture; |
| 293 |
|
| 294 |
static XConversationFixture parse_x_conversation_fixtures[] = { |
| 295 |
{ "X-Conversation abcdefghi c2NydWZmeTp6ZXJvZw==", NULL, "scruffy:zerog", 0 }, |
| 296 |
{ "X-Conversation abcdefghi", NULL, "", 0 }, |
| 297 |
{ "X-Conversation abcdefghi c2NydW!!meTp6ZXJvZw==", NULL, NULL, EINVAL }, |
| 298 |
{ NULL }, |
| 299 |
}; |
| 300 |
|
| 301 |
static void |
| 302 |
test_parse_x_conversation (char **user_ptr, |
| 303 |
gconstpointer data) |
| 304 |
{ |
| 305 |
const XConversationFixture *fix = data; |
| 306 |
g_autofree char *result = NULL; |
| 307 |
|
| 308 |
if (fix->ret == NULL) |
| 309 |
expect_message = "invalid"; |
| 310 |
|
| 311 |
result = cockpit_authorize_parse_x_conversation (fix->input, NULL); |
| 312 |
if (fix->errn != 0) |
| 313 |
g_assert_cmpint (errno, ==, fix->errn); |
| 314 |
if (fix->ret) |
| 315 |
{ |
| 316 |
g_assert_cmpstr (result, ==, fix->ret); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
g_assert_null (result); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
static XConversationFixture build_x_conversation_fixtures[] = { |
| 325 |
{ "scruffy:zerog", "abcdefghi", "X-Conversation abcdefghi c2NydWZmeTp6ZXJvZw==", 0 }, |
| 326 |
{ "scruffy:zerog", NULL, " c2NydWZmeTp6ZXJvZw==", 0 }, |
| 327 |
{ "", "abcdefghi", "X-Conversation abcdefghi", 0 }, |
| 328 |
{ "scruffy:zerog", "", NULL, EINVAL }, |
| 329 |
{ NULL }, |
| 330 |
}; |
| 331 |
|
| 332 |
static void |
| 333 |
test_build_x_conversation (char **user_ptr, |
| 334 |
gconstpointer data) |
| 335 |
{ |
| 336 |
const XConversationFixture *fix = data; |
| 337 |
g_autofree char *conversation = NULL; |
| 338 |
g_autofree char *result = NULL; |
| 339 |
|
| 340 |
if (fix->ret == NULL) |
| 341 |
expect_message = "invalid"; |
| 342 |
|
| 343 |
if (fix->conversation) |
| 344 |
conversation = g_strdup (fix->conversation); |
| 345 |
|
| 346 |
result = cockpit_authorize_build_x_conversation (fix->input, &conversation); |
| 347 |
if (fix->errn != 0) |
| 348 |
g_assert_cmpint (errno, ==, fix->errn); |
| 349 |
if (fix->ret) |
| 350 |
{ |
| 351 |
if (strstr (fix->ret, "X-Conversation")) |
| 352 |
g_assert_cmpstr (result, ==, fix->ret); |
| 353 |
else |
| 354 |
g_assert_nonnull (strstr (result, fix->ret)); |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
g_assert_null (result); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
int |
| 363 |
main (int argc, |
| 364 |
char *argv[]) |
| 365 |
{ |
| 366 |
int i; |
| 367 |
|
| 368 |
/* Some initial preparation */ |
| 369 |
signal (SIGPIPE, SIG_IGN); |
| 370 |
cockpit_authorize_logger (test_logger, 0); |
| 371 |
|
| 372 |
cockpit_test_init (&argc, &argv); |
| 373 |
|
| 374 |
for (i = 0; type_fixtures[i].input != NULL; i++) |
| 375 |
{ |
| 376 |
g_autofree gchar *name = g_strdup_printf ("/authorize/type/%s", type_fixtures[i].input); |
| 377 |
g_test_add (name, char *, type_fixtures + i, |
| 378 |
setup, test_type, teardown); |
| 379 |
} |
| 380 |
for (i = 0; subject_fixtures[i].input != NULL; i++) |
| 381 |
{ |
| 382 |
g_autofree gchar *name = g_strdup_printf ("/authorize/subject/%s", subject_fixtures[i].input); |
| 383 |
g_test_add (name, char *, subject_fixtures + i, |
| 384 |
setup, test_subject, teardown); |
| 385 |
} |
| 386 |
for (i = 0; basic_fixtures[i].input != NULL; i++) |
| 387 |
{ |
| 388 |
g_autofree gchar *name = g_strdup_printf ("/authorize/basic/%s", basic_fixtures[i].input); |
| 389 |
g_test_add (name, char *, basic_fixtures + i, |
| 390 |
setup, test_parse_basic, teardown); |
| 391 |
} |
| 392 |
for (i = 0; parse_negotiate_fixtures[i].input != NULL; i++) |
| 393 |
{ |
| 394 |
g_autofree gchar *name = g_strdup_printf ("/authorize/negotiate/parse/%s", parse_negotiate_fixtures[i].input); |
| 395 |
g_test_add (name, char *, parse_negotiate_fixtures + i, |
| 396 |
setup, test_parse_negotiate, teardown); |
| 397 |
} |
| 398 |
for (i = 0; build_negotiate_fixtures[i].ret != NULL; i++) |
| 399 |
{ |
| 400 |
g_autofree gchar *name = g_strdup_printf ("/authorize/negotiate/build/%s", build_negotiate_fixtures[i].ret); |
| 401 |
g_test_add (name, char *, build_negotiate_fixtures + i, |
| 402 |
setup, test_build_negotiate, teardown); |
| 403 |
} |
| 404 |
for (i = 0; parse_x_conversation_fixtures[i].input != NULL; i++) |
| 405 |
{ |
| 406 |
g_autofree gchar *name = g_strdup_printf ("/authorize/x-conversation/parse/%s", parse_x_conversation_fixtures[i].input); |
| 407 |
g_test_add (name, char *, parse_x_conversation_fixtures + i, |
| 408 |
setup, test_parse_x_conversation, teardown); |
| 409 |
} |
| 410 |
for (i = 0; build_x_conversation_fixtures[i].input || build_x_conversation_fixtures[i].ret; i++) |
| 411 |
{ |
| 412 |
g_autofree gchar *name = g_strdup_printf ("/authorize/x-conversation/build/%d", i); |
| 413 |
g_test_add (name, char *, build_x_conversation_fixtures + i, |
| 414 |
setup, test_build_x_conversation, teardown); |
| 415 |
} |
| 416 |
|
| 417 |
return g_test_run (); |
| 418 |
} |