OSDN Git Service

implement x-www-form-urlencoded encoder.
[bbk/bchanf.git] / src / coding / test_htmlform_urlencoder.c
1 /*
2  * test_htmlform_urlencoder.c
3  *
4  * Copyright (c) 2015 project bchan
5  *
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.
9  *
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:
13  *
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.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include    "test_coding.h"
28
29 #include    "htmlform_urlencoder.h"
30
31 #include    <btron/btron.h>
32 #include        <bstdio.h>
33 #include        <bstring.h>
34 #include        <bstdlib.h>
35
36 #include    <unittest_driver.h>
37
38 typedef struct urlencoder_testdata_t_ {
39         htmlform_field *src;
40         W src_len;
41         UB *expected;
42         W expected_len;
43 } urlencoder_testdata_t;
44
45 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_common(urlencoder_testdata_t *testdata)
46 {
47         htmlform_urlencoder_t encoder;
48         Bool cont;
49         UB *str, *result;
50         W len, result_len;
51         UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
52
53         result = (UB*)malloc(sizeof(UB));
54         if (result == NULL) {
55                 return UNITTEST_RESULT_FAIL;
56         }
57         result[0] = '\0';
58         result_len = 0;
59
60         htmlform_urlencoder_initialize(&encoder, testdata->src, testdata->src_len);
61
62         for (;;) {
63                 cont = htmlform_urlencoder_next(&encoder, &str, &len);
64                 if (cont == False) {
65                         break;
66                 }
67                 result = realloc(result, result_len + len + 1);
68                 if (result == NULL) {
69                         ret = UNITTEST_RESULT_FAIL;
70                         break;
71                 }
72                 strncat(result, str, len);
73                 result_len += len;
74         }
75
76         htmlform_urlencoder_finalize(&encoder);
77
78         if (result != NULL) {
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;
82                 }
83                 if (strncmp(testdata->expected, result, result_len) != 0) {
84                         printf("encoded string failure: %s\n", result);
85                         ret = UNITTEST_RESULT_FAIL;
86                 }
87
88                 free(result);
89         }
90
91         return ret;
92 }
93
94 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_1()
95 {
96         htmlform_field src[] = {
97                 (htmlform_field){"aaa", 3, "bbb", 3},
98                 (htmlform_field){"cc", 2, "dd", 2},
99         };
100         W src_len = 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
105         };
106         return test_htmlform_urlencoder_common(&testdata);
107 }
108
109 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_2()
110 {
111         htmlform_field src[] = {
112                 (htmlform_field){"aaa", 3, "bb", 2},
113                 (htmlform_field){"cc", 2, "ddd", 3},
114         };
115         W src_len = 2;
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
120         };
121         return test_htmlform_urlencoder_common(&testdata);
122 }
123
124 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_3()
125 {
126         htmlform_field src[] = {
127                 (htmlform_field){"a a", 3, "b b", 3},
128         };
129         W src_len = 1;
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
134         };
135         return test_htmlform_urlencoder_common(&testdata);
136 }
137
138 LOCAL UNITTEST_RESULT test_htmlform_urlencoder_4()
139 {
140         htmlform_field src[] = {
141                 (htmlform_field){(UB[]){0xA0, 0xA1, 0xA2}, 3, (UB[]){0xB3, 0xB4, 0xB5}, 3},
142         };
143         W src_len = 1;
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
148         };
149         return test_htmlform_urlencoder_common(&testdata);
150 }
151
152 EXPORT VOID test_htmlform_urlencoder_main(unittest_driver_t *driver)
153 {
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);
158 }