| 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 3 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, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU 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, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "test.h" |
| 19 | |
| 20 | static void test_kvp(void) { |
| 21 | struct kvp *k; |
| 22 | size_t n; |
| 23 | |
| 24 | /* decoding */ |
| 25 | #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S)) |
| 26 | fprintf(stderr, "5 ERROR reports expected {\n"); |
| 27 | insist(KVP_URLDECODE("=%zz") == 0); |
| 28 | insist(KVP_URLDECODE("=%0") == 0); |
| 29 | insist(KVP_URLDECODE("=%0z") == 0); |
| 30 | insist(KVP_URLDECODE("=%%") == 0); |
| 31 | insist(KVP_URLDECODE("==%") == 0); |
| 32 | fprintf(stderr, "}\n"); |
| 33 | insist(KVP_URLDECODE("wibble") == 0); |
| 34 | insist(KVP_URLDECODE("") == 0); |
| 35 | insist(KVP_URLDECODE("wibble&") == 0); |
| 36 | insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0); |
| 37 | check_string(kvp_get(k, "one"), "blat foo"); |
| 38 | insist(kvp_get(k, "ONE") == 0); |
| 39 | insist(k->next == 0); |
| 40 | insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0); |
| 41 | check_string(kvp_get(k, "wibble"), "splat"); |
| 42 | check_string(kvp_get(k, "bar"), "spong"); |
| 43 | insist(kvp_get(k, "ONE") == 0); |
| 44 | insist(k->next->next == 0); |
| 45 | /* encoding */ |
| 46 | insist(kvp_set(&k, "bar", "spong") == 0); |
| 47 | insist(kvp_set(&k, "bar", "foo") == 1); |
| 48 | insist(kvp_set(&k, "zog", "%") == 1); |
| 49 | insist(kvp_set(&k, "wibble", 0) == 1); |
| 50 | insist(kvp_set(&k, "wibble", 0) == 0); |
| 51 | check_string(kvp_urlencode(k, 0), |
| 52 | "bar=foo&zog=%25"); |
| 53 | check_string(kvp_urlencode(k, &n), |
| 54 | "bar=foo&zog=%25"); |
| 55 | insist(n == strlen("bar=foo&zog=%25")); |
| 56 | check_string(urlencodestring("abc% +\n"), |
| 57 | "abc%25%20%2b%0a"); |
| 58 | check_integer(urldecode(sink_error(), "bar=foo", 7), -1); |
| 59 | check_integer(urlencode(sink_error(), "wibble", 7), -1); |
| 60 | check_integer(urlencode(sink_error(), " ", 1), -1); |
| 61 | k = kvp_make("wibble", "spong", |
| 62 | "blit", "blat", |
| 63 | (char *)0); |
| 64 | check_string(kvp_urlencode(k, &n), |
| 65 | "blit=blat&wibble=spong"); |
| 66 | k = kvp_make("wibble", (char *)0, |
| 67 | "blit", "blat", |
| 68 | (char *)0); |
| 69 | check_string(kvp_urlencode(k, &n), |
| 70 | "blit=blat&wibble="); |
| 71 | } |
| 72 | |
| 73 | TEST(kvp); |
| 74 | |
| 75 | /* |
| 76 | Local Variables: |
| 77 | c-basic-offset:2 |
| 78 | comment-column:40 |
| 79 | fill-column:79 |
| 80 | indent-tabs-mode:nil |
| 81 | End: |
| 82 | */ |