Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

test_headers.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * test_headers.c - test wsp header packing and unpacking.
00059  *
00060  * Richard Braakman <dark@wapit.com>
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 /* Test the http_header_combine function. */
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                 /* Skip empty lines */
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                 /* comment */
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.