• 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 "assistant.h"
20 #include "file_utils.h"
21 
22 namespace OHOS::FileManagement {
23 using namespace testing;
24 using namespace testing::ext;
25 
26 class FileRangeLockTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32     static inline std::shared_ptr<CloudDisk::AssistantMock> insMock = nullptr;
33 };
34 
SetUpTestCase(void)35 void FileRangeLockTest::SetUpTestCase(void)
36 {
37     GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39 
TearDownTestCase(void)40 void FileRangeLockTest::TearDownTestCase(void)
41 {
42     GTEST_LOG_(INFO) << "TearDownTestCase";
43 }
44 
SetUp(void)45 void FileRangeLockTest::SetUp(void)
46 {
47     GTEST_LOG_(INFO) << "SetUp";
48     insMock = std::make_shared<CloudDisk::AssistantMock>();
49     CloudDisk::Assistant::ins = insMock;
50 }
51 
TearDown(void)52 void FileRangeLockTest::TearDown(void)
53 {
54     GTEST_LOG_(INFO) << "TearDown";
55     CloudDisk::Assistant::ins = nullptr;
56     insMock = nullptr;
57 }
58 
59 /**
60  * @tc.name: FilePosLockTest001
61  * @tc.desc: Verify the FilePosLock function
62  * @tc.type: FUNC
63  */
64 HWTEST_F(FileRangeLockTest, FilePosLockTest001, TestSize.Level1)
65 {
66     GTEST_LOG_(INFO) << "FilePosLockTest001 Start";
67     try {
68         int fd = 0;
69         off_t offset = 0;
70         size_t size = 4096;
71         EXPECT_CALL(*insMock, fcntl(_, _)).WillOnce(Return(-1));
72         int ret = FileRangeLock::FilePosLock(fd, offset, size, F_WRLCK);
73         EXPECT_NE(ret, 0);
74     } catch (...) {
75         EXPECT_TRUE(false);
76         GTEST_LOG_(INFO) << "FilePosLockTest001 ERROR";
77     }
78     GTEST_LOG_(INFO) << "FilePosLockTest001 End";
79 }
80 
81 /**a
82  * @tc.name: FilePosLockTest002
83  * @tc.desc: Verify the FilePosLock function
84  * @tc.type: FUNC
85  */
86 HWTEST_F(FileRangeLockTest, FilePosLockTest002, TestSize.Level1)
87 {
88     GTEST_LOG_(INFO) << "FilePosLockTest002 Start";
89     try {
90         int fd = 0;
91         off_t offset = 0;
92         size_t size = 4096;
93         EXPECT_CALL(*insMock, fcntl(_, _)).WillOnce(Return(0));
94         int ret = FileRangeLock::FilePosLock(fd, offset, size, F_WRLCK);
95         EXPECT_EQ(ret, 0);
96     } catch (...) {
97         EXPECT_TRUE(false);
98         GTEST_LOG_(INFO) << "FilePosLockTest002 ERROR";
99     }
100     GTEST_LOG_(INFO) << "FilePosLockTest002 End";
101 }
102 }