source: rtems/testsuites/fstests/fspermission/test.c @ bc75887

4.11
Last change on this file since bc75887 was bc75887, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 16, 2014 at 3:15:33 PM

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

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2011.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <errno.h>
17#include <string.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <dirent.h>
22#include <unistd.h>
23
24#include "fstest.h"
25#include "fs_config.h"
26#include "pmacros.h"
27
28const char rtems_test_name[] = "FSPERMISSION " FILESYSTEM;
29
30/*
31 *  Test the umask
32 */
33static void umask_test01(void )
34{
35
36  mode_t previous_cmask;
37  mode_t tmp_mode;
38  mode_t file_mode;
39  struct stat statbuf;
40  int status = 0;
41  int fd;
42
43  char* file01="file01";
44  char* file02="file02";
45  char* directory01="dir01";
46
47  const char* wd=__func__;
48
49  mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ;
50
51
52
53  /*
54   *Create a new directory and change the current directory to  this
55   */
56  status=mkdir(wd,mode);
57  rtems_test_assert(status==0);
58  status=chdir(wd);
59  rtems_test_assert(status==0);
60
61/*
62 *
63 *  Call open creat and mkdir to create new files and directory
64 */
65  fd=open(file01,O_CREAT|O_RDWR,mode);
66  status=close(fd);
67  rtems_test_assert(status==0);
68
69  fd=creat(file02,mode);
70  status=close(fd);
71  rtems_test_assert(status==0);
72
73  status=mkdir(directory01,mode);
74  rtems_test_assert(status==0);
75
76
77  /*
78   *Get the previous cmask and set it to a new one
79   */
80  previous_cmask = umask (0321);
81  printf("The previous cmask is %03o\n",(unsigned int)previous_cmask);
82  file_mode= mode & ~previous_cmask;
83
84  status = stat (file01, &statbuf);
85  rtems_test_assert (status == 0);
86  tmp_mode = (statbuf.st_mode) & ALLPERMS;
87  printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode);
88  rtems_test_assert(tmp_mode==file_mode);
89
90
91  status = stat (file02, &statbuf);
92  rtems_test_assert (status == 0);
93  tmp_mode = (statbuf.st_mode) & ALLPERMS;
94  printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode);
95  rtems_test_assert(tmp_mode==file_mode);
96
97  status = stat (directory01, &statbuf);
98  rtems_test_assert (status == 0);
99  tmp_mode = (statbuf.st_mode) & ALLPERMS;
100  printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode);
101  rtems_test_assert(tmp_mode==file_mode);
102
103  /*
104   *Remove them and recreate them with the same mode
105   */
106
107  status=unlink(file01);
108  rtems_test_assert(status==0);
109  fd=open(file01,O_CREAT|O_RDWR,mode);
110  status=close(fd);
111  rtems_test_assert(status==0);
112
113  status=unlink(file02);
114  rtems_test_assert(status==0);
115  fd=creat(file02,mode);
116  status=close(fd);
117  rtems_test_assert(status==0);
118
119  status=rmdir(directory01);
120  rtems_test_assert(status==0);
121  status=mkdir(directory01,mode);
122  rtems_test_assert(status==0);
123
124  /*
125   *Check the file mode
126   */
127
128  previous_cmask = umask (00);
129  printf("The previous cmask is %03o\n",(unsigned int)previous_cmask);
130  file_mode= mode & ~previous_cmask;
131
132  status = stat (file01, &statbuf);
133  rtems_test_assert (status == 0);
134  tmp_mode = (statbuf.st_mode) & ALLPERMS;
135  printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode);
136  rtems_test_assert(tmp_mode==file_mode);
137
138
139  status = stat (file02, &statbuf);
140  rtems_test_assert (status == 0);
141  tmp_mode = (statbuf.st_mode) & ALLPERMS;
142  printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode);
143  rtems_test_assert(tmp_mode==file_mode);
144
145  status = stat (directory01, &statbuf);
146  rtems_test_assert (status == 0);
147  tmp_mode = (statbuf.st_mode) & ALLPERMS;
148  printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode);
149  rtems_test_assert(tmp_mode==file_mode);
150
151  /*
152   * Go back to parent directory
153   */
154  status=chdir("..");
155  rtems_test_assert(status==0);
156
157}
158/*
159 * Check the file mode in file and directory
160 */
161static void test_premission01(void )
162{
163  mode_t tmp_mode;
164  struct stat statbuf;
165  int status = 0;
166  int fd;
167
168  char* file01="file01";
169  char* file02="file02";
170  char* directory01="dir01";
171
172  char path[20];
173  char* test_data="Test Data";
174  char* data_buf;
175  size_t len=strlen(test_data);
176
177  int n;
178  DIR *dp;
179
180  const char* wd=__func__;
181
182  mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ;
183  uid_t user_id =65534;
184  gid_t group_id =65534;
185
186  uid_t another_user_id =65533;
187  gid_t another_group_id =65533;
188
189
190  /*
191   *Create a new directory and change the current directory to this
192   */
193  umask(00);
194  status=mkdir(wd,mode);
195  rtems_test_assert(status==0);
196  status=chdir(wd);
197  rtems_test_assert(status==0);
198
199  status=seteuid(user_id);
200  rtems_test_assert(status==0);
201  status=setegid(group_id);
202  rtems_test_assert(status==0);
203
204
205  /*
206   *Create a file with mode 0777
207   */
208  fd=creat(file01,mode);
209  rtems_test_assert(fd >= 0);
210  status=close(fd);
211  rtems_test_assert(status==0);
212  /*
213   *Create a file with mode 0240
214   */
215
216  fd=creat(file02,0240);
217  rtems_test_assert(fd >= 0);
218  status=close(fd);
219  rtems_test_assert(status==0);
220
221
222  /*
223   *Check the file mode uid and gid
224   */
225  status = stat (file01, &statbuf);
226  rtems_test_assert (status == 0);
227  tmp_mode = (statbuf.st_mode) & ALLPERMS;
228  printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode);
229  rtems_test_assert(tmp_mode==mode);
230  rtems_test_assert(statbuf.st_uid==user_id);
231  rtems_test_assert(statbuf.st_gid==group_id);
232
233  status = stat (file02, &statbuf);
234  rtems_test_assert (status == 0);
235  tmp_mode = (statbuf.st_mode) & ALLPERMS;
236  printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode);
237  rtems_test_assert(tmp_mode==0240);
238  rtems_test_assert(statbuf.st_uid==user_id);
239  rtems_test_assert(statbuf.st_gid==group_id);
240
241  /*
242   * Create directory and a file in it for tese
243   */
244
245  status=mkdir(directory01,0777);
246  rtems_test_assert(status==0);
247  sprintf(path,"%s/%s",directory01,file01);
248  fd = creat(path,0777);
249  rtems_test_assert(fd >= 0);
250  status = close(fd);
251  rtems_test_assert(status == 0);
252
253  status=chmod(directory01,0340);
254  rtems_test_assert (status == 0);
255  status = stat (directory01, &statbuf);
256  rtems_test_assert (status == 0);
257  tmp_mode = (statbuf.st_mode) & ALLPERMS;
258  printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode);
259  rtems_test_assert(tmp_mode==0340);
260  rtems_test_assert(statbuf.st_uid==user_id);
261  rtems_test_assert(statbuf.st_gid==group_id);
262
263  /*
264   * Check the file with open and write
265   */
266
267  /*
268   * Test write
269   */
270  fd=open(file01,O_WRONLY);
271  rtems_test_assert(fd >= 0);
272  n=write(fd,test_data,len);
273  rtems_test_assert(n==len);
274  status=close(fd);
275  rtems_test_assert(status==0);
276
277  fd=open(file02,O_WRONLY);
278  n=write(fd,test_data,len);
279  rtems_test_assert(n==len);
280  status=close(fd);
281  rtems_test_assert(status==0);
282
283  /*
284   * Test read
285   */
286  data_buf=(char*)malloc(len+1);
287  fd=open(file01,O_RDWR);
288  rtems_test_assert(fd >= 0);
289  n=read(fd,data_buf,len);
290  rtems_test_assert(n==len);
291  status=close(fd);
292  rtems_test_assert(status==0);
293
294  EXPECT_ERROR(EACCES,open,file02,O_RDONLY);
295  EXPECT_ERROR(EACCES,open,file02,O_RDWR);
296
297  /*
298   * Test read directory
299   */
300  dp= opendir(directory01);
301  rtems_test_assert(dp==NULL);
302  rtems_test_assert(errno==EACCES);
303
304  /*
305   * Test write directory
306   */
307  status = lstat (path, &statbuf);
308  rtems_test_assert (status == 0);
309
310  status=unlink(path);
311  rtems_test_assert(status==0);
312
313
314  /*
315   * Change euid and check
316   */
317  puts("Change euid and check");
318  status=seteuid(0);
319  rtems_test_assert(status==0);
320
321  status=seteuid(another_user_id);
322  rtems_test_assert(status==0);
323
324  fd=open(file01,O_WRONLY);
325  rtems_test_assert(fd >= 0);
326  n=write(fd,test_data,len);
327  rtems_test_assert(n==len);
328  status=close(fd);
329  rtems_test_assert(status==0);
330
331  EXPECT_ERROR(EACCES,open,file02,O_WRONLY);
332  EXPECT_ERROR(EACCES,open,file02,O_RDWR);
333
334  /*
335   * Test read directory
336   */
337  dp= opendir(directory01);
338  rtems_test_assert(dp!=NULL);
339  status=closedir(dp);
340  rtems_test_assert(status==0);
341
342  /*
343   * Test write directory
344   */
345  EXPECT_ERROR(EACCES,creat,path,mode);
346  EXPECT_ERROR(EACCES,rename,path,"test");
347  EXPECT_ERROR(EACCES,truncate,path,0);
348  EXPECT_ERROR(EACCES,link,path,"test");
349  EXPECT_ERROR(EACCES,unlink,path);
350  /*
351   * Change egid and check
352   */
353  puts("Change egid and check");
354  status=seteuid(0);
355  rtems_test_assert(status==0);
356
357  status=setegid(another_group_id);
358  rtems_test_assert(status==0);
359
360  status=seteuid(another_user_id);
361  rtems_test_assert(status==0);
362
363  EXPECT_ERROR(EACCES,open,file02,O_WRONLY);
364  EXPECT_ERROR(EACCES,open,file02,O_RDONLY);
365  EXPECT_ERROR(EACCES,open,file02,O_RDWR);
366
367  /*
368   * Test read directory
369   */
370  dp= opendir(directory01);
371  rtems_test_assert(dp==NULL);
372  rtems_test_assert(errno==EACCES);
373
374   /*
375   * Test write directory
376   */
377  EXPECT_ERROR(EACCES,creat,path,mode);
378
379  /*
380   * Go back to parent directory
381   */
382  status=seteuid(0);
383  rtems_test_assert(status==0);
384  status=setegid(0);
385  rtems_test_assert(status==0);
386  free(data_buf);
387
388  status=chdir("..");
389  rtems_test_assert(status==0);
390}
391
392/*
393 * Test chown and chmod
394 */
395static void  test_premission02(void )
396{
397  struct stat statbuf;
398  int status = 0;
399  int fd;
400
401  char* file01="file01";
402  char* directory01="dir01";
403
404  mode_t tmp_mode;
405  mode_t file_mode=0321;
406
407  const char* wd=__func__;
408
409  mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ;
410  uid_t user_id =65534;
411  gid_t group_id =65534;
412
413  /*
414   *Create a new directory and change the current directory to  this
415   */
416  status=mkdir(wd,mode);
417  rtems_test_assert(status==0);
418  status=chdir(wd);
419  rtems_test_assert(status==0);
420
421  umask(00);
422
423  fd=creat(file01,mode);
424  status=close(fd);
425  rtems_test_assert(status==0);
426  status=stat(file01,&statbuf);
427  tmp_mode = (statbuf.st_mode) & ALLPERMS;
428  rtems_test_assert(tmp_mode==mode);
429
430  /*
431   *Change the file mode uid and gid
432   */
433
434  status=chmod(file01,file_mode);
435  rtems_test_assert(status==0);
436  status=chown(file01,user_id,group_id);
437  rtems_test_assert(status==0);
438  status=stat(file01,&statbuf);
439  tmp_mode = (statbuf.st_mode) & ALLPERMS;
440  rtems_test_assert(tmp_mode==file_mode);
441  rtems_test_assert(user_id==statbuf.st_uid);
442  rtems_test_assert(group_id==statbuf.st_gid);
443
444  status=mkdir(directory01,mode);
445  rtems_test_assert(status==0);
446  status=stat(directory01,&statbuf);
447  tmp_mode = (statbuf.st_mode) & ALLPERMS;
448  printf("The directory file mode is %0o\n",(unsigned int)tmp_mode);
449  rtems_test_assert(tmp_mode==mode);
450
451  /*
452   *Change the directory file mode
453   */
454  status=chmod(directory01,file_mode);
455  rtems_test_assert(status==0);
456  status=stat(directory01,&statbuf);
457  tmp_mode = (statbuf.st_mode) & ALLPERMS;
458  rtems_test_assert(tmp_mode==file_mode);
459  printf("The directory file mode is %0o\n",(unsigned int)tmp_mode);
460
461  /*
462   * Go back to parent directory
463   */
464  status=chdir("..");
465  rtems_test_assert(status==0);
466}
467static void root_test(void )
468{
469  int fd;
470  int sc;
471
472  fd =creat("test",0000);
473  sc=close(fd);
474  rtems_test_assert(sc==0);
475
476  fd=open("test",O_RDONLY);
477  rtems_test_assert(fd==-1);
478}
479
480void test(void )
481{
482  puts( "\n\n*** PERMISSION TEST ***" );
483  umask_test01();
484  test_premission01();
485  test_premission02();
486  root_test();
487  puts( "*** END OF PERMISSION TEST ***" );
488}
Note: See TracBrowser for help on using the repository browser.