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 "read_core.h"
17 #include "uv_fs_mock.h"
18
19 #include <cstdint>
20 #include <climits>
21 #include <gtest/gtest.h>
22
23 namespace OHOS::FileManagement::ModuleFileIO::Test {
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace std;
27
28 class ReadCoreMockTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 static inline std::shared_ptr<UvfsMock> uvMock = nullptr;
35 };
36
SetUpTestCase(void)37 void ReadCoreMockTest::SetUpTestCase(void)
38 {
39 GTEST_LOG_(INFO) << "SetUpTestCase";
40 uvMock = std::make_shared<UvfsMock>();
41 Uvfs::ins = uvMock;
42 }
43
TearDownTestCase(void)44 void ReadCoreMockTest::TearDownTestCase(void)
45 {
46 GTEST_LOG_(INFO) << "TearDownTestCase";
47 Uvfs::ins = nullptr;
48 uvMock = nullptr;
49 }
50
SetUp(void)51 void ReadCoreMockTest::SetUp(void)
52 {
53 GTEST_LOG_(INFO) << "SetUp";
54 }
55
TearDown(void)56 void ReadCoreMockTest::TearDown(void)
57 {
58 GTEST_LOG_(INFO) << "TearDown";
59 }
60
61 /**
62 * @tc.name: ReadCoreMockTest_DoRead_001
63 * @tc.desc: Test function of ReadCore::DoRead interface for FALSE.
64 * @tc.size: MEDIUM
65 * @tc.type: FUNC
66 * @tc.level Level 1
67 */
68 HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_001, testing::ext::TestSize.Level1)
69 {
70 GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_001";
71
72 int32_t fd = 1;
73 void *buf = nullptr;
74 ArrayBuffer arrayBuffer(buf, 0);
75
76 EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(-1));
77
78 auto res = ReadCore::DoRead(fd, arrayBuffer);
79 EXPECT_EQ(res.IsSuccess(), false);
80
81 GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_001";
82 }
83
84 /**
85 * @tc.name: ReadCoreMockTest_DoRead_002
86 * @tc.desc: Test function of ReadCore::DoRead interface for SUCCESS.
87 * @tc.size: MEDIUM
88 * @tc.type: FUNC
89 * @tc.level Level 1
90 */
91 HWTEST_F(ReadCoreMockTest, ReadCoreMockTest_DoRead_002, testing::ext::TestSize.Level1)
92 {
93 GTEST_LOG_(INFO) << "ReadCoreMockTest-begin ReadCoreMockTest_DoRead_002";
94
95 int32_t fd = 1;
96 void *buf = nullptr;
97 ArrayBuffer arrayBuffer(buf, 0);
98
99 EXPECT_CALL(*uvMock, uv_fs_read(_, _, _, _, _, _, _)).WillOnce(Return(1));
100
101 auto res = ReadCore::DoRead(fd, arrayBuffer);
102 EXPECT_EQ(res.IsSuccess(), true);
103
104 GTEST_LOG_(INFO) << "ReadCoreMockTest-end ReadCoreMockTest_DoRead_002";
105 }
106
107 } // namespace OHOS::FileManagement::ModuleFileIO::Test