chiark / gitweb /
d46335093aceaf8aadc3c59feb8b2049bd616bcc
[disorder] / lib / t-kvp.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include "test.h"
21
22 static void test_kvp(void) {
23   struct kvp *k;
24   size_t n;
25   
26   /* decoding */
27 #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
28   fprintf(stderr, "5 ERROR reports expected {\n");
29   insist(KVP_URLDECODE("=%zz") == 0);
30   insist(KVP_URLDECODE("=%0") == 0);
31   insist(KVP_URLDECODE("=%0z") == 0);
32   insist(KVP_URLDECODE("=%%") == 0);
33   insist(KVP_URLDECODE("==%") == 0);
34   fprintf(stderr, "}\n");
35   insist(KVP_URLDECODE("wibble") == 0);
36   insist(KVP_URLDECODE("") == 0);
37   insist(KVP_URLDECODE("wibble&") == 0);
38   insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
39   check_string(kvp_get(k, "one"), "blat foo");
40   insist(kvp_get(k, "ONE") == 0);
41   insist(k->next == 0);
42   insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
43   check_string(kvp_get(k, "wibble"), "splat");
44   check_string(kvp_get(k, "bar"), "spong");
45   insist(kvp_get(k, "ONE") == 0);
46   insist(k->next->next == 0);
47   /* encoding */
48   insist(kvp_set(&k, "bar", "spong") == 0);
49   insist(kvp_set(&k, "bar", "foo") == 1);
50   insist(kvp_set(&k, "zog", "%") == 1);
51   insist(kvp_set(&k, "wibble", 0) == 1);
52   insist(kvp_set(&k, "wibble", 0) == 0);
53   check_string(kvp_urlencode(k, 0),
54                "bar=foo&zog=%25");
55   check_string(kvp_urlencode(k, &n),
56                "bar=foo&zog=%25");
57   insist(n == strlen("bar=foo&zog=%25"));
58   check_string(urlencodestring("abc% +\n"),
59                "abc%25%20%2b%0a");
60   check_integer(urldecode(sink_error(), "bar=foo", 7), -1);
61   check_integer(urlencode(sink_error(), "wibble", 7), -1);
62   check_integer(urlencode(sink_error(), " ", 1), -1);
63 }
64
65 TEST(kvp);
66
67 /*
68 Local Variables:
69 c-basic-offset:2
70 comment-column:40
71 fill-column:79
72 indent-tabs-mode:nil
73 End:
74 */