torture_sip.c 99 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * 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
 *
 */

/**@ingroup sip_test @internal
 *
 * @CFILE torture_sip.c  
 *
 * Unit-testing functions for SIP.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Created: Tue Mar  6 18:33:42 2001 ppessi
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stddef.h>
41
#include <stdlib.h>
Pekka Pessi's avatar
Pekka Pessi committed
42

43 44 45 46
/* Avoid casting sip_t to msg_pub_t and sip_header_t to msg_header_t */
#define MSG_PUB_T       struct sip_s
#define MSG_HDR_T       union sip_header_u

47
#include <sofia-sip/su_types.h>
Pekka Pessi's avatar
Pekka Pessi committed
48

49 50 51
#include <sofia-sip/su_tag.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_io.h>
Pekka Pessi's avatar
Pekka Pessi committed
52

53 54 55
#include "sofia-sip/sip_parser.h"
#include <sofia-sip/sip_util.h>
#include <sofia-sip/sip_status.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57 58 59 60 61
#include <sofia-sip/sip_tag.h>
#include <sofia-sip/url_tag.h>
#include <sofia-sip/msg_addr.h>
#include <sofia-sip/msg_mclass.h>
#include <sofia-sip/msg_mclass_hash.h>
Pekka Pessi's avatar
Pekka Pessi committed
62

63 64
#include <sofia-sip/sip_extra.h>

Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68
int tstflags;

#define TSTFLAGS tstflags

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

char const *name = "torture_sip.c";

msg_mclass_t *test_mclass = NULL;

static msg_t *read_message(int flags, char const string[]);

77 78 79 80
int test_url_headers(void)
{
  BEGIN();
  su_home_t *home;
81 82 83 84 85 86
  char *s, *d;
  tagi_t *t;
  url_t *url;
  sip_from_t const *f;
  sip_accept_t const *ac;
  sip_payload_t const *body;
87 88 89

  TEST_1(home = su_home_new(sizeof *home));

90 91 92 93 94 95 96 97
  s = sip_headers_as_url_query
    (home,
     SIPTAG_SUBJECT_STR(";"),
     TAG_END());

  TEST_1(s);
  TEST_S(s, "subject=;");

98 99 100 101 102 103 104
  s = sip_headers_as_url_query
    (home,
     SIPTAG_TO_STR("\"Joe\" <sip:joe@example.com>;tag=foofaa"),
     SIPTAG_SUBJECT_STR("foo"),
     TAG_END());

  TEST_1(s);
105
  TEST_S(s, "to=%22Joe%22%20%3Csip%3Ajoe@example.com%3E;tag%3Dfoofaa"
106 107
	 "&subject=foo");

108 109 110 111
  url = url_format(home, "sip:test@example.net?%s", s); TEST_1(url);

  TEST_S(url->url_headers, s);

112 113 114
  s = sip_headers_as_url_query
    (home,
     SIPTAG_FROM_STR("<sip:joe@example.com>"),
115
     SIPTAG_ACCEPT_STR(""),
116
     SIPTAG_PAYLOAD_STR("hello"),
117
     SIPTAG_ACCEPT_STR(""),
118 119
     TAG_END());

120 121 122 123 124 125 126 127 128 129 130 131 132 133
  TEST_S(s, "from=%3Csip%3Ajoe@example.com%3E"
	 "&accept="
	 "&body=hello"
	 "&accept=");

  d = url_query_as_header_string(home, s);
  TEST_S(d, "from:<sip:joe@example.com>\n"
	 "accept:\n"
	 "accept:\n"
	 "\n"
	 "hello");

  t = sip_url_query_as_taglist(home, s, NULL); TEST_1(t);

134 135 136 137
  TEST_P(t[0].t_tag, siptag_from);    TEST_1(f = (void *)t[0].t_value);
  TEST_P(t[1].t_tag, siptag_accept);  TEST_1(ac = (void *)t[1].t_value);
  TEST_P(t[2].t_tag, siptag_payload); TEST_1(body = (void *)t[2].t_value);
  TEST_P(t[3].t_tag, siptag_accept); 
138 139 140 141 142

  s = "xyzzy=foo";

  t = sip_url_query_as_taglist(home, s, NULL); TEST_1(t);

143
  TEST_P(t[0].t_tag, siptag_header_str);
144 145
  TEST_1(d = (void *)t[0].t_value);
  TEST_S(d, "foo");
146 147 148 149 150 151 152 153

  TEST_1(!sip_headers_as_url_query(home, SIPTAG_SEPARATOR_STR(""), TAG_END()));

  TEST_VOID(su_home_unref(home));

  END();
}

154 155 156 157 158 159
int test_manipulation(void)
{
  BEGIN();

  sip_content_length_t *l;
  sip_payload_t *pl;
160
  msg_t *msg, *msg0;
161
  sip_t *sip;
162 163

  msg0 = read_message(MSG_DO_EXTRACT_COPY, 
164 165 166 167 168 169 170 171 172 173
    "MESSAGE sip:foo@bar SIP/2.0\r\n"
    "To: Joe User <sip:foo@bar>\r\n"
    "From: \"Bar Owner\" <sip:bar@foo>;tag=foobar\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq: 8 MESSAGE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133\r\n"
    "Content-Length: 7\r\n"
    "Content-Type: text/plain\r\n"
    "\r\n"
    "Heippa!");
174 175
  TEST_1(msg0);
  TEST_1(msg = msg_copy(msg0));
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  TEST_1(sip = sip_object(msg));

  TEST_1(l = sip_content_length_make(msg_home(msg), "6"));
  TEST_1(pl = sip_payload_make(msg_home(msg), "hello!"));

  TEST_1(msg_header_replace(msg, NULL, 
			    (void *)sip->sip_content_length, 
			    (void *)l) >= 0);
  TEST_1(msg_header_replace(msg, NULL, 
			    (void *)sip->sip_payload, 
			    (void *)pl) >= 0);

  TEST(msg_serialize(msg, NULL), 0);
  TEST_1(msg_prepare(msg) > 0);

191
  msg_destroy(msg);
192
  msg_destroy(msg0);
193 194 195 196

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
int test_methods(void)
{
  int i;
  char name[32];

  BEGIN();

  for (i = 1; sip_method_names[i]; i++) {
    TEST_S(sip_method_names[i], sip_method_name(i, "foo"));
  }

  {
    char version[] = "protocol /  version  ";
    char *end = version + strlen(version);
    char *s = version;
    char const *result = NULL;

    TEST(sip_version_d(&s, &result), 0);
215
    TEST_P(s, end);
Pekka Pessi's avatar
Pekka Pessi committed
216 217 218
    TEST_S(result, "protocol/version");
  }

219 220 221 222 223 224 225 226 227 228 229
  {
    char udp[] = "SIP/ 2.0  /  udp";
    char tcp[] = "SIP / 2.0  /  tcp";
    char tls[] = "SIP / 2.0  /  tls";
    char sctp[] = "SIP / 2.0  /  scTp";
    char dtls[] = "SIP/2.0/TLS-UDP";
    char tls_sctp[] = "SIP/2.0/TLS-SCTP";
    char *s, *end;
    char const *result = NULL;

    s = udp; end = s + strlen(s);
230
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
231 232 233
    TEST_S(result, sip_transport_udp);

    s = tcp; end = s + strlen(s);
234
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
235 236 237
    TEST_S(result, sip_transport_tcp);

    s = tls; end = s + strlen(s);
238
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
239 240 241
    TEST_S(result, sip_transport_tls);

    s = sctp; end = s + strlen(s);
242
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
243
    TEST_S(result, sip_transport_sctp);
Pekka Pessi's avatar
Pekka Pessi committed
244

245
    s = dtls; end = s + strlen(s);
246
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
247 248 249
    TEST_S(result, "SIP/2.0/TLS-UDP");

    s = tls_sctp; end = s + strlen(s);
250
    TEST_SIZE(sip_transport_d(&s, &result), 0); TEST_P(s, end);
251 252
    TEST_S(result, "SIP/2.0/TLS-SCTP");
  }
Pekka Pessi's avatar
Pekka Pessi committed
253 254 255 256 257 258
  END();
}

/* Test <sip_basic.c> functions. */
int test_basic(void)
{
259
  su_home_t *home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
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

  BEGIN();

  TEST_1(home);
  
  {
    sip_request_t *rq, *rq1;

    rq = sip_request_make(home, "INVITE sip:joe@example.com SIP/2.1");
    TEST_1(rq);
    TEST(rq->rq_method, sip_method_invite);
    TEST_S(rq->rq_method_name, "INVITE");
    TEST_1(rq1 = sip_request_dup(home, rq));

    su_free(home, rq);
    su_free(home, rq1);
    
    rq = sip_request_make(home, "invite sip:joe@example.com SIP/2.0");
    TEST_1(rq);
    TEST(rq->rq_method, sip_method_unknown);
    TEST_S(rq->rq_method_name, "invite");

    TEST_1(rq1 = sip_request_dup(home, rq));

    su_free(home, rq);
    su_free(home, rq1);

    TEST_1(!sip_request_create(home, sip_method_unknown, NULL, 
			       (void *)"sip:joe@example.com", NULL));
    TEST_1(rq = sip_request_create(home, sip_method_unknown, "invite", 
				   (void *)"sip:joe@example.com", NULL));
    TEST(rq->rq_method, sip_method_unknown);
    TEST_S(rq->rq_method_name, "invite");
    su_free(home, rq);

    TEST_1(rq = sip_request_create(home, sip_method_unknown, "INVITE", 
				   (void *)"sip:joe@example.com", NULL));
    TEST(rq->rq_method, sip_method_invite);
    TEST_S(rq->rq_method_name, "INVITE");

    su_free(home, rq);

    TEST_1(rq = sip_request_create(home, sip_method_invite, "foobar", 
				   (void *)"sip:joe@example.com", NULL));
    TEST(rq->rq_method, sip_method_invite);
    TEST_S(rq->rq_method_name, "INVITE");

    su_free(home, rq);
  }

  {
    sip_status_t *st;

    TEST_1(st = sip_status_make(home, "SIP/2.0 200 OK"));
    su_free(home, st);

Pekka Pessi's avatar
Pekka Pessi committed
316 317 318
    TEST_1(st = sip_status_make(home, "SIP/2.0 200"));
    su_free(home, st);

Pekka Pessi's avatar
Pekka Pessi committed
319 320 321 322 323
    TEST_1(!sip_status_make(home, "SIP2.0 200 OK"));
    TEST_1(!sip_status_create(home, 99, NULL, "SIP/2.1"));
    TEST_1(!sip_status_create(home, 700, NULL, "SIP/2.1"));
    TEST_1(st = sip_status_create(home, 200, "Ok", "SIP/2.2"));
    su_free(home, st);
Pekka Pessi's avatar
Pekka Pessi committed
324 325 326 327 328 329 330

    TEST_1(st = sip_status_create(home, 200, NULL, "SIP/2.0"));
    su_free(home, st);
    TEST_1(st = sip_status_create(home, 200, NULL, NULL));
    su_free(home, st);
    TEST_1(st = sip_status_create(home, 699, NULL, NULL));
    su_free(home, st);
Pekka Pessi's avatar
Pekka Pessi committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  }

  {
    sip_payload_t *pl;

    TEST_1(pl = sip_payload_create(home, "foo", 3));
    su_free(home, pl);

    TEST_1(pl = sip_payload_create(home, NULL, 3));
    su_free(home, pl);
  }

  {
    sip_separator_t *sep;

    TEST_1(!sip_separator_make(home, "foo"));
    TEST_1(sep = sip_separator_create(home));
    su_free(home, sep);
  }


Pekka Pessi's avatar
Pekka Pessi committed
352 353 354 355 356
  /* Test name-addr things */
  {
    su_home_t home[1] = { SU_HOME_INIT(home) };
    char const *display;
    url_t url[1];
357
    msg_param_t const *params;
Pekka Pessi's avatar
Pekka Pessi committed
358 359 360 361 362 363 364 365
    char const *comment;
    char const na[] = "Raaka Arska <tel:+358501970>;param=1;humppa (test) ";
    char const na2[] = "tel:+358501970;param=1;humppa (test) ";
    char *s, buf[sizeof(na)], ebuf[sizeof(na) + 32];

    s = strcpy(buf, na);

    TEST_1(sip_name_addr_d(home, &s, &display, url, &params, &comment) >= 0);
366
    TEST_P(s, buf + strlen(na));
Pekka Pessi's avatar
Pekka Pessi committed
367 368 369 370 371
    TEST_1(display);
    TEST(url->url_type, url_tel);
    TEST_1(params);
    TEST_1(comment);

372 373 374
    TEST_SIZE(sip_name_addr_e(ebuf, sizeof(ebuf), 0, display, 0, url, 
			      params, comment), 
	      strlen(na) - 1);
Pekka Pessi's avatar
Pekka Pessi committed
375 376 377 378 379 380
    TEST_1(strncmp(na, ebuf, strlen(na) - 1) == 0);

    s = strcpy(buf, na2);

    TEST_1(sip_name_addr_d(home, &s, &display, url, &params, &comment) >= 0);
    TEST_S(s, "");
381
    TEST_P(s, buf + strlen(na2));
Pekka Pessi's avatar
Pekka Pessi committed
382 383 384 385 386 387 388 389
    TEST_1(!display);
    TEST(url->url_type, url_tel);
    TEST_1(params);
    TEST_1(comment);

    su_home_deinit(home);
  }

Pekka Pessi's avatar
Pekka Pessi committed
390
  {
391
    sip_from_t *f; sip_to_t *t, *t2;
Pekka Pessi's avatar
Pekka Pessi committed
392 393 394 395 396 397

    TEST_1(f = sip_from_create(home, (void *)"sip:joe@bar"));
    TEST_1(sip_from_add_param(home, f, NULL) == -1);
    TEST_1(sip_from_add_param(home, f, "tag=tagged") == 0);
    TEST_S(f->a_tag, "tagged");
    TEST_1(sip_from_tag(home, f, "jxahudsf") == -1);
398 399
    while (f->a_params && f->a_params[0])
      msg_header_remove_param(f->a_common, f->a_params[0]);
400
    TEST_P(f->a_tag, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414
    TEST_1(sip_from_add_param(home, f, "test=1") == 0);
    TEST_1(sip_from_tag(home, f, "jxahudsf") == 0);
    TEST_S(f->a_tag, "jxahudsf");
    su_free(home, f);

    TEST_1(!sip_from_create(home, (void *)"sip:joe@[baa"));

    TEST_1(f = sip_from_make(home, (void *)"sip:joe@bar (foo)"));
    su_free(home, f);

    TEST_1(f = sip_from_make(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
    TEST_1(sip_from_tag(home, f, "tag=jxahudsf") == 0);
    su_free(home, f);

415 416 417 418
    TEST_1(f = sip_from_create(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
    TEST_1(sip_is_from((sip_header_t*)f));
    su_free(home, f);

Pekka Pessi's avatar
Pekka Pessi committed
419
    TEST_1(t = sip_to_create(home, (void *)"<sip:joe@bar;tag=bar> (joe)"));
420
    TEST_1(sip_is_to((sip_header_t*)t));
Pekka Pessi's avatar
Pekka Pessi committed
421
    TEST_1(sip_to_tag(home, t, "tag=jxahudsf") == 0);
422 423 424 425 426 427 428 429
    TEST_S(t->a_tag, "jxahudsf");
    TEST(msg_header_replace_param(home, t->a_common, "tag=bar"), 1);
    TEST_S(t->a_tag, "bar");

    TEST_1(t2 = sip_to_dup(home, t));
    TEST_S(t2->a_tag, "bar");

    TEST(msg_header_remove_param(t->a_common, "tag"), 1);
430
    TEST_P(t->a_tag, NULL);
431
    TEST_1(sip_to_add_param(home, t, "tst=1") == 0);
432
    TEST_P(t->a_tag, NULL);
433

Pekka Pessi's avatar
Pekka Pessi committed
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    su_free(home, t);
  }

  {
    sip_call_id_t *i, *i0;
    TEST_1(i = sip_call_id_create(home, "example.com"));
    i->i_hash = 0;
    TEST_1(i0 = sip_call_id_dup(home, i));
    su_free(home, i);
    TEST_1(i = sip_call_id_make(home, i0->i_id));
    TEST(i->i_hash, i0->i_hash);
    su_free(home, i);
    su_free(home, i0);
  }

  {
    sip_cseq_t *cs, *cs0;

    TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_invite, "1nvite"));
    TEST(cs->cs_seq, 123456789);
    TEST(cs->cs_method, sip_method_invite);
    TEST_S(cs->cs_method_name, "INVITE");

    su_free(home, cs);

    TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_invite, NULL));
    TEST(cs->cs_seq, 123456789);
    TEST(cs->cs_method, sip_method_invite);
    TEST_S(cs->cs_method_name, "INVITE");
    TEST_1(cs0 = sip_cseq_dup(home, cs));

    su_free(home, cs);
    su_free(home, cs0);

    TEST_1(!sip_cseq_create(home, 123456789, sip_method_unknown, NULL));

    TEST_1(cs = sip_cseq_create(home, 123456789, sip_method_unknown, 
				"invite"));
    TEST(cs->cs_seq, 123456789);
    TEST(cs->cs_method, sip_method_unknown);
    TEST_S(cs->cs_method_name, "invite");
    TEST_1(cs0 = sip_cseq_dup(home, cs));

    su_free(home, cs);
    su_free(home, cs0);
  }

  {
    sip_contact_t *m, *m0;

    TEST_1(!sip_contact_make(home, ",,"));
    
    TEST_1(m = sip_contact_create(home, (void *)"sip:joe@bar", 
				  "q=0.2", 
				  "+message",
				  NULL));
    TEST_S(m->m_q, "0.2");

    TEST_1(m0 = sip_contact_dup(home, m));

494 495
    TEST_1(sip_contact_add_param(home, m, "q=0.5") >= 0);
    TEST_1(sip_contact_add_param(home, m, "video=FALSE") >= 0);
Pekka Pessi's avatar
Pekka Pessi committed
496 497 498 499 500
    TEST_1(sip_contact_add_param(home, m, NULL) == -1);
    TEST_1(sip_contact_add_param(home, NULL, "video=FALSE") == -1);
    TEST_1(sip_contact_add_param(home, m, "audio=FALSE") == 0);
    TEST_1(sip_contact_add_param(home, m, "expires=0") == 0);

501 502 503
    TEST_S(m->m_q, "0.5");
    TEST_S(m->m_expires, "0");
    
Pekka Pessi's avatar
Pekka Pessi committed
504 505 506 507 508 509 510 511 512
    TEST_1(!sip_contact_create(home, (void *)"sip:joe@[baa",
			       "audio", "video", NULL));

    TEST_1(sip_header_format(home, sip_contact_class, "*"));

    su_free(home, m);
    su_free(home, m0);
  }

513 514 515 516 517 518
  {
    sip_via_t *v;
    char *s;

    v = sip_via_make(home, "SIP/2.0/UDP domain.invalid:5060"); TEST_1(v);
    s = sip_contact_string_from_via(home, v, NULL, v->v_protocol);
519
    TEST_S(s, "<sip:domain.invalid;transport=udp>");
520 521
    su_free(home, v), su_free(home, s);

522 523 524
    TEST_1(sip_transport_has_tls("SIP/2.0/TLS-SCTP"));
    TEST_1(sip_transport_has_tls("TLS-UDP"));

525 526 527 528 529 530
    v = sip_via_make(home, "SIP/2.0/TLS-SCTP domain.invalid"); TEST_1(v);
    s = sip_contact_string_from_via(home, v, NULL, v->v_protocol);
    TEST_S(s, "<sips:domain.invalid;transport=tls-sctp>");
    su_free(home, v), su_free(home, s);
  }

Pekka Pessi's avatar
Pekka Pessi committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  {
    sip_expires_t *ex;

    TEST_1(!sip_expires_make(home, "-12+1"));

    TEST_1(ex = sip_expires_make(home, "4294967297")); /* XXX */
    su_free(home, ex);

    TEST_1(ex = sip_expires_make(home, "Wed, 25 Mar 2004 14:49:29 GMT")); 
    su_free(home, ex);

    TEST_1(ex = sip_expires_create(home, 3600));
    su_free(home, ex);
  }

  {
    sip_retry_after_t *ra;
548
    char const *s;
Pekka Pessi's avatar
Pekka Pessi committed
549 550

    TEST_1(!(ra = sip_retry_after_make(home, "50 (foo")));
551 552 553 554
    TEST_1(ra = sip_retry_after_make(home, "50 (foo) ; duration = 13"));
    TEST_S(ra->af_duration, "13");
    TEST_S(ra->af_comment, "foo");
    TEST(msg_header_remove_param(ra->af_common, "duration"), 1);
555
    TEST_P(ra->af_duration, NULL);
556 557 558 559 560 561 562

    s = sip_header_as_string(home, (void*)ra);
    TEST_S(s, "50 (foo)");

    TEST(msg_header_add_param(home, ra->af_common, "x=z"), 0);
    s = sip_header_as_string(home, (void*)ra);
    TEST_S(s, "50 (foo) ;x=z");
Pekka Pessi's avatar
Pekka Pessi committed
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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

    su_free(home, ra);
  }

  {
    sip_date_t *d;

    TEST_1(!(d = sip_date_make(home, "Mon, 30 Feb 1896 23:59:59 GMT")));
    su_free(home, d);

    TEST_1(d = sip_date_create(home, (1<<30)));
    su_free(home, d);

    TEST_1(d = sip_date_create(home, 0));
    TEST_1(d->d_time != 0);
    su_free(home, d);
  }

  {
    sip_route_t *r, *r0;

    TEST_1(!sip_route_make(home, "<sip:foo@[bar:50>;lr"));
    TEST_1(r = sip_route_make(home, "<sip:foo@[baa::1]:5060>;lr"));
    TEST_1(r0 = sip_route_dup(home, r));

    TEST_1(sip_route_fix(r));
    TEST_1(url_has_param(r->r_url, "lr"));

    su_free(home, r); 
    TEST_1(r = sip_route_create(home, r0->r_url, r0->r_url));

    su_free(home, r); su_free(home, r0);
  }

  {
    sip_record_route_t *r, *r0;

    TEST_1(!sip_record_route_make(home, "<sip:foo@[bar:50>;lr"));
    TEST_1(!sip_record_route_make(home, "<sip:foo@[baa::1]>;lr bar, sip:foo"));
    TEST_1(r = sip_record_route_make(home, "<sip:foo@[baa::1]:5060>;lr"));
    TEST_1(r0 = sip_record_route_dup(home, r));
    su_free(home, r);

    TEST_1(r = sip_route_create(home, r0->r_url, r0->r_url));

    su_free(home, r), su_free(home, r0);
  }

  {
    sip_via_t *v, *v0;

    TEST_1(!sip_via_make(home, ",,"));
    TEST_1(!sip_via_make(home, "SIP// host:5060 (foo),"));
    TEST_1(!sip_via_make(home, "SIP/2.0/TCP host:5060 (foo) bar,"));
    TEST_1(!sip_via_make(home, "SIP/2.0/TCP [3ffe::1:5060 (foo),"));
    
    TEST_1(v = sip_via_create(home, "bar.com", 
			      "50600", 
			      "SIP/2.0/UDP",
622
			      "hidden",
Pekka Pessi's avatar
Pekka Pessi committed
623
			      "rport=50601",
624
			      "comp=sigcomp",
Pekka Pessi's avatar
Pekka Pessi committed
625 626 627 628
			      "branch=1",
			      "q=0.2",
			      NULL));
    TEST_S(v->v_branch, "1");
629 630 631 632
    TEST_S(v->v_rport, "50601");
    TEST_S(v->v_comp, "sigcomp");

    TEST_1(v = sip_via_make(home, "SIP/2.0/UDP bar.com:50600"
633 634
			    " ;hidden;rport=50601;comp=sigcomp;branch=1;ttl=15"
			    " ; maddr=[::227.0.0.1]"
635
			    " (This is a comment) "));
636 637
    TEST_S(v->v_ttl, "15");
    TEST_S(v->v_maddr, "[::227.0.0.1]");
638 639 640
    TEST_S(v->v_branch, "1");
    TEST_S(v->v_rport, "50601");
    TEST_S(v->v_comp, "sigcomp");
641
    
Pekka Pessi's avatar
Pekka Pessi committed
642 643
    TEST_1(v0 = sip_via_dup(home, v));

644 645 646
    TEST(msg_header_add_param(home, v->v_common, "rport"), 0);
    TEST_S(v->v_rport, "");
    TEST(msg_header_remove_param(v->v_common, "comp"), 1);
647
    TEST_P(v->v_comp, NULL);
648
    TEST(msg_header_remove_param(v->v_common, "ttl"), 1);
649
    TEST_P(v->v_ttl, NULL);
650
    TEST(msg_header_remove_param(v->v_common, "maddr"), 1);
651
    TEST_P(v->v_maddr, NULL);
652
    TEST(msg_header_remove_param(v->v_common, "rport"), 1);
653
    TEST_P(v->v_rport, NULL);
654
    TEST(msg_header_remove_param(v->v_common, "branch"), 1);
655
    TEST_P(v->v_branch, NULL);
656

Pekka Pessi's avatar
Pekka Pessi committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    TEST_1(sip_via_add_param(home, v, "video=FALSE") == 0);
    TEST_1(sip_via_add_param(home, v, NULL) == -1);
    TEST_1(sip_via_add_param(home, NULL, "video=FALSE") == -1);
    TEST_1(sip_via_add_param(home, v, "audio=FALSE") == 0);
    TEST_1(sip_via_add_param(home, v, "branch=0") == 0);

    su_free(home, v);
    su_free(home, v0);

    TEST_1(v = sip_via_create(home, "bar.com", 
			      "50600", 
			      NULL,
			      "rport=50601",
			      "branch=1",
			      "q=0.2",
			      NULL));
    TEST_S(v->v_protocol, "SIP/2.0/UDP");
    su_free(home, v);

  }

678 679 680 681 682 683 684 685 686 687 688
  {
    sip_call_info_t *ci, *ci0;

    TEST_1(ci = sip_call_info_make(home, 
				   "<http://www.nokia.com>;purpose=info"));
    TEST_S(ci->ci_purpose, "info");
    TEST_1(ci0 = sip_call_info_dup(home, ci));
    TEST_S(ci0->ci_purpose, "info");
    TEST_1(ci->ci_purpose != ci0->ci_purpose);

    TEST(msg_header_remove_param(ci->ci_common, "purpose"), 1);
689
    TEST_P(ci->ci_purpose, NULL);
690 691 692 693 694

    su_free(home, ci);
    su_free(home, ci0);
  }

Pekka Pessi's avatar
Pekka Pessi committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  su_home_check(home);
  su_home_zap(home);

  END();
}

int test_sip_msg_class(msg_mclass_t const *mc)
{
  int i, j, N;
  msg_hclass_t *hc;

  BEGIN();

  N = mc->mc_hash_size;

  /* check hashes */
  for (i = 0; i < N; i++) {
    if (!(hc = mc->mc_hash[i].hr_class))
      continue;
    for (j = i + 1; j < N; j++) {
      if (!mc->mc_hash[j].hr_class)
	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);
	return 1;
      }
    }
  }

  /* Check parser table sanity */
  for (i = 0; i < N; i++) {
    /* Verify each header entry */
    hc = mc->mc_hash[i].hr_class;
    
    if (hc == NULL)
      continue;

    /* Short form */
    if (hc->hc_short[0])
735
      TEST_P(mc->mc_short[hc->hc_short[0] - 'a'].hr_class, hc);
Pekka Pessi's avatar
Pekka Pessi committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

    /* Long form */
    j = msg_header_name_hash(hc->hc_name, NULL);
    TEST(j, hc->hc_hash);

    for (j = MC_HASH(hc->hc_name, N); j != i; j = (j + 1) % N)
      TEST_1(mc->mc_hash[j].hr_class);

  }

  END();
}

msg_t *read_message(int flags, char const buffer[])
{
751 752
  size_t n;
  int m;
Pekka Pessi's avatar
Pekka Pessi committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
  msg_t *msg;
  msg_iovec_t iovec[2];

  n = strlen(buffer);
  if (n == 0) 
    return NULL;

  msg = msg_create(test_mclass, flags);
  if (msg_recv_iovec(msg, iovec, 2, n, 1) < 0) {
    perror("msg_recv_iovec");
  }
  memcpy(iovec->mv_base, buffer, n);
  msg_recv_commit(msg, n, 1);

  m = msg_extract(msg);

  return msg;
}

static int test_encoding(void)
{
Pekka Pessi's avatar
Pekka Pessi committed
774
  msg_header_t *h, *h1;
Pekka Pessi's avatar
Pekka Pessi committed
775 776 777 778 779 780 781
  msg_common_t *c;
  msg_t *msg;
  sip_t *sip;
  su_home_t *home;

  BEGIN();

782
  TEST_1(home = su_home_new(sizeof *home));
Pekka Pessi's avatar
Pekka Pessi committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

  msg = read_message(MSG_DO_EXTRACT_COPY, 
    "SUBSCRIBE sip:foo@bar SIP/2.0\r\n"
    "To: Joe User <sip:foo@bar>\r\n"
    "From: \"Bar Owner\" <sip:bar@foo>;tag=foobar\r\n"
    "P-Asserted-Identity: <sip:bar@foo>\r\n"
    "P-Preferred-Identity: <sip:bar-owner@foo>\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq: 8 SUBSCRIBE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133\r\n"
    "Extension-Header: extended, more\r\n"
    "Reason: Q.850;cause=16;text=\"Terminated\"\r\n"
    "Contact: <sip:bar@pc.foo:5060>\r\n"
    "Date: Wed, 25 Mar 2004 14:49:29 GMT\r\n"
    "Max-Forwards: 80\r\n"
    "Min-Expires: 30\r\n"
    "Retry-After: 48 (this is a comment) ;duration=321\r\n"
    "Route: <sip:proxy.bar;maddr=172.21.40.40>\r\n"
    "Request-Disposition: proxy\r\n"
    "Accept-Contact: *;audio\r\n"
    "Reject-Contact: *;video\r\n"
    "Expires: 1200\r\n"
    "Event: presence;id=1\r\n"
    "In-Reply-To: {0h!a0i\"sndaksdj}@[kjsafi3], {0h!a0i\"snj}@[kjsfi3]\r\n"
    "Organization: Nuoret Banaani-Kotkat y.r.\r\n"
    "Priority: urgent\r\n"
    "Subject: ynk\r\n"
    "Timestamp: 3289129810.798259\r\n"
    "SIP-If-Match: foobar\r\n"
    "Proxy-Requires: prefs\r\n"
    "Supported: vnd.nokia\r\n"
    "User-Agent: Unknown Subscriber (1.0) Tonto (2.0)\r\n"
    "Accept: application/pidf+xml;version=1.0\r\n"
    "Accept-Encoding: gzip\r\n"
817
     /* Test loop below cannot encode multiple Accept-Language on one line */
Pekka Pessi's avatar
Pekka Pessi committed
818 819 820 821 822 823 824 825 826 827
    "Accept-Language: "/* "fi, "*/"en;q=0.2\r\n"
    "RAck: 421413 214214 INVITE\r\n"
    "Referred-By: <sips:bob@biloxi.example.com>\r\n"
    "Replaces: 12345601@atlanta.example.com;from-tag=314159;to-tag=1234567\r\n"
    "Authorization: Digest realm=\"foo\"\r\n"
    "Proxy-Authorization: Digest realm=\"foo\"\r\n"
    "Security-Client: tls\r\n"
    "Security-Verify: tls;q=0.2\r\n"
    "Privacy: none\r\n"
    "Content-Length: 7\r\n"
828
    "Content-Encoding: gzip, deflate, identity\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
829 830 831 832 833 834
    "Content-Disposition: filter\r\n"
    "Content-Language: fi\r\n"
    "MIME-Version: 1.0\r\n"
    "Min-SE: 123\r\n"
    "Session-Expires: 1200\r\n"
    "Content-Type: text/plain\r\n"
835 836 837
    "Refer-Sub: true\r\n"
    "Suppress-Body-If-Match: humppa\r\n"
    "Suppress-Notify-If-Match: zumppa\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
838 839 840 841 842 843
    "\r\n"
    "Heippa!");
  sip = sip_object(msg);

  TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);

Pekka Pessi's avatar
Pekka Pessi committed
844
  for (h = (msg_header_t *)sip->sip_request; h; h = h->sh_succ) {
Pekka Pessi's avatar
Pekka Pessi committed
845
    char b[80];
846
    size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
847

Pekka Pessi's avatar
Pekka Pessi committed
848
    if (h == (msg_header_t*)sip->sip_payload)
Pekka Pessi's avatar
Pekka Pessi committed
849 850
      break;

Pekka Pessi's avatar
Pekka Pessi committed
851 852
    TEST_1(h1 = msg_header_dup(home, h));
    n = msg_header_e(b, sizeof b, h1, 0);
853
    TEST_SIZE(n, h->sh_len);
Pekka Pessi's avatar
Pekka Pessi committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    TEST_M(b, h->sh_data, n);
    su_free(home, h1);
  }

  msg_destroy(msg), msg = NULL;

  /* Note: this should be canonic! */
  msg = read_message(MSG_DO_EXTRACT_COPY, 
    "SIP/2.0 200 Ok\r\n"
    "To: Joe User <sip:foo@bar>;tag=deadbeef\r\n"
    "From: sip:bar@foo;tag=foobar\r\n"
    "Call-ID: {0h!a0i\"sndaksdj}@[kjsafi3]\r\n"
    "CSeq: 8912734 SUBSCRIBE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133\r\n"
    "Extension-Header: extended, more\r\n"
    "Reason: SIP;cause=400;text=\"Bad Message\"\r\n"
    "Contact: <sip:bar@pc.foo:5060>;audio\r\n"
    "Date: Wed, 25 Mar 2004 14:49:29 GMT\r\n"
    "Max-Forwards: 80\r\n"
    "Min-Expires: 30\r\n"
    "Expires: Wed, 25 Mar 2004 15:49:29 GMT\r\n"
    "Retry-After: 48;duration=321\r\n"
    "Record-Route: <sip:record-route@proxy.bar;maddr=172.21.40.40>\r\n"
    "Event: presence;id=1\r\n"
    "Allow-Events: presence, presence.winfo\r\n"
    "Subscription-State: active;expires=1800\r\n"
    "Call-Info: <http://www.bar.com/xcap/joe/>;purpose=xcap\r\n"
    "Error-Info: <http://www.bar.com/xcap/joe/errors>;param=xcap\r\n"
    "Server: None\r\n"		     
    "Timestamp: 3289129810.798259 0.084054\r\n"
    "SIP-ETag: foobar\r\n"
    "SIP-If-Match: foobar\r\n"
    "Requires: vnd.nokia\r\n"
    "Unsupported: vnd.nokia.pic\r\n"
    "Accept-Disposition: filter\r\n"
    "Warning: 399 presence.bar:5060 \"Unimplemented filter\"\r\n"
    "RSeq: 421414\r\n"
    "Refer-To: <sip:hsdf@cdwf.xcfw.com?Subject=test&Organization=Bar>\r\n"
    "WWW-Authenticate: Digest realm=\"foo\"\r\n"
    "Proxy-Authenticate: Digest realm=\"foo\"\r\n"
    "Security-Server: tls;q=0.2\r\n"
    "Session-Expires: 1200;refresher=uac\r\n"
    "Content-Length: 7\r\n"
    "Content-Type: text/plain;charset=iso8859-1\r\n"
    "\r\n"
    "Heippa!");
  sip = sip_object(msg);

  TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);

Pekka Pessi's avatar
Pekka Pessi committed
904
  for (h = (msg_header_t *)sip->sip_status; h; h = h->sh_succ) {
Pekka Pessi's avatar
Pekka Pessi committed
905
    char b[80];
906
    size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
907 908 909 910 911 912

    if (h == (sip_header_t*)sip->sip_payload)
      break;

    TEST_1(h1 = sip_header_dup(home, h));
    n = sip_header_e(b, sizeof b, h1, 0);
913
    TEST_SIZE(n, h->sh_len);
Pekka Pessi's avatar
Pekka Pessi committed
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 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    TEST_M(b, h->sh_data, n);
    su_free(home, h1);
  }

  TEST_1(sip->sip_etag);
  TEST_S(sip->sip_etag->g_value, "foobar");
  TEST_1(sip->sip_if_match);

  msg_destroy(msg), msg = NULL;

  su_home_check(home);
  su_home_zap(home);

  msg = read_message(0, 
		     "SIP/2.0 200 Ok\r\n"
		     "Via: SIP/2.0/UDP 135.180.130.133\r\n"
		     "Via: SIP/2.0/UDP 135.180.130.130:5060\r\n"
		     "To: Joe User <sip:foo@bar>;tag=deadbeef\r\n"
		     "From: sip:bar@foo;tag=foobar\r\n"
		     "Call-ID: {0h!a0i\"sndaksdj}@[kjsafi3]\r\n"
		     "CSeq: 8912734 SUBSCRIBE\r\n"
		     "Record-Route: <sip:135.180.130.133;lr>\r\n"
		     "Record-Route: <sip:135.180.130.130;lr>\r\n"
		     "Content-Length: 0\r\n"
		     "\r\n");
		    
  sip = sip_object(msg);

  TEST_1(msg); TEST_1(sip); TEST_1(!sip->sip_error);

  sip->sip_flags |= MSG_FLG_COMPACT;

  TEST_1(msg_prepare(msg) != 0);

  TEST_1(c = sip->sip_status->st_common);
  TEST_M(c->h_data, "SIP/2.0 200 Ok\r\n", c->h_len);
  
  TEST_1(c = sip->sip_to->a_common);
  TEST_M(c->h_data, "t:Joe User<sip:foo@bar>;tag=deadbeef\r\n", c->h_len);

  TEST_1(c = sip->sip_from->a_common);
  TEST_M(c->h_data, "f:sip:bar@foo;tag=foobar\r\n", c->h_len);

  TEST_1(c = sip->sip_call_id->i_common);
  TEST_M(c->h_data, "i:{0h!a0i\"sndaksdj}@[kjsafi3]\r\n", c->h_len);

  TEST_1(c = sip->sip_cseq->cs_common);
  TEST_M(c->h_data, "CSeq:8912734 SUBSCRIBE\r\n", c->h_len);

  TEST_1(c = sip->sip_via->v_common);
  TEST_M(c->h_data, "v:SIP/2.0/UDP 135.180.130.133,SIP/2.0/UDP 135.180.130.130:5060\r\n", c->h_len);

  TEST_1(c = sip->sip_via->v_next->v_common);
967
  TEST_SIZE(c->h_len, 0); TEST_1(c->h_data);
Pekka Pessi's avatar
Pekka Pessi committed
968 969 970 971 972

  TEST_1(c = sip->sip_record_route->r_common);
  TEST_M(c->h_data, "Record-Route:<sip:135.180.130.133;lr>,<sip:135.180.130.130;lr>\r\n", c->h_len);

  TEST_1(c = sip->sip_record_route->r_next->r_common);
973
  TEST_SIZE(c->h_len, 0); TEST_1(c->h_data);
Pekka Pessi's avatar
Pekka Pessi committed
974 975 976 977 978 979 980 981 982 983 984 985

  TEST_1(c = sip->sip_content_length->l_common);
  TEST_M(c->h_data, "l:0\r\n", c->h_len);

  END();
}

#define XTRA(xtra, h) SU_ALIGN(xtra) + sip_header_size((sip_header_t*)h)

/** Test header filtering and duplicating */
int tag_test(void)
{
986
  su_home_t *home = su_home_new(sizeof(*home));
Pekka Pessi's avatar
Pekka Pessi committed
987 988 989 990 991 992 993 994 995 996 997 998 999
  sip_request_t *request = 
    sip_request_make(home, "INVITE sip:joe@example.com SIP/2.0");
  sip_to_t *to = sip_to_make(home, 
			     "Joe User <sip:joe.user@example.com;param=1>"
			     ";tag=12345678");
  sip_via_t *via = sip_via_make(home,
				"SIP/2.0/UDP sip.example.com"
				";maddr=128.12.9.254"
				";branch=289412978y641.321312");
  url_t *url = url_hdup(home, 
    (url_t *)"sip:test:pass@example.com;baz=1?foo&bar");

  tagi_t *lst, *dup;
1000
  size_t xtra;
Pekka Pessi's avatar
Pekka Pessi committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  tag_value_t v;

  BEGIN();

  su_home_check(home);

  TEST_1(home && request && to && via);

  lst = tl_list(SIPTAG_REQUEST(request),
		SIPTAG_TO(to), 
		SIPTAG_VIA(via),
		URLTAG_URL(url),
		TAG_NULL());

  xtra = 0;
  xtra += XTRA(xtra, request); 
  xtra += XTRA(xtra, to); 
  xtra += XTRA(xtra, via); 
  xtra += SU_ALIGN(xtra) + sizeof(*url) + url_xtra(url);

1021 1022
  TEST_SIZE(tl_len(lst), 5 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(lst, 0), xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1023 1024 1025 1026

  dup = tl_adup(NULL, lst);

  TEST(dup != NULL, 1);
1027 1028
  TEST_SIZE(tl_len(dup), 5 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(dup, 0), xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1029 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 1058 1059 1060 1061 1062

  if (tstflags & tst_verbatim)
    tl_print(stdout, "dup:\n", dup);

  su_free(NULL, dup);
  tl_vfree(lst);

  TEST_1(t_scan(siptag_request, home, "INVITE sip:example.org SIP/2.0", &v));
  TEST_1(request = (void *)v);
  TEST_1(request->rq_common->h_class == sip_request_class);
  TEST_S(request->rq_method_name, "INVITE");
  TEST_S(request->rq_version, "SIP/2.0");
  
  TEST_1(t_scan(siptag_to, home, "Example <sip:example.org>;tag=foo", &v));
  TEST_1(to = (void *)v);
  TEST_1(to->a_common->h_class == sip_to_class);
  TEST_S(to->a_display, "Example");
  TEST_S(to->a_tag, "foo");

  su_home_check(home);
  su_home_zap(home);

  END();
}

/** Test advanced tag features */
static int parser_tag_test(void)
{
  tagi_t *lst, *dup, *filter1, *filter2, *filter3, *filter4;
  tagi_t *b1, *b2, *b3, *b4;

  msg_t *msg;
  sip_t *sip;
  su_home_t *home;
1063
  size_t xtra;
Pekka Pessi's avatar
Pekka Pessi committed
1064 1065 1066

  BEGIN();

1067
  home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

  msg = read_message(MSG_DO_EXTRACT_COPY, 
"SIP/2.0 401 Unauthorized\r\n"
"Via: SIP/2.0/UDP srlab.sr.ntc.nokia.com:5060;maddr=192.168.102.5\r\n"
"Via: SIP/2.0/UDP 172.21.9.155\r\n"
"Record-Route: <sip:garage.sr.ntc.nokia.com:5060;maddr=srlab.sr.ntc.nokia.com>\r\n"
"From: sip:digest@garage.sr.ntc.nokia.com\r\n"
"To: sip:digest@garage.sr.ntc.nokia.com\r\n"
"Call-ID: 982773899-reg@172.21.9.155\r\n"
"CSeq: 1 REGISTER\r\n"
"WWW-Authenticate: Digest realm=\"garage.sr.ntc.nokia.com\",\r\n"
"  nonce=\"MjAwMS0wMS0yMSAxNTowODo1OA==\", algorithm=MD5, qop=\"auth\"\r\n"
"Proxy-Authenticate: Digest realm=\"IndigoSw\", domain=\"sip:indigosw.com\", "
"nonce=\"V2VkIEF1ZyAxNSAxODoxMzozMiBCU1QgMjAwMVtCQDJkYjE5ZA==\", "
"opaque=\"NzA3ZjJhYzU4MGY3MzU0MQ==\", stale=false, "
"algorithm=md5, algorithm=sha1, qop=\"auth\"\r\n" 
/* , qop=\"auth, auth-int\"\r */
"\r\n");

  sip = sip_object(msg);

  TEST_1(home && msg && sip);
1090
  TEST_1(sip->sip_size >= sizeof *sip);
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

  TEST_1(sip_is_status((sip_header_t *)sip->sip_status));
  TEST_1(sip_is_via((sip_header_t *)sip->sip_via));
  TEST_1(sip_is_via((sip_header_t *)sip->sip_via->v_next));
  TEST_1(sip_is_record_route((sip_header_t *)sip->sip_record_route));
  TEST_1(sip_is_from((sip_header_t *)sip->sip_from));
  TEST_1(sip_is_to((sip_header_t *)sip->sip_to));
  TEST_1(sip_is_call_id((sip_header_t *)sip->sip_call_id));
  TEST_1(sip_is_cseq((sip_header_t *)sip->sip_cseq));
  TEST_1(sip_is_www_authenticate(
    (sip_header_t *)sip->sip_www_authenticate));

  TEST_1(sip_complete_message(msg) == 0);

  TEST_1(sip_is_content_length((sip_header_t *)sip->sip_content_length));

1107
  TEST_P(sip->sip_content_length->l_common->h_succ, sip->sip_separator);
Pekka Pessi's avatar
Pekka Pessi committed
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

  lst = tl_list(SIPTAG_VIA(sip->sip_via), 
		SIPTAG_RECORD_ROUTE(sip->sip_record_route), 
		TAG_SKIP(2), 
		SIPTAG_CSEQ(sip->sip_cseq), 
		SIPTAG_PAYLOAD(sip->sip_payload), 
		TAG_NULL());
  filter1 = tl_list(SIPTAG_VIA(0), 
		    TAG_NULL());
  filter2 = tl_list(SIPTAG_CALL_ID(0), 
		    SIPTAG_FROM(0),
		    SIPTAG_ROUTE(0),
		    SIPTAG_CSEQ(0),
		    TAG_NULL());
  filter3 = tl_list(SIPTAG_CSEQ(0), 
		    SIPTAG_CONTENT_LENGTH(0), 
		    TAG_NULL());
  filter4 = tl_list(SIPTAG_STATUS(0),
		    SIPTAG_VIA(0),
		    SIPTAG_RECORD_ROUTE(0),
		    SIPTAG_FROM(0),
		    SIPTAG_TO(0),
		    SIPTAG_CALL_ID(0),
		    SIPTAG_CSEQ(0),
		    SIPTAG_WWW_AUTHENTICATE(0),
		    SIPTAG_PROXY_AUTHENTICATE(0),
		    SIPTAG_CONTENT_LENGTH(0),
		    TAG_NULL());

  TEST_1(lst && filter1 && filter2 && filter3 && filter4);

  b1 = tl_afilter(home, filter1, lst);
1140
  TEST_SIZE(tl_len(b1), 2 * sizeof(tagi_t));
Pekka Pessi's avatar
Pekka Pessi committed
1141 1142 1143 1144
  TEST_1(((sip_via_t *)b1->t_value)->v_next);
  xtra = sip_header_size((sip_header_t *)sip->sip_via);
  xtra += SU_ALIGN(xtra);
  xtra += sip_header_size((sip_header_t *)sip->sip_via->v_next);
1145
  TEST_SIZE(tl_xtra(b1, 0), xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1146 1147 1148

  dup = tl_adup(home, lst);

1149 1150
  TEST_SIZE(tl_len(dup), tl_len(lst));
  TEST_SIZE(tl_xtra(dup, 0), tl_xtra(lst, 0));
Pekka Pessi's avatar
Pekka Pessi committed
1151 1152 1153 1154 1155 1156

  tl_vfree(lst);

  lst = tl_list(SIPTAG_SIP(sip), TAG_NULL());

  b2 = tl_afilter(home, filter2, lst);
1157
  TEST_SIZE(tl_len(b2), 4 * sizeof(tagi_t));
Pekka Pessi's avatar
Pekka Pessi committed
1158 1159 1160 1161
  xtra = 0;
  xtra += XTRA(xtra, sip->sip_call_id);
  xtra += XTRA(xtra, sip->sip_from);
  xtra += XTRA(xtra, sip->sip_cseq);
1162
  TEST_SIZE(tl_xtra(b2, 0), xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1163 1164 1165

  b3 = tl_afilter(home, filter3, lst);

1166 1167 1168
  TEST_SIZE(tl_len(b3), 3 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b3, 0), 
	    sizeof(sip_content_length_t) + sizeof(sip_cseq_t));
Pekka Pessi's avatar
Pekka Pessi committed
1169 1170

  b4 = tl_afilter(home, filter4, lst);
1171
  TEST_SIZE(tl_len(b4), 11 * sizeof(tagi_t));
Pekka Pessi's avatar
Pekka Pessi committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  xtra = 0;
  xtra += XTRA(xtra, sip->sip_status); 
  xtra += XTRA(xtra, sip->sip_via); 
  xtra += XTRA(xtra, sip->sip_via->v_next); 
  xtra += XTRA(xtra, sip->sip_record_route);
  xtra += XTRA(xtra, sip->sip_from); 
  xtra += XTRA(xtra, sip->sip_to); 
  xtra += XTRA(xtra, sip->sip_call_id); 
  xtra += XTRA(xtra, sip->sip_cseq); 
  xtra += XTRA(xtra, sip->sip_www_authenticate); 
  xtra += XTRA(xtra, sip->sip_proxy_authenticate); 
  xtra += XTRA(xtra, sip->sip_content_length); 
1184
  TEST_SIZE(tl_xtra(b4, 0), xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

  tl_vfree(filter1); tl_vfree(filter2); tl_vfree(filter3); tl_vfree(filter4);
  tl_vfree(lst);

  su_home_check(home);

  su_free(home, b4);
  su_free(home, b3); 
  su_free(home, b2); 
  su_free(home, dup);
  su_free(home, b1); 

  su_home_check(home);

1199
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
1200 1201 1202 1203 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 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 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

  msg_destroy(msg);

  END();
}

/** Test error messages */
static int response_phrase_test(void)
{
  BEGIN();
  {
    struct { int status; char const *phrase; } const errors[] = 
      {
	{ SIP_100_TRYING },
	{ SIP_180_RINGING },
	{ SIP_181_CALL_IS_BEING_FORWARDED },
       	{ SIP_182_QUEUED },
       	{ SIP_183_SESSION_PROGRESS },
       	{ SIP_200_OK },
       	{ SIP_202_ACCEPTED },
       	{ SIP_300_MULTIPLE_CHOICES },
       	{ SIP_301_MOVED_PERMANENTLY },
       	{ SIP_302_MOVED_TEMPORARILY },
       	{ SIP_305_USE_PROXY },
       	{ SIP_380_ALTERNATIVE_SERVICE },
       	{ SIP_400_BAD_REQUEST },
       	{ SIP_401_UNAUTHORIZED },
       	{ SIP_402_PAYMENT_REQUIRED },
       	{ SIP_403_FORBIDDEN },
       	{ SIP_404_NOT_FOUND },
       	{ SIP_405_METHOD_NOT_ALLOWED },
       	{ SIP_406_NOT_ACCEPTABLE },
       	{ SIP_407_PROXY_AUTH_REQUIRED },
       	{ SIP_408_REQUEST_TIMEOUT },
       	{ SIP_409_CONFLICT },
       	{ SIP_410_GONE },
       	{ SIP_411_LENGTH_REQUIRED },
       	{ SIP_413_REQUEST_TOO_LARGE },
       	{ SIP_414_REQUEST_URI_TOO_LONG },
       	{ SIP_415_UNSUPPORTED_MEDIA },
       	{ SIP_416_UNSUPPORTED_URI },
       	{ SIP_420_BAD_EXTENSION },
       	{ SIP_421_EXTENSION_REQUIRED },
       	{ SIP_422_SESSION_TIMER_TOO_SMALL },
       	{ SIP_423_INTERVAL_TOO_BRIEF },
       	{ SIP_423_REGISTRATION_TOO_BRIEF },
       	{ SIP_480_TEMPORARILY_UNAVAILABLE },
       	{ SIP_481_NO_TRANSACTION },
       	{ SIP_481_NO_CALL },
       	{ SIP_482_LOOP_DETECTED },
       	{ SIP_483_TOO_MANY_HOPS },
       	{ SIP_484_ADDRESS_INCOMPLETE },
       	{ SIP_485_AMBIGUOUS },
       	{ SIP_486_BUSY_HERE },
       	{ SIP_487_REQUEST_TERMINATED },
       	{ SIP_487_REQUEST_CANCELLED },
       	{ SIP_488_NOT_ACCEPTABLE },
       	{ SIP_489_BAD_EVENT },
       	{ SIP_491_REQUEST_PENDING },
       	{ SIP_493_UNDECIPHERABLE },
       	{ SIP_500_INTERNAL_SERVER_ERROR },
       	{ SIP_501_NOT_IMPLEMENTED },
       	{ SIP_502_BAD_GATEWAY },
       	{ SIP_503_SERVICE_UNAVAILABLE },
       	{ SIP_504_GATEWAY_TIME_OUT },
       	{ SIP_505_VERSION_NOT_SUPPORTED },
       	{ SIP_513_MESSAGE_TOO_LARGE },
       	{ SIP_600_BUSY_EVERYWHERE },
       	{ SIP_603_DECLINE },
       	{ SIP_604_DOES_NOT_EXIST_ANYWHERE },
       	{ SIP_606_NOT_ACCEPTABLE },
	{ 0, NULL }
      };
    int i;

    for (i = 0; errors[i].status; i++)
      TEST_S(errors[i].phrase, sip_status_phrase(errors[i].status));
  }
  END();
}

/** Test parser and header manipulation */
static int parser_test(void)
{
  msg_t *msg;
  sip_t *sip;
  su_home_t *home;

  sip_route_t *r;

  sip_request_t        sip_request[1] = { SIP_REQUEST_INIT() };
  sip_status_t         sip_status[1]  = { SIP_STATUS_INIT() };
  sip_header_t         sip_unknown[1]  = { SIP_UNKNOWN_INIT() };
  sip_separator_t      sip_separator[1] = { SIP_SEPARATOR_INIT() };     
  sip_payload_t        sip_payload[1] = { SIP_PAYLOAD_INIT() };
  sip_via_t            sip_via[1] = { SIP_VIA_INIT() };
  sip_route_t          sip_route[1] = { SIP_ROUTE_INIT() };		
  sip_record_route_t   sip_record_route[1] = { SIP_RECORD_ROUTE_INIT() };
  sip_max_forwards_t   sip_max_forwards[1] = { SIP_MAX_FORWARDS_INIT() };
  sip_from_t           sip_from[1] = { SIP_FROM_INIT() };
  sip_to_t             sip_to[1] = { SIP_TO_INIT() };
  sip_call_id_t        sip_call_id[1] = { SIP_CALL_ID_INIT() };
  sip_cseq_t           sip_cseq[1] = { SIP_CSEQ_INIT() };
  sip_contact_t        sip_contact[1] = { SIP_CONTACT_INIT() };
						
  sip_expires_t        sip_expires[1] = { SIP_EXPIRES_INIT() };
  sip_date_t           sip_date[1] = { SIP_DATE_INIT() };
  sip_retry_after_t    sip_retry_after[1] = { SIP_RETRY_AFTER_INIT() };
  sip_timestamp_t      sip_timestamp[1] = { SIP_TIMESTAMP_INIT() };
  sip_subject_t        sip_subject[1] = { SIP_SUBJECT_INIT() };
  sip_priority_t       sip_priority[1] = { SIP_PRIORITY_INIT() };
		           		
  sip_call_info_t      sip_call_info[1] = { SIP_CALL_INFO_INIT() };
  sip_organization_t   sip_organization[1] = { SIP_ORGANIZATION_INIT() };
  sip_server_t         sip_server[1] = { SIP_SERVER_INIT() };
  sip_user_agent_t     sip_user_agent[1] = { SIP_USER_AGENT_INIT() };
  sip_in_reply_to_t    sip_in_reply_to[1] = { SIP_IN_REPLY_TO_INIT() };
		           		
  sip_accept_t         sip_accept[1] = { SIP_ACCEPT_INIT() };
  sip_accept_encoding_t sip_accept_encoding[1] = { SIP_ACCEPT_ENCODING_INIT() };
  sip_accept_language_t sip_accept_language[1] = { SIP_ACCEPT_LANGUAGE_INIT() };
   
  sip_session_expires_t sip_session_expires[1] = { SIP_SESSION_EXPIRES_INIT() };
  sip_min_se_t sip_min_se[1] = { SIP_MIN_SE_INIT() };   

  sip_allow_t          sip_allow[1] = { SIP_ALLOW_INIT() };
  sip_require_t        sip_require[1] = { SIP_REQUIRE_INIT() };
  sip_proxy_require_t  sip_proxy_require[1] = { SIP_PROXY_REQUIRE_INIT() };
  sip_supported_t      sip_supported[1] = { SIP_SUPPORTED_INIT() };
  sip_unsupported_t    sip_unsupported[1] = { SIP_UNSUPPORTED_INIT() };
#if SIP_HAVE_ENCRYPTION
  sip_encryption_t     sip_encryption[1] = { SIP_ENCRYPTION_INIT() };
#endif
#if SIP_HAVE_RESPONSE_KEY
  sip_response_key_t   sip_response_key[1] = { SIP_RESPONSE_KEY_INIT() };
#endif

  sip_proxy_authenticate_t  sip_proxy_authenticate[1] = { SIP_PROXY_AUTHENTICATE_INIT() };
  sip_proxy_authorization_t sip_proxy_authorization[1] = { SIP_PROXY_AUTHORIZATION_INIT() };
  sip_authorization_t  sip_authorization[1] = { SIP_AUTHORIZATION_INIT() };
  sip_www_authenticate_t sip_www_authenticate[1] = { SIP_WWW_AUTHENTICATE_INIT() };
  sip_error_info_t     sip_error_info[1] = { SIP_ERROR_INFO_INIT() };
  sip_warning_t        sip_warning[1] = { SIP_WARNING_INIT() };
		       
  sip_mime_version_t   sip_mime_version[1] = { SIP_MIME_VERSION_INIT() };
  sip_content_type_t   sip_content_type[1] = { SIP_CONTENT_TYPE_INIT() };
  sip_content_encoding_t sip_content_encoding[1] = { SIP_CONTENT_ENCODING_INIT() };
  sip_content_disposition_t sip_content_disposition[1] = { SIP_CONTENT_DISPOSITION_INIT() };
  sip_content_length_t sip_content_length[1] = { SIP_CONTENT_LENGTH_INIT() };

  BEGIN();

1352
  home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
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 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

  msg = read_message(MSG_DO_EXTRACT_COPY, 
    "INVITE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
    "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
    "  ; tag = deadbeef\r\n"
    "From: http://www.cs.columbia.edu\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq : 8 INVITE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133\r\n"
    "Route: <sip:1@a;lr>, sip:2@b;lr=2, <sip:3@c;lr=3>\r\n"
    "Route: <sip:1@d;lr=4>\r\n"
    "Route: sip:2@e;lr=5, <sip:3@f;lr=6>\r\n"
    "Route: <sip:1@g;lr=7>, <sip:2@h>;lr=8\r\n"
    "Content-Type: application/sdp\r\n"
    "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\"foo baa\", sip:kuik@foo.invalid\r\n"
    "Via: SIP/2.0/UDP [aa:bb::1]:5061\r\n"
    "\r\n"
    "v=0\r\n"
    "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n"
    "c=IN IP4 135.180.130.88\r\n"
    "m=audio 492170 RTP/AVP 0 12\r\n"
    "m=video 3227 RTP/AVP 31\r\n"
    "a=rtpmap:31 LPC\r\n");

  sip = sip_object(msg);

  TEST_1(home && msg && sip);

  TEST_1(sip_is_request((sip_header_t *)sip->sip_request));
  TEST_1(sip->sip_via); TEST_1(sip->sip_via->v_next);
  TEST_1(sip->sip_via->v_next->v_next == NULL);
  TEST_1(sip_sanity_check(sip) == 0);

  TEST_1(r = sip->sip_route); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(!r->r_next);

  TEST_1(r = sip_route_fix(sip->sip_route)); TEST_1(!r->r_common->h_data);
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data);
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data); 
  TEST_1(r = r->r_next); TEST_1(!r->r_common->h_data); 
  TEST_1(!r->r_next);

  /* Quiet lots of warnings */
  #define _msg_header_offset msg_header_offset
  #define msg_header_offset(msg, sip, h) \
    _msg_header_offset(msg, (msg_pub_t *)sip, (msg_header_t *)h)

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  TEST_P(msg_header_offset(msg, sip, sip_request), &sip->sip_request);
  TEST_P(msg_header_offset(msg, sip, sip_status), &sip->sip_status);
  TEST_P(msg_header_offset(msg, sip, sip_unknown), &sip->sip_unknown);
  TEST_P(msg_header_offset(msg, sip, sip_separator), &sip->sip_separator);
  TEST_P(msg_header_offset(msg, sip, sip_payload), &sip->sip_payload);
  TEST_P(msg_header_offset(msg, sip, sip_via), &sip->sip_via);
  TEST_P(msg_header_offset(msg, sip, sip_route), &sip->sip_route);
  TEST_P(msg_header_offset(msg, sip, sip_record_route),
	 &sip->sip_record_route);
  TEST_P(msg_header_offset(msg, sip, sip_max_forwards),
	 &sip->sip_max_forwards);
  TEST_P(msg_header_offset(msg, sip, sip_from), &sip->sip_from);
  TEST_P(msg_header_offset(msg, sip, sip_to), &sip->sip_to);
  TEST_P(msg_header_offset(msg, sip, sip_call_id), &sip->sip_call_id);
  TEST_P(msg_header_offset(msg, sip, sip_cseq), &sip->sip_cseq);
  TEST_P(msg_header_offset(msg, sip, sip_contact), &sip->sip_contact);
Pekka Pessi's avatar
Pekka Pessi committed
1427
						
1428 1429 1430 1431 1432 1433
  TEST_P(msg_header_offset(msg, sip, sip_expires), &sip->sip_expires);
  TEST_P(msg_header_offset(msg, sip, sip_date), &sip->sip_date);
  TEST_P(msg_header_offset(msg, sip, sip_retry_after), &sip->sip_retry_after);
  TEST_P(msg_header_offset(msg, sip, sip_timestamp), &sip->sip_timestamp);
  TEST_P(msg_header_offset(msg, sip, sip_subject), &sip->sip_subject);
  TEST_P(msg_header_offset(msg, sip, sip_priority), &sip->sip_priority);
Pekka Pessi's avatar
Pekka Pessi committed
1434
		           		
1435 1436 1437 1438 1439 1440
  TEST_P(msg_header_offset(msg, sip, sip_call_info), &sip->sip_call_info);
  TEST_P(msg_header_offset(msg, sip, sip_organization),
	 &sip->sip_organization);
  TEST_P(msg_header_offset(msg, sip, sip_server), &sip->sip_server);
  TEST_P(msg_header_offset(msg, sip, sip_user_agent), &sip->sip_user_agent);
  TEST_P(msg_header_offset(msg, sip, sip_in_reply_to), &sip->sip_in_reply_to);
Pekka Pessi's avatar
Pekka Pessi committed
1441
		           		
1442 1443 1444 1445 1446 1447 1448 1449 1450
  TEST_P(msg_header_offset(msg, sip, sip_accept), &sip->sip_accept);
  TEST_P(msg_header_offset(msg, sip, sip_accept_encoding),
	 &sip->sip_accept_encoding);
  TEST_P(msg_header_offset(msg, sip, sip_accept_language),
	 &sip->sip_accept_language);

  TEST_P(msg_header_offset(msg, sip, sip_session_expires),
	 &sip->sip_session_expires);
  TEST_P(msg_header_offset(msg, sip, sip_min_se), &sip->sip_min_se);   
Pekka Pessi's avatar
Pekka Pessi committed
1451
   
1452 1453 1454 1455 1456 1457
  TEST_P(msg_header_offset(msg, sip, sip_allow), &sip->sip_allow);
  TEST_P(msg_header_offset(msg, sip, sip_require), &sip->sip_require);
  TEST_P(msg_header_offset(msg, sip, sip_proxy_require),
	 &sip->sip_proxy_require);
  TEST_P(msg_header_offset(msg, sip, sip_supported), &sip->sip_supported);
  TEST_P(msg_header_offset(msg, sip, sip_unsupported), &sip->sip_unsupported);
Pekka Pessi's avatar
Pekka Pessi committed
1458 1459 1460 1461 1462 1463 1464
#if SIP_HAVE_ENCRYPTION
  TEST(msg_header_offset(msg, sip, sip_encryption), &sip->sip_encryption);
#endif
#if SIP_HAVE_RESPONSE_KEY
  TEST(msg_header_offset(msg, sip, sip_response_key), &sip->sip_response_key);
#endif

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  TEST_P(msg_header_offset(msg, sip, sip_proxy_authenticate),
	 &sip->sip_proxy_authenticate);
  TEST_P(msg_header_offset(msg, sip, sip_proxy_authorization),
	 &sip->sip_proxy_authorization);
  TEST_P(msg_header_offset(msg, sip, sip_authorization),
	 &sip->sip_authorization);
  TEST_P(msg_header_offset(msg, sip, sip_www_authenticate),
	 &sip->sip_www_authenticate);
  TEST_P(msg_header_offset(msg, sip, sip_error_info), &sip->sip_error_info);
  TEST_P(msg_header_offset(msg, sip, sip_warning), &sip->sip_warning);
Pekka Pessi's avatar
Pekka Pessi committed
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
  TEST_P(msg_header_offset(msg, sip, sip_mime_version), &sip->sip_mime_version);
  TEST_P(msg_header_offset(msg, sip, sip_content_type), &sip->sip_content_type);
  TEST_P(msg_header_offset(msg, sip, sip_content_encoding),
	 &sip->sip_content_encoding);
  TEST_P(msg_header_offset(msg, sip, sip_content_disposition),
	 &sip->sip_content_disposition);
  TEST_P(msg_header_offset(msg, sip, sip_content_length),
	 &sip->sip_content_length);

  TEST_SIZE(sip_request_class->hc_params, 0);
  TEST_SIZE(sip_status_class->hc_params, 0);
  TEST_SIZE(sip_unknown_class->hc_params, 0);
  TEST_SIZE(sip_separator_class->hc_params, 0);
  TEST_SIZE(sip_payload_class->hc_params, 0);
  TEST_SIZE(sip_via_class->hc_params, offsetof(sip_via_t, v_params));
  TEST_SIZE(sip_route_class->hc_params, offsetof(sip_route_t, r_params));
  TEST_SIZE(sip_record_route_class->hc_params, 
	    offsetof(sip_record_route_t, r_params));

  TEST_SIZE(sip_max_forwards_class->hc_params, 0);
  TEST_SIZE(sip_from_class->hc_params, offsetof(sip_from_t, a_params));
  TEST_SIZE(sip_to_class->hc_params, offsetof(sip_to_t, a_params));
  TEST_SIZE(sip_call_id_class->hc_params, 0);
  TEST_SIZE(sip_cseq_class->hc_params, 0);
  TEST_SIZE(sip_contact_class->hc_params, offsetof(sip_contact_t, m_params));
Pekka Pessi's avatar
Pekka Pessi committed
1501
						
1502 1503 1504 1505 1506 1507 1508
  TEST_SIZE(sip_expires_class->hc_params, 0);
  TEST_SIZE(sip_date_class->hc_params, 0);
  TEST_SIZE(sip_retry_after_class->hc_params, 
	    offsetof(sip_retry_after_t, af_params));
  TEST_SIZE(sip_timestamp_class->hc_params, 0);
  TEST_SIZE(sip_subject_class->hc_params, 0);
  TEST_SIZE(sip_priority_class->hc_params, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1509
		           		
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
  TEST_SIZE(sip_call_info_class->hc_params, 
	    offsetof(sip_call_info_t, ci_params));
  TEST_SIZE(sip_organization_class->hc_params, 0);
  TEST_SIZE(sip_server_class->hc_params, 0);
  TEST_SIZE(sip_user_agent_class->hc_params, 0);

  TEST_SIZE(sip_in_reply_to_class->hc_params, 
	    offsetof(sip_in_reply_to_t, k_items));
  TEST_SIZE(sip_accept_class->hc_params, offsetof(sip_accept_t, ac_params));
  TEST_SIZE(sip_accept_encoding_class->hc_params, 
	    offsetof(sip_accept_encoding_t, aa_params));
  TEST_SIZE(sip_accept_language_class->hc_params, 
	    offsetof(sip_accept_language_t, aa_params));
Pekka Pessi's avatar
Pekka Pessi committed
1523
   
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
  TEST_SIZE(sip_session_expires_class->hc_params,
	    offsetof(sip_session_expires_t, x_params));
  TEST_SIZE(sip_min_se_class->hc_params, offsetof(sip_min_se_t, min_params));

  TEST_SIZE(sip_allow_class->hc_params, offsetof(sip_allow_t, k_items));
  TEST_SIZE(sip_require_class->hc_params, offsetof(sip_require_t, k_items));
  TEST_SIZE(sip_proxy_require_class->hc_params, 
	    offsetof(sip_proxy_require_t, k_items));
  TEST_SIZE(sip_supported_class->hc_params, 
	    offsetof(sip_supported_t, k_items));
  TEST_SIZE(sip_unsupported_class->hc_params, 
	    offsetof(sip_unsupported_t, k_items));
Pekka Pessi's avatar
Pekka Pessi committed
1536 1537

#if SIP_HAVE_ENCRYPTION
1538 1539
  TEST_SIZE(sip_encryption_class->hc_params, 
	    offsetof(sip_encryption_t, au_params));
Pekka Pessi's avatar
Pekka Pessi committed
1540 1541
#endif
#if SIP_HAVE_RESPONSE_KEY
1542 1543
  TEST_SIZE(sip_response_key_class->hc_params, 
	    offsetof(sip_response_key_t, au_params));
Pekka Pessi's avatar
Pekka Pessi committed
1544
#endif
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
  TEST_SIZE(sip_proxy_authenticate_class->hc_params,  
	    offsetof(sip_proxy_authenticate_t, au_params));
  TEST_SIZE(sip_proxy_authorization_class->hc_params,  
	    offsetof(sip_proxy_authorization_t, au_params));
  TEST_SIZE(sip_authorization_class->hc_params,  
	    offsetof(sip_authorization_t, au_params));
  TEST_SIZE(sip_www_authenticate_class->hc_params,  
	    offsetof(sip_www_authenticate_t, au_params));

  TEST_SIZE(sip_error_info_class->hc_params, 
	    offsetof(sip_error_info_t, ei_params));
  TEST_SIZE(sip_warning_class->hc_params, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1557
		       
1558 1559 1560 1561 1562 1563 1564 1565
  TEST_SIZE(sip_mime_version_class->hc_params, 0);
  TEST_SIZE(sip_content_type_class->hc_params, 
	    offsetof(sip_content_type_t, c_params));
  TEST_SIZE(sip_content_encoding_class->hc_params, 
	    offsetof(sip_content_encoding_t, k_items));
  TEST_SIZE(sip_content_disposition_class->hc_params, 
	    offsetof(sip_content_disposition_t, cd_params));
  TEST_SIZE(sip_content_length_class->hc_params, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1566 1567 1568

  msg_destroy(msg);

1569
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

  END();
}

static int count(sip_common_t *h)
{
  sip_header_t *sh = (sip_header_t *)h;
  unsigned n;
  
  for (n = 0; sh; sh = sh->sh_next)
    n++;
  
  return n;
}

static int len(sip_common_t *h)
{
  sip_header_t *sh = (sip_header_t *)h;
  unsigned n;

  for (n = 0; sh; sh = sh->sh_next) {
    if (n) n +=2;
    n += sip_header_field_e(NULL, 0, sh, 0);
  }

  return n;
}

static int sip_header_test(void)
{
  msg_t *msg;
  sip_t *sip;
  su_home_t *home;
  void const *x;
  sip_via_t *v, *v0;
1605 1606
  tagi_t const *tl;
  tagi_t *tl0;
Pekka Pessi's avatar
Pekka Pessi committed
1607 1608 1609

  BEGIN();

1610
  home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

  TEST_1(msg = read_message(MSG_DO_EXTRACT_COPY, 
    "MESSAGE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
    "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
    "  ; tag = deadbeef\r\n"
    "From:h<http://www.cs.columbia.edu>\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq : 8 MESSAGE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
    "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
    "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
    "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, <sip:kuik@foo.invalid>, sip:barf\r\n"
    "Via: SIP/2.0/UDP [aa:bb::1]:5061\r\n"
    "Record-Route: Test Element <sip:[defa::00:12]:5061>;param=12+1\r\n"
    "Record-Route: sip:135.180.130.133,<sip:135.180.130.131;transport=tcp>,\r\n"
    "\t,Test Element <sip:[defa::00:12]:5061>;param=12+1\r\n"
    "Path: Test <sip:[defa::00:12]:5061>\r\n"
    "Service-Route: Test <sip:[defa::00:12]:5061>\r\n"
    "Route: ,\r\n"
    "Unknown-Extension: hip\r\n"
    "Hide: hop\r\n"
    "Max-Forwards: 12\r\n"
    "Min-Expires: 150\r\n"
    "Timestamp: 10.010 0.000100\r\n"
1635 1636 1637
    "Suppress-Body-If-Match: humppa \t\r\n"
    "Suppress-Notify-If-Match: zumppa\r\n"
    " \r\n"
Pekka Pessi's avatar
Pekka Pessi committed
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
    "Content-Type: application/sdp\r\n"
    "\r\n"
    "v=0\r\n"
    "o=mhandley 29739 7272939 IN IP4 126.5.4.3\r\n"
    "c=IN IP4 135.180.130.88\r\n"
    "m=audio 492170 RTP/AVP 0 12\r\n"
    "m=video 3227 RTP/AVP 31\r\n"
    "a=rtpmap:31 LPC\r\n"));

  TEST_1(sip = sip_object(msg));

  TEST(count(sip->sip_request->rq_common), 1);
  TEST(count(sip->sip_to->a_common), 1);
  TEST(count(sip->sip_from->a_common), 1);
  TEST(count(sip->sip_cseq->cs_common), 1);
  TEST(count(sip->sip_call_id->i_common), 1);
  TEST(count(sip->sip_via->v_common), 4);
  TEST(count(sip->sip_contact->m_common), 3);
  TEST(count(sip->sip_content_type->c_common), 1);
  TEST(count(sip->sip_route->r_common), 0);
  TEST(count(sip->sip_record_route->r_common), 4);
1659
#if SU_HAVE_EXPERIMENTAL
Pekka Pessi's avatar
Pekka Pessi committed
1660
  TEST(count(sip->sip_unknown->un_common), 2);
1661 1662 1663
#else
  TEST(count(sip->sip_unknown->un_common), 4);
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
  TEST(count(sip->sip_error->er_common), 1);
  TEST(count(sip->sip_max_forwards->mf_common), 1);
  TEST(count(sip->sip_min_expires->me_common), 1);
  TEST(count(sip->sip_timestamp->ts_common), 1);

  TEST_S(sip->sip_contact->m_display, "Joe Bob Briggs");
  TEST_1(sip->sip_contact->m_next->m_display != NULL);
  TEST_S(sip->sip_contact->m_next->m_display, "");
  TEST_1(sip->sip_contact->m_next->m_next->m_display == NULL);

  TEST(sip->sip_max_forwards->mf_count, 12);
  TEST(sip->sip_min_expires->me_delta, 150);

1677
#if SU_HAVE_EXPERIMENTAL
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
  {
    sip_suppress_body_if_match_t *sbim;
    sip_suppress_notify_if_match_t *snim;

    TEST_1(sbim = sip_suppress_body_if_match(sip));
    TEST_S(sbim->sbim_tag, "humppa");

    TEST_SIZE(offsetof(msg_generic_t, g_value),
	      offsetof(sip_suppress_body_if_match_t, sbim_tag));

    TEST_1(snim = sip_suppress_notify_if_match(sip));
    TEST_S(snim->snim_tag, "zumppa");

    TEST_SIZE(offsetof(msg_generic_t, g_value),
	      offsetof(sip_suppress_notify_if_match_t, snim_tag));
  }
1694
#endif
1695

Pekka Pessi's avatar
Pekka Pessi committed
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
  TEST_1(sip->sip_from->a_display);
  TEST_S(sip->sip_from->a_display, "h");

  v0 = sip->sip_via;

  TEST_1(v = sip_via_copy(home, v0));
  TEST(len(v->v_common), len(v0->v_common));
  for (; v && v0; v = v->v_next, v0 = v0->v_next) {
    if (v->v_params)
      TEST_1(v->v_params != v0->v_params);
    if (v->v_branch)
      TEST_1(v->v_branch == v0->v_branch);
  }
  TEST_1(v == NULL && v0 == NULL);
  
  v0 = sip->sip_via;
  
  TEST_1(v = sip_via_dup(home, v0));
  TEST(len(v->v_common), len(v0->v_common));
  for (; v && v0; v = v->v_next, v0 = v0->v_next) {
    if (v->v_params)
      TEST_1(v->v_params != v0->v_params);
    if (v->v_branch)
      TEST_1(v->v_branch != v0->v_branch);
  }
  TEST_1(v == NULL && v0 == NULL);

  TEST(sip_add_dup(msg, sip, (sip_header_t *)sip->sip_max_forwards), 0);
  /* Max-Forwards is last header? */
1725
  TEST_P(sip->sip_max_forwards, sip->sip_content_type->c_common->h_succ);
Pekka Pessi's avatar
Pekka Pessi committed
1726 1727 1728 1729 1730

  TEST(sip_to_tag(home, sip->sip_to, sip->sip_to->a_tag), 0);
  TEST(sip_to_tag(home, sip->sip_to, "tag=deadbeef"), 0);
  TEST(sip_to_tag(home, sip->sip_to, "foofaa"), -1);

Pekka Pessi's avatar
Pekka Pessi committed
1731
  msg_header_remove(msg, (msg_pub_t *)sip, (msg_header_t *)sip->sip_payload);
Pekka Pessi's avatar
Pekka Pessi committed
1732 1733

  TEST(sip_add_tl(msg, sip, 
Pekka Pessi's avatar
Pekka Pessi committed
1734 1735
		  SIPTAG_FROM(SIP_NONE),
		  SIPTAG_VIA(SIP_NONE),
Pekka Pessi's avatar
Pekka Pessi committed
1736 1737 1738 1739 1740 1741 1742 1743
		  SIPTAG_VIA_STR("SIP/2.0/SCTP foo.bar.com:5060;branch=foo"),
		  SIPTAG_TO_STR("<sip:foo@bar>"),
		  SIPTAG_HEADER_STR("Authorization: Basic foobar\n"
				    "Priority:\n urgent"),
		  SIPTAG_HEADER_STR("Accept: foo/bar\n"
				    "\n"
				    "test payload\n"),
		  SIPTAG_TIMESTAMP(sip->sip_timestamp),
1744 1745
		  SIPTAG_END(),
		  SIPTAG_REFER_TO_STR("<sip:foo@bar>"),
Pekka Pessi's avatar
Pekka Pessi committed
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
		  TAG_END()), 0);
  TEST_1(sip->sip_from == NULL);
  TEST_1(sip->sip_via); TEST_1(sip->sip_via->v_next == NULL);
  TEST_S(sip->sip_via->v_protocol, "SIP/2.0/SCTP");
  TEST_1(sip->sip_authorization);
  TEST_1(sip->sip_priority);
  TEST_1(sip->sip_payload);
  TEST_S(sip->sip_payload->pl_data, "test payload\n");
  TEST_1(sip->sip_timestamp);
  TEST_S(sip->sip_timestamp->ts_stamp, "10.010");
  TEST_S(sip->sip_timestamp->ts_delay, "0.000100");
1757 1758 1759 1760 1761 1762 1763 1764
  TEST_1(!sip->sip_refer_to);

  TEST_1(tl = tl0 = tl_list(SIPTAG_TO_STR("<sip:foo@bar>"),
			    SIPTAG_END(),
			    SIPTAG_REFER_TO_STR("<sip:foo@bar>"),
			    TAG_END()));
  /* sip_add_tagis should stop after SIPTAG_END() */
  TEST(sip_add_tagis(msg, sip, &tl), 0);
1765
  TEST_P(tl, tl0 + 2);
Pekka Pessi's avatar
Pekka Pessi committed
1766

1767 1768
  tl_free(tl0);

1769 1770
  TEST_P(sip_timestamp_make(home, "+1"), NULL);
  TEST_P(sip_timestamp_make(home, "1.0e6 13.0"), NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1771
  TEST_1(sip_timestamp_make(home, "1.0 .001"));
1772
  TEST_P(sip_timestamp_make(home, ".0001 13.0"), NULL);
Pekka Pessi's avatar
Pekka Pessi committed
1773 1774 1775

  TEST_1(x = sip->sip_path);
  TEST_1(sip_add_make(msg, sip, sip_path_class, "<sip:135.180.130.133>") == 0);
1776
  TEST_P(x, sip->sip_path->r_next);
Pekka Pessi's avatar
Pekka Pessi committed
1777 1778 1779 1780

  TEST_1(x = sip->sip_service_route);
  TEST_1(sip_add_make(msg, sip, sip_service_route_class, 
		      "<sip:135.180.130.133>") == 0);
1781
  TEST_P(x, sip->sip_service_route);
Pekka Pessi's avatar
Pekka Pessi committed
1782 1783 1784 1785 1786 1787 1788 1789
  TEST_1(sip->sip_service_route->r_next);

  /* Detect parsing errors */
  TEST_1(!sip_cseq_make(home, "21874624876976 INVITE"));
  TEST_1(!sip_cseq_make(home, "218746INVITE"));
  
  msg_destroy(msg), msg = NULL;

1790
  su_home_unref(home), home = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802

  END();
}

static int test_bad_packet(void)
{
  msg_t *msg;
  sip_t *sip;
  su_home_t *home;

  BEGIN();

1803
  home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

  TEST_1(msg = read_message(MSG_DO_EXTRACT_COPY, 
    "MESSAGE <sip:John_Smith@tct.hut.fi> SIP/2.0\r\n"
    "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
    "  ; tag = deadbeef\r\n"
    "From:h<http://www.cs.columbia.edu>\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq : 8 MESSAGE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
    "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
    "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
    "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, sip:kuik@foo.invalid\r\n"
    "Via: SIP/2.0/UDP [aa:bb::1]:5061\0\0"));

  TEST_1(sip = sip_object(msg));

  TEST(count(sip->sip_request->rq_common), 1);
  TEST(count(sip->sip_to->a_common), 1);
  TEST(count(sip->sip_from->a_common), 1);
  TEST(count(sip->sip_cseq->cs_common), 1);
  TEST(count(sip->sip_call_id->i_common), 1);
  TEST(count(sip->sip_via->v_common), 4);
  TEST(count(sip->sip_route->r_common), 0);

  TEST(sip->sip_request->rq_url->url_type, url_invalid);

1830
  su_home_unref(home), home = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845

  msg_destroy(msg), msg = NULL;

  END();
}

static int test_sip_list_header(void)
{
  msg_t *msg;
  sip_t *sip;
  su_home_t *home;
  sip_allow_t *a;

  BEGIN();

1846
  home = su_home_new(sizeof *home);
Pekka Pessi's avatar
Pekka Pessi committed
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863

  TEST_1(msg = read_message(0, 
    "MESSAGE sip:John_Smith@tct.hut.fi SIP/2.0\r\n"
    "To: John Smith <sip:John_Smith@tct.hut.fi:5066;user=ip;maddr=131.228.16.2>\r\n"
    "From: <sip:joe@doe.org>;tag=foobar\r\n"
    "Call-ID: 0ha0isndaksdj@10.1.2.3\r\n"
    "CSeq : 8 MESSAGE\r\n"
    "Via: SIP/2.0/UDP 135.180.130.133;received=defa:daf::00:12\r\n"
    "Via: SIP/2.0/TCP 135.180.130.131;branch=deadbeef.barf;ttl=3;hidden,,"
    "SIP/2.0/UDP\r\n 135.180.130.131:5061;received=[defa::00:12]\r\n"
    "Contact: Joe Bob Briggs <urn:ipaddr:122.1.2.3> ; bar=\042foo baa\042, <sip:kuik@foo.invalid>, sip:barf\r\n"
    "Allow: INVITE\r\n"
    "Allow: ACK\r\n"
    "Allow: CANCEL\r\n"
    "Allow: BYE\r\n"
    "Allow: OPTIONS\r\n"
    "Allow: MESSAGE\r\n"
1864
    "Allow: KUIK\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    "Max-Forwards: 12\r\n"
    "Content-Type: text/plain\r\n"
    "\r\n"
    "hello\r\n"));

  TEST_1(sip = sip_object(msg));
  TEST_1(a = sip->sip_allow);
  TEST_1(a->k_items);
  TEST_1(a->k_next == NULL);

1875 1876 1877 1878
  TEST_1(sip_is_allowed(a, SIP_METHOD_INVITE));
  TEST_1(!sip_is_allowed(a, SIP_METHOD_PUBLISH));
  TEST_1(sip_is_allowed(a, SIP_METHOD(KUIK)));
  TEST_1(!sip_is_allowed(a, SIP_METHOD(kuik)));
Pekka Pessi's avatar
Pekka Pessi committed
1879 1880 1881 1882

  TEST_1(a = sip_allow_make(home, ""));
  TEST_S(sip_header_as_string(home, (void *)a), "");

1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
  TEST_1(a = sip_allow_make(home, "INVITE, PUBLISH"));

  TEST_1(sip_is_allowed(a, SIP_METHOD_INVITE));

  /* Test with list header */
  TEST_1(msg_header_add_dup(msg, NULL, (msg_header_t *)a) == 0);

  TEST_1(a = sip_allow_make(home, "MESSAGE, SUBSCRIBE"));

  TEST_1(msg_header_add_dup(msg, NULL, (msg_header_t *)a) == 0);

  TEST_1(msg_header_add_make(msg, NULL, sip_allow_class, "kuik") == 0);

  TEST_1(a = sip->sip_allow);
  TEST_1(a->k_items);
  TEST_S(a->k_items[0], "INVITE");
  TEST_S(a->k_items[1], "ACK");
  TEST_S(a->k_items[2], "CANCEL");
  TEST_S(a->k_items[3], "BYE");
  TEST_S(a->k_items[4], "OPTIONS");
  TEST_S(a->k_items[5], "MESSAGE");
  TEST_S(a->k_items[6], "KUIK");
  TEST_S(a->k_items[7], "PUBLISH");
  TEST_S(a->k_items[8], "SUBSCRIBE");
  TEST_S(a->k_items[9], "kuik");
  TEST_P(a->k_items[10], NULL);

  msg_destroy(msg), msg = NULL;

1912
  su_home_unref(home), home = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935

  END();
}

static int test_prack(void)
{
  /* Test RAck and RSeq */
  su_home_t *home;
  sip_rack_t *rack, *rack0;
  sip_rseq_t *rseq, *rseq0;
  
  BEGIN();

  TEST_1(home = su_home_create());
  TEST_1(rack = sip_rack_make(home, "1 2 INVITE"));
  TEST(rack->ra_response, 1);
  TEST(rack->ra_cseq, 2);
  TEST(rack->ra_method, sip_method_invite);
  TEST_S(rack->ra_method_name, "INVITE");
  TEST_1(rseq = sip_rseq_make(home, "3"));
  TEST(rseq->rs_response, 3);

  TEST_1(rack0 = sip_rack_dup(home, rack));