1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2008 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 check_register.c
26  *
27  * @brief Check-driven tester for Sofia SIP User Agent library
28  *
29  * @author Pekka Pessi <Pekka.Pessi@nokia.com>
30  *
31  * @copyright (C) 2008 Nokia Corporation.
32  */
33 
34 #include "config.h"
35 
36 #undef NDEBUG
37 
38 #include "check_nua.h"
39 
40 #include <sofia-sip/sip_status.h>
41 #include <sofia-sip/sip_header.h>
42 #include <sofia-sip/soa.h>
43 #include <sofia-sip/su_tagarg.h>
44 #include <sofia-sip/su_tag_io.h>
45 #include <sofia-sip/su_log.h>
46 
47 #include <stdlib.h>
48 #include <string.h>
49 #include <assert.h>
50 
51 SOFIAPUBVAR su_log_t tport_log[];
52 
53 static nua_t *nua;
54 
register_setup(void)55 static void register_setup(void)
56 {
57   nua = s2_nua_setup("register", TAG_END());
58 }
59 
register_thread_setup(void)60 static void register_thread_setup(void)
61 {
62   s2_nua_thread = 1;
63   register_setup();
64 }
65 
register_threadless_setup(void)66 static void register_threadless_setup(void)
67 {
68   s2_nua_thread = 1;
69   register_setup();
70 }
71 
pingpong_setup(void)72 static void pingpong_setup(void)
73 {
74   nua = s2_nua_setup("register with pingpong",
75 		     TPTAG_PINGPONG(20000),
76 		     TPTAG_KEEPALIVE(10000),
77 		     TAG_END());
78   tport_set_params(s2sip->tcp.tport, TPTAG_PONG2PING(1), TAG_END());
79 }
80 
pingpong_thread_setup(void)81 static void pingpong_thread_setup(void)
82 {
83   s2_nua_thread = 1;
84   pingpong_setup();
85 }
86 
pingpong_threadless_setup(void)87 static void pingpong_threadless_setup(void)
88 {
89   s2_nua_thread = 0;
90   pingpong_setup();
91 }
92 
register_teardown(void)93 static void register_teardown(void)
94 {
95   s2_teardown_started("register");
96   nua_shutdown(nua);
97   fail_unless_event(nua_r_shutdown, 200);
98   s2_nua_teardown();
99 }
100 
add_register_fixtures(TCase * tc,int threading,int pingpong)101 static void add_register_fixtures(TCase *tc, int threading, int pingpong)
102 {
103   void (*setup)(void);
104 
105   if (pingpong)
106     setup = threading ? pingpong_thread_setup : pingpong_threadless_setup;
107   else
108     setup = threading ? register_thread_setup : register_threadless_setup;
109 
110   tcase_add_checked_fixture(tc, setup, register_teardown);
111 }
112 
113 /* ---------------------------------------------------------------------- */
114 
START_TEST(register_1_0_1)115 START_TEST(register_1_0_1)
116 {
117   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
118   struct message *m;
119 
120   S2_CASE("1.0.1", "Failed Register", "REGISTER returned 403 response");
121 
122   nua_register(nh, TAG_END());
123 
124   fail_unless((m = s2_sip_wait_for_request(SIP_METHOD_REGISTER)) != NULL, NULL);
125 
126   s2_sip_respond_to(m, NULL,
127 		SIP_403_FORBIDDEN,
128 		TAG_END());
129   s2_sip_free_message(m);
130 
131   nua_handle_destroy(nh);
132 
133 } END_TEST
134 
135 
START_TEST(register_1_1_1)136 START_TEST(register_1_1_1)
137 {
138   S2_CASE("1.1.1", "Basic Register", "REGISTER returning 200 OK");
139 
140   s2_register_setup();
141 
142   s2_register_teardown();
143 
144 } END_TEST
145 
146 
START_TEST(register_1_1_2)147 START_TEST(register_1_1_2)
148 {
149   nua_handle_t *nh;
150   struct message *m;
151 
152   S2_CASE("1.1.2", "Register with dual authentication",
153 	  "Register, authenticate");
154 
155   nh = nua_handle(nua, NULL, TAG_END());
156 
157   nua_register(nh, TAG_END());
158 
159   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
160   s2_sip_respond_to(m, NULL,
161 		SIP_407_PROXY_AUTH_REQUIRED,
162 		SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
163 		TAG_END());
164   s2_sip_free_message(m);
165   fail_unless_event(nua_r_register, 407);
166 
167   nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
168 
169   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
170   s2_sip_respond_to(m, NULL,
171 		SIP_401_UNAUTHORIZED,
172 		SIPTAG_WWW_AUTHENTICATE_STR(s2_auth2_digest_str),
173 		SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
174 		TAG_END());
175   s2_sip_free_message(m);
176   fail_unless_event(nua_r_register, 401);
177 
178   nua_authenticate(nh, NUTAG_AUTH(s2_auth2_credentials), TAG_END());
179 
180   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
181   fail_if(!m);
182   fail_if(!m->sip->sip_authorization);
183   fail_if(!m->sip->sip_proxy_authorization);
184   s2_save_register(m);
185 
186   s2_sip_respond_to(m, NULL,
187 		SIP_200_OK,
188 		SIPTAG_CONTACT(s2->registration->contact),
189 		TAG_END());
190   s2_sip_free_message(m);
191 
192   assert(s2->registration->contact != NULL);
193   fail_unless_event(nua_r_register, 200);
194 
195   s2->registration->nh = nh;
196 
197   s2_register_teardown();
198 
199 } END_TEST
200 
201 /* ---------------------------------------------------------------------- */
202 
203 static char const receive_natted[] = "received=4.255.255.9";
204 static char const receive_natted2[] = "received=4.255.255.10";
205 
206 /* Return Via that looks very natted */
natted_via(struct message * m,const char * receive_natted)207 static sip_via_t *natted_via(struct message *m, const char *receive_natted)
208 {
209   su_home_t *h;
210   sip_via_t *via;
211 
212   h = msg_home(m->msg);
213   via = sip_via_dup(h, m->sip->sip_via);
214   msg_header_replace_param(h, via->v_common, receive_natted);
215 
216   if (via->v_protocol == sip_transport_udp)
217     msg_header_replace_param(h, via->v_common, "rport=9");
218 
219   if (via->v_protocol == sip_transport_tcp && via->v_rport) {
220     tp_name_t const *tpn = tport_name(m->tport);
221     char *rport = su_sprintf(h, "rport=%s", tpn->tpn_port);
222     msg_header_replace_param(h, via->v_common, rport);
223   }
224 
225   return via;
226 }
227 
228 /* ---------------------------------------------------------------------- */
229 
START_TEST(register_1_2_1)230 START_TEST(register_1_2_1) {
231   nua_handle_t *nh;
232   struct message *m;
233 
234   S2_CASE("1.2.1", "Register behind NAT",
235 	  "Register through NAT, detect NAT, re-REGISTER");
236 
237   nh = nua_handle(nua, NULL, TAG_END());
238 
239   nua_register(nh, TAG_END());
240 
241   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
242   fail_if(!m);
243   fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
244   s2_save_register(m);
245 
246   s2_sip_respond_to(m, NULL,
247 		SIP_200_OK,
248 		SIPTAG_CONTACT(s2->registration->contact),
249 		SIPTAG_VIA(natted_via(m, receive_natted)),
250 		TAG_END());
251   s2_sip_free_message(m);
252 
253   assert(s2->registration->contact != NULL);
254   fail_unless_event(nua_r_register, 100);
255 
256   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
257   fail_if(!m);
258   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
259   s2_save_register(m);
260 
261   s2_sip_respond_to(m, NULL,
262 		SIP_200_OK,
263 		SIPTAG_CONTACT(s2->registration->contact),
264 		SIPTAG_VIA(natted_via(m, receive_natted)),
265 		TAG_END());
266   s2_sip_free_message(m);
267 
268   fail_unless(s2->registration->contact != NULL);
269   fail_if(s2->registration->contact->m_next != NULL);
270   fail_unless_event(nua_r_register, 200);
271 
272   s2->registration->nh = nh;
273 
274   s2_register_teardown();
275 
276 } END_TEST
277 
278 
make_auth_natted_register(nua_handle_t * nh,tag_type_t tag,tag_value_t value,...)279 static nua_handle_t *make_auth_natted_register(
280   nua_handle_t *nh,
281   tag_type_t tag, tag_value_t value, ...)
282 {
283   struct message *m;
284 
285   ta_list ta;
286   ta_start(ta, tag, value);
287   nua_register(nh, ta_tags(ta));
288   ta_end(ta);
289 
290   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
291   s2_sip_respond_to(m, NULL,
292 		SIP_401_UNAUTHORIZED,
293 		SIPTAG_WWW_AUTHENTICATE_STR(s2_auth_digest_str),
294 		SIPTAG_VIA(natted_via(m, receive_natted)),
295 		TAG_END());
296   s2_sip_free_message(m);
297 
298   fail_unless_event(nua_r_register, 401);
299 
300   nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
301 
302   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
303   fail_if(!m);
304   fail_if(!m->sip->sip_authorization);
305   /* should not unregister the previous contact
306    * as it has not been successfully registered */
307   fail_if(!m->sip->sip_contact);
308   fail_if(m->sip->sip_contact->m_next);
309   s2_save_register(m);
310 
311   s2_sip_respond_to(m, NULL,
312 		SIP_200_OK,
313 		SIPTAG_CONTACT(s2->registration->contact),
314 		SIPTAG_VIA(natted_via(m, receive_natted)),
315 		TAG_END());
316   s2_sip_free_message(m);
317 
318   assert(s2->registration->contact != NULL);
319   fail_unless_event(nua_r_register, 200);
320 
321   return nh;
322 }
323 
START_TEST(register_1_2_2_1)324 START_TEST(register_1_2_2_1)
325 {
326   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
327 
328   S2_CASE("1.2.2.1", "Register behind NAT",
329 	  "Authenticate, outbound activated");
330 
331   mark_point();
332   make_auth_natted_register(nh, TAG_END());
333   s2->registration->nh = nh;
334   s2_register_teardown();
335 }
336 END_TEST
337 
START_TEST(register_1_2_2_2)338 START_TEST(register_1_2_2_2)
339 {
340   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
341   struct message *m;
342 
343   S2_CASE("1.2.2.2", "Register behind NAT",
344 	  "Authenticate, outbound activated, "
345 	  "authenticate OPTIONS probe, "
346 	  "NAT binding change");
347 
348   mark_point();
349   make_auth_natted_register(nh, TAG_END());
350   s2->registration->nh = nh;
351 
352   mark_point();
353 
354   m = s2_sip_wait_for_request(SIP_METHOD_OPTIONS);
355   fail_if(!m);
356   s2_sip_respond_to(m, NULL,
357 		SIP_407_PROXY_AUTH_REQUIRED,
358 		SIPTAG_VIA(natted_via(m, receive_natted)),
359 		SIPTAG_PROXY_AUTHENTICATE_STR(s2_auth_digest_str),
360 		TAG_END());
361   s2_sip_free_message(m);
362   mark_point();
363 
364   m = s2_sip_wait_for_request(SIP_METHOD_OPTIONS);
365   fail_if(!m); fail_if(!m->sip->sip_proxy_authorization);
366   s2_sip_respond_to(m, NULL,
367 		SIP_200_OK,
368 		SIPTAG_VIA(natted_via(m, receive_natted)),
369 		TAG_END());
370   s2_sip_free_message(m);
371 
372   su_root_step(s2base->root, 20); su_root_step(s2base->root, 20);
373   s2_nua_fast_forward(120, s2base->root);	  /* Default keepalive interval */
374   mark_point();
375 
376   m = s2_sip_wait_for_request(SIP_METHOD_OPTIONS);
377   s2_sip_respond_to(m, NULL,
378 		SIP_200_OK,
379 		SIPTAG_VIA(natted_via(m, receive_natted)),
380 		TAG_END());
381   s2_sip_free_message(m);
382 
383   su_root_step(s2base->root, 20); su_root_step(s2base->root, 20);
384   s2_nua_fast_forward(120, s2base->root);	  /* Default keepalive interval */
385   mark_point();
386 
387   m = s2_sip_wait_for_request(SIP_METHOD_OPTIONS);
388   s2_sip_respond_to(m, NULL,
389 		SIP_200_OK,
390 		SIPTAG_VIA(natted_via(m, receive_natted2)),
391 		TAG_END());
392   s2_sip_free_message(m);
393 
394   fail_unless_event(nua_i_outbound, 0);
395 
396   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
397   fail_if(!m); fail_if(!m->sip->sip_authorization);
398   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
399   s2_save_register(m);
400 
401   s2_sip_respond_to(m, NULL,
402 		SIP_200_OK,
403 		SIPTAG_CONTACT(s2->registration->contact),
404 		SIPTAG_VIA(natted_via(m, receive_natted2)),
405 		TAG_END());
406   s2_sip_free_message(m);
407 
408   fail_unless_event(nua_r_register, 200);
409 
410   fail_unless(s2->registration->contact != NULL);
411   fail_if(s2->registration->contact->m_next != NULL);
412 
413   s2_register_teardown();
414 
415 } END_TEST
416 
417 
START_TEST(register_1_2_2_3)418 START_TEST(register_1_2_2_3)
419 {
420   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
421   struct message *m;
422 
423   S2_CASE("1.2.2.3", "Register behind NAT",
424 	  "Authenticate, outbound activated, "
425 	  "detect NAT binding change when re-REGISTERing");
426 
427   mark_point();
428 
429   make_auth_natted_register(nh,
430 			    NUTAG_OUTBOUND("no-options-keepalive, no-validate"),
431 			    TAG_END());
432   s2->registration->nh = nh;
433 
434   s2_nua_fast_forward(3600, s2base->root);
435   mark_point();
436 
437   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
438   fail_if(!m); fail_if(!m->sip->sip_authorization);
439   s2_save_register(m);
440 
441   s2_sip_respond_to(m, NULL,
442 		SIP_200_OK,
443 		SIPTAG_CONTACT(s2->registration->contact),
444 		SIPTAG_VIA(natted_via(m, receive_natted2)),
445 		TAG_END());
446   s2_sip_free_message(m);
447 
448   fail_unless_event(nua_r_register, 100);
449 
450   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
451   fail_if(!m); fail_if(!m->sip->sip_authorization);
452   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
453   s2_save_register(m);
454 
455   s2_sip_respond_to(m, NULL,
456 		SIP_200_OK,
457 		SIPTAG_CONTACT(s2->registration->contact),
458 		SIPTAG_VIA(natted_via(m, receive_natted2)),
459 		TAG_END());
460   s2_sip_free_message(m);
461 
462   fail_unless(s2->registration->contact != NULL);
463   fail_if(s2->registration->contact->m_next != NULL);
464 
465   fail_unless_event(nua_r_register, 200);
466 
467   s2_register_teardown();
468 
469 } END_TEST
470 
471 
START_TEST(register_1_2_3)472 START_TEST(register_1_2_3) {
473   nua_handle_t *nh;
474   struct message *m;
475 
476   S2_CASE("1.2.3", "Register behind NAT",
477 	  "Outbound activated by error response");
478 
479   nh = nua_handle(nua, NULL, TAG_END());
480   nua_register(nh, TAG_END());
481 
482   mark_point();
483 
484   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
485   fail_if(!m);
486   fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
487 
488   s2_sip_respond_to(m, NULL,
489 		400, "Bad Contact",
490 		SIPTAG_VIA(natted_via(m, receive_natted)),
491 		TAG_END());
492   s2_sip_free_message(m);
493 
494   fail_unless_event(nua_r_register, 100);
495 
496   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
497   fail_if(!m);
498   s2_save_register(m);
499 
500   s2_sip_respond_to(m, NULL,
501 		SIP_200_OK,
502 		SIPTAG_CONTACT(s2->registration->contact),
503 		SIPTAG_VIA(natted_via(m, receive_natted)),
504 		TAG_END());
505   s2_sip_free_message(m);
506 
507   fail_unless(s2->registration->contact != NULL);
508   fail_if(s2->registration->contact->m_next != NULL);
509   fail_unless_event(nua_r_register, 200);
510 
511   s2->registration->nh = nh;
512 
513   s2_register_teardown();
514 
515 } END_TEST
516 
517 
518 /* ---------------------------------------------------------------------- */
519 
START_TEST(register_1_3_1)520 START_TEST(register_1_3_1)
521 {
522   nua_handle_t *nh;
523   struct message *m;
524 
525   S2_CASE("1.3.1", "Register over TCP via NAT",
526 	  "REGISTER via TCP, detect NTA, re-REGISTER");
527 
528   nh = nua_handle(nua, NULL, TAG_END());
529 
530   nua_register(nh, NUTAG_PROXY(s2sip->tcp.contact->m_url), TAG_END());
531 
532   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
533   fail_if(!m); fail_if(!m->sip->sip_contact || m->sip->sip_contact->m_next);
534   fail_if(!tport_is_tcp(m->tport));
535   s2_save_register(m);
536 
537   s2_sip_respond_to(m, NULL,
538 		SIP_200_OK,
539 		SIPTAG_CONTACT(s2->registration->contact),
540 		SIPTAG_VIA(natted_via(m, receive_natted)),
541 		TAG_END());
542   s2_sip_free_message(m);
543 
544   assert(s2->registration->contact != NULL);
545   fail_unless_event(nua_r_register, 100);
546 
547   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
548   fail_if(!m);
549   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
550   s2_save_register(m);
551 
552   s2_sip_respond_to(m, NULL,
553 		SIP_200_OK,
554 		SIPTAG_CONTACT(s2->registration->contact),
555 		SIPTAG_VIA(natted_via(m, receive_natted)),
556 		TAG_END());
557   s2_sip_free_message(m);
558 
559   fail_unless(s2->registration->contact != NULL);
560   fail_if(s2->registration->contact->m_next != NULL);
561   fail_unless(
562     url_has_param(s2->registration->contact->m_url, "transport=tcp"));
563   fail_unless_event(nua_r_register, 200);
564 
565   s2->registration->nh = nh;
566 
567   s2_register_teardown();
568 
569 } END_TEST
570 
571 
START_TEST(register_1_3_2_1)572 START_TEST(register_1_3_2_1)
573 {
574   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
575 
576   S2_CASE("1.3.2.1", "Register behind NAT",
577 	  "Authenticate, outbound activated");
578 
579   mark_point();
580   s2->registration->nh = nh;
581   make_auth_natted_register(nh, NUTAG_PROXY(s2sip->tcp.contact->m_url), TAG_END());
582   fail_if(!tport_is_tcp(s2->registration->tport));
583   s2_register_teardown();
584 }
585 END_TEST
586 
587 
START_TEST(register_1_3_2_2)588 START_TEST(register_1_3_2_2)
589 {
590   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
591   struct message *m;
592 
593   S2_CASE("1.3.2.2", "Register behind NAT with TCP",
594 	  "Detect NAT over TCP using rport. "
595 	  "Authenticate, detect NAT, "
596 	  "close TCP at server, wait for re-REGISTERs.");
597 
598   nua_set_params(nua, NTATAG_TCP_RPORT(1), TAG_END());
599   fail_unless_event(nua_r_set_params, 200);
600 
601   mark_point();
602   s2->registration->nh = nh;
603   make_auth_natted_register(
604     nh, NUTAG_PROXY(s2sip->tcp.contact->m_url),
605     NUTAG_OUTBOUND("no-options-keepalive, no-validate"),
606     TAG_END());
607   fail_if(!tport_is_tcp(s2->registration->tport));
608   tport_shutdown(s2->registration->tport, 2);
609 
610   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
611   fail_if(!m); fail_if(!m->sip->sip_authorization);
612   s2_save_register(m);
613 
614   s2_sip_respond_to(m, NULL,
615 		SIP_200_OK,
616 		SIPTAG_CONTACT(s2->registration->contact),
617 		SIPTAG_VIA(natted_via(m, receive_natted)),
618 		TAG_END());
619   s2_sip_free_message(m);
620 
621   /* The "NAT binding" changed when new TCP connection is established */
622   /* => NUA re-REGISTERs with newly detected contact */
623   fail_unless_event(nua_r_register, 100);
624 
625   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
626   fail_if(!m); fail_if(!m->sip->sip_authorization);
627   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
628   s2_save_register(m);
629 
630   s2_sip_respond_to(m, NULL,
631 		SIP_200_OK,
632 		SIPTAG_CONTACT(s2->registration->contact),
633 		SIPTAG_VIA(natted_via(m, receive_natted)),
634 		TAG_END());
635   s2_sip_free_message(m);
636 
637   fail_unless_event(nua_r_register, 200);
638 
639   fail_unless(s2->registration->contact != NULL);
640   fail_if(s2->registration->contact->m_next != NULL);
641 
642   s2_register_teardown();
643 }
644 END_TEST
645 
646 #if nomore
START_TEST(register_1_3_3_1)647 START_TEST(register_1_3_3_1)
648 {
649   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
650   struct message *m;
651   tport_t *tcp;
652 
653   S2_CASE("1.3.3.1", "Register behind NAT with UDP and TCP",
654 	  "Register with UDP, UDP time-outing, then w/ TCP using rport. ");
655 
656   nua_set_params(nua, NTATAG_TCP_RPORT(1), TAG_END());
657   fail_unless_event(nua_r_set_params, 200);
658 
659   mark_point();
660   s2->registration->nh = nh;
661 
662   nua_register(nh,
663 	       NUTAG_OUTBOUND("no-options-keepalive, no-validate"),
664 	       TAG_END());
665 
666   /* NTA tries with UDP, we drop them */
667   for (;;) {
668     m = s2_sip_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
669     if (!tport_is_udp(m->tport)) /* Drop UDP */
670       break;
671     s2_sip_free_message(m);
672     s2_nua_fast_forward(4, s2base->root);
673   }
674 
675   tcp = tport_ref(m->tport);
676 
677   /* Respond to request over TCP */
678   s2_sip_respond_to(m, NULL,
679 		SIP_401_UNAUTHORIZED,
680 		SIPTAG_WWW_AUTHENTICATE_STR(s2_auth_digest_str),
681 		SIPTAG_VIA(natted_via(m, receive_natted)),
682 		TAG_END());
683   s2_sip_free_message(m);
684   fail_unless_event(nua_r_register, 401);
685   nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
686 
687   /* Turn off pong */
688   tport_set_params(tcp, TPTAG_PONG2PING(0), TAG_END());
689 
690   /* Now request over UDP ... registering TCP contact! */
691   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
692   fail_if(!m); fail_if(!m->sip->sip_authorization);
693   s2_save_register(m);
694   fail_unless(
695     url_has_param(s2->registration->contact->m_url, "transport=tcp"));
696   s2_sip_respond_to(m, NULL,
697 		SIP_200_OK,
698 		SIPTAG_CONTACT(s2->registration->contact),
699 		SIPTAG_VIA(natted_via(m, receive_natted)),
700 		TAG_END());
701   s2_sip_free_message(m);
702 
703   /* NUA detects oops... re-registers UDP */
704   fail_unless_event(nua_r_register, 100);
705 
706   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
707   fail_if(!m); fail_if(!m->sip->sip_authorization);
708   fail_if(!m->sip->sip_contact || !m->sip->sip_contact->m_next);
709   s2_save_register(m);
710 
711   s2_sip_respond_to(m, NULL,
712 		SIP_200_OK,
713 		SIPTAG_CONTACT(s2->registration->contact),
714 		SIPTAG_VIA(natted_via(m, receive_natted)),
715 		TAG_END());
716   s2_sip_free_message(m);
717 
718   fail_unless_event(nua_r_register, 200);
719 
720   fail_unless(s2->registration->contact != NULL);
721   fail_if(s2->registration->contact->m_next != NULL);
722 
723   /* Wait until ping-pong failure closes the TCP connection */
724   {
725     int i;
726     for (i = 0; i < 5; i++) {
727       su_root_step(s2base->root, 5);
728       su_root_step(s2base->root, 5);
729       su_root_step(s2base->root, 5);
730       s2_nua_fast_forward(5, s2base->root);
731     }
732   }
733 
734   s2_register_teardown();
735 }
736 END_TEST
737 #endif
738 
START_TEST(register_1_3_3_2)739 START_TEST(register_1_3_3_2)
740 {
741   nua_handle_t *nh = nua_handle(nua, NULL, TAG_END());
742   struct message *m;
743   tport_t *tcp;
744   int i;
745 
746   S2_CASE("1.3.3.2", "Register behind NAT with TCP",
747 	  "Register w/ TCP using rport, pingpong. ");
748 
749   nua_set_params(nua, NTATAG_TCP_RPORT(1), TAG_END());
750   fail_unless_event(nua_r_set_params, 200);
751 
752   mark_point();
753   s2->registration->nh = nh;
754 
755   nua_register(nh,
756 	       NUTAG_PROXY(s2sip->tcp.contact->m_url),
757 	       NUTAG_OUTBOUND("no-options-keepalive, no-validate"),
758 	       TAG_END());
759 
760   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER); fail_if(!m);
761 
762   fail_unless(tport_is_tcp(m->tport));
763 
764   tcp = tport_ref(m->tport);
765 
766   /* Respond to request over TCP */
767   s2_sip_respond_to(m, NULL,
768 		SIP_401_UNAUTHORIZED,
769 		SIPTAG_WWW_AUTHENTICATE_STR(s2_auth_digest_str),
770 		SIPTAG_VIA(natted_via(m, receive_natted)),
771 		TAG_END());
772   s2_sip_free_message(m);
773   fail_unless_event(nua_r_register, 401);
774   nua_authenticate(nh, NUTAG_AUTH(s2_auth_credentials), TAG_END());
775 
776   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
777   fail_if(!m); fail_if(!m->sip->sip_authorization);
778   fail_if(!m->sip->sip_contact);
779   s2_save_register(m);
780 
781   s2_sip_respond_to(m, NULL,
782 		SIP_200_OK,
783 		SIPTAG_CONTACT(s2->registration->contact),
784 		SIPTAG_VIA(natted_via(m, receive_natted)),
785 		TAG_END());
786   s2_sip_free_message(m);
787 
788   fail_unless_event(nua_r_register, 200);
789 
790   fail_unless(s2->registration->contact != NULL);
791   fail_if(s2->registration->contact->m_next != NULL);
792 
793   /* Turn off pong */
794   tport_set_params(tcp, TPTAG_PONG2PING(0), TAG_END());
795 
796   /* Wait until ping-pong failure closes the TCP connection */
797   for (i = 0; i < 100; i++) {
798     s2_nua_fast_forward(5, s2base->root);
799     if (tport_is_closed(tcp))
800       break;
801   }
802 
803   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
804   fail_unless(tport_is_tcp(m->tport));
805   fail_unless(tcp != m->tport);
806   fail_if(!m); fail_if(!m->sip->sip_authorization);
807   fail_if(!m->sip->sip_contact);
808   s2_save_register(m);
809 
810   s2_sip_respond_to(m, NULL,
811 		SIP_200_OK,
812 		SIPTAG_CONTACT(s2->registration->contact),
813 		SIPTAG_VIA(natted_via(m, receive_natted)),
814 		TAG_END());
815   s2_sip_free_message(m);
816 
817   tport_unref(tcp);
818 
819   /* Contact changed */
820   fail_unless_event(nua_r_register, 100);
821 
822   m = s2_sip_wait_for_request(SIP_METHOD_REGISTER);
823   fail_if(!m); fail_if(!m->sip->sip_authorization);
824   fail_if(!m->sip->sip_contact);
825   fail_if(!m->sip->sip_contact->m_next);
826   s2_save_register(m);
827 
828   s2_sip_respond_to(m, NULL,
829 		SIP_200_OK,
830 		SIPTAG_CONTACT(s2->registration->contact),
831 		SIPTAG_VIA(natted_via(m, receive_natted)),
832 		TAG_END());
833   s2_sip_free_message(m);
834 
835   fail_unless_event(nua_r_register, 200);
836 
837   s2_register_teardown();
838 }
839 END_TEST
840 
841 /* ---------------------------------------------------------------------- */
842 
register_tcase(int threading)843 TCase *register_tcase(int threading)
844 {
845   TCase *tc = tcase_create("1 - REGISTER");
846 
847   add_register_fixtures(tc, threading, 0);
848 
849   {
850     tcase_add_test(tc, register_1_0_1);
851     tcase_add_test(tc, register_1_1_1);
852     tcase_add_test(tc, register_1_1_2);
853     tcase_add_test(tc, register_1_2_1);
854     tcase_add_test(tc, register_1_2_2_1);
855     tcase_add_test(tc, register_1_2_2_2);
856     tcase_add_test(tc, register_1_2_2_3);
857     tcase_add_test(tc, register_1_2_3);
858     tcase_add_test(tc, register_1_3_1);
859     tcase_add_test(tc, register_1_3_2_1);
860     tcase_add_test(tc, register_1_3_2_2);
861   }
862   tcase_set_timeout(tc, 10);
863   return tc;
864 }
865 
pingpong_tcase(int threading)866 TCase *pingpong_tcase(int threading)
867 {
868   TCase *tc = tcase_create("1 - REGISTER (with PingPong)");
869 
870   add_register_fixtures(tc, threading, 1);
871 
872   {
873     tcase_add_test(tc, register_1_3_3_2);
874   }
875 
876   tcase_set_timeout(tc, 10);
877   return tc;
878 }
879 
880 /* ---------------------------------------------------------------------- */
881 
882 static struct dialog *dialog = NULL;
883 
registrar_setup(void)884 static void registrar_setup(void)
885 {
886   struct event *event;
887   tagi_t const *t;
888   sip_contact_t *m;
889 
890   dialog = su_home_new(sizeof *dialog); fail_if(!dialog);
891 
892   nua = s2_nua_setup("register",
893 		     NUTAG_APPL_METHOD("REGISTER"),
894 		     NUTAG_ALLOW("REGISTER"),
895 		     NUTAG_PROXY(SIP_NONE),
896 		     TAG_END());
897 
898   nua_get_params(nua, TAG_ANY(), TAG_END());
899   event = s2_wait_for_event(nua_r_get_params, 200);
900   fail_unless(event != NULL);
901 
902   t = tl_find(event->data->e_tags, ntatag_contact);
903   fail_unless(t != NULL);
904   m = sip_contact_dup(dialog->home, (sip_contact_t *)t->t_value);
905   fail_unless(m != NULL);
906 
907   s2sip->sut.contact = m;
908 }
909 
registrar_thread_setup(void)910 static void registrar_thread_setup(void)
911 {
912   s2_nua_thread = 1;
913   registrar_setup();
914 }
915 
registrar_threadless_setup(void)916 static void registrar_threadless_setup(void)
917 {
918   s2_nua_thread = 1;
919   registrar_setup();
920 }
921 
registrar_teardown(void)922 static void registrar_teardown(void)
923 {
924   s2_teardown_started("registrar");
925   nua_shutdown(nua);
926   fail_unless_event(nua_r_shutdown, 200);
927   s2_nua_teardown();
928 }
929 
add_registrar_fixtures(TCase * tc,int threading)930 static void add_registrar_fixtures(TCase *tc, int threading)
931 {
932   void (*setup)(void);
933 
934   if (threading)
935     setup = registrar_thread_setup;
936   else
937     setup = registrar_threadless_setup;
938 
939   tcase_add_checked_fixture(tc, setup, registrar_teardown);
940 }
941 
START_TEST(registrar_1_4_0)942 START_TEST(registrar_1_4_0)
943 {
944   struct event *event;
945   nua_handle_t *nh;
946   struct message *response;
947 
948   S2_CASE("1.4.0", "Registrar", "Test receiving a REGISTER");
949 
950   fail_if(s2_sip_request_to(dialog, SIP_METHOD_REGISTER, NULL,
951 			    SIPTAG_FROM_STR("<sip:tst@example.com>"),
952 			    SIPTAG_TO_STR("<sip:tst@example.com>"),
953 			    TAG_END()));
954 
955   event = s2_wait_for_event(nua_i_register, 100);
956   fail_unless(event != NULL);
957   nh = event->nh; fail_if(!nh);
958 
959   nua_respond(nh, 200, "Ok",
960 	      NUTAG_WITH_SAVED(event->event),
961 	      TAG_END());
962 
963   response = s2_sip_wait_for_response(200, SIP_METHOD_REGISTER);
964   fail_if(!response);
965   s2_sip_free_message(response);
966 
967   nua_handle_destroy(nh);
968 }
969 END_TEST
970 
START_TEST(registrar_1_4_1)971 START_TEST(registrar_1_4_1)
972 {
973   struct event *event;
974   nua_handle_t *nh;
975   struct message *response;
976 
977   S2_CASE("1.4.1", "Registrar", "Test receiving a REGISTER via TCP");
978 
979   fail_if(s2_sip_request_to(dialog, SIP_METHOD_REGISTER, s2sip->tcp.tport,
980 			    SIPTAG_FROM_STR("<sip:tst@example.com>"),
981 			    SIPTAG_TO_STR("<sip:tst@example.com>"),
982 			    TAG_END()));
983 
984   event = s2_wait_for_event(nua_i_register, 100);
985   fail_if(!event);
986   nh = event->nh; fail_if(!nh);
987 
988   nua_respond(nh, 200, "Ok",
989 	      NUTAG_WITH_SAVED(event->event),
990 	      TAG_END());
991 
992   response = s2_sip_wait_for_response(200, SIP_METHOD_REGISTER);
993   fail_if(!response);
994   tport_shutdown(response->tport, 2);
995   s2_sip_free_message(response);
996 
997   event = s2_wait_for_event(nua_i_media_error, 0);
998   fail_if(!event);
999   nua_handle_destroy(nh);
1000 
1001   fail_if(s2_sip_request_to(dialog, SIP_METHOD_REGISTER, s2sip->tcp.tport,
1002 			    SIPTAG_FROM_STR("<sip:tst@example.com>"),
1003 			    SIPTAG_TO_STR("<sip:tst@example.com>"),
1004 			    TAG_END()));
1005 
1006   event = s2_wait_for_event(nua_i_register, 100);
1007   fail_if(!event);
1008   nh = event->nh; fail_if(!nh);
1009 
1010   nua_respond(nh, 200, "Ok",
1011 	      NUTAG_WITH_SAVED(event->event),
1012 	      TAG_END());
1013 
1014   response = s2_sip_wait_for_response(200, SIP_METHOD_REGISTER);
1015   fail_if(!response);
1016   nua_handle_destroy(nh);
1017 
1018   s2_step();
1019   s2_step();
1020   s2_step();
1021 
1022   tport_shutdown(response->tport, 2);
1023   s2_sip_free_message(response);
1024 }
1025 END_TEST
1026 
registrar_tcase(int threading)1027 TCase *registrar_tcase(int threading)
1028 {
1029   TCase *tc = tcase_create("1.4 - REGISTER server");
1030 
1031   add_registrar_fixtures(tc, threading);
1032 
1033   tcase_add_test(tc, registrar_1_4_0);
1034   tcase_add_test(tc, registrar_1_4_1);
1035 
1036   tcase_set_timeout(tc, 10);
1037 
1038   return tc;
1039 }
1040 
check_register_cases(Suite * suite,int threading)1041 void check_register_cases(Suite *suite, int threading)
1042 {
1043   suite_add_tcase(suite, register_tcase(threading));
1044   suite_add_tcase(suite, pingpong_tcase(threading));
1045   suite_add_tcase(suite, registrar_tcase(threading));
1046 }
1047 
1048