source: rtems/testsuites/psxtests/psxndbm01/init.c @ 4763ef8

Last change on this file since 4763ef8 was 4763ef8, checked in by Frank Kühndel <frank.kuehndel@…>, on 10/05/20 at 14:23:01

psxndbm01 - Fixing string truncation warning

This fixes the following compiler warning:

testsuites/psxtests/psxndbm01/init.c:221:3: warning: 'strncpy' output truncated
before terminating nul copying 5 bytes from a string of the same length

221 | strncpy( test_strings, "Hello", 5 );

|

In addition, the comments from Sebastian Huber on an old version of
such a patch have been taken into account:

1) The use of sizeof() in key.dsize = sizeof( test_strings ); is wrong.

2) There is no need to allocate the string. One can simply use a string

constant.

(See https://lists.rtems.org/pipermail/devel/2020-August/061418.html)

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/**
2 *  @file
3 *  @brief Test suite for ndbm.h methods
4 */
5
6/*
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * Copyright (C) 2019 Vaibhav Gupta
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37/* header files are listed in lexical/lexicographical/alphabetical order */
38
39#include <errno.h>
40#include <fcntl.h>      /* contains definitions of 'open_flags' */
41#include <limits.h>
42#include <ndbm.h>       /* contains declarations of ndbm methods */
43#include <stddef.h>
44#include <stdint.h>
45#include <stdio.h>
46#include <sys/stat.h>   /* contains definitions of 'file_mode' */
47#include <string.h>
48#include <rtems/test-info.h>
49#include <tmacros.h>
50
51const char rtems_test_name[] = "PSXNDBM 01";
52
53#define NAME      "VARoDeK"
54#define PHONE_NO  "123-321-777-888"
55#define DB_NAME   "phones_test"
56#define NAME2     "VG"
57#define PHONE_NO2 "321-123-888-777"
58
59/* forward declarations to avoid warnings */
60rtems_task Init(rtems_task_argument ignored);
61
62/*
63* This Function takes DBM* as a argument and count the number of records in the
64* database pointed by it.
65*/
66static int count_no_of_records( DBM *db_local )
67{
68  int count = 0;
69  datum temp;
70
71  for (
72    temp = dbm_firstkey( db_local );
73    temp.dptr != NULL;
74    temp = dbm_nextkey( db_local ), count++
75  );
76
77  return count;
78}
79
80/* Test Function Begins */
81rtems_task Init(rtems_task_argument ignored)
82{
83  datum name          = { NAME, sizeof( NAME ) };
84  datum put_phone_no  = { PHONE_NO, sizeof( PHONE_NO ) };
85  datum name2         = { NAME2, sizeof( NAME2 ) };
86  datum put_phone_no2 = { PHONE_NO2, sizeof( PHONE_NO2 ) };
87
88  datum get_phone_no, key;
89
90  int i;
91  char *test_strings;
92  char hello_string[] = "hello";
93
94  DBM *db;
95
96  TEST_BEGIN();
97
98/* A Simple test to check if ndbm methods are call-able */
99
100/*
101 * A Simple test to check if NDBM methods are call-able
102 *
103 * We will try to open a database and then close it.
104 * If it successful, hence we can have further tests.
105 * Also, while opening it for first time, will create that database,
106 * hence we will be able to test for 'O_RDWR | O_EXCL' case later.
107 * Meanwhile we will also store one record, this record will be helpful in
108 * further tests.
109 * And fetch it, to make sure if basic NDBM methods are working correctly.
110 */
111
112  puts( "\nOpen Database." );
113  db = dbm_open( DB_NAME, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU );
114  rtems_test_assert( db != NULL );
115
116  /* This data will be useful in further tests */
117  puts( "Store Records in Database." );
118  dbm_store( db, name, put_phone_no, DBM_INSERT );
119
120  puts( "Fetch Records from Database and check." );
121  get_phone_no = dbm_fetch( db, name );
122  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
123
124  puts( "Close Database." );
125  dbm_close( db );
126
127/* dbm_open() */
128
129  puts( "\nTestcases for 'dbm_open()'." );
130
131/* The 'DB_NAME' is already created, hence 'O_RDWR | O_EXCL' should fail. */
132  puts( "Use 'O_CREAT | O_EXCL' to open existing file and confirm error." );
133  db = dbm_open( DB_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU );
134  rtems_test_assert( db == NULL );
135  rtems_test_assert( errno == EEXIST );
136
137/* Some implementations use 3 characters for the suffix and others use
138 * 4 characters for the suffix, applications should ensure that the maximum
139 * portable pathname length passed to dbm_open() is no greater than
140 * {PATH_MAX}-4 bytes, with the last component of the pathname no greater
141 * than {NAME_MAX}-4 bytes.
142 */
143
144/* inside 'ndbm.h' ; '#define   DBM_SUFFIX      ".db"' ;
145 * 2 alphabets and 1 period, hence 3 characters are used for suffix
146 * in this implementation.
147 */
148
149  puts( "Use path name larger than '{PATH_MAX}-3 bytes.' and confirm error." );
150  test_strings = (char*)malloc( PATH_MAX - 2 );
151  for ( i = 0; i < PATH_MAX - 3; i++ ) {
152    test_strings[i] = 'r';
153  }
154  test_strings[i] = '\0';
155  db = dbm_open(
156                (const char*)test_strings,
157                O_RDWR | O_CREAT | O_TRUNC,
158                S_IRWXU
159  );
160  rtems_test_assert( db == NULL );
161  rtems_test_assert( errno == ENAMETOOLONG );
162  free( test_strings );
163
164/* database opened for write-only access opens the files for read and
165 * write access or it will fail.
166 */
167
168/* Implementation of __hash_open in newlib does not support `O_WRONLY` */
169
170  puts( "Open file with write access only and confirm error." );
171  db = dbm_open( DB_NAME, O_WRONLY, S_IRWXU );
172  rtems_test_assert( db == NULL );
173  rtems_test_assert( errno == EINVAL );
174
175/* dbm_store() */
176
177  puts( "\nTestcases for 'dbm_store()'" );
178  db = dbm_open( DB_NAME, O_RDWR, S_IRWXU );
179  rtems_test_assert( db != NULL );
180
181  puts( "Insert new record with same key using 'DBM_INSERT' mode and "
182        "confirm error." );
183  rtems_test_assert( dbm_store( db, name, put_phone_no2, DBM_INSERT ) == 1 );
184
185  get_phone_no = dbm_fetch( db, name );
186  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
187
188  puts( "Insert new record with same key using 'DBM_REPLACE' mode and "
189        "confirm changes." );
190  rtems_test_assert( dbm_store( db, name, put_phone_no2, DBM_REPLACE ) == 0 );
191
192  get_phone_no = dbm_fetch( db, name );
193  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
194
195/* Revert for next tests */
196  rtems_test_assert( dbm_store( db, name, put_phone_no, DBM_REPLACE ) == 0 );
197
198  puts( "Store a new record and "
199        "confirm that total number of records is successful 2." );
200  rtems_test_assert( dbm_store( db, name2, put_phone_no2, DBM_INSERT ) == 0 );
201
202/* Confirm number of records */
203  rtems_test_assert( count_no_of_records( db ) == 2 );
204
205  dbm_close( db );
206
207/* dbm_fetch() */
208
209  puts( "\nTestcases for 'dbm_fetch()'" );
210  db = dbm_open( DB_NAME, O_RDONLY, S_IRWXU );
211  rtems_test_assert( db != NULL );
212
213  puts( "Fetch existing records and confirm results." );
214  get_phone_no = dbm_fetch( db, name );
215  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO ) == 0 );
216
217  get_phone_no = dbm_fetch( db, name2 );
218  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
219
220  puts( "Fetch non-existing record and confirm error." );
221/* The data pointed by test_string is now pointed by key.dptr */
222  key.dptr = hello_string;
223  key.dsize = strlen( hello_string ) + 1;
224  get_phone_no = dbm_fetch( db, key );
225  rtems_test_assert( get_phone_no.dptr == NULL );
226  dbm_close( db );
227
228/* We need the 'key' object, hence we cannot free 'test_strings' */
229
230/* dbm_delete() */
231
232  puts( "\nTestcases for 'dbm_delete()'" );
233  db = dbm_open( DB_NAME, O_RDWR, S_IRWXU );
234  rtems_test_assert( db != NULL );
235
236  puts( "Delete non-existing record and confirm error." );
237  rtems_test_assert( dbm_delete( db, key ) != 0 );
238  rtems_test_assert( count_no_of_records( db ) == 2);
239
240  puts( "Delete existing record and "
241        "confirm that total number of records is successful 1." );
242  rtems_test_assert( dbm_delete( db, name ) == 0 );
243  rtems_test_assert( count_no_of_records( db ) == 1);
244
245  puts( "Confirm if correct record is deleted." );
246  get_phone_no = dbm_fetch( db, name );
247  rtems_test_assert( get_phone_no.dptr == NULL );
248
249/* record returned by 'dbm_firstkey()' should be the only record
250 * left, this should be checked to confirm correct working of
251 * 'dbm_firstkey()'.
252 * Check if the data is not corrupted after usage of 'dbm_delete()'
253 */
254
255  puts( "Check if the data is not corrupted after usage of 'dbm_delete()'." );
256  get_phone_no = dbm_fetch( db, dbm_firstkey( db ) );
257  rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
258
259/* Empty the database and then try to use 'dbm_firstkey()', the
260 * dptr pointer should point to NULL.
261 */
262
263  puts( "Empty records in database and check results of 'dbm_firstkey()'." );
264  rtems_test_assert( dbm_delete( db, dbm_firstkey( db ) ) == 0 );
265  key = dbm_firstkey( db );
266  rtems_test_assert( key.dptr == NULL );
267  dbm_close( db );
268
269/*
270* All cases for 'dbm_firstkey()' and 'dbm_nextkey()' were tested while
271* performing other tests.
272* One such case be found in count_number_of_records() function.
273*/
274
275  TEST_END();
276  rtems_test_exit(0);
277}
278
279/* NOTICE: the clock driver is explicitly disabled */
280
281#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
282#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
283
284#define CONFIGURE_MAXIMUM_TASKS                  1
285
286#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6
287
288#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
289
290#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
291
292#define CONFIGURE_INIT
293#include <rtems/confdefs.h>
294/* end of file */
Note: See TracBrowser for help on using the repository browser.