torture_sdp.c 25 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
 * 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
 *
 */

/**@internal
 *
27
 * @CFILE sdp_torture.c
Pekka Pessi's avatar
Pekka Pessi committed
28
 *
29
 * Torture testing sdp module.
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35 36 37 38 39 40 41
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 *
 * @date Created: Tue Mar  6 18:33:42 2001 ppessi
 */

#include "config.h"

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

44
#include <sofia-sip/su_types.h>
45
#include <sofia-sip/su_string.h>
Pekka Pessi's avatar
Pekka Pessi committed
46

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

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

53
#define TSTFLAGS tstflags
Pekka Pessi's avatar
Pekka Pessi committed
54

55
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
56

57 58 59
int tstflags;

char const *name = "torture_sdp.c";
Pekka Pessi's avatar
Pekka Pessi committed
60 61 62

FILE *null;

63
static char const e0_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
64 65
"foo";

66
static char const e1_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
67 68 69 70 71 72 73 74 75 76 77 78 79
    "v=1\n"
    "s=/sdp_torture\n"
    "o=sdp_torture 0 0 IN IP4 0.0.0.0\n"
    "m=audio 0 RTP/AVP 96 97 98 10 99 8 0\n"
    "a=rtpmap:96 X-AMR-WB/16000\n"
    "a=rtpmap:97 X-AMR/8000\n"
    "a=rtpmap:98 GSM-EFR/8000\n"
    "a=rtpmap:10 L16/16000\n"
    "a=rtpmap:99 G723/8000\n"
    "a=rtpmap:8 PCMA/8000\n"
    "a=rtpmap:0 PCMU/8000\n"
    "m=video 0 *";

80
static int test_error(void)
Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
  su_home_t *home = su_home_create();
  sdp_parser_t *parser;

  BEGIN();

  su_home_check(home); TEST0(home);

  TEST_1((parser = sdp_parse(home, e0_msg, sizeof(e0_msg), 0)));
  TEST_1(sdp_session(parser) == NULL);
  TEST_1(sdp_parsing_error(parser));
  sdp_parser_free(parser);

  TEST_1((parser = sdp_parse(home, e1_msg, sizeof(e1_msg), 0)));
  TEST_1(sdp_session(parser) == NULL);
  TEST_1(sdp_parsing_error(parser));
  sdp_parser_free(parser);


  /* destroy the home objects */
  su_home_check(home); su_home_zap(home);

  END();
}

106
static char const s0_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    "v=0\n"
    "s=/sdp_torture\n"
    "o=sdp_torture 0 0 IN IP4 0.0.0.0\n"
    "m=audio 0 RTP/AVP 96 97 98 10 99 8 0\n"
    "a=rtpmap:96 X-AMR-WB/16000\n"
    "a=rtpmap:97 X-AMR/8000\n"
    "a=rtpmap:98 GSM-EFR/8000\n"
    "a=rtpmap:10 L16/16000\n"
    "a=rtpmap:99 G723/8000\n"
    "a=rtpmap:8 PCMA/8000\n"
    "a=rtpmap:0 PCMU/8000\n"
    "m=video 0 *\n"
    "m=* 0 RTP/AVP *\n"
  ;

122
static int test_session(void)
Pekka Pessi's avatar
Pekka Pessi committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
  su_home_t *home = su_home_create(), *home2 = su_home_create();
  sdp_session_t *sdp_src, *sdp_target;
  sdp_session_t const *sdp = NULL;
  sdp_parser_t *parser;
  sdp_printer_t *printer;
  sdp_media_t *m;
  char buffer[512];
  tagi_t *lst, *dup;

  BEGIN();

  su_home_check(home);
  TEST_1(home);

  su_home_check(home2);
  TEST_1(home2);
140

Pekka Pessi's avatar
Pekka Pessi committed
141 142 143 144
  TEST_1((parser = sdp_parse(home, s0_msg, sizeof(s0_msg), sdp_f_config)));
  TEST_1((sdp_src = sdp_session(parser)));
  TEST_1(sdp_src->sdp_media);
  TEST_1(sdp_src->sdp_media->m_session);
145
  TEST_P(sdp_src->sdp_media->m_session, sdp_src);
Pekka Pessi's avatar
Pekka Pessi committed
146 147 148 149

  /* clone the session using 'home2' */
  TEST_1((sdp_target = sdp_session_dup(home2, sdp_src)));

150 151 152
  /* Check comparing */
  TEST(sdp_session_cmp(sdp_src, sdp_target), 0);

Pekka Pessi's avatar
Pekka Pessi committed
153 154 155 156 157 158 159 160 161 162 163 164 165
  /* check the cloned session */
  TEST_1(sdp_target->sdp_subject);
  TEST_S(sdp_target->sdp_subject, "/sdp_torture");
  strcpy((char*)sdp_target->sdp_subject, "garbage");
  TEST_S(sdp_src->sdp_subject, "/sdp_torture");

  TEST_1(sdp_target->sdp_origin);
  TEST_1(sdp_target->sdp_origin->o_username);
  TEST_S(sdp_target->sdp_origin->o_username, "sdp_torture");
  strcpy((char*)sdp_target->sdp_origin->o_username, "garbage");
  TEST_S(sdp_src->sdp_origin->o_username, "sdp_torture");

  TEST_1(m = sdp_target->sdp_media);
166
  TEST_P(m->m_session, sdp_target);
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  TEST_1(sdp_src->sdp_media->m_session != sdp_target->sdp_media->m_session);

  TEST(m->m_type, sdp_media_audio);
  TEST_S(m->m_type_name, "audio");
  TEST(m->m_port, 0);
  TEST(m->m_number_of_ports, 0);
  TEST(m->m_proto, sdp_proto_rtp);
  TEST_S(m->m_proto_name, "RTP/AVP");
  /* FIXME: add more tests */

  /* frees all data created by the parser including 'sdp_src' */
  sdp_parser_free(parser);

  /* destroy the first home instance */
  su_home_check(home);
182
  su_home_unref(home);
183

Pekka Pessi's avatar
Pekka Pessi committed
184 185 186 187 188
  /* access all cloned data by printing it */
  printer = sdp_print(home2, sdp_target, buffer, sizeof(buffer), 0);
  if (printer != NULL) {
    char const *msg = sdp_message(printer);

189
    if (tstflags & tst_verbatim) {
Pekka Pessi's avatar
Pekka Pessi committed
190 191
      printf("sdp_torture.c: parsed SDP message:\"%s\".\n", msg);
    }
192

Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197 198 199
    sdp_printer_free(printer);
  }

  TEST_1(lst = tl_list(SDPTAG_SESSION(sdp_target), TAG_NULL()));

  TEST_1(dup = tl_adup(home2, lst));

200
  if (tstflags & tst_verbatim)
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204 205 206 207 208 209 210
    tl_print(stdout, "dup:\n", dup);
  else
    tl_print(null, "dup:\n", dup);

  TEST(tl_gets(dup, SDPTAG_SESSION_REF(sdp), TAG_END()), 1);

  /* access all copied data by printing it */
  printer = sdp_print(home2, sdp, buffer, sizeof(buffer), 0);
  if (printer != NULL) {
    char const *msg = sdp_message(printer);
211
    if (tstflags & tst_verbatim) {
Pekka Pessi's avatar
Pekka Pessi committed
212 213 214 215 216 217 218 219 220 221 222
      printf("sdp_torture.c: "
	     "SDP message passed through tag list:\n\"%s\".\n", msg);
    }
    sdp_printer_free(printer);
  }

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

  /* destroy the second home object */
  su_home_check(home2);
223
  su_home_unref(home2);
Pekka Pessi's avatar
Pekka Pessi committed
224 225 226 227

  END();
}

228
static char const s1_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
229 230 231 232 233 234
  "v=0\r\n"
  "o=- 2435697 2435697 IN IP4 172.21.137.44\r\n"
  "s=-\r\n"
  "c=IN IP4 172.21.137.44\r\n"
  "t=0 0\r\n"
  "a=sendonly\r\n"
235
  "m=video 49154 RTP/AVP 96 24 25 26 28 31 32 33 34\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
236
  "a=rtpmap:96 H263-1998/90000\r\n"
237
  "m=audio 49152 RTP/AVP 97 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240 241
  "a=rtpmap 97 AMR/8000\r\n"
  "a=fmtp:97 mode-set=\"0,1,2,3,4\"\r\n"
  "a=ptime:400\r\n";

242
static char const s2_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  "v=0\r\n"
  "o=- 308519342 2 IN IP4 172.168.1.55\r\n"
  "s=-\r\n"
  "c=IN IP4 172.168.1.55\r\n"
  "t=0 0\r\n"
  "a=recvonly\r\n"
  "m=video 59154 RTP/AVP 96\r\n"
  "b=AS:64\r\n"
  "a=rtpmap:96 H263-1998/90000\r\n"
  "a=framerate:8\r\n"
  "a=fmtp:96 QCIF=4\r\n"
  "m=audio 59152 RTP/AVP 97\r\n"
  "b=AS:8\r\n"
  "a=rtpmap:97 AMR/8000\r\n"
  "a=fmtp:97 mode-set=\"0\"\r\n"
  "a=maxptime:500\r\n";

260
static int test_session2(void)
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265
{
  su_home_t *home = su_home_create();
  sdp_session_t const *sdp = NULL;
  sdp_parser_t *parser;
  sdp_media_t *m;
266
  sdp_rtpmap_t *rm;
Pekka Pessi's avatar
Pekka Pessi committed
267 268 269 270 271 272 273 274 275

  BEGIN();

  su_home_check(home); TEST_1(home);

  TEST_1((parser = sdp_parse(home, s1_msg, sizeof(s1_msg), 0)));
  TEST_1((sdp = sdp_session(parser)));
  TEST_1(m = sdp->sdp_media);
  TEST(m->m_mode, sdp_sendonly);
276
  TEST_P(m->m_session, sdp);
277 278 279 280 281 282 283
  TEST_1(rm = m->m_rtpmaps);
  TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "H263-1998");
  TEST(rm->rm_rate, 90000);

  {
#define RTPMAP(pt, type, rate, params) \
284
    { sizeof(sdp_rtpmap_t), NULL, type, rate, (char *)params, NULL, 1, pt, 0 }
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    /* rtpmaps for well-known video codecs */
    static sdp_rtpmap_t const
      sdp_rtpmap_celb = RTPMAP(25, "CelB", 90000, 0),
      sdp_rtpmap_jpeg = RTPMAP(26, "JPEG", 90000, 0),
      sdp_rtpmap_nv = RTPMAP(28, "nv", 90000, 0),
      sdp_rtpmap_h261 = RTPMAP(31, "H261", 90000, 0),
      sdp_rtpmap_mpv = RTPMAP(32, "MPV", 90000, 0),
      sdp_rtpmap_mp2t = RTPMAP(33, "MP2T", 90000, 0),
      sdp_rtpmap_h263 = RTPMAP(34, "H263", 90000, 0);


    TEST_1(rm = rm->rm_next);
    TEST_S(rm->rm_encoding, ""); TEST(rm->rm_rate, 0);
    TEST_1(rm = rm->rm_next);
300
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_celb, rm), &sdp_rtpmap_celb);
301
    TEST_1(rm = rm->rm_next);
302
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_jpeg, rm), &sdp_rtpmap_jpeg);
303
    TEST_1(rm = rm->rm_next);
304
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_nv, rm), &sdp_rtpmap_nv);
305
    TEST_1(rm = rm->rm_next);
306
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_h261, rm), &sdp_rtpmap_h261);
307
    TEST_1(rm = rm->rm_next);
308
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_mpv, rm), &sdp_rtpmap_mpv);
309
    TEST_1(rm = rm->rm_next);
310
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_mp2t, rm), &sdp_rtpmap_mp2t);
311
    TEST_1(rm = rm->rm_next);
312
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_h263, rm), &sdp_rtpmap_h263);
313 314 315
    TEST_1(!rm->rm_next);
  }

Pekka Pessi's avatar
Pekka Pessi committed
316 317
  TEST_1(m = m->m_next);
  TEST(m->m_mode, sdp_sendonly);
318
  TEST_P(m->m_session, sdp);
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
  TEST_1(rm = m->m_rtpmaps);
  TEST(rm->rm_pt, 97);
  TEST_S(rm->rm_encoding, "AMR");
  TEST(rm->rm_rate, 8000);
  TEST_S(rm->rm_fmtp, "mode-set=\"0,1,2,3,4\"");

  {
    /* rtpmaps for well-known audio codecs */
    static sdp_rtpmap_t const
      sdp_rtpmap_pcmu = RTPMAP(0, "PCMU", 8000, "1"),
      sdp_rtpmap_1016 = RTPMAP(1, "1016", 8000, "1"),
      sdp_rtpmap_g721 = RTPMAP(2, "G721", 8000, "1"),
      sdp_rtpmap_gsm = RTPMAP(3, "GSM", 8000, "1"),
      sdp_rtpmap_g723 = RTPMAP(4, "G723", 8000, "1"),
      sdp_rtpmap_dvi4_8000 = RTPMAP(5, "DVI4", 8000, "1"),
      sdp_rtpmap_dvi4_16000 = RTPMAP(6, "DVI4", 16000, "1"),
      sdp_rtpmap_lpc = RTPMAP(7, "LPC", 8000, "1"),
      sdp_rtpmap_pcma = RTPMAP(8, "PCMA", 8000, "1"),
      sdp_rtpmap_g722 = RTPMAP(9, "G722", 8000, "1"),
      sdp_rtpmap_l16 = RTPMAP(10, "L16", 44100, "2"),
      sdp_rtpmap_l16_stereo = RTPMAP(11, "L16", 44100, "1"),
      sdp_rtpmap_qcelp = RTPMAP(12, "QCELP", 8000, "1"),
      sdp_rtpmap_cn = RTPMAP(13, "CN", 8000, "1"),
      sdp_rtpmap_mpa = RTPMAP(14, "MPA", 90000, 0),
      sdp_rtpmap_g728 = RTPMAP(15, "G728", 8000, "1"),
      sdp_rtpmap_dvi4_11025 = RTPMAP(16, "DVI4", 11025, "1"),
      sdp_rtpmap_dvi4_22050 = RTPMAP(17, "DVI4", 22050, "1"),
      sdp_rtpmap_g729 = RTPMAP(18, "G729", 8000, "1"),
      sdp_rtpmap_cn_reserved = RTPMAP(19, "CN", 8000, "1");

    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_pcmu, rm));
351
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_pcmu, rm), &sdp_rtpmap_pcmu);
352 353
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_1016, rm));
354
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_1016, rm), &sdp_rtpmap_1016);
355 356
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_g721, rm));
357
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_g721, rm), &sdp_rtpmap_g721);
358 359
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_gsm, rm));
360
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_gsm, rm), &sdp_rtpmap_gsm);
361 362
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_g723, rm));
363
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_g723, rm), &sdp_rtpmap_g723);
364 365
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_dvi4_8000, rm));
366 367
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_dvi4_8000, rm),
	   &sdp_rtpmap_dvi4_8000);
368 369
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_dvi4_16000, rm));
370 371
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_dvi4_16000, rm),
	   &sdp_rtpmap_dvi4_16000);
372 373
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_lpc, rm));
374
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_lpc, rm), &sdp_rtpmap_lpc);
375 376
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_pcma, rm));
377
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_pcma, rm), &sdp_rtpmap_pcma);
378 379
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_g722, rm));
380
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_g722, rm), &sdp_rtpmap_g722);
381 382
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_l16, rm));
383
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_l16, rm), &sdp_rtpmap_l16);
384 385
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_l16_stereo, rm));
386 387
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_l16_stereo, rm),
	   &sdp_rtpmap_l16_stereo);
388 389
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_qcelp, rm));
390
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_qcelp, rm), &sdp_rtpmap_qcelp);
391 392
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_cn, rm));
393
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_cn, rm), &sdp_rtpmap_cn);
394 395
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_mpa, rm));
396
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_mpa, rm), &sdp_rtpmap_mpa);
397 398
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_g728, rm));
399
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_g728, rm), &sdp_rtpmap_g728);
400 401
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_dvi4_11025, rm));
402 403
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_dvi4_11025, rm),
	   &sdp_rtpmap_dvi4_11025);
404 405
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_dvi4_22050, rm));
406 407
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_dvi4_22050, rm),
	   &sdp_rtpmap_dvi4_22050);
408 409
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_g729, rm));
410
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_g729, rm), &sdp_rtpmap_g729);
411 412
    TEST_1(rm = rm->rm_next);
    TEST_1(sdp_rtpmap_match(&sdp_rtpmap_cn_reserved, rm));
413 414
    TEST_P(sdp_rtpmap_find_matching(&sdp_rtpmap_cn_reserved, rm),
	   &sdp_rtpmap_cn_reserved);
415 416
    TEST_1(!rm->rm_next);
  }
417

Pekka Pessi's avatar
Pekka Pessi committed
418 419 420 421
  TEST_1((parser = sdp_parse(home, s2_msg, sizeof (s2_msg), 0)));
  TEST_1((sdp = sdp_session(parser)));
  TEST_1(m = sdp->sdp_media);
  TEST(m->m_mode, sdp_recvonly);
422
  TEST_P(m->m_session, sdp);
Pekka Pessi's avatar
Pekka Pessi committed
423 424 425 426 427 428 429
  TEST_1(m->m_rtpmaps);
  TEST(m->m_rtpmaps->rm_pt, 96);
  TEST_S(m->m_rtpmaps->rm_encoding, "H263-1998");
  TEST(m->m_rtpmaps->rm_rate, 90000);
  TEST_S(m->m_rtpmaps->rm_fmtp, "QCIF=4");
  TEST_1(m = sdp->sdp_media->m_next);
  TEST(m->m_mode, sdp_recvonly);
430
  TEST_P(m->m_session, sdp);
Pekka Pessi's avatar
Pekka Pessi committed
431 432 433 434 435 436
  TEST_1(m->m_rtpmaps);
  TEST(m->m_rtpmaps->rm_pt, 97);
  TEST_S(m->m_rtpmaps->rm_encoding, "AMR");
  TEST(m->m_rtpmaps->rm_rate, 8000);
  TEST_S(m->m_rtpmaps->rm_fmtp, "mode-set=\"0\"");

437
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
438 439 440 441

  END();
}

442
static char const s3_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
443 444 445 446 447 448 449 450 451 452 453 454
  "v=0\r\n"
  "o=- 2435697 2435697 IN IP4 172.21.137.44\r\n"
  "s=-\r\n"
  "t=0 0\r\n"
  "m=video 49154 RTP/AVP 34\r\n"
  "c=IN IP4 172.21.137.44\r\n"
  "m=audio 49152 RTP/AVP 8 0\r\n"
  "c=IN IP4 172.21.137.44\r\n"
  "m=message 0 MSRP/TCP *\r\n"
  ;


455
static int test_sanity(void)
Pekka Pessi's avatar
Pekka Pessi committed
456 457 458 459 460 461 462 463
{
  su_home_t *home = su_home_create();
  sdp_parser_t *parser;

  BEGIN();

  su_home_check(home); TEST_1(home);

464
  TEST_1((parser = sdp_parse(home, s3_msg, sizeof(s3_msg) - 1, 0)));
465

Pekka Pessi's avatar
Pekka Pessi committed
466 467
  TEST_1(sdp_sanity_check(parser) == 0);

468
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472

  END();
}

473
static char const pint_msg[] =
474 475 476 477
  "v=0\r\n"
  "o=- 2353687640 2353687640 IN IP4 128.3.4.5\r\n"
  "s=marketing\r\n"
  "e=john.jones.3@chinet.net\r\n"
478
  "c=TN RFC2543 +1-201-406-4090\r\n"
479 480 481 482
  "t=2353687640 0\r\n"
  "m=audio 1 voice -\r\n"
  ;

483
static char const pint_torture_msg[] =
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  "v=0\r\n"
  "o=- 2353687640 2353687640 IN IP4 128.3.4.5\r\n"
  "s=marketing\r\n"

  "c= TN RFC2543 123\r\n"
  "a=phone-context:+97252\r\n"
  "t=2353687640 0\r\n"
  "m= text 1  fax plain\r\n"
  "a=fmtp:plain  spr:fi6MeoclEjaF3EDfYHlkqx1zn8A1lMoiJFUHpQ5Xo\r\n"
  ;

static int test_pint(void)
{
  su_home_t *home = su_home_create();
  sdp_parser_t *parser;
  sdp_session_t *sdp;
500 501
  sdp_printer_t *printer;
  char const *m;
502 503 504 505 506 507 508 509

  BEGIN();

  su_home_check(home); TEST_1(home);

  TEST_1((parser = sdp_parse(home, pint_msg, sizeof(pint_msg) - 1, sdp_f_anynet)));
  TEST_1((sdp = sdp_session(parser)));

510 511 512 513 514
  TEST_1((printer = sdp_print(home, sdp, NULL, -1, 0)));
  TEST_1((m = sdp_message(printer)));
  TEST_S(m, pint_msg);
  TEST(sdp_message_size(printer), sizeof(pint_msg) - 1);

515 516 517
  TEST_1((parser = sdp_parse(home, pint_torture_msg, sizeof(pint_torture_msg) - 1,
			     sdp_f_anynet)));
  TEST_1((sdp = sdp_session(parser)));
518

519 520 521 522 523 524 525
  su_home_check(home);
  su_home_unref(home);

  END();
}


Pekka Pessi's avatar
Pekka Pessi committed
526 527 528 529
static sdp_list_t const l0[1] = {{ sizeof(l0), NULL, "foo" }};
static sdp_list_t const l1[1] = {{ sizeof(l1), (sdp_list_t *)l0, "bar" }};

/** Test list things */
530
int test_list(void)
Pekka Pessi's avatar
Pekka Pessi committed
531 532 533 534 535 536 537 538 539 540 541
{
  su_home_t *home = su_home_create();
  sdp_list_t *l;

  BEGIN();

  su_home_check(home);

  TEST_1(home);

  TEST_1((l = sdp_list_dup(home, l0)));
542
  TEST_P(l->l_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
543 544 545 546 547 548 549 550 551 552
  TEST_S(l->l_text, "foo");

  TEST_1((l = sdp_list_dup(home, l1)));
  TEST_1(l->l_next != NULL);
  TEST_1(l->l_next->l_next == NULL);
  TEST_S(l->l_text, "bar");
  TEST_S(l->l_next->l_text, "foo");

  su_home_check(home);

553
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
554 555 556 557 558

  END();
}

static
559 560
sdp_rtpmap_t const rm0[1] =
  {{
561 562 563
      sizeof(rm0), NULL, "AMR", 8000, "1",
      "mode-set=4,5,6 interleaving crc use-redundancy=1",
      0, 96, 0
Pekka Pessi's avatar
Pekka Pessi committed
564 565 566
  }};

static
567 568
sdp_rtpmap_t const rm1[1] =
  {{
569 570 571
      sizeof(rm1), (sdp_rtpmap_t *)rm0, "PCMA", 8000, "1",
      NULL,
      1, 8, 0,
Pekka Pessi's avatar
Pekka Pessi committed
572 573 574
  }};

/** Test rtpmap-related things */
575
int test_rtpmap(void)
Pekka Pessi's avatar
Pekka Pessi committed
576 577 578 579 580 581 582 583 584 585 586
{
  su_home_t *home = su_home_create();
  sdp_rtpmap_t *rm;

  BEGIN();

  su_home_check(home);

  TEST_1(home);

  TEST_1((rm = sdp_rtpmap_dup(home, rm0)));
587
  TEST_P(rm->rm_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
588 589
  TEST_S(rm->rm_encoding, "AMR");
  TEST_S(rm->rm_params, "1");
590
  TEST(rm->rm_pt, 96);
Pekka Pessi's avatar
Pekka Pessi committed
591 592 593 594 595 596 597
  TEST_S(rm->rm_fmtp, "mode-set=4,5,6 interleaving crc use-redundancy=1");

  TEST_1((rm = sdp_rtpmap_dup(home, rm1)));
  TEST_1(rm->rm_next != NULL);
  TEST_1(rm->rm_next->rm_next == NULL);
  TEST_S(rm->rm_encoding, "PCMA");
  TEST_S(rm->rm_params, "1");
598
  TEST(rm->rm_pt, 8);
Pekka Pessi's avatar
Pekka Pessi committed
599 600 601

  su_home_check(home);

602
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606 607 608

  END();
}

static sdp_attribute_t const a0[1] =
  {{ sizeof(a0), NULL, "foo", "2"}};
609
static sdp_attribute_t const a1[1] =
Pekka Pessi's avatar
Pekka Pessi committed
610 611
  {{ sizeof(a1), (sdp_attribute_t *)a0, "bar", "1" }};

612
static int test_attribute(void)
Pekka Pessi's avatar
Pekka Pessi committed
613 614
{
  su_home_t *home = su_home_create();
615
  sdp_attribute_t *a, *a_new, *list, *replaced;
Pekka Pessi's avatar
Pekka Pessi committed
616 617 618 619 620 621 622 623

  BEGIN();

  su_home_check(home);

  TEST_1(home);

  TEST_1((a = sdp_attribute_dup(home, a0)));
624
  TEST_P(a->a_next, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
  TEST_S(a->a_name, "foo");
  TEST_S(a->a_value, "2");

  strcpy((char *)a->a_name, "FOO");
  TEST_S(a0->a_name, "foo");

  strcpy((char *)a->a_value, "X");
  TEST_S(a0->a_value, "2");

  TEST_1((a = sdp_attribute_dup(home, a1)));
  TEST_1(a->a_next != NULL);
  TEST_1(a->a_next->a_next == NULL);
  TEST_S(a->a_name, "bar");
  TEST_S(a->a_value, "1");
  TEST_S(a->a_next->a_name, "foo");
  TEST_S(a->a_next->a_value, "2");

642 643
  list = a;

644 645 646 647
  TEST_P(sdp_attribute_remove(&list, NULL), NULL);
  TEST_P(sdp_attribute_remove(&list, "kuik"), NULL);
  TEST_P(sdp_attribute_remove(&list, "barf"), NULL);
  TEST_P(sdp_attribute_remove(&list, "bar"), a);
648 649 650
  TEST_1(a_new = sdp_attribute_dup(home, a));
  replaced = (void *)-1;
  TEST(sdp_attribute_replace(&list, NULL, &replaced), -1);
651
  TEST_P(replaced, NULL);
652
  TEST(sdp_attribute_replace(&list, a, &replaced), 0);
653
  TEST_P(replaced, NULL);
654
  TEST(sdp_attribute_replace(&list, a_new, &replaced), 1);
655
  TEST_P(replaced, a);
656

657
  TEST_VOID(sdp_attribute_append(&list, a));
658

659 660 661
  TEST_P(sdp_attribute_remove(&list, "bAr"), a_new);
  TEST_P(sdp_attribute_remove(&list, "BAR"), a);
  TEST_P(sdp_attribute_remove(&list, "bar"), NULL);
662

Pekka Pessi's avatar
Pekka Pessi committed
663 664
  su_home_check(home);

665
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
666 667 668 669

  END();
}

670
static int test_connection(void)
Pekka Pessi's avatar
Pekka Pessi committed
671 672 673 674 675
{
  BEGIN();
  END();
}

676
static char const media_msg[] =
Pekka Pessi's avatar
Pekka Pessi committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
"v=0\n"
"s=/sdp_torture\n"
"o=sdp_torture 0 0 IN IP4 1.2.3.4\n"
"c=IN IP4 1.2.3.4\n"
"m=audio 0 RTP/AVP 96 97 98 10 99 8 0\n"
"a=rtpmap:96 X-AMR-WB/16000\n"
"a=rtpmap:97 X-AMR/8000\n"
"a=rtpmap:98 GSM-EFR/8000\n"
"a=rtpmap:10 L16/16000\n"
"a=rtpmap:99 G723/8000\n"
"a=rtpmap:8 PCMA/8000\n"
"a=rtpmap:0 PCMU/8000\n"
"m=video 0 RTP/AVP 31\n"
"c=IN IP4 2.3.4.5\n";

static sdp_media_t const m0[1] =
693 694
  {{ sizeof(m0),
     NULL,
Pekka Pessi's avatar
Pekka Pessi committed
695 696
     NULL,
     sdp_media_audio,
697 698 699
     NULL,
     1234,
     5,
Pekka Pessi's avatar
Pekka Pessi committed
700 701 702 703
     sdp_proto_udp,
     "udp",
  }};

704
static int test_media(void)
Pekka Pessi's avatar
Pekka Pessi committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718
{
  su_home_t *home = su_home_create();
  sdp_media_t *media;
  sdp_session_t *sdp;
  sdp_parser_t *parser;

  BEGIN();

  su_home_check(home);
  TEST_1(home);

  TEST_1((parser = sdp_parse(home, media_msg, sizeof(media_msg), 0)));
  TEST_1((sdp = sdp_session(parser)));
  TEST_1((media = sdp_media_dup(home, m0, sdp)));
719 720
  /* Check comparing */
  TEST(sdp_media_cmp(media, m0), 0);
Pekka Pessi's avatar
Pekka Pessi committed
721 722 723 724

  TEST(media->m_type, sdp_media_audio);
  TEST(media->m_port, 1234);
  TEST(media->m_number_of_ports, 5);
725
  TEST_P(media->m_session, sdp);
Pekka Pessi's avatar
Pekka Pessi committed
726 727 728 729
  /* FIXME: add more tests */

  media->m_next = (sdp_media_t *)m0;
  TEST_1((media = sdp_media_dup_all(home, media, sdp)));
730
  TEST_P(media->m_connections, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
731
  TEST_1(media->m_next);
732 733 734
  TEST_P(media->m_next->m_connections, NULL);
  TEST_P(sdp_media_connections(media), sdp->sdp_connection);
  TEST_P(sdp_media_connections(media->m_next), sdp->sdp_connection);
Pekka Pessi's avatar
Pekka Pessi committed
735 736 737 738

  sdp_parser_free(parser);

  su_home_check(home);
739
  su_home_unref(home);
Pekka Pessi's avatar
Pekka Pessi committed
740 741 742 743

  END();
}

744
static int test_origin(void)
Pekka Pessi's avatar
Pekka Pessi committed
745 746 747 748 749
{
  BEGIN();
  END();
}

750
static int test_bandwidth(void)
Pekka Pessi's avatar
Pekka Pessi committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
{
  BEGIN();
  END();
}

static char const t_msg[] =
"v=0\n"
"s=/sdp_torture\n"
"o=sdp_torture 1 1 IN IP4 1.2.3.4\n"
"c=IN IP4 1.2.3.4\n"
"t=3309789956 3309793556\n"
"t=3309789956 3309793557\n"
"t=3309789955 3309793557\n"
"r=604800 3600 0 90000\n"
"z=2882844526 -1h 2898848070 0\n"
"t=3309789955 3309793557\n"
"r=604800 3600 0 90000\n"
"z=2882844526 -1h 2898848070 0\n"
  ;

771
static int test_time(void)
Pekka Pessi's avatar
Pekka Pessi committed
772 773 774 775 776 777 778 779 780 781 782 783 784
{
  sdp_parser_t *parser;
  sdp_session_t *sdp;
  sdp_time_t *t, t1[1], t2[1];

  BEGIN();

  TEST_1((parser = sdp_parse(NULL, t_msg, sizeof(t_msg), 0)));
  TEST_1((sdp = sdp_session(parser)));
  TEST_1((t = sdp->sdp_time)); *t1 = *t; t1->t_next = NULL; *t2 = *t1;
  TEST_1(sdp_time_cmp(t1, t1) == 0);
  TEST_1(sdp_time_cmp(t1, t2) == 0);
  TEST_1(sdp_time_cmp(t2, t1) == 0);
785
  TEST_1((t = t->t_next)); *t1 = *t; t1->t_next = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
786 787
  TEST_1(sdp_time_cmp(t1, t2) > 0);
  TEST_1(sdp_time_cmp(t2, t1) < 0);
788
  TEST_1((t = t->t_next)); *t2 = *t; t2->t_next = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
789 790 791 792
  TEST_1(t2->t_zone); TEST_1(t2->t_repeat);
  TEST_1(sdp_time_cmp(t2, t2) == 0);
  TEST_1(sdp_time_cmp(t1, t2) > 0);
  TEST_1(sdp_time_cmp(t2, t1) < 0);
793
  TEST_1((t = t->t_next)); *t1 = *t; t1->t_next = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
794 795 796 797
  TEST_1(t1->t_zone); TEST_1(t1->t_repeat);
  TEST_1(sdp_time_cmp(t1, t1) == 0);
  TEST_1(sdp_time_cmp(t2, t2) == 0);
  TEST_1(sdp_time_cmp(t1, t2) == 0);
798

Pekka Pessi's avatar
Pekka Pessi committed
799 800 801 802 803
  sdp_parser_free(parser);

  END();
}

804
static int test_key(void)
Pekka Pessi's avatar
Pekka Pessi committed
805 806 807 808 809 810 811 812
{
  BEGIN();
  END();
}

#include <time.h>
#include <stdlib.h>

813
static int test_build(void)
Pekka Pessi's avatar
Pekka Pessi committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
{
  sdp_session_t *sdp, *dup;
  sdp_origin_t *o;
  sdp_time_t *t;
  sdp_connection_t *c;
  sdp_media_t *m, *m1;
  sdp_rtpmap_t *rm;
  sdp_list_t *l, *l1;
  sdp_attribute_t *a;
  su_home_t *home;
  sdp_printer_t *printer;
  char const *data;

  BEGIN();

  srand(time(NULL));

  TEST_1(home = su_home_create());

833
  /*
Pekka Pessi's avatar
Pekka Pessi committed
834
   * Allocate an SDP structure using su_salloc().
835
   * su_salloc() puts memory area size to the beginning of structure
Pekka Pessi's avatar
Pekka Pessi committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
   * and zeroes rest of the structure.
   */
  TEST_1(sdp = su_salloc(home, sizeof(*sdp)));
  TEST_1(o = su_salloc(home, sizeof(*o)));
  TEST_1(t = su_salloc(home, sizeof(*t)));
  TEST_1(c = su_salloc(home, sizeof(*c)));
  TEST_1(m = su_salloc(home, sizeof(*m)));
  TEST_1(rm = su_salloc(home, sizeof(*rm)));

  sdp->sdp_origin = o;
  sdp->sdp_time = t;		/* zero time is fine for SIP */
  sdp->sdp_connection = c;
  sdp->sdp_media = m;

  o->o_username = "test";
  o->o_id = rand();
  o->o_version = 1;
  o->o_address = c;
854

Pekka Pessi's avatar
Pekka Pessi committed
855 856 857 858 859 860 861 862 863
  c->c_nettype = sdp_net_in;
  c->c_addrtype = sdp_addr_ip4;
  c->c_address = "172.21.40.40";

  m->m_session = sdp;
  m->m_type = sdp_media_audio; m->m_type_name = "audio";
  m->m_port = 5004;
  m->m_proto = sdp_proto_rtp; m->m_proto_name = "RTP/AVP";
  m->m_rtpmaps = rm;
864

Pekka Pessi's avatar
Pekka Pessi committed
865 866
  rm->rm_predef = 1;
  rm->rm_pt = 8;
867
  rm->rm_encoding = "PCMA";
Pekka Pessi's avatar
Pekka Pessi committed
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
  rm->rm_rate = 8000;

  TEST_1(m1 = su_salloc(home, sizeof(*m1)));
  TEST_1(l = su_salloc(home, sizeof(*l)));
  TEST_1(l1 = su_salloc(home, sizeof(*l1)));
  TEST_1(a = su_salloc(home, sizeof(*a)));

  m->m_next = m1;

  m1->m_session = sdp;
  m1->m_type = sdp_media_message; m->m_type_name = "message";
  m1->m_port = 5060;
  m1->m_proto = sdp_proto_tcp; m->m_proto_name = "TCP";
  m1->m_format = l;
  m1->m_attributes = a;

  l->l_text = "sip";

  l->l_next = l1;
  l1->l_text = "cpim";

  a->a_name = "user";
  a->a_value = "chat-81273845";

  TEST_1(dup = sdp_session_dup(home, sdp));

  TEST_1(printer = sdp_print(home, dup, NULL, 0, 0));
  TEST_1(data = sdp_message(printer));

897
  if (tstflags & tst_verbatim)
Pekka Pessi's avatar
Pekka Pessi committed
898 899 900 901 902 903 904
    printf("sdp_torture.c: built SDP message:\"%s\".\n", data);

  sdp_printer_free(printer);

  END();
}

905
void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
906
{
907 908
  fprintf(stderr, "usage: %s [-v] [-a]\n", name);
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
909 910 911 912 913 914 915 916
}

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

  for (i = 1; argv[i]; i++) {
917
    if (su_strmatch(argv[i], "-v"))
918
      tstflags |= tst_verbatim;
919
    else if (su_strmatch(argv[i], "-a"))
920
      tstflags |= tst_abort;
Pekka Pessi's avatar
Pekka Pessi committed
921
    else
922
      usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
923 924 925 926
  }

  null = fopen("/dev/null", "ab");

927 928 929
  retval |= test_error(); fflush(stdout);
  retval |= test_session(); fflush(stdout);
  retval |= test_session2(); fflush(stdout);
930
  retval |= test_pint(); fflush(stdout);
931 932 933 934 935 936 937 938 939 940 941
  retval |= test_sanity(); fflush(stdout);
  retval |= test_list(); fflush(stdout);
  retval |= test_rtpmap(); fflush(stdout);
  retval |= test_origin(); fflush(stdout);
  retval |= test_connection(); fflush(stdout);
  retval |= test_bandwidth(); fflush(stdout);
  retval |= test_time(); fflush(stdout);
  retval |= test_key(); fflush(stdout);
  retval |= test_attribute(); fflush(stdout);
  retval |= test_media(); fflush(stdout);
  retval |= test_build(); fflush(stdout);
Pekka Pessi's avatar
Pekka Pessi committed
942 943 944

  return retval;
}