xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/testcases/sanitizer.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li // A binary that tests for opened or closed file descriptors as specified.
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #include <fcntl.h>
18*ec63e07aSXin Li #include <linux/fs.h>
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include <cerrno>
21*ec63e07aSXin Li #include <cstdio>
22*ec63e07aSXin Li #include <cstdlib>
23*ec63e07aSXin Li #include <cstring>
24*ec63e07aSXin Li 
25*ec63e07aSXin Li // The fd provided should not be open.
TestClosedFd(int fd)26*ec63e07aSXin Li void TestClosedFd(int fd) {
27*ec63e07aSXin Li   int ret = fcntl(fd, F_GETFD);
28*ec63e07aSXin Li   if (ret != -1) {
29*ec63e07aSXin Li     printf("FAILURE: FD:%d is not closed\n", fd);
30*ec63e07aSXin Li     exit(EXIT_FAILURE);
31*ec63e07aSXin Li   }
32*ec63e07aSXin Li   if (errno != EBADF) {
33*ec63e07aSXin Li     printf(
34*ec63e07aSXin Li         "FAILURE: fcntl(%d) returned errno=%d (%s), should have "
35*ec63e07aSXin Li         "errno=EBADF/%d (%s)\n",
36*ec63e07aSXin Li         fd, errno, strerror(errno), EBADF, strerror(EBADF));
37*ec63e07aSXin Li     exit(EXIT_FAILURE);
38*ec63e07aSXin Li   }
39*ec63e07aSXin Li }
40*ec63e07aSXin Li 
41*ec63e07aSXin Li // The fd provided should be open.
TestOpenFd(int fd)42*ec63e07aSXin Li void TestOpenFd(int fd) {
43*ec63e07aSXin Li   int ret = fcntl(fd, F_GETFD);
44*ec63e07aSXin Li   if (ret == -1) {
45*ec63e07aSXin Li     printf("FAILURE: fcntl(%d) returned -1 with errno=%d (%s)\n", fd, errno,
46*ec63e07aSXin Li            strerror(errno));
47*ec63e07aSXin Li     exit(EXIT_FAILURE);
48*ec63e07aSXin Li   }
49*ec63e07aSXin Li }
50*ec63e07aSXin Li 
main(int argc,char * argv[])51*ec63e07aSXin Li int main(int argc, char* argv[]) {
52*ec63e07aSXin Li   // Disable caching.
53*ec63e07aSXin Li   setbuf(stdin, nullptr);
54*ec63e07aSXin Li   setbuf(stdout, nullptr);
55*ec63e07aSXin Li   setbuf(stderr, nullptr);
56*ec63e07aSXin Li 
57*ec63e07aSXin Li   for (int i = 0; i <= INR_OPEN_MAX; i++) {
58*ec63e07aSXin Li     bool should_be_closed = true;
59*ec63e07aSXin Li     for (int j = 0; j < argc; j++) {
60*ec63e07aSXin Li       errno = 0;
61*ec63e07aSXin Li       int cur_fd = strtol(argv[j], nullptr, 10);  // NOLINT
62*ec63e07aSXin Li       if (errno != 0) {
63*ec63e07aSXin Li         printf("FAILURE: strtol('%s') failed\n", argv[j]);
64*ec63e07aSXin Li         exit(EXIT_FAILURE);
65*ec63e07aSXin Li       }
66*ec63e07aSXin Li       if (i == cur_fd) {
67*ec63e07aSXin Li         should_be_closed = false;
68*ec63e07aSXin Li       }
69*ec63e07aSXin Li     }
70*ec63e07aSXin Li 
71*ec63e07aSXin Li     printf("%d:%c ", i, should_be_closed ? 'C' : 'O');
72*ec63e07aSXin Li 
73*ec63e07aSXin Li     if (should_be_closed) {
74*ec63e07aSXin Li       TestClosedFd(i);
75*ec63e07aSXin Li     } else {
76*ec63e07aSXin Li       TestOpenFd(i);
77*ec63e07aSXin Li     }
78*ec63e07aSXin Li   }
79*ec63e07aSXin Li 
80*ec63e07aSXin Li   printf("OK: All tests went OK\n");
81*ec63e07aSXin Li   return EXIT_SUCCESS;
82*ec63e07aSXin Li }
83