1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include "securec.h"
27
28 using namespace testing::ext;
29 using namespace std;
30
31 static const char *TEST_FILE = "/data/local/tmp/test.txt";
32 mode_t MODE_0644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
33 mode_t MODE_0755 = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
34 struct stat g_statbuf;
35
36 class FchmodApiTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 private:
43 };
SetUp()44 void FchmodApiTest::SetUp()
45 {
46 }
TearDown()47 void FchmodApiTest::TearDown()
48 {
49 }
SetUpTestCase()50 void FchmodApiTest::SetUpTestCase()
51 {
52 }
TearDownTestCase()53 void FchmodApiTest::TearDownTestCase()
54 {
55 unlink(TEST_FILE);
56 }
57
58 /*
59 * @tc.number : SUB_KERNEL_SYSCALL_FCHMOD_0100
60 * @tc.name : FchmodFileModeSuccess_0001
61 * @tc.desc : fchmod change file mode bits success.
62 * @tc.size : MediumTest
63 * @tc.type : Function
64 * @tc.level : Level 1
65 */
66 HWTEST_F(FchmodApiTest, FchmodFileModeSuccess_0001, Function | MediumTest | Level1)
67 {
68 int ret = -1;
69
70 int fd = open(TEST_FILE, O_CREAT | O_RDWR, MODE_0644);
71 EXPECT_TRUE(fd > 0);
72
73 ret = fchmod(fd, MODE_0755);
74 EXPECT_EQ(ret, 0);
75
76 ret = fstat(fd, &g_statbuf);
77 EXPECT_EQ(ret, 0);
78 EXPECT_EQ((g_statbuf.st_mode & S_IXUSR), S_IXUSR);
79 EXPECT_EQ((g_statbuf.st_mode & S_IXGRP), S_IXGRP);
80 EXPECT_EQ((g_statbuf.st_mode & S_IXOTH), S_IXOTH);
81
82 close(fd);
83 }
84
85 /*
86 * @tc.number : SUB_KERNEL_SYSCALL_FCHMOD_0200
87 * @tc.name : FchmodInvalidFdModeFail_0002
88 * @tc.desc : fchmod change invalid fd mode bits fail, errno EBADF.
89 * @tc.size : MediumTest
90 * @tc.type : Function
91 * @tc.level : Level 2
92 */
93 HWTEST_F(FchmodApiTest, FchmodInvalidFdModeFail_0002, Function | MediumTest | Level2)
94 {
95 int ret = -1;
96 int invalidFd = -1;
97 errno = 0;
98 ret = fchmod(invalidFd, MODE_0644);
99 EXPECT_NE(ret, 0);
100 EXPECT_EQ(errno, EBADF);
101 }
102