test_msg.c 46 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 79 80 81 82 83

void usage(void)
{
  fprintf(stderr, "usage: %s [-v]\n", name);
}

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);
84 85
  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
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  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];

    memset(x, 0, sizeof (x));

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

  END();
  return 0;
}

144 145 146 147
static int addr_test(void)
{
  BEGIN();
  
Pekka Pessi's avatar
Pekka Pessi committed
148 149
  /* It *will* fail. */
  /* TEST(sizeof(socklen_t), sizeof(msg_addrlen(NULL))); */
150 151 152 153

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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);
181
    TEST_P(s, end);
Pekka Pessi's avatar
Pekka Pessi committed
182 183 184 185
    TEST_S(params[0], "uffe");
    TEST_S(params[1], "duffe=\042entten\042");
    TEST_S(params[2], "doo=[::1]");
    TEST_1(params[3] == NULL);
186 187
    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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    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;
    
    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\"";
    
    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";
    
    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);
  }
    
  {
    /* 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";
    
    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);
  
    su_home_deinit(home);
  }

  {
    /* Test that long lists are handled correctly */
    su_home_t home[1] = { SU_HOME_INIT(home) };
    
    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";
    char list4[] = "one, two, three, four, five, six, seven, eight, nine";    

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

463 464 465 466 467 468 469 470
  {
    /* 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"), "");
471
    TEST_S(msg_header_find_item(ce->k_common, "zz"), "zz");
472
    TEST_P(msg_header_find_param(ce->k_common, "k"), NULL);
473 474 475 476
    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);
477
    TEST_P(msg_header_find_param(ce->k_common, "zip"), NULL);
478 479 480
    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");
481
    TEST(msg_header_replace_param(home, ce->k_common, "zip=zup"), 1);
482 483 484 485 486 487
    TEST_S(msg_header_find_param(ce->k_common, "zip"), "zup");

    su_home_deinit(home);
  }


Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 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 517 518 519 520 521 522
  END();
}

int hash_test(void)
{
  int i, j, hash = 0;
  msg_mclass_t const *mc = msg_test_mclass;
  msg_hclass_t *hc;
  
  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
523
  return msg_make(msg_test_mclass, MSG_DO_EXTRACT_COPY, buffer, -1);
Pekka Pessi's avatar
Pekka Pessi committed
524 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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
}

/**Check if header chain contains any loops. 
 *
 * @return
 * Return 0 if no loop, -1 otherwise.
 */
static 
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;
}

/** Check header chain consistency. 
 *
 * @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;
  
  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;
  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);
594
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
595 596 597 598 599

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

600
  TEST_P(tst->msg_error, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
601 602 603 604

  TEST_1(tst->msg_accept_language);

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

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

  TEST_1(separator = msg_separator_make(home, "\r\n"));
613
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
614 615
  TEST_P(tst->msg_separator, separator); 
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
616 617 618

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
619
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
620
  /* It is appended */
621 622
  TEST_P(tst->msg_payload->pl_next, payload); 
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
623 624 625 626 627 628 629 630 631 632 633

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

    vs = NULL;
    MSG_PARAM_MATCH(vs, foo, "foo"); 
    TEST_S(vs, "bar");
    vs = NULL;
    MSG_PARAM_MATCH(vs, foo, "fo"); 
634
    TEST_P(vs, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
635 636 637 638 639 640 641 642 643 644 645 646
    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);

647 648 649 650 651 652 653 654
  /* 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);
655
  tst = msg_test_public(msg);
656 657 658 659 660 661 662 663 664 665 666 667 668 669
  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"));
670
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)bzip2, (void *)lzss), 0);
671 672 673 674 675 676 677 678 679 680 681
    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");
    TEST_M(en->aa_common->h_data, 
	   "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF,
	   en->aa_common->h_len);

682 683
    TEST_P((char *)en->aa_common->h_data + en->aa_common->h_len, 
	   fi->aa_common->h_data);
684 685 686

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

687
    TEST(msg_header_replace(msg, (msg_pub_t *)tst, (void *)se, (void *)de), 0);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    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);

    TEST_P(en->aa_common->h_succ, fi); 
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
    TEST_P(fi->aa_common->h_succ, de); 
    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);
703 704 705 706 707

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

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

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

714 715 716 717 718 719 720 721
    TEST_P(en->aa_common->h_succ, sv); 
    TEST_P(en->aa_common->h_prev, &deflate->aa_common->h_succ);
    TEST_P(sv->aa_common->h_succ, sv_fi); 
    TEST_P(sv->aa_common->h_prev, &en->aa_common->h_succ);
    TEST_P(sv_fi->aa_common->h_succ, de); 
    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);
722

723
    TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
724 725
  }

Pekka Pessi's avatar
Pekka Pessi committed
726 727 728 729 730 731 732 733 734
  /* Bug #2429 */
  orig = read_msg("GET a-life HTTP/1.1" CRLF
		 "Accept-Language: en;q=0.8, fi, se ; q = 0.6" CRLF
		 "Foo: bar" CRLF
		 "Content-Length: 6" CRLF
		 CRLF
		 "test" CRLF 
		 "extra stuff" CRLF);
  TEST_1(orig);
735
  otst = msg_test_public(orig);
Pekka Pessi's avatar
Pekka Pessi committed
736 737
  TEST_1(otst);

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

  home = msg_home(msg);

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

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

  TEST_1(location = 
	 msg_content_location_make(home, "http://localhost:8080/wife"));

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

753
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
754 755 756 757
  TEST_1(msg_prepare(msg) > 0);

  TEST_1(language = 
	 msg_content_language_make(home, "se-FI, fi-FI, sv-FI"));
758
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (void *)language), 0);
Pekka Pessi's avatar
Pekka Pessi committed
759
  
760
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
761 762 763 764 765 766
  TEST_1(msg_prepare(msg) > 0);

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

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

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

775
    TEST_SIZE(location->g_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
776 777 778 779 780 781
    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";
782
    TEST_SIZE(language->k_common->h_len, strlen(encoded));
Pekka Pessi's avatar
Pekka Pessi committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    TEST_M(language->k_common->h_data, encoded, language->k_common->h_len);
  }

  msg_destroy(msg);

  END();
}

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

  BEGIN();

799
  TEST_1(home = su_home_new(sizeof *home));
Pekka Pessi's avatar
Pekka Pessi committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

  TEST_1((w = msg_warning_make(home, 
			       "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\"");

820
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

  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);
845
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

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

885
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
  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);
902
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
903 904 905 906 907

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

908
  TEST_SIZE(msg_iovec(msg, NULL, 0), 1);
Pekka Pessi's avatar
Pekka Pessi committed
909 910 911 912 913 914 915 916 917 918 919 920 921

  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");
922
  TEST_P(la->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
923 924 925 926
  TEST_1(la->k_next);
  TEST_1(la->k_next->k_common->h_data);
  TEST_1(la->k_next->k_items == NULL);

927 928 929
  TEST(msg_header_add_make(msg, (msg_pub_t *)tst, 
			   msg_content_language_class, 
			   "en-gb"), 0);
930 931
  TEST_P(la, tst->msg_content_language);
  TEST_P(la->k_common->h_data, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
932
  TEST_S(la->k_items[2], "en-gb");
933
  TEST_P(la->k_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
934 935

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

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

  TEST_1(separator = msg_separator_make(home, "\r\n"));
944
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)separator), 0);
945 946
  TEST_P(tst->msg_separator, separator); 
  TEST_P(separator->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
947 948 949

  /* Try to add a new payload */
  TEST_1(payload = msg_payload_make(home, "foofaa\r\n"));
950
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)payload), 0);
Pekka Pessi's avatar
Pekka Pessi committed
951
  /* The new payload should be appended */
952 953
  TEST_P(tst->msg_payload->pl_next, payload); 
  TEST_P(tst->msg_payload->pl_common->h_succ, payload);
Pekka Pessi's avatar
Pekka Pessi committed
954 955 956 957 958

  /* Try to add a new header */
  TEST_1(l = msg_content_length_create(home, 
				       tst->msg_payload->pl_len + 
				       payload->pl_len));
959
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)l), 0);
Pekka Pessi's avatar
Pekka Pessi committed
960
  /* The new header should be last before separator */
961
  TEST_P(l->l_common->h_succ, separator);
Pekka Pessi's avatar
Pekka Pessi committed
962 963 964 965 966 967

  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));
968
  TEST(msg_header_insert(msg, (msg_pub_t *)tst, (msg_header_t *)foo), 0);
969
  TEST_P(tst->msg_unknown->un_next, foo);
Pekka Pessi's avatar
Pekka Pessi committed
970 971 972

  TEST_1(k = msg_content_encoding_make(home, "gzip, compress"));
  k0 = tst->msg_content_encoding;
973
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
974
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
975 976 977
  TEST_1(k0->k_items);
  TEST_S(k0->k_items[0], "gzip");
  TEST_S(k0->k_items[1], "compress");
978
  TEST_P(k0->k_items[2], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
979 980

  TEST_1(k = msg_content_encoding_make(home, "gzip, deflate, compress"));
981
  TEST(msg_header_add_dup(msg, (msg_pub_t *)tst, (msg_header_t *)k), 0);
982
  TEST_P(k0, tst->msg_content_encoding);
Pekka Pessi's avatar
Pekka Pessi committed
983 984 985 986
  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");
987
  TEST_P(k0->k_items[3], NULL);
Pekka Pessi's avatar
Pekka Pessi committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

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

  char const s[] = 
    "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));

1019
  TEST_SIZE(msg_iovec(msg0, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1020 1021 1022 1023 1024 1025 1026 1027

  TEST_1(msg = msg_copy(msg0));
  TEST_1(copy = msg_test_public(msg));
  TEST_1(copy->msg_request);
  TEST_1(tst0->msg_request);
  TEST_S(copy->msg_request->rq_url->url_path, 
	 tst0->msg_request->rq_url->url_path);
  TEST_S(copy->msg_request->rq_url->url_path, "a-life");
1028 1029
  TEST_P(copy->msg_request->rq_url->url_path, 
	 tst0->msg_request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  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);
  TEST_S(dup->msg_request->rq_url->url_path, 
	 tst0->msg_request->rq_url->url_path);
  TEST_S(dup->msg_request->rq_url->url_path, "a-life");
  TEST_1(dup->msg_request->rq_url->url_path != 
	 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);
1058
  TEST_P(tst0->msg_request->rq_url->url_path, request->rq_url->url_path);
Pekka Pessi's avatar
Pekka Pessi committed
1059

1060
  TEST_SIZE(msg_iovec(msg, iovec, 8), 1);
Pekka Pessi's avatar
Pekka Pessi committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 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

  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;

  char const s[] = 
    "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
    "test" CRLF			
    CRLF			/* 1 */
    "--LaGqGt4BI6Ho" "  " CRLF	
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
    "--LaGqGt4BI6Ho" CRLF	
    "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 */
#define BODY3 "<html><body>part 3</body></html>" CRLF 
    BODY3			/* 14 */
    CRLF			/* 15 */
    "--LaGqGt4BI6Ho--" CRLF;	

  BEGIN();

  msg = read_msg(s);
  home = msg_home(msg);
1147
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

  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)
1174
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
  }

  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);
  TEST_M(mp->mp_common->h_data, CRLF "--" "LaGqGt4BI6Ho" "  " CRLF, 
	 mp->mp_common->h_len);

  TEST_1(pl = mp->mp_payload); TEST_1(pl->pl_data);
1191
  TEST_SIZE(strlen("part 1" CRLF), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
  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");
  TEST_1(c->c_params);   TEST_1(c->c_params[0]); 
  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);
1210
  TEST_SIZE(strlen("part 2"), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1211 1212 1213 1214 1215 1216 1217 1218
  TEST(memcmp(pl->pl_data, "part 2", pl->pl_len), 0);
  
  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);
1219
  TEST_SIZE(strlen(BODY3), pl->pl_len);
Pekka Pessi's avatar
Pekka Pessi committed
1220 1221 1222 1223 1224 1225 1226 1227 1228
  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));
1229
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  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);

  head = NULL; 
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1244
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  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);

  /* Add a new part to multipart */   
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew); 
  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);

  head = NULL; 
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1263
  TEST_P((void *)h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
  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));
1289
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1290 1291 1292 1293 1294
  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)
1295
      TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    TEST_1(h != removed);
  }

  TEST(n, 19);

  /* Add an recursive multipart */
  mpnew = su_zalloc(home, sizeof(*mpnew)); TEST_1(mpnew); 
  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);

  head = NULL; 
  TEST_1(h = msg_multipart_serialize(&head, mp0));
1311
  TEST_P(h, mpX->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1312 1313 1314 1315 1316 1317 1318 1319 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 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386

  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;

  char const s[] = 
    "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
    "test" CRLF			
    CRLF			/* 1 */
    "--LaGqGt4BI6Ho" "  " CRLF	
    CRLF			/* 2 */
    "part 1" CRLF		/* 3 */
    CRLF			/* 4 */
    "--LaGqGt4BI6Ho" CRLF	
    "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 */
#define BODY3 "<html><body>part 3</body></html>" CRLF 
    BODY3			/* 14 */
    CRLF			/* 15 */
    "--LaGqGt4BI6Ho--" CRLF;	

  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);
1387
  tst = msg_test_public(msg);
Pekka Pessi's avatar
Pekka Pessi committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

  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)
1399
      TEST_P((char*)h->sh_data + h->sh_len, h->sh_succ->sh_data);
Pekka Pessi's avatar
Pekka Pessi committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
  }

  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);
  
    mp = msg_multipart_parse(home, c, pl); TEST_1(mp);

    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) 
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
    
    n = msg_multipart_prepare(msg, mp, 0);
    
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
    
    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);
  
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);

    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) 
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
    
    n = msg_multipart_prepare(msg, mp, 0);
    
    TEST_1(end = strstr(s, "--LaGqGt4BI6Ho  "));
    len = strlen(end);
    TEST(len, n);
    
    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);
  
    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);

    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) 
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
    
    n = msg_multipart_prepare(msg, mp, 0);

    TEST(len, n);
    
    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);
  
    /* 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;
    
    mp = msg_multipart_parse(h0, NULL, pl);
    TEST_1(mp);
    
    for (n = 0, h = (msg_header_t *)mp; h; h = h->sh_succ, n++) 
      h->sh_data = NULL, h->sh_len = 0;
    TEST(n, 15);
    
    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)));
  TEST_1(mp->mp_next = 
	 msg_multipart_create(home, "text/html", part2, strlen(part2)));
  TEST_1(mp->mp_next->mp_next = 
	 msg_multipart_create(home, "application/octet-stream", 
			      part3, sizeof part3));

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

  h = NULL;
1527
  TEST_P(msg_multipart_serialize(&h, mp), mp->mp_next->mp_next->mp_close_delim);
Pekka Pessi's avatar
Pekka Pessi committed
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572

  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;

  char const s[] = 
    "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);
1573
  TEST(msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)ala), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
  TEST_S(ala->aa_value, "de");

  TEST_1(ala = tst->msg_accept_language); 
  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);
1587
  TEST(msg_header_remove_all(msg, (msg_pub_t *)tst, (msg_header_t *)aen), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

  TEST_1(aen = tst->msg_accept_encoding); 
  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);
  TEST_1(aen->aa_next == NULL); 

  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;

1603
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1604
  TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0);
1605 1606
  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
1607

1608
  msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)tst->msg_separator);
Pekka Pessi's avatar
Pekka Pessi committed
1609 1610
  TEST_1(sep = msg_separator_make(home, CRLF));
  tst->msg_separator = sep;
1611
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1612
  TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0);
1613 1614
  TEST_P(mime->g_common->h_succ, sep);
  TEST_P(sep->sep_common->h_succ, tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
1615

1616
  msg_header_remove(msg, (msg_pub_t *)tst, (msg_header_t *)tst->msg_payload);
Pekka Pessi's avatar
Pekka Pessi committed
1617 1618 1619
  TEST_1(pl = msg_payload_make(home, "foobar" CRLF));
  pl->pl_next = tst->msg_payload;
  tst->msg_payload = pl;
1620
  TEST(msg_serialize(msg, (msg_pub_t *)tst), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1621
  TEST(msg_chain_errors((msg_header_t *)tst->msg_request), 0);
1622 1623 1624
  TEST_P(mime->g_common->h_succ, sep);
  TEST_P(sep->sep_common->h_succ, pl);
  TEST_P(pl->pl_common->h_succ, pl->pl_next);
Pekka Pessi's avatar
Pekka Pessi committed
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642

  msg_destroy(msg);

  END();
}

static int random_test(void)
{
  struct { uint64_t low, mid, hi; } seed = { 0, 0, 0 };
  uint8_t zeros[24] = { 0 };
  uint8_t ones[24];

  char token[33];

  BEGIN();

  memset(ones, 255, sizeof ones);

1643
  TEST_SIZE(msg_random_token(token, 32, (void *)&seed, sizeof(seed)), 32);
Pekka Pessi's avatar
Pekka Pessi committed
1644 1645
  TEST_S(token, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

1646
  TEST_SIZE(msg_random_token(token, 32, zeros, 4), 7);
Pekka Pessi's avatar
Pekka Pessi committed
1647
  TEST_S(token, "aaaaaaa");
1648
  TEST_SIZE(msg_random_token(token, 32, ones, 4), 7);
Pekka Pessi's avatar
Pekka Pessi committed
1649 1650
  /* Last char may vary.. */
  token[6] = 0;  TEST_S(token, "999999");
1651
  TEST_SIZE(msg_random_token(token, 32, zeros, 8), 13);
Pekka Pessi's avatar
Pekka Pessi committed
1652
  TEST_S(token, "aaaaaaaaaaaaa");
1653
  TEST_SIZE(msg_random_token(token, 32, zeros, 12), 20);
Pekka Pessi's avatar
Pekka Pessi committed
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
  TEST_S(token, "aaaaaaaaaaaaaaaaaaaa");
  
  END();
}

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      test_flags |= tst_verbatim;
    else
      usage();
  }

  retval |= msg_time_test(); fflush(stdout);
1672
  retval |= addr_test(); fflush(stdout);
Pekka Pessi's avatar
Pekka Pessi committed
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
  retval |= hash_test(); fflush(stdout);
  retval |= random_test(); fflush(stdout);
  retval |= test_header_parsing(); fflush(stdout);
  retval |= test_msg_parsing(); fflush(stdout);
  retval |= test_warning(); fflush(stdout);
  retval |= test_msg_error(); fflush(stdout);
  retval |= test_mclass(); fflush(stdout);
  retval |= test_copy(); fflush(stdout);
  retval |= test_mime(); fflush(stdout);
  retval |= test_mime2(); fflush(stdout);
  retval |= test_serialize(); fflush(stdout);

  return retval;
}