http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / libosip2 / src / test / torture_rfc3264.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  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 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <osipparser2/osip_rfc3264.h>
25 #include <osipparser2/osip_port.h>
26
27 #define AUDIO_CODEC 0x01
28 #define VIDEO_CODEC 0x02
29 #define T38_CODEC   0x04
30 #define APP_CODEC   0x08
31
32 int test_add_codec(struct osip_rfc3264 *cnf, int codec_type,
33                    int payload, char *attribute);
34 sdp_message_t *get_test_remote_message(int index, FILE *torture_file, int verbose);
35
36 int test_add_codec(struct osip_rfc3264 *cnf, int codec_type,
37                    int payload, char *attribute)
38 {
39   sdp_media_t *med;
40   sdp_attribute_t *attr;
41   char *tmp;
42   int i;
43   if (payload>127) return -1;
44   if (attribute==NULL || strlen(attribute)==0) return -1;
45
46   i = sdp_media_init(&med);
47   if (i!=0) goto error;
48   
49   tmp = malloc(4);
50   snprintf(tmp, 3, "%i", payload);
51   med->m_proto = strdup("RTP/AVP");
52   osip_list_add(med->m_payloads, tmp, -1);
53
54   i = sdp_attribute_init (&attr);
55   if (i != 0)
56     return -1;
57   attr->a_att_field = strdup("rtpmap");
58   attr->a_att_value = strdup(attribute);
59   osip_list_add (med->a_attributes, attr, -1);
60
61
62   switch(codec_type)
63     {
64     case AUDIO_CODEC:
65       med->m_media = strdup("audio");
66       osip_rfc3264_add_audio_media(cnf, med, -1);
67       break;
68     case VIDEO_CODEC:
69       med->m_media = strdup("video");
70       osip_rfc3264_add_video_media(cnf, med, -1);
71       break;
72     }
73
74   return 0;
75
76  error:
77   return -1;
78 }
79
80 sdp_message_t *get_test_remote_message(int index, FILE *torture_file, int verbose)
81 {
82   sdp_message_t *remote_sdp;
83   char *msg;
84   char *tmpmsg;
85   char *tmp;
86   char *marker;
87   int i;
88
89   i = 0;
90   tmp = (char *) osip_malloc (500);
91   marker = fgets (tmp, 500, torture_file);      /* lines are under 500 */
92   while (marker != NULL && i < index)
93     {
94       if (0 == strncmp (tmp, "|", 1))
95         i++;
96       marker = fgets (tmp, 500, torture_file);
97     }
98
99   msg = (char *) osip_malloc (10000);   /* msg are under 10000 */
100   tmpmsg = msg;
101
102   if (marker == NULL)
103     {
104       fprintf (stderr,
105                "Error! The message's number you specified does not exist\n");
106       osip_free (msg);
107       return NULL;                      /* end of file detected! */
108     }
109   /* this part reads an entire message, separator is "|" */
110   /* (it is unlinkely that it will appear in messages!) */
111   while (marker != NULL && strncmp (tmp, "|", 1))
112     {
113       osip_strncpy (tmpmsg, tmp, strlen (tmp));
114       tmpmsg = tmpmsg + strlen (tmp);
115       marker = fgets (tmp, 500, torture_file);
116     }
117
118   if (verbose)
119       fprintf (stdout, "%s\n", msg);
120
121   i = sdp_message_init(&remote_sdp);
122   if (i!=0) return NULL;
123   
124   i = sdp_message_parse(remote_sdp, msg);
125   if (i!=0) return NULL;
126   
127   return remote_sdp;
128 }
129
130 int main(int argc, char **argv)
131 {
132   struct osip_rfc3264 *cnf;
133   int i;
134   int verbose = 0;              /* 0: verbose, 1 (or nothing: not verbose) */
135   FILE *torture_file;
136
137   if (argc > 3)
138     {
139       if (0 == strncmp (argv[3], "-v", 2))
140         verbose = 1;
141       if (0 == strcmp (argv[3], "-vv"))
142         verbose = 2;
143     }
144
145   fprintf (stdout, "test %i : ============================ \n", atoi(argv[2]));
146
147   torture_file = fopen (argv[1], "r");
148   if (torture_file == NULL)
149     {
150       fprintf (stderr,
151                "Failed to open \"torture_sdps\" file.\nUsage: %s torture_file [-v]\n",
152                argv[0]);
153       fprintf (stdout, "test %s : ============================ FAILED\n", argv[2]);
154       exit (1);
155     }
156
157
158
159
160
161
162   i = osip_rfc3264_init(&cnf);
163   if (i!=0)
164     {
165       fprintf(stderr, "Cannot Initialize Negotiator feature.\n");
166       osip_rfc3264_free(cnf);
167       fclose (torture_file);
168       fprintf (stdout, "test %s : ============================ FAILED\n", argv[2]);
169       return -1;
170     }
171
172   test_add_codec(cnf, AUDIO_CODEC, 0,  "0 PCMU/8000");
173   test_add_codec(cnf, AUDIO_CODEC, 8,  "8 PCMA/8000");
174   test_add_codec(cnf, AUDIO_CODEC, 18, "18 G729/8000");
175
176   test_add_codec(cnf, VIDEO_CODEC, 97, "97 XXX/11111");
177   test_add_codec(cnf, VIDEO_CODEC, 31, "31 H261/90000");
178
179 #if 0
180   osip_rfc3264_del_video_media(cnf, 0);
181   osip_rfc3264_del_audio_media(cnf, 1);
182   osip_rfc3264_del_audio_media(cnf, 2);
183
184   test_add_codec(cnf, AUDIO_CODEC, 8,  "0 PCMA/8000");
185   test_add_codec(cnf, AUDIO_CODEC, 18, "18 G729/8000");
186 #endif
187
188   if (verbose==2)
189     __osip_rfc3264_print_codecs(cnf);
190
191   {
192     sdp_message_t *remote_sdp;
193     sdp_media_t *audio_tab[10];
194     sdp_media_t *video_tab[10];
195     sdp_media_t *t38_tab[10];
196     sdp_media_t *app_tab[10];
197     int res_audio = 0;
198     int res_video = 0;
199     int res_t38   = 0;
200     int res_app   = 0;
201
202     char str_local_sdp[8192];
203     sdp_message_t *local_sdp;
204     int mline;
205     char *tmp = NULL;
206
207     remote_sdp = get_test_remote_message(atoi(argv[2]), torture_file, verbose);
208     if (!remote_sdp)
209       {
210         fprintf(stderr, "Cannot Get remote SDP message for testing.\n");
211         osip_rfc3264_free(cnf);
212         fclose (torture_file);
213         fprintf (stdout, "test %s : ============================ FAILED\n", argv[2]);
214         return -1;
215       }
216
217     i=osip_rfc3264_prepare_answer(cnf, remote_sdp, str_local_sdp, 8192);
218     if (i!=0)
219       {
220         fprintf(stderr, "Cannot Prepare local SDP answer from offer.\n");
221         osip_rfc3264_free(cnf);
222         fclose (torture_file);
223         fprintf (stdout, "test %s : ============================ FAILED\n", argv[2]);
224         return -1;
225       }
226     sdp_message_init(&local_sdp);
227     i=sdp_message_parse(local_sdp, str_local_sdp);
228     if (i!=0)
229       {
230         fprintf(stderr, "Cannot Parse uncomplete SDP answer from offer.\n");
231         sdp_message_free(local_sdp);
232         osip_rfc3264_free(cnf);
233         fclose (torture_file);
234         fprintf (stdout, "test %s : ============================ FAILED\n", argv[2]);
235         return -1;
236       }
237
238     mline=0;
239     while (0==osip_rfc3264_match(cnf, remote_sdp,
240                                  audio_tab, video_tab, t38_tab, app_tab, mline))
241       {
242         int pos;
243
244         if (audio_tab[0]==NULL && video_tab[0]==NULL && t38_tab[0]==NULL && app_tab[0]==NULL)
245           {
246             if (verbose)
247               fprintf(stdout, "The remote SDP does not match any local payloads.\n");
248           }
249         else
250           {
251             for (pos=0;audio_tab[pos]!=NULL;pos++)
252               {
253                 int pos2 = 0;
254                 sdp_media_t *med = audio_tab[pos];
255                 char *str = (char *) osip_list_get (med->m_payloads, 0);
256                 if (verbose==2)
257                   fprintf(stdout, "\tm=%s %s %s %s\n",
258                           med->m_media,
259                           med->m_port,
260                           med->m_proto,
261                           str);
262                 while (!osip_list_eol (med->a_attributes, pos2))
263                   {
264                     sdp_attribute_t *attr =
265                       (sdp_attribute_t *) osip_list_get (med->a_attributes, pos2);
266                     if (verbose==2)
267                       fprintf(stdout, "\ta=%s:%s\n",
268                               attr->a_att_field,
269                               attr->a_att_value);
270                     pos2++;
271                   }
272                 if (verbose==2)
273                   fprintf(stdout, "\n");
274
275                 i=osip_rfc3264_complete_answer(cnf, remote_sdp, local_sdp, audio_tab[pos], mline);
276                 if (i!=0)
277                   {
278                     if (verbose)
279                       fprintf(stdout, "Error Adding support for codec in answer?\n");
280                   }
281                 else
282                   {
283                     if (verbose==2)
284                       fprintf(stdout, "support for codec added in answer:\n");
285                   }
286               }
287             
288             for (pos=0;video_tab[pos]!=NULL;pos++)
289               {
290                 int pos2 = 0;
291                 sdp_media_t *med = video_tab[pos];
292                 char *str = (char *) osip_list_get (med->m_payloads, 0);
293                 if (verbose==2)
294                   fprintf(stdout, "\tm=%s %s %s %s\n",
295                           med->m_media,
296                           med->m_port,
297                           med->m_proto,
298                           str);
299                 while (!osip_list_eol (med->a_attributes, pos2))
300                   {
301                     sdp_attribute_t *attr =
302                       (sdp_attribute_t *) osip_list_get (med->a_attributes, pos2);
303                     if (verbose==2)
304                       fprintf(stdout, "\ta=%s:%s\n",
305                               attr->a_att_field,
306                               attr->a_att_value);
307                     pos2++;
308                   }
309                 if (verbose==2)
310                   fprintf(stdout, "\n");
311
312                 i=osip_rfc3264_complete_answer(cnf, remote_sdp, local_sdp, video_tab[pos], mline);
313                 if (i!=0)
314                   {
315                     if (verbose)
316                       fprintf(stdout, "Error Adding support for codec in answer?\n");
317                   }
318                 else
319                   {
320                     if (verbose==2)
321                       fprintf(stdout, "support for codec added in answer:\n");
322                   }
323               }     
324           }
325
326         mline++;
327       }
328
329     if (verbose)
330       fprintf(stdout, "Result in answer:\n");
331     sdp_message_to_str(local_sdp, &tmp);
332     if (tmp!=NULL)
333       {
334         if (verbose)
335           fprintf(stdout, "\n%s\n", tmp);
336         free(tmp);
337       }
338     else
339       {
340         if (verbose)
341           fprintf(stdout, "ERROR\n"); 
342       }
343   }
344
345   osip_rfc3264_free(cnf);
346   fclose (torture_file);
347   fprintf (stdout, "test %s : ============================ OK\n", argv[2]);
348   return 0;
349 }