test_msg.c 48.4 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
/**@ingroup test_msg
Pekka Pessi's avatar
Pekka Pessi committed
26
 *
27
 * @CFILE test_msg.c
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36 37
 *
 * Torture tests for message parser.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Aug 21 15:18:26 2001 ppessi
 */

#include "config.h"

38 39
#include "test_class.h"
#include "test_protos.h"
40 41 42 43 44 45 46
#include "sofia-sip/msg.h"
#include "sofia-sip/msg_addr.h"
#include "sofia-sip/msg_date.h"
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/bnf.h"
#include "sofia-sip/msg_mclass.h"
#include "sofia-sip/msg_mclass_hash.h"
Pekka Pessi's avatar
Pekka Pessi committed
47

48 49 50 51 52 53 54
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
55 56 57
static int test_flags = 0;
#define TSTFLAGS test_flags

58
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
59

60
char const name[] = "test_msg";
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

static int msg_time_test(void)
{
  char buf[32];
  char const *s;
  char date1900[] = "Mon,  1 Jan 1900 00:00:00 GMT";
  char date1900_1[] = "Mon, 01 Jan 1900 00:00:01 GMT";
  char date822[] = "Thursday, 01-Jan-70 00:00:01 GMT";
  char date822b[] = "Wednesday, 09-Nov-99 23:12:40 GMT";
  char date822c[] = "Wednesday, 01-Sep-04 23:12:40 GMT";
  char date2822[] = "Monday, 01-Jan-1900 00:00:01 GMT";
  char dateasc[] = "Mon Jan  1 00:00:01 1900";
  msg_time_t now = msg_now(), date = now;

  BEGIN();
  s = date1900;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 0);
79 80
  TEST_SIZE(msg_date_e(buf, sizeof(buf), date), strlen(date1900));
  TEST_SIZE(msg_date_e(buf, sizeof(buf), 1), strlen(date1900_1));
Pekka Pessi's avatar
Pekka Pessi committed
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
  TEST_S(buf, date1900_1);

  s = date822;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 2208988801U);

  s = date822b;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 3151177960U);

  s = date822c;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 3303069160U);

  s = date2822;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 1);

  s = dateasc;
  TEST_1(msg_date_d(&s, &date) == 0);
  TEST(date, 1);

  {
    char error1[] = "Mo";
    char error2[] = "Mon,  1 Jan 19100 00:00:00 GMT";
    char error3[] = "Mon,  1 Jan 1900 00:00:";
    char error4[] = "Mon,  1 Jan 1900 25:00:00 GMT";
    char noerror5[] = "Mon,  1 Jan 1899 24:00:00 GMT";
    char error6[] = "Mon, 30 Feb 1896 23:59:59 GMT";
    char noerror7[] = "Mon, 29 Feb 1896 23:59:59 GMT";
    char error8[] = "Mon, 32 Jan 1900 24:00:00 GMT";
    char error9[] = "Mon, 27 Fev 1900 24:00:00 GMT";

    s = error1; TEST_1(msg_date_d(&s, &date) < 0);
    s = error2; TEST_1(msg_date_d(&s, &date) < 0);
    s = error3; TEST_1(msg_date_d(&s, &date) < 0);
    s = error4; TEST_1(msg_date_d(&s, &date) < 0);
    s = noerror5; TEST_1(msg_date_d(&s, &date) == 0); TEST(date, 0);
    s = error6; TEST_1(msg_date_d(&s, &date) < 0);
    s = noerror7; TEST_1(msg_date_d(&s, &date) == 0); TEST(date, 0);
    s = error8; TEST_1(msg_date_d(&s, &date) < 0);
    s = error9; TEST_1(msg_date_d(&s, &date) < 0);
  }

  {
    char error1[] = "4294967297";
    char *s;
    msg_numeric_t x[1];

130
    memset(x, 0, sizeof (x)); x->x_common->h_class = test_numeric_class;
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133 134 135 136 137

    TEST_1(msg_numeric_d(NULL, (msg_header_t *)x, s = error1, strlen(error1)) < 0);
  }

  END();
}

138 139 140
static int addr_test(void)
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
141

Pekka Pessi's avatar
Pekka Pessi committed
142 143
  /* It *will* fail. */
  /* TEST(sizeof(socklen_t), sizeof(msg_addrlen(NULL))); */
144 145 146 147

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
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
int test_header_parsing(void)
{
  BEGIN();

  {
    /* Test quoting/unquoting */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    char *quoted = "\"foo \\b\\a\\r\\\"\\\\\"extra";
    char *unquoted;

    TEST_1(unquoted = msg_unquote_dup(home, quoted));
    TEST_S(unquoted, "foo bar\"\\");

    su_home_deinit(home);
  }

  {
    /* Test parameter list */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_param_t const *params = NULL;
    char str[] = ";uffe;duffe = \"entten\" ; doo = [::1]  ", *s = str;
    char const canonic[] = ";uffe;duffe=\"entten\";doo=[::1]";
    char *end = str + strlen(str);
    char b[sizeof(canonic) + 8];

    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params != 0);
175
    TEST_P(s, end);
Pekka Pessi's avatar
Pekka Pessi committed
176 177 178 179
    TEST_S(params[0], "uffe");
    TEST_S(params[1], "duffe=\042entten\042");
    TEST_S(params[2], "doo=[::1]");
    TEST_1(params[3] == NULL);
180 181
    TEST_SIZE(msg_params_e(NULL, 0, params), strlen(canonic));
    TEST_SIZE(msg_params_e(b, sizeof(b), params), strlen(canonic));
Pekka Pessi's avatar
Pekka Pessi committed
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
    TEST_S(b, canonic);

    TEST_S(msg_params_find(params, "uffe"), "");
    TEST_S(msg_params_find(params, "uffe="), "");
    TEST_S(msg_params_find(params, "duffe"), "\"entten\"");
    TEST_S(msg_params_find(params, "duffe="), "\"entten\"");
    TEST_S(msg_params_find(params, "doo"), "[::1]");
    TEST_S(msg_params_find(params, "doo="), "[::1]");

    TEST(msg_params_remove((msg_param_t *)params, "uffe"), 1);
    TEST_S(params[0], "duffe=\042entten\042");
    TEST_S(params[1], "doo=[::1]");
    TEST_1(params[2] == NULL);

    TEST(msg_params_remove((msg_param_t *)params, "doo"), 1);
    TEST_S(params[0], "duffe=\042entten\042");
    TEST_1(params[1] == NULL);

    su_home_deinit(home);
  }

  {
    /* Test that parameter list of length MSG_PARAMS_N is handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_param_t const *params = NULL;
    char list1[] = ";one;two;three;four;five;six;seven;eight", *s = list1;
    char list2[] = ";one;two;three;four;five;six;seven";
    char list3[] = ";one;two;three;four;five;six;seven, humppaa";
    char *end3 = strchr(list3, ',');
    char list4[] = ";one;two;three;four;five;six;seven;eight;nine;ten"
      ";eleven;twelve;thirteen;fourteen;fiveteen;sixteen";
    char list5[] = ";one;two;three;four;five;six;seven;eight;nine;ten"
      ";eleven;twelve;thirteen;fourteen;fiveteen";
    char list6[] = ";one;two;three;four;five;six;seven;eight;nine;ten"
      ";eleven;twelve;thirteen;fourteen;fiveteen;sixteen;seventeen";
    int i;

    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params);
    for (i = 0; i < 8; i++)
      TEST_1(params[i]);
    TEST_1(params[8] == NULL);

    s = list2, params = NULL;
    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params);
    for (i = 0; i < 7; i++)
      TEST_1(params[i]);
    TEST_1(params[7] == NULL);

    s = list3; params = NULL;
    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_S(s, end3);
    TEST_1(params);
    for (i = 0; i < 7; i++)
      TEST_1(params[i]);
    TEST_1(params[7] == NULL);

    s = list4; params = NULL;
    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params);
    for (i = 0; i < 16; i++)
      TEST_1(params[i]);
    TEST_1(params[16] == NULL);

    s = list5; params = NULL;
    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params);
    for (i = 0; i < 15; i++)
      TEST_1(params[i]);
    TEST_1(params[15] == NULL);

    s = list6 ; params = NULL;
    TEST_1(msg_params_d(home, &s, &params) >= 0);
    TEST_1(params);
    for (i = 0; i < 17; i++)
      TEST_1(params[i]);
    TEST_1(params[17] == NULL);

    su_home_deinit(home);
  }

  {
    /* Test parameter lists */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    unsigned i, j;

    msg_param_t const *p = NULL;
    char *master = ";0", *list, *end;
Pekka Pessi's avatar
Pekka Pessi committed
271

Pekka Pessi's avatar
Pekka Pessi committed
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
    for (i = 1; i < 256; i++) {
      master = su_sprintf(home, "%s; %u", master, i); TEST_1(master);
      list = end = su_strdup(home, master);
      TEST_1(msg_params_d(home, &end, &p) >= 0);
      TEST_S(end, "");
      TEST_1(p);
      for (j = 0; j <= i; j++) {
	char number[10];
	snprintf(number, sizeof number, "%u", j);
	TEST_S(p[j], number);
      }
      TEST_1(p[i + 1] == NULL);
      su_free(home, list);
      su_free(home, (void *)p), p = NULL;
    }

    su_home_deinit(home);
  }

  {
    /* Test comma-separated list */
    su_home_t home[1] = { SU_HOME_INIT(home) };

    msg_list_t k1[1] = {{{{ 0 }}}};
    char list1[] = "foo, bar, baz  zi  \"baz\"";
Pekka Pessi's avatar
Pekka Pessi committed
297

Pekka Pessi's avatar
Pekka Pessi committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    TEST_1(msg_list_d(home, (msg_header_t *)k1, list1, strlen(list1)) >= 0);
    TEST_1(k1->k_items);
    TEST_S(k1->k_items[0], "foo");
    TEST_S(k1->k_items[1], "bar");
    TEST_S(k1->k_items[2], "baz zi\042baz\042");
    TEST_1(!k1->k_items[3]);

    su_home_deinit(home);
  }

  {
    /* Test that list of length MSG_PARAMS_N is handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_list_t k2[1] = {{{{ 0 }}}};
    char list2[] = "one, two, three, four, five, six, seven, eight";
Pekka Pessi's avatar
Pekka Pessi committed
313

Pekka Pessi's avatar
Pekka Pessi committed
314 315 316 317 318 319 320 321
    TEST_1(
	  msg_list_d(home, (msg_header_t *)k2, list2, strlen(list2)) >= 0);
    TEST_1(k2->k_items);
    TEST_1(k2->k_items[7]);
    TEST_1(k2->k_items[8] == NULL);

    su_home_deinit(home);
  }
Pekka Pessi's avatar
Pekka Pessi committed
322

Pekka Pessi's avatar
Pekka Pessi committed
323 324 325 326 327
  {
    /* Test that list longer than MSG_PARAMS_N is handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_list_t k3[1] = {{{{ 0 }}}};
    char list3[] = "one, two, three, four, five, six, seven, eight, nine";
Pekka Pessi's avatar
Pekka Pessi committed
328

Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334
    TEST_1(
	  msg_list_d(home, (msg_header_t *)k3, list3, strlen(list3)) >= 0);
    TEST_1(k3->k_items);
    TEST_1(k3->k_items[7]);
    TEST_1(k3->k_items[8]);
    TEST_1(k3->k_items[9] == NULL);
Pekka Pessi's avatar
Pekka Pessi committed
335

Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341
    su_home_deinit(home);
  }

  {
    /* Test that long lists are handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
Pekka Pessi's avatar
Pekka Pessi committed
342

Pekka Pessi's avatar
Pekka Pessi committed
343 344 345 346 347
    msg_param_t *k = NULL;
    char *s;
    char list1[] = "one, two, three, four, five, six, seven, eight";
    char list2[] = "one, two, three, four, five, six, seven, eight";
    char list3[] = "one, two, three, four, five, six, seven, eight";
Pekka Pessi's avatar
Pekka Pessi committed
348
    char list4[] = "one, two, three, four, five, six, seven, eight, nine";
Pekka Pessi's avatar
Pekka Pessi committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

    s = list1; TEST_1(msg_commalist_d(home, &s, &k, msg_token_scan) >= 0);
    TEST_1(k);
    TEST_1(k[7]);
    TEST_1(k[8] == NULL);

    s = list2; TEST_1(msg_commalist_d(home, &s, &k, msg_token_scan) >= 0);
    s = list3; TEST_1(msg_commalist_d(home, &s, &k, msg_token_scan) >= 0);
    s = list4; TEST_1(msg_commalist_d(home, &s, &k, msg_token_scan) >= 0);

    su_home_deinit(home);
  }

  {
    /* Test parameter lists */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    unsigned i, j;

    msg_param_t *p = NULL;
    char *master = "0", *list, *end;
Pekka Pessi's avatar
Pekka Pessi committed
369

Pekka Pessi's avatar
Pekka Pessi committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    for (i = 1; i < 256; i++) {
      master = su_sprintf(home, "%s, %u", master, i); TEST_1(master);
      list = end = su_strdup(home, master);
      TEST_1(msg_commalist_d(home, &end, &p, msg_token_scan) >= 0);
      TEST_S(end, "");
      TEST_1(p);
      for (j = 0; j <= i; j++) {
	char number[10];
	snprintf(number, sizeof number, "%u", j);
	TEST_S(p[j], number);
      }
      TEST_1(p[i + 1] == NULL);
      su_free(home, list);
      su_free(home, (void *)p), p = NULL;
    }

    su_home_deinit(home);
  }

  {
    /* Test that errors in lists are handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
Pekka Pessi's avatar
Pekka Pessi committed
392

Pekka Pessi's avatar
Pekka Pessi committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
    msg_param_t *k = NULL;
    char *s;
    char list1[] = "one, two, three, four, five, six, seven, foo=\"eight";
    char list2[] = "one, two, three,,@,$ four, five, six, seven, eight";

    s = list1; TEST_1(msg_commalist_d(home, &s, &k, NULL) < 0);
    TEST_1(k == NULL);

    s = list2; TEST_1(msg_commalist_d(home, &s, &k, msg_token_scan) < 0);

    su_home_deinit(home);
  }

  {
    /* Test empty parameter list */
    su_home_t home[1] = { SU_HOME_INIT(home) };

    msg_list_t k4[1] = {{{{ 0 }}}};
    char list4[] = ", ,\t,\r\n\t,  ,   ";
Pekka Pessi's avatar
Pekka Pessi committed
412

Pekka Pessi's avatar
Pekka Pessi committed
413 414 415 416 417 418 419 420 421 422 423 424 425
    TEST_1(
	  msg_list_d(home, (msg_header_t *)k4, list4, strlen(list4)) >= 0);
    TEST_1(k4->k_items == NULL);

    su_home_deinit(home);
  }

  {
    /* Test authentication headers */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_auth_t au[1] = {{{{ 0 }}}};
    char s[] = "Basic foo = \"bar==\" ,, bar=baari,"
      "baz=\"bof,\\\\ \\\" baff\", base\t64/ - is== ,,";
Pekka Pessi's avatar
Pekka Pessi committed
426

Pekka Pessi's avatar
Pekka Pessi committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    TEST_1(msg_auth_d(home, (msg_header_t *)au, s, strlen(s)) >= 0);
    TEST_S(au->au_scheme, "Basic");
    TEST_1(au->au_params);
    TEST_S(au->au_params[0], "foo=\042bar==\042");
    TEST_S(au->au_params[1], "bar=baari");
    TEST_S(au->au_params[2], "baz=\042bof,\\\\ \\\042 baff\042");
    TEST_S(au->au_params[3], "base 64/- is==");
    TEST_1(!au->au_params[4]);

    su_home_deinit(home);
  }

  /* Test that msg_*_format() works */
  {
    su_home_t home[1] = { SU_HOME_INIT(home) };

    msg_content_type_t *c;
Pekka Pessi's avatar
Pekka Pessi committed
444

Pekka Pessi's avatar
Pekka Pessi committed
445 446 447 448 449 450 451 452 453 454 455 456
    c = msg_content_type_format(home, "%s/%s;%s;%s;%s;%s;%s;%s",
				"text", "plain",
				"charset=iso-8859-15",
				"format=flowed",
				"q=0.999",
				"msg-size=782572564",
				"name-space-url=\"http://www.nokia.com/foo\"",
				"foo=bar");

    su_home_deinit(home);
  }

457 458 459 460 461 462 463 464
  {
    /* Test parameter handling */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    msg_content_encoding_t *ce;

    ce = msg_content_encoding_make(home, "zip, zap, zup, lz, zl, zz, ll");
    TEST_1(ce);
    TEST_S(msg_header_find_param(ce->k_common, "zz"), "");
465
    TEST_S(msg_header_find_item(ce->k_common, "zz"), "zz");
466
    TEST_P(msg_header_find_param(ce->k_common, "k"), NULL);
467 468 469 470
    TEST(msg_header_add_param(home, ce->k_common, "zip"), 0);
    TEST(msg_header_remove_param(ce->k_common, "zip"), 1);
    TEST_S(msg_header_find_param(ce->k_common, "zip"), "");
    TEST(msg_header_remove_param(ce->k_common, "zip"), 1);
471
    TEST_P(msg_header_find_param(ce->k_common, "zip"), NULL);
472 473 474
    TEST(msg_header_remove_param(ce->k_common, "zip"), 0);
    TEST(msg_header_replace_param(home, ce->k_common, "zip=zap"), 0);
    TEST_S(msg_header_find_param(ce->k_common, "zip=1"), "zap");
475
    TEST(msg_header_replace_param(home, ce->k_common, "zip=zup"), 1);
476 477 478 479 480 481
    TEST_S(msg_header_find_param(ce->k_common, "zip"), "zup");

    su_home_deinit(home);
  }


Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489
  END();
}

int hash_test(void)
{
  int i, j, hash = 0;
  msg_mclass_t const *mc = msg_test_mclass;
  msg_hclass_t *hc;
Pekka Pessi's avatar
Pekka Pessi committed
490

Pekka Pessi's avatar
Pekka Pessi committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  BEGIN();

  for (i = 0; i < mc->mc_hash_size; i++) {
    hc = mc->mc_hash[i].hr_class;
    if (hc == NULL)
      continue;

    hash = msg_header_name_hash(hc->hc_name, NULL);
    TEST(hash, hc->hc_hash);

    /* Cross-check hashes */
    for (j = i + 1; j < mc->mc_hash_size; j++) {
      if (mc->mc_hash[j].hr_class == NULL)
	continue;
      if (hc->hc_hash == mc->mc_hash[j].hr_class->hc_hash)
	fprintf(stderr, "\t%s and %s have same hash\n",
		hc->hc_name, mc->mc_hash[j].hr_class->hc_name);
      TEST_1(hc->hc_hash != mc->mc_hash[j].hr_class->hc_hash);
    }
  }

  END();
}

msg_t *read_msg(char const buffer[])
{
Pekka Pessi's avatar
Pekka Pessi committed
517
  return msg_make(msg_test_mclass, MSG_DO_EXTRACT_COPY, buffer, -1);
Pekka Pessi's avatar
Pekka Pessi committed
518 519
}

Pekka Pessi's avatar
Pekka Pessi committed
520
/**Check if header chain contains any loops.
Pekka Pessi's avatar
Pekka Pessi committed
521 522 523 524
 *
 * @return
 * Return 0 if no loop, -1 otherwise.
 */
Pekka Pessi's avatar
Pekka Pessi committed
525
static
Pekka Pessi's avatar
Pekka Pessi committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
int msg_chain_loop(msg_header_t const *h)
{
  msg_header_t const *h2;

  if (!h) return 0;

  for (h2 = h->sh_succ; h && h2 && h2->sh_succ; h = h->sh_succ) {
    if (h == h2 || h == h2->sh_succ)
      return 1;

    h2 = h2->sh_succ->sh_succ;

    if (h == h2)
      return 1;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
545
/** Check header chain consistency.
Pekka Pessi's avatar
Pekka Pessi committed
546 547 548 549 550 551 552 553 554
 *
 * @return
 * Return 0 if consistent, number of errors otherwise.
 */
static
int msg_chain_errors(msg_header_t const *h)
{
  if (msg_chain_loop(h))
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
555

Pekka Pessi's avatar
Pekka Pessi committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  for (; h; h = h->sh_succ) {
    if (h->sh_succ && h->sh_succ->sh_prev != &h->sh_succ)
      return -1;
    if (h->sh_prev && h != (*h->sh_prev))
      return -1;
  }

  return 0;
}

int test_msg_parsing(void)
{
  msg_t *msg, *orig;
  su_home_t *home;
  msg_test_t *tst, *otst;
  msg_request_t *request;
  msg_status_t *status;
  msg_content_location_t *location;
  msg_content_language_t *language;
575
  msg_accept_language_t *se;
Pekka Pessi's avatar
Pekka Pessi committed
576 577 578 579 580 581 582 583 584 585 586 587 588
  msg_separator_t *separator;
  msg_payload_t *payload;

  BEGIN();

  msg = read_msg("GET a-life HTTP/1.1" CRLF
		 "Content-Length: 6" CRLF
		 "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF
		 "Foo: bar" CRLF
		 CRLF
		 "test" CRLF);

  home = msg_home(msg);
Pekka Pessi's avatar
Pekka Pessi committed
589
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
590 591 592 593 594

  TEST_1(msg);
  TEST_1(home);
  TEST_1(tst);

595
  TEST_P(tst->msg_error, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
596 597 598 599

  TEST_1(tst->msg_accept_language);

  TEST_1(status = msg_status_make(home, "HTTP/1.1 200 Ok"));
Pekka Pessi's avatar
Pekka Pessi committed
600
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)status), 0);
601
  TEST_P(tst->msg_status, status); TEST_P(tst->msg_request, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
602 603

  TEST_1(request = msg_request_make(home, "GET a-wife HTTP/1.0"));
Pekka Pessi's avatar
Pekka Pessi committed
604
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)request), 0);
605
  TEST_P(tst->msg_request, request); TEST_P(tst->msg_status, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
606 607

  TEST_1(separator = msg_separator_make(home, "\r\n"));
Pekka Pessi's avatar
Pekka Pessi committed
608
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
Pekka Pessi's avatar
Pekka Pessi committed
609
  TEST_P(tst->msg_separator, separator);
610
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
611 612 613

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
Pekka Pessi's avatar
Pekka Pessi committed
614
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
615
  /* It is appended */
Pekka Pessi's avatar
Pekka Pessi committed
616
  TEST_P(tst->msg_payload->pl_next, payload);
617
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
618 619 620 621 622 623 624

  {
    msg_param_t vs;
    int vi = 0;
    msg_param_t foo = "foo=bar";

    vs = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
625
    MSG_PARAM_MATCH(vs, foo, "foo");
Pekka Pessi's avatar
Pekka Pessi committed
626 627
    TEST_S(vs, "bar");
    vs = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
628
    MSG_PARAM_MATCH(vs, foo, "fo");
629
    TEST_P(vs, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
630 631 632 633 634 635 636 637 638 639 640 641
    vi = 0;
    MSG_PARAM_MATCH_P(vi, foo, "foo");
    TEST(vi, 1);
    MSG_PARAM_MATCH_P(vi, foo, "fo");
    TEST(vi, 1);
    vi = 0;
    MSG_PARAM_MATCH_P(vi, foo, "fo");
    TEST(vi, 0);
  }

  msg_destroy(msg);

Pekka Pessi's avatar
Pekka Pessi committed
642 643 644 645 646 647 648 649
  /* Bug #2624: */
  msg = read_msg("GET /replaces HTTP/1.1" CRLF
		 "Accept-Encoding: gzip" CRLF
		 "Accept-Encoding: bzip2" CRLF
		 "Accept-Encoding: deflate" CRLF
		 "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF
		 );
  TEST_1(msg);
Pekka Pessi's avatar
Pekka Pessi committed
650
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664
  TEST_1(tst);

  {
    msg_accept_encoding_t *gzip, *bzip2, *deflate;
    msg_accept_encoding_t *lzss;
    msg_accept_language_t *en, *fi, *se;
    msg_accept_language_t *de, *sv, *sv_fi;

    TEST_1(gzip = tst->msg_accept_encoding);
    TEST_1(bzip2 = gzip->aa_next);
    TEST_1(deflate = bzip2->aa_next);

    TEST_1(gzip->aa_common->h_data);
    TEST_1(lzss = msg_accept_encoding_make(msg_home(msg), "lzss"));
Pekka Pessi's avatar
Pekka Pessi committed
665
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)bzip2, (void *)lzss), 0);
Pekka Pessi's avatar
Pekka Pessi committed
666 667 668 669 670 671 672
    TEST_1(gzip->aa_common->h_data);

    TEST_1(en = tst->msg_accept_language);
    TEST_1(fi = en->aa_next);
    TEST_1(se = fi->aa_next);

    TEST_S(en->aa_value, "en");
Pekka Pessi's avatar
Pekka Pessi committed
673
    TEST_M(en->aa_common->h_data,
Pekka Pessi's avatar
Pekka Pessi committed
674 675 676
	   "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF,
	   en->aa_common->h_len);

Pekka Pessi's avatar
Pekka Pessi committed
677
    TEST_P((char *)en->aa_common->h_data + en->aa_common->h_len,
678
	   fi->aa_common->h_data);
679
    TEST(fi->aa_common->h_len, 0);
Pekka Pessi's avatar
Pekka Pessi committed
680
    TEST_P((char *)en->aa_common->h_data + en->aa_common->h_len,
681 682
	   se->aa_common->h_data);
    TEST(se->aa_common->h_len, 0);
Pekka Pessi's avatar
Pekka Pessi committed
683 684 685

    TEST_1(de = msg_accept_language_make(msg_home(msg), "de;q=0.3"));

Pekka Pessi's avatar
Pekka Pessi committed
686
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)se, (void *)de), 0);
687 688 689 690 691
    TEST_P(en->aa_common->h_data, NULL);
    TEST_P(en->aa_next, fi);
    TEST_P(fi->aa_next, de);
    TEST_P(de->aa_next, NULL);

Pekka Pessi's avatar
Pekka Pessi committed
692
    TEST_P(en->aa_common->h_succ, fi);
693
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
Pekka Pessi's avatar
Pekka Pessi committed
694
    TEST_P(fi->aa_common->h_succ, de);
695 696 697 698 699 700 701
    TEST_P(fi->aa_common->h_prev, &en->aa_common->h_succ);
    TEST_P(de->aa_common->h_succ, NULL);
    TEST_P(de->aa_common->h_prev, &fi->aa_common->h_succ);

    TEST_P(se->aa_next, NULL);
    TEST_P(se->aa_common->h_succ, NULL);
    TEST_P(se->aa_common->h_prev, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
702

Pekka Pessi's avatar
Pekka Pessi committed
703
    TEST_1(sv = msg_accept_language_make(msg_home(msg),
Pekka Pessi's avatar
Pekka Pessi committed
704 705 706
					 "sv;q=0.6,sv_FI;q=0.7"));
    TEST_1(sv_fi = sv->aa_next);

Pekka Pessi's avatar
Pekka Pessi committed
707
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)fi, (void *)sv), 0);
Pekka Pessi's avatar
Pekka Pessi committed
708

709
    TEST_P(en->aa_next, sv);
Pekka Pessi's avatar
Pekka Pessi committed
710
    TEST_P(sv->aa_next->aa_next, de);
711
    TEST_P(de->aa_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
712

Pekka Pessi's avatar
Pekka Pessi committed
713
    TEST_P(en->aa_common->h_succ, sv);
714
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
Pekka Pessi's avatar
Pekka Pessi committed
715
    TEST_P(sv->aa_common->h_succ, sv_fi);
716
    TEST_P(sv->aa_common->h_prev, &en->aa_common->h_succ);
Pekka Pessi's avatar
Pekka Pessi committed
717
    TEST_P(sv_fi->aa_common->h_succ, de);
718 719 720
    TEST_P(sv_fi->aa_common->h_prev, &sv->aa_common->h_succ);
    TEST_P(de->aa_common->h_succ, NULL);
    TEST_P(de->aa_common->h_prev, &sv_fi->aa_common->h_succ);
Pekka Pessi's avatar
Pekka Pessi committed
721

Pekka Pessi's avatar
Pekka Pessi committed
722
    TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
723 724
  }

Pekka Pessi's avatar
Pekka Pessi committed
725 726 727 728 729
  /* Bug #2429 */
  orig = read_msg("GET a-life HTTP/1.1" CRLF
		 "Foo: bar" CRLF
		 "Content-Length: 6" CRLF
		 CRLF
Pekka Pessi's avatar
Pekka Pessi committed
730
		 "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
731 732
		 "extra stuff" CRLF);
  TEST_1(orig);
Pekka Pessi's avatar
Pekka Pessi committed
733
  otst = msg_test_public(orig);
Pekka Pessi's avatar
Pekka Pessi committed
734 735
  TEST_1(otst);

Pekka Pessi's avatar
Pekka Pessi committed
736 737
  msg = msg_copy(orig);
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
738 739 740 741 742 743
  TEST_1(tst);

  home = msg_home(msg);

  TEST_1(request = msg_request_make(home, "GET a-wife HTTP/1.1"));

Pekka Pessi's avatar
Pekka Pessi committed
744
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)request), 0);
Pekka Pessi's avatar
Pekka Pessi committed
745

Pekka Pessi's avatar
Pekka Pessi committed
746
  TEST_1(location =
Pekka Pessi's avatar
Pekka Pessi committed
747 748
	 msg_content_location_make(home, "http://localhost:8080/wife"));

Pekka Pessi's avatar
Pekka Pessi committed
749
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)location), 0);
Pekka Pessi's avatar
Pekka Pessi committed
750

Pekka Pessi's avatar
Pekka Pessi committed
751
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
752 753
  TEST_1(msg_prepare(msg) > 0);

Pekka Pessi's avatar
Pekka Pessi committed
754
  TEST_1(language =
Pekka Pessi's avatar
Pekka Pessi committed
755
	 msg_content_language_make(home, "se-FI, fi-FI, sv-FI"));
Pekka Pessi's avatar
Pekka Pessi committed
756
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)language), 0);
757 758 759 760

  TEST_1(se = msg_accept_language_make(home, "se, fi, sv"));
  TEST_1(se->aa_next);  TEST_1(se->aa_next->aa_next);
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)se), 0);
Pekka Pessi's avatar
Pekka Pessi committed
761

Pekka Pessi's avatar
Pekka Pessi committed
762
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
763 764 765 766 767 768
  TEST_1(msg_prepare(msg) > 0);

  {
    char const encoded[] =
      "GET a-wife HTTP/1.1\r\n";

769
    TEST_SIZE(request->rq_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
770 771 772 773 774 775 776
    TEST_M(request->rq_common->h_data, encoded, request->rq_common->h_len);
  }

  {
    char const encoded[] =
      "Content-Location: http://localhost:8080/wife\r\n";

777
    TEST_SIZE(location->g_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
778 779 780 781 782 783
    TEST_M(location->g_common->h_data, encoded, location->g_common->h_len);
  }

  {
    char const encoded[] =
      "Content-Language: se-FI, fi-FI, sv-FI\r\n";
784
    TEST_SIZE(language->k_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
785 786 787
    TEST_M(language->k_common->h_data, encoded, language->k_common->h_len);
  }

788 789 790 791
  {
    char const encoded[] = "Accept-Language: se, fi, sv\r\n";
    TEST_SIZE(se->aa_common->h_len, strlen(encoded));
    TEST_M(se->aa_common->h_data, encoded, se->aa_common->h_len);
Pekka Pessi's avatar
Pekka Pessi committed
792
    TEST_P((char *)se->aa_common->h_data + se->aa_common->h_len,
793
	   se->aa_next->aa_common->h_data);
Pekka Pessi's avatar
Pekka Pessi committed
794
    TEST_P((char *)se->aa_common->h_data + se->aa_common->h_len,
795 796 797 798 799
	   se->aa_next->aa_next->aa_common->h_data);
  }

  {
    size_t size = SIZE_MAX;
Pekka Pessi's avatar
Pekka Pessi committed
800 801 802 803 804 805 806
    char *s;
    char body[66 * 15 + 1];
    int i;
    msg_payload_t *pl;

    /* Bug #1726034 */
    for (i = 0; i < 15; i++)
Pekka Pessi's avatar
Pekka Pessi committed
807
      strcpy(body + i * 66,
Pekka Pessi's avatar
Pekka Pessi committed
808 809 810 811 812 813 814
	     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
	     "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n");
    pl = msg_payload_make(msg_home(msg), body);

    TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)pl), 0);

    s = msg_as_string(msg_home(msg), msg, NULL, 0, &size);
Pekka Pessi's avatar
Pekka Pessi committed
815
    TEST_S(s,
816 817 818 819 820 821 822
"GET a-wife HTTP/1.1" CRLF
"Foo: bar" CRLF
"Content-Length: 6" CRLF
"Content-Location: http://localhost:8080/wife\r\n"
"Content-Language: se-FI, fi-FI, sv-FI\r\n"
"Accept-Language: se, fi, sv\r\n"
CRLF
Pekka Pessi's avatar
Pekka Pessi committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
"test" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" CRLF
);
840 841
  }

Pekka Pessi's avatar
Pekka Pessi committed
842 843 844 845 846 847 848 849 850 851 852 853 854
  msg_destroy(msg);

  END();
}

static int test_warning(void)
{
  msg_warning_t *w;
  su_home_t *home;
  char buf[64];

  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
855
  TEST_1(home = su_home_new(sizeof *home));
Pekka Pessi's avatar
Pekka Pessi committed
856

Pekka Pessi's avatar
Pekka Pessi committed
857
  TEST_1((w = msg_warning_make(home,
Pekka Pessi's avatar
Pekka Pessi committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
			       "399 host:5060 \"Ok\", "
			       "399 [::1]:39999 \"foo\\\" bar\"")));
  TEST(w->w_code, 399);
  TEST_S(w->w_host, "host");
  TEST_S(w->w_port, "5060");
  TEST_S(w->w_text, "Ok");
  TEST_1(w = w->w_next);

  TEST(w->w_code, 399);
  TEST_S(w->w_host, "[::1]");
  TEST_S(w->w_port, "39999");
  TEST_S(w->w_text, "foo\" bar");
  TEST_1(w->w_next == NULL);

  TEST_1(msg_warning_e(buf, sizeof buf, (msg_header_t *)w, 0) > 0);

  TEST_S(buf, "399 [::1]:39999 \"foo\\\" bar\"");

Pekka Pessi's avatar
Pekka Pessi committed
876
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

  END();
}


/* Test error handling */
int test_msg_error(void)
{
  msg_t *msg;
  su_home_t *home;
  msg_test_t *tst;

  BEGIN();

  msg = read_msg("GET a-life HTTP/1.1" CRLF
		 "Content-Length: 6" CRLF
		 "Content-Language: fi" CRLF
		 "Content-Language: <se>" CRLF
		 "Accept-Language: en;q=0.8, fi, \"\", se ; q = 0.6" CRLF
		 "Foo bar baf: bar" CRLF
		 CRLF
		 "test" CRLF);

  home = msg_home(msg);
Pekka Pessi's avatar
Pekka Pessi committed
901
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940

  TEST_1(msg);
  TEST_1(home);
  TEST_1(tst);

  TEST_1(tst->msg_error);

  msg_destroy(msg);

  END();
}

int test_mclass(void)
{
  msg_t *msg;
  su_home_t *home;
  msg_test_t *tst;
  msg_request_t *request;
  msg_status_t *status;
  msg_separator_t *separator;
  msg_payload_t *payload;
  msg_content_length_t *l;
  msg_content_language_t *la;
  msg_content_encoding_t *k0, *k;
  msg_unknown_t *foo;

  BEGIN();

  /* Test that critical errors are signaled */
  msg = read_msg("GET a-life HTTP/1.1" CRLF
		 "Content-Length: 6bytes" CRLF
		 "Content-Type: *" CRLF
		 "Foo: bar" CRLF
		 "Content-Encoding: identity" CRLF
		 "Content-Language: en" CRLF
		 "Content-Language: en-us" CRLF
		 CRLF
		 "test" CRLF);

Pekka Pessi's avatar
Pekka Pessi committed
941
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  TEST_1(msg);
  TEST_1(tst);
  TEST_1(MSG_HAS_ERROR(tst->msg_flags)); /* Content-Length is critical */
  msg_destroy(msg);

  msg = read_msg("GET a-life HTTP/1.1" CRLF
		 "Content-Length: 6" CRLF
		 "Content-Type: *" CRLF
		 "Foo: bar" CRLF
		 "Content-Encoding: " CRLF /* XXX */
		 "Content-Language: en" CRLF
		 "Content-Language: en-us" CRLF
		 CRLF
		 "test" CRLF);

  home = msg_home(msg);
Pekka Pessi's avatar
Pekka Pessi committed
958
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
959 960 961 962 963

  TEST_1(msg);
  TEST_1(home);
  TEST_1(tst);

964
  TEST_SIZE(msg_iovec(msg, NULL, 0), 1);
Pekka Pessi's avatar
Pekka Pessi committed
965 966 967 968 969 970 971 972 973 974 975 976 977

  TEST_1(tst->msg_unknown);	/* Foo */
  TEST_1(tst->msg_content_type == NULL);
  TEST_1(tst->msg_error);	/* Content-Type */
  TEST_1(tst->msg_error->er_next == NULL);

  TEST_1(!MSG_HAS_ERROR(tst->msg_flags)); /* Content-type is not critical */

  TEST_1(la = tst->msg_content_language);
  TEST_1(la->k_common->h_data);
  TEST_1(la->k_items);
  TEST_S(la->k_items[0], "en");
  TEST_S(la->k_items[1], "en-us");
978
  TEST_P(la->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
979 980 981 982
  TEST_1(la->k_next);
  TEST_1(la->k_next->k_common->h_data);
  TEST_1(la->k_next->k_items == NULL);

Pekka Pessi's avatar
Pekka Pessi committed
983 984
  TEST(msg_header_add_make(msg, (msg_pub_t *)tst,
			   msg_content_language_class,
Pekka Pessi's avatar
Pekka Pessi committed
985
			   "en-gb"), 0);
986 987
  TEST_P(la, tst->msg_content_language);
  TEST_P(la->k_common->h_data, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
988
  TEST_S(la->k_items[2], "en-gb");
989
  TEST_P(la->k_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
990 991

  TEST_1(status = msg_status_make(home, "HTTP/1.1 200 Ok"));
Pekka Pessi's avatar
Pekka Pessi committed
992
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)status), 0);
993
  TEST_P(tst->msg_status, status); TEST_P(tst->msg_request, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
994 995

  TEST_1(request = msg_request_make(home, "GET a-wife HTTP/1.0"));
Pekka Pessi's avatar
Pekka Pessi committed
996
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)request), 0);
997
  TEST_P(tst->msg_request, request); TEST_P(tst->msg_status, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
998 999

  TEST_1(separator = msg_separator_make(home, "\r\n"));
Pekka Pessi's avatar
Pekka Pessi committed
1000
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1001
  TEST_P(tst->msg_separator, separator);
1002
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
1003 1004 1005

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
Pekka Pessi's avatar
Pekka Pessi committed
1006
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1007
  /* The new payload should be appended */
Pekka Pessi's avatar
Pekka Pessi committed
1008
  TEST_P(tst->msg_payload->pl_next, payload);
1009
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
1010 1011

  /* Try to add a new header */
Pekka Pessi's avatar
Pekka Pessi committed
1012 1013
  TEST_1(l = msg_content_length_create(home,
				       tst->msg_payload->pl_len +
Pekka Pessi's avatar
Pekka Pessi committed
1014
				       payload->pl_len));
Pekka Pessi's avatar
Pekka Pessi committed
1015
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)l), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1016
  /* The new header should be last before separator */
1017
  TEST_P(l->l_common->h_succ, separator);
Pekka Pessi's avatar
Pekka Pessi committed
1018 1019 1020 1021 1022 1023

  TEST_1(foo = tst->msg_unknown);
  TEST_S(foo->un_name, "Foo");
  TEST_S(foo->un_value, "bar");
  foo->un_value = "baz";
  TEST_1(foo = msg_unknown_dup(home, foo));
Pekka Pessi's avatar
Pekka Pessi committed
1024
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)foo), 0);
1025
  TEST_P(tst->msg_unknown->un_next, foo);
Pekka Pessi's avatar
Pekka Pessi committed
1026 1027 1028

  TEST_1(k = msg_content_encoding_make(home, "gzip, compress"));
  k0 = tst->msg_content_encoding;
Pekka Pessi's avatar
Pekka Pessi committed
1029
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
1030
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
1031 1032 1033
  TEST_1(k0->k_items);
  TEST_S(k0->k_items[0], "gzip");
  TEST_S(k0->k_items[1], "compress");
1034
  TEST_P(k0->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1035 1036

  TEST_1(k = msg_content_encoding_make(home, "gzip, deflate, compress"));
Pekka Pessi's avatar
Pekka Pessi committed
1037
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
1038
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
1039 1040 1041 1042
  TEST_1(k0->k_items);
  TEST_S(k0->k_items[0], "gzip");
  TEST_S(k0->k_items[1], "compress");
  TEST_S(k0->k_items[2], "deflate");
1043
  TEST_P(k0->k_items[3], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

  msg_destroy(msg);

  END();
}

int test_copy(void)
{
  msg_t *msg0, *msg, *msg1, *msg2;
  su_home_t *home;
  msg_test_t *tst0, *tst, *copy, *dup;
  msg_request_t *request;
  msg_common_t *h, *h_succ;
  msg_iovec_t iovec[8];

Pekka Pessi's avatar
Pekka Pessi committed
1059
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    "GET /a-life HTTP/1.1" CRLF
    "Content-Length: 6" CRLF
    "Content-Type: *" CRLF
    "Foo: bar" CRLF
    "Content-Language: " CRLF
    CRLF
    "test" CRLF;

  BEGIN();

  msg0 = read_msg(s);

  TEST_1(msg0);
  TEST_1(tst0 = msg_test_public(msg0));

1075
  TEST_SIZE(msg_iovec(msg0, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1076 1077 1078 1079 1080

  TEST_1(msg = msg_copy(msg0));
  TEST_1(copy = msg_test_public(msg));
  TEST_1(copy->msg_request);
  TEST_1(tst0->msg_request);
Pekka Pessi's avatar
Pekka Pessi committed
1081
  TEST_S(copy->msg_request->rq_url->url_path,
Pekka Pessi's avatar
Pekka Pessi committed
1082 1083
	 tst0->msg_request->rq_url->url_path);
  TEST_S(copy->msg_request->rq_url->url_path, "a-life");
Pekka Pessi's avatar
Pekka Pessi committed
1084
  TEST_P(copy->msg_request->rq_url->url_path,
1085
	 tst0->msg_request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1086 1087 1088 1089 1090 1091 1092

  msg_destroy(msg);

  TEST_1(msg = msg_dup(msg0));
  TEST_1(dup = msg_test_public(msg));
  TEST_1(dup->msg_request);
  TEST_1(tst0->msg_request);
Pekka Pessi's avatar
Pekka Pessi committed
1093
  TEST_S(dup->msg_request->rq_url->url_path,
Pekka Pessi's avatar
Pekka Pessi committed
1094 1095
	 tst0->msg_request->rq_url->url_path);
  TEST_S(dup->msg_request->rq_url->url_path, "a-life");
Pekka Pessi's avatar
Pekka Pessi committed
1096
  TEST_1(dup->msg_request->rq_url->url_path !=
Pekka Pessi's avatar
Pekka Pessi committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	 tst0->msg_request->rq_url->url_path);

  msg_destroy(msg);

  TEST_1(msg = msg_copy(msg0)); msg_destroy(msg0);

  TEST_1(home = msg_home(msg));
  TEST_1(tst = msg_test_public(msg));

  TEST_1(tst->msg_unknown);	/* Foo */
  TEST_1(tst->msg_content_type == NULL);
  TEST_1(tst->msg_error); /* Content-Type */

  TEST_1(!MSG_HAS_ERROR(tst->msg_flags)); /* Flags are not copied */

  TEST_1(tst0->msg_request);
  TEST_1(request = tst->msg_request);
1114
  TEST_P(tst0->msg_request->rq_url->url_path, request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1115

1116
  TEST_SIZE(msg_iovec(msg, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

  TEST_S(iovec->siv_base, s);

  TEST_1(msg1 = msg_dup(msg));
  TEST_1(tst = msg_test_public(msg1));
  TEST_1(tst->msg_request);

  for (h = tst->msg_request->rq_common; h; h = h_succ) {
    if (h->h_prev)
      *h->h_prev = NULL;
    h_succ = (msg_common_t*)h->h_succ;
    h->h_succ = NULL;
  }

  TEST_1(msg2 = msg_copy(msg1));
  msg_destroy(msg2);

  TEST_1(msg2 = msg_dup(msg1));
  msg_destroy(msg2);

  msg_destroy(msg1);
  msg_destroy(msg);

  END();
}

int test_mime(void)
{
  msg_t *msg;
  su_home_t *home;
  int n;
  msg_test_t *tst;
  msg_header_t *h, *h_succ, *head;
  void *removed;
  msg_accept_t *ac, *ac0;
  msg_accept_charset_t *aa;
  msg_multipart_t *mp, *mp0, *mpX, *mpnew;
  msg_payload_t *pl;
  msg_content_type_t *c;
  msg_content_id_t *cid;
  msg_content_transfer_encoding_t *cte;

Pekka Pessi's avatar
Pekka Pessi committed
1159
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    "GET /a-life HTTP/1.1" CRLF
    "Accept: text/html;level=4;q=1" CRLF
    "Accept: text / plain;q=0.9" CRLF
    "Accept-Charset: *;q=0.1, iso-latin-1, utf-8;q=0.9" CRLF
    "Accept-Encoding: gzip;q=0.9, deflate" CRLF
    "Accept-Encoding: , identity ," CRLF
    "Accept: */*;q=0.2" CRLF
    "Accept-Language: en;q=0.5, es;q=0.2, fr;q=0.9, fi, x-pig-latin" CRLF
    "Content-Language: fi, se" CRLF
    "Content-Language: en, de" CRLF
    "Content-Disposition: render; required" CRLF
    "Content-Encoding: gzip, deflate" CRLF
    "Content-Base: http://localhost/foo" CRLF
    "MIME-Version: 1.0" CRLF
    "Content-Type: multipart/alternative ; boundary=\"LaGqGt4BI6Ho\"" CRLF
    /* "Content-Length: 305" CRLF */
    "Content-MD5: LLO7gLaGqGt4BI6HouiWng==" CRLF
    CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1178
    "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1179
    CRLF			/* 1 */
Pekka Pessi's avatar
Pekka Pessi committed
1180
    "--LaGqGt4BI6Ho" "  " CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1181 1182 1183
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
Pekka Pessi's avatar
Pekka Pessi committed
1184
    "--LaGqGt4BI6Ho" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1185 1186 1187 1188 1189 1190 1191 1192 1193
    "Content-Type: text/plain ; charset = iso-8859-1" CRLF /* 5 */
    "Content-ID: <m7ZvEEm49xdTT0WCDUgnww@localhost>" CRLF /* 6 */
    "Content-Transfer-Encoding: quoted-unreadable" CRLF	/* 7 */
    CRLF			/* 8 */
    "part 2"			/* 9 */
    CRLF "--LaGqGt4BI6Ho"	/* 10 */
    "Content-Type: text/html" CRLF /* 11 */
    "Content-ID: <4SP77aQZ9z6Top2dvLqKPQ@localhost>" CRLF /* 12 */
    CRLF			/* 13 */
Pekka Pessi's avatar
Pekka Pessi committed
1194
#define BODY3 "<html><body>part 3</body></html>" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1195 1196
    BODY3			/* 14 */
    CRLF			/* 15 */
Pekka Pessi's avatar
Pekka Pessi committed
1197
    "--LaGqGt4BI6Ho--" CRLF;
Pekka Pessi's avatar
Pekka Pessi committed
1198 1199 1200 1201 1202

  BEGIN();

  msg = read_msg(s);
  home = msg_home(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1203
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

  TEST_1(msg);
  TEST_1(home);
  TEST_1(tst);

  TEST_1(tst->msg_error == NULL);
  TEST_1((tst->msg_flags & MSG_FLG_ERROR) == 0);

  TEST_1(ac = tst->msg_accept);
  TEST_1(ac = ac->ac_next);
  TEST_S(ac->ac_type, "text/plain");
  TEST_S(ac->ac_q, "0.9");
  TEST_1(ac = msg_accept_dup(home, ac));
  TEST_S(ac->ac_type, "text/plain");
  TEST_S(ac->ac_q, "0.9");
  TEST_S(tst->msg_accept->ac_next->ac_q, "0.9");
  TEST_1(ac->ac_q != tst->msg_accept->ac_next->ac_q);

  TEST_1(ac = msg_accept_make(home, "text / plain"));
  ac->ac_q = "1.0";
  TEST_1(ac = msg_accept_dup(home, ac0 = ac));
  TEST_1(ac->ac_q != ac0->ac_q);

  for (h = (msg_header_t *)tst->msg_request; h; h = h->sh_succ) {
    TEST_1(h->sh_data);
    if (h->sh_succ)
1230
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  }

  TEST_1(aa = tst->msg_accept_charset);
  TEST_S(aa->aa_value, "*");   TEST_S(aa->aa_q, "0.1");

  mp = msg_multipart_parse(home, tst->msg_content_type, tst->msg_payload);

  TEST_1(mp0 = mp);

  TEST_1(mp->mp_data);
  TEST(memcmp(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CRLF, mp->mp_len), 0);
  TEST_1(mp->mp_common->h_data);
Pekka Pessi's avatar
Pekka Pessi committed
1243
  TEST_M(mp->mp_common->h_data, CRLF "--" "LaGqGt4BI6Ho" "  " CRLF,
Pekka Pessi's avatar
Pekka Pessi committed
1244 1245 1246
	 mp->mp_common->h_len);

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
1247
  TEST_SIZE(strlen("part 1" CRLF), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1248 1249 1250 1251 1252 1253 1254 1255 1256
  TEST(memcmp(pl->pl_data, "part 1" CRLF, pl->pl_len), 0);

  TEST_1(mp = mp->mp_next);

  TEST_1(mp->mp_data);
  TEST(memcmp(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CR LF, mp->mp_len), 0);

  TEST_1(c = mp->mp_content_type);
  TEST_S(c->c_type, "text/plain"); TEST_S(c->c_subtype, "plain");
Pekka Pessi's avatar
Pekka Pessi committed
1257
  TEST_1(c->c_params);   TEST_1(c->c_params[0]);
Pekka Pessi's avatar
Pekka Pessi committed
1258 1259 1260 1261 1262 1263 1264 1265
  TEST_S(c->c_params[0], "charset=iso-8859-1");

  TEST_1(cid = mp->mp_content_id);

  TEST_1(cte = mp->mp_content_transfer_encoding);
  TEST_S(cte->g_string, "quoted-unreadable");

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
1266
  TEST_SIZE(strlen("part 2"), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1267
  TEST(memcmp(pl->pl_data, "part 2", pl->pl_len), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1268

Pekka Pessi's avatar
Pekka Pessi committed
1269 1270 1271 1272 1273 1274
  TEST_1(mp = mp->mp_next);

  TEST_1(mp->mp_data);
  TEST_M(mp->mp_data, CRLF "--" "LaGqGt4BI6Ho" CRLF, mp->mp_len);

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
1275
  TEST_SIZE(strlen(BODY3), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1276 1277 1278 1279 1280 1281 1282 1283 1284
  TEST(memcmp(pl->pl_data, BODY3, pl->pl_len), 0);

  mpX = mp;

  TEST_1(!(mp = mp->mp_next));

  /* Test serialization */
  head = NULL;
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1285
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
  TEST_1(!msg_chain_errors((msg_header_t *)mp0));

  /* Remove chain */
  for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) {
    h_succ = h->sh_succ;
    if (h->sh_prev) *h->sh_prev = NULL;
    h->sh_prev = NULL;
    h->sh_succ = NULL;
  }

  TEST(n, 15);

Pekka Pessi's avatar
Pekka Pessi committed
1298
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1299
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1300
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1301 1302 1303 1304 1305 1306 1307 1308
  TEST_1(!msg_chain_errors((msg_header_t *)mp0));

  for (h = (msg_header_t *)mp0, n = 0; h; h = h_succ, n++) {
    h_succ = h->sh_succ;
  }

  TEST(n, 15);

Pekka Pessi's avatar
Pekka Pessi committed
1309 1310
  /* Add a new part to multipart */
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew);
Pekka Pessi's avatar
Pekka Pessi committed
1311 1312 1313 1314 1315 1316
  removed = mpX->mp_close_delim;
  mpX->mp_next = mpnew; mpX = mpnew;
  mpnew->mp_content_type = msg_content_type_make(home, "multipart/mixed");
  TEST_1(mpnew->mp_content_type);
  TEST(msg_multipart_complete(msg_home(msg), tst->msg_content_type, mp0), 0);

Pekka Pessi's avatar
Pekka Pessi committed
1317
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1318
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1319
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed