and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / test / torture_sdp.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc2543-)
3   Copyright (C) 2001  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21 #ifdef ENABLE_MPATROL
22 #include <mpatrol.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <osipparser2/osip_port.h>
29 #include <osipparser2/sdp_message.h>
30 #include <osip2/osip_negotiation.h>
31
32 int test_sdp_message (char *msg, int verbose);
33 int test_accessor_get_api (sdp_message_t * sdp);
34 int test_accessor_set_api (sdp_message_t * sdp);
35
36
37 typedef struct _ua_context_t
38 {
39
40   /* only one audio port is allowed at this time.. In the case, we
41      receive more than one m audio line, this may fail... */
42   char *m_audio_port;           /* audio port to be used for this session */
43   char *m_video_port;           /* audio port to be used for this session */
44
45 }
46 ua_context_t;
47
48 ua_context_t *ua_context = NULL;
49
50 osip_negotiation_t *osip_negotiation = NULL;
51
52 static int
53 ua_sdp_accept_audio_codec (osip_negotiation_ctx_t * context,
54                            char *port, char *number_of_port,
55                            int audio_qty, char *payload)
56 {
57   /* this may come from buggy implementation who                 */
58   /* propose several sdp lines while they only want 1 connection */
59   if (0 != audio_qty)
60     return -1;
61
62   if (0 == strcmp (payload, "0") || 0 == strcmp (payload, "3") ||
63       0 == strcmp (payload, "7") || 0 == strcmp (payload, "8"))
64     return 0;
65   return -1;
66 }
67
68 static int
69 ua_sdp_accept_video_codec (osip_negotiation_ctx_t * context,
70                            char *port, char *number_of_port,
71                            int video_qty, char *payload)
72 {
73   /* this may come from buggy implementation who                 */
74   /* propose several sdp lines while they only want 1 connection */
75   if (0 != video_qty)
76     return -1;
77   /* ... */
78   if (0 == strcmp (payload, "31"))
79     return 0;
80   return -1;
81 }
82
83 static int
84 ua_sdp_accept_other_codec (osip_negotiation_ctx_t * context,
85                            char *type, char *port,
86                            char *number_of_port, char *payload)
87 {
88   /* ... */
89   return -1;
90 }
91
92 static char *
93 ua_sdp_get_video_port (osip_negotiation_ctx_t * context, int pos_media)
94 {
95   ua_context_t *ua_con;
96
97   ua_con = (ua_context_t *) context->mycontext;
98   return osip_strdup (ua_con->m_video_port);    /* this port should not be static ... */
99   /* also, this method should be called more than once... */
100   /* If there is more than one audio line, this may fail :( */
101 }
102
103 static char *
104 ua_sdp_get_audio_port (osip_negotiation_ctx_t * context, int pos_media)
105 {
106   ua_context_t *ua_con;
107
108   ua_con = (ua_context_t *) context->mycontext;
109   return osip_strdup (ua_con->m_audio_port);    /* this port should not be static ... */
110   /* also, this method should be called more than once... */
111   /* If there is more than one audio line, this may fail :( */
112 }
113
114 int
115 main (int argc, char **argv)
116 {
117   int i;
118   int verbose = 0;              /* 0: verbose, 1 (or nothing: not verbose) */
119   char *marker;
120   FILE *torture_file;
121   char *tmp;
122   char *msg;
123   char *tmpmsg;
124   static int num_test = 0;
125
126   if (argc > 3)
127     {
128       if (0 == strncmp (argv[3], "-v", 2))
129         verbose = 1;
130     }
131
132   torture_file = fopen (argv[1], "r");
133   if (torture_file == NULL)
134     {
135       fprintf (stderr,
136                "Failed to open \"torture_sdps\" file.\nUsage: %s torture_file [-v]\n",
137                argv[0]);
138       exit (1);
139     }
140
141   ua_context = (ua_context_t *) osip_malloc (sizeof (ua_context_t));
142
143   ua_context->m_audio_port = osip_strdup ("20030");
144   ua_context->m_video_port = osip_strdup ("20040");
145
146   i = osip_negotiation_init (&osip_negotiation);
147   if (i != 0)
148     {
149       fprintf (stderr, "Failed to initialize the SDP negociator\n");
150       fclose (torture_file);
151       exit (1);
152     }
153   osip_negotiation_set_o_username (osip_negotiation, osip_strdup ("userX"));
154   osip_negotiation_set_o_session_id (osip_negotiation,
155                                      osip_strdup ("20000001"));
156   osip_negotiation_set_o_session_version (osip_negotiation,
157                                           osip_strdup ("20000001"));
158   osip_negotiation_set_o_nettype (osip_negotiation, osip_strdup ("IN"));
159   osip_negotiation_set_o_addrtype (osip_negotiation, osip_strdup ("IP4"));
160   osip_negotiation_set_o_addr (osip_negotiation,
161                                osip_strdup ("192.168.1.114"));
162
163   osip_negotiation_set_c_nettype (osip_negotiation, osip_strdup ("IN"));
164   osip_negotiation_set_c_addrtype (osip_negotiation, osip_strdup ("IP4"));
165   osip_negotiation_set_c_addr (osip_negotiation,
166                                osip_strdup ("192.168.1.114"));
167
168   /* ALL CODEC MUST SHARE THE SAME "C=" line and proto as the media 
169      will appear on the same "m" line... */
170   osip_negotiation_add_support_for_audio_codec (osip_negotiation,
171                                                 osip_strdup ("0"),
172                                                 osip_strdup ("2"),
173                                                 /* NULL, */
174                                                 osip_strdup ("RTP/AVP"),
175                                                 NULL, NULL, NULL,
176                                                 NULL, NULL,
177                                                 osip_strdup ("0 PCMU/8000"));
178   osip_negotiation_add_support_for_audio_codec (osip_negotiation,
179                                                 osip_strdup ("3"), NULL,
180                                                 osip_strdup ("RTP/AVP"), NULL,
181                                                 NULL, NULL, NULL, NULL,
182                                                 osip_strdup ("3 GSM/8000"));
183   osip_negotiation_add_support_for_audio_codec (osip_negotiation,
184                                                 osip_strdup ("7"), NULL,
185                                                 osip_strdup ("RTP/AVP"), NULL,
186                                                 NULL, NULL, NULL, NULL,
187                                                 osip_strdup ("7 LPC/8000"));
188   osip_negotiation_add_support_for_audio_codec (osip_negotiation,
189                                                 osip_strdup ("8"), NULL,
190                                                 osip_strdup ("RTP/AVP"), NULL,
191                                                 NULL, NULL, NULL, NULL,
192                                                 osip_strdup ("8 PCMA/8000"));
193   osip_negotiation_add_support_for_video_codec (osip_negotiation,
194                                                 osip_strdup ("31"), NULL,
195                                                 osip_strdup ("RTP/AVP"), NULL,
196                                                 NULL, NULL, NULL, NULL,
197                                                 osip_strdup
198                                                 ("31 H261/90000"));
199
200   osip_negotiation_set_fcn_accept_audio_codec (osip_negotiation,
201                                                &ua_sdp_accept_audio_codec);
202   osip_negotiation_set_fcn_accept_video_codec (osip_negotiation,
203                                                &ua_sdp_accept_video_codec);
204   osip_negotiation_set_fcn_accept_other_codec (osip_negotiation,
205                                                &ua_sdp_accept_other_codec);
206   osip_negotiation_set_fcn_get_audio_port (osip_negotiation,
207                                            &ua_sdp_get_audio_port);
208   osip_negotiation_set_fcn_get_video_port (osip_negotiation,
209                                            &ua_sdp_get_video_port);
210
211   i = 0;
212   tmp = (char *) osip_malloc (500);
213   marker = fgets (tmp, 500, torture_file);      /* lines are under 500 */
214   while (marker != NULL && i < atoi (argv[2]))
215     {
216       if (0 == strncmp (tmp, "|", 1))
217         i++;
218       marker = fgets (tmp, 500, torture_file);
219     }
220
221   num_test++;
222
223   msg = (char *) osip_malloc (10000);   /* msg are under 10000 */
224   tmpmsg = msg;
225
226   if (marker == NULL)
227     {
228       fprintf (stderr,
229                "Error! The message's number you specified does not exist\n");
230       fclose (torture_file);
231       osip_free (msg);
232       exit (1);                 /* end of file detected! */
233     }
234   /* this part reads an entire message, separator is "|" */
235   /* (it is unlinkely that it will appear in messages!) */
236   while (marker != NULL && strncmp (tmp, "|", 1))
237     {
238       osip_strncpy (tmpmsg, tmp, strlen (tmp));
239       tmpmsg = tmpmsg + strlen (tmp);
240       marker = fgets (tmp, 500, torture_file);
241     }
242
243   if (verbose)
244     {
245       fprintf (stdout, "test %s : ============================ \n", argv[2]);
246       fprintf (stdout, "%s", msg);
247
248       if (0 == test_sdp_message (msg, verbose))
249         fprintf (stdout, "test %s : ============================ OK\n",
250                  argv[2]);
251       else
252         fprintf (stdout, "test %s : ============================ FAILED\n",
253                  argv[2]);
254     }
255   else
256     {
257       if (0 == test_sdp_message (msg, verbose))
258         fprintf (stdout, "test %s : OK\n", argv[2]);
259       else
260         fprintf (stdout, "test %s : FAILED\n", argv[2]);
261     }
262
263   osip_free (msg);
264   osip_free (tmp);
265   osip_negotiation_free (osip_negotiation);
266   osip_free (ua_context->m_audio_port);
267   osip_free (ua_context->m_video_port);
268   osip_free (ua_context);
269   fclose (torture_file);
270   return 0;
271 }
272
273 int
274 test_sdp_message (char *msg, int verbose)
275 {
276   sdp_message_t *sdp;
277
278   {
279     char *result;
280
281     sdp_message_init (&sdp);
282     if (sdp_message_parse (sdp, msg) != 0)
283       {
284         fprintf (stdout, "ERROR: failed while parsing!\n");
285         sdp_message_free (sdp); /* try to free msg, even if it failed! */
286         /* this seems dangerous..... */
287         return -1;
288       }
289     else
290       {
291         int i;
292
293         i = sdp_message_to_str (sdp, &result);
294         test_accessor_get_api (sdp);
295         if (i == -1)
296           {
297             fprintf (stdout, "ERROR: failed while printing message!\n");
298             sdp_message_free (sdp);
299             return -1;
300           }
301         else
302           {
303             if (verbose)
304               fprintf (stdout, "%s", result);
305             if (strlen (result) != strlen (msg))
306               fprintf (stdout, "length differ from original message!\n");
307             if (0 == strncmp (result, msg, strlen (result)))
308               fprintf (stdout, "result equals msg!!\n");
309             osip_free (result);
310             {
311               osip_negotiation_ctx_t *context;
312
313               sdp_message_t *dest;
314
315               i = osip_negotiation_ctx_init (&context);
316               i =
317                 osip_negotiation_ctx_set_mycontext (context,
318                                                     (void *) ua_context);
319
320               {
321                 sdp_message_t *sdp;
322
323                 osip_negotiation_sdp_build_offer (osip_negotiation,
324                                                   context, &sdp,
325                                                   ua_context->m_audio_port,
326                                                   ua_context->m_video_port);
327                 sdp_message_to_str (sdp, &result);
328                 fprintf (stdout, "Here is the offer:\n%s\n", result);
329                 osip_free (result);
330                 osip_negotiation_sdp_message_put_on_hold (sdp);
331                 sdp_message_to_str (sdp, &result);
332                 fprintf (stdout, "Here is the offer on hold:\n%s\n", result);
333                 osip_free (result);
334                 sdp_message_free (sdp);
335               }
336
337
338
339               i = osip_negotiation_ctx_set_remote_sdp (context, sdp);
340               if (i != 0)
341                 {
342                   fprintf (stdout,
343                            "Initialisation of context failed. Could not negociate\n");
344                 }
345               else
346                 {
347                   fprintf (stdout, "Trying to execute a SIP negotiation:\n");
348                   i =
349                     osip_negotiation_ctx_execute_negotiation
350                     (osip_negotiation, context);
351                   fprintf (stdout, "return code: %i\n", i);
352                   if (i == 200)
353                     {
354                       dest = osip_negotiation_ctx_get_local_sdp (context);
355                       fprintf (stdout, "SDP answer:\n");
356                       i = sdp_message_to_str (dest, &result);
357                       if (i != 0)
358                         fprintf (stdout,
359                                  "Error found in SDP answer while printing\n");
360                       else
361                         fprintf (stdout, "%s\n", result);
362                       osip_free (result);
363                     }
364                   osip_negotiation_ctx_free (context);
365                   return 0;
366                 }
367             }
368           }
369         sdp_message_free (sdp);
370       }
371   }
372   return 0;
373 }
374
375 int
376 test_accessor_set_api (sdp_message_t * sdp)
377 {
378   return 0;
379 }
380
381 int
382 test_accessor_get_api (sdp_message_t * sdp)
383 {
384   char *tmp;
385   char *tmp2;
386   char *tmp3;
387   char *tmp4;
388   char *tmp5;
389   int i;
390   int k;
391
392   if (sdp_message_v_version_get (sdp))
393     printf ("v_version:      |%s|\n", sdp_message_v_version_get (sdp));
394   if (sdp_message_o_username_get (sdp))
395     printf ("o_originator:   |%s|", sdp_message_o_username_get (sdp));
396   if (sdp_message_o_sess_id_get (sdp))
397     printf (" |%s|", sdp_message_o_sess_id_get (sdp));
398   if (sdp_message_o_sess_version_get (sdp) != NULL)
399     printf (" |%s|", sdp_message_o_sess_version_get (sdp));
400   if (sdp_message_o_nettype_get (sdp))
401     printf (" |%s|", sdp_message_o_nettype_get (sdp));
402   if (sdp_message_o_addrtype_get (sdp))
403     printf (" |%s|", sdp_message_o_addrtype_get (sdp));
404   if (sdp_message_o_addr_get (sdp))
405     printf (" |%s|\n", sdp_message_o_addr_get (sdp));
406   if (sdp_message_s_name_get (sdp))
407     printf ("s_name:         |%s|\n", sdp_message_s_name_get (sdp));
408   if (sdp_message_i_info_get (sdp, -1))
409     printf ("i_info:         |%s|\n", sdp_message_i_info_get (sdp, -1));
410   if (sdp_message_u_uri_get (sdp))
411     printf ("u_uri:          |%s|\n", sdp_message_u_uri_get (sdp));
412
413   i = 0;
414   do
415     {
416       tmp = sdp_e_email_get (sdp, i);
417       if (tmp != NULL)
418         printf ("e_email:        |%s|\n", tmp);
419       i++;
420     }
421   while (tmp != NULL);
422   i = 0;
423   do
424     {
425       tmp = sdp_message_p_phone_get (sdp, i);
426       if (tmp != NULL)
427         printf ("p_phone:        |%s|\n", tmp);
428       i++;
429     }
430   while (tmp != NULL);
431
432   k = 0;
433   tmp = sdp_message_c_nettype_get (sdp, -1, k);
434   tmp2 = sdp_message_c_addrtype_get (sdp, -1, k);
435   tmp3 = sdp_message_c_addr_get (sdp, -1, k);
436   tmp4 = sdp_message_c_addr_multicast_ttl_get (sdp, -1, k);
437   tmp5 = sdp_message_c_addr_multicast_int_get (sdp, -1, k);
438   if (tmp != NULL && tmp4 != NULL && tmp5 != NULL)
439     printf ("c_connection:   |%s| |%s| |%s| |%s| |%s|\n",
440             tmp, tmp2, tmp3, tmp4, tmp5);
441   else if (tmp != NULL && tmp4 != NULL)
442     printf ("c_connection:   |%s| |%s| |%s| |%s| |%s|\n",
443             tmp, tmp2, tmp3, tmp4, "(null)");
444   else if (tmp != NULL && tmp5 != NULL)
445     printf ("c_connection:   |%s| |%s| |%s| |%s| |%s|\n",
446             tmp, tmp2, tmp3, "(null)", tmp5);
447   k = 0;
448   do
449     {
450       tmp = sdp_message_b_bwtype_get (sdp, -1, k);
451       tmp2 = sdp_message_b_bandwidth_get (sdp, -1, k);
452       if (tmp != NULL && tmp2 != NULL)
453         printf ("b_bandwidth:    |%s|:|%s|\n", tmp, tmp2);
454       else if (tmp != NULL)
455         printf ("b_bandwidth:    |%s|:|%s|\n", tmp, "(null)");
456       k++;
457     }
458   while (tmp != NULL);
459
460   k = 0;
461   do
462     {
463       tmp = sdp_message_t_start_time_get (sdp, k);
464       tmp2 = sdp_message_t_stop_time_get (sdp, k);
465       if (tmp != NULL && tmp2 != NULL)
466         printf ("t_descr_time:   |%s| |%s|\n", tmp, tmp2);
467       else if (tmp != NULL)
468         printf ("t_descr_time:   |%s| |%s|\n", tmp, "(null)");
469       i = 0;
470       do
471         {
472           tmp2 = sdp_message_r_repeat_get (sdp, k, i);
473           i++;
474           if (tmp2 != NULL)
475             printf ("r_repeat:    |%s|\n", tmp2);
476         }
477       while (tmp2 != NULL);
478       k++;
479     }
480   while (tmp != NULL);
481
482   /* TODO r */
483
484   if (sdp_message_z_adjustments_get (sdp) != NULL)
485     printf ("z_adjustments:  |%s|\n", sdp_message_z_adjustments_get (sdp));
486
487   tmp = sdp_message_k_keytype_get (sdp, -1);
488   tmp2 = sdp_message_k_keydata_get (sdp, -1);
489   if (tmp != NULL && tmp2 != NULL)
490     printf ("k_key:          |%s|:|%s|\n", tmp, tmp2);
491   else if (tmp != NULL)
492     printf ("k_key:          |%s|:|%s|\n", tmp, "(null)");
493
494   k = 0;
495   do
496     {
497       tmp = sdp_message_a_att_field_get (sdp, -1, k);
498       tmp2 = sdp_message_a_att_value_get (sdp, -1, k);
499       if (tmp != NULL && tmp2 != NULL)
500         printf ("a_attribute:    |%s|:|%s|\n", tmp, tmp2);
501       if (tmp != NULL)
502         printf ("a_attribute:    |%s|:|%s|\n", tmp, "(null)");
503       k++;
504     }
505   while (tmp != NULL);
506
507   i = 0;
508   while (!sdp_message_endof_media (sdp, i))
509     {
510
511       tmp = sdp_message_m_media_get (sdp, i);
512       tmp2 = sdp_message_m_port_get (sdp, i);
513       tmp3 = sdp_message_m_number_of_port_get (sdp, i);
514       tmp4 = sdp_message_m_proto_get (sdp, i);
515       if (tmp != NULL)
516         printf ("m_media:        |%s|", tmp);
517       else
518         printf ("m_media:        |%s|", "(null)");
519       if (tmp2 != NULL)
520         printf (" |%s|", tmp2);
521       else
522         printf (" |%s|", "(null)");
523       if (tmp3 != NULL)
524         printf (" |%s|", tmp3);
525       else
526         printf (" |%s|", "(null)");
527       if (tmp4 != NULL)
528         printf (" |%s|", tmp4);
529       else
530         printf (" |%s|", "(null)");
531       k = 0;
532       do
533         {
534           tmp = sdp_message_m_payload_get (sdp, i, k);
535           if (tmp != NULL)
536             printf (" |%s|", tmp);
537           k++;
538         }
539       while (tmp != NULL);
540       printf ("\n");
541       k = 0;
542       do
543         {
544           tmp = sdp_message_c_nettype_get (sdp, i, k);
545           tmp2 = sdp_message_c_addrtype_get (sdp, i, k);
546           tmp3 = sdp_message_c_addr_get (sdp, i, k);
547           tmp4 = sdp_message_c_addr_multicast_ttl_get (sdp, i, k);
548           tmp5 = sdp_message_c_addr_multicast_int_get (sdp, i, k);
549           if (tmp != NULL)
550             printf ("c_connection:   |%s| |%s| |%s| |%s| |%s|\n",
551                     tmp, tmp2, tmp3, tmp4, tmp5);
552           else
553             printf ("c_connection:   |%s|", "(null)");
554           if (tmp2 != NULL)
555             printf (" |%s|", tmp2);
556           else
557             printf (" |%s|", "(null)");
558           if (tmp3 != NULL)
559             printf (" |%s|", tmp3);
560           else
561             printf (" |%s|", "(null)");
562           if (tmp4 != NULL)
563             printf (" |%s|", tmp4);
564           else
565             printf (" |%s|", "(null)");
566           if (tmp5 != NULL)
567             printf (" |%s|", tmp5);
568           else
569             printf (" |%s|", "(null)");
570           printf ("\n");
571
572           if (tmp != NULL)
573             k++;
574         }
575       while (tmp != NULL);
576
577       k = 0;
578       do
579         {
580           tmp = sdp_message_b_bwtype_get (sdp, i, k);
581           tmp2 = sdp_message_b_bandwidth_get (sdp, i, k);
582           if (tmp != NULL)
583             printf ("b_bandwidth:    |%s|", tmp);
584           else
585             printf ("b_bandwidth:    |%s|", "(null)");
586           if (tmp2 != NULL)
587             printf (":|%s|\n", tmp2);
588           else
589             printf (":|%s|", "(null)");
590           printf ("\n");
591
592           k++;
593         }
594       while (tmp != NULL);
595
596
597       tmp = sdp_message_k_keytype_get (sdp, i);
598       tmp2 = sdp_message_k_keydata_get (sdp, i);
599       if (tmp != NULL)
600         printf ("k_key:          |%s|", tmp);
601       else
602         printf ("k_key:          |%s|", "(null)");
603       if (tmp2 != NULL)
604         printf (":|%s|", tmp2);
605       else
606         printf (":|%s|", "(null)");
607       printf ("\n");
608
609       k = 0;
610       do
611         {
612           tmp = sdp_message_a_att_field_get (sdp, i, k);
613           tmp2 = sdp_message_a_att_value_get (sdp, i, k);
614           if (tmp != NULL)
615             printf ("a_attribute:    |%s|", tmp);
616           else
617             printf ("a_attribute:    |%s|", "(null)");
618           if (tmp2 != NULL)
619             printf (":|%s|", tmp2);
620           else
621             printf (":|%s|", "(null)");
622           printf ("\n");
623
624           k++;
625         }
626       while (tmp != NULL);
627
628       i++;
629     }
630
631   return 0;
632 }
633
634 /*
635 int
636 ua_sdp_set_info(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
637   return 0;
638
639 }
640 int
641 ua_sdp_set_uri(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
642   return 0;
643 }
644
645 int
646 ua_sdp_add_email(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
647   return 0;
648 }
649
650 int
651 ua_sdp_add_phone(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
652   return 0;
653 }
654
655 int
656 ua_sdp_add_attributes(osip_negotiation_ctx_t *context, sdp_message_t *dest, int pos_media) {
657   return 0;
658 }
659 */