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