Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_headers.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * test_headers.c - test wsp header packing and unpacking.
59  *
60  * Richard Braakman <dark@wapit.com>
61  */
62 
63 #include <string.h>
64 #include <unistd.h>
65 #include <signal.h>
66 
67 #include "gwlib/gwlib.h"
68 #include "wap/wsp_headers.h"
69 #include "wap/wsp_strings.h"
70 
71 
72 /* Test the http_header_combine function. */
73 static void test_header_combine(void)
74 {
75  List *old;
76  List *new;
77  List *tmp;
78 
82 
83  http_header_add(old, "Accept", "text/html");
84  http_header_add(old, "Accept", "text/plain");
85  http_header_add(old, "Accept-Language", "en");
86  http_header_add(old, "Accept", "image/jpeg");
87 
88  http_header_combine(tmp, old);
89  if (gwlist_len(tmp) != 4) {
90  error(0, "http_combine_header with an empty 'old' did not just append.");
91  }
92 
93  http_header_combine(old, new);
94  if (gwlist_len(old) != 4) {
95  error(0, "http_combine_header with an empty 'new' changed 'old'.");
96  }
97 
98  http_header_add(new, "Accept", "text/html");
99  http_header_add(new, "Accept", "text/plain");
100 
101  http_header_combine(old, new);
102  if (gwlist_len(old) != 3 ||
103  octstr_compare(gwlist_get(old, 0),
104  octstr_imm("Accept-Language: en")) != 0 ||
105  octstr_compare(gwlist_get(old, 1),
106  octstr_imm("Accept: text/html")) != 0 ||
107  octstr_compare(gwlist_get(old, 2),
108  octstr_imm("Accept: text/plain")) != 0) {
109  error(0, "http_header_combine failed.");
110  }
111 
115 }
116 
117 
118 static void split_headers(Octstr *headers, List **split, List **expected)
119 {
120  long start;
121  long pos;
122 
123  *split = gwlist_create();
124  *expected = gwlist_create();
125  start = 0;
126  for (pos = 0; pos < octstr_len(headers); pos++) {
127  if (octstr_get_char(headers, pos) == '\n') {
128  int c;
129  Octstr *line;
130 
131  if (pos == start) {
132  /* Skip empty lines */
133  start = pos + 1;
134  continue;
135  }
136 
137  line = octstr_copy(headers, start, pos - start);
138  start = pos + 1;
139 
140  c = octstr_get_char(line, 0);
141  octstr_delete(line, 0, 2);
142  if (c == '|') {
143  gwlist_append(*split, line);
144  gwlist_append(*expected, octstr_duplicate(line));
145  } else if (c == '<') {
146  gwlist_append(*split, line);
147  } else if (c == '>') {
148  gwlist_append(*expected, line);
149  } else if (c == '#') {
150  /* comment */
151  octstr_destroy(line);
152  } else {
153  warning(0, "Bad line in test headers file");
154  octstr_destroy(line);
155  }
156  }
157  }
158 }
159 
160 int main(int argc, char **argv)
161 {
162  Octstr *headers;
163  List *expected;
164  List *split;
165  Octstr *packed;
166  List *unpacked;
167  Octstr *filename;
168  long i;
169  int mptr;
170 
171  gwlib_init();
173 
174  mptr = get_and_set_debugs(argc, argv, NULL);
175  if (argc - mptr <= 0)
176  panic(0, "Usage: test_headers [options] header-file");
177 
178  filename = octstr_create(argv[mptr]);
179  headers = octstr_read_file(octstr_get_cstr(filename));
180  split_headers(headers, &split, &expected);
181  packed = wsp_headers_pack(split, 0, WSP_1_2);
182  unpacked = wsp_headers_unpack(packed, 0);
183 
184  if (gwlist_len(unpacked) != gwlist_len(expected)) {
185  error(0, "Expected %ld headers, generated %ld.\n",
186  gwlist_len(expected), gwlist_len(unpacked));
187  } else {
188  for (i = 0; i < gwlist_len(unpacked); i++) {
189  Octstr *got, *exp;
190  got = gwlist_get(unpacked, i);
191  exp = gwlist_get(expected, i);
192  if (octstr_compare(got, exp) != 0) {
193  error(0, "Exp: %s", octstr_get_cstr(exp));
194  error(0, "Got: %s", octstr_get_cstr(got));
195  }
196  }
197  }
198 
200 
201  octstr_destroy(headers);
202  octstr_destroy(filename);
205  octstr_destroy(packed);
207 
209  gwlib_shutdown();
210  return 0;
211 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
static void test_header_combine(void)
Definition: test_headers.c:73
void gwlist_append(List *list, void *item)
Definition: list.c:179
Octstr * wsp_headers_pack(List *headers, int separate_content_type, int wsp_version)
Definition: wsp_headers.c:2963
long gwlist_len(List *list)
Definition: list.c:166
static void split_headers(Octstr *headers, List **split, List **expected)
Definition: test_headers.c:118
void * gwlist_get(List *list, long pos)
Definition: list.c:292
void http_header_combine(List *old_headers, List *new_headers)
Definition: http.c:3045
int main(int argc, char **argv)
Definition: test_headers.c:160
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
void http_destroy_headers(List *headers)
Definition: http.c:2856
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
List * http_create_empty_headers(void)
Definition: http.c:2849
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
char filename[FILENAME_MAX+1]
Definition: log.c:135
#define octstr_create(cstr)
Definition: octstr.h:125
void octstr_destroy_item(void *os)
Definition: octstr.c:334
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1546
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void wsp_strings_shutdown(void)
Definition: wsp_strings.c:286
void wsp_strings_init(void)
Definition: wsp_strings.c:269
List * wsp_headers_unpack(Octstr *headers, int content_type_present)
Definition: wsp_headers.c:1331
#define panic
Definition: log.h:87
static List * split
Definition: test_http.c:88
void gwlib_shutdown(void)
Definition: gwlib.c:94
#define gwlist_create()
Definition: list.h:136
void gwlib_init(void)
Definition: gwlib.c:78
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
int get_and_set_debugs(int argc, char **argv, int(*find_own)(int index, int argc, char **argv))
Definition: utils.c:626
Definition: wsp.h:72
Definition: list.c:102
static int start
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.