source: rtems/testsuites/psxtests/psxchroot01/test.c @ 6c0301d

4.115
Last change on this file since 6c0301d was 698c2e50, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 07:06:16

tests/psxtests: Use <rtems/test.h>

  • 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
29const char rtems_test_name[] = "PSXCHROOT 1";
30
31/* forward declarations to avoid warnings */
32int test_main(void);
33
34static void touch( char *file )
35{
36  int fd;
37
38  rtems_test_assert( file );
39
40  fd = open( file, O_RDWR|O_CREAT, 0777 );
41  rtems_test_assert( fd != -1 );
42  close( fd );
43}
44
45static int fileexists( char *file )
46{
47  int         status;
48  struct stat statbuf;
49
50  rtems_test_assert( file );
51
52  status = stat( file, &statbuf );
53
54  if ( status == -1 ) {
55    /* printf( ": %s\n", strerror( errno ) ); */
56    return 0;
57  }
58  return 1;
59}
60
61#if defined(__rtems__)
62int test_main(void)
63#else
64int main(
65  int argc,
66  char **argv
67)
68#endif
69{
70  static const uintptr_t global_location_size [] = {
71    sizeof(rtems_filesystem_global_location_t)
72  };
73
74  int status;
75  void *opaque;
76/*
77 *  This test is the C equivalent of this sequence.
78#mkdir /one
79#mkdir /one/one
80#touch /one/one.test
81#touch /one/two/two.test
82# an error case to ENOTSUP from chroot
83# chroot
84#chroot /one
85#if !fileexists(/one/one.test) echo "SUCCESSFUL"
86#if fileexists(/two/two.test) echo "SUCCESSFUL"
87#rtems_set_private_env() ! reset at the global environment
88#if fileexists(/one/one.test) echo "SUCESSFUL"
89#if !fileexists(/two/two.test) echo "SUCCESSFUL"
90*/
91
92  TEST_BEGIN();
93
94  status = mkdir( "/one", 0777);
95  rtems_test_assert( status == 0 );
96
97  status = mkdir( "/one/one", 0777);
98  rtems_test_assert( status == 0 );
99
100  status = mkdir( "/one/two", 0777);
101  rtems_test_assert( status == 0 );
102
103  touch( "/one/one.test" );
104  touch( "/one/two/two.test" );
105
106  puts( "chroot with bad path - expect ENOENT" );
107  status = chroot( "/three" );
108  rtems_test_assert( status == -1 );
109  rtems_test_assert( errno == ENOENT );
110
111  puts( "chroot with file - expect ENOTDIR" );
112  status = chroot( "/one/one.test" );
113  rtems_test_assert( status == -1 );
114  rtems_test_assert( errno == ENOTDIR );
115
116  puts( "allocate most of memory - attempt to fail chroot - expect ENOMEM" );
117  opaque = rtems_heap_greedy_allocate( global_location_size, 1 );
118
119  status = chroot( "/one" );
120  rtems_test_assert( status == -1 );
121  rtems_test_assert( errno == ENOMEM );
122
123  puts( "freeing the allocated memory" );
124  rtems_heap_greedy_free( opaque );
125
126  status = chroot( "/one" );
127  rtems_test_assert( status == 0 );
128
129  status = fileexists( "/one/one.test" );
130  printf( "%s on /one/one.test\n", (!status) ? "SUCCESS" : "FAILURE" );
131
132  status = fileexists( "/two/two.test" );
133  printf( "%s on /two/two.test\n", (status) ? "SUCCESS" : "FAILURE" );
134
135  puts( "Go back to global environment" );
136  rtems_libio_use_global_env();
137
138  status = fileexists( "/one/one.test" );
139  printf( "%s on /one/one.test\n", ( status) ? "SUCCESS" : "FAILURE" );
140
141  status = fileexists( "/two/two.test" );
142  printf( "%s on /two/two.test\n", (!status) ? "SUCCESS" : "FAILURE" );
143
144  TEST_END();
145  rtems_test_exit(0);
146}
Note: See TracBrowser for help on using the repository browser.