1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Exercise putk, printk, and getchark |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1989-2010. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions |
---|
11 | * are met: |
---|
12 | * 1. Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | * POSSIBILITY OF SUCH DAMAGE. |
---|
29 | */ |
---|
30 | |
---|
31 | #ifdef HAVE_CONFIG_H |
---|
32 | #include "config.h" |
---|
33 | #endif |
---|
34 | |
---|
35 | #include <rtems/base64.h> |
---|
36 | #include <rtems/dev/io.h> |
---|
37 | |
---|
38 | /* |
---|
39 | * Undefined the RTEMS_PRINTFLIKE and make it nothing. The test code |
---|
40 | * contained in the file is suppose to be wrong. |
---|
41 | */ |
---|
42 | #undef RTEMS_PRINTFLIKE |
---|
43 | #define RTEMS_PRINTFLIKE(_a, _b) |
---|
44 | |
---|
45 | #include <rtems/bspIo.h> |
---|
46 | #include <tmacros.h> |
---|
47 | |
---|
48 | const char rtems_test_name[] = "SPPRINTK"; |
---|
49 | |
---|
50 | static int test_getchar(void) |
---|
51 | { |
---|
52 | return 0x35; |
---|
53 | } |
---|
54 | |
---|
55 | static void do_getchark(void) |
---|
56 | { |
---|
57 | int sc; |
---|
58 | BSP_polling_getchar_function_type poll_char; |
---|
59 | |
---|
60 | poll_char = BSP_poll_char; |
---|
61 | |
---|
62 | BSP_poll_char = NULL; |
---|
63 | |
---|
64 | putk( "getchark - NULL getchar method - return -1" ); |
---|
65 | sc = getchark(); |
---|
66 | rtems_test_assert( sc == -1 ); |
---|
67 | |
---|
68 | putk( "getchark - test getchar method - returns 0x35" ); |
---|
69 | BSP_poll_char = test_getchar; |
---|
70 | sc = getchark(); |
---|
71 | rtems_test_assert( sc == 0x35 ); |
---|
72 | |
---|
73 | BSP_poll_char = poll_char; |
---|
74 | } |
---|
75 | |
---|
76 | static void do_putk(void) |
---|
77 | { |
---|
78 | putk( "This is a test of putk" ); |
---|
79 | } |
---|
80 | |
---|
81 | static void do_printk(void) |
---|
82 | { |
---|
83 | long lm = 2147483647L; |
---|
84 | unsigned long ulm = 4294967295UL; |
---|
85 | long long llm = 9223372036854775807LL; |
---|
86 | long long ullm = 18446744073709551615ULL; |
---|
87 | |
---|
88 | printk( "bad format -- %%q in parentheses (%q)\n" ); |
---|
89 | |
---|
90 | printk( "bad format -- %%lq in parentheses (%lq)\n" ); |
---|
91 | |
---|
92 | printk( "%%o of 16 -- %o\n", 16 ); |
---|
93 | printk( "%%i of 16 -- %i\n", 16 ); |
---|
94 | printk( "%%d of 16 -- %d\n", 16 ); |
---|
95 | printk( "'%%-3d' of 16 -- '%-3d'\n", 16 ); |
---|
96 | printk( "'%%3d' of 16 -- '%3d'\n", 16 ); |
---|
97 | printk( "%%u of 16 -- %u\n", 16 ); |
---|
98 | printk( "%%X of 16 -- %X\n", 16 ); |
---|
99 | printk( "%%x of 16 -- %x\n", 16 ); |
---|
100 | printk( "%%p of 0x1234 -- %p\n", (void *)0x1234 ); |
---|
101 | |
---|
102 | /* long */ |
---|
103 | printk( "%%lo of 2147483647 -- %lo\n", lm ); |
---|
104 | printk( "%%li of 2147483647 -- %li\n", lm ); |
---|
105 | printk( "%%lu of 2147483647 -- %lu\n", lm ); |
---|
106 | printk( "%%lx of 2147483647 -- %lx\n", lm ); |
---|
107 | printk( "%%lo of -2147483648 -- %lo\n", -lm - 1L ); |
---|
108 | printk( "%%li of -2147483648 -- %li\n", -lm - 1L ); |
---|
109 | printk( "%%lx of -2147483648 -- %lx\n", -lm - 1L ); |
---|
110 | printk( "%%lo of 4294967295 -- %lo\n", ulm ); |
---|
111 | printk( "%%lu of 4294967295 -- %lu\n", ulm ); |
---|
112 | printk( "%%lx of 4294967295 -- %lx\n", ulm ); |
---|
113 | |
---|
114 | /* long long */ |
---|
115 | printk( "%%llo of 9223372036854775807 -- %llo\n", llm ); |
---|
116 | printk( "%%lli of 9223372036854775807 -- %lli\n", llm ); |
---|
117 | printk( "%%llu of 9223372036854775807 -- %llu\n", llm ); |
---|
118 | printk( "%%llx of 9223372036854775807 -- %llx\n", llm ); |
---|
119 | printk( "%%llo of -9223372036854775808 -- %llo\n", -llm - 1LL ); |
---|
120 | printk( "%%lli of -9223372036854775808 -- %lli\n", -llm - 1LL ); |
---|
121 | printk( "%%llx of -9223372036854775808 -- %llx\n", -llm - 1LL ); |
---|
122 | printk( "%%llo of 18446744073709551615 -- %llo\n", ullm ); |
---|
123 | printk( "%%llu of 18446744073709551615 -- %llu\n", ullm ); |
---|
124 | printk( "%%llx of 18446744073709551615 -- %llx\n", ullm ); |
---|
125 | |
---|
126 | /* negative numbers */ |
---|
127 | printk( "%%d of -16 -- %d\n", -16 ); |
---|
128 | printk( "%%d of -16 -- %-3d\n", -16 ); |
---|
129 | printk( "%%u of -16 -- %u\n", -16 ); |
---|
130 | |
---|
131 | /* string formats */ |
---|
132 | printk( "%%s of Mary Had a Little Lamb -- '%s'\n", |
---|
133 | "Mary Had a Little Lamb" ); |
---|
134 | printk( "%%s of NULL -- '%s'\n", NULL ); |
---|
135 | printk( "%%12s of joel -- '%20s'\n", "joel" ); |
---|
136 | printk( "%%4s of joel -- '%4s'\n", "joel" ); |
---|
137 | printk( "%%-12s of joel -- '%-20s'\n", "joel" ); |
---|
138 | printk( "%%-4s of joel -- '%-4s'\n", "joel" ); |
---|
139 | printk( "%%c of X -- '%c'\n", 'X' ); |
---|
140 | printk( "%%hhu of X -- %hhu\n", 'X' ); |
---|
141 | } |
---|
142 | |
---|
143 | typedef struct { |
---|
144 | char buf[128]; |
---|
145 | size_t i; |
---|
146 | } test_context; |
---|
147 | |
---|
148 | static test_context test_instance; |
---|
149 | |
---|
150 | static void clear( test_context *ctx ) |
---|
151 | { |
---|
152 | ctx->i = 0; |
---|
153 | memset( ctx->buf, 0, sizeof( ctx->buf ) ); |
---|
154 | } |
---|
155 | |
---|
156 | static void put_char( int c, void *arg ) |
---|
157 | { |
---|
158 | test_context *ctx; |
---|
159 | |
---|
160 | ctx = arg; |
---|
161 | |
---|
162 | if ( ctx->i < sizeof( ctx->buf ) ) { |
---|
163 | ctx->buf[ ctx->i ] = (char) c; |
---|
164 | ++ctx->i; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | static test_context test_instance; |
---|
169 | |
---|
170 | static void test_io_printf( test_context *ctx ) |
---|
171 | { |
---|
172 | int i; |
---|
173 | intmax_t j; |
---|
174 | long long ll; |
---|
175 | long l; |
---|
176 | size_t z; |
---|
177 | ptrdiff_t t; |
---|
178 | |
---|
179 | clear( ctx ); |
---|
180 | i = 123; |
---|
181 | _IO_Printf( put_char, ctx, "%i", i ); |
---|
182 | rtems_test_assert( strcmp( ctx->buf, "123" ) == 0 ); |
---|
183 | |
---|
184 | clear( ctx ); |
---|
185 | j = 456; |
---|
186 | _IO_Printf( put_char, ctx, "%ji", j ); |
---|
187 | rtems_test_assert( strcmp( ctx->buf, "456" ) == 0 ); |
---|
188 | |
---|
189 | clear( ctx ); |
---|
190 | ll = 789; |
---|
191 | _IO_Printf( put_char, ctx, "%lli", ll ); |
---|
192 | rtems_test_assert( strcmp( ctx->buf, "789" ) == 0 ); |
---|
193 | |
---|
194 | clear( ctx ); |
---|
195 | l = 101112; |
---|
196 | _IO_Printf( put_char, ctx, "%li", l ); |
---|
197 | rtems_test_assert( strcmp( ctx->buf, "101112" ) == 0 ); |
---|
198 | |
---|
199 | clear( ctx ); |
---|
200 | z = 131415; |
---|
201 | _IO_Printf( put_char, ctx, "%zi", z ); |
---|
202 | rtems_test_assert( strcmp( ctx->buf, "131415" ) == 0 ); |
---|
203 | |
---|
204 | clear( ctx ); |
---|
205 | t = 161718; |
---|
206 | _IO_Printf( put_char, ctx, "%ti", t ); |
---|
207 | rtems_test_assert( strcmp( ctx->buf, "161718" ) == 0 ); |
---|
208 | } |
---|
209 | |
---|
210 | static void test_io_base64( test_context *ctx ) |
---|
211 | { |
---|
212 | unsigned char buf[] = "abcdefghi"; |
---|
213 | int n; |
---|
214 | |
---|
215 | clear( ctx ); |
---|
216 | n = _Base64_Encode( put_char, ctx, buf, 9, "\n", 0 ); |
---|
217 | rtems_test_assert( n == 14 ); |
---|
218 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2hp" ) == 0 ); |
---|
219 | |
---|
220 | clear( ctx ); |
---|
221 | n = _Base64_Encode( put_char, ctx, buf, 8, "\n", 4 ); |
---|
222 | rtems_test_assert( n == 14 ); |
---|
223 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2g=" ) == 0 ); |
---|
224 | |
---|
225 | clear( ctx ); |
---|
226 | n = _Base64_Encode( put_char, ctx, buf, 7, "\n", 4 ); |
---|
227 | rtems_test_assert( n == 14 ); |
---|
228 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZw==" ) == 0 ); |
---|
229 | |
---|
230 | clear( ctx ); |
---|
231 | n = _Base64_Encode( put_char, ctx, buf, 6, "\n", 4 ); |
---|
232 | rtems_test_assert( n == 9 ); |
---|
233 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm" ) == 0 ); |
---|
234 | |
---|
235 | clear( ctx ); |
---|
236 | n = _Base64_Encode( put_char, ctx, buf, 5, "\n", 4 ); |
---|
237 | rtems_test_assert( n == 9 ); |
---|
238 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGU=" ) == 0 ); |
---|
239 | |
---|
240 | clear( ctx ); |
---|
241 | n = _Base64_Encode( put_char, ctx, buf, 4, "\n", 4 ); |
---|
242 | rtems_test_assert( n == 9 ); |
---|
243 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZA==" ) == 0 ); |
---|
244 | |
---|
245 | clear( ctx ); |
---|
246 | n = _Base64_Encode( put_char, ctx, buf, 3, "\n", 4 ); |
---|
247 | rtems_test_assert( n == 4 ); |
---|
248 | rtems_test_assert( strcmp( ctx->buf, "YWJj" ) == 0 ); |
---|
249 | |
---|
250 | clear( ctx ); |
---|
251 | n = _Base64_Encode( put_char, ctx, buf, 2, "\n", 4 ); |
---|
252 | rtems_test_assert( n == 4 ); |
---|
253 | rtems_test_assert( strcmp( ctx->buf, "YWI=" ) == 0 ); |
---|
254 | |
---|
255 | clear( ctx ); |
---|
256 | n = _Base64_Encode( put_char, ctx, buf, 1, "\n", 4 ); |
---|
257 | rtems_test_assert( n == 4 ); |
---|
258 | rtems_test_assert( strcmp( ctx->buf, "YQ==" ) == 0 ); |
---|
259 | |
---|
260 | clear( ctx ); |
---|
261 | n = _Base64_Encode( put_char, ctx, buf, 0, "\n", 4 ); |
---|
262 | rtems_test_assert( n == 0 ); |
---|
263 | } |
---|
264 | |
---|
265 | static void test_io_base64url( test_context *ctx ) |
---|
266 | { |
---|
267 | unsigned char buf[] = { 0, 0, 62, 0, 0, 63 }; |
---|
268 | int n; |
---|
269 | |
---|
270 | clear( ctx ); |
---|
271 | n = _Base64url_Encode( put_char, ctx, buf, sizeof( buf ), "\n", 0 ); |
---|
272 | rtems_test_assert( n == 9 ); |
---|
273 | rtems_test_assert( strcmp( ctx->buf, "AAA-\nAAA_" ) == 0 ); |
---|
274 | } |
---|
275 | |
---|
276 | static rtems_task Init( |
---|
277 | rtems_task_argument argument |
---|
278 | ) |
---|
279 | { |
---|
280 | TEST_BEGIN(); |
---|
281 | |
---|
282 | do_putk(); |
---|
283 | putk(""); |
---|
284 | |
---|
285 | do_printk(); |
---|
286 | putk(""); |
---|
287 | |
---|
288 | do_getchark(); |
---|
289 | test_io_printf(&test_instance); |
---|
290 | test_io_base64(&test_instance); |
---|
291 | test_io_base64url(&test_instance); |
---|
292 | |
---|
293 | TEST_END(); |
---|
294 | rtems_test_exit( 0 ); |
---|
295 | } |
---|
296 | |
---|
297 | /* configuration information */ |
---|
298 | |
---|
299 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
300 | |
---|
301 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
302 | |
---|
303 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
304 | |
---|
305 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
306 | |
---|
307 | #define CONFIGURE_INIT |
---|
308 | |
---|
309 | #include <rtems/confdefs.h> |
---|