00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #include <string.h>
00064 #include <unistd.h>
00065 #include <signal.h>
00066
00067 #include "gwlib/gwlib.h"
00068 #include "wap/wsp_headers.h"
00069 #include "wap/wsp_strings.h"
00070
00071
00072
00073 static void test_header_combine(void)
00074 {
00075 List *old;
00076 List *new;
00077 List *tmp;
00078
00079 old = http_create_empty_headers();
00080 new = http_create_empty_headers();
00081 tmp = http_create_empty_headers();
00082
00083 http_header_add(old, "Accept", "text/html");
00084 http_header_add(old, "Accept", "text/plain");
00085 http_header_add(old, "Accept-Language", "en");
00086 http_header_add(old, "Accept", "image/jpeg");
00087
00088 http_header_combine(tmp, old);
00089 if (gwlist_len(tmp) != 4) {
00090 error(0, "http_combine_header with an empty 'old' did not just append.");
00091 }
00092
00093 http_header_combine(old, new);
00094 if (gwlist_len(old) != 4) {
00095 error(0, "http_combine_header with an empty 'new' changed 'old'.");
00096 }
00097
00098 http_header_add(new, "Accept", "text/html");
00099 http_header_add(new, "Accept", "text/plain");
00100
00101 http_header_combine(old, new);
00102 if (gwlist_len(old) != 3 ||
00103 octstr_compare(gwlist_get(old, 0),
00104 octstr_imm("Accept-Language: en")) != 0 ||
00105 octstr_compare(gwlist_get(old, 1),
00106 octstr_imm("Accept: text/html")) != 0 ||
00107 octstr_compare(gwlist_get(old, 2),
00108 octstr_imm("Accept: text/plain")) != 0) {
00109 error(0, "http_header_combine failed.");
00110 }
00111
00112 http_destroy_headers(old);
00113 http_destroy_headers(new);
00114 http_destroy_headers(tmp);
00115 }
00116
00117
00118 static void split_headers(Octstr *headers, List **split, List **expected)
00119 {
00120 long start;
00121 long pos;
00122
00123 *split = gwlist_create();
00124 *expected = gwlist_create();
00125 start = 0;
00126 for (pos = 0; pos < octstr_len(headers); pos++) {
00127 if (octstr_get_char(headers, pos) == '\n') {
00128 int c;
00129 Octstr *line;
00130
00131 if (pos == start) {
00132
00133 start = pos + 1;
00134 continue;
00135 }
00136
00137 line = octstr_copy(headers, start, pos - start);
00138 start = pos + 1;
00139
00140 c = octstr_get_char(line, 0);
00141 octstr_delete(line, 0, 2);
00142 if (c == '|') {
00143 gwlist_append(*split, line);
00144 gwlist_append(*expected, octstr_duplicate(line));
00145 } else if (c == '<') {
00146 gwlist_append(*split, line);
00147 } else if (c == '>') {
00148 gwlist_append(*expected, line);
00149 } else if (c == '#') {
00150
00151 octstr_destroy(line);
00152 } else {
00153 warning(0, "Bad line in test headers file");
00154 octstr_destroy(line);
00155 }
00156 }
00157 }
00158 }
00159
00160 int main(int argc, char **argv)
00161 {
00162 Octstr *headers;
00163 List *expected;
00164 List *split;
00165 Octstr *packed;
00166 List *unpacked;
00167 Octstr *filename;
00168 long i;
00169 int mptr;
00170
00171 gwlib_init();
00172 wsp_strings_init();
00173
00174 mptr = get_and_set_debugs(argc, argv, NULL);
00175 if (argc - mptr <= 0)
00176 panic(0, "Usage: test_headers [options] header-file");
00177
00178 filename = octstr_create(argv[mptr]);
00179 headers = octstr_read_file(octstr_get_cstr(filename));
00180 split_headers(headers, &split, &expected);
00181 packed = wsp_headers_pack(split, 0, WSP_1_2);
00182 unpacked = wsp_headers_unpack(packed, 0);
00183
00184 if (gwlist_len(unpacked) != gwlist_len(expected)) {
00185 error(0, "Expected %ld headers, generated %ld.\n",
00186 gwlist_len(expected), gwlist_len(unpacked));
00187 } else {
00188 for (i = 0; i < gwlist_len(unpacked); i++) {
00189 Octstr *got, *exp;
00190 got = gwlist_get(unpacked, i);
00191 exp = gwlist_get(expected, i);
00192 if (octstr_compare(got, exp) != 0) {
00193 error(0, "Exp: %s", octstr_get_cstr(exp));
00194 error(0, "Got: %s", octstr_get_cstr(got));
00195 }
00196 }
00197 }
00198
00199 test_header_combine();
00200
00201 octstr_destroy(headers);
00202 octstr_destroy(filename);
00203 gwlist_destroy(split, octstr_destroy_item);
00204 gwlist_destroy(expected, octstr_destroy_item);
00205 octstr_destroy(packed);
00206 gwlist_destroy(unpacked, octstr_destroy_item);
00207
00208 wsp_strings_shutdown();
00209 gwlib_shutdown();
00210 return 0;
00211 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.