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();
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 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\"";
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";
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);
  }
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";
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);
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) };
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";
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;
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) };
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,  ,   ";
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== ,,";
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;
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;
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
}

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.
 */
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;
}

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;
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);
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"));
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"));
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"));
608
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
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"));
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 */
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;
625
    MSG_PARAM_MATCH(vs, foo, "foo");
Pekka Pessi's avatar
Pekka Pessi committed
626 627
    TEST_S(vs, "bar");
    vs = NULL;
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);

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);
650
  tst = msg_test_public(msg);
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"));
665
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)bzip2, (void *)lzss), 0);
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");
673
    TEST_M(en->aa_common->h_data,
674 675 676
	   "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF,
	   en->aa_common->h_len);

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);
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);
683 684 685

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

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

692
    TEST_P(en->aa_common->h_succ, fi);
693
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
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);
702

703
    TEST_1(sv = msg_accept_language_make(msg_home(msg),
704 705 706
					 "sv;q=0.6,sv_FI;q=0.7"));
    TEST_1(sv_fi = sv->aa_next);

707
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)fi, (void *)sv), 0);
708

709
    TEST_P(en->aa_next, sv);
710
    TEST_P(sv->aa_next->aa_next, de);
711
    TEST_P(de->aa_next, NULL);
712

713
    TEST_P(en->aa_common->h_succ, sv);
714
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
715
    TEST_P(sv->aa_common->h_succ, sv_fi);
716
    TEST_P(sv->aa_common->h_prev, &en->aa_common->h_succ);
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);
721

722
    TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
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
730
		 "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
731 732
		 "extra stuff" CRLF);
  TEST_1(orig);
733
  otst = msg_test_public(orig);
Pekka Pessi's avatar
Pekka Pessi committed
734 735
  TEST_1(otst);

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

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

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

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

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

754
  TEST_1(language =
Pekka Pessi's avatar
Pekka Pessi committed
755
	 msg_content_language_make(home, "se-FI, fi-FI, sv-FI"));
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);
761

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);
792
    TEST_P((char *)se->aa_common->h_data + se->aa_common->h_len,
793
	   se->aa_next->aa_common->h_data);
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;
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++)
807
      strcpy(body + i * 66,
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);
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
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();

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

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

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

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

983 984
  TEST(msg_header_add_make(msg, (msg_pub_t *)tst,
			   msg_content_language_class,
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"));
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"));
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"));
1000
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
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"));
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 */
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 */
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));
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));
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;
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"));
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];

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

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
1178
    "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1179
    CRLF			/* 1 */
1180
    "--LaGqGt4BI6Ho" "  " CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1181 1182 1183
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
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 */
1194
#define BODY3 "<html><body>part 3</body></html>" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1195 1196
    BODY3			/* 14 */
    CRLF			/* 15 */
1197
    "--LaGqGt4BI6Ho--" CRLF;
Pekka Pessi's avatar
Pekka Pessi committed
1198 1199 1200 1201 1202

  BEGIN();

  msg = read_msg(s);
  home = msg_home(msg);
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);
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");
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);
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);

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

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

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
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  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);
  }

  TEST(n, 19);

#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);
  remove(mp0->mp_next->mp_next->mp_next->mp_close_delim);

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

  head = NULL;
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1345
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1346 1347 1348 1349 1350
  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)
1351
      TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1352 1353 1354 1355 1356 1357
    TEST_1(h != removed);
  }

  TEST(n, 19);

  /* Add an recursive multipart */
1358
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew);
Pekka Pessi's avatar
Pekka Pessi committed
1359 1360 1361 1362 1363 1364
  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);

1365
  head = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1366
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1367
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

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

  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;

1396
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    "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
1414
    "test" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1415
    CRLF			/* 1 */
1416
    "--LaGqGt4BI6Ho" "  " CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1417 1418 1419
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
1420
    "--LaGqGt4BI6Ho" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1421 1422 1423 1424 1425 1426 1427 1428 1429
    "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 */
1430
#define BODY3 "<html><body>part 3</body></html>" CRLF
Pekka Pessi's avatar
Pekka Pessi committed
1431 1432
    BODY3			/* 14 */
    CRLF			/* 15 */
1433
    "--LaGqGt4BI6Ho--" CRLF;
Pekka Pessi's avatar
Pekka Pessi committed
1434 1435 1436 1437 1438 1439 1440 1441 1442

  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);
1443
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454

  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)
1455
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
  }

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

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

1471
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1472 1473
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1474

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

Pekka Pessi's avatar
Pekka Pessi committed
1477 1478 1479
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
1480

Pekka Pessi's avatar
Pekka Pessi committed
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    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);
1492

Pekka Pessi's avatar
Pekka Pessi committed
1493 1494 1495
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);

1496
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1497 1498
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1499

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

Pekka Pessi's avatar
Pekka Pessi committed
1502 1503 1504
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
1505

Pekka Pessi's avatar
Pekka Pessi committed
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    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);
1517

Pekka Pessi's avatar
Pekka Pessi committed
1518 1519 1520 1521 1522 1523 1524
    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);

1525
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++)
Pekka Pessi's avatar
Pekka Pessi committed
1526 1527
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
1528

Pekka Pessi's avatar
Pekka Pessi committed
1529 1530 1531
    n = msg_multipart_prepare(msg, mp, 0);

    TEST(len, n);
1532

Pekka Pessi's avatar
Pekka Pessi committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
    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);
1545

Pekka Pessi's avatar
Pekka Pessi committed
1546 1547 1548 1549 1550 1551 1552
    /* 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;
1553

Pekka Pessi's avatar
Pekka Pessi committed
1554 1555
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);
1556 1557

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

Pekka Pessi's avatar
Pekka Pessi committed
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    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)));
1574
  TEST_1(mp->mp_next =
Pekka Pessi's avatar
Pekka Pessi committed
1575
	 msg_multipart_create(home, "text/html", part2, strlen(part2)));
1576 1577
  TEST_1(mp->mp_next->mp_next =
	 msg_multipart_create(home, "application/octet-stream",
Pekka Pessi's avatar
Pekka Pessi committed
1578 1579 1580 1581 1582
			      part3, sizeof part3));

  TEST(msg_multipart_complete(home, c, mp), 0);

  h = NULL;
1583
  TEST_P(msg_multipart_serialize(&h, mp), mp->mp_next->mp_next->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607

  TEST_1(msg_multipart_prepare(msg, mp, 0));

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

  su_home_check(home);
  su_home_zap(home);

  END();
}


/* Test serialization */
int test_serialize(void)
{
  msg_t *msg;
  su_home_t *home;
  msg_test_t *tst;
  msg_mime_version_t *mime;
  msg_separator_t *sep;
  msg_payload_t *pl;
  msg_accept_encoding_t *aen;
  msg_accept_language_t *ala;

1608
  char const s[] =
Pekka Pessi's avatar
Pekka Pessi committed
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    "GET /a-life HTTP/1.1" CRLF
    "Accept-Language: fi" CRLF
    "Accept-Encoding: z0" CRLF
    "Accept-Language: se, de" CRLF
    "Accept-Encoding: z1, z2" CRLF
    "Accept-Language: en, sv" CRLF
    "Accept-Encoding: z3, z4" CRLF
    "Content-Length: 6" CRLF
    CRLF
    "test" CRLF;

  BEGIN();

  msg = read_msg(s);

  TEST_1(msg); home = msg_home(msg);
  TEST_1(tst = msg_test_public(msg));
  TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0);

  TEST_1(ala = tst->msg_accept_language->aa_next->aa_next);
1629
  TEST(msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)ala), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1630 1631
  TEST_S(ala->aa_value, "de");

1632
  TEST_1(ala = tst->msg_accept_language);
Pekka Pessi's avatar
Pekka Pessi committed
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
  TEST_1(ala = ala->aa_next); TEST_S(ala->aa_value, "se");
  /* Make sure that cached encoding of se is reset */
  TEST_1(ala->aa_common->h_data == NULL);
  TEST_1(ala->aa_common->h_len == 0);
  TEST_1(ala = ala->aa_next); TEST_S(ala->aa_value, "en");
  /* Make sure that cached encoding of en is kept intact */
  TEST_1(ala->aa_common->h_data != NULL);
  TEST_1(ala->aa_common->h_len != 0);

  TEST_1(aen = tst->msg_accept_encoding->aa_next->aa_next);
1643
  TEST(msg_header_remove_all(msg, (msg_pub_t *)tst, (msg_header_t *)aen), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1644

1645
  TEST_1(aen = tst->msg_accept_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
1646 1647 1648 1649
  TEST_1(aen = aen->aa_next); TEST_S(aen->aa_value, "z1");
  /* Make sure that cached encoding of z1 is reset */
  TEST_1(aen->aa_common->h_data == NULL);
  TEST_1(aen->aa_common->h_len == 0);
1650
  TEST_1(aen->aa_next == NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1651 1652 1653 1654 1655 1656 1657 1658

  TEST_1(aen->aa_common->h_succ == (void *)ala);
  TEST_1(ala->aa_next->aa_common);
  TEST_1(ala->aa_next->aa_common->h_succ == (void *)tst->msg_content_length);

  TEST_1(mime = msg_mime_version_make(home, "1.0"));
  tst->msg_mime_version = mime;

1659
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1660
  TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0);
1661 1662
  TEST_P(tst->msg_content_length->l_common->h_succ, mime);
  TEST_P(mime->g_common->h_succ, tst->msg_separator);
Pekka Pessi's avatar
Pekka Pessi committed
1663

1664
  msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)tst->msg_separator);
Pekka Pessi's avatar
Pekka Pessi committed
1665 1666
  TEST_1(sep = msg_separator_make(home, CRLF));
  tst->msg_separator = sep;
1667
  TEST(msg_serialize