test_msg.c 50.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();
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;
271

Pekka Pessi's avatar
Pekka Pessi committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    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;
    }

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    master = ";0";

    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(NULL, &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(NULL, (void *)p), p = NULL;
    }

Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309 310 311 312 313 314
    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\"";
315

Pekka Pessi's avatar
Pekka Pessi committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    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";
331

Pekka Pessi's avatar
Pekka Pessi committed
332 333 334 335 336 337 338 339
    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);
  }
340

Pekka Pessi's avatar
Pekka Pessi committed
341 342 343 344 345
  {
    /* 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";
346

Pekka Pessi's avatar
Pekka Pessi committed
347 348 349 350 351 352
    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);
353

Pekka Pessi's avatar
Pekka Pessi committed
354 355 356 357 358 359
    su_home_deinit(home);
  }

  {
    /* Test that long lists are handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
360

Pekka Pessi's avatar
Pekka Pessi committed
361 362 363 364 365
    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";
366
    char list4[] = "one, two, three, four, five, six, seven, eight, nine";
Pekka Pessi's avatar
Pekka Pessi committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

    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;
387

Pekka Pessi's avatar
Pekka Pessi committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    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) };
410

Pekka Pessi's avatar
Pekka Pessi committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    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,  ,   ";
430

Pekka Pessi's avatar
Pekka Pessi committed
431 432 433 434 435 436 437 438 439 440 441 442 443
    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== ,,";
444

Pekka Pessi's avatar
Pekka Pessi committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    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;
462

Pekka Pessi's avatar
Pekka Pessi committed
463 464 465 466 467 468 469 470 471 472 473 474
    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);
  }

475 476 477 478 479 480 481 482
  {
    /* 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"), "");
483
    TEST_S(msg_header_find_item(ce->k_common, "zz"), "zz");
484
    TEST_P(msg_header_find_param(ce->k_common, "k"), NULL);
485 486 487 488
    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);
489
    TEST_P(msg_header_find_param(ce->k_common, "zip"), NULL);
490 491 492
    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");
493
    TEST(msg_header_replace_param(home, ce->k_common, "zip=zup"), 1);
494 495 496 497 498
    TEST_S(msg_header_find_param(ce->k_common, "zip"), "zup");

    su_home_deinit(home);
  }

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  {
    char b[8];
    TEST(msg_unquoted_e(NULL, 0, "\"\""), 6);
    TEST(msg_unquoted_e(b, 0, "\"\""), 6);
    TEST(msg_unquoted_e(b, 4, "\"\""), 6);
    TEST(msg_unquoted_e(b, 6, "\"\""), 6);
    TEST(memcmp(b, "\"\\\"\\\"\"", 6), 0);
    TEST(msg_unquoted_e(b, 4, "\""), 4);
    memset(b, 0, sizeof b);
    TEST(msg_unquoted_e(b, 1, "\"kuik"), 8);
    TEST(memcmp(b, "\"\0", 2), 0);
    TEST(msg_unquoted_e(b, 3, "\"kuik"), 8);
    TEST(memcmp(b, "\"\\\"\0", 4), 0);
    TEST(msg_unquoted_e(b, 7, "\"kuik"), 8);
    TEST(memcmp(b, "\"\\\"kuik\0", 8), 0);
  }
515

Pekka Pessi's avatar
Pekka Pessi committed
516 517 518 519 520 521 522 523
  END();
}

int hash_test(void)
{
  int i, j, hash = 0;
  msg_mclass_t const *mc = msg_test_mclass;
  msg_hclass_t *hc;
524

Pekka Pessi's avatar
Pekka Pessi committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  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
551
  return msg_make(msg_test_mclass, MSG_DO_EXTRACT_COPY, buffer, -1);
Pekka Pessi's avatar
Pekka Pessi committed
552 553
}

554
/**Check if header chain contains any loops.
Pekka Pessi's avatar
Pekka Pessi committed
555 556 557 558
 *
 * @return
 * Return 0 if no loop, -1 otherwise.
 */
559
static
Pekka Pessi's avatar
Pekka Pessi committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
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;
}

579
/** Check header chain consistency.
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582 583 584 585 586 587 588
 *
 * @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;
589

Pekka Pessi's avatar
Pekka Pessi committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
  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;
609
  msg_accept_language_t *en, *se;
Pekka Pessi's avatar
Pekka Pessi committed
610 611 612 613 614 615 616
  msg_separator_t *separator;
  msg_payload_t *payload;

  BEGIN();

  msg = read_msg("GET a-life HTTP/1.1" CRLF
		 "Content-Length: 6" CRLF
617
		 "Accept-Encoding: bzip2" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
618
		 "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF
619
		 "Accept-Encoding: gzip" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
620 621 622 623 624
		 "Foo: bar" CRLF
		 CRLF
		 "test" CRLF);

  home = msg_home(msg);
625
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
626 627 628 629 630

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

631
  TEST_P(tst->msg_error, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
632 633 634 635

  TEST_1(tst->msg_accept_language);

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

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

  TEST_1(separator = msg_separator_make(home, "\r\n"));
644
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
645
  TEST_P(tst->msg_separator, separator);
646
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
647 648 649

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
650
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
651
  /* It is appended */
652
  TEST_P(tst->msg_payload->pl_next, payload);
653
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
654 655 656 657 658 659 660

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

    vs = NULL;
661
    MSG_PARAM_MATCH(vs, foo, "foo");
Pekka Pessi's avatar
Pekka Pessi committed
662 663
    TEST_S(vs, "bar");
    vs = NULL;
664
    MSG_PARAM_MATCH(vs, foo, "fo");
665
    TEST_P(vs, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
666 667 668 669 670 671 672 673 674 675
    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);
  }

676 677 678 679 680 681 682 683
  /* Test msg_fragment_clear_chain() */
  en = tst->msg_accept_language;
  TEST_1(en->aa_common->h_data != NULL);
  msg_fragment_clear_chain((msg_header_t *)en->aa_next->aa_next);
  TEST_1(en->aa_common->h_data == NULL);
  TEST_1(en->aa_next->aa_common->h_data == NULL);
  TEST_1(en->aa_next->aa_next->aa_common->h_data == NULL);

Pekka Pessi's avatar
Pekka Pessi committed
684 685
  msg_destroy(msg);

686 687 688 689 690 691 692 693
  /* 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);
694
  tst = msg_test_public(msg);
695 696 697 698 699 700 701 702 703 704 705 706 707 708
  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"));
709
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)bzip2, (void *)lzss), 0);
710 711 712 713 714 715 716
    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");
717
    TEST_M(en->aa_common->h_data,
718 719 720
	   "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF,
	   en->aa_common->h_len);

721
    TEST_P((char *)en->aa_common->h_data + en->aa_common->h_len,
722
	   fi->aa_common->h_data);
723
    TEST(fi->aa_common->h_len, 0);
724
    TEST_P((char *)en->aa_common->h_data + en->aa_common->h_len,
725 726
	   se->aa_common->h_data);
    TEST(se->aa_common->h_len, 0);
727 728 729

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

730
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)se, (void *)de), 0);
731 732 733 734 735
    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);

736
    TEST_P(en->aa_common->h_succ, fi);
737
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
738
    TEST_P(fi->aa_common->h_succ, de);
739 740 741 742 743 744 745
    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);
746

747
    TEST_1(sv = msg_accept_language_make(msg_home(msg),
748 749 750
					 "sv;q=0.6,sv_FI;q=0.7"));
    TEST_1(sv_fi = sv->aa_next);

751
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)fi, (void *)sv), 0);
752

753
    TEST_P(en->aa_next, sv);
754
    TEST_P(sv->aa_next->aa_next, de);
755
    TEST_P(de->aa_next, NULL);
756

757
    TEST_P(en->aa_common->h_succ, sv);
758
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
759
    TEST_P(sv->aa_common->h_succ, sv_fi);
760
    TEST_P(sv->aa_common->h_prev, &en->aa_common->h_succ);
761
    TEST_P(sv_fi->aa_common->h_succ, de);
762 763 764
    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);
765

766
    TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
767 768
  }

769 770
  msg_destroy(msg);

Pekka Pessi's avatar
Pekka Pessi committed
771 772 773 774 775
  /* Bug #2429 */
  orig = read_msg("GET a-life HTTP/1.1" CRLF
		 "Foo: bar" CRLF
		 "Content-Length: 6" CRLF
		 CRLF
776
		 "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
777 778
		 "extra stuff" CRLF);
  TEST_1(orig);
779
  otst = msg_test_public(orig);
Pekka Pessi's avatar
Pekka Pessi committed
780 781
  TEST_1(otst);

782
  msg = msg_copy(orig);
783
  msg_destroy(orig);
784
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
785 786 787 788 789 790
  TEST_1(tst);

  home = msg_home(msg);

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

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

793
  TEST_1(location =
Pekka Pessi's avatar
Pekka Pessi committed
794 795
	 msg_content_location_make(home, "http://localhost:8080/wife"));

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

798
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
799 800
  TEST_1(msg_prepare(msg) > 0);

801
  TEST_1(language =
Pekka Pessi's avatar
Pekka Pessi committed
802
	 msg_content_language_make(home, "se-FI, fi-FI, sv-FI"));
803
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)language), 0);
804 805 806 807

  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);
808

809
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
810 811 812 813 814 815
  TEST_1(msg_prepare(msg) > 0);

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

816
    TEST_SIZE(request->rq_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
817 818 819 820 821 822 823
    TEST_M(request->rq_common->h_data, encoded, request->rq_common->h_len);
  }

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

824
    TEST_SIZE(location->g_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
825 826 827 828 829 830
    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";
831
    TEST_SIZE(language->k_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
832 833 834
    TEST_M(language->k_common->h_data, encoded, language->k_common->h_len);
  }

835 836 837 838
  {
    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);
839
    TEST_P((char *)se->aa_common->h_data + se->aa_common->h_len,
840
	   se->aa_next->aa_common->h_data);
841
    TEST_P((char *)se->aa_common->h_data + se->aa_common->h_len,
842 843 844 845 846
	   se->aa_next->aa_next->aa_common->h_data);
  }

  {
    size_t size = SIZE_MAX;
847 848 849 850 851 852 853
    char *s;
    char body[66 * 15 + 1];
    int i;
    msg_payload_t *pl;

    /* Bug #1726034 */
    for (i = 0; i < 15; i++)
854
      strcpy(body + i * 66,
855 856 857 858 859 860 861
	     "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);
862
    TEST_S(s,
863 864 865 866 867 868 869
"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
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
"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
);
887 888
  }

Pekka Pessi's avatar
Pekka Pessi committed
889 890 891 892 893 894 895 896 897 898 899 900 901
  msg_destroy(msg);

  END();
}

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

  BEGIN();

902
  TEST_1(home = su_home_new(sizeof *home));
Pekka Pessi's avatar
Pekka Pessi committed
903

904
  TEST_1((w = msg_warning_make(home,
Pekka Pessi's avatar
Pekka Pessi committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
			       "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\"");

923
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

  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);
948
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

  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);

988
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
  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);
1005
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1006 1007 1008 1009 1010

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

1011
  TEST_SIZE(msg_iovec(msg, NULL, 0), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

  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");
1025
  TEST_P(la->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1026 1027 1028 1029
  TEST_1(la->k_next);
  TEST_1(la->k_next->k_common->h_data);
  TEST_1(la->k_next->k_items == NULL);

1030 1031
  TEST(msg_header_add_make(msg, (msg_pub_t *)tst,
			   msg_content_language_class,
1032
			   "en-gb"), 0);
1033 1034
  TEST_P(la, tst->msg_content_language);
  TEST_P(la->k_common->h_data, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1035
  TEST_S(la->k_items[2], "en-gb");
1036
  TEST_P(la->k_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1037 1038

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

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

  TEST_1(separator = msg_separator_make(home, "\r\n"));
1047
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
1048
  TEST_P(tst->msg_separator, separator);
1049
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
1050 1051 1052

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
1053
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1054
  /* The new payload should be appended */
1055
  TEST_P(tst->msg_payload->pl_next, payload);
1056
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
1057 1058

  /* Try to add a new header */
1059 1060
  TEST_1(l = msg_content_length_create(home,
				       tst->msg_payload->pl_len +
Pekka Pessi's avatar
Pekka Pessi committed
1061
				       payload->pl_len));
1062
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)l), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1063
  /* The new header should be last before separator */
1064
  TEST_P(l->l_common->h_succ, separator);
Pekka Pessi's avatar
Pekka Pessi committed
1065 1066 1067 1068 1069 1070

  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));
1071
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)foo), 0);
1072
  TEST_P(tst->msg_unknown->un_next, foo);
Pekka Pessi's avatar
Pekka Pessi committed
1073 1074 1075

  TEST_1(k = msg_content_encoding_make(home, "gzip, compress"));
  k0 = tst->msg_content_encoding;
1076
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
1077
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
1078 1079 1080
  TEST_1(k0->k_items);
  TEST_S(k0->k_items[0], "gzip");
  TEST_S(k0->k_items[1], "compress");
1081
  TEST_P(k0->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1082 1083

  TEST_1(k = msg_content_encoding_make(home, "gzip, deflate, compress"));
1084
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
1085
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
1086 1087 1088 1089
  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");
1090
  TEST_P(k0->k_items[3], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105

  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];

1106
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
    "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));

1122
  TEST_SIZE(msg_iovec(msg0, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1123 1124 1125 1126 1127

  TEST_1(msg = msg_copy(msg0));
  TEST_1(copy = msg_test_public(msg));
  TEST_1(copy->msg_request);
  TEST_1(tst0->msg_request);
1128
  TEST_S(copy->msg_request->rq_url->url_path,
Pekka Pessi's avatar
Pekka Pessi committed
1129 1130
	 tst0->msg_request->rq_url->url_path);
  TEST_S(copy->msg_request->rq_url->url_path, "a-life");
1131
  TEST_P(copy->msg_request->rq_url->url_path,
1132
	 tst0->msg_request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1133 1134 1135 1136 1137 1138 1139

  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);
1140
  TEST_S(dup->msg_request->rq_url->url_path,
Pekka Pessi's avatar
Pekka Pessi committed
1141 1142
	 tst0->msg_request->rq_url->url_path);
  TEST_S(dup->msg_request->rq_url->url_path, "a-life");
1143
  TEST_1(dup->msg_request->rq_url->url_path !=
Pekka Pessi's avatar
Pekka Pessi committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
	 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);
1161
  TEST_P(tst0->msg_request->rq_url->url_path, request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1162

1163
  TEST_SIZE(msg_iovec(msg, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

  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;

1206
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
    "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
1225
    "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1226
    CRLF			/* 1 */
1227
    "--LaGqGt4BI6Ho" "  " CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1228 1229 1230
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
1231
    "--LaGqGt4BI6Ho" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1232 1233 1234 1235 1236 1237 1238 1239 1240
    "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 */
1241
#define BODY3 "<html><body>part 3</body></html>" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1242
    BODY3			/* 14 */
1243 1244 1245 1246 1247 1248 1249 1250 1251
    CRLF "--LaGqGt4BI6Ho"	/* 15 */
    "c: text/html" CRLF		/* 16 */
    "l: 9" CRLF			/* 17 */
    "e: identity" CRLF		/* 18 */
    CRLF			/* 19 */
#define BODY4 "<html/>" CRLF
    BODY4			  /* 20 */
    CRLF "--LaGqGt4BI6Ho--"	  /* 21 */
    CRLF;
Pekka Pessi's avatar
Pekka Pessi committed
1252 1253 1254 1255 1256

  BEGIN();

  msg = read_msg(s);
  home = msg_home(msg);
1257
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

  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)
1284
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
  }

  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);
1297
  TEST_M(mp->mp_common->h_data, CRLF "--" "LaGqGt4BI6Ho" "  " CRLF,
Pekka Pessi's avatar
Pekka Pessi committed
1298 1299 1300
	 mp->mp_common->h_len);

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
1301
  TEST_SIZE(strlen("part 1" CRLF), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1302 1303 1304 1305 1306 1307 1308 1309 1310
  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");
1311
  TEST_1(c->c_params);   TEST_1(c->c_params[0]);
Pekka Pessi's avatar
Pekka Pessi committed
1312 1313 1314 1315 1316 1317 1318 1319
  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);
1320
  TEST_SIZE(strlen("part 2"), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1321
  TEST(memcmp(pl->pl_data, "part 2", pl->pl_len), 0);
1322

Pekka Pessi's avatar
Pekka Pessi committed
1323 1324 1325 1326 1327 1328
  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);
1329
  TEST_SIZE(strlen(BODY3), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1330 1331
  TEST(memcmp(pl->pl_data, BODY3, pl->pl_len), 0);

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
  TEST_1(mp = mp->mp_next);

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

  TEST_1(mp->mp_content_encoding);
  TEST_1(mp->mp_content_type);

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
  TEST_SIZE(strlen(BODY4), pl->pl_len);
  TEST(memcmp(pl->pl_data, BODY4, pl->pl_len), 0);

Pekka Pessi's avatar
Pekka Pessi committed
1344 1345 1346 1347 1348 1349 1350
  mpX = mp;

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

  /* Test serialization */
  head = NULL;
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1351
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  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;
  }

1362
  TEST(n, 21);
Pekka Pessi's avatar
Pekka Pessi committed
1363

1364
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1365
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1366
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1367 1368 1369 1370 1371 1372
  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;
  }

1373
  TEST(n, 21);
Pekka Pessi's avatar
Pekka Pessi committed
1374

1375 1376
  /* Add a new part to multipart */
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew);
Pekka Pessi's avatar
Pekka Pessi committed
1377 1378 1379 1380 1381 1382
  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);

1383
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1384
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1385
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1386 1387 1388 1389 1390 1391 1392
  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_1(h != removed);
  }

1393
  TEST(n, 21 + 4);
Pekka Pessi's avatar
Pekka Pessi committed
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404

#define remove(h) \
  (((*((msg_header_t*)(h))->sh_prev = ((msg_header_t*)(h))->sh_succ) ? \
   (((msg_header_t*)(h))->sh_succ->sh_prev = ((msg_header_t*)(h))->sh_prev) \
   : NULL), \
   ((msg_header_t*)(h))->sh_succ = NULL, \
   ((msg_header_t*)(h))->sh_prev = NULL)

  remove(mp0->mp_separator);
  remove(mp0->mp_next->mp_payload);
  remove(mp0->mp_next->mp_next->mp_content_type);
1405
  remove(mp0->mp_next->mp_next->mp_next->mp_next->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1406 1407 1408 1409 1410

  TEST_1(!msg_chain_errors((msg_header_t *)mp0));

  head = NULL;
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1411
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1412 1413 1414 1415 1416
  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;
    if (h_succ == NULL)
1417
      TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1418 1419 1420
    TEST_1(h != removed);
  }

1421
  TEST(n, 21 + 4);
Pekka Pessi's avatar
Pekka Pessi committed
1422 1423

  /* Add an recursive multipart */
1424
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew);
Pekka Pessi's avatar
Pekka Pessi committed
1425 1426 1427 1428 1429 1430
  mpX->mp_multipart = mpnew;
  mpnew->mp_content_type = msg_content_type_make(home, "text/plain");
  TEST_1(mpnew->mp_content_type);
  TEST(msg_multipart_complete(msg_home(msg), tst->msg_content_type, mp0), 0);
  TEST_1(mpnew->mp_close_delim);

1431
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1432
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1433
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1434 1435 1436 1437 1438 1439

  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;

1440
  TEST(n, 21 + 9);
Pekka Pessi's avatar
Pekka Pessi committed
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461

  su_home_check(home);
  su_home_zap(home);

  END();
}

/** Test MIME encoding */
int test_mime2(void)
{
  msg_t *msg;
  su_home_t *home;
  int n, m, len;
  msg_test_t *tst;
  msg_header_t *h;
  msg_accept_charset_t *aa;
  msg_multipart_t *mp;
  msg_content_type_t *c;
  msg_payload_t *pl;
  char const *end;

1462
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
    "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-MD5: LLO7gLaGqGt4BI6HouiWng==" CRLF
    CRLF
1480
    "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1481
    CRLF			/* 1 */
1482
    "--LaGqGt4BI6Ho" "  " CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1483 1484 1485
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
1486
    "--LaGqGt4BI6Ho" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1487 1488 1489 1490 1491 1492 1493 1494 1495
    "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 */
1496
#define BODY3 "<html><body>part 3</body></html>" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1497 1498
    BODY3			/* 14 */
    CRLF			/* 15 */
1499
    "--LaGqGt4BI6Ho--" CRLF;
Pekka Pessi's avatar
Pekka Pessi committed
1500 1501 1502 1503 1504 1505 1506 1507 1508

  char const part1[] = "This is text\n";
  char const part2[] = "<html><body>This is html</body></html>";
  char const part3[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  BEGIN();

  msg = read_msg(s);
  home = msg_home(msg);
1509
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520

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

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

  for (h = (msg_header_t *)tst->msg_request; h; h = h->sh_succ) {
    TEST_1(h->sh_data);
    if (h->sh_succ)
1521
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
  }

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

  TEST_1(c = tst->msg_content_type);
  TEST_1(tst->msg_payload);

  {
    su_home_t h0[1] = { SU_HOME_INIT(h0) };

    pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl);
1534

Pekka Pessi's avatar
Pekka Pessi committed
1535 1536
    mp = msg_multipart_parse(home, c, pl); TEST_1(mp);

1537
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1538 1539
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1540

Pekka Pessi's avatar
Pekka Pessi committed
1541
    n = msg_multipart_prepare(msg, mp, 0);
1542

Pekka Pessi's avatar
Pekka Pessi committed
1543 1544 1545
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
1546

Pekka Pessi's avatar
Pekka Pessi committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
    TEST_1(mp = msg_multipart_dup(h0, mp));

    su_home_check(h0);
    su_home_deinit(h0);
  }

  /* Test parsing without explicit boundary */
  {
    su_home_t h0[1] = { SU_HOME_INIT(h0) };

    pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl);
1558

Pekka Pessi's avatar
Pekka Pessi committed
1559 1560 1561
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);

1562
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1563 1564
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1565

Pekka Pessi's avatar
Pekka Pessi committed
1566
    n = msg_multipart_prepare(msg, mp, 0);
1567

Pekka Pessi's avatar
Pekka Pessi committed
1568 1569 1570
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
1571

Pekka Pessi's avatar
Pekka Pessi committed
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
    TEST_1(mp = msg_multipart_dup(h0, mp));

    su_home_check(h0);
    su_home_deinit(h0);
  }

  /* Test parsing without preamble and explicit boundary */
  {
    su_home_t h0[1] = { SU_HOME_INIT(h0) };

    pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl);
1583

Pekka Pessi's avatar
Pekka Pessi committed
1584 1585 1586 1587 1588 1589 1590
    n = strstr(pl->pl_data, "--LaGqGt4BI6Ho") - (char *)pl->pl_data;
    pl->pl_data = n + (char *)pl->pl_data; pl->pl_len -= n;

    len = pl->pl_len;

    mp = msg_multipart_parse(h0, NULL, pl); TEST_1(mp);

1591
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1592 1593
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1594

Pekka Pessi's avatar
Pekka Pessi committed
1595 1596 1597
    n = msg_multipart_prepare(msg, mp, 0);

    TEST(len, n);
1598

Pekka Pessi's avatar
Pekka Pessi committed
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    TEST_1(mp = msg_multipart_dup(h0, mp));

    su_home_check(h0);
    su_home_deinit(h0);
  }

  /* Test parsing without CR's */
  {
    su_home_t h0[1] = { SU_HOME_INIT(h0) };
    char *b;

    pl = msg_payload_dup(h0, tst->msg_payload); TEST_1(pl);
1611

Pekka Pessi's avatar
Pekka Pessi committed
1612 1613 1614 1615 1616 1617 1618
    /* Remove CRs */
    b = pl->pl_data, len = pl->pl_len;
    for (n = m = 0; n < len; n++) {
      if ((b[m] = b[n]) != '\r')
	m++;
    }
    pl->pl_len = m;
1619

Pekka Pessi's avatar
Pekka Pessi committed
1620 1621
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);
1622 1623

    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1624 1625
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1626

Pekka Pessi's avatar
Pekka Pessi committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
    n = msg_multipart_prepare(msg, mp, 0);
    TEST_1(n > 0);

    TEST_1(mp = msg_multipart_dup(h0, mp));

    su_home_check(h0);
    su_home_deinit(h0);
  }

  /* Create a new multipart from three parts */
  TEST_1(c = msg_content_type_make(home, "multipart/related"));

  TEST_1(mp = msg_multipart_create(home, "text/plain", part1, strlen(part1)));
1640
  TEST_1(mp->mp_next =
Pekka Pessi's avatar
Pekka Pessi committed
1641
	 msg_multipart_create(home, "text/html", part2, strlen(part2)));