su_tag_test.c 12.8 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 41
 * 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
 *
 */

/**@SU_TAG
 * 
 * @file su_tag_test.c
 *
 * Testing functions for su_tag module.
 *
 * @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 <stdlib.h>

42 43 44 45
#include <sofia-sip/su_tag.h>
#include <sofia-sip/su_tag_io.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48 49 50 51 52 53 54 55 56

#ifndef WIN32
#define DEVNULL "/dev/null"
#else
#define DEVNULL "nul"
#endif 

int tstflags = 0;

#define TSTFLAGS tstflags

57
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
58 59 60 61 62 63 64 65 66 67 68 69 70

#define TAG_A(s)      tag_a, tag_str_v((s))
#define TAG_A_REF(s)  tag_a_ref, tag_str_vr(&(s))
#define TAG_B(s)      tag_b, tag_str_v((s))
#define TAG_B_REF(s)  tag_b_ref, tag_str_vr(&(s))

#define TAG_I(i)      tag_i, tag_int_v((i))
#define TAG_I_REF(i)  tag_i_ref, tag_int_vr(&(i))
#define TAG_J(i)      tag_j, tag_int_v((i))
#define TAG_J_REF(i)  tag_j_ref, tag_int_vr(&(i))
#define TAG_K(i)      tag_k, tag_int_v((i))
#define TAG_K_REF(i)  tag_k_ref, tag_int_vr(&(i))

71 72
#define TAG_ANY_PQ()  tag_any_pq, (tag_value_t)0

Pekka Pessi's avatar
Pekka Pessi committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#define TAG_P(i)      tag_p, tag_bool_v((i))
#define TAG_P_REF(i)  tag_p_ref, tag_bool_vr(&(i))
#define TAG_Q(i)      tag_q, tag_bool_v((i))
#define TAG_Q_REF(i)  tag_q_ref, tag_bool_vr(&(i))

char const *name = "su_tag_test";

static int test_assumptions();
static int test_stackargs(int i, ...);
static int test_dup(void);
static int test_filters(void);
static int test_print(void);
static int test_tagargs(void);
static int test_gets(void);
static int test_scan(void);

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

#if HAVE_WIN32
typedef struct tag_type_s tag_typedef_win_t[1];

tag_typedef_win_t tag_a, tag_a_ref, tag_b, tag_b_ref;
tag_typedef_win_t tag_i, tag_i_ref, tag_j, tag_j_ref;
tag_typedef_win_t tag_k, tag_k_ref, tag_n, tag_n_ref;
102
tag_typedef_win_t tag_any_pq;
Pekka Pessi's avatar
Pekka Pessi committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
tag_typedef_win_t tag_p, tag_p_ref, tag_q, tag_q_ref;

#else
tag_typedef_t tag_a = STRTAG_TYPEDEF(a);
tag_typedef_t tag_a_ref = REFTAG_TYPEDEF(tag_a);

tag_typedef_t tag_b = STRTAG_TYPEDEF(b);
tag_typedef_t tag_b_ref = REFTAG_TYPEDEF(tag_b);

tag_typedef_t tag_i = INTTAG_TYPEDEF(i);
tag_typedef_t tag_i_ref = REFTAG_TYPEDEF(tag_i);

tag_typedef_t tag_j = INTTAG_TYPEDEF(j);
tag_typedef_t tag_j_ref = REFTAG_TYPEDEF(tag_j);

tag_typedef_t tag_k = INTTAG_TYPEDEF(k);
tag_typedef_t tag_k_ref = REFTAG_TYPEDEF(tag_k);

tag_typedef_t tag_n = INTTAG_TYPEDEF(n);
tag_typedef_t tag_n_ref = REFTAG_TYPEDEF(tag_n);

124 125 126 127 128
#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"

tag_typedef_t tag_any_pq = NSTAG_TYPEDEF(*);

Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
tag_typedef_t tag_p = BOOLTAG_TYPEDEF(p);
tag_typedef_t tag_p_ref = REFTAG_TYPEDEF(tag_p);

tag_typedef_t tag_q = BOOLTAG_TYPEDEF(q);
tag_typedef_t tag_q_ref = REFTAG_TYPEDEF(tag_q);

#endif

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

#if HAVE_WIN32
  tag_typedef_t _tag_a = STRTAG_TYPEDEF(a);
  tag_typedef_t _tag_a_ref = REFTAG_TYPEDEF(tag_a);

  tag_typedef_t _tag_b = STRTAG_TYPEDEF(b);
  tag_typedef_t _tag_b_ref = REFTAG_TYPEDEF(tag_b);
 
  tag_typedef_t _tag_i = INTTAG_TYPEDEF(i);
  tag_typedef_t _tag_i_ref = REFTAG_TYPEDEF(tag_i);
 
  tag_typedef_t _tag_j = INTTAG_TYPEDEF(j);
  tag_typedef_t _tag_j_ref = REFTAG_TYPEDEF(tag_j);
 
  tag_typedef_t _tag_k = INTTAG_TYPEDEF(k);
  tag_typedef_t _tag_k_ref = REFTAG_TYPEDEF(tag_k);
  
  tag_typedef_t _tag_n = INTTAG_TYPEDEF(n);
  tag_typedef_t _tag_n_ref = REFTAG_TYPEDEF(tag_n);
160 161 162

#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"
Pekka Pessi's avatar
Pekka Pessi committed
163
  
164 165
  tag_typedef_t _tag_any_pq = NSTAG_TYPEDEF(*);

Pekka Pessi's avatar
Pekka Pessi committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  tag_typedef_t _tag_p = BOOLTAG_TYPEDEF(p);
  tag_typedef_t _tag_p_ref = REFTAG_TYPEDEF(tag_p);
  
  tag_typedef_t _tag_q = BOOLTAG_TYPEDEF(q);
  tag_typedef_t _tag_q_ref = REFTAG_TYPEDEF(tag_q);

  *(struct tag_type_s *)tag_a = *_tag_a;
  *(struct tag_type_s *)tag_a_ref = *_tag_a_ref;
  *(struct tag_type_s *)tag_b = *_tag_b;
  *(struct tag_type_s *)tag_b_ref = *_tag_b_ref;
  *(struct tag_type_s *)tag_i = *_tag_i;
  *(struct tag_type_s *)tag_i_ref = *_tag_i_ref;
  *(struct tag_type_s *)tag_j = *_tag_j;
  *(struct tag_type_s *)tag_j_ref = *_tag_j_ref;
  *(struct tag_type_s *)tag_k = *_tag_k;
  *(struct tag_type_s *)tag_k_ref = *_tag_k_ref;
  *(struct tag_type_s *)tag_n = *_tag_n;
  *(struct tag_type_s *)tag_n_ref = *_tag_n_ref;
184
  *(struct tag_type_s *)tag_any_pq = *_tag_any_pq;
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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
  *(struct tag_type_s *)tag_p = *_tag_p;
  *(struct tag_type_s *)tag_p_ref = *_tag_p_ref;
  *(struct tag_type_s *)tag_q = *_tag_q;
  *(struct tag_type_s *)tag_q_ref = *_tag_q_ref;
#endif

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

  retval |= test_assumptions();
  retval |= test_stackargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  retval |= test_dup();
  retval |= test_filters();
  retval |= test_print();
  retval |= test_tagargs();
  retval |= test_gets();
  retval |= test_scan();

  return retval;
}

static int test_assumptions(void)
{
  tagi_t tags[2], *tagi;

  BEGIN();

  TEST_1(sizeof (tag_value_t) >= sizeof (void *));

  /* First some pointer arithmetics - this is always true, right? */
  tagi = tags;
  TEST(tagi + 1, tags + 1);
  tagi = (tagi_t *)(sizeof(tagi_t) + (char *)tagi);
  TEST(tagi, tags + 1);

  END();
}

#if SU_HAVE_TAGSTACK

/* Automake thing:

###
### Test if we have stack suitable for handling tags directly
###
AC_TRY_RUN( [
#include <stdarg.h>

int test1(int l, int h, ...)
{
  va_list ap;
  int i, *p = &l;

  va_start(ap, h);

  if (*p++ != l || *p++ != h) return 1;

  for (i = l; i <= h; i++) {
    if (*p++ != i)
      return 1;
  }

  for (i = l; i <= h; i++) {
    if (va_arg(ap, int) != i)
      return 1;
  }

  va_end(ap);

  return 0;
}

int main(int avc, char *av[])
{
  return test1(1, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
], 
AC_SU_DEFINE(SU_HAVE_TAGSTACK, 1, [
Define this as 1 if your compiler puts the variable argument list nicely in memory]), 
dnl SU_HAVE_TAGSTACK=0 
dnl SU_HAVE_TAGSTACK=0
)

*/

static int test_stackargs(int l, ...)
{
  va_list ap;  
  int i, *p;

  BEGIN();

  va_start(ap, l);
  
  p = &l;

  for (i = l; i <= 10; i++) {
    TEST(*p++, i);
  }

  for (i = l + 1; i <= 10; i++) {
    TEST(va_arg(ap, int), i);
  }

  va_end(ap);

  END();
}
#else
static int test_stackargs(int l, ...) { return 0; }
#endif

301
/** Test tl_list, tl_llist and tl_dup */
Pekka Pessi's avatar
Pekka Pessi committed
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
static int test_dup(void)
{
  tagi_t const rest[] = {{ TAG_A("Foo") }, { TAG_NULL() }};
  tagi_t *lst, *dup;

  BEGIN();

  lst = tl_list(TAG_A("Moro"), 
		TAG_A("Vaan"), 
		TAG_I(1), 
		TAG_SKIP(2), 
		TAG_NEXT(rest));

  TEST(lst[0].t_tag, tag_a);
  TEST(lst[1].t_tag, tag_a);
  TEST(lst[2].t_tag, tag_i);
  TEST(lst[3].t_tag, tag_skip);
  TEST(lst[4].t_tag, tag_next);

  TEST(tl_len(lst), 5 * sizeof(tagi_t));
  TEST(tl_xtra(lst, 0), strlen("Moro" "Vaan" "Foo") + 3);

  dup = tl_adup(NULL, lst);

  TEST0(dup != NULL);
  TEST(tl_len(dup), 5 * sizeof(tagi_t));
  TEST(tl_xtra(dup, 0), strlen("Moro" "Vaan" "Foo") + 3);

  su_free(NULL, dup);
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

  dup = tl_llist(TAG_B("Moi"), TAG_NEXT(lst));

  TEST(dup[0].t_tag, tag_b);
  TEST(dup[1].t_tag, tag_a);
  TEST(dup[2].t_tag, tag_a);
  TEST(dup[3].t_tag, tag_i);
  TEST(dup[4].t_tag, tag_a);
  TEST(dup[5].t_tag, NULL);

  su_free(NULL, dup);

  dup = tl_llist(TAG_NEXT(NULL));
  TEST(dup[0].t_tag, 0);
  su_free(NULL, dup);

  dup = tl_llist(TAG_END());
  TEST(dup[0].t_tag, 0);
  su_free(NULL, dup);

  dup = tl_llist(TAG_SKIP(1), TAG_NEXT(lst + 4));
  TEST(dup[0].t_tag, tag_a);
  TEST(dup[1].t_tag, 0);
  su_free(NULL, dup);

Pekka Pessi's avatar
Pekka Pessi committed
356 357 358 359 360 361 362 363 364 365
  tl_vfree(lst);

  END();
}

/* Test tl_afilter() */
static int test_filters(void)
{
  tagi_t *lst, *filter1, *filter2, *filter3, *filter4, *b1, *b2, *b3, *b4;

366 367
  tagi_t *nsfilter, *b5;

Pekka Pessi's avatar
Pekka Pessi committed
368 369 370
  BEGIN();

  lst = tl_list(TAG_A("Moro"), 
371 372
		TAG_I(2),
		TAG_Q(3),
Pekka Pessi's avatar
Pekka Pessi committed
373 374 375
		TAG_SKIP(2), 
		TAG_A("Vaan"), 
		TAG_I(1), 
376
		TAG_P(2),
Pekka Pessi's avatar
Pekka Pessi committed
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
		TAG_NULL());

  filter1 = tl_list(TAG_A(""), TAG_NULL());
  filter2 = tl_list(TAG_I(0), TAG_NULL());
  filter3 = tl_list(TAG_A(""), TAG_I(0), TAG_NULL());
  filter4 = tl_list(TAG_ANY(), TAG_NULL());

  TEST0(lst && filter1 && filter2 && filter3);

  b1 = tl_afilter(NULL, filter1, lst);

  TEST(tl_len(b1), 3 * sizeof(tagi_t));
  TEST(tl_xtra(b1, 0), strlen("Moro" "Vaan") + 2);

  b2 = tl_afilter(NULL, filter2, lst);

  TEST(tl_len(b2), 3 * sizeof(tagi_t));
  TEST(tl_xtra(b2, 0), 0);

  b3 = tl_afilter(NULL, filter3, lst);

  TEST(tl_len(b3), 5 * sizeof(tagi_t));
  TEST(tl_xtra(b3, 0), strlen("Moro" "Vaan") + 2);

  b4 = tl_afilter(NULL, filter4, lst);

403
  TEST(tl_len(b4), 7 * sizeof(tagi_t));
Pekka Pessi's avatar
Pekka Pessi committed
404 405 406
  TEST(tl_xtra(b4, 0), strlen("Moro" "Vaan") + 2);

  su_free(NULL, b1); su_free(NULL, b2); su_free(NULL, b3); su_free(NULL, b4);
407 408

  nsfilter = tl_list(TAG_ANY_PQ(), TAG_END());
Pekka Pessi's avatar
Pekka Pessi committed
409
  
410 411 412 413 414 415 416
  b5 = tl_afilter(NULL, nsfilter, lst);
  TEST(tl_len(b5), 3 * sizeof(tagi_t));
  TEST(tl_xtra(b5, 0), 0);

  TEST(b5[0].t_tag, tag_q);
  TEST(b5[1].t_tag, tag_p);

Pekka Pessi's avatar
Pekka Pessi committed
417 418 419 420 421 422 423 424 425
  tl_vfree(filter1); tl_vfree(filter2); tl_vfree(filter3); tl_vfree(filter4);
  tl_vfree(lst);

  END();
}

/* Test tl_print */
static int test_print(void)
{
426 427
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
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 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 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 523 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
  tagi_t *lst;
  FILE *out;

  lst = tl_list(TAG_A("Moro"), 
		TAG_I(2), 
		TAG_J(3),
		TAG_K(5),
		TAG_SKIP(2), 
		TAG_A("Vaan"), 
		TAG_B("b"),
		TAG_I(1), 
		TAG_NULL());

  if (tstflags & tst_verbatim)
    out = stdout;
  else
    out = fopen(DEVNULL, "w");

  if (out) {
    tl_print(out, "test_print: ", lst);

    if ((tstflags & tst_verbatim) == 0)
      fclose(out);
  }

  END();
}

static int test_tagargs2(tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  tagi_t const *t;

  BEGIN();

  ta_start(ta, tag, value);

  t = ta_args(ta);
  TEST(t->t_tag, tag_a);
  TEST_S((char *)t->t_value, "a");

  t = tl_next(t);
  TEST(t->t_tag, tag_b);
  TEST_S((char *)t->t_value, "b");

  ta_end(ta);

  END();
}

/* Test tagargs) */
static int test_tagargs(void)
{
  return test_tagargs2(TAG_A("a"),
		       TAG_B("b"),
		       TAG_I(1),
		       TAG_J(2),
		       TAG_K(3),
		       TAG_P(0),
		       TAG_Q(-1),
		       TAG_END());
}

/* Test tl_tgets() and tl_gets() */
static int test_gets(void)
{
  tagi_t *lst;
  char const *a = "B", *b = "A";
  int i = 1, j = 0, k = 0, p = -1;

  BEGIN();

  lst = tl_list(TAG_A("Moro"), 
		TAG_I(2), 
		TAG_J(3),
		TAG_K(5),
		TAG_P(3),
		TAG_SKIP(2), 
		TAG_A("Vaan"), 
		TAG_B("b"),
		TAG_I(1), 
		TAG_NULL());

  TEST_1(lst);

  TEST(tl_gets(lst, 
	       TAG_A_REF(a),
	       TAG_B_REF(b),
	       TAG_I_REF(i),
	       TAG_J_REF(j),
	       TAG_K_REF(k),
	       TAG_P_REF(p),
	       TAG_END()),
       6);

  /* tl_gets() semantics have changed */
#if 0
  TEST_S(a, "Moro");
  TEST(i, 2);
#else
  TEST_S(a, "Vaan");
  TEST(i, 1);
#endif
  TEST(j, 3);
  TEST(k, 5);
  TEST(p, 1);
  TEST_S(b, "b");

  lst = tl_list(TAG_A_REF(a),
		TAG_I_REF(i),
		TAG_NULL());
  
  TEST(tl_tgets(lst, TAG_A("Foo"), TAG_I(-1), TAG_END()), 2);

  TEST_S(a, "Foo");
  TEST(i, -1);

  END();
}

static int test_scan(void)
{
  tag_value_t v = 0;

  BEGIN();

  TEST(t_scan(tag_a, NULL, NULL, &v), -1); /* Invalid case */

  TEST(t_scan(tag_a, NULL, "foo", &v), 1);
  TEST_S((char *)v, "foo");
  su_free(NULL, (void *)v);

  TEST(t_scan(tag_i, NULL, "", &v), -1); /* Invalid case */
  TEST(t_scan(tag_i, NULL, "kukkuu-reset", &v), -1); /* Invalid case */

  TEST(t_scan(tag_i, NULL, "-1234", &v), 1);
  TEST(v, -1234);

  TEST(t_scan(tag_n, NULL, "", &v), -1); /* Invalid case */

  TEST(t_scan(tag_n, NULL, "3243432", &v), 1);
  TEST(v, 3243432);

  TEST(t_scan(tag_p, NULL, "kukkuu-reset", &v), -1); /* Invalid case */
  TEST(t_scan(tag_p, NULL, "", &v), -1); /* Invalid case */

  TEST(t_scan(tag_p, NULL, "true", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "true ", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "  134  ", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "false   \n\t", &v), 1);
  TEST(v, 0);

  TEST(t_scan(tag_p, NULL, "  000.000  ", &v), 1);
  TEST(v, 0);

589 590 591
  TEST(t_scan(tag_null, NULL, "NULL", &v), -2);
  TEST(t_scan(tag_skip, NULL, "SKIP", &v), -2);
  TEST(t_scan(tag_any, NULL, "any", &v), -2);
Pekka Pessi's avatar
Pekka Pessi committed
592 593 594 595 596 597 598 599

  TEST(t_scan(NULL, NULL, "-1234", &v), -1);
  TEST(t_scan(tag_a, NULL, NULL, &v), -1);
  TEST(t_scan(tag_a, NULL, "-1234", NULL), -1);

  END();
}