• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: GetInstanceTest002
145  * @tc.desc: Verify the GetInstance function
146  * @tc.type: FUNC
147  * @tc.require: I6H5MH
148  */
149 HWTEST_F(CloudDaemonServiceProxyTest, GetInstanceTest002, TestSize.Level1)
150 {
151     GTEST_LOG_(INFO) << "GetInstanceTest002 Start";
152     try {
153         g_smcflag = true;
154         auto CloudDaemonServiceProxy = CloudDaemonServiceProxy::GetInstance();
155         EXPECT_NE(CloudDaemonServiceProxy, nullptr);
156     } catch (...) {
157         EXPECT_TRUE(false);
158         GTEST_LOG_(INFO) << "GetInstanceTest002  ERROR";
159     }
160     GTEST_LOG_(INFO) << "GetInstanceTest002 End";
161 }
162 
163 /**
164  * @tc.name: InvaildInstanceTest
165  * @tc.desc: Verify the InvaildInstance function
166  * @tc.type: FUNC
167  * @tc.require: I6H5MH
168  */
169 HWTEST_F(CloudDaemonServiceProxyTest, InvaildInstanceTest, TestSize.Level1)
170 {
171     GTEST_LOG_(INFO) << "InvaildInstanceTest Start";
172     try {
173         g_smcflag = true;
174         auto CloudDaemonServiceProxy = CloudDaemonServiceProxy::GetInstance();
175         EXPECT_NE(CloudDaemonServiceProxy, nullptr);
176         CloudDaemonServiceProxy::InvaildInstance();
177         EXPECT_EQ(CloudDaemonServiceProxy::serviceProxy_, nullptr);
178     } catch (...) {
179         EXPECT_TRUE(false);
180         GTEST_LOG_(INFO) << "InvaildInstanceTest  ERROR";
181     }
182     GTEST_LOG_(INFO) << "InvaildInstanceTest End";
183 }
184 
185 /**
186  * @tc.name: StartFuseTest001
187  * @tc.desc: Verify the StartFuse function
188  * @tc.type: FUNC
189  * @tc.require: I6H5MH
190  */
191 HWTEST_F(CloudDaemonServiceProxyTest, StartFuseTest001, TestSize.Level1)
192 {
193     GTEST_LOG_(INFO) << "StartFuseTest001 Start";
194     try {
195         EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
196         int32_t userId = 100;
197         int32_t devFd = open("/dev/fuse", O_RDWR);
198         string path = "continue";
199         int ret = proxy_->StartFuse(userId, devFd, path);
200         EXPECT_EQ(ret, E_OK);
201     } catch (...) {
202         EXPECT_TRUE(false);
203         GTEST_LOG_(INFO) << "StartFuseTest001 ERROR";
204     }
205     GTEST_LOG_(INFO) << "StartFuseTest001 End";
206 }
207 
208 /**
209  * @tc.name: StartFuseTest002
210  * @tc.desc: Verify the StartFuse function
211  * @tc.type: FUNC
212  * @tc.require: I6H5MH
213  */
214 HWTEST_F(CloudDaemonServiceProxyTest, StartFuseTest002, TestSize.Level1)
215 {
216     GTEST_LOG_(INFO) << "StartFuseTest002 Start";
217     try {
218         EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(-1));
219         int32_t userId = 100;
220         int32_t devFd = open("/dev/fuse", O_RDWR);
221         string path = "continue";
222         int ret = proxy_->StartFuse(userId, devFd, path);
223         EXPECT_EQ(ret, E_BROKEN_IPC);
224     } catch (...) {
225         EXPECT_TRUE(false);
226         GTEST_LOG_(INFO) << "StartFuseTest002 ERROR";
227     }
228     GTEST_LOG_(INFO) << "StartFuseTest002 End";
229 }
230 } // namespace OHOS::FileManagement::CloudFile::Test