Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_headers.c File Reference
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "gwlib/gwlib.h"
#include "wap/wsp_headers.h"
#include "wap/wsp_strings.h"

Go to the source code of this file.

Functions

static void test_header_combine (void)
 
static void split_headers (Octstr *headers, List **split, List **expected)
 
int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 160 of file test_headers.c.

References error(), filename, get_and_set_debugs(), gwlib_init(), gwlib_shutdown(), gwlist_destroy(), gwlist_get(), gwlist_len(), octstr_compare(), octstr_create, octstr_destroy(), octstr_destroy_item(), octstr_get_cstr, octstr_read_file(), panic, split, split_headers(), test_header_combine(), WSP_1_2, wsp_headers_pack(), wsp_headers_unpack(), wsp_strings_init(), and wsp_strings_shutdown().

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
static void test_header_combine(void)
Definition: test_headers.c:73
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
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
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
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
void gwlib_init(void)
Definition: gwlib.c:78
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
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
static void split_headers ( Octstr headers,
List **  split,
List **  expected 
)
static

Definition at line 118 of file test_headers.c.

References gwlist_append(), gwlist_create, octstr_copy, octstr_delete(), octstr_destroy(), octstr_duplicate, octstr_get_char(), octstr_len(), start, and warning().

Referenced by main().

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 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
#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
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
#define gwlist_create()
Definition: list.h:136
int octstr_get_char(const Octstr *ostr, long pos)
Definition: octstr.c:404
static int start
static void test_header_combine ( void  )
static

Definition at line 73 of file test_headers.c.

References error(), gwlist_get(), gwlist_len(), http_create_empty_headers(), http_destroy_headers(), http_header_add(), http_header_combine(), octstr_compare(), and octstr_imm().

Referenced by main().

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 }
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
long gwlist_len(List *list)
Definition: list.c:166
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
void http_destroy_headers(List *headers)
Definition: http.c:2856
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
List * http_create_empty_headers(void)
Definition: http.c:2849
Definition: list.c:102
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.