test_nua_api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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_nua_api.c
  25. * @brief NUA API tester.
  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. #include "sofia-sip/tport_tag.h"
  35. #if HAVE_FUNC
  36. #elif HAVE_FUNCTION
  37. #define __func__ __FUNCTION__
  38. #else
  39. #define __func__ "test_nua_api_errors"
  40. #endif
  41. /* ------------------------------------------------------------------------ */
  42. /* API tests */
  43. SOFIAPUBVAR su_log_t nua_log[];
  44. int check_set_status(int status, char const *phrase)
  45. {
  46. return status == 200 && strcmp(phrase, sip_200_OK) == 0;
  47. }
  48. int test_nua_api_errors(struct context *ctx)
  49. {
  50. BEGIN();
  51. /* Invoke every API function with invalid arguments */
  52. int level;
  53. int status; char const *phrase;
  54. if (print_headings)
  55. printf("TEST NUA-1.0: test API\n");
  56. /* This is a nasty macro. Test it. */
  57. #define SET_STATUS1(x) ((status = x), status), (phrase = ((void)x))
  58. TEST_1(check_set_status(SET_STATUS1(SIP_200_OK)));
  59. TEST(status, 200); TEST_S(phrase, sip_200_OK);
  60. su_log_init(nua_log);
  61. level = nua_log->log_level;
  62. if (!(tstflags & tst_verbatim))
  63. su_log_set_level(nua_log, 0); /* Log at level 0 by default */
  64. TEST_1(!nua_create(NULL, NULL, NULL, TAG_END()));
  65. TEST_VOID(nua_shutdown(NULL));
  66. TEST_VOID(nua_destroy(NULL));
  67. TEST_VOID(nua_set_params(NULL, TAG_END()));
  68. TEST_VOID(nua_get_params(NULL, TAG_END()));
  69. TEST_1(!nua_default(NULL));
  70. TEST_1(!nua_handle(NULL, NULL, TAG_END()));
  71. TEST_VOID(nua_handle_destroy(NULL));
  72. TEST_VOID(nua_handle_bind(NULL, NULL));
  73. TEST_1(!nua_handle_has_invite(NULL));
  74. TEST_1(!nua_handle_has_subscribe(NULL));
  75. TEST_1(!nua_handle_has_register(NULL));
  76. TEST_1(!nua_handle_has_active_call(NULL));
  77. TEST_1(!nua_handle_has_call_on_hold(NULL));
  78. TEST_1(!nua_handle_has_events(NULL));
  79. TEST_1(!nua_handle_has_registrations(NULL));
  80. TEST_1(!nua_handle_remote(NULL));
  81. TEST_1(!nua_handle_local(NULL));
  82. TEST_S(nua_event_name(-111), "NUA_UNKNOWN");
  83. TEST_VOID(nua_register(NULL, TAG_END()));
  84. TEST_VOID(nua_unregister(NULL, TAG_END()));
  85. TEST_VOID(nua_invite(NULL, TAG_END()));
  86. TEST_VOID(nua_ack(NULL, TAG_END()));
  87. TEST_VOID(nua_prack(NULL, TAG_END()));
  88. TEST_VOID(nua_options(NULL, TAG_END()));
  89. TEST_VOID(nua_publish(NULL, TAG_END()));
  90. TEST_VOID(nua_message(NULL, TAG_END()));
  91. TEST_VOID(nua_chat(NULL, TAG_END()));
  92. TEST_VOID(nua_info(NULL, TAG_END()));
  93. TEST_VOID(nua_subscribe(NULL, TAG_END()));
  94. TEST_VOID(nua_unsubscribe(NULL, TAG_END()));
  95. TEST_VOID(nua_notify(NULL, TAG_END()));
  96. TEST_VOID(nua_notifier(NULL, TAG_END()));
  97. TEST_VOID(nua_terminate(NULL, TAG_END()));
  98. TEST_VOID(nua_refer(NULL, TAG_END()));
  99. TEST_VOID(nua_update(NULL, TAG_END()));
  100. TEST_VOID(nua_bye(NULL, TAG_END()));
  101. TEST_VOID(nua_cancel(NULL, TAG_END()));
  102. TEST_VOID(nua_authenticate(NULL, TAG_END()));
  103. TEST_VOID(nua_redirect(NULL, TAG_END()));
  104. TEST_VOID(nua_respond(NULL, 0, "", TAG_END()));
  105. TEST_1(!nua_handle_home(NULL));
  106. TEST_1(!nua_save_event(NULL, NULL));
  107. TEST_1(!nua_event_data(NULL));
  108. TEST_VOID(nua_destroy_event(NULL));
  109. {
  110. nua_saved_event_t event[1];
  111. memset(event, 0, sizeof event);
  112. TEST_1(!nua_save_event(NULL, event));
  113. TEST_1(!nua_event_data(event));
  114. TEST_VOID(nua_destroy_event(event));
  115. }
  116. su_log_set_level(nua_log, level);
  117. if (print_headings)
  118. printf("TEST NUA-1.0: PASSED\n");
  119. END();
  120. }
  121. /* ======================================================================== */
  122. void destroy_callback(nua_event_t event,
  123. int status, char const *phrase,
  124. nua_t *nua, struct context *ctx,
  125. nua_handle_t *nh, struct call *call,
  126. sip_t const *sip,
  127. tagi_t tags[])
  128. {
  129. if (status >= 200) {
  130. nua_destroy(ctx->a.nua), ctx->a.nua = NULL;
  131. su_root_break(ctx->root);
  132. }
  133. }
  134. /* Test different nua_destroy() corner cases */
  135. int test_nua_destroy(struct context *ctx)
  136. {
  137. BEGIN();
  138. struct endpoint *a = &ctx->a;
  139. TEST_1(ctx->root = su_root_create(NULL));
  140. #if 0
  141. a->nua = nua_create(ctx->root, destroy_callback, ctx,
  142. NUTAG_URL("sip:0.0.0.0:*"),
  143. TAG_IF(ctx->a.logging, TPTAG_LOG(1)),
  144. TAG_END());
  145. TEST_1(a->nua);
  146. nua_get_params(a->nua, TAG_ANY(), TAG_END());
  147. su_root_run(ctx->root);
  148. TEST_1(a->nua == NULL);
  149. #endif
  150. a->nua = nua_create(ctx->root, destroy_callback, ctx,
  151. NUTAG_URL("sip:0.0.0.0:*"),
  152. TAG_IF(ctx->a.logging, TPTAG_LOG(1)),
  153. TAG_END());
  154. TEST_1(a->nua);
  155. nua_shutdown(a->nua);
  156. su_root_run(ctx->root);
  157. TEST_1(a->nua == NULL);
  158. su_root_destroy(ctx->root), ctx->root = NULL;
  159. END();
  160. }
  161. /* ======================================================================== */
  162. int test_byecancel_without_invite(struct context *ctx)
  163. {
  164. BEGIN();
  165. struct endpoint *a = &ctx->a, *b = &ctx->b;
  166. struct call *a_call = a->call;
  167. struct event *e;
  168. int internal_error = 900;
  169. if (print_headings)
  170. printf("TEST NUA-1.2.1: CANCEL without INVITE\n");
  171. TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
  172. CANCEL(a, a_call, a_call->nh, TAG_END());
  173. run_a_until(ctx, -1, save_until_final_response);
  174. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_cancel);
  175. TEST(e->data->e_status, 481);
  176. TEST_1(!e->next);
  177. free_events_in_list(ctx, a->events);
  178. nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  179. if (print_headings)
  180. printf("TEST NUA-1.2.1: PASSED\n");
  181. /* -BYE without INVITE--------------------------------------------------- */
  182. if (print_headings)
  183. printf("TEST NUA-1.2.2: BYE without INVITE\n");
  184. a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END());
  185. TEST_1(a_call->nh);
  186. BYE(a, a_call, a_call->nh, TAG_END());
  187. run_a_until(ctx, -1, save_until_final_response);
  188. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_bye);
  189. TEST(e->data->e_status, internal_error);
  190. TEST_1(!e->next);
  191. free_events_in_list(ctx, a->events);
  192. nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  193. if (print_headings)
  194. printf("TEST NUA-1.2.2: PASSED\n");
  195. END();
  196. }
  197. int test_unregister_without_register(struct context *ctx)
  198. {
  199. BEGIN();
  200. struct endpoint *a = &ctx->a, *b = &ctx->b;
  201. struct call *a_call = a->call;
  202. struct event *e;
  203. /* -Un-register without REGISTER--------------------------------------- */
  204. if (print_headings)
  205. printf("TEST NUA-1.2.3: unregister without register\n");
  206. a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(a->to), TAG_END());
  207. TEST_1(a_call->nh);
  208. UNREGISTER(a, a_call, a_call->nh, TAG_END());
  209. run_a_until(ctx, -1, save_until_final_response);
  210. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_unregister);
  211. if (e->data->e_status == 401)
  212. TEST(e->data->e_status, 401);
  213. else
  214. TEST(e->data->e_status, 406);
  215. TEST_1(!e->next);
  216. free_events_in_list(ctx, a->events);
  217. nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  218. if (print_headings)
  219. printf("TEST NUA-1.2.3: PASSED\n");
  220. /* -Un-publish without publish--------------------------------------- */
  221. if (print_headings)
  222. printf("TEST NUA-1.2.4: unpublish without publish\n");
  223. a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END());
  224. TEST_1(a_call->nh);
  225. UNPUBLISH(a, a_call, a_call->nh, TAG_END());
  226. run_a_until(ctx, -1, save_until_final_response);
  227. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_unpublish);
  228. TEST(e->data->e_status, 404);
  229. TEST_1(!e->next);
  230. free_events_in_list(ctx, a->events);
  231. nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  232. if (print_headings)
  233. printf("TEST NUA-1.2.4: PASSED\n");
  234. END();
  235. }
  236. /* -terminate without notifier--------------------------------------- */
  237. int test_terminate_without_notifier(struct context *ctx)
  238. {
  239. BEGIN();
  240. struct endpoint *a = &ctx->a, *b = &ctx->b;
  241. struct call *a_call = a->call;
  242. struct event *e;
  243. int internal_error = 900;
  244. if (print_headings)
  245. printf("TEST NUA-1.2.5: terminate without notifier\n");
  246. TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
  247. TERMINATE(a, a_call, a_call->nh, TAG_END());
  248. run_a_until(ctx, -1, save_until_final_response);
  249. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_terminate);
  250. TEST(e->data->e_status, internal_error);
  251. TEST_1(!e->next);
  252. free_events_in_list(ctx, a->events);
  253. AUTHORIZE(a, a_call, a_call->nh, TAG_END());
  254. run_a_until(ctx, -1, save_until_final_response);
  255. TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_authorize);
  256. TEST(e->data->e_status, internal_error);
  257. TEST_1(!e->next);
  258. free_events_in_list(ctx, a->events);
  259. nua_handle_destroy(a_call->nh), a_call->nh = NULL;
  260. if (print_headings)
  261. printf("TEST NUA-1.2.5: PASSED\n");
  262. END();
  263. }
  264. int destroy_on_503(CONDITION_PARAMS)
  265. {
  266. save_event_in_list(ctx, event, ep, call);
  267. if (status == 503) {
  268. assert(nh == call->nh);
  269. nua_handle_destroy(call->nh), call->nh = NULL;
  270. }
  271. return
  272. nua_r_set_params <= event && event < nua_i_network_changed
  273. && status >= 200;
  274. }
  275. int test_register_503(struct context *ctx)
  276. {
  277. BEGIN();
  278. struct endpoint *a = &ctx->a;
  279. struct call *a_reg = a->reg;
  280. struct event *e;
  281. /* REGISTER test
  282. A
  283. |------REGISTER--\
  284. |<-------503-----/
  285. |
  286. */
  287. if (print_headings)
  288. printf("TEST NUA-1.2.6: REGISTER with bad domain\n");
  289. TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
  290. REGISTER(a, a_reg, a_reg->nh,
  291. NUTAG_REGISTRAR(URL_STRING_MAKE("sip:bad.domain")),
  292. SIPTAG_TO_STR("sip:lissu@bad.domain"),
  293. TAG_END());
  294. run_a_until(ctx, -1, destroy_on_503);
  295. TEST_1(e = a->events->head);
  296. TEST_E(e->data->e_event, nua_r_register);
  297. TEST(e->data->e_status, 503);
  298. TEST_1(!e->next);
  299. free_events_in_list(ctx, a->events);
  300. TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
  301. REGISTER(a, a_reg, a_reg->nh,
  302. NUTAG_REGISTRAR(URL_STRING_MAKE("sip:bad.domain")),
  303. SIPTAG_TO_STR("sip:lissu@bad.domain"),
  304. TAG_END());
  305. nua_handle_destroy(a_reg->nh), a_reg->nh = NULL;
  306. if (print_headings)
  307. printf("TEST NUA-1.2.6: PASSED\n");
  308. END();
  309. }
  310. int test_stack_errors(struct context *ctx)
  311. {
  312. BEGIN();
  313. struct endpoint *a = &ctx->a;
  314. if (print_headings)
  315. printf("TEST NUA-1.2: Stack error handling\n");
  316. TEST_1(ctx->root == NULL);
  317. TEST_1(ctx->root = su_root_create(NULL));
  318. a->nua = nua_create(ctx->root, a_callback, ctx,
  319. NUTAG_URL("sip:0.0.0.0:*"),
  320. TAG_IF(ctx->a.logging, TPTAG_LOG(1)),
  321. TAG_END());
  322. TEST_1(a->nua);
  323. TEST(test_byecancel_without_invite(ctx), 0);
  324. if (ctx->proxy_tests)
  325. TEST(test_unregister_without_register(ctx), 0);
  326. TEST(test_terminate_without_notifier(ctx), 0);
  327. TEST(test_register_503(ctx), 0);
  328. TEST(test_register_503(ctx), 0);
  329. nua_shutdown(a->nua);
  330. run_a_until(ctx, -1, until_final_response);
  331. TEST_VOID(nua_destroy(a->nua));
  332. a->nua = NULL;
  333. su_root_destroy(ctx->root), ctx->root = NULL;
  334. if (print_headings)
  335. printf("TEST NUA-1.2: PASSED\n");
  336. END();
  337. }