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 "fsync_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 FsyncCoreMockTest : 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> uvfs = nullptr;
33 };
34
SetUpTestCase(void)35 void FsyncCoreMockTest::SetUpTestCase(void)
36 {
37 uvfs = std::make_shared<UvfsMock>();
38 Uvfs::ins = uvfs;
39 GTEST_LOG_(INFO) << "SetUpTestCase";
40 }
41
TearDownTestCase(void)42 void FsyncCoreMockTest::TearDownTestCase(void)
43 {
44 Uvfs::ins = nullptr;
45 uvfs = nullptr;
46 GTEST_LOG_(INFO) << "TearDownTestCase";
47 }
48
SetUp(void)49 void FsyncCoreMockTest::SetUp(void)
50 {
51 GTEST_LOG_(INFO) << "SetUp";
52 }
53
TearDown(void)54 void FsyncCoreMockTest::TearDown(void)
55 {
56 GTEST_LOG_(INFO) << "TearDown";
57 }
58
59 /**
60 * @tc.name: FsyncCoreMockTest_DoFsync_001
61 * @tc.desc: Test function of RenameCore::DoFsync interface for FALSE.
62 * @tc.size: MEDIUM
63 * @tc.type: FUNC
64 * @tc.level Level 1
65 */
66 HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_001, testing::ext::TestSize.Level1)
67 {
68 GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_001";
69
70 EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(-1));
71
72 auto res = FsyncCore::DoFsync(1);
73 EXPECT_EQ(res.IsSuccess(), false);
74
75 GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_001";
76 }
77
78 /**
79 * @tc.name: FsyncCoreMockTest_DoFsync_002
80 * @tc.desc: Test function of RenameCore::DoFsync interface for SUCCESS.
81 * @tc.size: MEDIUM
82 * @tc.type: FUNC
83 * @tc.level Level 1
84 */
85 HWTEST_F(FsyncCoreMockTest, FsyncCoreMockTest_DoFsync_002, testing::ext::TestSize.Level1)
86 {
87 GTEST_LOG_(INFO) << "FsyncCoreMockTest-begin FsyncCoreMockTest_DoFsync_002";
88
89 EXPECT_CALL(*uvfs, uv_fs_fsync(_, _, _, _)).WillOnce(Return(1));
90
91 auto res = FsyncCore::DoFsync(1);
92 EXPECT_EQ(res.IsSuccess(), true);
93
94 GTEST_LOG_(INFO) << "FsyncCoreMockTest-end FsyncCoreMockTest_DoFsync_002";
95 }
96
97
98 } // namespace OHOS::FileManagement::ModuleFileIO::Test