Back to home page

LXR

 
 

    


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

0001 /**
0002  *  @file
0003  *  @brief Test suite for ndbm.h methods
0004  */
0005 
0006 /*
0007  * SPDX-License-Identifier: BSD-2-Clause
0008  *
0009  * Copyright (C) 2019 Vaibhav Gupta
0010  *
0011  * Redistribution and use in source and binary forms, with or without
0012  * modification, are permitted provided that the following conditions
0013  * are met:
0014  * 1. Redistributions of source code must retain the above copyright
0015  *    notice, this list of conditions and the following disclaimer.
0016  * 2. Redistributions in binary form must reproduce the above copyright
0017  *    notice, this list of conditions and the following disclaimer in the
0018  *    documentation and/or other materials provided with the distribution.
0019  *
0020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0030  * POSSIBILITY OF SUCH DAMAGE.
0031  */
0032 
0033 #ifdef HAVE_CONFIG_H
0034 #include "config.h"
0035 #endif
0036 
0037 /* header files are listed in lexical/lexicographical/alphabetical order */
0038 
0039 #include <errno.h>
0040 #include <fcntl.h>      /* contains definitions of 'open_flags' */
0041 #include <limits.h>
0042 #include <ndbm.h>       /* contains declarations of ndbm methods */
0043 #include <stddef.h>
0044 #include <stdint.h>
0045 #include <stdio.h>
0046 #include <sys/stat.h>   /* contains definitions of 'file_mode' */
0047 #include <string.h>
0048 #include <rtems/test-info.h>
0049 #include <tmacros.h>
0050 
0051 const char rtems_test_name[] = "PSXNDBM 01";
0052 
0053 #define NAME      "VARoDeK"
0054 #define PHONE_NO  "123-321-777-888"
0055 #define DB_NAME   "phones_test"
0056 #define NAME2     "VG"
0057 #define PHONE_NO2 "321-123-888-777"
0058 
0059 /* forward declarations to avoid warnings */
0060 rtems_task Init(rtems_task_argument ignored);
0061 
0062 /*
0063 * This Function takes DBM* as a argument and count the number of records in the
0064 * database pointed by it.
0065 */
0066 static int count_no_of_records( DBM *db_local )
0067 {
0068   int count = 0;
0069   datum temp;
0070 
0071   for (
0072     temp = dbm_firstkey( db_local );
0073     temp.dptr != NULL;
0074     temp = dbm_nextkey( db_local ), count++
0075   );
0076 
0077   return count;
0078 }
0079 
0080 /* Test Function Begins */
0081 rtems_task Init(rtems_task_argument ignored)
0082 {
0083   datum name          = { NAME, sizeof( NAME ) };
0084   datum put_phone_no  = { PHONE_NO, sizeof( PHONE_NO ) };
0085   datum name2         = { NAME2, sizeof( NAME2 ) };
0086   datum put_phone_no2 = { PHONE_NO2, sizeof( PHONE_NO2 ) };
0087 
0088   datum get_phone_no, key;
0089 
0090   int i;
0091   char *test_strings;
0092   char hello_string[] = "hello";
0093 
0094   DBM *db;
0095 
0096   TEST_BEGIN();
0097 
0098 /* A Simple test to check if ndbm methods are call-able */
0099 
0100 /*
0101  * A Simple test to check if NDBM methods are call-able
0102  *
0103  * We will try to open a database and then close it.
0104  * If it successful, hence we can have further tests.
0105  * Also, while opening it for first time, will create that database,
0106  * hence we will be able to test for 'O_RDWR | O_EXCL' case later.
0107  * Meanwhile we will also store one record, this record will be helpful in
0108  * further tests.
0109  * And fetch it, to make sure if basic NDBM methods are working correctly.
0110  */
0111 
0112   puts( "\nOpen Database." );
0113   db = dbm_open( DB_NAME, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
0114   rtems_test_assert( db != NULL );
0115 
0116   /* This data will be useful in further tests */
0117   puts( "Store Records in Database." );
0118   dbm_store( db, name, put_phone_no, DBM_INSERT );
0119 
0120   puts( "Fetch Records from Database and check." );
0121   get_phone_no = dbm_fetch( db, name );
0122   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
0123 
0124   puts( "Close Database." );
0125   dbm_close( db );
0126 
0127 /* dbm_open() */
0128 
0129   puts( "\nTestcases for 'dbm_open()'." );
0130 
0131 /* The 'DB_NAME' is already created, hence 'O_RDWR | O_EXCL' should fail. */
0132   puts( "Use 'O_CREAT | O_EXCL' to open existing file and confirm error." );
0133   db = dbm_open( DB_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU );
0134   rtems_test_assert( db == NULL );
0135   rtems_test_assert( errno == EEXIST );
0136 
0137 /* Some implementations use 3 characters for the suffix and others use
0138  * 4 characters for the suffix, applications should ensure that the maximum
0139  * portable pathname length passed to dbm_open() is no greater than
0140  * {PATH_MAX}-4 bytes, with the last component of the pathname no greater
0141  * than {NAME_MAX}-4 bytes.
0142  */
0143 
0144 /* inside 'ndbm.h' ; '#define   DBM_SUFFIX  ".db"' ;
0145  * 2 alphabets and 1 period, hence 3 characters are used for suffix
0146  * in this implementation.
0147  */
0148 
0149   puts( "Use path name larger than '{PATH_MAX}-3 bytes.' and confirm error." );
0150   test_strings = (char*)malloc( PATH_MAX - 2 );
0151   for ( i = 0; i < PATH_MAX - 3; i++ ) {
0152     test_strings[i] = 'r';
0153   }
0154   test_strings[i] = '\0';
0155   db = dbm_open(
0156                 (const char*)test_strings,
0157                 O_RDWR | O_CREAT | O_TRUNC,
0158                 S_IRWXU
0159   );
0160   rtems_test_assert( db == NULL );
0161   rtems_test_assert( errno == ENAMETOOLONG );
0162   free( test_strings );
0163 
0164 /* database opened for write-only access opens the files for read and
0165  * write access or it will fail.
0166  */
0167 
0168 /* Implementation of __hash_open in newlib does not support `O_WRONLY` */
0169 
0170   puts( "Open file with write access only and confirm error." );
0171   db = dbm_open( DB_NAME, O_WRONLY, S_IRWXU );
0172   rtems_test_assert( db == NULL );
0173   rtems_test_assert( errno == EINVAL );
0174 
0175 /* dbm_store() */
0176 
0177   puts( "\nTestcases for 'dbm_store()'" );
0178   db = dbm_open( DB_NAME, O_RDWR, S_IRWXU );
0179   rtems_test_assert( db != NULL );
0180 
0181   puts( "Insert new record with same key using 'DBM_INSERT' mode and "
0182         "confirm error." );
0183   rtems_test_assert( dbm_store( db, name, put_phone_no2, DBM_INSERT ) == 1 );
0184 
0185   get_phone_no = dbm_fetch( db, name );
0186   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
0187 
0188   puts( "Insert new record with same key using 'DBM_REPLACE' mode and "
0189         "confirm changes." );
0190   rtems_test_assert( dbm_store( db, name, put_phone_no2, DBM_REPLACE ) == 0 );
0191 
0192   get_phone_no = dbm_fetch( db, name );
0193   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
0194 
0195 /* Revert for next tests */
0196   rtems_test_assert( dbm_store( db, name, put_phone_no, DBM_REPLACE ) == 0 );
0197 
0198   puts( "Store a new record and "
0199         "confirm that total number of records is successful 2." );
0200   rtems_test_assert( dbm_store( db, name2, put_phone_no2, DBM_INSERT ) == 0 );
0201 
0202 /* Confirm number of records */
0203   rtems_test_assert( count_no_of_records( db ) == 2 );
0204 
0205   dbm_close( db );
0206 
0207 /* dbm_fetch() */
0208 
0209   puts( "\nTestcases for 'dbm_fetch()'" );
0210   db = dbm_open( DB_NAME, O_RDONLY, S_IRWXU );
0211   rtems_test_assert( db != NULL );
0212 
0213   puts( "Fetch existing records and confirm results." );
0214   get_phone_no = dbm_fetch( db, name );
0215   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
0216 
0217   get_phone_no = dbm_fetch( db, name2 );
0218   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
0219 
0220   puts( "Fetch non-existing record and confirm error." );
0221 /* The data pointed by test_string is now pointed by key.dptr */
0222   key.dptr = hello_string;
0223   key.dsize = strlen( hello_string ) + 1;
0224   get_phone_no = dbm_fetch( db, key );
0225   rtems_test_assert( get_phone_no.dptr == NULL );
0226   dbm_close( db );
0227 
0228 /* We need the 'key' object, hence we cannot free 'test_strings' */
0229 
0230 /* dbm_delete() */
0231 
0232   puts( "\nTestcases for 'dbm_delete()'" );
0233   db = dbm_open( DB_NAME, O_RDWR, S_IRWXU );
0234   rtems_test_assert( db != NULL );
0235 
0236   puts( "Delete non-existing record and confirm error." );
0237   rtems_test_assert( dbm_delete( db, key ) != 0 );
0238   rtems_test_assert( count_no_of_records( db ) == 2);
0239 
0240   puts( "Delete existing record and "
0241         "confirm that total number of records is successful 1." );
0242   rtems_test_assert( dbm_delete( db, name ) == 0 );
0243   rtems_test_assert( count_no_of_records( db ) == 1);
0244 
0245   puts( "Confirm if correct record is deleted." );
0246   get_phone_no = dbm_fetch( db, name );
0247   rtems_test_assert( get_phone_no.dptr == NULL );
0248 
0249 /* record returned by 'dbm_firstkey()' should be the only record
0250  * left, this should be checked to confirm correct working of
0251  * 'dbm_firstkey()'.
0252  * Check if the data is not corrupted after usage of 'dbm_delete()'
0253  */
0254 
0255   puts( "Check if the data is not corrupted after usage of 'dbm_delete()'." );
0256   get_phone_no = dbm_fetch( db, dbm_firstkey( db ) );
0257   rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
0258 
0259 /* Empty the database and then try to use 'dbm_firstkey()', the
0260  * dptr pointer should point to NULL.
0261  */
0262 
0263   puts( "Empty records in database and check results of 'dbm_firstkey()'." );
0264   rtems_test_assert( dbm_delete( db, dbm_firstkey( db ) ) == 0 );
0265   key = dbm_firstkey( db );
0266   rtems_test_assert( key.dptr == NULL );
0267   dbm_close( db );
0268 
0269 /*
0270 * All cases for 'dbm_firstkey()' and 'dbm_nextkey()' were tested while
0271 * performing other tests.
0272 * One such case be found in count_number_of_records() function.
0273 */
0274 
0275   TEST_END();
0276   rtems_test_exit(0);
0277 }
0278 
0279 /* NOTICE: the clock driver is explicitly disabled */
0280 
0281 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0282 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
0283 
0284 #define CONFIGURE_MAXIMUM_TASKS                  1
0285 
0286 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6
0287 
0288 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0289 
0290 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0291 
0292 #define CONFIGURE_INIT
0293 #include <rtems/confdefs.h>
0294 /* end of file */