test_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE test_ops.c
  25. * @brief High-level test framework for Sofia SIP User Agent Engine
  26. *
  27. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  28. * @author Martti Mela <Martti Mela@nokia.com>
  29. *
  30. * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
  31. */
  32. #include "config.h"
  33. #include "test_nua.h"
  34. #if HAVE_FUNC
  35. #elif HAVE_FUNCTION
  36. #define __func__ __FUNCTION__
  37. #else
  38. #define __func__ name
  39. #endif
  40. int save_events(CONDITION_PARAMS)
  41. {
  42. return save_event_in_list(ctx, event, ep, call) == event_is_normal;
  43. }
  44. int until_final_response(CONDITION_PARAMS)
  45. {
  46. return status >= 200;
  47. }
  48. int save_until_final_response(CONDITION_PARAMS)
  49. {
  50. save_event_in_list(ctx, event, ep, call);
  51. return
  52. nua_r_set_params <= event && event < nua_i_network_changed
  53. && status >= 200;
  54. }
  55. /** Save events.
  56. *
  57. * Terminate when a event is saved.
  58. */
  59. int save_until_received(CONDITION_PARAMS)
  60. {
  61. return save_event_in_list(ctx, event, ep, call) == event_is_normal;
  62. }
  63. /** Save events until nua_i_outbound is received. */
  64. int save_until_special(CONDITION_PARAMS)
  65. {
  66. return save_event_in_list(ctx, event, ep, call) == event_is_special;
  67. }
  68. /* Return call state from event tag list */
  69. int callstate(tagi_t const *tags)
  70. {
  71. tagi_t const *ti = tl_find(tags, nutag_callstate);
  72. return ti ? ti->t_value : -1;
  73. }
  74. /* Return true if offer is sent */
  75. int is_offer_sent(tagi_t const *tags)
  76. {
  77. tagi_t const *ti = tl_find(tags, nutag_offer_sent);
  78. return ti ? ti->t_value : 0;
  79. }
  80. /* Return true if answer is sent */
  81. int is_answer_sent(tagi_t const *tags)
  82. {
  83. tagi_t const *ti = tl_find(tags, nutag_answer_sent);
  84. return ti ? ti->t_value : 0;
  85. }
  86. /* Return true if offer is recv */
  87. int is_offer_recv(tagi_t const *tags)
  88. {
  89. tagi_t const *ti = tl_find(tags, nutag_offer_recv);
  90. return ti ? ti->t_value : 0;
  91. }
  92. /* Return true if answer is recv */
  93. int is_answer_recv(tagi_t const *tags)
  94. {
  95. tagi_t const *ti = tl_find(tags, nutag_answer_recv);
  96. return ti ? ti->t_value : 0;
  97. }
  98. /* Return true if offer/answer is sent/recv */
  99. int is_offer_answer_done(tagi_t const *tags)
  100. {
  101. tagi_t const *ti;
  102. return
  103. ((ti = tl_find(tags, nutag_answer_recv)) && ti->t_value) ||
  104. ((ti = tl_find(tags, nutag_offer_sent)) && ti->t_value) ||
  105. ((ti = tl_find(tags, nutag_offer_recv)) && ti->t_value) ||
  106. ((ti = tl_find(tags, nutag_answer_sent)) && ti->t_value);
  107. }
  108. /* Return audio state from event tag list */
  109. int audio_activity(tagi_t const *tags)
  110. {
  111. tagi_t const *ti = tl_find(tags, soatag_active_audio);
  112. return ti ? ti->t_value : -1;
  113. }
  114. /* Return video state from event tag list */
  115. int video_activity(tagi_t const *tags)
  116. {
  117. tagi_t const *ti = tl_find(tags, soatag_active_video);
  118. return ti ? ti->t_value : -1;
  119. }
  120. void print_event(nua_event_t event,
  121. char const *operation,
  122. int status, char const *phrase,
  123. nua_t *nua, struct context *ctx,
  124. struct endpoint *ep,
  125. nua_handle_t *nh, struct call *call,
  126. sip_t const *sip,
  127. tagi_t tags[])
  128. {
  129. tagi_t const *t;
  130. static su_nanotime_t started = 0;
  131. su_nanotime_t now;
  132. char timestamp[32];
  133. su_nanotime(&now);
  134. if (started == 0) started = now;
  135. now -= started; now /= 1000000;
  136. snprintf(timestamp, sizeof timestamp, "%03u.%03u",
  137. (unsigned)(now / 1000), (unsigned)(now % 1000));
  138. if (event == nua_i_state) {
  139. fprintf(stderr, "%s %s.nua(%p): event %s %s\n", timestamp,
  140. ep->name, (void *)nh, nua_event_name(event),
  141. nua_callstate_name(callstate(tags)));
  142. }
  143. else if ((int)event >= nua_r_set_params) {
  144. t = tl_find(tags, nutag_substate);
  145. if (t) {
  146. fprintf(stderr, "%s %s.nua(%p): event %s status %u %s (%s)\n", timestamp,
  147. ep->name, (void*)nh, nua_event_name(event), status, phrase,
  148. nua_substate_name(t->t_value));
  149. }
  150. else {
  151. fprintf(stderr, "%s %s.nua(%p): event %s status %u %s\n", timestamp,
  152. ep->name, (void *)nh, nua_event_name(event), status, phrase);
  153. }
  154. }
  155. else if (event == nua_i_notify) {
  156. t = tl_find(tags, nutag_substate);
  157. fprintf(stderr, "%s %s.nua(%p): event %s %s (%s)\n", timestamp,
  158. ep->name, (void *)nh, nua_event_name(event), phrase,
  159. nua_substate_name(t ? t->t_value : 0));
  160. }
  161. else if ((int)event >= nua_i_bye ||
  162. event == nua_i_invite || event == nua_i_cancel ||
  163. event == nua_i_ack) {
  164. fprintf(stderr, "%s %s.nua(%p): event %s %03d %s\n", timestamp,
  165. ep->name, (void *)nh, nua_event_name(event), status, phrase);
  166. }
  167. else if ((int)event >= 0) {
  168. fprintf(stderr, "%s %s.nua(%p): event %s %s\n", timestamp,
  169. ep->name, (void *)nh, nua_event_name(event), phrase);
  170. }
  171. else if (status > 0) {
  172. fprintf(stderr, "%s %s.nua(%p): call %s() with status %u %s\n", timestamp,
  173. ep->name, (void *)nh, operation, status, phrase);
  174. }
  175. else {
  176. t = tl_find(tags, siptag_subject_str);
  177. if (t && t->t_value) {
  178. char const *subject = (char const *)t->t_value;
  179. fprintf(stderr, "%s %s.nua(%p): call %s() \"%s\"\n", timestamp,
  180. ep->name, (void *)nh, operation, subject);
  181. }
  182. else
  183. fprintf(stderr, "%s %s.nua(%p): call %s()\n", timestamp,
  184. ep->name, (void *)nh, operation);
  185. }
  186. if (tags &&
  187. ((tstflags & tst_verbatim) || ctx->print_tags || ep->print_tags))
  188. tl_print(stderr, "", tags);
  189. }
  190. void ep_callback(nua_event_t event,
  191. int status, char const *phrase,
  192. nua_t *nua, struct context *ctx,
  193. struct endpoint *ep,
  194. nua_handle_t *nh, struct call *call,
  195. sip_t const *sip,
  196. tagi_t tags[])
  197. {
  198. if (ep->printer)
  199. ep->printer(event, "", status, phrase, nua, ctx, ep, nh, call, sip, tags);
  200. if (call == NULL && nh) {
  201. for (call = ep->call; call; call = call->next) {
  202. if (!call->nh)
  203. break;
  204. if (nh == call->nh)
  205. break;
  206. }
  207. if (call && call->nh == NULL) {
  208. call->nh = nh;
  209. nua_handle_bind(nh, call);
  210. }
  211. }
  212. if ((ep->next_condition == NULL ||
  213. ep->next_condition(event, status, phrase,
  214. nua, ctx, ep, nh, call, sip, tags))
  215. &&
  216. (ep->next_event == -1 || ep->next_event == event))
  217. ep->running = 0;
  218. ep->last_event = event;
  219. if (call == NULL && nh)
  220. nua_handle_destroy(nh);
  221. }
  222. void a_callback(nua_event_t event,
  223. int status, char const *phrase,
  224. nua_t *nua, struct context *ctx,
  225. nua_handle_t *nh, struct call *call,
  226. sip_t const *sip,
  227. tagi_t tags[])
  228. {
  229. ep_callback(event, status, phrase, nua, ctx, &ctx->a, nh, call, sip, tags);
  230. }
  231. void b_callback(nua_event_t event,
  232. int status, char const *phrase,
  233. nua_t *nua, struct context *ctx,
  234. nua_handle_t *nh, struct call *call,
  235. sip_t const *sip,
  236. tagi_t tags[])
  237. {
  238. ep_callback(event, status, phrase, nua, ctx, &ctx->b, nh, call, sip, tags);
  239. }
  240. void c_callback(nua_event_t event,
  241. int status, char const *phrase,
  242. nua_t *nua, struct context *ctx,
  243. nua_handle_t *nh, struct call *call,
  244. sip_t const *sip,
  245. tagi_t tags[])
  246. {
  247. ep_callback(event, status, phrase, nua, ctx, &ctx->c, nh, call, sip, tags);
  248. }
  249. void run_abc_until(struct context *ctx,
  250. nua_event_t a_event, condition_function *a_condition,
  251. nua_event_t b_event, condition_function *b_condition,
  252. nua_event_t c_event, condition_function *c_condition)
  253. {
  254. struct endpoint *a = &ctx->a, *b = &ctx->b, *c = &ctx->c;
  255. a->next_event = a_event;
  256. a->next_condition = a_condition;
  257. a->last_event = -1;
  258. a->running = a_condition != NULL && a_condition != save_events;
  259. a->running |= a_event != -1;
  260. memset(&a->flags, 0, sizeof a->flags);
  261. b->next_event = b_event;
  262. b->next_condition = b_condition;
  263. b->last_event = -1;
  264. b->running = b_condition != NULL && b_condition != save_events;
  265. b->running |= b_event != -1;
  266. memset(&b->flags, 0, sizeof b->flags);
  267. c->next_event = c_event;
  268. c->next_condition = c_condition;
  269. c->last_event = -1;
  270. c->running = c_condition != NULL && c_condition != save_events;
  271. c->running |= c_event != -1;
  272. memset(&c->flags, 0, sizeof c->flags);
  273. for (; a->running || b->running || c->running;) {
  274. su_root_step(ctx->root, 100);
  275. }
  276. }
  277. void run_ab_until(struct context *ctx,
  278. nua_event_t a_event, condition_function *a_condition,
  279. nua_event_t b_event, condition_function *b_condition)
  280. {
  281. run_abc_until(ctx, a_event, a_condition, b_event, b_condition, -1, NULL);
  282. }
  283. void run_bc_until(struct context *ctx,
  284. nua_event_t b_event, condition_function *b_condition,
  285. nua_event_t c_event, condition_function *c_condition)
  286. {
  287. run_abc_until(ctx, -1, NULL, b_event, b_condition, c_event, c_condition);
  288. }
  289. int run_a_until(struct context *ctx,
  290. nua_event_t a_event,
  291. condition_function *a_condition)
  292. {
  293. run_abc_until(ctx, a_event, a_condition, -1, NULL, -1, NULL);
  294. return ctx->a.last_event;
  295. }
  296. int run_b_until(struct context *ctx,
  297. nua_event_t b_event,
  298. condition_function *b_condition)
  299. {
  300. run_abc_until(ctx, -1, NULL, b_event, b_condition, -1, NULL);
  301. return ctx->b.last_event;
  302. }
  303. int run_c_until(struct context *ctx,
  304. nua_event_t event,
  305. condition_function *condition)
  306. {
  307. run_abc_until(ctx, -1, NULL, -1, NULL, event, condition);
  308. return ctx->c.last_event;
  309. }
  310. #define OPERATION(X, x) \
  311. int X(struct endpoint *ep, \
  312. struct call *call, nua_handle_t *nh, \
  313. tag_type_t tag, tag_value_t value, \
  314. ...) \
  315. { \
  316. ta_list ta; \
  317. ta_start(ta, tag, value); \
  318. \
  319. if (ep->printer) \
  320. ep->printer(-1, "nua_" #x, 0, "", ep->nua, ep->ctx, ep, \
  321. nh, call, NULL, ta_args(ta)); \
  322. \
  323. nua_##x(nh, ta_tags(ta)); \
  324. \
  325. ta_end(ta); \
  326. return 0; \
  327. } extern int dummy
  328. OPERATION(INVITE, invite);
  329. OPERATION(ACK, ack);
  330. OPERATION(BYE, bye);
  331. OPERATION(CANCEL, cancel);
  332. OPERATION(AUTHENTICATE, authenticate);
  333. OPERATION(UPDATE, update);
  334. OPERATION(INFO, info);
  335. OPERATION(PRACK, prack);
  336. OPERATION(REFER, refer);
  337. OPERATION(MESSAGE, message);
  338. OPERATION(METHOD, method);
  339. OPERATION(OPTIONS, options);
  340. OPERATION(PUBLISH, publish);
  341. OPERATION(UNPUBLISH, unpublish);
  342. OPERATION(REGISTER, register);
  343. OPERATION(UNREGISTER, unregister);
  344. OPERATION(SUBSCRIBE, subscribe);
  345. OPERATION(UNSUBSCRIBE, unsubscribe);
  346. OPERATION(NOTIFY, notify);
  347. OPERATION(NOTIFIER, notifier);
  348. OPERATION(TERMINATE, terminate);
  349. OPERATION(AUTHORIZE, authorize);
  350. /* Respond via endpoint and handle */
  351. int RESPOND(struct endpoint *ep,
  352. struct call *call,
  353. nua_handle_t *nh,
  354. int status, char const *phrase,
  355. tag_type_t tag, tag_value_t value,
  356. ...)
  357. {
  358. ta_list ta;
  359. ta_start(ta, tag, value);
  360. if (ep->printer)
  361. ep->printer(-1, "nua_respond", status, phrase, ep->nua, ep->ctx, ep,
  362. nh, call, NULL, ta_args(ta));
  363. nua_respond(nh, status, phrase, ta_tags(ta));
  364. ta_end(ta);
  365. return 0;
  366. }
  367. /* Destroy a handle */
  368. int DESTROY(struct endpoint *ep,
  369. struct call *call,
  370. nua_handle_t *nh)
  371. {
  372. if (ep->printer)
  373. ep->printer(-1, "nua_handle_destroy", 0, "", ep->nua, ep->ctx, ep,
  374. nh, call, NULL, NULL);
  375. nua_handle_destroy(nh);
  376. if (call->nh == nh)
  377. call->nh = NULL;
  378. return 0;
  379. }
  380. /* Reject all but currently used handles */
  381. struct call *check_handle(struct endpoint *ep,
  382. struct call *call,
  383. nua_handle_t *nh,
  384. int status, char const *phrase)
  385. {
  386. if (call)
  387. return call;
  388. if (status)
  389. RESPOND(ep, call, nh, status, phrase, TAG_END());
  390. nua_handle_destroy(nh);
  391. return NULL;
  392. }
  393. /* Save nua event in call-specific list */
  394. int save_event_in_list(struct context *ctx,
  395. nua_event_t nevent,
  396. struct endpoint *ep,
  397. struct call *call)
  398. {
  399. struct eventlist *list;
  400. struct event *e;
  401. int action = ep->is_special(nevent);
  402. if (action == event_is_extra)
  403. return 0;
  404. else if (action == event_is_special || call == NULL)
  405. list = ep->specials;
  406. else if (call->events)
  407. list = call->events;
  408. else
  409. list = ep->events;
  410. e = su_zalloc(ctx->home, sizeof *e);
  411. if (!e) { perror("su_zalloc"), abort(); }
  412. if (!nua_save_event(ep->nua, e->saved_event)) {
  413. su_free(ctx->home, e);
  414. return -1;
  415. }
  416. *(e->prev = list->tail) = e; list->tail = &e->next;
  417. e->call = call;
  418. e->data = nua_event_data(e->saved_event);
  419. return action;
  420. }
  421. /* Free nua events from endpoint list */
  422. void free_events_in_list(struct context *ctx,
  423. struct eventlist *list)
  424. {
  425. struct event *e;
  426. while ((e = list->head)) {
  427. if ((*e->prev = e->next))
  428. e->next->prev = e->prev;
  429. nua_destroy_event(e->saved_event);
  430. su_free(ctx->home, e);
  431. }
  432. list->tail = &list->head;
  433. }
  434. void free_event_in_list(struct context *ctx,
  435. struct eventlist *list,
  436. struct event *e)
  437. {
  438. if (e) {
  439. if ((*e->prev = e->next))
  440. e->next->prev = e->prev;
  441. nua_destroy_event(e->saved_event);
  442. su_free(ctx->home, e);
  443. if (list->head == NULL)
  444. list->tail = &list->head;
  445. }
  446. }
  447. struct event *event_by_type(struct event *e, nua_event_t etype)
  448. {
  449. for (; e; e = e->next) {
  450. if (e->data->e_event == etype)
  451. break;
  452. }
  453. return e;
  454. }
  455. size_t count_events(struct event const *e)
  456. {
  457. size_t n;
  458. for (n = 0; e; e = e->next)
  459. n++;
  460. return n;
  461. }
  462. int is_special(nua_event_t e)
  463. {
  464. if (e == nua_i_active || e == nua_i_terminated)
  465. return event_is_extra;
  466. if (e == nua_i_outbound)
  467. return event_is_special;
  468. return event_is_normal;
  469. }
  470. void
  471. endpoint_init(struct context *ctx, struct endpoint *e, char id)
  472. {
  473. e->name[0] = id;
  474. e->ctx = ctx;
  475. e->is_special = is_special;
  476. call_init(e->call);
  477. call_init(e->reg);
  478. eventlist_init(e->events);
  479. eventlist_init(e->specials);
  480. }
  481. void nolog(void *stream, char const *fmt, va_list ap) {}