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_daemon_service_mock.h"
17 #include "cloud_daemon_service_proxy.h"
18 #include "cloud_file_daemon_interface_code.h"
19 #include "dfs_error.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "utils_log.h"
24 #include <fcntl.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <sys/stat.h>
28 
29 static bool g_isamflag = true;
30 static bool g_smcflag = true;
31 
32 namespace OHOS {
33 class RemoteObject : public IRemoteObject {
34 public:
GetObjectRefCount()35     virtual int32_t GetObjectRefCount()
36     {
37         return 0;
38     }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39     virtual int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
40     {
41         return 0;
42     }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)43     virtual bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
44     {
45         return 0;
46     }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)47     virtual bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient)
48     {
49         return false;
50     }
Dump(int fd,const std::vector<std::u16string> & args)51     virtual int Dump(int fd, const std::vector<std::u16string> &args)
52     {
53         return 0;
54     }
55 };
56 
CheckSystemAbility(int32_t systemAbilityId)57 sptr<IRemoteObject> ISystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId)
58 {
59     if (g_isamflag == true) {
60         sptr<IRemoteObject> isamObject = sptr(new RemoteObject());
61         return isamObject;
62     } else {
63         return nullptr;
64     }
65     return nullptr;
66 }
67 
GetInstance()68 SystemAbilityManagerClient &SystemAbilityManagerClient::GetInstance()
69 {
70     static auto instance = new SystemAbilityManagerClient();
71     return *instance;
72 }
73 
GetSystemAbilityManager()74 sptr<ISystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
75 {
76     if (g_smcflag == true) {
77         sptr<IRemoteObject> smcObject = IPCSkeleton::GetContextObject();
78         systemAbilityManager_ = iface_cast<ISystemAbilityManager>(smcObject);
79         return systemAbilityManager_;
80     } else {
81         return nullptr;
82     }
83 }
84 } // namespace OHOS
85 
86 namespace OHOS::FileManagement::CloudFile::Test {
87 using namespace testing;
88 using namespace testing::ext;
89 using namespace std;
90 
91 class CloudDaemonServiceProxyTest : public testing::Test {
92 public:
93     static void SetUpTestCase(void);
94     static void TearDownTestCase(void);
95     void SetUp();
96     void TearDown();
97     shared_ptr<CloudDaemonServiceProxy> proxy_ = nullptr;
98     sptr<CloudDaemonServiceMock> mock_ = nullptr;
99 };
100 
SetUpTestCase(void)101 void CloudDaemonServiceProxyTest::SetUpTestCase(void)
102 {
103     GTEST_LOG_(INFO) << "SetUpTestCase";
104 }
105 
TearDownTestCase(void)106 void CloudDaemonServiceProxyTest::TearDownTestCase(void)
107 {
108     GTEST_LOG_(INFO) << "TearDownTestCase";
109 }
110 
SetUp(void)111 void CloudDaemonServiceProxyTest::SetUp(void)
112 {
113     mock_ = sptr(new CloudDaemonServiceMock());
114     proxy_ = make_shared<CloudDaemonServiceProxy>(mock_);
115     GTEST_LOG_(INFO) << "SetUp";
116 }
117 
TearDown(void)118 void CloudDaemonServiceProxyTest::TearDown(void)
119 {
120     GTEST_LOG_(INFO) << "TearDown";
121 }
122 
123 /**
124  * @tc.name: GetInstanceTest001
125  * @tc.desc: Verify the GetInstance function
126  * @tc.type: FUNC
127  * @tc.require: I6H5MH
128  */
129 HWTEST_F(CloudDaemonServiceProxyTest, GetInstanceTest001, TestSize.Level1)
130 {
131     GTEST_LOG_(INFO) << "GetInstanceTest001 Start";
132     try {
133         g_smcflag = false;
134         auto CloudDaemonServiceProxy = CloudDaemonServiceProxy::GetInstance();
135         EXPECT_EQ(CloudDaemonServiceProxy, nullptr);
136     } catch (...) {
137         EXPECT_TRUE(false);
138         GTEST_LOG_(INFO) << "GetInstanceTest001  ERROR";
139     }
140     GTEST_LOG_(INFO) << "GetInstanceTest001 End";
141 }
142 
143 /**
144  * @tc.name: StartFuseTest001
145  * @tc.desc: Verify the StartFuse function
146  * @tc.type: FUNC
147  * @tc.require: I6H5MH
148  */
149 HWTEST_F(CloudDaemonServiceProxyTest, StartFuseTest001, TestSize.Level1)
150 {
151     GTEST_LOG_(INFO) << "StartFuseTest001 Start";
152     try {
153         EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
154         int32_t userId = 100;
155         int32_t devFd = open("/dev/fuse", O_RDWR);
156         string path = "continue";
157         int ret = proxy_->StartFuse(userId, devFd, path);
158         EXPECT_EQ(ret, E_OK);
159     } catch (...) {
160         EXPECT_TRUE(false);
161         GTEST_LOG_(INFO) << "StartFuseTest001 ERROR";
162     }
163     GTEST_LOG_(INFO) << "StartFuseTest001 End";
164 }
165 
166 /**
167  * @tc.name: StartFuseTest002
168  * @tc.desc: Verify the StartFuse function
169  * @tc.type: FUNC
170  * @tc.require: I6H5MH
171  */
172 HWTEST_F(CloudDaemonServiceProxyTest, StartFuseTest002, TestSize.Level1)
173 {
174     GTEST_LOG_(INFO) << "StartFuseTest002 Start";
175     try {
176         EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(-1));
177         int32_t userId = 100;
178         int32_t devFd = open("/dev/fuse", O_RDWR);
179         string path = "continue";
180         int ret = proxy_->StartFuse(userId, devFd, path);
181         EXPECT_EQ(ret, E_BROKEN_IPC);
182     } catch (...) {
183         EXPECT_TRUE(false);
184         GTEST_LOG_(INFO) << "StartFuseTest002 ERROR";
185     }
186     GTEST_LOG_(INFO) << "StartFuseTest002 End";
187 }
188 } // namespace OHOS::FileManagement::CloudFile::Test