• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "file_entity.h"
20 #include "fs_randomaccessfile.h"
21 #include "uv_fs_mock.h"
22 
23 
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28 
29 class FsRandomAccessFileMockTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     static inline shared_ptr<UvfsMock> uvMock = nullptr;
36 protected:
37     unique_ptr<RandomAccessFileEntity> rafEntity;
38     unique_ptr<FsRandomAccessFile> raf;
39 };
40 
SetUpTestCase(void)41 void FsRandomAccessFileMockTest::SetUpTestCase(void)
42 {
43     uvMock = make_shared<UvfsMock>();
44     Uvfs::ins = uvMock;
45     GTEST_LOG_(INFO) << "SetUpTestCase";
46 }
47 
TearDownTestCase(void)48 void FsRandomAccessFileMockTest::TearDownTestCase(void)
49 {
50     Uvfs::ins = nullptr;
51     uvMock = nullptr;
52     GTEST_LOG_(INFO) << "TearDownTestCase";
53 }
54 
SetUp(void)55 void FsRandomAccessFileMockTest::SetUp(void)
56 {
57     GTEST_LOG_(INFO) << "SetUp";
58     rafEntity = make_unique<RandomAccessFileEntity>();
59     const int fdValue = 3;
60     const bool isClosed = false;
61     rafEntity->fd = make_unique<DistributedFS::FDGuard>(fdValue, isClosed);
62     rafEntity->filePointer = 0;
63     raf = make_unique<FsRandomAccessFile>(move(rafEntity));
64 }
65 
TearDown(void)66 void FsRandomAccessFileMockTest::TearDown(void)
67 {
68     GTEST_LOG_(INFO) << "TearDown";
69 }
70 
71 /**
72  * @tc.name: FsRandomAccessFileMockTest_ReadSync_001
73  * @tc.desc: Test function of ReadSync() interface for is failed for options is nullopt.
74  * @tc.size: MEDIUM
75  * @tc.type: FUNC
76  * @tc.level Level 1
77 
78 */
79 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_001, TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_001";
82 
83     ArrayBuffer buffer(malloc(100), 100);
84     EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1));
85     auto result = raf->ReadSync(buffer, nullopt);
86     EXPECT_EQ(result.IsSuccess(), false);
87     free(buffer.buf);
88 
89     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_001";
90 }
91 
92 /**
93  * @tc.name: FsRandomAccessFileMockTest_ReadSync_002
94  * @tc.desc: Test function of ReadSync() interface for SUCCESS.
95  * @tc.size: MEDIUM
96  * @tc.type: FUNC
97  * @tc.level Level 1
98 
99 */
100 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_ReadSync_002, TestSize.Level1)
101 {
102     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_ReadSync_002";
103 
104     ArrayBuffer buffer(malloc(100), 100);
105     ReadOptions options;
106     options.offset = 10;
107     options.length = 10;
108     raf->rafEntity->filePointer = 20;
109 
110     EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(0));
111     auto result = raf->ReadSync(buffer, options);
112     EXPECT_EQ(result.IsSuccess(), true);
113     free(buffer.buf);
114 
115     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_ReadSync_002";
116 }
117 
118 /**
119  * @tc.name: FsRandomAccessFileMockTest_WriteSync_003
120  * @tc.desc: Test function of WriteSync() interface for is failed for options is nullopt.
121  * @tc.size: MEDIUM
122  * @tc.type: FUNC
123  * @tc.level Level 1
124 
125 */
126 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_003, TestSize.Level1)
127 {
128     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_003";
129 
130     string data = "test data";
131     EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1));
132     auto result = raf->WriteSync(data, nullopt);
133     EXPECT_EQ(result.IsSuccess(), false);
134 
135     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_003";
136 }
137 
138 /**
139  * @tc.name: FsRandomAccessFileMockTest_WriteSync_004
140  * @tc.desc: Test function of WriteSync() interface for SUCCESS.
141  * @tc.size: MEDIUM
142  * @tc.type: FUNC
143  * @tc.level Level 1
144 
145 */
146 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_004, TestSize.Level1)
147 {
148     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_004";
149 
150     string data = "test data";
151     WriteOptions options;
152     options.length = 4;
153     options.offset = 0;
154 
155     EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0));
156     auto result = raf->WriteSync(data, options);
157     EXPECT_EQ(result.IsSuccess(), true);
158 
159     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_004";
160 }
161 
162 /**
163  * @tc.name: FsRandomAccessFileMockTest_WriteSync_005
164  * @tc.desc: Test function of WriteSync() interface for is failed for uv_fs_write() return -1.
165  * @tc.size: MEDIUM
166  * @tc.type: FUNC
167  * @tc.level Level 1
168 
169 */
170 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_005, TestSize.Level1)
171 {
172     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_005";
173 
174     ArrayBuffer buffer(malloc(100), 100);
175     WriteOptions options;
176     options.length = 4;
177     options.offset = 0;
178 
179     EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(-1));
180     auto result = raf->WriteSync(buffer, options);
181     EXPECT_EQ(result.IsSuccess(), false);
182     free(buffer.buf);
183 
184     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_005";
185 }
186 
187 /**
188  * @tc.name: FsRandomAccessFileMockTest_WriteSync_006
189  * @tc.desc: Test function of WriteSync() interface for SUCCESS.
190  * @tc.size: MEDIUM
191  * @tc.type: FUNC
192  * @tc.level Level 1
193 
194 */
195 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_WriteSync_006, TestSize.Level1)
196 {
197     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_WriteSync_006";
198 
199     ArrayBuffer buffer(malloc(100), 100);
200     WriteOptions options;
201     options.length = 4;
202     options.offset = 0;
203 
204     EXPECT_CALL(*uvMock, uv_fs_write(_, _, _, _, _, _, _)).WillOnce(Return(0));
205     auto result = raf->WriteSync(buffer, options);
206     EXPECT_EQ(result.IsSuccess(), true);
207     free(buffer.buf);
208 
209     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_WriteSync_006";
210 }
211 
212 /**
213  * @tc.name: FsRandomAccessFileMockTest_CloseSync_007
214  * @tc.desc: Test function of CloseSync() interface for is failed for uv_fs_write() return -1.
215  * @tc.size: MEDIUM
216  * @tc.type: FUNC
217  * @tc.level Level 1
218 
219 */
220 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_007, TestSize.Level1)
221 {
222     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_007";
223 
224     EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(-1));
225     auto result = raf->CloseSync();
226     EXPECT_EQ(result.IsSuccess(), false);
227 
228     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_007";
229 }
230 
231 /**
232  * @tc.name: FsRandomAccessFileMockTest_CloseSync_008
233  * @tc.desc: Test function of CloseSync() interface for SUCCESS.
234  * @tc.size: MEDIUM
235  * @tc.type: FUNC
236  * @tc.level Level 1
237 
238 */
239 HWTEST_F(FsRandomAccessFileMockTest, FsRandomAccessFileMockTest_CloseSync_008, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-begin FsRandomAccessFileMockTest_CloseSync_008";
242 
243     EXPECT_CALL(*uvMock, uv_fs_close(_, _, _, _)).WillOnce(Return(0));
244     auto result = raf->CloseSync();
245     EXPECT_EQ(result.IsSuccess(), true);
246 
247     GTEST_LOG_(INFO) << "FsRandomAccessFileMockTest-end FsRandomAccessFileMockTest_CloseSync_008";
248 }
249 
250 }