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 "utimes_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 UtimesCoreMockTest : 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 UtimesCoreMockTest::SetUpTestCase(void)
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 uvMock = std::make_shared<UvfsMock>();
39 Uvfs::ins = uvMock;
40 }
41
TearDownTestCase(void)42 void UtimesCoreMockTest::TearDownTestCase(void)
43 {
44 GTEST_LOG_(INFO) << "TearDownTestCase";
45 Uvfs::ins = nullptr;
46 uvMock = nullptr;
47 }
48
SetUp(void)49 void UtimesCoreMockTest::SetUp(void)
50 {
51 GTEST_LOG_(INFO) << "SetUp";
52 }
53
TearDown(void)54 void UtimesCoreMockTest::TearDown(void)
55 {
56 GTEST_LOG_(INFO) << "TearDown";
57 }
58
59 /**
60 * @tc.name: UtimesCoreMockTest_DoUtimes_001
61 * @tc.desc: Test function of UtimesCore::DoUtimes interface for Failed.
62 * @tc.size: MEDIUM
63 * @tc.type: FUNC
64 * @tc.level Level 1
65 */
66 HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_001, testing::ext::TestSize.Level1)
67 {
68 GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_001";
69
70 string path;
71 double mtime = 1;
72
73 EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(-1));
74 auto res = UtimesCore::DoUtimes(path, mtime);
75 EXPECT_EQ(res.IsSuccess(), false);
76
77 GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_001";
78 }
79
80 /**
81 * @tc.name: UtimesCoreMockTest_DoUtimes_002
82 * @tc.desc: Test function of UtimesCore::DoUtimes interface for FALSE.
83 * @tc.size: MEDIUM
84 * @tc.type: FUNC
85 * @tc.level Level 1
86 */
87 HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_002, testing::ext::TestSize.Level1)
88 {
89 GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_002";
90
91 string path;
92 double mtime = 1;
93
94 EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1));
95 EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(-1));
96 auto res = UtimesCore::DoUtimes(path, mtime);
97 EXPECT_EQ(res.IsSuccess(), false);
98
99 GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_002";
100 }
101
102 /**
103 * @tc.name: UtimesCoreMockTest_DoUtimes_003
104 * @tc.desc: Test function of UtimesCore::DoUtimes interface for SUCCESS.
105 * @tc.size: MEDIUM
106 * @tc.type: FUNC
107 * @tc.level Level 1
108 */
109 HWTEST_F(UtimesCoreMockTest, UtimesCoreMockTest_DoUtimes_003, testing::ext::TestSize.Level1)
110 {
111 GTEST_LOG_(INFO) << "UtimesCoreMockTest-begin UtimesCoreMockTest_DoUtimes_003";
112
113 string path;
114 double mtime = 1;
115
116 EXPECT_CALL(*uvMock, uv_fs_stat(_, _, _, _)).WillOnce(Return(1));
117 EXPECT_CALL(*uvMock, uv_fs_utime(_, _, _, _, _, _)).WillOnce(Return(1));
118 auto res = UtimesCore::DoUtimes(path, mtime);
119 EXPECT_EQ(res.IsSuccess(), true);
120
121 GTEST_LOG_(INFO) << "UtimesCoreMockTest-end UtimesCoreMockTest_DoUtimes_003";
122 }
123
124 } // namespace OHOS::FileManagement::ModuleFileIO::Test
125