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

4.115
Last change on this file since a7c39d3 was a7c39d3, checked in by Joel Sherrill <joel.sherrill@…>, on 08/01/11 at 21:54:19

2011-08-01 Xiang Cui <medivhc@…>

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