1 /*
2  * Copyright (c) 2023 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 "cloud_file_daemon_interface_code.h"
17 #include "ipc/cloud_daemon_stub.h"
18 #include "utils_log.h"
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 namespace OHOS::FileManagement::CloudFile::Test {
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace std;
27 
28 class MockCloudDaemonStub : public CloudDaemonStub {
29 public:
30     MOCK_METHOD3(StartFuse, int32_t(int32_t userId, int32_t deviceFd, const std::string &path));
31 };
32 
33 class CloudDaemonStubTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39     std::shared_ptr<CloudDaemonStub> cloudDaemonStub_;
40 };
41 
SetUpTestCase(void)42 void CloudDaemonStubTest::SetUpTestCase(void)
43 {
44     GTEST_LOG_(INFO) << "SetUpTestCase";
45 }
46 
TearDownTestCase(void)47 void CloudDaemonStubTest::TearDownTestCase(void)
48 {
49     GTEST_LOG_(INFO) << "TearDownTestCase";
50 }
51 
SetUp(void)52 void CloudDaemonStubTest::SetUp(void)
53 {
54     cloudDaemonStub_ = std::make_shared<MockCloudDaemonStub>();
55     GTEST_LOG_(INFO) << "SetUp";
56 }
57 
TearDown(void)58 void CloudDaemonStubTest::TearDown(void)
59 {
60     GTEST_LOG_(INFO) << "TearDown";
61 }
62 
63 /**
64  * @tc.name: OnRemoteRequestTest001
65  * @tc.desc: Verify the OnRemoteRequest function
66  * @tc.type: FUNC
67  * @tc.require: I6H5MH
68  */
69 HWTEST_F(CloudDaemonStubTest, OnRemoteRequestTest001, TestSize.Level1)
70 {
71     GTEST_LOG_(INFO) << "OnRemoteRequestTest Start";
72     try {
73         MessageParcel data;
74         MessageParcel reply;
75         MessageOption option;
76         int ret = cloudDaemonStub_->OnRemoteRequest(
77             static_cast<uint32_t>(CloudFileDaemonInterfaceCode::CLOUD_DAEMON_CMD_START_FUSE), data, reply, option);
78         EXPECT_EQ(ret, ICloudDaemon::CLOUD_DAEMON_DESCRIPTOR_IS_EMPTY);
79     } catch (...) {
80         EXPECT_TRUE(false);
81         GTEST_LOG_(INFO) << "OnRemoteRequestTest  ERROR";
82     }
83     GTEST_LOG_(INFO) << "OnRemoteRequestTest End";
84 }
85 
86 /**
87  * @tc.name: OnRemoteRequestTest002
88  * @tc.desc: Verify the OnRemoteRequest function
89  * @tc.type: FUNC
90  * @tc.require: I6H5MH
91  */
92 HWTEST_F(CloudDaemonStubTest, OnRemoteRequestTest002, TestSize.Level1)
93 {
94     GTEST_LOG_(INFO) << "OnRemoteRequestTest Start";
95     try {
96         int32_t code = 1000;
97         MessageParcel data;
98         EXPECT_TRUE(data.WriteInterfaceToken(ICloudDaemon::GetDescriptor()));
99         MessageParcel reply;
100         MessageOption option;
101         int ret = cloudDaemonStub_->OnRemoteRequest(code, data, reply, option);
102         EXPECT_NE(ret, 0);
103     } catch (...) {
104         EXPECT_TRUE(false);
105         GTEST_LOG_(INFO) << "OnRemoteRequestTest  ERROR";
106     }
107     GTEST_LOG_(INFO) << "OnRemoteRequestTest End";
108 }
109 
110 /**
111  * @tc.name: OnRemoteRequestTest003
112  * @tc.desc: Verify the OnRemoteRequest function
113  * @tc.type: FUNC
114  * @tc.require: I6H5MH
115  */
116 HWTEST_F(CloudDaemonStubTest, OnRemoteRequestTest003, TestSize.Level1)
117 {
118     GTEST_LOG_(INFO) << "OnRemoteRequestTest Start";
119     try {
120         MessageParcel data;
121         EXPECT_TRUE(data.WriteInterfaceToken(ICloudDaemon::GetDescriptor()));
122         MessageParcel reply;
123         MessageOption option;
124         int ret = cloudDaemonStub_->OnRemoteRequest(
125             static_cast<uint32_t>(CloudFileDaemonInterfaceCode::CLOUD_DAEMON_CMD_START_FUSE), data, reply, option);
126         EXPECT_EQ(ret, 0);
127     } catch (...) {
128         EXPECT_TRUE(false);
129         GTEST_LOG_(INFO) << "OnRemoteRequestTest  ERROR";
130     }
131     GTEST_LOG_(INFO) << "OnRemoteRequestTest End";
132 }
133 
134 } // namespace OHOS::FileManagement::CloudSync::Test