test_soa.c 40.7 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 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 42 43 44 45 46 47 48
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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
 *
 */

/**@CFILE test_soa.c
 * @brief High-level tester for Sofia SDP Offer/Answer Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#if HAVE_ALARM
#include <unistd.h>
#include <signal.h>
#endif

struct context;
#define SOA_MAGIC_T struct context

49 50 51
#include "sofia-sip/soa.h"
#include "sofia-sip/soa_tag.h"
#include "sofia-sip/soa_add.h"
Pekka Pessi's avatar
Pekka Pessi committed
52

53
#include <sofia-sip/sdp.h>
54

55
#include <sofia-sip/su_log.h>
56
#include <sofia-sip/sip_tag.h>
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63

extern su_log_t soa_log[];

char const name[] = "test_soa";
int tstflags = 0;
#define TSTFLAGS tstflags

64
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72

#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
#define __func__ name
#endif

Pekka Pessi's avatar
Pekka Pessi committed
73 74
#define NONE ((void*)-1)

Pekka Pessi's avatar
Pekka Pessi committed
75 76 77
struct context 
{
  su_home_t home[1];
Pekka Pessi's avatar
Pekka Pessi committed
78 79 80 81 82
  su_root_t *root;

  struct {
    soa_session_t *a;
    soa_session_t *b;
83 84 85 86
  } asynch;

  soa_session_t *a;
  soa_session_t *b;
Pekka Pessi's avatar
Pekka Pessi committed
87 88

  soa_session_t *completed;
Pekka Pessi's avatar
Pekka Pessi committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102
};

int test_api_completed(struct context *arg, soa_session_t *session)
{
  return 0;
}

int test_api_errors(struct context *ctx)
{
  BEGIN();

  char const *phrase = NULL;
  char const *null = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
103 104
  TEST_1(!soa_create("default", NULL, NULL));
  TEST_1(!soa_clone(NULL, NULL, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
105 106 107 108 109
  TEST_VOID(soa_destroy(NULL));

  TEST_1(-1 == soa_set_params(NULL, TAG_END()));
  TEST_1(-1 == soa_get_params(NULL, TAG_END()));

110
  TEST_1(!soa_get_paramlist(NULL, TAG_END()));
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118

  TEST(soa_error_as_sip_response(NULL, &phrase), 500);
  TEST_S(phrase, "Internal Server Error");

  TEST_1(soa_error_as_sip_reason(NULL));

  TEST_1(!soa_media_features(NULL, 0, NULL));

119 120
  TEST_1(!soa_sip_require(NULL));
  TEST_1(!soa_sip_supported(NULL));
Pekka Pessi's avatar
Pekka Pessi committed
121 122 123

  TEST_1(-1 == soa_remote_sip_features(NULL, &null, &null));

124 125 126
  TEST_1(soa_set_capability_sdp(NULL, NULL, NULL, -1) < 0);
  TEST_1(soa_set_remote_sdp(NULL, NULL, NULL, -1) < 0);
  TEST_1(soa_set_user_sdp(NULL, NULL, NULL, -1) < 0);
127

128 129 130 131
  TEST_1(soa_get_capability_sdp(NULL, NULL, NULL, NULL) < 0);
  TEST_1(soa_get_remote_sdp(NULL, NULL, NULL, NULL) < 0);
  TEST_1(soa_get_user_sdp(NULL, NULL, NULL, NULL) < 0);
  TEST_1(soa_get_local_sdp(NULL, NULL, NULL, NULL) < 0);
132 133

  TEST_1(-1 == soa_generate_offer(NULL, 0, test_api_completed)); 
Pekka Pessi's avatar
Pekka Pessi committed
134

135
  TEST_1(-1 == soa_generate_answer(NULL, test_api_completed)); 
Pekka Pessi's avatar
Pekka Pessi committed
136

137
  TEST_1(-1 == soa_process_answer(NULL, test_api_completed)); 
Pekka Pessi's avatar
Pekka Pessi committed
138

139 140
  TEST_1(-1 == soa_process_reject(NULL, test_api_completed));

141 142
  TEST(soa_activate(NULL, "both"), -1);
  TEST(soa_deactivate(NULL, "both"), -1);
Pekka Pessi's avatar
Pekka Pessi committed
143 144 145 146 147 148
  TEST_VOID(soa_terminate(NULL, "both"));

  TEST_1(!soa_is_complete(NULL));

  TEST_1(!soa_init_offer_answer(NULL));

Pekka Pessi's avatar
Pekka Pessi committed
149 150 151 152
  TEST(soa_is_audio_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_video_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_image_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_chat_active(NULL), SOA_ACTIVE_DISABLED);
Pekka Pessi's avatar
Pekka Pessi committed
153

Pekka Pessi's avatar
Pekka Pessi committed
154 155 156 157
  TEST(soa_is_remote_audio_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_video_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_image_active(NULL), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_chat_active(NULL), SOA_ACTIVE_DISABLED);
Pekka Pessi's avatar
Pekka Pessi committed
158 159 160 161

  END();
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
int test_soa_tags(struct context *ctx)
{
  BEGIN();
  
  su_home_t home[1] = { SU_HOME_INIT(home) };
  tagi_t *t;

  tagi_t const soafilter[] = {
    { TAG_FILTER(soa_tag_filter) },
    { TAG_NULL() }
  };

  t = tl_filtered_tlist(home, soafilter,
			SIPTAG_FROM_STR("sip:my.domain"),
			SOATAG_USER_SDP_STR("v=0"),
			SOATAG_HOLD("*"),
			TAG_END());
  TEST_1(t);
  TEST(t[0].t_tag, soatag_user_sdp_str);
  TEST(t[1].t_tag, soatag_hold);
  TEST_1(t[2].t_tag == NULL || t[2].t_tag == tag_null);

  su_home_deinit(home);

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
189 190 191 192
int test_init(struct context *ctx, char *argv[])
{
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197 198 199
  int n;

  ctx->root = su_root_create(ctx); TEST_1(ctx->root);

  ctx->asynch.a = soa_create("asynch", ctx->root, ctx); 
  TEST_1(!ctx->asynch.a);

Pekka Pessi's avatar
Pekka Pessi committed
200
#if 0
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204 205 206 207 208 209 210 211 212
  TEST_1(!soa_find("asynch"));
  TEST_1(soa_find("default"));

  n = soa_add("asynch", &soa_asynch_actions); TEST(n, 0);

  TEST_1(soa_find("asynch"));

  ctx->asynch.a = soa_create("asynch", ctx->root, ctx); 
  TEST_1(ctx->asynch.a);

  ctx->asynch.b = soa_create("asynch", ctx->root, ctx);
  TEST_1(ctx->asynch.b);
Pekka Pessi's avatar
Pekka Pessi committed
213
#endif
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216

  /* Create asynchronous endpoints */

217 218
  ctx->a = soa_create("static", ctx->root, ctx); 
  TEST_1(!ctx->a);
Pekka Pessi's avatar
Pekka Pessi committed
219 220 221 222

  TEST_1(!soa_find("static"));
  TEST_1(soa_find("default"));

Pekka Pessi's avatar
Pekka Pessi committed
223
  n = soa_add("static", &soa_default_actions); TEST(n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
224 225 226

  TEST_1(soa_find("static"));

227 228
  ctx->a = soa_create("static", ctx->root, ctx);
  TEST_1(ctx->a);
Pekka Pessi's avatar
Pekka Pessi committed
229

230 231
  ctx->b = soa_create("static", ctx->root, ctx);
  TEST_1(ctx->b);
Pekka Pessi's avatar
Pekka Pessi committed
232 233 234 235 236 237 238

  END();
}

int test_params(struct context *ctx)
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
239
  int n;
Pekka Pessi's avatar
Pekka Pessi committed
240
  int af;
Pekka Pessi's avatar
Pekka Pessi committed
241
  char const *address;
242
  char const *hold;
243
  int rtp_select, rtp_sort;
244 245 246
  int rtp_mismatch;
  int srtp_enable, srtp_confidentiality, srtp_integrity;
  soa_session_t *a = ctx->a, *b = ctx->b;
Pekka Pessi's avatar
Pekka Pessi committed
247 248 249 250 251 252

  n = soa_set_params(a, TAG_END()); TEST(n, 0);
  n = soa_set_params(b, TAG_END()); TEST(n, 0);

  af = -42;
  address = NONE;
253 254 255 256 257
  hold = NONE;

  rtp_select = -1, rtp_sort = -1, rtp_mismatch = -1;
  srtp_enable = -1, srtp_confidentiality = -1, srtp_integrity = -1;

Pekka Pessi's avatar
Pekka Pessi committed
258 259 260
  TEST(soa_get_params(a,
		      SOATAG_AF_REF(af),
		      SOATAG_ADDRESS_REF(address),
261 262 263 264 265 266 267 268 269
		      SOATAG_HOLD_REF(hold),

		      SOATAG_RTP_SELECT_REF(rtp_select),
		      SOATAG_RTP_SORT_REF(rtp_sort),
		      SOATAG_RTP_MISMATCH_REF(rtp_mismatch),

		      SOATAG_SRTP_ENABLE_REF(srtp_enable),
		      SOATAG_SRTP_CONFIDENTIALITY_REF(srtp_confidentiality),
		      SOATAG_SRTP_INTEGRITY_REF(srtp_integrity),
270
		      TAG_END()),
271
       9);
Pekka Pessi's avatar
Pekka Pessi committed
272 273
  TEST(af, SOA_AF_ANY);
  TEST(address, 0);
274 275 276 277 278 279 280
  TEST(hold, 0);
  TEST(rtp_select, SOA_RTP_SELECT_SINGLE);
  TEST(rtp_sort, SOA_RTP_SORT_DEFAULT);
  TEST(rtp_mismatch, 0);
  TEST(srtp_enable, 0);
  TEST(srtp_confidentiality, 0);
  TEST(srtp_integrity, 0);
Pekka Pessi's avatar
Pekka Pessi committed
281 282 283

  TEST(soa_set_params(a,
		      SOATAG_AF(SOA_AF_IP4_IP6),
284 285 286 287 288 289 290 291 292 293 294
		      SOATAG_ADDRESS("127.0.0.1"),
		      SOATAG_HOLD("audio"),

		      SOATAG_RTP_SELECT(SOA_RTP_SELECT_ALL),
		      SOATAG_RTP_SORT(SOA_RTP_SORT_LOCAL),
		      SOATAG_RTP_MISMATCH(1),

		      SOATAG_SRTP_ENABLE(1),
		      SOATAG_SRTP_CONFIDENTIALITY(1),
		      SOATAG_SRTP_INTEGRITY(1),

295
		      TAG_END()),
296
       9);
Pekka Pessi's avatar
Pekka Pessi committed
297 298
  TEST(soa_get_params(a,
		      SOATAG_AF_REF(af),
299 300 301 302 303 304 305 306 307 308
		      SOATAG_ADDRESS_REF(address),
		      SOATAG_HOLD_REF(hold),

		      SOATAG_RTP_SELECT_REF(rtp_select),
		      SOATAG_RTP_SORT_REF(rtp_sort),
		      SOATAG_RTP_MISMATCH_REF(rtp_mismatch),

		      SOATAG_SRTP_ENABLE_REF(srtp_enable),
		      SOATAG_SRTP_CONFIDENTIALITY_REF(srtp_confidentiality),
		      SOATAG_SRTP_INTEGRITY_REF(srtp_integrity),
309
		      TAG_END()),
310
       9);
Pekka Pessi's avatar
Pekka Pessi committed
311
  TEST(af, SOA_AF_IP4_IP6);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  TEST_S(address, "127.0.0.1");
  TEST_S(hold, "audio");
  TEST(rtp_select, SOA_RTP_SELECT_ALL);
  TEST(rtp_sort, SOA_RTP_SORT_LOCAL);
  TEST(rtp_mismatch, 1);
  TEST(srtp_enable, 1);
  TEST(srtp_confidentiality, 1);
  TEST(srtp_integrity, 1);

  /* Restore defaults */
  TEST(soa_set_params(a,
		      SOATAG_AF(SOA_AF_IP4_IP6),
		      SOATAG_ADDRESS(NULL),
		      SOATAG_HOLD(NULL),

		      SOATAG_RTP_SELECT(SOA_RTP_SELECT_SINGLE),
		      SOATAG_RTP_SORT(SOA_RTP_SORT_DEFAULT),
		      SOATAG_RTP_MISMATCH(0),

		      SOATAG_SRTP_ENABLE(0),
		      SOATAG_SRTP_CONFIDENTIALITY(0),
		      SOATAG_SRTP_INTEGRITY(0),

		      TAG_END()),
       9);
337

Pekka Pessi's avatar
Pekka Pessi committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  END();
}

int test_completed(struct context *ctx, soa_session_t *session)
{
  ctx->completed = session;
  su_root_break(ctx->root);
  return 0;
}

int test_static_offer_answer(struct context *ctx)
{
  BEGIN();
  int n;
  
Pekka Pessi's avatar
Pekka Pessi committed
353 354
  soa_session_t *a, *b;

355
  char const *caps = NONE, *offer = NONE, *answer = NONE;
356
  isize_t capslen = -1, offerlen = -1, answerlen = -1;
Pekka Pessi's avatar
Pekka Pessi committed
357

Pekka Pessi's avatar
Pekka Pessi committed
358 359
  su_home_t home[1] = { SU_HOME_INIT(home) };

Pekka Pessi's avatar
Pekka Pessi committed
360
  char const a_caps[] = 
Pekka Pessi's avatar
Pekka Pessi committed
361 362 363
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
364
    "m=audio 0 RTP/AVP 0 8\r\n";
Pekka Pessi's avatar
Pekka Pessi committed
365

Pekka Pessi's avatar
Pekka Pessi committed
366
  char const b_caps[] = 
367
    "m=audio 5004 RTP/AVP 96 8\n"
Pekka Pessi's avatar
Pekka Pessi committed
368 369
    "m=rtpmap:96 GSM/8000\n";

370
  TEST(soa_set_capability_sdp(ctx->a, 0, "m=audio 0 RTP/AVP 0 8", -1), 
371
       1);
372
  TEST(soa_set_capability_sdp(ctx->a, 0, a_caps, strlen(a_caps)), 
373
       1);
374
  TEST(soa_get_capability_sdp(ctx->a, NULL, &caps, &capslen), 1);
Pekka Pessi's avatar
Pekka Pessi committed
375 376 377 378

  TEST_1(caps != NULL && caps != NONE);
  TEST_1(capslen > 0);

379 380
  TEST(soa_set_user_sdp(ctx->b, 0, b_caps, strlen(b_caps)), 1);
  TEST(soa_get_capability_sdp(ctx->a, NULL, &caps, &capslen), 1);
381

382 383
  TEST_1(a = soa_clone(ctx->a, ctx->root, ctx));
  TEST_1(b = soa_clone(ctx->b, ctx->root, ctx));
Pekka Pessi's avatar
Pekka Pessi committed
384

385
  n = soa_get_local_sdp(a, NULL, &offer, &offerlen); TEST(n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
386

387
  n = soa_set_user_sdp(a, 0, "m=audio 5004 RTP/AVP 0 8", -1); TEST(n, 1);
388

Pekka Pessi's avatar
Pekka Pessi committed
389 390
  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);

391
  n = soa_get_local_sdp(a, NULL, &offer, &offerlen); TEST(n, 1);
392
  TEST_1(offer != NULL && offer != NONE);
Pekka Pessi's avatar
Pekka Pessi committed
393

394
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
395

396
  n = soa_get_local_sdp(b, NULL, &answer, &answerlen); TEST(n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
397

398 399
  n = soa_set_params(b,
		     SOATAG_LOCAL_SDP_STR("m=audio 5004 RTP/AVP 8"),
400 401
		     SOATAG_AF(SOA_AF_IP4_ONLY),
		     SOATAG_ADDRESS("1.2.3.4"),
402 403
		     TAG_END());
  
Pekka Pessi's avatar
Pekka Pessi committed
404
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
405

406 407
  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);
Pekka Pessi's avatar
Pekka Pessi committed
408

409
  n = soa_get_local_sdp(b, NULL, &answer, &answerlen); TEST(n, 1);
410
  TEST_1(answer != NULL && answer != NONE);
411
  TEST_1(strstr(answer, "c=IN IP4 1.2.3.4"));
Pekka Pessi's avatar
Pekka Pessi committed
412

413
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
414

Pekka Pessi's avatar
Pekka Pessi committed
415
  n = soa_process_answer(a, test_completed); TEST(n, 0);
Pekka Pessi's avatar
Pekka Pessi committed
416

417 418
  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);
Pekka Pessi's avatar
Pekka Pessi committed
419

Pekka Pessi's avatar
Pekka Pessi committed
420 421 422 423
  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_DISABLED);
  TEST(soa_is_image_active(a), SOA_ACTIVE_DISABLED);
  TEST(soa_is_chat_active(a), SOA_ACTIVE_DISABLED);
Pekka Pessi's avatar
Pekka Pessi committed
424

Pekka Pessi's avatar
Pekka Pessi committed
425 426 427 428
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_video_active(a), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_image_active(a), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_chat_active(a), SOA_ACTIVE_DISABLED);
Pekka Pessi's avatar
Pekka Pessi committed
429

430
  /* 'A' will put call on hold */
Pekka Pessi's avatar
Pekka Pessi committed
431
  offer = NONE;
432 433 434
  TEST(soa_set_params(a, SOATAG_HOLD("*"), TAG_END()), 1);

  TEST(soa_generate_offer(a, 1, test_completed), 0);
435
  TEST(soa_get_local_sdp(a, NULL, &offer, &offerlen), 1);
436
  TEST_1(offer != NULL && offer != NONE);
437 438
  TEST_1(strstr(offer, "a=sendonly"));
  TEST(soa_set_remote_sdp(b, 0, offer, offerlen), 1);
439 440 441
  TEST(soa_generate_answer(b, test_completed), 0);
  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);
442
  TEST(soa_get_local_sdp(b, NULL, &answer, &answerlen), 1);
443
  TEST_1(answer != NULL && answer != NONE);
444 445
  TEST_1(strstr(answer, "a=recvonly"));
  TEST(soa_set_remote_sdp(a, 0, answer, -1), 1);
446
  TEST(soa_process_answer(a, test_completed), 0);
447 448
  TEST(soa_activate(a, NULL), 0);

Pekka Pessi's avatar
Pekka Pessi committed
449 450
  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDONLY);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDONLY);
451

Pekka Pessi's avatar
Pekka Pessi committed
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
  /* 'A' will release hold. */ 
  TEST(soa_set_params(a, SOATAG_HOLD(NULL), TAG_END()), 1);

  TEST(soa_generate_offer(a, 1, test_completed), 0);
  TEST(soa_get_local_sdp(a, NULL, &offer, &offerlen), 1);
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(!strstr(offer, "a=sendonly"));
  TEST(soa_set_remote_sdp(b, 0, offer, offerlen), 1);
  TEST(soa_generate_answer(b, test_completed), 0);
  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);
  TEST(soa_get_local_sdp(b, NULL, &answer, &answerlen), 1);
  TEST_1(answer != NULL && answer != NONE);
  TEST_1(!strstr(answer, "a=recvonly"));
  TEST(soa_set_remote_sdp(a, 0, answer, -1), 1);
  TEST(soa_process_answer(a, test_completed), 0);
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);

  /* 'A' will put B on hold but this time with c=IN IP4 0.0.0.0 */
  TEST(soa_set_params(a, SOATAG_HOLD("*"), TAG_END()), 1);
  TEST(soa_generate_offer(a, 1, test_completed), 0);

  {
    sdp_session_t const *o_sdp;
    sdp_session_t *sdp;
    sdp_printer_t *p;
    sdp_connection_t *c;

    TEST(soa_get_local_sdp(a, &o_sdp, NULL, NULL), 1);
    TEST_1(o_sdp != NULL && o_sdp != NONE);
    TEST_1(sdp = sdp_session_dup(home, o_sdp));

    /* Remove mode, change c=, encode offer */
    if (sdp->sdp_media->m_connections)
      c = sdp->sdp_media->m_connections;
    else
      c = sdp->sdp_connection;
    TEST_1(c);
    c->c_address = "0.0.0.0";
    
    TEST_1(p = sdp_print(home, sdp, NULL, 0, sdp_f_realloc));
    TEST_1(sdp_message(p));
    offer = sdp_message(p); offerlen = strlen(offer);
  }

  TEST(soa_set_remote_sdp(b, 0, offer, -1), 1);
  TEST(soa_generate_answer(b, test_completed), 0);
  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);
  TEST(soa_get_local_sdp(b, NULL, &answer, &answerlen), 1);
  TEST_1(answer != NULL && answer != NONE);
  TEST_1(strstr(answer, "a=recvonly"));
  TEST(soa_set_remote_sdp(a, 0, answer, -1), 1);
  TEST(soa_process_answer(a, test_completed), 0);
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDONLY);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDONLY);
  TEST(soa_is_audio_active(b), SOA_ACTIVE_RECVONLY);
  TEST(soa_is_remote_audio_active(b), SOA_ACTIVE_RECVONLY);

  /* 'A' will propose adding video. */ 
517
  /* 'B' will reject. */ 
Pekka Pessi's avatar
Pekka Pessi committed
518 519
  TEST(soa_set_params(a,
		      SOATAG_HOLD(NULL),  /* 'A' will release hold. */ 
520 521 522 523 524
		      SOATAG_USER_SDP_STR("m=audio 5004 RTP/AVP 0 8\r\n"
					  "m=video 5006 RTP/AVP 34\r\n"),
		      TAG_END()), 2);

  TEST(soa_generate_offer(a, 1, test_completed), 0);
525
  TEST(soa_get_local_sdp(a, NULL, &offer, &offerlen), 1);
526 527 528 529 530 531 532
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(!strstr(offer, "a=sendonly"));
  TEST_1(strstr(offer, "m=video"));
  TEST(soa_set_remote_sdp(b, 0, offer, offerlen), 1);
  TEST(soa_generate_answer(b, test_completed), 0);
  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);
533
  TEST(soa_get_local_sdp(b, NULL, &answer, &answerlen), 1);
534 535 536 537 538 539
  TEST_1(answer != NULL && answer != NONE);
  TEST_1(!strstr(answer, "a=recvonly"));
  TEST_1(strstr(answer, "m=video"));
  TEST(soa_set_remote_sdp(a, 0, answer, -1), 1);
  TEST(soa_process_answer(a, test_completed), 0);
  TEST(soa_activate(a, NULL), 0);
540

Pekka Pessi's avatar
Pekka Pessi committed
541 542 543
  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_REJECTED);
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
  {
    /* Test tags */
    sdp_session_t const *l = NULL, *u = NULL, *r = NULL;
    sdp_media_t const *m;

    TEST(soa_get_params(b, 
			SOATAG_LOCAL_SDP_REF(l),
			SOATAG_USER_SDP_REF(u),
			SOATAG_REMOTE_SDP_REF(r),
			TAG_END()), 3);

    TEST_1(l); TEST_1(u); TEST_1(r);
    TEST_1(m = l->sdp_media); TEST(m->m_type, sdp_media_audio);
    TEST_1(!m->m_rejected);
    TEST_1(m = m->m_next); TEST(m->m_type, sdp_media_video);
    TEST_1(m->m_rejected);
  }

563 564 565 566 567 568 569 570
  /* 'B' will now propose adding video. */
  /* 'A' will accept. */
  TEST(soa_set_params(b, 
		      SOATAG_USER_SDP_STR("m=audio 5004 RTP/AVP 0 8\r\n"
					  "m=video 5006 RTP/AVP 34\r\n"),
		      TAG_END()), 1);

  TEST(soa_generate_offer(b, 1, test_completed), 0);
571
  TEST(soa_get_local_sdp(b, NULL, &offer, &offerlen), 1);
572 573 574 575 576 577 578
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(!strstr(offer, "b=sendonly"));
  TEST_1(strstr(offer, "m=video"));
  TEST(soa_set_remote_sdp(a, 0, offer, offerlen), 1);
  TEST(soa_generate_answer(a, test_completed), 0);
  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);
579
  TEST(soa_get_local_sdp(a, NULL, &answer, &answerlen), 1);
580 581 582 583 584 585 586
  TEST_1(answer != NULL && answer != NONE);
  TEST_1(!strstr(answer, "b=recvonly"));
  TEST_1(strstr(answer, "m=video"));
  TEST(soa_set_remote_sdp(b, 0, answer, -1), 1);
  TEST(soa_process_answer(b, test_completed), 0);
  TEST(soa_activate(b, NULL), 0);

Pekka Pessi's avatar
Pekka Pessi committed
587 588 589
  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_SENDRECV);
590
  
Pekka Pessi's avatar
Pekka Pessi committed
591
  TEST_VOID(soa_terminate(a, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
592

Pekka Pessi's avatar
Pekka Pessi committed
593 594
  TEST(soa_is_audio_active(a), SOA_ACTIVE_DISABLED);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_DISABLED);
Pekka Pessi's avatar
Pekka Pessi committed
595

Pekka Pessi's avatar
Pekka Pessi committed
596
  TEST_VOID(soa_terminate(b, NULL));
Pekka Pessi's avatar
Pekka Pessi committed
597
  
Pekka Pessi's avatar
Pekka Pessi committed
598 599 600
  TEST_VOID(soa_destroy(a));
  TEST_VOID(soa_destroy(b));

Pekka Pessi's avatar
Pekka Pessi committed
601 602
  su_home_deinit(home);

Pekka Pessi's avatar
Pekka Pessi committed
603 604 605
  END();
}

606 607 608 609 610 611 612 613
int test_codec_selection(struct context *ctx)
{
  BEGIN();
  int n;
  
  soa_session_t *a, *b;

  char const *offer = NONE, *answer = NONE;
614
  isize_t offerlen = -1, answerlen = -1;
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

  sdp_session_t const *a_sdp, *b_sdp;
  sdp_media_t const *m;
  sdp_rtpmap_t const *rm;

  char const a_caps[] = 
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5008 RTP/AVP 0 8 97\r\n"
    "a=rtpmap:97 GSM/8000\n"
    ;

  char const b_caps[] = 
    "m=audio 5004 RTP/AVP 96 97\n"
    "a=rtpmap:96 G7231/8000\n"
    "a=rtpmap:97 G729/8000\n";

  TEST_1(a = soa_create("static", ctx->root, ctx));
  TEST_1(b = soa_create("static", ctx->root, ctx));

  TEST(soa_set_user_sdp(a, 0, a_caps, strlen(a_caps)), 1);
  TEST(soa_set_user_sdp(b, 0, b_caps, strlen(b_caps)), 1);

639 640
  TEST(soa_set_params(a, SOATAG_AUDIO_AUX("cn telephone-event"), TAG_END()), 1);

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 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 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, NULL, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_get_local_sdp(b, NULL, &answer, &answerlen); TEST(n, 0);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_REJECTED);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_REJECTED);

  TEST_1(m = b_sdp->sdp_media); TEST_1(m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G7231");
  /* Not using payload type 97 from offer */
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 98);
  TEST_S(rm->rm_encoding, "G729");
  TEST_1(!rm->rm_next);

  /* ---------------------------------------------------------------------- */

  /* Re-O/A: A generates new SDP */
  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 0);
  n = soa_process_answer(a, test_completed); TEST(n, 0);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  /* Re-O/A: no-one regenerates new SDP */
  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 0);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 0);
  n = soa_process_answer(a, test_completed); TEST(n, 0);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  /* ---------------------------------------------------------------------- */

  /* Re-O/A: accept media without common codecs */

  /* Accept media without common codecs */
  TEST_1(soa_set_params(a, SOATAG_RTP_MISMATCH(1), TAG_END()));
  TEST_1(soa_set_params(b, SOATAG_RTP_MISMATCH(1), TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);

  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G7231");
  /* Not using payload type 97 from offer */
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 98);
  TEST_S(rm->rm_encoding, "G729");
  TEST_1(!rm->rm_next);

  /* ---------------------------------------------------------------------- */

  /* Re-O/A: add a common codec */

  /* Accept media without common codecs */
  TEST_1(soa_set_params(a, SOATAG_RTP_MISMATCH(0), 
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
750
    "m=audio 5008 RTP/AVP 0 8 96 3 127\r\n"
751
    "a=rtpmap:96 G729/8000\n"
752
    "a=rtpmap:127 CN/8000\n"
753 754 755 756 757 758 759 760 761
    ),			
			SOATAG_RTP_SORT(SOA_RTP_SORT_REMOTE),
			SOATAG_RTP_SELECT(SOA_RTP_SELECT_ALL),
			TAG_END()));
  TEST_1(soa_set_params(b, SOATAG_RTP_MISMATCH(0), 
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
762
    "m=audio 5004 RTP/AVP 96 3 97 111\r\n"
763 764
    "a=rtpmap:96 G7231/8000\n"
    "a=rtpmap:97 G729/8000\n"
765 766
    "a=rtpmap:111 telephone-event/8000\n"
    "a=fmtp:111 0-15\n"
767
    ),			
768
			SOATAG_AUDIO_AUX("cn telephone-event"),
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
			SOATAG_RTP_SORT(SOA_RTP_SORT_LOCAL),
			SOATAG_RTP_SELECT(SOA_RTP_SELECT_COMMON),
			TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, NULL, NULL); TEST(n, 1);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);

  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 3);
  TEST_S(rm->rm_encoding, "GSM");
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G729");
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 0);
  TEST_S(rm->rm_encoding, "PCMU");
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 8);
  TEST_S(rm->rm_encoding, "PCMA");
802 803
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 127);
  TEST_S(rm->rm_encoding, "CN");
804 805 806 807 808 809 810 811
  TEST_1(!rm->rm_next);

  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 3);
  TEST_S(rm->rm_encoding, "GSM");
  /* Using payload type 96 from offer */
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G729");
812 813
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 111);
  TEST_S(rm->rm_encoding, "telephone-event");
814 815 816 817 818 819 820 821 822 823
  TEST_1(!rm->rm_next);

  /* ---------------------------------------------------------------------- */
  /* Re-O/A: prune down to single codec. */

  TEST_1(soa_set_params(a, 
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
824
    "m=audio 5008 RTP/AVP 0 8 97 96 127\r\n"
825 826
    "a=rtpmap:96 G729/8000\n"
    "a=rtpmap:97 GSM/8000\n"
827
    "a=rtpmap:127 CN/8000\n"
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    ),			
			SOATAG_RTP_MISMATCH(0),
			SOATAG_RTP_SELECT(SOA_RTP_SELECT_COMMON),
			TAG_END()));
  TEST_1(soa_set_params(b,
			SOATAG_RTP_MISMATCH(0),
			SOATAG_RTP_SORT(SOA_RTP_SORT_LOCAL),
			SOATAG_RTP_SELECT(SOA_RTP_SELECT_SINGLE),
			TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);

  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 97);
  TEST_S(rm->rm_encoding, "GSM");
861 862
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 127);
  TEST_S(rm->rm_encoding, "CN");
863 864 865 866
  TEST_1(!rm->rm_next);

  /* Answering end matches payload types 
     then sorts by local preference, 
867
     then select best codec => GSM with pt 97 */
868 869 870
  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 97);
  TEST_S(rm->rm_encoding, "GSM");
871 872
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 111);
  TEST_S(rm->rm_encoding, "telephone-event");
873 874 875 876 877 878 879 880 881 882
  TEST_1(!rm->rm_next);

  /* ---------------------------------------------------------------------- */
  /* Re-O/A: A generates new SDP offer with single codec only */
  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 97);
  TEST_S(rm->rm_encoding, "GSM");
883 884
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 127);
  TEST_S(rm->rm_encoding, "CN");
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
  TEST_1(!rm->rm_next);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  /* Answer from B is identical to previous one */
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 0);
  n = soa_process_answer(a, test_completed); TEST(n, 0);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 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 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  /* ---------------------------------------------------------------------- */

  /* Add new media - without common codecs */
  TEST_1(soa_set_params(a,
			SOATAG_RTP_MISMATCH(0),
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5008 RTP/AVP 0 8 96 3 127\r\n"
    "a=rtpmap:96 G729/8000\n"
    "a=rtpmap:127 CN/8000\n"
    "m=video 5010 RTP/AVP 31\r\n"
    "m=audio 6008 RTP/SAVP 3\n"
    ),
			TAG_END()));
  TEST_1(soa_set_params(b,
			SOATAG_RTP_MISMATCH(0),
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5004 RTP/AVP 96 3 97 111\r\n"
    "a=rtpmap:96 G7231/8000\n"
    "a=rtpmap:97 G729/8000\n"
    "a=rtpmap:111 telephone-event/8000\n"
    "a=fmtp:111 0-15\n"
    "m=audio 6004 RTP/SAVP 96\n"
    "a=rtpmap:96 G729/8000\n"
    "m=video 5006 RTP/AVP 34\n"
    ),
			TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  /* Answer from B rejects video */
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_REJECTED);
  TEST(soa_is_remote_video_active(a), SOA_ACTIVE_REJECTED);

  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(m->m_rejected);
  TEST_1(!m->m_next);

  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(m->m_rejected);
  /* Rejected but tell what we support */
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 34);
  TEST_S(rm->rm_encoding, "H263");
  TEST_1(m = m->m_next); TEST_1(m->m_rejected);
  TEST_1(!m->m_next);

  /* ---------------------------------------------------------------------- */
  /* A adds H.263 to video */
  TEST_1(soa_set_params(a,
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5008 RTP/AVP 0 8 96 3 127\r\n"
    "a=rtpmap:96 G729/8000\n"
    "a=rtpmap:127 CN/8000\n"
    "m=video 5010 RTP/AVP 31 34\r\n"
    "m=audio 6008 RTP/SAVP 3\n"
    ),
			TAG_END()));

  /* B adds GSM to SRTP */
  TEST_1(soa_set_params(b,
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5004 RTP/AVP 96 3 97 111\r\n"
    "a=rtpmap:96 G7231/8000\n"
    "a=rtpmap:97 G729/8000\n"
    "a=rtpmap:111 telephone-event/8000\n"
    "a=fmtp:111 0-15\n"
    "m=audio 6004 RTP/SAVP 96 3\n"
    "a=rtpmap:96 G729/8000\n"
    "m=video 5006 RTP/AVP 34\n"
    ),
			TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  /* Answer from B now accepts video */
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_video_active(a), SOA_ACTIVE_SENDRECV);

  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 34);
  TEST_S(rm->rm_encoding, "H263");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);

  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 34);
  TEST_S(rm->rm_encoding, "H263");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);
1045

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  /* ---------------------------------------------------------------------- */
  /* A drops GSM support */

  TEST_1(soa_set_params(a,
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5008 RTP/AVP 0 8 96 127\r\n"
    "a=rtpmap:96 G729/8000\n"
    "a=rtpmap:127 CN/8000\n"
    "m=video 5010 RTP/AVP 31 34\r\n"
    "m=audio 6008 RTP/SAVP 3\n"
    ),
			TAG_END()));

Pekka Pessi's avatar
Pekka Pessi committed
1062
  /* B adds GSM to SRTP, changes IP address */
1063 1064 1065 1066
  TEST_1(soa_set_params(b,
			SOATAG_USER_SDP_STR(
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
Pekka Pessi's avatar
Pekka Pessi committed
1067
    "c=IN IP4 127.0.0.3\r\n"
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    "m=audio 5004 RTP/AVP 96 3 97 111\r\n"
    "a=rtpmap:96 G7231/8000\n"
    "a=rtpmap:97 G729/8000\n"
    "a=rtpmap:111 telephone-event/8000\n"
    "a=fmtp:111 0-15\n"
    "m=audio 6004 RTP/SAVP 96 3\n"
    "a=rtpmap:96 G729/8000\n"
    "m=video 5006 RTP/AVP 34\n"
    ),
			TAG_END()));

  n = soa_generate_offer(a, 1, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);
  TEST_1(offer != NULL && offer != NONE);
  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);
  n = soa_set_remote_sdp(b, 0, offer, offerlen); TEST(n, 1);
  n = soa_generate_answer(b, test_completed); TEST(n, 0);
  /* Answer from B now accepts video */
  n = soa_get_local_sdp(b, &b_sdp, &answer, &answerlen); TEST(n, 1);
  TEST_1(answer != NULL && answer != NONE);
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092 1093
  /* Check that updated c= line is propagated */
  TEST_1(b_sdp->sdp_connection);
  TEST_S(b_sdp->sdp_connection->c_address, "127.0.0.3");
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
  n = soa_set_remote_sdp(a, 0, answer, -1); TEST(n, 1);
  n = soa_process_answer(a, test_completed); TEST(n, 0);
  n = soa_get_local_sdp(a, &a_sdp, &offer, &offerlen); TEST(n, 1);

  TEST_1(soa_is_complete(b));
  TEST(soa_activate(b, NULL), 0);

  TEST_1(soa_is_complete(a));
  TEST(soa_activate(a, NULL), 0);

  TEST(soa_is_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_audio_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_video_active(a), SOA_ACTIVE_SENDRECV);
  TEST(soa_is_remote_video_active(a), SOA_ACTIVE_SENDRECV);

  TEST_1(m = a_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G729");
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 127);
  TEST_S(rm->rm_encoding, "CN");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 34);
  TEST_S(rm->rm_encoding, "H263");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);

  TEST_1(m = b_sdp->sdp_media); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 96);
  TEST_S(rm->rm_encoding, "G729");
  TEST_1(rm = rm->rm_next); TEST(rm->rm_pt, 111);
  TEST_S(rm->rm_encoding, "telephone-event");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(rm = m->m_rtpmaps); TEST(rm->rm_pt, 34);
  TEST_S(rm->rm_encoding, "H263");
  TEST_1(m = m->m_next); TEST_1(!m->m_rejected);
  TEST_1(!m->m_next);

  /* ---------------------------------------------------------------------- */

1133 1134 1135 1136 1137 1138 1139 1140 1141
  TEST_VOID(soa_terminate(a, NULL));
  TEST_VOID(soa_terminate(b, NULL));
  
  TEST_VOID(soa_destroy(a));
  TEST_VOID(soa_destroy(b));

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
1142 1143 1144 1145

int test_asynch_offer_answer(struct context *ctx)
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
1146 1147

#if 0
Pekka Pessi's avatar
Pekka Pessi committed
1148 1149
  int n;
  
1150
  char const *caps = NONE, *offer = NONE, *answer = NONE;
1151
  isize_t capslen = -1, offerlen = -1, answerlen = -1;
Pekka Pessi's avatar
Pekka Pessi committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

  char const a[] = 
    "v=0\r\n"
    "o=left 219498671 2 IN IP4 127.0.0.2\r\n"
    "c=IN IP4 127.0.0.2\r\n"
    "m=audio 5004 RTP/AVP 0 8\r\n";

  char const b[] = 
    "v=0\n"
    "o=right 93298573265 321974 IN IP4 127.0.0.3\n"
    "c=IN IP4 127.0.0.3\n"
    "m=audio 5006 RTP/AVP 96\n"
    "m=rtpmap:96 GSM/8000\n";

1166 1167
  n = soa_set_capability_sdp(ctx->asynch.a, 0, 
			     "m=audio 5004 RTP/AVP 0 8", -1); 
1168 1169
  TEST(n, 1);

1170 1171
  n = soa_set_capability_sdp(ctx->asynch.a, 0, a, strlen(a)); TEST(n, 1);
  n = soa_get_capability_sdp(ctx->asynch.a, 0, &caps, &capslen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1172 1173 1174 1175

  TEST_1(caps != NULL && caps != NONE);
  TEST_1(capslen > 0);

1176
  n = soa_set_capability_sdp(ctx->asynch.b, 0, b, strlen(b)); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1177

1178
  n = soa_generate_offer(ctx->asynch.a, 1, test_completed); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1179

1180 1181
  su_root_run(ctx->root); TEST(ctx->completed, ctx->asynch.a); 
  ctx->completed = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1182

1183
  n = soa_get_local_sdp(ctx->asynch.a, 0, &offer, &offerlen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1184

1185
  n = soa_set_remote_sdp(ctx->asynch.b, 0, offer, offerlen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1186

1187
  n = soa_generate_answer(ctx->asynch.b, test_completed); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1188

1189 1190
  su_root_run(ctx->root); TEST(ctx->completed, ctx->asynch.b); 
  ctx->completed = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1191

1192 1193
  TEST_1(soa_is_complete(ctx->asynch.b));
  TEST(soa_activate(ctx->asynch.b, NULL), 0);
Pekka Pessi's avatar
Pekka Pessi committed
1194

1195
  n = soa_get_local_sdp(ctx->asynch.b, 0, &answer, &answerlen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1196

1197
  n = soa_set_remote_sdp(ctx->asynch.a, 0, answer, answerlen); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1198

1199
  n = soa_process_answer(ctx->asynch.a, test_completed); TEST(n, 1);
Pekka Pessi's avatar
Pekka Pessi committed
1200

1201 1202
  su_root_run(ctx->root); TEST(ctx->completed, ctx->asynch.a); 
  ctx->completed = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1203

1204 1205
  TEST_1(soa_is_complete(ctx->asynch.a));