1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libsoup/soup.h"
#include "libsoup/soup-auth.h"
#include "libsoup/soup-session.h"
#ifdef HAVE_APACHE
#include "apache-wrapper.h"
#endif
int errors = 0;
typedef struct {
/* Explanation of what you should see */
const char *explanation;
/* URL to test against */
const char *url;
/* Provided passwords, 1 character each. ('1', '2', and '3'
* mean the correct passwords for "realm1", "realm2", and
* "realm3" respectively. '4' means "use the wrong password".)
* The first password (if present) will be used by
* authenticate(), and the second (if present) will be used by
* reauthenticate().
*/
const char *provided;
/* Expected passwords, 1 character each. (As with the provided
* passwords, with the addition that '0' means "no
* Authorization header expected".) Used to verify that soup
* used the password it was supposed to at each step.
*/
const char *expected;
/* What the final status code should be. */
guint final_status;
} SoupAuthTest;
SoupAuthTest tests[] = {
{ "No auth available, should fail",
"Basic/realm1/", "", "0", SOUP_STATUS_UNAUTHORIZED },
{ "Should fail with no auth, fail again with bad password, and give up",
"Basic/realm2/", "4", "04", SOUP_STATUS_UNAUTHORIZED },
{ "Known realm, auth provided, so should succeed immediately",
"Basic/realm1/", "1", "1", SOUP_STATUS_OK },
{ "Now should automatically reuse previous auth",
"Basic/realm1/", "", "1", SOUP_STATUS_OK },
{ "Subdir should also automatically reuse auth",
"Basic/realm1/subdir/", "", "1", SOUP_STATUS_OK },
{ "Subdir should retry last auth, but will fail this time",
"Basic/realm1/realm2/", "", "1", SOUP_STATUS_UNAUTHORIZED },
{ "Now should use provided auth on first try",
"Basic/realm1/realm2/", "2", "2", SOUP_STATUS_OK },
{ "Reusing last auth. Should succeed on first try",
"Basic/realm1/realm2/", "", "2", SOUP_STATUS_OK },
{ "Reuse will fail, but 2nd try will succeed because it's a known realm",
"Basic/realm1/realm2/realm1/", "", "21", SOUP_STATUS_OK },
{ "Should succeed on first try. (Known realm with cached password)",
"Basic/realm2/", "", "2", SOUP_STATUS_OK },
{ "Fail once, then use typoed password, then use right password",
"Basic/realm3/", "43", "043", SOUP_STATUS_OK },
{ "No auth available, should fail",
"Digest/realm1/", "", "0", SOUP_STATUS_UNAUTHORIZED },
{ "Should fail with no auth, fail again with bad password, and give up",
"Digest/realm2/", "4", "04", SOUP_STATUS_UNAUTHORIZED },
{ "Known realm, auth provided, so should succeed immediately",
"Digest/realm1/", "1", "1", SOUP_STATUS_OK },
{ "Now should automatically reuse previous auth",
"Digest/realm1/", "", "1", SOUP_STATUS_OK },
{ "Subdir should also automatically reuse auth",
"Digest/realm1/subdir/", "", "1", SOUP_STATUS_OK },
{ "Should already know correct domain and use provided auth on first try",
"Digest/realm1/realm2/", "2", "2", SOUP_STATUS_OK },
{ "Reusing last auth. Should succeed on first try",
"Digest/realm1/realm2/", "", "2", SOUP_STATUS_OK },
{ "Should succeed on first try because of earlier domain directive",
"Digest/realm1/realm2/realm1/", "", "1", SOUP_STATUS_OK },
{ "Should succeed on first try. (Known realm with cached password)",
"Digest/realm2/", "", "2", SOUP_STATUS_OK },
{ "Fail once, then use typoed password, then use right password",
"Digest/realm3/", "43", "043", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Basic/realm1/", "", "1", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Basic/realm1/realm2/", "", "2", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Basic/realm1/realm2/realm1/", "", "1", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Basic/realm2/", "", "2", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Basic/realm3/", "", "3", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Digest/realm1/", "", "1", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Digest/realm1/realm2/", "", "2", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Digest/realm1/realm2/realm1/", "", "1", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Digest/realm2/", "", "2", SOUP_STATUS_OK },
{ "Make sure we haven't forgotten anything",
"Digest/realm3/", "", "3", SOUP_STATUS_OK },
{ "Now the server will reject the formerly-good password",
"Basic/realm1/not/", "1" /* should not be used */, "1", SOUP_STATUS_UNAUTHORIZED },
{ "Make sure we've forgotten it",
"Basic/realm1/", "", "0", SOUP_STATUS_UNAUTHORIZED },
{ "Likewise, reject the formerly-good Digest password",
"Digest/realm1/not/", "1" /* should not be used */, "1", SOUP_STATUS_UNAUTHORIZED },
{ "Make sure we've forgotten it",
"Digest/realm1/", "", "0", SOUP_STATUS_UNAUTHORIZED }
};
int ntests = sizeof (tests) / sizeof (tests[0]);
static const char *auths[] = {
"no password", "password 1",
"password 2", "password 3",
"intentionally wrong password",
};
static int
identify_auth (SoupMessage *msg)
{
const char *header;
int num;
header = soup_message_get_header (msg->request_headers,
"Authorization");
if (!header)
return 0;
if (!g_ascii_strncasecmp (header, "Basic ", 6)) {
char *token;
int len;
token = soup_base64_decode (header + 6, &len);
num = token[len - 1] - '0';
g_free (token);
} else {
const char *user;
user = strstr (header, "username=\"user");
if (user)
num = user[14] - '0';
else
num = 0;
}
g_assert (num >= 0 && num <= 4);
return num;
}
static void
handler (SoupMessage *msg, gpointer data)
{
char *expected = data;
int auth, exp;
auth = identify_auth (msg);
printf (" %d %s (using %s)\n", msg->status_code, msg->reason_phrase,
auths[auth]);
if (*expected) {
exp = *expected - '0';
if (auth != exp) {
printf (" expected %s!\n", auths[exp]);
errors++;
}
memmove (expected, expected + 1, strlen (expected));
} else {
printf (" expected to be finished\n");
errors++;
}
}
static void
authenticate (SoupSession *session, SoupMessage *msg,
const char *auth_type, const char *auth_realm,
char **username, char **password, gpointer data)
{
int *i = data;
if (tests[*i].provided[0]) {
*username = g_strdup_printf ("user%c", tests[*i].provided[0]);
*password = g_strdup_printf ("realm%c", tests[*i].provided[0]);
}
}
static void
reauthenticate (SoupSession *session, SoupMessage *msg,
const char *auth_type, const char *auth_realm,
char **username, char **password, gpointer data)
{
int *i = data;
if (tests[*i].provided[0] && tests[*i].provided[1]) {
*username = g_strdup_printf ("user%c", tests[*i].provided[1]);
*password = g_strdup_printf ("realm%c", tests[*i].provided[1]);
}
}
int
main (int argc, char **argv)
{
SoupSession *session;
SoupMessage *msg;
char *base_uri, *uri, *expected;
#ifdef HAVE_APACHE
gboolean using_apache = FALSE;
#endif
int i;
g_type_init ();
g_thread_init (NULL);
if (argc != 1) {
char *p;
base_uri = argv[0];
p = strrchr (base_uri, '/');
if (!p || p[1])
base_uri = g_strdup_printf ("%s/", base_uri);
} else {
#ifdef HAVE_APACHE
if (!apache_init ()) {
fprintf (stderr, "Could not start apache\n");
return 1;
}
base_uri = "http://localhost:47524/";
using_apache = TRUE;
#else
fprintf (stderr, "Must specify base_uri for tests if configured --without-apache\n");
return 1;
#endif
}
session = soup_session_async_new ();
g_signal_connect (session, "authenticate",
G_CALLBACK (authenticate), &i);
g_signal_connect (session, "reauthenticate",
G_CALLBACK (reauthenticate), &i);
for (i = 0; i < ntests; i++) {
printf ("Test %d: %s\n", i + 1, tests[i].explanation);
uri = g_strconcat (base_uri, tests[i].url, NULL);
printf (" GET %s\n", uri);
msg = soup_message_new (SOUP_METHOD_GET, uri);
g_free (uri);
if (!msg) {
fprintf (stderr, "auth-test: Could not parse URI\n");
exit (1);
}
expected = g_strdup (tests[i].expected);
soup_message_add_status_code_handler (
msg, SOUP_STATUS_UNAUTHORIZED,
SOUP_HANDLER_PRE_BODY, handler, expected);
soup_message_add_status_code_handler (
msg, SOUP_STATUS_OK, SOUP_HANDLER_PRE_BODY,
handler, expected);
soup_session_send_message (session, msg);
if (msg->status_code != SOUP_STATUS_UNAUTHORIZED &&
msg->status_code != SOUP_STATUS_OK) {
printf (" %d %s !\n", msg->status_code,
msg->reason_phrase);
errors++;
}
if (*expected) {
printf (" expected %d more round(s)\n",
(int)strlen (expected));
errors++;
}
g_free (expected);
if (msg->status_code != tests[i].final_status)
printf (" expected %d\n", tests[i].final_status);
printf ("\n");
g_object_unref (msg);
}
g_object_unref (session);
#ifdef HAVE_APACHE
if (using_apache)
apache_cleanup ();
#endif
printf ("\nauth-test: %d errors\n", errors);
return errors;
}
|