Added SILC Server library.
[silc.git] / lib / silccore / tests / test_silcargument.c
1 /*
2
3   test_silcargument.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /* Tests:
21    silc_argument_payload_parse
22    silc_argument_payload_encode
23    silc_argument_payload_encode_one
24    silc_argument_payload_encode_payload
25    silc_argument_payload_free
26    silc_argument_get_arg_num
27    silc_argument_get_arg_first_arg
28    silc_argument_get_arg_next_arg
29    silc_argument_get_arg_type
30 */
31
32 #include "silc.h"
33 #include "silcargument.h"
34
35 #define ARG_NUM 250
36
37 int main(int argc, char **argv)
38 {
39   SilcArgumentPayload payload;
40   SilcBuffer args, args2;
41   char arg[ARG_NUM + 2];
42   int i;
43   unsigned char **argvv, *a;
44   SilcUInt32 *argvv_lens, l;
45   SilcUInt32 *argvv_types, t;
46   SilcBool success = FALSE;
47
48   if (argc > 1 && !strcmp(argv[1], "-d")) {
49     silc_debug = 1;
50     silc_debug_hexdump = 1;
51     silc_log_set_debug_string("*argument*");
52   }
53
54   SILC_LOG_DEBUG(("Encoding %d arguments", ARG_NUM));
55   argvv = silc_calloc(ARG_NUM, sizeof(unsigned char *));
56   argvv_lens = silc_calloc(ARG_NUM, sizeof(SilcUInt32));
57   argvv_types = silc_calloc(ARG_NUM, sizeof(SilcUInt32));
58   for (i = 0; i < ARG_NUM; i++) {
59     memset(arg, 0, sizeof(arg));
60     memset(arg, 'a', i + 1);
61
62     SILC_LOG_DEBUG(("Argument %d, len %d, type %d", i + 1,
63                    strlen(arg), i + 1));
64     SILC_LOG_HEXDUMP(("Argument data"), arg, strlen(arg));
65
66     argvv[i] = silc_memdup(arg, strlen(arg));
67     argvv_lens[i] = strlen(arg);
68     argvv_types[i] = i + 1;
69   }
70   args = silc_argument_payload_encode(ARG_NUM, argvv, argvv_lens, argvv_types);
71   if (!args)
72     goto out;
73   SILC_LOG_DEBUG(("Encoding was successful"));
74
75
76   SILC_LOG_DEBUG(("Adding one extra argument"));
77   memset(arg, 0, sizeof(arg));
78   memset(arg, 'a', ARG_NUM + 1);
79   SILC_LOG_DEBUG(("Argument %d, len %d, type %d", ARG_NUM + 1,
80                  strlen(arg), ARG_NUM + 1));
81   SILC_LOG_HEXDUMP(("Argument data"), arg, strlen(arg));
82   args = silc_argument_payload_encode_one(args, arg, strlen(arg), 
83                                           ARG_NUM + 1);
84   if (!args)
85     goto out;
86   SILC_LOG_DEBUG(("Adding one argument was successful"));
87
88   SILC_LOG_HEXDUMP(("Encoded payload"), args->data, args->len);
89
90
91   SILC_LOG_DEBUG(("Parsing the encoded payload"));
92   payload = silc_argument_payload_parse(args->data, args->len, ARG_NUM + 1);
93   if (!payload)
94     goto out;
95   SILC_LOG_DEBUG(("Parsing was successful"));
96
97
98   SILC_LOG_DEBUG(("Re-encoding the parsed payload"));
99   args2 = silc_argument_payload_encode_payload(payload);
100   if (!args2)
101     goto out;
102   if (args2->len != args->len ||
103       memcmp(args2->data, args->data, args->len)) {
104     SILC_LOG_DEBUG(("Re-encoding failed"));
105     goto out;
106   }
107   silc_buffer_free(args2);
108   SILC_LOG_DEBUG(("Re-encoding was successful"));
109
110
111   SILC_LOG_DEBUG(("Checking number of arguments"));
112   SILC_LOG_DEBUG(("Number of arguments: %d (expecting %d)",
113                  silc_argument_get_arg_num(payload), ARG_NUM + 1));
114   if (silc_argument_get_arg_num(payload) != ARG_NUM + 1)
115     goto out;
116
117
118   SILC_LOG_DEBUG(("Traversing the parsed arguments"));
119   i = 0;
120   a = silc_argument_get_first_arg(payload, &t, &l);
121   if (!a || t != argvv_types[0] || l != argvv_lens[0] ||
122       memcmp(a, argvv[0], l)) {
123     SILC_LOG_DEBUG(("First argument failed"));
124     goto out;
125   }
126   while (a) {
127     if (i + 1 == ARG_NUM + 1) {
128       SILC_LOG_DEBUG(("Argument %d, len %d (expected %d), "
129                       "type %d (expected %d)", i + 1, l, strlen(arg),
130                       t, ARG_NUM + 1));
131       if (!a || t != ARG_NUM + 1 || l != strlen(arg) ||
132           memcmp(a, arg, l)) {
133         SILC_LOG_DEBUG(("Argument %d failed", ARG_NUM + 1));
134         goto out;
135       }
136     } else {
137       SILC_LOG_DEBUG(("Argument %d, len %d (expected %d), "
138                       "type %d (expected %d)", i + 1, l, argvv_lens[i],
139                       t, argvv_types[i]));
140       if (!a || t != argvv_types[i] || l != argvv_lens[i] ||
141           memcmp(a, argvv[i], l)) {
142         SILC_LOG_DEBUG(("Argument %d failed", i + 1));
143         goto out;
144       }
145     }
146     a = silc_argument_get_next_arg(payload, &t, &l);
147     i++;
148   }
149   if (i != ARG_NUM + 1) {
150     SILC_LOG_DEBUG(("All arguments was not parsed, missing %d args",
151                     ARG_NUM + 1 - i));
152     goto out;
153   }
154   SILC_LOG_DEBUG(("Traversing successful"));
155
156
157   SILC_LOG_DEBUG(("Traversing arguments by type"));
158   for (i = 0; i < ARG_NUM + 1; i++) {
159     a = silc_argument_get_arg_type(payload, i + 1, &l);
160     if (i + 1 == ARG_NUM + 1) {
161       if (!a || t != ARG_NUM + 1 || l != strlen(arg) ||
162           memcmp(a, arg, l)) {
163         SILC_LOG_DEBUG(("Argument %d failed", ARG_NUM + 1));
164         goto out;
165       }
166     } else {
167       if (!a || l != argvv_lens[i] || memcmp(a, argvv[i], l)) {
168         SILC_LOG_DEBUG(("Argument %d failed", i + 1));
169         goto out;
170       }
171     }
172   }
173   SILC_LOG_DEBUG(("Traversing successful"));
174
175   success = TRUE;
176
177  out:
178   silc_argument_payload_free(payload);
179   for (i = 0; i < ARG_NUM; i++)
180     silc_free(argvv[i]);
181   silc_free(argvv);
182   silc_free(argvv_lens);
183   silc_free(argvv_types);
184   silc_buffer_free(args);
185
186   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
187   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
188
189   exit(success);
190 }