generic/470: use thin volume for dmlogwrites target device
[xfstests-dev.git] / src / fs_perms.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *   Copyright (c) International Business Machines  Corp., 2000
4  */
5
6 /*
7  *  FILE(s)     : fs_perms.c simpletest.sh textx.o Makefile README
8  *  DESCRIPTION : Regression test for Linux filesystem permissions.
9  *  AUTHOR      : Jeff Martin (martinjn@us.ibm.com)
10  *  HISTORY     : 
11  *     (04/12/01)v.99  First attempt at using C for fs-regression test.  Only tests read and write bits.
12  *     (04/19/01)v1.0  Added test for execute bit.
13  *     (05/23/01)v1.1  Added command line parameter to specify test file.
14  *     (07/12/01)v1.2  Removed conf file and went to command line parameters.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/wait.h>
26 #include <time.h>
27 #include <utime.h>
28
29 int testsetup(mode_t mode, int cuserId, int cgroupId);
30 int testfperm(int userId, int groupId, char* fperm);
31
32 int main( int argc, char *argv[]) {
33    char *fperm;
34    int result, exresult=0,  cuserId=0, cgroupId=0, userId=0, groupId=0;
35    mode_t mode;
36
37    switch (argc) {
38       case 8:
39               mode = strtol(argv[1],(char**)NULL,010);
40               cuserId = atoi(argv[2]);
41               cgroupId = atoi(argv[3]);
42               userId = atoi(argv[4]);
43               groupId = atoi(argv[5]);
44               fperm = argv[6];
45               exresult = atoi(argv[7]);
46               break;
47       default:
48               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]);
49               exit(0);
50    }
51
52    testsetup(mode,cuserId,cgroupId);
53    result=testfperm(userId,groupId,fperm);
54    system("rm -f test.file");
55    printf("%s a %03o file owned by (%d/%d) as user/group(%d/%d)  ",fperm,mode,cuserId,cgroupId,userId,groupId);
56    if (result == exresult) {
57       printf("PASS\n");
58       exit(0);
59       }
60    else {
61       printf("FAIL\n");
62       exit(1);
63       }
64 }
65
66 int testsetup(mode_t mode, int cuserId, int cgroupId) {
67    system("cp testx.file test.file");
68    chmod("test.file",mode);
69    chown("test.file",cuserId,cgroupId);
70    return(0);
71 }
72
73 int testfperm(int userId, int groupId, char* fperm) {
74
75     int ret;
76
77     /*  SET CURRENT USER/GROUP PERMISSIONS */
78     ret = -1;
79     if(setegid(groupId)) {
80         printf("could not setegid to %d.\n",groupId);
81         goto out;
82     }
83     if(seteuid(userId)) {
84         printf("could not seteuid to %d.\n",userId);
85         goto out;
86     }
87
88     if (!strcmp("x", fperm)) {
89         int status;
90         pid_t pid;
91
92         pid = fork();
93         if (pid == 0) {
94             execlp("./test.file","test.file",NULL);
95             exit(0);
96         }
97         wait(&status);
98         ret = WEXITSTATUS(status);
99     } else if (!strcmp("t", fperm)) {
100         ret = utime("test.file", NULL) ? 0 : 1;
101     } else if (!strcmp("T", fperm)) {
102         time_t now = time(NULL);
103         struct utimbuf times = {
104                 .actime = now - 1,
105                 .modtime = now - 1
106         };
107
108         ret = utime("test.file", &times) ? 0 : 1;
109     } else {
110         FILE *file;
111
112         if((file = fopen("test.file",fperm))){
113             fclose(file);
114             ret = 1;
115             goto out;
116         } else {
117             ret = 0;
118             goto out;
119         }
120     }
121
122 out:
123     seteuid(0);
124     setegid(0);
125     return ret;
126 }