source: rtems/testsuites/psxtests/psxchroot01/test.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  Modifications to support reference counting in the file system are
6 *  Copyright (c) 2012 embedded brains GmbH.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.org/license/LICENSE.
11 */
12
13#ifdef HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <stdio.h>
18#include <sys/types.h>
19#include <fcntl.h>
20#include <string.h>
21#include <unistd.h>
22#include <errno.h>
23#include <rtems/libio.h>
24#include <rtems/userenv.h>
25#include <rtems/malloc.h>
26#include <pmacros.h>
27#include <rtems/libcsupport.h>
28
29/* forward declarations to avoid warnings */
30int test_main(void);
31
32static void touch( char *file )
33{
34  int fd;
35
36  rtems_test_assert( file );
37
38  fd = open( file, O_RDWR|O_CREAT, 0777 );
39  rtems_test_assert( fd != -1 );
40  close( fd );
41}
42
43static int fileexists( char *file )
44{
45  int         status;
46  struct stat statbuf;
47
48  rtems_test_assert( file );
49
50  status = stat( file, &statbuf );
51
52  if ( status == -1 ) {
53    /* printf( ": %s\n", strerror( errno ) ); */
54    return 0;
55  }
56  return 1;
57}
58
59#if defined(__rtems__)
60int test_main(void)
61#else
62int main(
63  int argc,
64  char **argv
65)
66#endif
67{
68  static const uintptr_t global_location_size [] = {
69    sizeof(rtems_filesystem_global_location_t)
70  };
71
72  int status;
73  void *opaque;
74/*
75 *  This test is the C equivalent of this sequence.
76#mkdir /one
77#mkdir /one/one
78#touch /one/one.test
79#touch /one/two/two.test
80# an error case to ENOTSUP from chroot
81# chroot
82#chroot /one
83#if !fileexists(/one/one.test) echo "SUCCESSFUL"
84#if fileexists(/two/two.test) echo "SUCCESSFUL"
85#rtems_set_private_env() ! reset at the global environment
86#if fileexists(/one/one.test) echo "SUCESSFUL"
87#if !fileexists(/two/two.test) echo "SUCCESSFUL"
88*/
89
90  printf( "\n\n*** CHROOT01 TEST ***\n" );
91
92  status = mkdir( "/one", 0777);
93  rtems_test_assert( status == 0 );
94
95  status = mkdir( "/one/one", 0777);
96  rtems_test_assert( status == 0 );
97
98  status = mkdir( "/one/two", 0777);
99  rtems_test_assert( status == 0 );
100
101  touch( "/one/one.test" );
102  touch( "/one/two/two.test" );
103
104  puts( "chroot with bad path - expect ENOENT" );
105  status = chroot( "/three" );
106  rtems_test_assert( status == -1 );
107  rtems_test_assert( errno == ENOENT );
108
109  puts( "chroot with file - expect ENOTDIR" );
110  status = chroot( "/one/one.test" );
111  rtems_test_assert( status == -1 );
112  rtems_test_assert( errno == ENOTDIR );
113
114  puts( "allocate most of memory - attempt to fail chroot - expect ENOMEM" );
115  opaque = rtems_heap_greedy_allocate( global_location_size, 1 );
116
117  status = chroot( "/one" );
118  rtems_test_assert( status == -1 );
119  rtems_test_assert( errno == ENOMEM );
120
121  puts( "freeing the allocated memory" );
122  rtems_heap_greedy_free( opaque );
123
124  status = chroot( "/one" );
125  rtems_test_assert( status == 0 );
126
127  status = fileexists( "/one/one.test" );
128  printf( "%s on /one/one.test\n", (!status) ? "SUCCESS" : "FAILURE" );
129
130  status = fileexists( "/two/two.test" );
131  printf( "%s on /two/two.test\n", (status) ? "SUCCESS" : "FAILURE" );
132
133  puts( "Go back to global environment" );
134  rtems_libio_use_global_env();
135
136  status = fileexists( "/one/one.test" );
137  printf( "%s on /one/one.test\n", ( status) ? "SUCCESS" : "FAILURE" );
138
139  status = fileexists( "/two/two.test" );
140  printf( "%s on /two/two.test\n", (!status) ? "SUCCESS" : "FAILURE" );
141
142  printf( "*** END OF CHROOT01 TEST ***\n" );
143  rtems_test_exit(0);
144}
Note: See TracBrowser for help on using the repository browser.