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 <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <string>
20 #include <unistd.h>
21 #include <vector>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/xattr.h>
26 #include "securec.h"
27 #include <linux/io_uring.h>
28 #include <sys/mman.h>
29 #include <sys/syscall.h>
30
io_uring_setup(unsigned entries,struct io_uring_params * p)31 static int io_uring_setup(unsigned entries,struct io_uring_params *p){
32 return syscall(__NR_io_uring_setup,entries,p);
33 }
34
35 using namespace testing::ext;
36
37 static const char *TEST_VALID_PATH = "/data/local/tmp/attr_test_dir";
38 static const char *TEST_VALID_FILE = "/data/local/tmp/attr_test_dir/attr_test.txt";
39
40 class HatsSetupTest:public testing::Test{
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 private:
47 };
48
SetUp()49 void HatsSetupTest::SetUp()
50 {
51 int fd = -1;
52 if (access(TEST_VALID_PATH, F_OK) == 0) {
53 (void)remove(TEST_VALID_PATH);
54 }
55 (void)mkdir(TEST_VALID_PATH, S_IWUSR | S_IRUSR | S_IXUSR);
56 fd = open(TEST_VALID_FILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
57 close(fd);
58 }
TearDown()59 void HatsSetupTest::TearDown()
60 {
61 (void)remove(TEST_VALID_FILE);
62 if (access(TEST_VALID_PATH, F_OK) == 0) {
63 (void)remove(TEST_VALID_PATH);
64 }
65 }
SetUpTestCase()66 void HatsSetupTest::SetUpTestCase()
67 {
68 static io_uring_params p = {0};
69 io_uring_setup(1, &p);
70 if (errno == ENOSYS) {
71 GTEST_SKIP() << "Not support wearable, skip testCase";
72 }
73 }
TearDownTestCase()74 void HatsSetupTest::TearDownTestCase()
75 {
76 }
77
78 /**
79 * @tc.number : SUB_BASIC_FM_USE_IOURING_SETUP_0100
80 * @tc.name : SetupApiTestSuccess_0001
81 * @tc.desc : RdbStore backup and restore callback test
82 * @tc.size : MediumTest
83 * @tc.type : Function
84 * @tc.level : Level 1
85 */
86 HWTEST_F(HatsSetupTest, SetupApiTestSuccess_0001, Function | MediumTest | Level1)
87 {
88 static io_uring_params p = {0};
89
90 int uringFd01 = io_uring_setup(1, &p);
91 EXPECT_TRUE(uringFd01 > 0);
92
93 int uringFd02 = io_uring_setup(2, &p);
94 EXPECT_TRUE(uringFd02 > 0);
95
96 int uringFd03 = io_uring_setup(8, &p);
97 EXPECT_TRUE(uringFd03 > 0);
98
99 int uringFd04 = io_uring_setup(32767, &p);
100 EXPECT_TRUE(uringFd04 > 0);
101
102 int uringFd05 = io_uring_setup(32768, &p);
103 EXPECT_TRUE(uringFd05 > 0);
104 }
105
106 /**
107 * @tc.number : SUB_BASIC_FM_USE_IOURING_SETUP_0200
108 * @tc.name : SetupApiTest_params_null_0002
109 * @tc.desc : RdbStore backup and restore callback test
110 * @tc.size : MediumTest
111 * @tc.type : Function
112 * @tc.level : Level 1
113 */
114 HWTEST_F(HatsSetupTest, SetupApiTest_params_null_0002, Function | MediumTest | Level1)
115 {
116 io_uring_setup(1, nullptr);
117 EXPECT_EQ(errno, 14);
118 }
119
120 /**
121 * @tc.number : SUB_BASIC_FM_USE_IOURING_SETUP_0300
122 * @tc.name : SetupApiTestFail_0003
123 * @tc.desc : RdbStore backup and restore callback test
124 * @tc.size : MediumTest
125 * @tc.type : Function
126 * @tc.level : Level 1
127 */
128 HWTEST_F(HatsSetupTest, SetupApiTestFail_0003, Function | MediumTest | Level1)
129 {
130 static io_uring_params p = {0};
131
132 int uringFd01 = io_uring_setup(-1, &p);
133 EXPECT_EQ(uringFd01, -1);
134 EXPECT_EQ(errno, 22);
135
136 int uringFd02 = io_uring_setup(0, &p);
137 EXPECT_EQ(uringFd02, -1);
138 EXPECT_EQ(errno, 22);
139
140 int uringFd03 = io_uring_setup(32769, &p);
141 EXPECT_EQ(uringFd03, -1);
142 EXPECT_EQ(errno, 22);
143 }
144
145 /**
146 * @tc.number : SUB_BASIC_FM_USE_IOURING_SETUP_0400
147 * @tc.name : SetupApiTest_params_int_0004
148 * @tc.desc : RdbStore backup and restore callback test
149 * @tc.size : MediumTest
150 * @tc.type : Function
151 * @tc.level : Level 1
152 */
153 HWTEST_F(HatsSetupTest,SetupApiTest_params_int_0004,Function | MediumTest | Level1)
154 {
155 static io_uring_params params;
156 memset_s(¶ms, sizeof(int), 0, sizeof(int));
157 io_uring_setup(8, ¶ms);
158 EXPECT_EQ(errno, 2);
159 }