test_nth.c 22.8 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25
/**@file test_nth.c
Pekka Pessi's avatar
Pekka Pessi committed
26
 * @brief Tests for nth module
27
 *
Pekka Pessi's avatar
Pekka Pessi committed
28
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29
 *
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35 36 37 38
 * @date Created: Tue Oct 22 20:52:37 2002 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
39

Pekka Pessi's avatar
Pekka Pessi committed
40 41
#include <assert.h>

42 43 44 45 46
#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
47 48
typedef struct tester tester_t;
typedef struct site site_t;
49
typedef struct client client_t;
Pekka Pessi's avatar
Pekka Pessi committed
50 51 52

#define SU_ROOT_MAGIC_T tester_t

53 54
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
55

56
#define NTH_CLIENT_MAGIC_T client_t
Pekka Pessi's avatar
Pekka Pessi committed
57 58
#define NTH_SITE_MAGIC_T site_t

59 60 61 62
#include "sofia-sip/nth.h"
#include <sofia-sip/http_header.h>
#include <sofia-sip/msg_mclass.h>
#include <sofia-sip/tport_tag.h>
63
#include <sofia-sip/auth_module.h>
Pekka Pessi's avatar
Pekka Pessi committed
64 65 66 67 68

int tstflags = 0;

#define TSTFLAGS tstflags

69
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76 77

#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
#define __func__ name
#endif

78
char const name[] = "test_nth";
Pekka Pessi's avatar
Pekka Pessi committed
79 80 81 82 83 84 85 86 87

static int init_test(tester_t *t);
static int deinit_test(tester_t *t);
static int test_nth_client_api(tester_t *t);
static int test_nth_server_api(tester_t *t);
static int init_server(tester_t *t);
static int test_requests(tester_t *t);
static int init_engine(tester_t *t);

88
struct site
Pekka Pessi's avatar
Pekka Pessi committed
89
{
90
  site_t       *s_next, *s_parent;
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93 94 95 96 97 98 99
  tester_t     *s_tester;
  url_string_t *s_url;
  nth_site_t   *s_ns;
  int           s_called;
  int           s_status;
  char const   *s_phrase;
  tagi_t       *s_tags;
};

100 101 102 103 104
struct client
{
  unsigned      c_status;
};

Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108 109 110 111 112 113
struct tester
{
  su_home_t     t_home[1];
  su_root_t    *t_root;
  msg_mclass_t *t_mclass;
  url_string_t *t_proxy;
  nth_engine_t *t_engine;

  char const   *t_srcdir;
114
  char const   *t_pem;
Pekka Pessi's avatar
Pekka Pessi committed
115 116 117 118

  su_sockaddr_t t_addr[1];
  socklen_t     t_addrlen;

119 120 121 122 123
  su_socket_t   t_sink;
  url_string_t *t_sinkuri;
  su_sockaddr_t t_sinkaddr[1];
  socklen_t     t_sinkaddrlen;

Pekka Pessi's avatar
Pekka Pessi committed
124 125 126 127
  site_t       *t_sites;
  site_t       *t_master;
};

128
static int test_site(site_t *t,
129
		     nth_site_t *server,
130
		     nth_request_t *req,
131 132 133 134 135 136 137
		     http_t const *http,
		     char const *path);

static site_t *site_create(tester_t *t, site_t *parent,
			   char const *url,
			   int status, char const *phrase,
			   tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
138
{
139
  nth_site_t *pns = parent ? parent->s_ns : NULL;
Pekka Pessi's avatar
Pekka Pessi committed
140 141 142
  site_t *s;
  ta_list ta;

143 144
  if (url == NULL)
    return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
145

146 147 148
  s = su_zalloc(t->t_home, sizeof *s);
  if (s == NULL)
    return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
149

150
  s->s_url = URL_STRING_MAKE(url);
Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155 156
  s->s_tester = t;
  s->s_next = t->t_sites;
  s->s_status = status;
  s->s_phrase = phrase;

  ta_start(ta, tag, value);
157

158
  s->s_tags = tl_adup(t->t_home, ta_args(ta));
159 160
  if (s->s_tags)
    s->s_ns = nth_site_create(pns, test_site, s,
161
			      (url_string_t *)s->s_url,
162 163 164
			      NTHTAG_ROOT(t->t_root),
			      ta_tags(ta));

Pekka Pessi's avatar
Pekka Pessi committed
165
  ta_end(ta);
166

167 168 169 170
  if (s->s_ns == NULL)
    return NULL;

  t->t_sites = s;
171

172
  return s;
Pekka Pessi's avatar
Pekka Pessi committed
173 174 175 176
}

static int init_test(tester_t *t)
{
177
  su_socket_t s;
Pekka Pessi's avatar
Pekka Pessi committed
178 179 180 181

  BEGIN();

  t->t_root = su_root_create(t); TEST_1(t->t_root);
182
  t->t_mclass = msg_mclass_clone(http_default_mclass(), 0, 0);
Pekka Pessi's avatar
Pekka Pessi committed
183 184
  TEST_1(t->t_mclass);

185
  t->t_addr->su_len = (sizeof t->t_addr->su_sin);
186
  s = su_socket(t->t_addr->su_family = AF_INET, SOCK_STREAM, 0);
187
  TEST_1(s != INVALID_SOCKET);
188
  TEST_1(su_inet_pton(AF_INET, "127.0.0.1", &t->t_addr->su_sin.sin_addr) >= 0);
189
  TEST_1(bind(s, &t->t_addr->su_sa,
Pekka Pessi's avatar
Pekka Pessi committed
190 191
	      t->t_addrlen = (sizeof t->t_addr->su_sin)) != -1);
  TEST_1(getsockname(s, &t->t_addr->su_sa, &t->t_addrlen) != -1);
192
  TEST_1(t->t_addr->su_port != 0);
Pekka Pessi's avatar
Pekka Pessi committed
193 194
  TEST_1(su_close(s) != -1);

195
  t->t_pem = su_sprintf(t->t_home, "%s/agent.pem", t->t_srcdir);
Pekka Pessi's avatar
Pekka Pessi committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  END();
}

static int deinit_test(tester_t *t)
{
  site_t *s, *s_next;

  BEGIN();

  nth_engine_destroy(t->t_engine);

  for (s = t->t_sites; s; s = s_next) {
    s_next = s->s_next;
    nth_site_destroy(s->s_ns), s->s_ns = NULL;
    su_free(t->t_home, s);
  }

  su_root_destroy(t->t_root);

216 217 218 219
  su_home_deinit(t->t_home);

  memset(t, 0, sizeof t);

Pekka Pessi's avatar
Pekka Pessi committed
220 221 222 223 224 225 226 227 228 229
  END();
}


static int test_nth_client_api(tester_t *t)
{
  char const *s;

  BEGIN();

230
  s = nth_engine_version();
Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234 235 236 237
  TEST_1(s); TEST_1(strlen(s)); TEST_S(s, "sofia-http-client/" NTH_CLIENT_VERSION);

  TEST_1(nth_engine_create(NULL, TAG_END()) == NULL);
  TEST(errno, EINVAL);
  TEST_VOID(nth_engine_destroy(NULL));
  TEST_1(nth_engine_get_params(NULL, TAG_END()) == -1);
  TEST_1(nth_engine_set_params(NULL, TAG_END()) == -1);
238 239 240
  TEST_1(!nth_client_tcreate(NULL, NULL, NULL,
			     HTTP_METHOD_OPTIONS,
			     URL_STRING_MAKE("*"),
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244
			     TAG_END()));
  TEST(nth_client_status(NULL), 400);
  TEST(nth_client_method(NULL), http_method_invalid);
  TEST(nth_client_is_streaming(NULL), 0);
245 246 247
  TEST_P(nth_client_url(NULL), NULL);
  TEST_P(nth_client_request(NULL), NULL);
  TEST_P(nth_client_response(NULL), NULL);
Pekka Pessi's avatar
Pekka Pessi committed
248 249
  TEST_VOID(nth_client_destroy(NULL));

250
  t->t_engine = nth_engine_create(t->t_root,
Pekka Pessi's avatar
Pekka Pessi committed
251 252 253 254 255 256 257
				  NTHTAG_ERROR_MSG(2),
				  NTHTAG_MCLASS(t->t_mclass),
				  NTHTAG_MFLAGS(MSG_DO_CANONIC|MSG_DO_COMPACT),
				  NTHTAG_STREAMING(0),
				  NTHTAG_PROXY("http://localhost:8888"),
				  TAG_END());
  TEST_1(t->t_engine);
258

Pekka Pessi's avatar
Pekka Pessi committed
259 260 261 262
  {
    int error_msg = -1;
    msg_mclass_t const *mclass = (void *)-1;
    int mflags = -1;
263
    unsigned expires = -1;
Pekka Pessi's avatar
Pekka Pessi committed
264 265 266 267 268
    int streaming = -1;
    url_string_t const *proxy = (void *)-1;

    char *proxy_str;

269
    TEST(nth_engine_get_params(t->t_engine,
Pekka Pessi's avatar
Pekka Pessi committed
270 271 272
			       NTHTAG_ERROR_MSG_REF(error_msg),
			       NTHTAG_MCLASS_REF(mclass),
			       NTHTAG_MFLAGS_REF(mflags),
273
			       NTHTAG_EXPIRES_REF(expires),
Pekka Pessi's avatar
Pekka Pessi committed
274 275
			       NTHTAG_STREAMING_REF(streaming),
			       NTHTAG_PROXY_REF(proxy),
276
			       TAG_END()),
277
	 6);
Pekka Pessi's avatar
Pekka Pessi committed
278 279

    TEST(error_msg, 1);
280
    TEST_P(mclass, t->t_mclass);
Pekka Pessi's avatar
Pekka Pessi committed
281
    TEST(mflags, MSG_DO_CANONIC|MSG_DO_COMPACT);
282
    TEST(expires, 32000);
Pekka Pessi's avatar
Pekka Pessi committed
283 284 285 286 287 288 289
    TEST(streaming, 0);
    TEST_1(proxy != NULL);
    TEST_1(proxy_str = url_as_string(t->t_home, proxy->us_url));
    TEST_S(proxy_str, "http://localhost:8888");

    proxy = URL_STRING_MAKE("http://127.0.0.1:80");

290
    TEST(nth_engine_set_params(t->t_engine,
Pekka Pessi's avatar
Pekka Pessi committed
291 292 293
			       NTHTAG_ERROR_MSG(0),
			       NTHTAG_MCLASS(http_default_mclass()),
			       NTHTAG_MFLAGS(0),
294
			       NTHTAG_EXPIRES(10000),
Pekka Pessi's avatar
Pekka Pessi committed
295 296
			       NTHTAG_STREAMING(2),
			       NTHTAG_PROXY(proxy),
297
			       TAG_END()),
298
	 6);
Pekka Pessi's avatar
Pekka Pessi committed
299 300 301 302

    error_msg = -1;
    mclass = (void *)-1;
    mflags = -1;
303
    expires = (unsigned)-1;
Pekka Pessi's avatar
Pekka Pessi committed
304 305 306
    streaming = -1;
    proxy = (void *)-1;

307
    TEST(nth_engine_get_params(t->t_engine,
Pekka Pessi's avatar
Pekka Pessi committed
308 309 310
			       NTHTAG_ERROR_MSG_REF(error_msg),
			       NTHTAG_MCLASS_REF(mclass),
			       NTHTAG_MFLAGS_REF(mflags),
311
			       NTHTAG_EXPIRES_REF(expires),
Pekka Pessi's avatar
Pekka Pessi committed
312 313
			       NTHTAG_STREAMING_REF(streaming),
			       NTHTAG_PROXY_REF(proxy),
314
			       TAG_END()),
315
	 6);
Pekka Pessi's avatar
Pekka Pessi committed
316 317

    TEST(error_msg, 0);
318
    TEST_P(mclass, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
319
    TEST(mflags, 0);
320
    TEST(expires, 10000);
Pekka Pessi's avatar
Pekka Pessi committed
321
    TEST(streaming, 1);
322
    TEST_1(proxy != NULL);
Pekka Pessi's avatar
Pekka Pessi committed
323 324 325 326 327
    TEST_1(proxy_str = url_as_string(t->t_home, proxy->us_url));
    TEST_S(proxy_str, "http://127.0.0.1:80");
  }

  TEST_1(nth_engine_get_stats(NULL, TAG_END()) == -1);
328

Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  {
    msg_t *msg;
    http_t *http;

    TEST_1(nth_engine_msg_create(NULL, -1) == NULL);
    TEST_1(msg = nth_engine_msg_create(t->t_engine, -1));
    TEST_1(http = http_object(msg));
    TEST(http->http_flags, MSG_FLG_USERMASK);
    msg_destroy(msg);

    /* Use mflags set by set_params (+ streaming flag) */
    TEST_1(msg = nth_engine_msg_create(t->t_engine, 0));
    TEST_1(http = http_object(msg));
    TEST(http->http_flags, MSG_FLG_STREAMING | t->t_mclass->mc_flags);
    msg_destroy(msg);
  }

  TEST_VOID(nth_engine_destroy(t->t_engine));
  t->t_engine = NULL;

  END();
}

352
static int site_check_all(site_t *t,
Pekka Pessi's avatar
Pekka Pessi committed
353
			  nth_site_t *server,
354
			  nth_request_t *req,
Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359 360 361
			  http_t const *http,
			  char const *path);

static int test_nth_server_api(tester_t *t)

{
  char const *v;
362
  site_t s[1];
363

Pekka Pessi's avatar
Pekka Pessi committed
364 365
  BEGIN();

366 367
  memset(s, 0, sizeof s);

368
  v = nth_site_server_version();
Pekka Pessi's avatar
Pekka Pessi committed
369 370 371
  TEST_1(v); TEST_1(strlen(v)); TEST_S(v, "nth/" NTH_SERVER_VERSION);

  /* Fails because no parent site, no root */
372 373 374
  TEST_1(!nth_site_create(NULL, test_site, s,
			  URL_STRING_MAKE("http://127.0.0.1:8888"),
			  TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
375 376

  /* Fails because url specifies both host and path */
377 378
  TEST_1(!nth_site_create(NULL, site_check_all, s,
			  URL_STRING_MAKE("http://127.0.0.1:8888/foo/"),
Pekka Pessi's avatar
Pekka Pessi committed
379 380 381
			  NTHTAG_ROOT(t->t_root), TAG_END()));

  TEST_VOID(nth_site_destroy(NULL));
382
  TEST_P(nth_site_magic(NULL), NULL);
383
  TEST_VOID(nth_site_bind(NULL, test_site, s));
Pekka Pessi's avatar
Pekka Pessi committed
384 385 386 387 388
  TEST_1(nth_site_set_params(NULL, TAG_END()) == -1);
  TEST_1(nth_site_get_params(NULL, TAG_END()) == -1);
  TEST_1(nth_site_get_stats(NULL, TAG_END()) == -1);
  TEST(nth_request_status(NULL), 400);
  TEST(nth_request_method(NULL), http_method_invalid);
389
  TEST_P(nth_request_message(NULL), NULL);
Pekka Pessi's avatar
Pekka Pessi committed
390 391 392 393 394 395
  TEST_1(nth_request_treply(NULL, HTTP_200_OK, TAG_END()) == -1);
  TEST_VOID(nth_request_destroy(NULL));

  END();
}

396
static int test_site(site_t *s,
397
		     nth_site_t *ns,
398
		     nth_request_t *req,
399 400
		     http_t const *http,
		     char const *path)
Pekka Pessi's avatar
Pekka Pessi committed
401 402 403 404 405 406 407 408 409 410 411 412
{
  if (s == NULL || ns == NULL || req == NULL)
    return 500;

  TEST_1(nth_request_treply(req, s->s_status, s->s_phrase,
			    TAG_NEXT(s->s_tags)) != -1);

  TEST_VOID(nth_request_destroy(req));

  return s->s_status;
}

413

414
static int site_check_all(site_t *s,
Pekka Pessi's avatar
Pekka Pessi committed
415
			  nth_site_t *ns,
416
			  nth_request_t *req,
Pekka Pessi's avatar
Pekka Pessi committed
417 418 419 420
			  http_t const *http,
			  char const *path)
{
  msg_t *msg;
421
  auth_status_t *as;
Pekka Pessi's avatar
Pekka Pessi committed
422 423 424 425 426 427 428 429 430 431 432 433

  TEST_1(s); TEST_1(ns); TEST_1(req); TEST_1(http); TEST_1(path);

  if (s == NULL || ns == NULL || req == NULL)
    return 500;

  TEST(nth_request_status(req), 0);
  TEST(nth_request_method(req), http_method_get);
  TEST_1(msg = nth_request_message(req));

  msg_destroy(msg);

434 435
  as = nth_request_auth(req);

Pekka Pessi's avatar
Pekka Pessi committed
436 437 438 439 440 441 442 443
  TEST_1(nth_request_treply(req, s->s_status, s->s_phrase,
			    TAG_NEXT(s->s_tags)) != -1);

  TEST_VOID(nth_request_destroy(req));

  return s->s_status;
}

444 445 446
static char passwd_name[] = "tmp_sippasswd.XXXXXX";

static void remove_tmp(void)
Pekka Pessi's avatar
Pekka Pessi committed
447
{
448 449 450 451 452 453 454 455
  if (passwd_name[0])
    unlink(passwd_name);
}

static char const passwd[] =
  "alice:secret:\n"
  "bob:secret:\n"
  "charlie:secret:\n";
Pekka Pessi's avatar
Pekka Pessi committed
456

457 458
static int init_server(tester_t *t)
{
Pekka Pessi's avatar
Pekka Pessi committed
459 460
  BEGIN();

461 462 463 464
  site_t *m = t->t_master, *sub2;
  auth_mod_t *am;
  int temp;

465
  TEST_1(t->t_master = m =
466
	 site_create(t, NULL,
467
		     su_sprintf(t->t_home, "HTTP://127.0.0.1:%u",
468
				htons(t->t_addr->su_port)),
469
		     HTTP_200_OK,
470 471 472 473 474 475
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR("<html><body>Hello</body></html>\n"),
		     TPTAG_CERTIFICATE(t->t_pem),
		     TAG_END()));

  TEST_1(site_create(t, m, "/sub/sub",
476
		     HTTP_200_OK,
477 478 479 480 481 482
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR
		     ("<html><body>sub/sub</body></html>\n"),
		     TAG_END()));

  TEST_1(site_create(t, m, "/sub/",
483
		     HTTP_200_OK,
484 485 486
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR("<html><body>sub/</body></html>\n"),
		     TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
487

488
  TEST_1(site_create(t, m, "/sub/sub/",
489
		     HTTP_200_OK,
490 491 492 493 494
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR
		     ("<html><body>sub/sub/</body></html>\n"),
		     TAG_END()));

495
  TEST_1(sub2 =
496
	 site_create(t, m, "/sub2/",
497
		     HTTP_200_OK,
498 499 500 501 502
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR("<html><body>sub2/</body></html>\n"),
		     TAG_END()));

  TEST_1(site_create(t, sub2, "sub/",
503
		     HTTP_200_OK,
504 505 506 507
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR
		     ("<html><body>sub2/sub/</body></html>\n"),
		     TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
508

509 510 511 512 513 514 515 516 517

#ifndef _WIN32
  temp = mkstemp(passwd_name);
#else
  temp = open(passwd_name, O_WRONLY|O_CREAT|O_TRUNC, 666);
#endif
  TEST_1(temp != -1);
  atexit(remove_tmp);		/* Make sure temp file is unlinked */

518
  TEST_SIZE(write(temp, passwd, strlen(passwd)), strlen(passwd));
519 520 521

  TEST_1(close(temp) == 0);

522 523
  am = auth_mod_create(t->t_root,
		       AUTHTAG_METHOD("Digest"),
524 525 526 527 528 529
		       AUTHTAG_REALM("auth"),
		       AUTHTAG_DB(passwd_name),
		       TAG_END());
  TEST_1(am);

  TEST_1(site_create(t, m, "auth/",
530
		     HTTP_200_OK,
531 532 533 534 535 536 537 538 539
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR
		     ("<html><body>auth/</body></html>\n"),
		     NTHTAG_AUTH_MODULE(am),
		     TAG_END()));

  auth_mod_unref(am);


540 541
  am = auth_mod_create(t->t_root,
		       AUTHTAG_METHOD("Delayed+Basic"),
542 543 544 545 546 547
		       AUTHTAG_REALM("auth2"),
		       AUTHTAG_DB(passwd_name),
		       TAG_END());
  TEST_1(am);

  TEST_1(site_create(t, m, "auth2/",
548
		     HTTP_200_OK,
549 550 551 552 553 554 555 556
		     HTTPTAG_CONTENT_TYPE_STR("text/html"),
		     HTTPTAG_PAYLOAD_STR
		     ("<html><body>auth/</body></html>\n"),
		     NTHTAG_AUTH_MODULE(am),
		     TAG_END()));

  auth_mod_unref(am);

Pekka Pessi's avatar
Pekka Pessi committed
557 558 559
  END();
}

560
static int send_request(tester_t *t, char const *req, size_t reqlen,
Pekka Pessi's avatar
Pekka Pessi committed
561 562 563 564
			int close_socket,
			char reply[], int rlen,
			int *return_len)
{
565
  static su_socket_t c = INVALID_SOCKET;
Pekka Pessi's avatar
Pekka Pessi committed
566
  int m, r;
567
  su_wait_t w[1];
Pekka Pessi's avatar
Pekka Pessi committed
568 569 570

  BEGIN();

571
  if (c == INVALID_SOCKET) {
572
    c = su_socket(t->t_addr->su_family, SOCK_STREAM, 0); TEST_1(c != SOCK_STREAM);
573
    TEST_1(su_setblocking(c, 1) != -1);
Pekka Pessi's avatar
Pekka Pessi committed
574 575 576 577 578 579 580 581
    TEST_1(connect(c, &t->t_addr->su_sa, t->t_addrlen) != -1);

    while (su_root_step(t->t_root, 1) == 0);
  }

  if (reqlen == (size_t)-1)
    reqlen = strlen(req);

582
  TEST_SIZE(su_send(c, req, reqlen, 0), reqlen);
Pekka Pessi's avatar
Pekka Pessi committed
583 584 585 586

  if (close_socket == 1)
    shutdown(c, 1);

587 588 589
  TEST(su_wait_create(w, c, SU_WAIT_IN), 0);

  while (su_root_step(t->t_root, 1) == 0 || su_wait(w, 1, 0) < 0);
Pekka Pessi's avatar
Pekka Pessi committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606

  for (r = 0;;) {
    TEST_1((m = recv(c, reply, rlen - r - 1, 0)) != -1);
    r += m;
    if (m == 0 || r == rlen - 1)
      break;
  }
  reply[r] = '\0';

  if (close_socket != -1)
    su_close(c), c = -1;

  *return_len = r;

  END();
}

607
int sspace(char const *buffer)
Pekka Pessi's avatar
Pekka Pessi committed
608
{
609
  int m = strcspn(buffer, " ");
Pekka Pessi's avatar
Pekka Pessi committed
610

611 612
  if (buffer[m])
    m += 1 + strcspn(buffer + m + 1, " ");
Pekka Pessi's avatar
Pekka Pessi committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626

  return m;
}

#define CRLF "\r\n"

static int test_requests(tester_t *t)
{
  char buffer[4096 + 1];
  int m;

  BEGIN();

  {
627
    static char const get[] =
Pekka Pessi's avatar
Pekka Pessi committed
628 629 630 631 632 633 634 635 636 637 638 639 640
      "GET / HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      CRLF;

    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);

    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 200");
  }

  {
641
    static char const get[] =
Pekka Pessi's avatar
Pekka Pessi committed
642 643 644 645 646 647 648 649 650 651 652 653
      "GET / HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      CRLF;

    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);

    m = strcspn(buffer, CRLF); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
  }

654
  {
655
    static char const request[] =
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
      "GET %s HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      CRLF;
    char *get;

    get = su_sprintf(NULL, request, "/sub");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 301");
    m += strcspn(buffer + m, CRLF) + 1;
    free(get);

    get = su_sprintf(NULL, request, "/sub/");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = strcspn(buffer, CRLF); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
    TEST_1(strstr(buffer + m, "<body>sub/</body>"));
    free(get);

    get = su_sprintf(NULL, request, "/sub2/");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = strcspn(buffer, CRLF); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
    TEST_1(strstr(buffer + m, "<body>sub2/</body>"));
    free(get);

    get = su_sprintf(NULL, request, "/sub2/hub");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = strcspn(buffer, CRLF); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
    TEST_1(strstr(buffer + m, "<body>sub2/</body>"));
    free(get);

    /* Test that absolute path for subdir site is calculated correctly */
    get = su_sprintf(NULL, request, "/sub2/sub");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 301");
    TEST_1(strstr(buffer + m, "/sub2/sub/" CRLF));
    free(get);

    get = su_sprintf(NULL, request, "/sub2/sub/");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = strcspn(buffer, CRLF); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
    TEST_1(strstr(buffer + m, "<body>sub2/sub/</body>"));
    free(get);

    get = su_sprintf(NULL, request, "/sub/sub");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = strcspn(buffer, CRLF); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200 OK");
    TEST_1(strstr(buffer + m, "<body>sub/sub</body>"));
    free(get);
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

    get = su_sprintf(NULL, request, "/auth/");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 401");
    free(get);

    get = su_sprintf(NULL, request, "/auth2/");
    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 401");
    free(get);
  }

  {
727
    static char const get[] =
728 729 730 731 732 733 734 735 736 737 738
      "GET /auth2/ HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      /* alice:secret in base64 */
      "Authorization: Basic YWxpY2U6c2VjcmV0" CRLF
      CRLF;

    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m++] = '\0';
    TEST_S(buffer, "HTTP/1.1 200");
739 740
  }

Pekka Pessi's avatar
Pekka Pessi committed
741
  {
742
    static char const kuik[] =
Pekka Pessi's avatar
Pekka Pessi committed
743 744 745 746 747 748 749 750
      "kuik" CRLF CRLF;

    TEST(send_request(t, kuik, -1, 0, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 400");
  }

  {
751
    static char const kuik[] =
Pekka Pessi's avatar
Pekka Pessi committed
752 753 754 755 756 757 758 759 760 761 762
      "POST / HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "Content-Length: 4294967296" CRLF
      CRLF;

    TEST(send_request(t, kuik, -1, 1, buffer, sizeof(buffer), &m), 0);
    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 400");
  }

  {
763
    static char const get[] =
Pekka Pessi's avatar
Pekka Pessi committed
764 765 766 767 768 769 770 771 772 773 774 775 776
      "GET / HTTP/10.10" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      CRLF;

    TEST(send_request(t, get, -1, 0, buffer, sizeof(buffer), &m), 0);

    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 505");
  }

  {
777
    static char const get[] =
Pekka Pessi's avatar
Pekka Pessi committed
778 779 780 781 782 783 784 785 786 787
      "GET /" CRLF;

    TEST(send_request(t, get, -1, 1, buffer, sizeof(buffer) - 1, &m), 0);

    buffer[6] = '\0';
    TEST_S(buffer, "<html>");
  }

  if (0)
  {
788
    static char const post[] =
Pekka Pessi's avatar
Pekka Pessi committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
      "POST /foo HTTP/1.1" CRLF
      "Host: 127.0.0.1" CRLF
      "User-Agent: Test-Tool" CRLF
      "Connection: close" CRLF
      "Content-Length: 7" CRLF
      "Expect: 100-continue" CRLF
      CRLF;
    static char const body[] =
      "<html/>";

    TEST(send_request(t, post, -1, -1, buffer, sizeof(buffer) - 1, &m), 0);

    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 100");

    TEST(send_request(t, body, -1, 0, buffer, sizeof(buffer) - 1, &m), 0);

    m = sspace(buffer); buffer[m] = '\0';
    TEST_S(buffer, "HTTP/1.1 200");
  }

  END();
}


static int init_engine(tester_t *t)
{
  BEGIN();
817
  su_socket_t s;
Pekka Pessi's avatar
Pekka Pessi committed
818

819
  t->t_engine = nth_engine_create(t->t_root,
Pekka Pessi's avatar
Pekka Pessi committed
820 821 822 823
				  NTHTAG_STREAMING(0),
				  TAG_END());
  TEST_1(t->t_engine);

824
  t->t_sink = s = su_socket(AF_INET, SOCK_STREAM, 0); TEST_1(s != -1);
825
  TEST(bind(s, &t->t_sinkaddr->su_sa,
826 827 828 829
	    t->t_sinkaddrlen = (sizeof t->t_sinkaddr->su_sin)),
       0);
  TEST_1(getsockname(s, &t->t_sinkaddr->su_sa, &t->t_sinkaddrlen) != -1);
  TEST(listen(t->t_sink, 5), 0);
830

831
  TEST_1(t->t_sinkuri = (url_string_t *)
832
	 su_sprintf(t->t_home, "HTTP://127.0.0.1:%u",
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
		    htons(t->t_sinkaddr->su_port)));

  END();
}


static int response_to_client(client_t *c,
			      nth_client_t *hc,
			      http_t const *http)
{
  if (http) {
    c->c_status = http->http_status->st_status;
  }
  else {
    c->c_status = nth_client_status(hc);
  }

  return 0;
}


static int test_client(tester_t *t)
{
  BEGIN();

  nth_client_t *hc;
  char *uri;
  client_t client[1];

  memset(client, 0, sizeof client);

  TEST_1(uri = su_strcat(NULL, t->t_master->s_url->us_str, "/"));
  TEST_1(hc = nth_client_tcreate(t->t_engine,
				 response_to_client, client,
				 HTTP_METHOD_GET,
				 URL_STRING_MAKE(uri),
				 TAG_END()));
  while (client->c_status == 0) su_root_step(t->t_root, 1);
  TEST(client->c_status, 200);
  nth_client_destroy(hc);
  su_free(NULL, uri);

  memset(client, 0, sizeof client);

  TEST_1(hc = nth_client_tcreate(t->t_engine,
				 response_to_client, client,
				 HTTP_METHOD_GET,
				 URL_STRING_MAKE(t->t_sinkuri),
				 NTHTAG_EXPIRES(1000),
				 TAG_END()));
  while (client->c_status == 0) su_root_step(t->t_root, 1);
  TEST(client->c_status, 408);
  nth_client_destroy(hc);
886

Pekka Pessi's avatar
Pekka Pessi committed
887 888
  END();
}
889 890 891 892 893 894 895
#if HAVE_ALARM
static RETSIGTYPE sig_alarm(int s)
{
  fprintf(stderr, "%s: FAIL! test timeout!\n", name);
  exit(1);
}
#endif
Pekka Pessi's avatar
Pekka Pessi committed
896

897
void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
898
{
899 900
  fprintf(stderr, "usage: %s [-v|-q] [-a] [-p proxy-uri]\n", name);
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
901 902 903 904 905 906
}

int main(int argc, char **argv)
{
  int i;
  int retval = 0;
907
  int o_alarm = 1;
Pekka Pessi's avatar
Pekka Pessi committed
908 909 910 911 912 913 914 915 916 917 918

  tester_t t[1] = {{{ SU_HOME_INIT(t) }}};

  char const *srcdir = getenv("srcdir");

  if (srcdir == NULL)
    srcdir = ".";

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
919 920
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
Pekka Pessi's avatar
Pekka Pessi committed
921 922 923 924 925 926
    else if (strcmp(argv[i], "-q") == 0)
      tstflags &= ~tst_verbatim;
    else if (strcmp(argv[i], "-p") == 0 && argv[i + 1])
      t->t_proxy = (url_string_t *)argv[++i];
    else if (strcmp(argv[i], "-s") == 0 && argv[i + 1])
      srcdir = argv[++i];
927 928 929
    else if (strcmp(argv[i], "--no-alarm") == 0) {
      o_alarm = 0;
    }
Pekka Pessi's avatar
Pekka Pessi committed
930 931 932 933 934 935 936
    else if (strcmp(argv[i], "-") == 0) {
      i++; break;
    }
    else if (argv[i][0] != '-') {
      break;
    }
    else
937
      usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
938 939 940 941
  }

  t->t_srcdir = srcdir;

942 943 944 945 946 947 948
#if HAVE_ALARM
  if (o_alarm) {
    alarm(60);
    signal(SIGALRM, sig_alarm);
  }
#endif

Pekka Pessi's avatar
Pekka Pessi committed
949 950 951 952 953 954 955 956
  su_init();

  retval |= init_test(t);
  retval |= test_nth_client_api(t);
  retval |= test_nth_server_api(t);
  retval |= init_server(t);
  retval |= test_requests(t);
  retval |= init_engine(t);
957
  retval |= test_client(t);
Pekka Pessi's avatar
Pekka Pessi committed
958
  retval |= deinit_test(t);
959

Pekka Pessi's avatar
Pekka Pessi committed
960 961 962 963
  su_deinit();

  return retval;
}