Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:48

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Exercise putk, printk, and getchark
0005  *
0006  *  COPYRIGHT (c) 1989-2010.
0007  *  On-Line Applications Research Corporation (OAR).
0008  *
0009  * Redistribution and use in source and binary forms, with or without
0010  * modification, are permitted provided that the following conditions
0011  * are met:
0012  * 1. Redistributions of source code must retain the above copyright
0013  *    notice, this list of conditions and the following disclaimer.
0014  * 2. Redistributions in binary form must reproduce the above copyright
0015  *    notice, this list of conditions and the following disclaimer in the
0016  *    documentation and/or other materials provided with the distribution.
0017  *
0018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0028  * POSSIBILITY OF SUCH DAMAGE.
0029  */
0030 
0031 #ifdef HAVE_CONFIG_H
0032 #include "config.h"
0033 #endif
0034 
0035 #include <rtems/base64.h>
0036 #include <rtems/dev/io.h>
0037 
0038 /*
0039  * Undefined the RTEMS_PRINTFLIKE and make it nothing. The test code
0040  * contained in the file is suppose to be wrong.
0041  */
0042 #undef RTEMS_PRINTFLIKE
0043 #define RTEMS_PRINTFLIKE(_a, _b)
0044 
0045 #include <rtems/bspIo.h>
0046 #include <tmacros.h>
0047 
0048 const char rtems_test_name[] = "SPPRINTK";
0049 
0050 static int test_getchar(void)
0051 {
0052   return 0x35;
0053 }
0054 
0055 static void do_getchark(void)
0056 {
0057   int                                sc;
0058   BSP_polling_getchar_function_type  poll_char;
0059 
0060   poll_char = BSP_poll_char;
0061 
0062   BSP_poll_char = NULL;
0063 
0064   putk( "getchark - NULL getchar method - return -1" );
0065   sc = getchark();
0066   rtems_test_assert( sc == -1 );
0067 
0068   putk( "getchark - test getchar method - returns 0x35" );
0069   BSP_poll_char = test_getchar;
0070   sc = getchark();
0071   rtems_test_assert( sc == 0x35 );
0072 
0073   BSP_poll_char = poll_char;
0074 }
0075 
0076 static void do_putk(void)
0077 {
0078   putk( "This is a test of putk" );
0079 }
0080 
0081 static void do_printk(void)
0082 {
0083   long lm = 2147483647L;
0084   unsigned long ulm = 4294967295UL;
0085   long long llm = 9223372036854775807LL;
0086   long long ullm = 18446744073709551615ULL;
0087 
0088   printk( "bad format                   -- %%q in parentheses (%q)\n" );
0089 
0090   printk( "bad format                   -- %%lq in parentheses (%lq)\n" );
0091 
0092   printk( "%%o of 16                     -- %o\n", 16 );
0093   printk( "%%i of 16                     -- %i\n", 16 );
0094   printk( "%%d of 16                     -- %d\n", 16 );
0095   printk( "'%%-3d' of 16                 -- '%-3d'\n", 16 );
0096   printk( "'%%3d' of 16                  -- '%3d'\n", 16 );
0097   printk( "%%u of 16                     -- %u\n", 16 );
0098   printk( "%%X of 16                     -- %X\n", 16 );
0099   printk( "%%x of 16                     -- %x\n", 16 );
0100   printk( "%%p of 0x1234                 -- %p\n", (void *)0x1234 );
0101 
0102   /* long */
0103   printk( "%%lo of 2147483647            -- %lo\n", lm );
0104   printk( "%%li of 2147483647            -- %li\n", lm );
0105   printk( "%%lu of 2147483647            -- %lu\n", lm );
0106   printk( "%%lx of 2147483647            -- %lx\n", lm );
0107   printk( "%%lo of -2147483648           -- %lo\n", -lm - 1L );
0108   printk( "%%li of -2147483648           -- %li\n", -lm - 1L );
0109   printk( "%%lx of -2147483648           -- %lx\n", -lm - 1L );
0110   printk( "%%lo of 4294967295            -- %lo\n", ulm );
0111   printk( "%%lu of 4294967295            -- %lu\n", ulm );
0112   printk( "%%lx of 4294967295            -- %lx\n", ulm );
0113 
0114   /* long long */
0115   printk( "%%llo of 9223372036854775807  -- %llo\n", llm );
0116   printk( "%%lli of 9223372036854775807  -- %lli\n", llm );
0117   printk( "%%llu of 9223372036854775807  -- %llu\n", llm );
0118   printk( "%%llx of 9223372036854775807  -- %llx\n", llm );
0119   printk( "%%llo of -9223372036854775808 -- %llo\n", -llm - 1LL );
0120   printk( "%%lli of -9223372036854775808 -- %lli\n", -llm - 1LL );
0121   printk( "%%llx of -9223372036854775808 -- %llx\n", -llm - 1LL );
0122   printk( "%%llo of 18446744073709551615 -- %llo\n", ullm );
0123   printk( "%%llu of 18446744073709551615 -- %llu\n", ullm );
0124   printk( "%%llx of 18446744073709551615 -- %llx\n", ullm );
0125 
0126   /* negative numbers */
0127   printk( "%%d of -16                    -- %d\n", -16 );
0128   printk( "%%d of -16                    -- %-3d\n", -16 );
0129   printk( "%%u of -16                    -- %u\n", -16 );
0130 
0131   /* string formats */
0132   printk( "%%s of Mary Had a Little Lamb -- '%s'\n",
0133           "Mary Had a Little Lamb" );
0134   printk( "%%s of NULL                   -- '%s'\n", NULL );
0135   printk( "%%12s of joel                 -- '%20s'\n", "joel" );
0136   printk( "%%4s of joel                  -- '%4s'\n", "joel" );
0137   printk( "%%-12s of joel                -- '%-20s'\n", "joel" );
0138   printk( "%%-4s of joel                 -- '%-4s'\n", "joel" );
0139   printk( "%%c of X                      -- '%c'\n", 'X' );
0140   printk( "%%hhu of X                    -- %hhu\n", 'X' );
0141 }
0142 
0143 typedef struct {
0144   char buf[128];
0145   size_t i;
0146 } test_context;
0147 
0148 static test_context test_instance;
0149 
0150 static void clear( test_context *ctx )
0151 {
0152   ctx->i = 0;
0153   memset( ctx->buf, 0, sizeof( ctx->buf ) );
0154 }
0155 
0156 static void put_char( int c, void *arg )
0157 {
0158   test_context *ctx;
0159 
0160   ctx = arg;
0161 
0162   if ( ctx->i < sizeof( ctx->buf ) ) {
0163     ctx->buf[ ctx->i ] = (char) c;
0164     ++ctx->i;
0165   }
0166 }
0167 
0168 static test_context test_instance;
0169 
0170 static void test_io_printf( test_context *ctx )
0171 {
0172   int i;
0173   intmax_t j;
0174   long long ll;
0175   long l;
0176   size_t z;
0177   ptrdiff_t t;
0178 
0179   clear( ctx );
0180   i = 123;
0181   _IO_Printf( put_char, ctx, "%i", i );
0182   rtems_test_assert( strcmp( ctx->buf, "123" ) == 0 );
0183 
0184   clear( ctx );
0185   j = 456;
0186   _IO_Printf( put_char, ctx, "%ji", j );
0187   rtems_test_assert( strcmp( ctx->buf, "456" ) == 0 );
0188 
0189   clear( ctx );
0190   ll = 789;
0191   _IO_Printf( put_char, ctx, "%lli", ll );
0192   rtems_test_assert( strcmp( ctx->buf, "789" ) == 0 );
0193 
0194   clear( ctx );
0195   l = 101112;
0196   _IO_Printf( put_char, ctx, "%li", l );
0197   rtems_test_assert( strcmp( ctx->buf, "101112" ) == 0 );
0198 
0199   clear( ctx );
0200   z = 131415;
0201   _IO_Printf( put_char, ctx, "%zi", z );
0202   rtems_test_assert( strcmp( ctx->buf, "131415" ) == 0 );
0203 
0204   clear( ctx );
0205   t = 161718;
0206   _IO_Printf( put_char, ctx, "%ti", t );
0207   rtems_test_assert( strcmp( ctx->buf, "161718" ) == 0 );
0208 }
0209 
0210 static void test_io_base64( test_context *ctx )
0211 {
0212   unsigned char buf[] = "abcdefghi";
0213   int n;
0214 
0215   clear( ctx );
0216   n = _Base64_Encode( put_char, ctx, buf, 9, "\n", 0 );
0217   rtems_test_assert( n == 14 );
0218   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2hp" ) == 0 );
0219 
0220   clear( ctx );
0221   n = _Base64_Encode( put_char, ctx, buf, 8, "\n", 4 );
0222   rtems_test_assert( n == 14 );
0223   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2g=" ) == 0 );
0224 
0225   clear( ctx );
0226   n = _Base64_Encode( put_char, ctx, buf, 7, "\n", 4 );
0227   rtems_test_assert( n == 14 );
0228   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZw==" ) == 0 );
0229 
0230   clear( ctx );
0231   n = _Base64_Encode( put_char, ctx, buf, 6, "\n", 4 );
0232   rtems_test_assert( n == 9 );
0233   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm" ) == 0 );
0234 
0235   clear( ctx );
0236   n = _Base64_Encode( put_char, ctx, buf, 5, "\n", 4 );
0237   rtems_test_assert( n == 9 );
0238   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGU=" ) == 0 );
0239 
0240   clear( ctx );
0241   n = _Base64_Encode( put_char, ctx, buf, 4, "\n", 4 );
0242   rtems_test_assert( n == 9 );
0243   rtems_test_assert( strcmp( ctx->buf, "YWJj\nZA==" ) == 0 );
0244 
0245   clear( ctx );
0246   n = _Base64_Encode( put_char, ctx, buf, 3, "\n", 4 );
0247   rtems_test_assert( n == 4 );
0248   rtems_test_assert( strcmp( ctx->buf, "YWJj" ) == 0 );
0249 
0250   clear( ctx );
0251   n = _Base64_Encode( put_char, ctx, buf, 2, "\n", 4 );
0252   rtems_test_assert( n == 4 );
0253   rtems_test_assert( strcmp( ctx->buf, "YWI=" ) == 0 );
0254 
0255   clear( ctx );
0256   n = _Base64_Encode( put_char, ctx, buf, 1, "\n", 4 );
0257   rtems_test_assert( n == 4 );
0258   rtems_test_assert( strcmp( ctx->buf, "YQ==" ) == 0 );
0259 
0260   clear( ctx );
0261   n = _Base64_Encode( put_char, ctx, buf, 0, "\n", 4 );
0262   rtems_test_assert( n == 0 );
0263 }
0264 
0265 static void test_io_base64url( test_context *ctx )
0266 {
0267   unsigned char buf[] = { 0, 0, 62, 0, 0, 63 };
0268   int n;
0269 
0270   clear( ctx );
0271   n = _Base64url_Encode( put_char, ctx, buf, sizeof( buf ), "\n", 0 );
0272   rtems_test_assert( n == 9 );
0273   rtems_test_assert( strcmp( ctx->buf, "AAA-\nAAA_" ) == 0 );
0274 }
0275 
0276 static rtems_task Init(
0277   rtems_task_argument argument
0278 )
0279 {
0280   TEST_BEGIN();
0281 
0282   do_putk();
0283   putk("");
0284 
0285   do_printk();
0286   putk("");
0287 
0288   do_getchark();
0289   test_io_printf(&test_instance);
0290   test_io_base64(&test_instance);
0291   test_io_base64url(&test_instance);
0292 
0293   TEST_END();
0294   rtems_test_exit( 0 );
0295 }
0296 
0297 /* configuration information */
0298 
0299 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0300 
0301 #define CONFIGURE_MAXIMUM_TASKS           1
0302 
0303 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0304 
0305 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0306 
0307 #define CONFIGURE_INIT
0308 
0309 #include <rtems/confdefs.h>