and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / test / torture.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_parser.h>
29 #include <osip2/internal.h>
30
31 int test_message (char *msg, int verbose, int clone);
32 static void usage (void);
33
34 static void
35 usage ()
36 {
37   fprintf (stderr, "Usage: ./torture_test torture_file number [-v] [-c]\n");
38   exit (1);
39 }
40
41 int
42 main (int argc, char **argv)
43 {
44   int i;
45   int verbose = 0;              /* 1: verbose, 0 (or nothing: not verbose) */
46   int clone = 0;                /* 1: verbose, 0 (or nothing: not verbose) */
47   char *marker;
48   FILE *torture_file;
49   char *tmp;
50   char *msg;
51   char *tmpmsg;
52   static int num_test = 0;
53
54   if (argc > 3)
55     {
56       if (0 == strncmp (argv[3], "-v", 2))
57         verbose = 1;
58       else if (0 == strncmp (argv[3], "-c", 2))
59         clone = 1;
60       else
61         usage ();
62     }
63
64   if (argc > 4)
65     {
66       if (0 == strncmp (argv[4], "-v", 2))
67         verbose = 1;
68       else if (0 == strncmp (argv[4], "-c", 2))
69         clone = 1;
70       else
71         usage ();
72     }
73
74   if (argc < 3)
75     {
76       usage ();
77     }
78
79   torture_file = fopen (argv[1], "r");
80   if (torture_file == NULL)
81     {
82       usage ();
83     }
84
85   /* initialize parser */
86   parser_init ();
87
88   i = 0;
89   tmp = (char *) osip_malloc (500);
90   marker = fgets (tmp, 500, torture_file);      /* lines are under 500 */
91   while (marker != NULL && i < atoi (argv[2]))
92     {
93       if (0 == strncmp (tmp, "|", 1))
94         i++;
95       marker = fgets (tmp, 500, torture_file);
96     }
97   num_test++;
98
99   msg = (char *) osip_malloc (50000);   /* msg are under 10000 */
100   if (msg == NULL)
101     {
102       fprintf (stderr, "Error! osip_malloc failed\n");
103       return -1;
104     }
105   tmpmsg = msg;
106
107   if (marker == NULL)
108     {
109       fprintf (stderr,
110                "Error! The message's number you specified does not exist\n");
111       exit (1);                 /* end of file detected! */
112     }
113   /* this part reads an entire message, separator is "|" */
114   /* (it is unlinkely that it will appear in messages!) */
115   while (marker != NULL && strncmp (tmp, "|", 1))
116     {
117       osip_strncpy (tmpmsg, tmp, strlen (tmp));
118       tmpmsg = tmpmsg + strlen (tmp);
119       marker = fgets (tmp, 500, torture_file);
120     }
121
122   if (verbose)
123     {
124       fprintf (stdout, "test %s : ============================ \n", argv[2]);
125       fprintf (stdout, "%s", msg);
126
127       if (0 == test_message (msg, verbose, clone))
128         fprintf (stdout, "test %s : ============================ OK\n",
129                  argv[2]);
130       else
131         fprintf (stdout, "test %s : ============================ FAILED\n",
132                  argv[2]);
133     }
134   else
135     {
136       if (0 == test_message (msg, verbose, clone))
137         fprintf (stdout, "test %s : OK\n", argv[2]);
138       else
139         fprintf (stdout, "test %s : FAILED\n", argv[2]);
140     }
141
142   osip_free (msg);
143   osip_free (tmp);
144   fclose (torture_file);
145
146   return 0;
147 }
148
149 int
150 test_message (char *msg, int verbose, int clone)
151 {
152   osip_message_t *sip;
153
154   {
155     char *result;
156
157     /* int j=10000; */
158     int j = 1;
159
160     fprintf (stdout,
161              "Trying %i sequentials calls to osip_message_init(), osip_message_parse() and osip_message_free()\n",
162              j);
163     while (j != 0)
164       {
165         j--;
166         osip_message_init (&sip);
167         if (osip_message_parse (sip, msg) != 0)
168           {
169             fprintf (stdout, "ERROR: failed while parsing!\n");
170             osip_message_free (sip);    /* try to free msg, even if it failed! */
171             return -1;
172           }
173         osip_message_free (sip);        /* try to free msg, even if it failed! */
174       }
175
176     osip_message_init (&sip);
177     if (osip_message_parse (sip, msg) != 0)
178       {
179         fprintf (stdout, "ERROR: failed while parsing!\n");
180         osip_message_free (sip);        /* try to free msg, even if it failed! */
181         /* this seems dangerous..... */
182         return -1;
183       }
184     else
185       {
186         int i;
187
188         i = osip_message_to_str (sip, &result);
189         if (i == -1)
190           {
191             fprintf (stdout, "ERROR: failed while printing message!\n");
192             osip_message_free (sip);
193             return -1;
194           }
195         else
196           {
197             if (verbose)
198               fprintf (stdout, "%s", result);
199             if (clone)
200               {
201                 /* create a clone of message */
202                 /* int j = 10000; */
203                 int j = 1;
204
205                 fprintf (stdout,
206                          "Trying %i sequentials calls to osip_message_clone() and osip_message_free()\n",
207                          j);
208                 while (j != 0)
209                   {
210                     osip_message_t *copy;
211
212                     j--;
213                     i = osip_message_clone (sip, &copy);
214                     if (i != 0)
215                       {
216                         fprintf (stdout,
217                                  "ERROR: failed while creating copy of message!\n");
218                       }
219                     else
220                       {
221                         char *tmp;
222
223                         osip_message_force_update (copy);
224                         i = osip_message_to_str (copy, &tmp);
225                         if (i != 0)
226                           {
227                             fprintf (stdout,
228                                      "ERROR: failed while printing message!\n");
229                           }
230                         else
231                           {
232                             if (0 == strcmp (result, tmp))
233                               {
234                                 if (verbose)
235                                   printf
236                                     ("The osip_message_clone method works perfectly\n");
237                               }
238                             else
239                               printf
240                                 ("ERROR: The osip_message_clone method DOES NOT works\n");
241                             if (verbose)
242                               printf ("Here is the copy: \n%s\n", tmp);
243
244                             osip_free (tmp);
245                           }
246                         osip_message_free (copy);
247                       }
248                   }
249                 fprintf (stdout, "sequentials calls: done\n");
250               }
251             osip_free (result);
252           }
253         osip_message_free (sip);
254       }
255   }
256   return 0;
257 }