[0473b2b3] | 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
| 2 | |
---|
[4f7b4a8] | 3 | /* |
---|
[ba959a4b] | 4 | * Exercise putk, printk, and getchark |
---|
[02ba7ca] | 5 | * |
---|
[ba959a4b] | 6 | * COPYRIGHT (c) 1989-2010. |
---|
[4f7b4a8] | 7 | * On-Line Applications Research Corporation (OAR). |
---|
| 8 | * |
---|
[0473b2b3] | 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. |
---|
[4f7b4a8] | 29 | */ |
---|
| 30 | |
---|
[7d3f9c6] | 31 | #ifdef HAVE_CONFIG_H |
---|
| 32 | #include "config.h" |
---|
| 33 | #endif |
---|
| 34 | |
---|
[317df86] | 35 | #include <rtems/base64.h> |
---|
[527af2b7] | 36 | #include <rtems/dev/io.h> |
---|
[e8020d1] | 37 | |
---|
[24d0ee57] | 38 | /* |
---|
[e8020d1] | 39 | * Undefined the RTEMS_PRINTFLIKE and make it nothing. The test code |
---|
[24d0ee57] | 40 | * contained in the file is suppose to be wrong. |
---|
| 41 | */ |
---|
[e8020d1] | 42 | #undef RTEMS_PRINTFLIKE |
---|
| 43 | #define RTEMS_PRINTFLIKE(_a, _b) |
---|
[24d0ee57] | 44 | |
---|
[7e102915] | 45 | #include <rtems/bspIo.h> |
---|
[addf1aa] | 46 | #include <tmacros.h> |
---|
| 47 | |
---|
| 48 | const char rtems_test_name[] = "SPPRINTK"; |
---|
| 49 | |
---|
[1082798] | 50 | static int test_getchar(void) |
---|
[02ba7ca] | 51 | { |
---|
[ba959a4b] | 52 | return 0x35; |
---|
| 53 | } |
---|
| 54 | |
---|
[1082798] | 55 | static void do_getchark(void) |
---|
[ba959a4b] | 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; |
---|
[24d0ee57] | 63 | |
---|
[ba959a4b] | 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 | } |
---|
[02ba7ca] | 75 | |
---|
[1082798] | 76 | static void do_putk(void) |
---|
[ba959a4b] | 77 | { |
---|
| 78 | putk( "This is a test of putk" ); |
---|
| 79 | } |
---|
[046f4715] | 80 | |
---|
[1082798] | 81 | static void do_printk(void) |
---|
[ba959a4b] | 82 | { |
---|
[b1196e3] | 83 | long lm = 2147483647L; |
---|
| 84 | unsigned long ulm = 4294967295UL; |
---|
| 85 | long long llm = 9223372036854775807LL; |
---|
| 86 | long long ullm = 18446744073709551615ULL; |
---|
| 87 | |
---|
[1082798] | 88 | printk( "bad format -- %%q in parentheses (%q)\n" ); |
---|
[02ba7ca] | 89 | |
---|
[1082798] | 90 | printk( "bad format -- %%lq in parentheses (%lq)\n" ); |
---|
[51dc1b3f] | 91 | |
---|
[1082798] | 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 ); |
---|
[4f67befd] | 101 | |
---|
[b1196e3] | 102 | /* long */ |
---|
[1082798] | 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 ); |
---|
[b1196e3] | 113 | |
---|
| 114 | /* long long */ |
---|
[1082798] | 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 ); |
---|
[b1196e3] | 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 | |
---|
[4f67befd] | 126 | /* negative numbers */ |
---|
[1082798] | 127 | printk( "%%d of -16 -- %d\n", -16 ); |
---|
| 128 | printk( "%%d of -16 -- %-3d\n", -16 ); |
---|
| 129 | printk( "%%u of -16 -- %u\n", -16 ); |
---|
[4f67befd] | 130 | |
---|
| 131 | /* string formats */ |
---|
[1082798] | 132 | printk( "%%s of Mary Had a Little Lamb -- '%s'\n", |
---|
[51dc1b3f] | 133 | "Mary Had a Little Lamb" ); |
---|
[1082798] | 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 ); |
---|
[ba959a4b] | 208 | } |
---|
| 209 | |
---|
[a6b36334] | 210 | static void test_io_base64( test_context *ctx ) |
---|
| 211 | { |
---|
| 212 | unsigned char buf[] = "abcdefghi"; |
---|
| 213 | int n; |
---|
| 214 | |
---|
| 215 | clear( ctx ); |
---|
[317df86] | 216 | n = _Base64_Encode( put_char, ctx, buf, 9, "\n", 0 ); |
---|
[a6b36334] | 217 | rtems_test_assert( n == 14 ); |
---|
| 218 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2hp" ) == 0 ); |
---|
| 219 | |
---|
| 220 | clear( ctx ); |
---|
[317df86] | 221 | n = _Base64_Encode( put_char, ctx, buf, 8, "\n", 4 ); |
---|
[a6b36334] | 222 | rtems_test_assert( n == 14 ); |
---|
| 223 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2g=" ) == 0 ); |
---|
| 224 | |
---|
| 225 | clear( ctx ); |
---|
[317df86] | 226 | n = _Base64_Encode( put_char, ctx, buf, 7, "\n", 4 ); |
---|
[a6b36334] | 227 | rtems_test_assert( n == 14 ); |
---|
| 228 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZw==" ) == 0 ); |
---|
| 229 | |
---|
| 230 | clear( ctx ); |
---|
[317df86] | 231 | n = _Base64_Encode( put_char, ctx, buf, 6, "\n", 4 ); |
---|
[a6b36334] | 232 | rtems_test_assert( n == 9 ); |
---|
| 233 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm" ) == 0 ); |
---|
| 234 | |
---|
| 235 | clear( ctx ); |
---|
[317df86] | 236 | n = _Base64_Encode( put_char, ctx, buf, 5, "\n", 4 ); |
---|
[a6b36334] | 237 | rtems_test_assert( n == 9 ); |
---|
| 238 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGU=" ) == 0 ); |
---|
| 239 | |
---|
| 240 | clear( ctx ); |
---|
[317df86] | 241 | n = _Base64_Encode( put_char, ctx, buf, 4, "\n", 4 ); |
---|
[a6b36334] | 242 | rtems_test_assert( n == 9 ); |
---|
| 243 | rtems_test_assert( strcmp( ctx->buf, "YWJj\nZA==" ) == 0 ); |
---|
| 244 | |
---|
| 245 | clear( ctx ); |
---|
[317df86] | 246 | n = _Base64_Encode( put_char, ctx, buf, 3, "\n", 4 ); |
---|
[a6b36334] | 247 | rtems_test_assert( n == 4 ); |
---|
| 248 | rtems_test_assert( strcmp( ctx->buf, "YWJj" ) == 0 ); |
---|
| 249 | |
---|
| 250 | clear( ctx ); |
---|
[317df86] | 251 | n = _Base64_Encode( put_char, ctx, buf, 2, "\n", 4 ); |
---|
[a6b36334] | 252 | rtems_test_assert( n == 4 ); |
---|
| 253 | rtems_test_assert( strcmp( ctx->buf, "YWI=" ) == 0 ); |
---|
| 254 | |
---|
| 255 | clear( ctx ); |
---|
[317df86] | 256 | n = _Base64_Encode( put_char, ctx, buf, 1, "\n", 4 ); |
---|
[a6b36334] | 257 | rtems_test_assert( n == 4 ); |
---|
| 258 | rtems_test_assert( strcmp( ctx->buf, "YQ==" ) == 0 ); |
---|
| 259 | |
---|
| 260 | clear( ctx ); |
---|
[317df86] | 261 | n = _Base64_Encode( put_char, ctx, buf, 0, "\n", 4 ); |
---|
[a6b36334] | 262 | rtems_test_assert( n == 0 ); |
---|
| 263 | } |
---|
| 264 | |
---|
[5f8bc83] | 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 ); |
---|
[317df86] | 271 | n = _Base64url_Encode( put_char, ctx, buf, sizeof( buf ), "\n", 0 ); |
---|
[5f8bc83] | 272 | rtems_test_assert( n == 9 ); |
---|
| 273 | rtems_test_assert( strcmp( ctx->buf, "AAA-\nAAA_" ) == 0 ); |
---|
| 274 | } |
---|
| 275 | |
---|
[1082798] | 276 | static rtems_task Init( |
---|
[ba959a4b] | 277 | rtems_task_argument argument |
---|
| 278 | ) |
---|
| 279 | { |
---|
[24d0ee57] | 280 | TEST_BEGIN(); |
---|
[ba959a4b] | 281 | |
---|
| 282 | do_putk(); |
---|
| 283 | putk(""); |
---|
| 284 | |
---|
| 285 | do_printk(); |
---|
| 286 | putk(""); |
---|
[4f67befd] | 287 | |
---|
[ba959a4b] | 288 | do_getchark(); |
---|
[1082798] | 289 | test_io_printf(&test_instance); |
---|
[a6b36334] | 290 | test_io_base64(&test_instance); |
---|
[5f8bc83] | 291 | test_io_base64url(&test_instance); |
---|
[51dc1b3f] | 292 | |
---|
[24d0ee57] | 293 | TEST_END(); |
---|
[02ba7ca] | 294 | rtems_test_exit( 0 ); |
---|
| 295 | } |
---|
[4f7b4a8] | 296 | |
---|
| 297 | /* configuration information */ |
---|
| 298 | |
---|
| 299 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
| 300 | |
---|
[02ba7ca] | 301 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
| 302 | |
---|
[6c0301d] | 303 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
| 304 | |
---|
[4f7b4a8] | 305 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
| 306 | |
---|
[02ba7ca] | 307 | #define CONFIGURE_INIT |
---|
[4f7b4a8] | 308 | |
---|
| 309 | #include <rtems/confdefs.h> |
---|