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_register.c
26  * @brief Test registering, outbound, nat traversal.
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_register"
44 #endif
45 
46 /* ======================================================================== */
47 /* Test REGISTER */
48 
49 int test_clear_registrations(struct context *ctx);
50 int test_outbound_cases(struct context *ctx);
51 int test_register_a(struct context *ctx);
52 int test_register_b(struct context *ctx);
53 int test_register_c(struct context *ctx);
54 int test_register_refresh(struct context *ctx);
55 
test_register_to_proxy(struct context * ctx)56 int test_register_to_proxy(struct context *ctx)
57 {
58   return
59     test_clear_registrations(ctx) ||
60     test_outbound_cases(ctx) ||
61     test_register_a(ctx) ||
62     test_register_b(ctx) ||
63     test_register_c(ctx) ||
64     test_register_refresh(ctx);
65 }
66 
test_clear_registrations(struct context * ctx)67 int test_clear_registrations(struct context *ctx)
68 {
69   BEGIN();
70 
71   struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
72   struct call *a_reg = a->reg, *b_reg = b->reg, *c_reg = c->reg;
73 
74   if (print_headings)
75     printf("TEST NUA-2.3.0.1: un-REGISTER a\n");
76 
77   TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
78   UNREGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
79 	     SIPTAG_CONTACT_STR("*"),
80 	     TAG_END());
81   run_a_until(ctx, -1, until_final_response);
82   AUTHENTICATE(a, a_reg, a_reg->nh,
83 	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
84   run_a_until(ctx, -1, until_final_response);
85   nua_handle_destroy(a_reg->nh);
86 
87   if (print_headings)
88     printf("TEST NUA-2.3.0.1: PASSED\n");
89 
90   if (print_headings)
91     printf("TEST NUA-2.3.0.2: un-REGISTER b\n");
92 
93   TEST_1(b_reg->nh = nua_handle(b->nua, b_reg, TAG_END()));
94   UNREGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to),
95 	     SIPTAG_CONTACT_STR("*"),
96 	     TAG_END());
97   run_b_until(ctx, -1, until_final_response);
98   AUTHENTICATE(b, b_reg, b_reg->nh,
99 	       NUTAG_AUTH("Digest:\"test-proxy\":bob:secret"), TAG_END());
100   run_b_until(ctx, -1, until_final_response);
101   nua_handle_destroy(b_reg->nh);
102 
103   if (print_headings)
104     printf("TEST NUA-2.3.0.2: PASSED\n");
105 
106   if (print_headings)
107     printf("TEST NUA-2.3.0.3: un-REGISTER c\n");
108 
109   TEST_1(c_reg->nh = nua_handle(c->nua, c_reg, TAG_END()));
110   UNREGISTER(c, c_reg, c_reg->nh,
111 	     SIPTAG_FROM(c->to), SIPTAG_TO(c->to),
112 	     SIPTAG_CONTACT_STR("*"),
113 	     TAG_END());
114   run_c_until(ctx, -1, until_final_response);
115   AUTHENTICATE(c, c_reg, c_reg->nh,
116 	       NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
117   run_c_until(ctx, -1, until_final_response);
118   nua_handle_destroy(c_reg->nh);
119 
120   if (print_headings)
121     printf("TEST NUA-2.3.0.3: PASSED\n");
122 
123   END();
124 }
125 
test_outbound_cases(struct context * ctx)126 int test_outbound_cases(struct context *ctx)
127 {
128   BEGIN();
129 
130 #if 0
131 
132   struct endpoint *a = &ctx->a, *x;
133   struct call *a_reg = a->reg;
134   struct event *e;
135   sip_t const *sip;
136   sip_contact_t m[1];
137 
138 /* REGISTER test
139 
140    A			R
141    |------REGISTER----->|
142    |<-------401---------|
143    |------REGISTER----->|
144    |<-------200---------|
145    |			|
146 
147 */
148 
149   if (print_headings)
150     printf("TEST NUA-2.3.1: REGISTER a\n");
151 
152   test_proxy_domain_set_expiration(ctx->a.domain, 5, 5, 10);
153 
154   TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
155 
156   sip_contact_init(m);
157   m->m_display = "Lissu";
158   *m->m_url = *a->contact->m_url;
159   m->m_url->url_user = "a";
160   m->m_url->url_params = "transport=udp";
161 
162   REGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
163 	   NUTAG_OUTBOUND("use-rport no-options-keepalive"),
164 	   SIPTAG_CONTACT(m),
165 	   TAG_END());
166   run_a_until(ctx, -1, save_until_final_response);
167 
168   TEST_1(e = a->events->head);
169   TEST_E(e->data->e_event, nua_r_register);
170   TEST(e->data->e_status, 401);
171   TEST_1(sip = sip_object(e->data->e_msg));
172   TEST(sip->sip_status->st_status, 401);
173   TEST_1(!sip->sip_contact);
174   TEST_1(!e->next);
175   free_events_in_list(ctx, a->events);
176 
177   AUTHENTICATE(a, a_reg, a_reg->nh,
178 	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
179   run_a_until(ctx, -1, save_until_final_response);
180 
181   TEST_1(e = a->events->head);
182   TEST_E(e->data->e_event, nua_r_register);
183   TEST(e->data->e_status, 200);
184   TEST_1(sip = sip_object(e->data->e_msg));
185   TEST_1(sip->sip_contact);
186   TEST_S(sip->sip_contact->m_display, "Lissu");
187   TEST_S(sip->sip_contact->m_url->url_user, "a");
188   TEST_1(strstr(sip->sip_contact->m_url->url_params, "transport=udp"));
189 
190   if (ctx->nat) {
191     TEST_1(e = a->specials->head);
192   }
193 
194   test_proxy_domain_set_expiration(ctx->a.domain, 600, 3600, 36000);
195 
196   if (print_headings)
197     printf("TEST NUA-2.3.1: PASSED\n");
198 
199   if (print_headings)
200     printf("TEST NUA-2.3.4: refresh REGISTER\n");
201 
202   if (!ctx->p) {
203     free_events_in_list(ctx, a->events);
204     return 0;
205   }
206 
207   /* Wait for A to refresh its registrations */
208 
209   /*
210    * Avoid race condition: if X has already refreshed registration
211    * with expiration time of 3600 seconds, do not wait for new refresh
212    */
213   a->next_condition = save_until_final_response;
214 
215   for (x = a; x; x = NULL) {
216     for (e = x->events->head; e; e = e->next) {
217       if (e->data->e_event == nua_r_register &&
218 	  e->data->e_status == 200 &&
219 	  (sip = sip_object(e->data->e_msg)) &&
220 	  sip->sip_contact &&
221 	  sip->sip_contact->m_expires &&
222 	  strcmp(sip->sip_contact->m_expires, "3600") == 0) {
223 	x->next_condition = NULL;
224 	break;
225       }
226     }
227   }
228 
229   run_a_until(ctx, -1, a->next_condition);
230 
231   for (e = a->events->head; e; e = e->next) {
232     TEST_E(e->data->e_event, nua_r_register);
233     TEST(e->data->e_status, 200);
234     TEST_1(sip = sip_object(e->data->e_msg));
235     TEST_1(sip->sip_contact);
236     if (!e->next)
237       break;
238   }
239   TEST_1(e);
240   TEST_1(sip = sip_object(e->data->e_msg));
241   TEST_S(sip->sip_contact->m_expires, "3600");
242   TEST_1(!e->next);
243   free_events_in_list(ctx, a->events);
244 
245   if (print_headings)
246     printf("TEST NUA-2.3.4: PASSED\n");
247 
248   TEST_1(0);
249 
250 #endif
251 
252   END();
253 }
254 
test_register_a(struct context * ctx)255 int test_register_a(struct context *ctx)
256 {
257   BEGIN();
258 
259   struct endpoint *a = &ctx->a;
260   struct call *a_reg = a->reg;
261   struct event *e;
262   sip_t const *sip;
263   sip_cseq_t cseq[1];
264 
265 /* REGISTER test
266 
267    A			R
268    |------REGISTER----->|
269    |<-------401---------|
270    |------REGISTER----->|
271    |<-------200---------|
272    |			|
273 
274 */
275 
276   if (print_headings)
277     printf("TEST NUA-2.3.1: REGISTER a\n");
278 
279   test_proxy_domain_set_expiration(ctx->a.domain, 5, 5, 10);
280 
281   TEST_1(a_reg->nh = nua_handle(a->nua, a_reg, TAG_END()));
282 
283   sip_cseq_init(cseq)->cs_seq = 12;
284   cseq->cs_method = sip_method_register;
285   cseq->cs_method_name = sip_method_name_register;
286 
287   REGISTER(a, a_reg, a_reg->nh, SIPTAG_TO(a->to),
288 	   NUTAG_OUTBOUND("natify options-keepalive validate"),
289 	   NUTAG_KEEPALIVE(1000),
290 	   NUTAG_M_DISPLAY("A&A"),
291 	   NUTAG_M_USERNAME("a"),
292 	   NUTAG_M_PARAMS("foo=bar"),
293 	   NUTAG_M_FEATURES("q=0.9"),
294 	   SIPTAG_CSEQ(cseq),
295 	   TAG_END());
296   run_a_until(ctx, -1, save_until_final_response);
297 
298   TEST_1(e = a->events->head);
299   TEST_1(sip = sip_object(e->data->e_msg));
300   if (ctx->nat && e->data->e_status == 100) {
301     TEST_E(e->data->e_event, nua_r_register);
302     TEST(e->data->e_status, 100);
303     TEST(sip->sip_status->st_status, 406);
304     /* Check that CSeq included in tags is actually used in the request */
305     TEST(sip->sip_cseq->cs_seq, 13);
306     TEST_1(!sip->sip_contact);
307     TEST_1(e = e->next);
308     TEST_1(sip = sip_object(e->data->e_msg));
309     TEST(sip->sip_cseq->cs_seq, 14);
310   }
311   else {
312     /* Check that CSeq included in tags is actually used in the request */
313     TEST(sip->sip_cseq->cs_seq, 13);
314   }
315   TEST_E(e->data->e_event, nua_r_register);
316   TEST(e->data->e_status, 401);
317   TEST(sip->sip_status->st_status, 401);
318   TEST_1(!sip->sip_contact);
319   TEST_1(!e->next);
320   free_events_in_list(ctx, a->events);
321 
322   AUTHENTICATE(a, a_reg, a_reg->nh,
323 	       NUTAG_AUTH("Digest:\"test-proxy\":alice:secret"), TAG_END());
324   run_a_until(ctx, -1, save_until_final_response);
325 
326   TEST_1(e = a->events->head);
327   TEST_E(e->data->e_event, nua_r_register);
328   TEST(e->data->e_status, 200);
329   TEST_1(sip = sip_object(e->data->e_msg));
330   TEST_1(sip->sip_contact);
331   { char const *expect_m_display = "\"A&A\"";
332     /* VC does not dig \" with TEST_S() */
333   TEST_S(sip->sip_contact->m_display, expect_m_display); }
334   TEST_S(sip->sip_contact->m_url->url_user, "a");
335   TEST_1(strstr(sip->sip_contact->m_url->url_params, "foo=bar"));
336   TEST_S(sip->sip_contact->m_q, "0.9");
337 
338   if (ctx->nat) {
339     TEST_1(e = a->specials->head);
340   }
341 
342   test_proxy_domain_set_expiration(ctx->a.domain, 600, 3600, 36000);
343 
344   if (print_headings)
345     printf("TEST NUA-2.3.1: PASSED\n");
346 
347   END();
348 }
349 
test_register_b(struct context * ctx)350 int test_register_b(struct context *ctx)
351 {
352   BEGIN();
353 
354   struct endpoint  *b = &ctx->b;
355   struct call *b_reg = b->reg;
356   struct event *e;
357   sip_t const *sip;
358 
359   if (print_headings)
360     printf("TEST NUA-2.3.2: REGISTER b\n");
361 
362   test_proxy_domain_set_expiration(ctx->b.domain, 5, 5, 10);
363 
364   TEST_1(b_reg->nh = nua_handle(b->nua, b_reg, TAG_END()));
365 
366   /* Test application-supplied contact */
367   {
368     sip_contact_t m[1];
369     sip_contact_init(m)->m_url[0] = b->contact->m_url[0];
370 
371     m->m_display = "B";
372     m->m_url->url_user = "b";
373 
374     /* Include "tcp" transport parameter in Contact */
375     if (ctx->p)
376       m->m_url->url_params = "transport=tcp";
377 
378     REGISTER(b, b_reg, b_reg->nh, SIPTAG_TO(b->to),
379 	     SIPTAG_CONTACT(m),
380 	     /* Do not include credentials unless challenged */
381 	     NUTAG_AUTH_CACHE(nua_auth_cache_challenged),
382 	     TAG_END());
383   }
384   run_ab_until(ctx, -1, save_events, -1, save_until_final_response);
385 
386   TEST_1(e = b->events->head);
387   TEST_E(e->data->e_event, nua_r_register);
388   TEST_1(sip = sip_object(e->data->e_msg));
389   TEST(e->data->e_status, 401);
390   TEST(sip->sip_status->st_status, 401);
391   TEST_1(!sip->sip_contact);
392   TEST_1(!e->next);
393   free_events_in_list(ctx, b->events);
394 
395   AUTHENTICATE(b, b_reg, b_reg->nh,
396 	       NUTAG_AUTH("Digest:\"test-proxy\":bob:secret"), TAG_END());
397   run_ab_until(ctx, -1, save_events, -1, save_until_final_response);
398 
399   TEST_1(e = b->events->head);
400   TEST_E(e->data->e_event, nua_r_register);
401   TEST(e->data->e_status, 200);
402   TEST_1(sip = sip_object(e->data->e_msg));
403   TEST_1(sip->sip_contact);
404   TEST_S(sip->sip_contact->m_display, "B");
405   TEST_S(sip->sip_contact->m_url->url_user, "b");
406   free_events_in_list(ctx, b->events);
407 
408   test_proxy_domain_set_expiration(ctx->b.domain, 600, 3600, 36000);
409 
410   if (print_headings)
411     printf("TEST NUA-2.3.2: PASSED\n");
412 
413   END();
414 }
415 
test_register_c(struct context * ctx)416 int test_register_c(struct context *ctx)
417 {
418   BEGIN();
419 
420   struct endpoint *c = &ctx->c;
421   struct call *c_reg = c->reg;
422   struct event *e;
423   sip_t const *sip;
424 
425   if (print_headings)
426     printf("TEST NUA-2.3.3: REGISTER c\n");
427 
428   test_proxy_domain_set_expiration(ctx->c.domain, 600, 3600, 36000);
429   test_proxy_domain_set_authorize(ctx->c.domain, "test-proxy-0");
430 
431   TEST_1(c_reg->nh = nua_handle(c->nua, c_reg, TAG_END()));
432 
433   REGISTER(c, c_reg, c_reg->nh, SIPTAG_TO(c->to),
434 	   SIPTAG_FROM(c->to),
435 	   NUTAG_OUTBOUND(NULL),
436 	   NUTAG_M_DISPLAY("C"),
437 	   NUTAG_M_USERNAME("c"),
438 	   NUTAG_M_PARAMS("c=1"),
439 	   NUTAG_M_FEATURES("q=0.987;expires=5"),
440 	   NUTAG_CALLEE_CAPS(1),
441 	   SIPTAG_EXPIRES_STR("5"), /* Test 423 negotiation */
442 	   TAG_END());
443   run_abc_until(ctx, -1, save_events, -1, save_events,
444 		-1, save_until_final_response);
445 
446   TEST_1(e = c->events->head);
447   TEST_E(e->data->e_event, nua_r_register);
448   TEST_1(sip = sip_object(e->data->e_msg));
449   TEST(e->data->e_status, 401);
450   TEST(sip->sip_status->st_status, 401);
451   TEST_1(!sip->sip_contact);
452   TEST_1(!e->next);
453   free_events_in_list(ctx, c->events);
454 
455   AUTHENTICATE(c, c_reg, c_reg->nh,
456 	       NUTAG_AUTH("Digest:\"test-proxy-0\":charlie:secret"), TAG_END());
457   run_abc_until(ctx, -1, save_events, -1, save_events,
458 		-1, save_until_final_response);
459 
460   TEST_1(e = c->events->head);
461   TEST_E(e->data->e_event, nua_r_register);
462   TEST(e->data->e_status, 100);
463   TEST_1(sip = sip_object(e->data->e_msg));
464   TEST(sip->sip_status->st_status, 423);
465   TEST_1(e = e->next);
466   if (e->data->e_status == 100 && e->data->e_event == nua_r_register) {
467     TEST_1(sip = sip_object(e->data->e_msg));
468     TEST(sip->sip_status->st_status, 401);
469     TEST_1(e = e->next);
470   }
471   TEST(e->data->e_status, 200); TEST_E(e->data->e_event, nua_r_register);
472   TEST_1(sip = sip_object(e->data->e_msg));
473   TEST_1(sip->sip_contact);
474   TEST_S(sip->sip_contact->m_display, "C");
475   TEST_S(sip->sip_contact->m_url->url_user, "c");
476   TEST_1(strstr(sip->sip_contact->m_url->url_params, "c=1"));
477   TEST_S(sip->sip_contact->m_q, "0.987");
478   TEST_1(msg_header_find_param(sip->sip_contact->m_common, "methods="));
479   TEST_1(!e->next);
480   free_events_in_list(ctx, c->events);
481 
482   if (print_headings)
483     printf("TEST NUA-2.3.3: PASSED\n");
484 
485   END();
486 }
487 
test_register_refresh(struct context * ctx)488 int test_register_refresh(struct context *ctx)
489 {
490   BEGIN();
491 
492   struct endpoint *a = &ctx->a,  *b = &ctx->b, *x;
493   struct event *e;
494   sip_t const *sip;
495   int seen_401;
496 
497   if (print_headings)
498     printf("TEST NUA-2.3.4: refresh REGISTER\n");
499 
500   if (!ctx->p) {
501     free_events_in_list(ctx, a->events);
502     free_events_in_list(ctx, b->events);
503     return 0;
504   }
505 
506   /* Wait for A and B to refresh their registrations */
507 
508   /*
509    * Avoid race condition: if X has already refreshed registration
510    * with expiration time of 3600 seconds, do not wait for new refresh
511    */
512   a->next_condition = save_until_final_response;
513   b->next_condition = save_until_final_response;
514 
515   for (x = a; x; x = x == a ? b : NULL) {
516     for (e = x->events->head; e; e = e->next) {
517       if (e->data->e_event == nua_r_register &&
518 	  e->data->e_status == 200 &&
519 	  (sip = sip_object(e->data->e_msg)) &&
520 	  sip->sip_contact &&
521 	  sip->sip_contact->m_expires &&
522 	  strcmp(sip->sip_contact->m_expires, "3600") == 0) {
523 	x->next_condition = NULL;
524 	break;
525       }
526     }
527   }
528 
529   run_ab_until(ctx, -1, a->next_condition, -1, b->next_condition);
530 
531   for (e = a->events->head; e; e = e->next) {
532     TEST_E(e->data->e_event, nua_r_register);
533     TEST(e->data->e_status, 200);
534     TEST_1(sip = sip_object(e->data->e_msg));
535     TEST_1(sip->sip_contact);
536     if (!e->next)
537       break;
538   }
539   TEST_1(e);
540   TEST_1(sip = sip_object(e->data->e_msg));
541   TEST_S(sip->sip_contact->m_expires, "3600");
542   TEST_1(!e->next);
543   free_events_in_list(ctx, a->events);
544 
545   seen_401 = 0;
546 
547   for (e = b->events->head; e; e = e->next) {
548     TEST_E(e->data->e_event, nua_r_register);
549     TEST_1(sip = sip_object(e->data->e_msg));
550 
551     if (e->data->e_status == 200) {
552       TEST(e->data->e_status, 200);
553       TEST_1(seen_401);
554       TEST_1(sip->sip_contact);
555     }
556     else if (sip->sip_status && sip->sip_status->st_status == 401) {
557       seen_401 = 1;
558     }
559 
560     if (!e->next)
561       break;
562   }
563   TEST_1(e);
564   TEST_S(sip->sip_contact->m_expires, "3600");
565   TEST_1(!e->next);
566   free_events_in_list(ctx, b->events);
567 
568   if (print_headings)
569     printf("TEST NUA-2.3.4: PASSED\n");
570 
571   if (!ctx->p)
572     return 0;
573 
574   if (print_headings)
575     printf("TEST NUA-2.3.5: re-REGISTER when TCP connection is closed\n");
576 
577   test_proxy_close_tports(ctx->p);
578 
579   run_b_until(ctx, -1, save_until_final_response);
580 
581   TEST_1(e = b->events->head);
582   TEST_E(e->data->e_event, nua_r_register);
583   if (e->data->e_status == 100)
584     TEST_1(e = e->next);
585   TEST_1(sip = sip_object(e->data->e_msg));
586   TEST_1(sip->sip_contact);
587   TEST_S(sip->sip_contact->m_expires, "3600");
588   TEST_1(!e->next);
589 
590   free_events_in_list(ctx, b->events);
591 
592   if (print_headings)
593     printf("TEST NUA-2.3.5: PASSED\n");
594 
595   END();
596 }
597 
registrar_299(CONDITION_PARAMS)598 int registrar_299(CONDITION_PARAMS)
599 {
600   msg_t *request = nua_current_request(nua);
601 
602   save_event_in_list(ctx, event, ep, ep->call);
603 
604   if (event == nua_i_register) {
605     RESPOND(ep, call, nh, 299, "YES", NUTAG_WITH(request), TAG_END());
606     return 1;
607   }
608 
609   return 0;
610 }
611 
test_register_to_c(struct context * ctx)612 int test_register_to_c(struct context *ctx)
613 {
614   BEGIN();
615 
616   struct endpoint *b = &ctx->b, *c = &ctx->c;
617   struct call *b_call = b->call, *c_call = c->call;
618   struct event *e;
619   sip_t const *sip;
620 
621   if (print_headings)
622     printf("TEST NUA-2.6.1: REGISTER b to c\n");
623 
624   nua_set_params(ctx->c.nua,
625 		 NUTAG_ALLOW("REGISTER"),
626 		 TAG_END());
627   run_c_until(ctx, nua_r_set_params, until_final_response);
628 
629   TEST_1(b_call->nh = nua_handle(b->nua, b_call, TAG_END()));
630 
631   REGISTER(b, b_call, b_call->nh,
632 	   NUTAG_REGISTRAR((url_string_t *)c->contact->m_url),
633 	   SIPTAG_TO(b->to),
634 	   NUTAG_OUTBOUND(NULL),
635 	   SIPTAG_CONTACT_STR(NULL),
636 	   TAG_END());
637   run_bc_until(ctx, -1, save_until_final_response, -1, registrar_299);
638 
639   TEST_1(e = b->events->head);
640   TEST_E(e->data->e_event, nua_r_register);
641   TEST(e->data->e_status, 299);
642   TEST_1(sip = sip_object(e->data->e_msg));
643   TEST_1(!sip->sip_contact);
644 
645   free_events_in_list(ctx, b->events);
646   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
647 
648   TEST_1(e = c->events->head);
649   TEST_E(e->data->e_event, nua_i_register);
650   TEST(e->data->e_status, 100);
651   TEST_1(sip = sip_object(e->data->e_msg));
652   TEST_1(!sip->sip_contact);
653 
654   free_events_in_list(ctx, c->events);
655   nua_handle_destroy(c_call->nh), c_call->nh = NULL;
656 
657   if (print_headings)
658     printf("TEST NUA-2.6.1: PASSED\n");
659 
660   END();
661 }
662 
663 
test_register(struct context * ctx)664 int test_register(struct context *ctx)
665 {
666   if (test_register_to_c(ctx))
667     return 1;
668 
669   if (ctx->proxy_tests)
670     if (test_register_to_proxy(ctx)) return 1;
671 
672   return 0;
673 }
674 
675 
test_connectivity(struct context * ctx)676 int test_connectivity(struct context *ctx)
677 {
678   if (!ctx->proxy_tests)
679     return 0;			/* No proxy */
680 
681   BEGIN();
682 
683   struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
684   struct call *a_call = a->call, *b_call = b->call, *c_call = c->call;
685   struct event *e;
686   sip_t const *sip;
687 
688   /* Connectivity test using OPTIONS */
689 
690   if (print_headings)
691     printf("TEST NUA-2.4.1: OPTIONS from A to B\n");
692 
693   TEST_1(a_call->nh = nua_handle(a->nua, a_call, SIPTAG_TO(b->to), TAG_END()));
694 
695   OPTIONS(a, a_call, a_call->nh,
696 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(b->contact->m_url)),
697 	  NUTAG_ALLOW("OPTIONS"),
698 	  TAG_END());
699 
700   run_ab_until(ctx, -1, save_until_final_response, -1, save_until_received);
701 
702   /* Client events: nua_options(), nua_r_options */
703   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_r_options);
704   TEST(e->data->e_status, 200);
705   TEST_1(sip = sip_object(e->data->e_msg));
706   TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
707   /* TEST_1(sip->sip_content_type); */
708   /* TEST_1(sip->sip_payload); */
709   TEST_1(!e->next);
710 
711   free_events_in_list(ctx, a->events);
712   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
713 
714   /* Server events: nua_i_options */
715   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_i_options);
716   TEST(e->data->e_status, 200);
717   TEST_1(!e->next);
718 
719   free_events_in_list(ctx, b->events);
720   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
721 
722   if (print_headings)
723     printf("TEST NUA-2.4.1: PASSED\n");
724 
725   if (print_headings)
726     printf("TEST NUA-2.4.2: OPTIONS from B to C\n");
727 
728   TEST_1(b_call->nh = nua_handle(b->nua, b_call, SIPTAG_TO(c->to), TAG_END()));
729 
730   OPTIONS(b, b_call, b_call->nh,
731 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(c->contact->m_url)),
732 	  TAG_END());
733 
734   run_abc_until(ctx, -1, NULL,
735 		-1, save_until_final_response,
736 		-1, save_until_received);
737 
738   /* Client events: nua_options(), nua_r_options */
739   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_r_options);
740   TEST(e->data->e_status, 200);
741   TEST_1(sip = sip_object(e->data->e_msg));
742   TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
743   /* TEST_1(sip->sip_content_type); */
744   /* TEST_1(sip->sip_payload); */
745   TEST_1(!e->next);
746 
747   free_events_in_list(ctx, b->events);
748   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
749 
750   /* Server events: nua_i_options */
751   TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_i_options);
752   TEST(e->data->e_status, 200);
753   TEST_1(!e->next);
754 
755   free_events_in_list(ctx, c->events);
756   nua_handle_destroy(c_call->nh), c_call->nh = NULL;
757 
758   if (print_headings)
759     printf("TEST NUA-2.4.2: PASSED\n");
760 
761   if (print_headings)
762     printf("TEST NUA-2.4.3: OPTIONS from C to A\n");
763 
764   TEST_1(c_call->nh = nua_handle(c->nua, c_call, SIPTAG_TO(a->to), TAG_END()));
765 
766   OPTIONS(c, c_call, c_call->nh,
767 	  SIPTAG_FROM(c->to),
768 	  TAG_IF(!ctx->proxy_tests, NUTAG_URL(a->contact->m_url)),
769 	  TAG_END());
770 
771   if (ctx->proxy_tests) {
772     run_abc_until(ctx, -1, NULL, -1, NULL, -1, save_until_final_response);
773 
774     /* Client events: nua_options(), nua_r_options */
775     TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
776     TEST(e->data->e_status, 407);
777     TEST_1(!e->next);
778 
779     free_events_in_list(ctx, c->events);
780 
781     /* Sneakily change the realm */
782 
783     TEST(test_proxy_domain_set_authorize(ctx->c.domain, "test-proxy"), 0);
784 
785     AUTHENTICATE(c, c_call, c_call->nh,
786 		 NUTAG_AUTH("Digest:\"test-proxy-0\":charlie:secret"),
787 		 TAG_END());
788 
789     run_abc_until(ctx, -1, NULL, -1, NULL, -1, save_until_final_response);
790 
791     /* Client events: nua_options(), nua_r_options */
792     TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
793     TEST(e->data->e_status, 407);
794     TEST_1(!e->next);
795 
796     free_events_in_list(ctx, c->events);
797 
798     AUTHENTICATE(c, c_call, c_call->nh,
799 		 NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"),
800 		 TAG_END());
801   }
802 
803   run_abc_until(ctx, -1, save_until_received,
804 		-1, NULL,
805 		-1, save_until_final_response);
806 
807   /* Client events: nua_options(), nua_r_options */
808   TEST_1(e = c->events->head); TEST_E(e->data->e_event, nua_r_options);
809   TEST(e->data->e_status, 200);
810   TEST_1(sip = sip_object(e->data->e_msg));
811   TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
812   /* TEST_1(sip->sip_content_type); */
813   /* TEST_1(sip->sip_payload); */
814   TEST_1(!e->next);
815 
816   free_events_in_list(ctx, c->events);
817   nua_handle_destroy(c_call->nh), c_call->nh = NULL;
818 
819   /* Server events: nua_i_options */
820   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_options);
821   TEST(e->data->e_status, 200);
822   TEST_1(!e->next);
823 
824   free_events_in_list(ctx, a->events);
825   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
826 
827   if (print_headings)
828     printf("TEST NUA-2.4.3: PASSED\n");
829 
830   END();
831 }
832 
test_nat_timeout(struct context * ctx)833 int test_nat_timeout(struct context *ctx)
834 {
835   if (!ctx->proxy_tests || !ctx->nat)
836     return 0;			/* No proxy */
837 
838   BEGIN();
839 
840   struct endpoint *a = &ctx->a,  *b = &ctx->b;
841   struct call *a_call = a->call, *b_call = b->call;
842   struct event *e;
843   sip_t const *sip;
844 
845   /* Test what happens when NAT bindings go away */
846 
847   if (print_headings)
848     printf("TEST NUA-2.5.1: NAT binding change\n");
849 
850   free_events_in_list(ctx, a->specials);
851 
852   test_nat_flush(ctx->nat);	/* Break our connections */
853 
854   /* Run until we get final response to REGISTER */
855   run_a_until(ctx, -1, save_until_final_response);
856 
857   TEST_1(e = a->specials->head);
858   TEST_E(e->data->e_event, nua_i_outbound);
859   TEST(e->data->e_status, 102);
860   TEST_S(e->data->e_phrase, "NAT binding changed");
861   TEST_1(!e->next);
862 
863   free_events_in_list(ctx, a->specials);
864 
865   TEST_1(e = a->events->head);
866   TEST_E(e->data->e_event, nua_r_register);
867   TEST(e->data->e_status, 200);
868   TEST_1(!e->next);
869 
870   free_events_in_list(ctx, a->events);
871 
872   if (print_headings)
873     printf("TEST NUA-2.5.1: PASSED\n");
874 
875   if (print_headings)
876     printf("TEST NUA-2.5.2: OPTIONS from B to A\n");
877 
878   TEST_1(b_call->nh = nua_handle(b->nua, b_call, SIPTAG_TO(a->to), TAG_END()));
879 
880   OPTIONS(b, b_call, b_call->nh, TAG_END());
881 
882   run_ab_until(ctx, -1, save_until_received,
883 	       -1, save_until_final_response);
884 
885   /* Client events: nua_options(), nua_r_options */
886   TEST_1(e = b->events->head); TEST_E(e->data->e_event, nua_r_options);
887   TEST(e->data->e_status, 200);
888   TEST_1(sip = sip_object(e->data->e_msg));
889   TEST_1(sip->sip_allow); TEST_1(sip->sip_accept); TEST_1(sip->sip_supported);
890   /* TEST_1(sip->sip_content_type); */
891   /* TEST_1(sip->sip_payload); */
892   TEST_1(!e->next);
893 
894   free_events_in_list(ctx, b->events);
895   nua_handle_destroy(b_call->nh), b_call->nh = NULL;
896 
897   /* Server events: nua_i_options */
898   TEST_1(e = a->events->head); TEST_E(e->data->e_event, nua_i_options);
899   TEST(e->data->e_status, 200);
900   TEST_1(!e->next);
901 
902   free_events_in_list(ctx, a->events);
903   nua_handle_destroy(a_call->nh), a_call->nh = NULL;
904 
905   if (print_headings)
906     printf("TEST NUA-2.5.2: PASSED\n");
907 
908   END();
909 }
910 
test_unregister(struct context * ctx)911 int test_unregister(struct context *ctx)
912 {
913   if (!ctx->proxy_tests)
914     return 0;			/* No proxy */
915 
916   BEGIN();
917 
918   struct endpoint *a = &ctx->a,  *b = &ctx->b, *c = &ctx->c;
919   struct event *e;
920   sip_t const *sip;
921 
922 /* un-REGISTER test
923 
924    A			B
925    |----un-REGISTER---->|
926    |<-------200---------|
927    |			|
928 
929 */
930   if (print_headings)
931     printf("TEST NUA-13.1: un-REGISTER a\n");
932 
933   if (a->reg->nh) {
934     free_events_in_list(ctx, a->events);
935     UNREGISTER(a, NULL, a->reg->nh, TAG_END());
936     run_a_until(ctx, -1, save_until_final_response);
937     TEST_1(e = a->events->head);
938     TEST_E(e->data->e_event, nua_r_unregister);
939     if (e->data->e_status == 100) {
940       TEST_1(e = e->next);
941       TEST_E(e->data->e_event, nua_r_unregister);
942     }
943     TEST(e->data->e_status, 200);
944     TEST_1(sip = sip_object(e->data->e_msg));
945     TEST_1(!sip->sip_contact);
946     TEST_1(!e->next);
947     free_events_in_list(ctx, a->events);
948     nua_handle_destroy(a->reg->nh), a->reg->nh = NULL;
949   }
950 
951   if (print_headings)
952     printf("TEST NUA-13.1: PASSED\n");
953 
954   if (print_headings)
955     printf("TEST NUA-13.2: un-REGISTER b\n");
956 
957   if (b->reg->nh) {
958     free_events_in_list(ctx, b->events);
959     UNREGISTER(b, NULL, b->reg->nh, TAG_END());
960     run_b_until(ctx, -1, save_until_final_response);
961     TEST_1(e = b->events->head);
962     TEST_E(e->data->e_event, nua_r_unregister);
963     if (e->data->e_status == 100) {
964       TEST_1(e = e->next);
965       TEST_E(e->data->e_event, nua_r_unregister);
966     }
967     TEST(e->data->e_status, 200);
968     TEST_1(sip = sip_object(e->data->e_msg));
969     TEST_1(!sip->sip_contact);
970     TEST_1(!e->next);
971     free_events_in_list(ctx, b->events);
972     nua_handle_destroy(b->reg->nh), b->reg->nh = NULL;
973   }
974   if (print_headings)
975     printf("TEST NUA-13.2: PASSED\n");
976 
977   if (print_headings)
978     printf("TEST NUA-13.3: un-REGISTER c\n");
979 
980   /* Unregister using another handle */
981   free_events_in_list(ctx, c->events);
982   TEST_1(c->call->nh = nua_handle(c->nua, c->call, TAG_END()));
983   UNREGISTER(c, c->call, c->call->nh, SIPTAG_TO(c->to), SIPTAG_FROM(c->to),
984 	     NUTAG_M_DISPLAY("C"),
985 	     NUTAG_M_USERNAME("c"),
986 	     NUTAG_M_PARAMS("c=1"),
987 	     TAG_END());
988   run_c_until(ctx, -1, save_until_final_response);
989 
990   TEST_1(e = c->events->head);
991   TEST_E(e->data->e_event, nua_r_unregister);
992   TEST_1(sip = sip_object(e->data->e_msg));
993   TEST(e->data->e_status, 401);
994   TEST(sip->sip_status->st_status, 401);
995   TEST_1(!sip->sip_contact);
996   TEST_1(!e->next);
997   free_events_in_list(ctx, c->events);
998 
999   AUTHENTICATE(c, c->call, c->call->nh,
1000 	       NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
1001   run_c_until(ctx, -1, save_until_final_response);
1002 
1003   TEST_1(e = c->events->head);
1004   TEST_E(e->data->e_event, nua_r_unregister);
1005   TEST(e->data->e_status, 200);
1006   TEST_1(sip = sip_object(e->data->e_msg));
1007   TEST_1(!sip->sip_contact);
1008   TEST_1(!e->next);
1009   free_events_in_list(ctx, c->events);
1010   nua_handle_destroy(c->call->nh), c->call->nh = NULL;
1011 
1012   if (c->reg->nh) {
1013     UNREGISTER(c, NULL, c->reg->nh, TAG_END());
1014     run_c_until(ctx, -1, save_until_final_response);
1015     TEST_1(e = c->events->head);
1016     TEST_E(e->data->e_event, nua_r_unregister);
1017     if (e->data->e_status == 100) {
1018       TEST_1(e = e->next);
1019       TEST_E(e->data->e_event, nua_r_unregister);
1020     }
1021     if (e->data->e_status == 401) {
1022       TEST_1(!e->next);
1023       free_events_in_list(ctx, c->events);
1024       AUTHENTICATE(c, NULL, c->reg->nh,
1025 		   NUTAG_AUTH("Digest:\"test-proxy\":charlie:secret"), TAG_END());
1026       run_c_until(ctx, -1, save_until_final_response);
1027       TEST_1(e = c->events->head);
1028       TEST_E(e->data->e_event, nua_r_unregister);
1029     }
1030     TEST(e->data->e_status, 200);
1031     TEST_1(sip = sip_object(e->data->e_msg));
1032     TEST_1(sip->sip_from->a_url->url_user);
1033     TEST_1(!sip->sip_contact);
1034     TEST_1(!e->next);
1035     free_events_in_list(ctx, c->events);
1036     nua_handle_destroy(c->reg->nh), c->reg->nh = NULL;
1037   }
1038 
1039   if (print_headings)
1040     printf("TEST NUA-13.3: PASSED\n");
1041 
1042   END();
1043 }
1044