2 * test_htmlform_urlencoder.c
4 * Copyright (c) 2015 project bchan
6 * This software is provided 'as-is', without any express or implied
7 * warranty. In no event will the authors be held liable for any damages
8 * arising from the use of this software.
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
22 * 3. This notice may not be removed or altered from any source
27 #include "test_coding.h"
29 #include "htmlform_urlencoder.h"
31 #include <btron/btron.h>
36 #include <unittest_driver.h>
38 typedef struct urlencoder_testdata_t_ {
43 } urlencoder_testdata_t;
45 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_common(urlencoder_testdata_t *testdata)
47 htmlform_urlencoder_t encoder;
51 UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
53 result = (UB*)malloc(sizeof(UB));
55 return UNITTEST_RESULT_FAIL;
60 htmlform_urlencoder_initialize(&encoder, testdata->src, testdata->src_len);
63 cont = htmlform_urlencoder_next(&encoder, &str, &len);
67 result = realloc(result, result_len + len + 1);
69 ret = UNITTEST_RESULT_FAIL;
72 strncat(result, str, len);
76 htmlform_urlencoder_finalize(&encoder);
79 if (testdata->expected_len != result_len) {
80 printf("length failure: expected = %d, result = %d\n", testdata->expected_len, result_len);
81 ret = UNITTEST_RESULT_FAIL;
83 if (strncmp(testdata->expected, result, result_len) != 0) {
84 printf("encoded string failure: %s\n", result);
85 ret = UNITTEST_RESULT_FAIL;
94 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_1()
96 htmlform_field src[] = {
97 (htmlform_field){"aaa", 3, "bbb", 3},
98 (htmlform_field){"cc", 2, "dd", 2},
101 UB expected[] = "aaa=bbb&cc=dd";
102 W expected_len = strlen(expected);
103 urlencoder_testdata_t testdata = {
104 src, src_len, expected, expected_len
106 return test_htmlform_urlencoder_common(&testdata);
109 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_2()
111 htmlform_field src[] = {
112 (htmlform_field){"aaa", 3, "bb", 2},
113 (htmlform_field){"cc", 2, "ddd", 3},
116 UB expected[] = "aaa=bb&cc=ddd";
117 W expected_len = strlen(expected);
118 urlencoder_testdata_t testdata = {
119 src, src_len, expected, expected_len
121 return test_htmlform_urlencoder_common(&testdata);
124 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_3()
126 htmlform_field src[] = {
127 (htmlform_field){"a a", 3, "b b", 3},
130 UB expected[] = "a+a=b+b";
131 W expected_len = strlen(expected);
132 urlencoder_testdata_t testdata = {
133 src, src_len, expected, expected_len
135 return test_htmlform_urlencoder_common(&testdata);
138 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_4()
140 htmlform_field src[] = {
141 (htmlform_field){(UB[]){0xA0, 0xA1, 0xA2}, 3, (UB[]){0xB3, 0xB4, 0xB5}, 3},
144 UB expected[] = "%A0%A1%A2=%B3%B4%B5";
145 W expected_len = strlen(expected);
146 urlencoder_testdata_t testdata = {
147 src, src_len, expected, expected_len
149 return test_htmlform_urlencoder_common(&testdata);
152 EXPORT VOID test_htmlform_urlencoder_main(unittest_driver_t *driver)
154 UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_1);
155 UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_2);
156 UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_3);
157 UNITTEST_DRIVER_REGIST(driver, test_htmlform_urlencoder_4);