• 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 <filesystem>
17 #include <fstream>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
21 #include "mkdtemp_core.h"
22 #include "uv_fs_mock.h"
23 
24 namespace OHOS::FileManagement::ModuleFileIO::Test {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace std;
28 
29 class MkdtempCoreMockTest : public testing::Test {
30 public:
31     static filesystem::path tempFilePath;
32     static void SetUpTestCase(void);
33     static void TearDownTestCase(void);
34     void SetUp();
35     void TearDown();
36     static inline shared_ptr<UvfsMock> uvMock = nullptr;
37 };
38 
39 filesystem::path MkdtempCoreMockTest::tempFilePath;
40 
SetUpTestCase(void)41 void MkdtempCoreMockTest::SetUpTestCase(void)
42 {
43     GTEST_LOG_(INFO) << "SetUpTestCase";
44     tempFilePath = filesystem::temp_directory_path() / "test";
45     std::filesystem::create_directory(tempFilePath);
46     uvMock = std::make_shared<UvfsMock>();
47     Uvfs::ins = uvMock;
48 }
49 
TearDownTestCase(void)50 void MkdtempCoreMockTest::TearDownTestCase(void)
51 {
52     GTEST_LOG_(INFO) << "TearDownTestCase";
53     filesystem::remove_all(tempFilePath);
54     Uvfs::ins = nullptr;
55     uvMock = nullptr;
56 }
57 
SetUp(void)58 void MkdtempCoreMockTest::SetUp(void)
59 {
60     GTEST_LOG_(INFO) << "SetUp";
61 }
62 
TearDown(void)63 void MkdtempCoreMockTest::TearDown(void)
64 {
65     GTEST_LOG_(INFO) << "TearDown";
66 }
67 
68 /**
69  * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0001
70  * @tc.desc: Test function of DoMkdtemp() interface for SUCCESS.
71  * @tc.size: MEDIUM
72  * @tc.type: FUNC
73  * @tc.level Level 1
74  */
75 HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0001, testing::ext::TestSize.Level1)
76 {
77     GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0001";
78 
79     uv_fs_t mock_req;
80     mock_req.path = const_cast<char *>("/data/local/tmp/test/XXXXXX");
81 
82     EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _))
__anonc535f0580102(uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) 83         .WillOnce(Invoke([&](uv_loop_t*, uv_fs_t* req, const char*, uv_fs_cb) {
84             *req = mock_req;
85             return 0;
86         }));
87 
88     auto ret = MkdtempCore::DoMkdtemp("/data/local/tmp/test/XXXXXX");
89     EXPECT_EQ(ret.IsSuccess(), true);
90 
91     GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0001";
92 }
93 
94 /**
95  * @tc.name: MkdtempCoreMockTest_DoMkdtemp_0002
96  * @tc.desc: Test function of DoMkdtemp() interface for FAILED.
97  * @tc.size: MEDIUM
98  * @tc.type: FUNC
99  * @tc.level Level 1
100  */
101 HWTEST_F(MkdtempCoreMockTest, MkdtempCoreMockTest_DoMkdtemp_0002, testing::ext::TestSize.Level1)
102 {
103     GTEST_LOG_(INFO) << "MkdtempCoreMockTest-begin MkdtempCoreMockTest_DoMkdtemp_0002";
104 
105     string path = tempFilePath.string() + "/XXXXXX";
106 
107     EXPECT_CALL(*uvMock, uv_fs_mkdtemp(_, _, _, _)).WillOnce(Return(-1));
108     auto ret = MkdtempCore::DoMkdtemp(path);
109     EXPECT_EQ(ret.IsSuccess(), false);
110 
111     GTEST_LOG_(INFO) << "MkdtempCoreMockTest-end MkdtempCoreMockTest_DoMkdtemp_0002";
112 }
113 
114 } // namespace OHOS::FileManagement::ModuleFileIO::Test