1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
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 <gtest/gtest.h>
17
18 #include "create_randomaccessfile_core.h"
19
20 namespace OHOS::FileManagement::ModuleFileIO::Test {
21 using namespace testing;
22 using namespace testing::ext;
23 using namespace std;
24
25 class CreateRandomAccessFileCoreTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 };
32
SetUpTestCase(void)33 void CreateRandomAccessFileCoreTest::SetUpTestCase(void)
34 {
35 GTEST_LOG_(INFO) << "SetUpTestCase";
36 }
37
TearDownTestCase(void)38 void CreateRandomAccessFileCoreTest::TearDownTestCase(void)
39 {
40 GTEST_LOG_(INFO) << "TearDownTestCase";
41 }
42
SetUp(void)43 void CreateRandomAccessFileCoreTest::SetUp(void)
44 {
45 GTEST_LOG_(INFO) << "SetUp";
46 }
47
TearDown(void)48 void CreateRandomAccessFileCoreTest::TearDown(void)
49 {
50 GTEST_LOG_(INFO) << "TearDown";
51 }
52
53 /**
54 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001
55 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify an invalid mode.
56 * @tc.size: MEDIUM
57 * @tc.type: FUNC
58 * @tc.level Level 1
59 */
60 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001,
61 testing::ext::TestSize.Level1)
62 {
63 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001";
64
65 string path = "/test/path.txt";
66 int32_t mode = -5;
67 optional<RandomAccessFileOptions> options = nullopt;
68
69 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options);
70 EXPECT_EQ(res.IsSuccess(), false);
71
72 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_001";
73 }
74
75 /**
76 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002
77 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE.
78 * @tc.size: MEDIUM
79 * @tc.type: FUNC
80 * @tc.level Level 1
81 */
82 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002,
83 testing::ext::TestSize.Level1)
84 {
85 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002";
86
87 string path = "/test/path.txt";
88 int32_t mode = 0;
89 RandomAccessFileOptions opts;
90 opts.start = -1;
91 opts.end = 100;
92
93 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts);
94 EXPECT_EQ(res.IsSuccess(), false);
95
96 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_002";
97 }
98
99 /**
100 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003
101 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE.
102 * @tc.size: MEDIUM
103 * @tc.type: FUNC
104 * @tc.level Level 1
105 */
106 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003,
107 testing::ext::TestSize.Level1)
108 {
109 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003";
110
111 string path = "/test/path.txt";
112 int32_t mode = 0;
113 RandomAccessFileOptions opts;
114 opts.start = 10;
115 opts.end = -1;
116
117 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, opts);
118 EXPECT_EQ(res.IsSuccess(), false);
119
120 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_003";
121 }
122
123 /**'
124 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004
125 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for FALSE.
126 * @tc.size: MEDIUM
127 * @tc.type: FUNC
128 * @tc.level Level 1
129 */
130 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004,
131 testing::ext::TestSize.Level1)
132 {
133 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004";
134
135 int fd = -1;
136 optional<RandomAccessFileOptions> opts = nullopt;
137
138 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts);
139 EXPECT_EQ(res.IsSuccess(), false);
140 auto err = res.GetError();
141 EXPECT_EQ(err.GetErrNo(), 13900020);
142
143 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_004";
144 }
145
146 /**'
147 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005
148 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile to verify the path is invalid.
149 * @tc.size: MEDIUM
150 * @tc.type: FUNC
151 * @tc.level Level 1
152 */
153 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005,
154 testing::ext::TestSize.Level1)
155 {
156 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005";
157
158 string path = "";
159 int32_t mode = 0;
160 optional<RandomAccessFileOptions> options = nullopt;
161
162 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(path, mode, options);
163 EXPECT_EQ(res.IsSuccess(), false);
164
165 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_005";
166 }
167
168 /**
169 * @tc.name: CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006
170 * @tc.desc: Test function of CreateRandomAccessFileCore::DoCreateRandomAccessFile interface for SUCCESS.
171 * @tc.size: MEDIUM
172 * @tc.type: FUNC
173 * @tc.level Level 1
174 */
175 HWTEST_F(CreateRandomAccessFileCoreTest, CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006,
176 testing::ext::TestSize.Level1)
177 {
178 GTEST_LOG_(INFO) << "Test-begin CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006";
179
180 int fd = 3;
181 optional<RandomAccessFileOptions> opts = nullopt;
182
183 auto res = CreateRandomAccessFileCore::DoCreateRandomAccessFile(fd, opts);
184 EXPECT_EQ(res.IsSuccess(), true);
185
186 GTEST_LOG_(INFO) << "Test-end CreateRandomAccessFileCoreTest_DoCreateRandomAccessFile_006";
187 }
188
189 } // namespace OHOS::FileManagement::ModuleFileIO::Test