generic/486: Get rid of the redundant error=%d printing
[xfstests-dev.git] / src / fs_perms.c
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2000
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 /*
21  *  FILE(s)     : fs_perms.c simpletest.sh textx.o Makefile README
22  *  DESCRIPTION : Regression test for Linux filesystem permissions.
23  *  AUTHOR      : Jeff Martin (martinjn@us.ibm.com)
24  *  HISTORY     : 
25  *     (04/12/01)v.99  First attempt at using C for fs-regression test.  Only tests read and write bits.
26  *     (04/19/01)v1.0  Added test for execute bit.
27  *     (05/23/01)v1.1  Added command line parameter to specify test file.
28  *     (07/12/01)v1.2  Removed conf file and went to command line parameters.
29  *
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <sys/wait.h>
40 #include <time.h>
41 #include <utime.h>
42
43 int testsetup(mode_t mode, int cuserId, int cgroupId);
44 int testfperm(int userId, int groupId, char* fperm);
45
46 int main( int argc, char *argv[]) {
47    char *fperm;
48    int result, exresult=0,  cuserId=0, cgroupId=0, userId=0, groupId=0;
49    mode_t mode;
50
51    switch (argc) {
52       case 8:
53               mode = strtol(argv[1],(char**)NULL,010);
54               cuserId = atoi(argv[2]);
55               cgroupId = atoi(argv[3]);
56               userId = atoi(argv[4]);
57               groupId = atoi(argv[5]);
58               fperm = argv[6];
59               exresult = atoi(argv[7]);
60               break;
61       default:
62               printf("Usage: %s <mode of file> <UID of file> <GID of file> <UID of tester> <GID of tester> <permission to test r|w|x|t|T> <expected result as 0|1>\n",argv[0]);
63               exit(0);
64    }
65
66    testsetup(mode,cuserId,cgroupId);
67    result=testfperm(userId,groupId,fperm);
68    system("rm -f test.file");
69    printf("%s a %03o file owned by (%d/%d) as user/group(%d/%d)  ",fperm,mode,cuserId,cgroupId,userId,groupId);
70    if (result == exresult) {
71       printf("PASS\n");
72       exit(0);
73       }
74    else {
75       printf("FAIL\n");
76       exit(1);
77       }
78 }
79
80 int testsetup(mode_t mode, int cuserId, int cgroupId) {
81    system("cp testx.file test.file");
82    chmod("test.file",mode);
83    chown("test.file",cuserId,cgroupId);
84    return(0);
85 }
86
87 int testfperm(int userId, int groupId, char* fperm) {
88
89     int ret;
90
91     /*  SET CURRENT USER/GROUP PERMISSIONS */
92     ret = -1;
93     if(setegid(groupId)) {
94         printf("could not setegid to %d.\n",groupId);
95         goto out;
96     }
97     if(seteuid(userId)) {
98         printf("could not seteuid to %d.\n",userId);
99         goto out;
100     }
101
102     if (!strcmp("x", fperm)) {
103         int status;
104         pid_t pid;
105
106         pid = fork();
107         if (pid == 0) {
108             execlp("./test.file","test.file",NULL);
109             exit(0);
110         }
111         wait(&status);
112         ret = WEXITSTATUS(status);
113     } else if (!strcmp("t", fperm)) {
114         ret = utime("test.file", NULL) ? 0 : 1;
115     } else if (!strcmp("T", fperm)) {
116         time_t now = time(NULL);
117         struct utimbuf times = {
118                 .actime = now - 1,
119                 .modtime = now - 1
120         };
121
122         ret = utime("test.file", &times) ? 0 : 1;
123     } else {
124         FILE *file;
125
126         if((file = fopen("test.file",fperm))){
127             fclose(file);
128             ret = 1;
129             goto out;
130         } else {
131             ret = 0;
132             goto out;
133         }
134     }
135
136 out:
137     seteuid(0);
138     setegid(0);
139     return ret;
140 }