Back to home page

LXR

 
 

    


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

0001 /*
0002  *  @file
0003  *  @brief Test suite for ftw.h methods
0004  */
0005 
0006 /*
0007  * SPDX-License-Identifier: BSD-2-Clause
0008  *
0009  * Copyright (C) 2020 Eshan Dhawan, embedded brains GmbH & Co. KG
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 */
0038 #include <bsp.h> /* for device driver prototypes */
0039 #include "tmacros.h"
0040 #include <rtems.h>
0041 #include <rtems/bspIo.h>
0042 #include <rtems/untar.h>
0043 #include <rtems/error.h>
0044 #include <ftw.h>
0045 #include <dirent.h>
0046 #include <sys/stat.h>
0047 #include <errno.h>
0048 #include <unistd.h>
0049 #include <string.h>
0050 #include <stdio.h>
0051 #include <stdlib.h>
0052 #include <rtems/test.h>
0053 
0054 #include "psxftw01-tar.h"
0055 #include "psxftw01-tar-gz.h"
0056 
0057 #define TARFILE_START    psxftw01_tar
0058 #define TARFILE_SIZE     psxftw01_tar_size
0059 #define TARFILE_GZ_START psxftw01_tar_gz
0060 #define TARFILE_GZ_SIZE  psxftw01_tar_gz_size
0061 
0062 const char rtems_test_name[] = "psxftw01";
0063 /* Function decleration to avoid warning */
0064 void *POSIX_Init (void * argument);
0065 
0066 static char  file_traverse_order[6][20];
0067 static int file_order;
0068 
0069 static int fn_function (const char *fpath, const struct stat *sb,
0070 int tflag, struct FTW *ftwbuf)
0071 {
0072   strcpy(file_traverse_order [file_order], fpath + ftwbuf->base) ;
0073   file_order = file_order + 1;
0074   return 0; /* to make nftw run further */
0075 }
0076 
0077 
0078 T_TEST_CASE(ftw)
0079 {
0080 
0081   /* VARIABLE DECLEARATION */
0082   int r;
0083   char *files_path;
0084   int flags;
0085   int i;
0086   /* setting up filesystem */
0087   rtems_status_code sc;
0088 
0089   /*Untaring from memory */
0090   sc = Untar_FromMemory_Print(
0091    (void *)TARFILE_START,
0092    TARFILE_SIZE,
0093    &rtems_test_printer
0094    );
0095   T_rsc_success( sc );
0096 
0097   /* Array with expected file order */
0098   char arr_ftw_depth[5][20] = { "test_file", "test_script", "def", "abc", "home" };
0099   char arr_ftw_phys[5][20] = { "home", "test_file", "abc", "def", "test_script" };
0100   /* defining the path to run nftw*/
0101   files_path = "/home";
0102   /* nftw test with FTW_DEPTH flag*/
0103   flags = 0;
0104   file_order = 0;
0105   flags |= FTW_DEPTH;
0106   r = nftw( files_path, fn_function, 5, flags );
0107   T_psx_success( r );
0108 
0109   /*comparing the nftw file tree to the expexted file tree traversal */
0110   for (i = 0; i < file_order; i++){
0111     r = strcmp( arr_ftw_depth[i], file_traverse_order[i]);
0112     T_eq_int( r, 0 );
0113   }
0114     /*----------------Test Block 2--------------------*/
0115   flags = 0;
0116   file_order = 0;
0117   flags |= FTW_PHYS;
0118   r = nftw( files_path, fn_function, 5, flags );
0119   T_psx_success(r);
0120 
0121   /*comparing the nftw file tree to the expected file tree traversal*/
0122   for (i = 0; i < file_order; i++){
0123     r = strcmp( arr_ftw_phys[i], file_traverse_order[i]);
0124     T_eq_int( r, 0 );
0125   }
0126 
0127 }
0128 
0129 void *POSIX_Init (void * argument)
0130 {
0131   rtems_test_run( (rtems_task_argument) argument, TEST_STATE );
0132 }
0133 
0134 /* NOTICE: the clock driver is explicitly disabled */
0135 
0136 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0137 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
0138 
0139 #define CONFIGURE_MAXIMUM_TASKS                  1
0140 
0141 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6
0142 
0143 #define CONFIGURE_MAXIMUM_POSIX_THREADS  2
0144 
0145 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0146 
0147 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0148 
0149 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0150 
0151 
0152 #define CONFIGURE_INIT
0153 #include <rtems/confdefs.h>
0154 /* end of file */