File indexing completed on 2025-05-11 08:24:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #ifdef HAVE_CONFIG_H
0034 #include "config.h"
0035 #endif
0036
0037
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
0060 rtems_task Init(rtems_task_argument ignored);
0061
0062
0063
0064
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
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
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
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
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
0128
0129 puts( "\nTestcases for 'dbm_open()'." );
0130
0131
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
0138
0139
0140
0141
0142
0143
0144
0145
0146
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
0165
0166
0167
0168
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
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
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
0203 rtems_test_assert( count_no_of_records( db ) == 2 );
0204
0205 dbm_close( db );
0206
0207
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
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
0229
0230
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
0250
0251
0252
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
0260
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
0271
0272
0273
0274
0275 TEST_END();
0276 rtems_test_exit(0);
0277 }
0278
0279
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