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 
25 /**@CFILE test_nua_simple.c
26  * @brief NUA-11: Test SIMPLE methods: MESSAGE, PUBLISH and SUBSCRIBE/NOTIFY.
27  *
28  * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29  * @author Martti Mela <Martti Mela@nokia.com>
30  *
31  * @date Created: Wed Aug 17 12:12:12 EEST 2005 ppessi
32  */
33 
34 #include "config.h"
35 
36 #include "test_nua.h"
37 #include <sofia-sip/su_tag_class.h>
38 
39 #if HAVE_FUNC
40 #elif HAVE_FUNCTION
41 #define __func__ __FUNCTION__
42 #else
43 #define __func__ "test_simple"
44 #endif
45 
46 extern int accept_request(CONDITION_PARAMS);
47 
48 int save_until_nth_final_response(CONDITION_PARAMS);
49 int accept_n_notifys(CONDITION_PARAMS);
50 
test_message(struct context * ctx)51 int test_message(struct context *ctx)
52 {
53   BEGIN();
54 
55   struct endpoint *a = &ctx->a,  *b = &ctx->b;
56   struct call *a_call = a->call, *b_call = b->call;
57   struct event *e;
58   sip_t const *sip;
59   url_t url[1];
60 
61 /* Message test
62 
63    A			B
64    |-------MESSAGE----->|
65    |<-------200---------|
66    |			|
67 
68 */
69   if (print_headings)
70     printf("TEST NUA-11.1.1: MESSAGE\n");
71 
72   if (ctx->proxy_tests)
73     *url = *b->to->a_url;
74   else
75     *url = *b->contact->m_url;
76 
77   /* Test that query part is included in request sent to B */
78   url->url_headers = "organization=United%20Testers";
79 
80   TEST_1(a_call->nh = nua_handle(a->nua, a_call, TAG_END()));
81 
82   MESSAGE(a, a_call, a_call->nh,
83 	  NUTAG_URL(url),
84 	  SIPTAG_SUBJECT_STR("NUA-11.1.1"),
85 	  SIPTAG_CONTENT_TYPE_STR("text/plain"),
86 	  SIPTAG_PAYLOAD_STR("Hello hellO!\n"),
87 	  TAG_END());
88 
89   run_ab_until(ctx, -1, save_until_final_response, -1, save_until_received);
90 
91   /* Client events:
92      nua_message(), nua_r_message
93   */
94   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_message);
95   TEST(e->data->e_status, 200);
96   TEST_1(!e->next);
97 
98   /*
99    Server events:
100    nua_i_message
101   */
102   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_message);
103   TEST(e->data->e_status, 200);
104   TEST_1(sip = sip_object(e->data->e_msg));
105   TEST_1(sip->sip_subject && sip->sip_subject->g_string);
106   TEST_S(sip->sip_subject->g_string, "NUA-11.1.1");
107   TEST_1(sip->sip_organization);
108   TEST_S(sip->sip_organization->g_string, "United Testers");
109   TEST_1(!e->next);
110 
111   free_events_in_list(ctx, a->events);
112   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
113 
114   free_events_in_list(ctx, b->events);
115   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
116 
117   if (print_headings)
118     printf("TEST NUA-11.1.1: PASSED\n");
119 
120 /* MESSAGE as application method
121 
122    A			B
123    |-------MESSAGE----->|
124    |<-------202---------|
125    |			|
126 */
127 
128   if (print_headings)
129     printf("TEST NUA-11.1.2: MESSAGE\n");
130 
131   nua_set_params(b->nua, NUTAG_APPL_METHOD("MESSAGE"), TAG_END());
132   run_b_until(ctx, nua_r_set_params, until_final_response);
133 
134   TEST_1(a_call->nh = nua_handle(a->nua, a_call, TAG_END()));
135 
136   MESSAGE(a, a_call, a_call->nh,
137 	  NUTAG_URL(url),
138 	  SIPTAG_SUBJECT_STR("NUA-11.1.2"),
139 	  SIPTAG_CONTENT_TYPE_STR("text/plain"),
140 	  SIPTAG_PAYLOAD_STR("Hello hellO!\n"),
141 	  TAG_END());
142 
143   run_ab_until(ctx, -1, save_until_final_response, -1, accept_request);
144 
145   /* Client events:
146      nua_message(), nua_r_message
147   */
148   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_message);
149   TEST(e->data->e_status, 202);
150   TEST_1(sip = sip_object(e->data->e_msg));
151   TEST_1(sip_user_agent(sip));
152   TEST_S(sip_user_agent(sip)->g_value, "007");
153   TEST_1(!e->next);
154 
155   /*
156    Server events:
157    nua_i_message
158   */
159   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_message);
160   TEST(e->data->e_status, 100);
161   TEST_1(sip = sip_object(e->data->e_msg));
162   TEST_1(sip->sip_subject && sip->sip_subject->g_string);
163   TEST_S(sip->sip_subject->g_string, "NUA-11.1.2");
164   TEST_1(sip->sip_organization);
165   TEST_S(sip->sip_organization->g_string, "United Testers");
166   TEST_1(!e->next);
167 
168   free_events_in_list(ctx, a->events);
169   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
170 
171   free_events_in_list(ctx, b->events);
172   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
173 
174   if (print_headings)
175     printf("TEST NUA-11.1.2: PASSED\n");
176 
177 
178 /* Message test
179 
180    A
181    |-------MESSAGE--\
182    |<---------------/
183    |--------200-----\
184    |<---------------/
185    |
186 
187 */
188   if (print_headings)
189     printf("TEST NUA-11.2: MESSAGE to myself\n");
190 
191   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(a->to), TAG_END()));
192 
193   MESSAGE(a, a_call, a_call->nh,
194 	  /* We cannot reach us by using our contact! */
195 	  NUTAG_URL(!ctx->p && !ctx->proxy_tests ? a->contact->m_url : NULL),
196 	  SIPTAG_SUBJECT_STR("NUA-11.2"),
197 	  SIPTAG_CONTENT_TYPE_STR("text/plain"),
198 	  SIPTAG_PAYLOAD_STR("Hello hellO!\n"),
199 	  TAG_END());
200 
201   run_a_until(ctx, -1, save_until_final_response);
202 
203   /* Events:
204      nua_message(), nua_i_message, nua_r_message
205   */
206   TEST_1(e = a->specials->head);
207   while (e->data->e_event == nua_i_outbound)
208     e = e->next;
209   TEST_E(e->data->e_event, nua_i_message);
210   TEST(e->data->e_status, 200);
211   TEST_1(sip = sip_object(e->data->e_msg));
212   TEST_1(sip->sip_subject && sip->sip_subject->g_string);
213   TEST_S(sip->sip_subject->g_string, "NUA-11.2");
214 
215   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_message);
216   TEST(e->data->e_status, 200);
217   TEST_1(!e->next);
218 
219   free_events_in_list(ctx, a->events);
220   free_events_in_list(ctx, a->specials);
221   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
222 
223   if (print_headings)
224     printf("TEST NUA-11.2: PASSED\n");
225 
226   END();
227 }
228 
accept_request(CONDITION_PARAMS)229 int accept_request(CONDITION_PARAMS)
230 {
231   msg_t *with = nua_current_request(nua);
232 
233   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
234     return 0;
235 
236   save_event_in_list(ctx, event, ep, call);
237 
238   if (status < 200) {
239     RESPOND(ep, call, nh, SIP_202_ACCEPTED,
240 	    NUTAG_WITH(with),
241 	    SIPTAG_USER_AGENT_STR("007"),
242 	    TAG_END());
243     return 1;
244   }
245 
246   return 0;
247 }
248 
249 char const *test_etag = "tagtag";
250 
respond_with_etag(CONDITION_PARAMS)251 int respond_with_etag(CONDITION_PARAMS)
252 {
253   msg_t *with = nua_current_request(nua);
254 
255   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
256     return 1;
257 
258   save_event_in_list(ctx, event, ep, call);
259 
260   switch (event) {
261     char const *etag;
262   case nua_i_publish:
263     etag = sip->sip_if_match ? sip->sip_if_match->g_value : NULL;
264     if (sip->sip_if_match && (etag == NULL || strcmp(etag, test_etag))) {
265       RESPOND(ep, call, nh, SIP_412_PRECONDITION_FAILED,
266 	      NUTAG_WITH(with),
267 	      TAG_END());
268     }
269     else {
270       RESPOND(ep, call, nh, SIP_200_OK,
271 	      NUTAG_WITH(with),
272 	      SIPTAG_ETAG_STR(test_etag),
273 	      SIPTAG_EXPIRES_STR("3600"),
274 	      SIPTAG_EXPIRES(sip->sip_expires),	/* overrides 3600 */
275 	      TAG_END());
276     }
277     return 1;
278   default:
279     return 0;
280   }
281 }
282 
close_handle(CONDITION_PARAMS)283 static int close_handle(CONDITION_PARAMS)
284 {
285   if (call->nh == nh)
286     call->nh = NULL;
287   nua_handle_destroy(nh);
288   return 1;
289 }
290 
test_publish(struct context * ctx)291 int test_publish(struct context *ctx)
292 {
293   BEGIN();
294 
295   struct endpoint *a = &ctx->a,  *b = &ctx->b;
296   struct call *a_call = a->call, *b_call = b->call;
297   struct event *e;
298   sip_t const *sip;
299 
300 
301 /* PUBLISH test
302 
303    A			B
304    |-------PUBLISH----->|
305    |<-------405---------| (method not allowed by default)
306    |			|
307    |-------PUBLISH----->|
308    |<-------501---------| (no events allowed)
309    |			|
310    |-------PUBLISH----->|
311    |<-------489---------| (event not allowed by default)
312    |			|
313    |-------PUBLISH----->|
314    |<-------200---------| (event allowed, responded)
315    |			|
316    |-----un-PUBLISH---->|
317    |<-------200---------| (event allowed, responded)
318 
319 */
320   if (print_headings)
321     printf("TEST NUA-11.3: PUBLISH\n");
322 
323   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
324 
325   PUBLISH(a, a_call, a_call->nh,
326 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
327 	  SIPTAG_EVENT_STR("presence"),
328 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
329 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
330 	  TAG_END());
331 
332   run_ab_until(ctx, -1, save_until_final_response, -1, NULL);
333 
334   /* Client events:
335      nua_publish(), nua_r_publish
336   */
337   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
338   TEST(e->data->e_status, 405);
339   TEST_1(!e->next);
340 
341   free_events_in_list(ctx, a->events);
342   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
343 
344   free_events_in_list(ctx, b->events);
345   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
346 
347   nua_set_params(b->nua, NUTAG_ALLOW("PUBLISH"), TAG_END());
348 
349   run_b_until(ctx, nua_r_set_params, until_final_response);
350 
351   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
352 
353   PUBLISH(a, a_call, a_call->nh,
354 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
355 	  SIPTAG_EVENT_STR("presence"),
356 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
357 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
358 	  TAG_END());
359 
360   run_a_until(ctx, -1, save_until_final_response);
361 
362   /* Client events:
363      nua_publish(), nua_r_publish
364   */
365   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
366   TEST(e->data->e_status, 501);	/* Not implemented */
367   TEST_1(!e->next);
368 
369   free_events_in_list(ctx, a->events);
370   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
371 
372   /* Allow presence event */
373 
374   nua_set_params(b->nua, NUTAG_ALLOW_EVENTS("presence"), TAG_END());
375 
376   run_b_until(ctx, nua_r_set_params, until_final_response);
377 
378   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
379 
380   PUBLISH(a, a_call, a_call->nh,
381 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
382 	  SIPTAG_EVENT_STR("reg"),
383 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
384 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
385 	  TAG_END());
386 
387   run_a_until(ctx, -1, save_until_final_response);
388 
389   /* Client events:
390      nua_publish(), nua_r_publish
391   */
392   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
393   TEST(e->data->e_status, 489);	/* Bad Event */
394   TEST_1(!e->next);
395 
396   free_events_in_list(ctx, a->events);
397   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
398 
399   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
400 
401   PUBLISH(a, a_call, a_call->nh,
402 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
403 	  SIPTAG_EVENT_STR("presence"),
404 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
405 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
406 	  SIPTAG_EXPIRES_STR("5"),
407 	  TAG_END());
408 
409   run_ab_until(ctx, -1, save_until_final_response, -1, respond_with_etag);
410 
411   /* Client events:
412      nua_publish(), nua_r_publish
413   */
414   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
415   TEST(e->data->e_status, 200);
416   TEST_1(sip = sip_object(e->data->e_msg));
417   TEST_1(sip->sip_etag);
418   TEST_S(sip->sip_etag->g_string, test_etag);
419   TEST_1(!e->next);
420 
421   /*
422    Server events:
423    nua_i_publish
424   */
425   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_publish);
426   TEST(e->data->e_status, 100);
427   TEST_1(!e->next);
428 
429   free_events_in_list(ctx, a->events);
430   free_events_in_list(ctx, b->events);
431   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
432 
433   if (!ctx->expensive && 0)
434     goto skip_republish;
435 
436   run_ab_until(ctx, -1, save_until_final_response, -1, respond_with_etag);
437 
438   /* Client events: nua_r_publish
439   */
440   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
441   TEST(e->data->e_status, 200);
442   TEST_1(sip = sip_object(e->data->e_msg));
443   TEST_1(sip->sip_etag);
444   TEST_S(sip->sip_etag->g_string, test_etag);
445   TEST_1(!e->next);
446 
447   free_events_in_list(ctx, a->events);
448 
449   /*
450    Server events:
451    nua_i_publish
452   */
453   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_publish);
454   TEST(e->data->e_status, 100);
455   TEST_1(sip = sip_object(e->data->e_msg));
456   TEST_1(sip = sip_object(e->data->e_msg));
457   TEST_1(sip->sip_if_match);
458   TEST_S(sip->sip_if_match->g_string, "tagtag");
459   TEST_1(!sip->sip_content_type);
460   TEST_1(!sip->sip_payload);
461   TEST_1(!e->next);
462 
463   free_events_in_list(ctx, b->events);
464   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
465 
466  skip_republish:
467 
468   UNPUBLISH(a, a_call, a_call->nh, TAG_END());
469 
470   run_ab_until(ctx, -1, save_until_final_response, -1, respond_with_etag);
471 
472   /* Client events:
473      nua_publish(), nua_r_publish
474   */
475   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_unpublish);
476   TEST(e->data->e_status, 200);
477   TEST_1(!e->next);
478 
479   /*
480    Server events:
481    nua_i_publish
482   */
483   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_publish);
484   TEST(e->data->e_status, 100);
485   TEST_1(!e->next);
486 
487   free_events_in_list(ctx, a->events);
488   free_events_in_list(ctx, b->events);
489 
490   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
491   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
492 
493   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
494 
495   /* Let server close handle without responding to PUBLISH */
496   PUBLISH(a, a_call, a_call->nh,
497 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
498 	  SIPTAG_EVENT_STR("presence"),
499 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
500 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
501 	  TAG_END());
502 
503   run_ab_until(ctx, -1, save_until_final_response, -1, close_handle);
504 
505   /* Client events:
506      nua_publish(), nua_r_publish
507   */
508   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
509   TEST(e->data->e_status, 500);
510   TEST_1(!e->next);
511 
512   free_events_in_list(ctx, a->events);
513 
514   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
515 
516   /* No Event header */
517 
518   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
519 
520   PUBLISH(a, a_call, a_call->nh,
521 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
522 	  SIPTAG_CONTENT_TYPE_STR("text/urllist"),
523 	  SIPTAG_PAYLOAD_STR("sip:example.com\n"),
524 	  TAG_END());
525 
526   run_ab_until(ctx, -1, save_until_final_response, -1, save_events);
527 
528   /* Client events:
529      nua_publish(), nua_r_publish
530   */
531   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_publish);
532   TEST(e->data->e_status, 489);
533   TEST_1(!e->next);
534 
535   free_events_in_list(ctx, a->events);
536 
537   /*
538    Server events: nothing
539   */
540   TEST_1(!b->events->head);
541 
542   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
543 
544   if (print_headings)
545     printf("TEST NUA-11.3: PASSED\n");
546 
547   END();
548 }
549 
550 static char const presence_open[] =
551     "<?xml version='1.0' encoding='UTF-8'?>\n"
552     "<presence xmlns='urn:ietf:params:xml:ns:cpim-pidf' \n"
553     "   entity='pres:bob@example.org'>\n"
554     "  <tuple id='ksac9udshce'>\n"
555     "    <status><basic>open</basic></status>\n"
556     "    <contact priority='1.0'>sip:bob@example.org</contact>\n"
557     "  </tuple>\n"
558     "</presence>\n";
559 
560 static char const presence_closed[] =
561     "<?xml version='1.0' encoding='UTF-8'?>\n"
562     "<presence xmlns='urn:ietf:params:xml:ns:cpim-pidf' \n"
563     "   entity='pres:bob@example.org'>\n"
564     "  <tuple id='ksac9udshce'>\n"
565     "    <status><basic>closed</basic></status>\n"
566     "  </tuple>\n"
567     "</presence>\n";
568 
569 
accept_and_notify_twice(CONDITION_PARAMS)570 int accept_and_notify_twice(CONDITION_PARAMS)
571 {
572   msg_t *with = nua_current_request(nua);
573 
574   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
575     return 0;
576 
577   save_event_in_list(ctx, event, ep, call);
578 
579   switch (event) {
580   case nua_i_subscribe:
581     if (status < 200) {
582       NOTIFY(ep, call, nh,
583 	     SIPTAG_EVENT(sip->sip_event),
584 	     SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
585 	     SIPTAG_PAYLOAD_STR(presence_closed),
586 	     NUTAG_SUBSTATE(nua_substate_pending),
587 	     TAG_END());
588       NOTIFY(ep, call, nh,
589 	     SIPTAG_EVENT(sip->sip_event),
590 	     SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
591 	     SIPTAG_PAYLOAD_STR(presence_open),
592 	     NUTAG_SUBSTATE(nua_substate_active),
593 	     TAG_END());
594       RESPOND(ep, call, nh, SIP_202_ACCEPTED,
595 	      NUTAG_WITH(with),
596 	      SIPTAG_EXPIRES_STR("360"),
597 	      TAG_END());
598     }
599     return 0;
600 
601   case nua_r_notify:
602     return status >= 200 &&
603       tl_find(tags, nutag_substate)->t_value == nua_substate_active;
604 
605   default:
606     return 0;
607   }
608 }
609 
save_until_responded_and_notified_twice(CONDITION_PARAMS)610 int save_until_responded_and_notified_twice(CONDITION_PARAMS)
611 {
612   save_event_in_list(ctx, event, ep, call);
613 
614   if (event == nua_i_notify) {
615     if (ep->flags.bit0)
616       ep->flags.bit1 = 1;
617     ep->flags.bit0 = 1;
618   }
619 
620   if (event == nua_r_subscribe || event == nua_r_unsubscribe) {
621     if (status >= 300)
622       return 1;
623     else if (status >= 200) {
624       ep->flags.bit2 = 1;
625     }
626   }
627 
628   return ep->flags.bit0 && ep->flags.bit1 && ep->flags.bit2;
629 }
630 
631 
accept_and_notify(CONDITION_PARAMS)632 int accept_and_notify(CONDITION_PARAMS)
633 {
634   msg_t *with = nua_current_request(nua);
635 
636   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
637     return 0;
638 
639   save_event_in_list(ctx, event, ep, call);
640 
641   switch (event) {
642   case nua_i_subscribe:
643     if (status < 200) {
644       RESPOND(ep, call, nh, SIP_202_ACCEPTED,
645 	      NUTAG_WITH(with),
646 	      SIPTAG_EXPIRES_STR("360"),
647 	      SIPTAG_EXPIRES(sip->sip_expires),
648 	      TAG_END());
649 
650       NOTIFY(ep, call, nh,
651 	     SIPTAG_EVENT(sip->sip_event),
652 	     SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
653 	     SIPTAG_PAYLOAD_STR(presence_closed),
654 	     NUTAG_SUBSTATE(nua_substate_pending),
655 	     TAG_END());
656     }
657     return 0;
658 
659   case nua_r_notify:
660     return status >= 200;
661 
662   default:
663     return 0;
664   }
665 }
666 
save_and_notify(CONDITION_PARAMS)667 int save_and_notify(CONDITION_PARAMS)
668 {
669   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
670     return 0;
671 
672   save_event_in_list(ctx, event, ep, call);
673 
674   switch (event) {
675   case nua_i_subscribe:
676     NOTIFY(ep, call, nh,
677 	   SIPTAG_EVENT(sip->sip_event),
678 	   SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
679 	   SIPTAG_PAYLOAD_STR(presence_closed),
680 	   NUTAG_SUBSTATE(nua_substate_active),
681 	   TAG_END());
682     return 0;
683 
684   case nua_r_notify:
685     return status >= 200;
686 
687   default:
688     return 0;
689   }
690 }
691 
692 extern int save_until_notified_and_responded(CONDITION_PARAMS);
693 extern int save_until_notified(CONDITION_PARAMS);
694 
test_subscribe_notify(struct context * ctx)695 int test_subscribe_notify(struct context *ctx)
696 {
697   BEGIN();
698 
699   struct endpoint *a = &ctx->a,  *b = &ctx->b;
700   struct call *a_call = a->call, *b_call = b->call;
701   struct event *e, *en1, *en2, *es;
702   sip_t const *sip;
703   tagi_t const *n_tags, *r_tags;
704 
705   if (print_headings)
706     printf("TEST NUA-11.4: notifier server using nua_notify()\n");
707 
708   if (print_headings)
709     printf("TEST NUA-11.4.1: establishing subscription\n");
710 
711   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
712 
713   SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
714 	    SIPTAG_EVENT_STR("presence"),
715 	    SIPTAG_EXPIRES_STR("333"),
716 	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
717 	    TAG_END());
718 
719   run_ab_until(ctx, -1, save_until_responded_and_notified_twice,
720 	       -1, accept_and_notify_twice);
721 
722   /* Client events:
723      nua_subscribe(), nua_i_notify/nua_r_subscribe/nua_i_notify
724   */
725   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
726     if (en1 == NULL && e->data->e_event == nua_i_notify)
727       en1 = e;
728     else if (en2 == NULL && e->data->e_event == nua_i_notify)
729       en2 = e;
730     else if (e->data->e_event == nua_r_subscribe)
731       es = e;
732     else
733       TEST_1(!e);
734   }
735 
736   TEST_1(e = en1);
737   TEST_E(e->data->e_event, nua_i_notify);
738   TEST_1(tl_find(e->data->e_tags, nutag_substate));
739   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_pending);
740   TEST_1(sip = sip_object(e->data->e_msg));
741   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
742   TEST_1(sip->sip_content_type);
743   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
744   TEST_1(sip->sip_subscription_state);
745   TEST_S(sip->sip_subscription_state->ss_substate, "pending");
746   TEST_1(sip->sip_subscription_state->ss_expires);
747 
748   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
749   TEST_1(e->data->e_status == 202 || e->data->e_status == 200);
750   TEST_1(tl_find(e->data->e_tags, nutag_substate));
751   r_tags = tl_find(e->data->e_tags, nutag_substate);
752   if (es == a->events->head) {
753     TEST(r_tags->t_value, nua_substate_embryonic);
754   }
755   else if (es == a->events->head->next) {
756     TEST_1(r_tags->t_value == nua_substate_pending);
757   }
758   else {
759     TEST_1(r_tags->t_value == nua_substate_active);
760   }
761   TEST_1(sip = sip_object(e->data->e_msg));
762   TEST_1(sip->sip_expires);
763   TEST_1(sip->sip_expires->ex_delta <= 333);
764 
765   free_events_in_list(ctx, a->events);
766 
767   /* Server events: nua_i_subscribe, nua_r_notify */
768   TEST_1(e = b->events->head);
769   TEST_E(e->data->e_event, nua_i_subscribe);
770   TEST_E(e->data->e_status, 100);
771   TEST_1(sip = sip_object(e->data->e_msg));
772 
773   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
774   r_tags = e->data->e_tags;
775   TEST_1(tl_find(r_tags, nutag_substate));
776   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_pending);
777 
778   free_events_in_list(ctx, b->events);
779 
780   if (print_headings)
781     printf("TEST NUA-11.4.1: PASSED\n");
782 
783   /* ---------------------------------------------------------------------- */
784 
785 /* NOTIFY with updated content
786 
787    A			B
788    |                    |
789    |<------NOTIFY-------|
790    |-------200 OK------>|
791    |                    |
792 */
793   if (print_headings)
794     printf("TEST NUA-11.4.2: send NOTIFY\n");
795 
796   /* Update presence data */
797 
798   NOTIFY(b, b_call, b_call->nh,
799 	 NUTAG_SUBSTATE(nua_substate_active),
800 	 SIPTAG_EVENT_STR("presence"),
801 	 SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
802 	 SIPTAG_PAYLOAD_STR(presence_open),
803 	 TAG_END());
804 
805   run_ab_until(ctx, -1, save_until_notified,
806 	       -1, save_until_final_response);
807 
808   /* subscriber events:
809      nua_i_notify
810   */
811   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_notify);
812   TEST_1(sip = sip_object(e->data->e_msg));
813   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
814   TEST_1(sip->sip_content_type);
815   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
816   TEST_1(sip->sip_subscription_state);
817   TEST_S(sip->sip_subscription_state->ss_substate, "active");
818   TEST_1(sip->sip_subscription_state->ss_expires);
819   n_tags = e->data->e_tags;
820   TEST_1(tl_find(n_tags, nutag_substate));
821   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
822   TEST_1(!e->next);
823   free_events_in_list(ctx, a->events);
824 
825   /* Notifier events: nua_r_notify */
826   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_r_notify);
827   r_tags = e->data->e_tags;
828   TEST_1(tl_find(r_tags, nutag_substate));
829   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
830 
831   free_events_in_list(ctx, b->events);
832 
833   if (print_headings)
834     printf("TEST NUA-11.4.2: PASSED\n");
835 
836   /* ---------------------------------------------------------------------- */
837 
838 /* Re-SUBSCRIBE
839 
840    A			B
841    |                    |
842    |<------NOTIFY-------|
843    |-------200 OK------>|
844    |                    |
845 */
846   if (print_headings)
847     printf("TEST NUA-11.4.3: re-SUBSCRIBE\n");
848 
849   /* Set default expiration time */
850   nua_set_hparams(b_call->nh, NUTAG_SUB_EXPIRES(365), TAG_END());
851   run_b_until(ctx, nua_r_set_params, until_final_response);
852 
853   SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
854 	    SIPTAG_EVENT_STR("presence"),
855 	    SIPTAG_EXPIRES_STR("3600"),
856 	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
857 	    TAG_END());
858 
859   run_ab_until(ctx, -1, save_until_notified_and_responded,
860 	       -1, save_until_final_response);
861 
862   /* Client events:
863      nua_subscribe(), nua_i_notify/nua_r_subscribe
864   */
865   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
866     if (en1 == NULL && e->data->e_event == nua_i_notify)
867       en1 = e;
868     else if (e->data->e_event == nua_r_subscribe)
869       es = e;
870     else
871       TEST_1(!e);
872   }
873   TEST_1(e = en1);
874   TEST_E(e->data->e_event, nua_i_notify);
875   TEST_1(sip = sip_object(e->data->e_msg));
876   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
877   TEST_1(sip->sip_content_type);
878   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
879   TEST_1(sip->sip_subscription_state);
880   TEST_S(sip->sip_subscription_state->ss_substate, "active");
881   TEST_1(sip->sip_subscription_state->ss_expires);
882   n_tags = e->data->e_tags;
883   TEST_1(tl_find(n_tags, nutag_substate));
884   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
885 
886   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
887   TEST_1(tl_find(e->data->e_tags, nutag_substate));
888   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_active);
889   TEST_1(sip = sip_object(e->data->e_msg));
890   TEST_1(sip->sip_expires);
891   TEST_1(sip->sip_expires->ex_delta <= 365);
892 
893   free_events_in_list(ctx, a->events);
894 
895   /* Server events: nua_i_subscribe, nua_r_notify */
896   TEST_1(e = b->events->head);
897   TEST_E(e->data->e_event, nua_i_subscribe);
898   TEST_E(e->data->e_status, 100);
899   TEST_1(sip = sip_object(e->data->e_msg));
900   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
901   r_tags = e->data->e_tags;
902   TEST_1(tl_find(r_tags, nutag_substate));
903   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
904 
905   free_events_in_list(ctx, b->events);
906 
907   if (print_headings)
908     printf("TEST NUA-11.4.3: PASSED\n");
909 
910   /* ---------------------------------------------------------------------- */
911 
912 /* un-SUBSCRIBE
913 
914    A			B
915    |                    |
916    |------SUBSCRIBE---->|
917    |<--------202--------|
918    |<------NOTIFY-------|
919    |-------200 OK------>|
920    |                    |
921 */
922   if (print_headings)
923     printf("TEST NUA-11.4.4: un-SUBSCRIBE\n");
924 
925   UNSUBSCRIBE(a, a_call, a_call->nh, TAG_END());
926 
927   run_ab_until(ctx, -1, save_until_final_response,
928 	       -1, save_until_final_response);
929 
930   /* Client events:
931      nua_unsubscribe(), nua_i_notify/nua_r_unsubscribe
932   */
933   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
934     if (en1 == NULL && e->data->e_event == nua_i_notify)
935       en1 = e;
936     else if (e->data->e_event == nua_r_unsubscribe)
937       es = e;
938     else
939       TEST_1(!e);
940   }
941   if (en1) {
942     TEST_1(e = en1); TEST_E(e->data->e_event, nua_i_notify);
943     TEST_1(sip = sip_object(e->data->e_msg));
944     TEST_1(sip->sip_event);
945     TEST_1(sip->sip_subscription_state);
946     TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
947     TEST_1(!sip->sip_subscription_state->ss_expires);
948     n_tags = e->data->e_tags;
949     TEST_1(tl_find(n_tags, nutag_substate));
950     TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
951   }
952 
953   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_unsubscribe);
954   TEST(e->data->e_status, 200);
955   r_tags = e->data->e_tags;
956   TEST_1(tl_find(r_tags, nutag_substate));
957   if (en1 == a->events->head)
958     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
959   else
960     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
961   TEST_1(sip = sip_object(e->data->e_msg));
962   TEST_1(sip->sip_expires);
963   TEST_1(sip->sip_expires->ex_delta == 0);
964 
965   free_events_in_list(ctx, a->events);
966 
967   /* Notifier events: nua_r_notify */
968   TEST_1(e = b->events->head);
969   TEST_E(e->data->e_event, nua_i_subscribe);
970   TEST(e->data->e_status, 200);
971   TEST_1(sip = sip_object(e->data->e_msg));
972   TEST_1(tl_find(e->data->e_tags, nutag_substate));
973   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value,
974        nua_substate_terminated);
975   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
976   TEST_1(e->data->e_status >= 200);
977   r_tags = e->data->e_tags;
978   TEST_1(tl_find(r_tags, nutag_substate));
979   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
980 
981   free_events_in_list(ctx, b->events);
982 
983   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
984   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
985 
986   if (print_headings)
987     printf("TEST NUA-11.4.4: PASSED\n");
988 
989   if (print_headings)
990     printf("TEST NUA-11.4: PASSED\n");
991 
992   END();
993 }
994 
995 /* ---------------------------------------------------------------------- */
996 /* Subscriber gracefully terminates dialog upon 483 */
997 
998 static
999 size_t change_status_to_483(void *a, void *message, size_t len);
1000 int save_until_notified_and_responded_twice(CONDITION_PARAMS);
1001 int save_until_notify_responded_twice(CONDITION_PARAMS);
1002 int accept_subscription_until_terminated(CONDITION_PARAMS);
1003 
test_subscribe_notify_graceful(struct context * ctx)1004 int test_subscribe_notify_graceful(struct context *ctx)
1005 {
1006   if (!ctx->nat)
1007     return 0;
1008 
1009   BEGIN();
1010 
1011   struct endpoint *a = &ctx->a,  *b = &ctx->b;
1012   struct call *a_call = a->call, *b_call = b->call;
1013   struct event *e, *en1, *en2, *es;
1014   sip_t const *sip;
1015   tagi_t const *n_tags, *r_tags;
1016   struct nat_filter *f;
1017 
1018   if (print_headings)
1019     printf("TEST NUA-11.5.1: establishing subscription\n");
1020 
1021   nua_set_params(b->nua, NUTAG_APPL_METHOD("NOTIFY"),
1022 		 TAG_END());
1023   run_b_until(ctx, nua_r_set_params, until_final_response);
1024 
1025   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1026 
1027   SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1028 	    SIPTAG_EVENT_STR("presence"),
1029 	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
1030 	    TAG_END());
1031 
1032   run_ab_until(ctx, -1, save_until_notified_and_responded,
1033 	       -1, accept_and_notify);
1034 
1035   /* Client events:
1036      nua_subscribe(), nua_i_notify/nua_r_subscribe
1037   */
1038   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
1039     if (en1 == NULL && e->data->e_event == nua_i_notify)
1040       en1 = e;
1041     else if (es == NULL && e->data->e_event == nua_r_subscribe)
1042       es = e;
1043     else
1044       TEST_1(!e);
1045   }
1046   TEST_1(e = en1); TEST_E(e->data->e_event, nua_i_notify);
1047   TEST_1(sip = sip_object(e->data->e_msg));
1048   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
1049   TEST_1(sip->sip_content_type);
1050   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
1051   TEST_1(sip->sip_subscription_state);
1052   TEST_S(sip->sip_subscription_state->ss_substate, "pending");
1053   TEST_1(sip->sip_subscription_state->ss_expires);
1054   n_tags = e->data->e_tags;
1055   TEST_1(tl_find(n_tags, nutag_substate));
1056   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_pending);
1057 
1058   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
1059   r_tags = e->data->e_tags;
1060   TEST_1(tl_find(r_tags, nutag_substate));
1061   if (es == a->events->head)
1062     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_embryonic);
1063   else
1064     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_pending);
1065   free_events_in_list(ctx, a->events);
1066 
1067   /* Server events: nua_i_subscribe, nua_r_notify */
1068   TEST_1(e = b->events->head);
1069   TEST_E(e->data->e_event, nua_i_subscribe);
1070   TEST_E(e->data->e_status, 100);
1071   TEST_1(sip = sip_object(e->data->e_msg));
1072 
1073   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1074   r_tags = e->data->e_tags;
1075   TEST_1(tl_find(r_tags, nutag_substate));
1076   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_pending);
1077 
1078   free_events_in_list(ctx, b->events);
1079 
1080   if (print_headings)
1081     printf("TEST NUA-11.5.1: PASSED\n");
1082 
1083   if (print_headings)
1084     printf("TEST NUA-11.5.2: terminate gracefully upon 483\n");
1085 
1086   TEST_1(f = test_nat_add_filter(ctx->nat,
1087 				 change_status_to_483, NULL,
1088 				 nat_inbound));
1089 
1090   SUBSCRIBE(a, a_call, a_call->nh, TAG_END());
1091 
1092   run_ab_until(ctx, -1, save_until_notified_and_responded_twice,
1093 	       -1, accept_subscription_until_terminated);
1094 
1095 #if 0
1096   /* Client events:
1097      nua_unsubscribe(), nua_i_notify/nua_r_unsubscribe
1098   */
1099   TEST_1(e = a->events->head);
1100   if (e->data->e_event == nua_i_notify) {
1101     TEST_E(e->data->e_event, nua_i_notify);
1102     n_tags = e->data->e_tags;
1103     TEST_1(sip = sip_object(e->data->e_msg));
1104     TEST_1(sip->sip_event);
1105     TEST_1(sip->sip_subscription_state);
1106     TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1107     TEST_1(!sip->sip_subscription_state->ss_expires);
1108     TEST_1(tl_find(n_tags, nutag_substate));
1109     TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1110     TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_unsubscribe);
1111     TEST(e->data->e_status, 200);
1112     r_tags = e->data->e_tags;
1113     TEST_1(tl_find(r_tags, nutag_substate));
1114     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1115   }
1116   else {
1117     TEST_E(e->data->e_event, nua_r_unsubscribe);
1118     TEST(e->data->e_status, 200);
1119     r_tags = e->data->e_tags;
1120     TEST_1(tl_find(r_tags, nutag_substate));
1121     TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1122   }
1123   TEST_1(!e->next);
1124 
1125   /* Notifier events: nua_r_notify */
1126   TEST_1(e = b->events->head);
1127   TEST_E(e->data->e_event, nua_i_subscribe);
1128   TEST(e->data->e_status, 200);
1129   TEST_1(sip = sip_object(e->data->e_msg));
1130   TEST_1(tl_find(e->data->e_tags, nutag_substate));
1131   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value,
1132        nua_substate_terminated);
1133   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1134   TEST_1(e->data->e_status >= 200);
1135   r_tags = e->data->e_tags;
1136   TEST_1(tl_find(r_tags, nutag_substate));
1137   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1138 #endif
1139 
1140   free_events_in_list(ctx, a->events);
1141   free_events_in_list(ctx, b->events);
1142 
1143   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1144   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1145 
1146   test_nat_remove_filter(ctx->nat, f);
1147 
1148   if (print_headings)
1149     printf("TEST NUA-11.5.2: PASSED\n");
1150 
1151   END();
1152 }
1153 
1154 static
change_status_to_483(void * a,void * message,size_t len)1155 size_t change_status_to_483(void *a, void *message, size_t len)
1156 {
1157   (void)a;
1158 
1159   if (strncmp("SIP/2.0 2", message, 9) == 0) {
1160     memcpy(message, "SIP/2.0 483", 11);
1161   }
1162   return len;
1163 }
1164 
save_until_notified_and_responded_twice(CONDITION_PARAMS)1165 int save_until_notified_and_responded_twice(CONDITION_PARAMS)
1166 {
1167   save_event_in_list(ctx, event, ep, call);
1168 
1169   if (event == nua_i_notify) {
1170     if (ep->flags.bit0)
1171       ep->flags.bit1 = 1;
1172     ep->flags.bit0 = 1;
1173   }
1174 
1175   if (event == nua_r_subscribe || event == nua_r_unsubscribe) {
1176     if (status >= 300)
1177       return 1;
1178     else if (status >= 200) {
1179       if (ep->flags.bit2)
1180 	ep->flags.bit3 = 1;
1181       ep->flags.bit2 = 1;
1182     }
1183   }
1184 
1185   return ep->flags.bit0 && ep->flags.bit1 && ep->flags.bit2 && ep->flags.bit3;
1186 }
1187 
save_until_notify_responded_twice(CONDITION_PARAMS)1188 int save_until_notify_responded_twice(CONDITION_PARAMS)
1189 {
1190   save_event_in_list(ctx, event, ep, call);
1191 
1192   if (event == nua_r_notify) {
1193     if (ep->flags.bit0)
1194       ep->flags.bit1 = 1;
1195     ep->flags.bit0 = 1;
1196   }
1197 
1198   return ep->flags.bit0 && ep->flags.bit1;
1199 }
1200 
1201 /* ---------------------------------------------------------------------- */
1202 
1203 /*
1204  * When incoming SUBSCRIBE, send NOTIFY,
1205  * 200 OK SUBSCRIBE when NOTIFY has been responded.
1206  */
notify_and_accept(CONDITION_PARAMS)1207 int notify_and_accept(CONDITION_PARAMS)
1208 {
1209   if (!(check_handle(ep, call, nh, SIP_500_INTERNAL_SERVER_ERROR)))
1210     return 0;
1211 
1212   save_event_in_list(ctx, event, ep, call);
1213 
1214   switch (event) {
1215   case nua_i_subscribe:
1216     if (status < 200) {
1217       NOTIFY(ep, call, nh,
1218 	     SIPTAG_EVENT(sip->sip_event),
1219 	     SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
1220 	     SIPTAG_PAYLOAD_STR(presence_closed),
1221 	     TAG_END());
1222     }
1223     return 0;
1224 
1225   case nua_r_notify:
1226     if (status >= 200) {
1227       struct event *e;
1228       for (e = ep->events->head; e; e = e->next) {
1229 	if (e->data->e_event == nua_i_subscribe) {
1230 	  RESPOND(ep, call, nh, SIP_200_OK,
1231 		  NUTAG_WITH(e->data->e_msg),
1232 		  TAG_END());
1233 	  break;
1234 	}
1235       }
1236       return 1;
1237     }
1238 
1239   default:
1240     return 0;
1241   }
1242 }
1243 
test_event_fetch(struct context * ctx)1244 int test_event_fetch(struct context *ctx)
1245 {
1246   BEGIN();
1247 
1248   struct endpoint *a = &ctx->a,  *b = &ctx->b;
1249   struct call *a_call = a->call, *b_call = b->call;
1250   struct event *e, *en1, *en2, *es;
1251   sip_t const *sip;
1252   tagi_t const *r_tags;
1253 
1254   if (print_headings)
1255     printf("TEST NUA-11.6.1: event fetch using nua_notify()\n");
1256 
1257   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1258 
1259 /* Fetch 1:
1260 
1261    A			 B
1262    |                     |
1263    |------SUBSCRIBE----->|
1264    |     Expires: 0      |
1265    |<---------202--------|
1266    |                     |
1267    |<-------NOTIFY-------|
1268    | S-State: terminated |
1269    |-------200 OK------->|
1270    |                     |
1271 */
1272 
1273   SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1274 	    SIPTAG_EVENT_STR("presence"),
1275 	    SIPTAG_EXPIRES_STR("0"),
1276 	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
1277 	    TAG_END());
1278 
1279   run_ab_until(ctx, -1, save_until_notified_and_responded,
1280 	       -1, accept_and_notify);
1281 
1282   /* Client events:
1283      nua_subscribe(), nua_i_notify/nua_r_subscribe/nua_i_notify
1284   */
1285   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
1286     if (en1 == NULL && e->data->e_event == nua_i_notify)
1287       en1 = e;
1288     else if (en2 == NULL && e->data->e_event == nua_i_notify)
1289       en2 = e;
1290     else if (e->data->e_event == nua_r_subscribe)
1291       es = e;
1292     else
1293       TEST_1(!e);
1294   }
1295 
1296   TEST_1(e = en1);
1297   TEST_E(e->data->e_event, nua_i_notify);
1298   TEST_1(tl_find(e->data->e_tags, nutag_substate));
1299   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_terminated);
1300   TEST_1(sip = sip_object(e->data->e_msg));
1301   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
1302   TEST_1(sip->sip_content_type);
1303   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
1304   TEST_1(sip->sip_subscription_state);
1305   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1306 
1307   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
1308   TEST_1(e->data->e_status == 202 || e->data->e_status == 200);
1309   TEST_1(tl_find(e->data->e_tags, nutag_substate));
1310 
1311   if (es == a->events->head)
1312     TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_embryonic);
1313   else
1314     TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_terminated);
1315   TEST_1(sip = sip_object(e->data->e_msg));
1316   TEST_1(sip->sip_expires);
1317   TEST_1(sip->sip_expires->ex_delta == 0);
1318 
1319   free_events_in_list(ctx, a->events);
1320 
1321   /* Server events: nua_i_subscribe, nua_r_notify */
1322   TEST_1(e = b->events->head);
1323   TEST_E(e->data->e_event, nua_i_subscribe);
1324   TEST_E(e->data->e_status, 100);
1325   TEST_1(sip = sip_object(e->data->e_msg));
1326 
1327   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1328   r_tags = e->data->e_tags;
1329   TEST_1(tl_find(r_tags, nutag_substate));
1330   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1331 
1332   free_events_in_list(ctx, b->events);
1333 
1334   if (print_headings)
1335     printf("TEST NUA-11.6.1: PASSED\n");
1336 
1337   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1338   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1339 
1340 
1341   if (print_headings)
1342     printf("TEST NUA-11.6.2: event fetch, NOTIFY comes before 202\n");
1343 
1344   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1345 
1346 /* Fetch 2:
1347 
1348    A			 B
1349    |                     |
1350    |------SUBSCRIBE----->|
1351    |     Expires: 0      |
1352    |<-------NOTIFY-------|
1353    | S-State: terminated |
1354    |-------200 OK------->|
1355    |                     |
1356    |<---------202--------|
1357    |                     |
1358 */
1359 
1360   SUBSCRIBE(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1361 	    SIPTAG_EVENT_STR("presence"),
1362 	    SIPTAG_EXPIRES_STR("0"),
1363 	    SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
1364 	    TAG_END());
1365 
1366   run_ab_until(ctx, -1, save_until_notified_and_responded,
1367 	       -1, notify_and_accept);
1368 
1369   /* Client events:
1370      nua_subscribe(), nua_i_notify/nua_r_subscribe/nua_i_notify
1371   */
1372   for (en1 = en2 = es = NULL, e = a->events->head; e; e = e->next) {
1373     if (en1 == NULL && e->data->e_event == nua_i_notify)
1374       en1 = e;
1375     else if (en2 == NULL && e->data->e_event == nua_i_notify)
1376       en2 = e;
1377     else if (e->data->e_event == nua_r_subscribe)
1378       es = e;
1379     else
1380       TEST_1(!e);
1381   }
1382 
1383   TEST_1(e = en1);
1384   TEST_E(e->data->e_event, nua_i_notify);
1385   TEST_1(tl_find(e->data->e_tags, nutag_substate));
1386   TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_terminated);
1387   TEST_1(sip = sip_object(e->data->e_msg));
1388   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
1389   TEST_1(sip->sip_content_type);
1390   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
1391   TEST_1(sip->sip_subscription_state);
1392   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1393 
1394   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_subscribe);
1395   TEST_1(e->data->e_status == 202 || e->data->e_status == 200);
1396   TEST_1(tl_find(e->data->e_tags, nutag_substate));
1397 
1398   if (es == a->events->head)
1399     TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_embryonic);
1400   else
1401     TEST(tl_find(e->data->e_tags, nutag_substate)->t_value, nua_substate_terminated);
1402   TEST_1(sip = sip_object(e->data->e_msg));
1403   TEST_1(sip->sip_expires);
1404   TEST_1(sip->sip_expires->ex_delta == 0);
1405 
1406   free_events_in_list(ctx, a->events);
1407 
1408   /* Server events: nua_i_subscribe, nua_r_notify */
1409   TEST_1(e = b->events->head);
1410   TEST_E(e->data->e_event, nua_i_subscribe);
1411   TEST_E(e->data->e_status, 100);
1412   TEST_1(sip = sip_object(e->data->e_msg));
1413 
1414   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1415   r_tags = e->data->e_tags;
1416   TEST_1(tl_find(r_tags, nutag_substate));
1417   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1418 
1419   free_events_in_list(ctx, b->events);
1420 
1421   if (print_headings)
1422     printf("TEST NUA-11.6.2: PASSED\n");
1423 
1424   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1425   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1426 
1427   END();
1428 }
1429 
1430 /* ---------------------------------------------------------------------- */
1431 /* Unsolicited NOTIFY */
1432 
1433 int accept_notify(CONDITION_PARAMS);
1434 
test_newsub_notify(struct context * ctx)1435 int test_newsub_notify(struct context *ctx)
1436 {
1437   BEGIN();
1438 
1439   struct endpoint *a = &ctx->a,  *b = &ctx->b;
1440   struct call *a_call = a->call, *b_call = b->call;
1441   struct event *e;
1442   sip_t const *sip;
1443   sip_call_id_t *i;
1444   tagi_t const *n_tags, *r_tags;
1445 
1446 #if 0
1447 
1448   if (print_headings)
1449     printf("TEST NUA-11.7.1: rejecting NOTIFY without subscription locally\n");
1450 
1451   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1452 
1453   NOTIFY(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1454 	 SIPTAG_SUBJECT_STR("NUA-11.7.1"),
1455 	 SIPTAG_EVENT_STR("message-summary"),
1456 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1457 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1458 	 TAG_END());
1459 
1460   run_a_until(ctx, -1, save_until_final_response);
1461 
1462   /* Client events:
1463      nua_notify(), nua_r_notify
1464   */
1465   TEST_1(e = a->events->head);
1466   TEST_E(e->data->e_event, nua_r_notify);
1467   TEST(e->data->e_status, 481);
1468   TEST_1(!e->data->e_msg);
1469   TEST_1(!e->next);
1470   free_events_in_list(ctx, a->events);
1471 
1472   if (print_headings)
1473     printf("TEST NUA-11.7.1: PASSED\n");
1474 
1475   if (print_headings)
1476     printf("TEST NUA-11.7.2: rejecting NOTIFY without subscription\n");
1477 
1478   TEST_1(i = sip_call_id_create(nua_handle_home(a_call->nh), NULL));
1479 
1480   NOTIFY(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1481 	 NUTAG_NEWSUB(1),
1482 	 SIPTAG_SUBJECT_STR("NUA-11.7.2 first"),
1483 	 SIPTAG_FROM_STR("<sip:alice@example.com>;tag=nua-11.7.2"),
1484 	 SIPTAG_CALL_ID(i),
1485 	 SIPTAG_CSEQ_STR("1 NOTIFY"),
1486 	 SIPTAG_EVENT_STR("message-summary"),
1487 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1488 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1489 	 TAG_END());
1490 
1491   run_a_until(ctx, -1, save_until_final_response);
1492 
1493   /* Client events:
1494      nua_notify(), nua_r_notify
1495   */
1496   TEST_1(e = a->events->head);
1497   TEST_E(e->data->e_event, nua_r_notify);
1498   TEST(e->data->e_status, 481);
1499   TEST_1(e->data->e_msg);
1500   TEST_1(!e->next);
1501 
1502   free_events_in_list(ctx, a->events);
1503 
1504   /* 2nd NOTIFY using same dialog */
1505   /* Check that server really discards the dialog */
1506 
1507   NOTIFY(a, a_call, a_call->nh, NUTAG_URL(b->contact->m_url),
1508 	 NUTAG_NEWSUB(1),
1509 	 SIPTAG_SUBJECT_STR("NUA-11.7.2 second"),
1510 	 SIPTAG_FROM_STR("<sip:alice@example.com>;tag=nua-11.7.2"),
1511 	 SIPTAG_CALL_ID(i),
1512 	 SIPTAG_CSEQ_STR("2 NOTIFY"),
1513 	 SIPTAG_EVENT_STR("message-summary"),
1514 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1515 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1516 	 TAG_END());
1517 
1518   run_a_until(ctx, -1, save_until_final_response);
1519 
1520   /* Client events:
1521      nua_notify(), nua_r_notify
1522   */
1523   TEST_1(e = a->events->head);
1524   TEST_E(e->data->e_event, nua_r_notify);
1525   TEST(e->data->e_status, 481);
1526   TEST_1(e->data->e_msg);
1527   TEST_1(!e->next);
1528 
1529   free_events_in_list(ctx, a->events);
1530 
1531   if (print_headings)
1532     printf("TEST NUA-11.7.2: PASSED\n");
1533 
1534   /* ---------------------------------------------------------------------- */
1535 
1536   if (print_headings)
1537     printf("TEST NUA-11.7.3: accept NOTIFY\n");
1538 
1539   nua_set_params(b->nua, NUTAG_APPL_METHOD("NOTIFY"), TAG_END());
1540   run_b_until(ctx, nua_r_set_params, until_final_response);
1541 
1542   NOTIFY(a, a_call, a_call->nh,
1543 	 NUTAG_URL(b->contact->m_url),
1544 	 NUTAG_NEWSUB(1),
1545 	 SIPTAG_SUBJECT_STR("NUA-11.7.3"),
1546 	 SIPTAG_EVENT_STR("message-summary"),
1547 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1548 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1549 	 TAG_END());
1550 
1551   run_ab_until(ctx, -1, save_until_final_response, -1, accept_notify);
1552 
1553   /* Notifier events: nua_r_notify */
1554   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_notify);
1555   TEST(e->data->e_status, 200);
1556   r_tags = e->data->e_tags;
1557   TEST_1(tl_find(r_tags, nutag_substate));
1558   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1559 
1560   /* subscriber events:
1561      nua_i_notify
1562   */
1563   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_notify);
1564   TEST_1(sip = sip_object(e->data->e_msg));
1565   TEST_1(sip->sip_subscription_state);
1566   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1567   n_tags = e->data->e_tags;
1568   TEST_1(tl_find(n_tags, nutag_substate));
1569   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1570   TEST_1(!e->next);
1571 
1572   free_events_in_list(ctx, a->events);
1573   free_events_in_list(ctx, b->events);
1574 
1575   if (print_headings)
1576     printf("TEST NUA-11.7.3: PASSED\n");
1577 
1578   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1579   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1580 
1581 #else
1582   (void)i;
1583   nua_set_params(b->nua, NUTAG_APPL_METHOD("NOTIFY"), TAG_END());
1584   run_b_until(ctx, nua_r_set_params, until_final_response);
1585 #endif
1586 
1587   /* ---------------------------------------------------------------------- */
1588 
1589   if (print_headings)
1590     printf("TEST NUA-11.7.4: multiple unsolicited NOTIFYs\n");
1591 
1592   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1593 
1594   NOTIFY(a, a_call, a_call->nh,
1595 	 NUTAG_URL(b->contact->m_url),
1596 	 NUTAG_NEWSUB(1),
1597 	 SIPTAG_EXPIRES_STR("10"),
1598 	 SIPTAG_SUBJECT_STR("NUA-11.7.4aa"),
1599 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1600 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: no"),
1601 	 TAG_END());
1602 
1603   NOTIFY(a, a_call, a_call->nh,
1604 	 NUTAG_URL(b->contact->m_url),
1605 	 NUTAG_NEWSUB(1),
1606 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=333"),
1607 	 SIPTAG_SUBJECT_STR("NUA-11.7.4a"),
1608 	 SIPTAG_EVENT_STR("message-summary"),
1609 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1610 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: no"),
1611 	 TAG_END());
1612 
1613   NOTIFY(a, a_call, a_call->nh,
1614 	 NUTAG_URL(b->contact->m_url),
1615 	 NUTAG_NEWSUB(1),
1616 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=3000"),
1617 	 SIPTAG_SUBJECT_STR("NUA-11.7.4b"),
1618 	 SIPTAG_EVENT_STR("message-summary"),
1619 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1620 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1621 	 TAG_END());
1622 
1623   NOTIFY(a, a_call, a_call->nh,
1624 	 NUTAG_URL(b->contact->m_url),
1625 	 NUTAG_NEWSUB(1),
1626 	 SIPTAG_SUBSCRIPTION_STATE_STR("terminated"),
1627 	 SIPTAG_SUBJECT_STR("NUA-11.7.4c"),
1628 	 SIPTAG_EVENT_STR("message-summary"),
1629 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1630 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1631 	 TAG_END());
1632 
1633   a->state.n = 4;
1634   b->state.n = 4;
1635 
1636   run_ab_until(ctx, -1, save_until_nth_final_response,
1637 	       -1, accept_n_notifys);
1638 
1639   /* Notifier events: nua_r_notify nua_r_notify */
1640   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_notify);
1641   TEST(e->data->e_status, 200);
1642   r_tags = e->data->e_tags;
1643   TEST_1(tl_find(r_tags, nutag_substate));
1644   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1645 
1646   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1647   TEST(e->data->e_status, 200);
1648   r_tags = e->data->e_tags;
1649   TEST_1(tl_find(r_tags, nutag_substate));
1650   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
1651   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1652   TEST(e->data->e_status, 200);
1653   r_tags = e->data->e_tags;
1654   TEST_1(tl_find(r_tags, nutag_substate));
1655   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
1656   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1657   TEST(e->data->e_status, 200);
1658   r_tags = e->data->e_tags;
1659   TEST_1(tl_find(r_tags, nutag_substate));
1660   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1661   TEST_1(!e->next);
1662 
1663   /* subscriber events:
1664      nua_i_notify
1665   */
1666   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_notify);
1667   TEST_1(sip = sip_object(e->data->e_msg));
1668   TEST_1(sip->sip_subscription_state);
1669   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1670   n_tags = e->data->e_tags;
1671   TEST_1(tl_find(n_tags, nutag_substate));
1672   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1673 
1674   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1675   TEST_1(sip = sip_object(e->data->e_msg));
1676   TEST_1(sip->sip_subscription_state);
1677   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1678   n_tags = e->data->e_tags;
1679   TEST_1(tl_find(n_tags, nutag_substate));
1680   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1681   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1682   TEST_1(sip = sip_object(e->data->e_msg));
1683   TEST_1(sip->sip_subscription_state);
1684   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1685   n_tags = e->data->e_tags;
1686   TEST_1(tl_find(n_tags, nutag_substate));
1687   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1688   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1689   TEST_1(sip = sip_object(e->data->e_msg));
1690   TEST_1(sip->sip_subscription_state);
1691   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1692   n_tags = e->data->e_tags;
1693   TEST_1(tl_find(n_tags, nutag_substate));
1694   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1695   TEST_1(!e->next);
1696 
1697   free_events_in_list(ctx, a->events);
1698   free_events_in_list(ctx, b->events);
1699 
1700   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1701   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1702 
1703   if (print_headings)
1704     printf("TEST NUA-11.7.4: PASSED\n");
1705 
1706   /* ---------------------------------------------------------------------- */
1707 
1708   if (print_headings)
1709     printf("TEST NUA-11.7.5: multiple unsolicited NOTIFYs\n");
1710 
1711   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1712 
1713   NOTIFY(a, a_call, a_call->nh,
1714 	 NUTAG_URL(b->contact->m_url),
1715 	 NUTAG_NEWSUB(1),
1716 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=333"),
1717 	 SIPTAG_SUBJECT_STR("NUA-11.7.5a"),
1718 	 SIPTAG_EVENT_STR("message-summary"),
1719 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1720 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: no"),
1721 	 TAG_END());
1722 
1723   NOTIFY(a, a_call, a_call->nh,
1724 	 NUTAG_URL(b->contact->m_url),
1725 	 NUTAG_NEWSUB(1),
1726 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=3000"),
1727 	 SIPTAG_SUBJECT_STR("NUA-11.7.5b"),
1728 	 SIPTAG_EVENT_STR("message-summary"),
1729 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1730 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1731 	 TAG_END());
1732 
1733   NOTIFY(a, a_call, a_call->nh,
1734 	 NUTAG_URL(b->contact->m_url),
1735 	 NUTAG_NEWSUB(1),
1736 	 SIPTAG_SUBSCRIPTION_STATE_STR("terminated"),
1737 	 SIPTAG_SUBJECT_STR("NUA-11.7.5c"),
1738 	 SIPTAG_EVENT_STR("message-summary"),
1739 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1740 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1741 	 TAG_END());
1742 
1743   a->state.n = 3;
1744   b->state.n = 3;
1745 
1746   run_ab_until(ctx, -1, save_until_nth_final_response,
1747 	       -1, accept_n_notifys);
1748 
1749   /* Notifier events: nua_r_notify nua_r_notify */
1750   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_notify);
1751   TEST(e->data->e_status, 200);
1752   r_tags = e->data->e_tags;
1753   TEST_1(tl_find(r_tags, nutag_substate));
1754   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
1755   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1756   TEST(e->data->e_status, 200);
1757   r_tags = e->data->e_tags;
1758   TEST_1(tl_find(r_tags, nutag_substate));
1759   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_active);
1760   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
1761   TEST(e->data->e_status, 200);
1762   r_tags = e->data->e_tags;
1763   TEST_1(tl_find(r_tags, nutag_substate));
1764   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
1765   TEST_1(!e->next);
1766 
1767   /* subscriber events:
1768      nua_i_notify
1769   */
1770   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_notify);
1771   TEST_1(sip = sip_object(e->data->e_msg));
1772   TEST_1(sip->sip_subscription_state);
1773   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1774   n_tags = e->data->e_tags;
1775   TEST_1(tl_find(n_tags, nutag_substate));
1776   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1777   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1778   TEST_1(sip = sip_object(e->data->e_msg));
1779   TEST_1(sip->sip_subscription_state);
1780   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1781   n_tags = e->data->e_tags;
1782   TEST_1(tl_find(n_tags, nutag_substate));
1783   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1784   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1785   TEST_1(sip = sip_object(e->data->e_msg));
1786   TEST_1(sip->sip_subscription_state);
1787   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1788   n_tags = e->data->e_tags;
1789   TEST_1(tl_find(n_tags, nutag_substate));
1790   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1791   TEST_1(!e->next);
1792 
1793   free_events_in_list(ctx, a->events);
1794   free_events_in_list(ctx, b->events);
1795 
1796   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1797   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1798 
1799   if (print_headings)
1800     printf("TEST NUA-11.7.5: PASSED\n");
1801 
1802   /* ---------------------------------------------------------------------- */
1803 
1804 #if 0
1805 
1806   if (print_headings)
1807     printf("TEST NUA-11.7.6: unsolicited NOTIFY handle destroyed\n");
1808 
1809   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1810 
1811   NOTIFY(a, a_call, a_call->nh,
1812 	 NUTAG_URL(b->contact->m_url),
1813 	 NUTAG_NEWSUB(1),
1814 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=333"),
1815 	 SIPTAG_SUBJECT_STR("NUA-11.7.6a"),
1816 	 SIPTAG_EVENT_STR("message-summary"),
1817 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1818 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: no"),
1819 	 TAG_END());
1820 
1821   NOTIFY(a, a_call, a_call->nh,
1822 	 NUTAG_URL(b->contact->m_url),
1823 	 NUTAG_NEWSUB(1),
1824 	 SIPTAG_SUBSCRIPTION_STATE_STR("active; expires=3000"),
1825 	 SIPTAG_SUBJECT_STR("NUA-11.7.6b"),
1826 	 SIPTAG_EVENT_STR("message-summary"),
1827 	 SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"),
1828 	 SIPTAG_PAYLOAD_STR("Messages-Waiting: yes"),
1829 	 TAG_END());
1830 
1831   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
1832 
1833   a->state.n = 3;
1834   b->state.n = 3;
1835 
1836   run_b_until(ctx, -1, accept_n_notifys);
1837 
1838   /* subscriber events:
1839      nua_i_notify
1840   */
1841   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_notify);
1842   TEST_1(sip = sip_object(e->data->e_msg));
1843   TEST_1(sip->sip_subscription_state);
1844   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1845   n_tags = e->data->e_tags;
1846   TEST_1(tl_find(n_tags, nutag_substate));
1847   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1848   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1849   TEST_1(sip = sip_object(e->data->e_msg));
1850   TEST_1(sip->sip_subscription_state);
1851   TEST_S(sip->sip_subscription_state->ss_substate, "active");
1852   n_tags = e->data->e_tags;
1853   TEST_1(tl_find(n_tags, nutag_substate));
1854   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_active);
1855   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_i_notify);
1856   TEST_1(sip = sip_object(e->data->e_msg));
1857   TEST_1(sip->sip_subscription_state);
1858   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
1859   n_tags = e->data->e_tags;
1860   TEST_1(tl_find(n_tags, nutag_substate));
1861   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
1862   TEST_1(!e->next);
1863 
1864   free_events_in_list(ctx, b->events);
1865 
1866   if (print_headings)
1867     printf("TEST NUA-11.7.6: PASSED\n");
1868 
1869   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
1870 
1871 #endif
1872 
1873   if (print_headings)
1874     printf("TEST NUA-11.7: PASSED\n");
1875 
1876   END();
1877 }
1878 
1879 /**Terminate when received notify.
1880  * Respond to NOTIFY with 200 OK if it has not been responded.
1881  * Save events (except nua_i_active or terminated).
1882  */
accept_notify(CONDITION_PARAMS)1883 int accept_notify(CONDITION_PARAMS)
1884 {
1885   if (event == nua_i_notify && status < 200)
1886     RESPOND(ep, call, nh, SIP_200_OK,
1887 	    NUTAG_WITH_THIS(ep->nua),
1888 	    TAG_END());
1889 
1890   save_event_in_list(ctx, event, ep, call);
1891 
1892   return event == nua_i_notify;
1893 }
1894 
save_until_nth_final_response(CONDITION_PARAMS)1895 int save_until_nth_final_response(CONDITION_PARAMS)
1896 {
1897   save_event_in_list(ctx, event, ep, call);
1898 
1899   if (nua_r_set_params <= event && event < nua_i_network_changed
1900       && status >= 200) {
1901     if (ep->state.n > 0)
1902       ep->state.n--;
1903     return ep->state.n == 0;
1904   }
1905 
1906   return 0;
1907 }
1908 
accept_n_notifys(CONDITION_PARAMS)1909 int accept_n_notifys(CONDITION_PARAMS)
1910 {
1911   tagi_t const *substate = tl_find(tags, nutag_substate);
1912 
1913   if (event == nua_i_notify && status < 200)
1914     RESPOND(ep, call, nh, SIP_200_OK,
1915 	    NUTAG_WITH_THIS(ep->nua),
1916 	    TAG_END());
1917 
1918   save_event_in_list(ctx, event, ep, call);
1919 
1920   if (event != nua_i_notify)
1921     return 0;
1922 
1923   if (ep->state.n > 0)
1924     ep->state.n--;
1925 
1926   if (ep->state.n == 0)
1927     return 1;
1928 
1929   if (substate && substate->t_value == nua_substate_terminated) {
1930     if (call && call->nh == nh) {
1931       call->nh = NULL;
1932       nua_handle_destroy(nh);
1933     }
1934   }
1935 
1936   return 0;
1937 }
1938 
1939 /* ======================================================================== */
1940 
1941 int save_until_subscription_terminated(CONDITION_PARAMS);
1942 int accept_subscription_until_terminated(CONDITION_PARAMS);
1943 
1944 /* Timeout subscription */
test_subscription_timeout(struct context * ctx)1945 int test_subscription_timeout(struct context *ctx)
1946 {
1947   BEGIN();
1948 
1949   struct endpoint *a = &ctx->a,  *b = &ctx->b;
1950   struct call *a_call = a->call, *b_call = b->call;
1951   struct event *e, *en, *es;
1952   sip_t const *sip;
1953   tagi_t const *n_tags, *r_tags;
1954 
1955   if (print_headings)
1956     printf("TEST NUA-11.8: subscribe and wait until subscription times out\n");
1957 
1958   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
1959 
1960   METHOD(a, a_call, a_call->nh,
1961 	 NUTAG_METHOD("SUBSCRIBE"),
1962 	 NUTAG_URL(b->contact->m_url),
1963 	 SIPTAG_EVENT_STR("presence"),
1964 	 SIPTAG_ACCEPT_STR("application/xpidf, application/pidf+xml"),
1965 	 SIPTAG_EXPIRES_STR("2"),
1966 	 NUTAG_APPL_METHOD("NOTIFY"),
1967 	 NUTAG_DIALOG(2),
1968 	 TAG_END());
1969 
1970   run_ab_until(ctx,
1971 	       -1, save_until_subscription_terminated,
1972 	       -1, accept_subscription_until_terminated);
1973 
1974   /* Client events:
1975      nua_method(), nua_i_notify/nua_r_method, nua_i_notify
1976   */
1977   TEST_1(en = event_by_type(a->events->head, nua_i_notify));
1978   TEST_1(es = event_by_type(a->events->head, nua_r_method));
1979 
1980   TEST_1(e = en); TEST_E(e->data->e_event, nua_i_notify);
1981   TEST_1(sip = sip_object(e->data->e_msg));
1982   n_tags = e->data->e_tags;
1983 
1984   TEST_1(e = es); TEST_E(e->data->e_event, nua_r_method);
1985   r_tags = e->data->e_tags;
1986 
1987   TEST_1(sip->sip_event); TEST_S(sip->sip_event->o_type, "presence");
1988   TEST_1(sip->sip_content_type);
1989   TEST_S(sip->sip_content_type->c_type, "application/pidf+xml");
1990   TEST_1(sip->sip_subscription_state);
1991   TEST_S(sip->sip_subscription_state->ss_substate, "pending");
1992   TEST_1(sip->sip_subscription_state->ss_expires);
1993   TEST_1(tl_find(n_tags, nutag_substate));
1994   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_pending);
1995 
1996   if (es->next == en)
1997     e = en->next;
1998   else
1999     e = es->next;
2000 
2001   TEST_1(e); TEST_E(e->data->e_event, nua_i_notify);
2002   n_tags = e->data->e_tags;
2003   TEST_1(tl_find(n_tags, nutag_substate));
2004   TEST(tl_find(n_tags, nutag_substate)->t_value, nua_substate_terminated);
2005   TEST_1(sip = sip_object(e->data->e_msg));
2006   TEST_1(sip->sip_event);
2007   TEST_1(sip->sip_subscription_state);
2008   TEST_S(sip->sip_subscription_state->ss_substate, "terminated");
2009   TEST_1(!sip->sip_subscription_state->ss_expires);
2010   TEST_1(!e->next);
2011 
2012   free_events_in_list(ctx, a->events);
2013 
2014   /* Server events: nua_i_subscribe, nua_r_notify */
2015   TEST_1(e = b->events->head);
2016   TEST_E(e->data->e_event, nua_i_subscribe);
2017   TEST_E(e->data->e_status, 100);
2018   TEST_1(sip = sip_object(e->data->e_msg));
2019 
2020   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
2021   r_tags = e->data->e_tags;
2022   TEST_1(tl_find(r_tags, nutag_substate));
2023   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_pending);
2024 
2025   /* Notifier events: 2nd nua_r_notify */
2026   TEST_1(e = e->next); TEST_E(e->data->e_event, nua_r_notify);
2027   TEST_1(e->data->e_status >= 200);
2028   r_tags = e->data->e_tags;
2029   TEST_1(tl_find(r_tags, nutag_substate));
2030   TEST(tl_find(r_tags, nutag_substate)->t_value, nua_substate_terminated);
2031 
2032   free_events_in_list(ctx, b->events);
2033 
2034   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
2035   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
2036 
2037   if (print_headings)
2038     printf("TEST NUA-11.8: PASSED\n");
2039 
2040   END();
2041 }
2042 
save_until_subscription_terminated(CONDITION_PARAMS)2043 int save_until_subscription_terminated(CONDITION_PARAMS)
2044 {
2045   void *with = nua_current_request(nua);
2046 
2047   save_event_in_list(ctx, event, ep, call);
2048 
2049   if (event == nua_i_notify) {
2050     if (status < 200)
2051       RESPOND(ep, call, nh, SIP_200_OK, NUTAG_WITH(with), TAG_END());
2052 
2053     tags = tl_find(tags, nutag_substate);
2054     return tags && tags->t_value == nua_substate_terminated;
2055   }
2056 
2057   return 0;
2058 }
2059 
accept_subscription_until_terminated(CONDITION_PARAMS)2060 int accept_subscription_until_terminated(CONDITION_PARAMS)
2061 {
2062   void *with = nua_current_request(nua);
2063 
2064   save_event_in_list(ctx, event, ep, call);
2065 
2066   if (event == nua_i_subscribe && status < 200) {
2067     RESPOND(ep, call, nh, SIP_202_ACCEPTED,
2068 	    NUTAG_WITH(with),
2069 	    SIPTAG_EXPIRES_STR("360"),
2070 	    TAG_END());
2071     NOTIFY(ep, call, nh,
2072 	   SIPTAG_EVENT(sip->sip_event),
2073 	   SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"),
2074 	   SIPTAG_PAYLOAD_STR(presence_closed),
2075 	   NUTAG_SUBSTATE(nua_substate_pending),
2076 	   TAG_END());
2077   }
2078   else if (event == nua_r_notify) {
2079     tags = tl_find(tags, nutag_substate);
2080     return tags && tags->t_value == nua_substate_terminated;
2081   }
2082 
2083   return 0;
2084 }
2085 
2086 
2087 /* ======================================================================== */
2088 /* Test simple methods: MESSAGE, PUBLISH, SUBSCRIBE/NOTIFY */
2089 
test_simple(struct context * ctx)2090 int test_simple(struct context *ctx)
2091 {
2092   return 0
2093     || test_message(ctx)
2094     || test_publish(ctx)
2095     || test_subscribe_notify(ctx)
2096     || test_event_fetch(ctx)
2097     || test_newsub_notify(ctx)
2098     || test_subscribe_notify_graceful(ctx)
2099     || test_subscription_timeout(ctx)
2100     ;
2101 }
2102