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