1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include "ipc_skeleton.h"
18 #include "mock_distributed_sched.h"
19
20 namespace OHOS {
21 namespace DistributedSchedule {
22 using namespace std;
23 using namespace AAFwk;
24
25 namespace {
26 const std::string TAG = "MockDistributedSched";
27 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
28 }
DistributedSchedStub()29 DistributedSchedStub::DistributedSchedStub()
30 {
31 localFuncsMap_[START_REMOTE_SHARE_FORM] = &DistributedSchedStub::StartRemoteShareFormInner;
32 }
33
~DistributedSchedStub()34 DistributedSchedStub::~DistributedSchedStub()
35 {
36 localFuncsMap_.clear();
37 }
38
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t DistributedSchedStub::OnRemoteRequest(uint32_t code,
40 MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 const auto &funcsMap = localFuncsMap_;
43 auto iter = funcsMap.find(code);
44 if (iter != funcsMap.end()) {
45 auto func = iter->second;
46 if (!EnforceInterfaceToken(data)) {
47 return 1;
48 }
49 if (func != nullptr) {
50 return (this->*func)(data, reply);
51 }
52 }
53
54 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
55 }
56
EnforceInterfaceToken(MessageParcel & data)57 bool DistributedSchedStub::EnforceInterfaceToken(MessageParcel &data)
58 {
59 u16string interfaceToken = data.ReadInterfaceToken();
60 return interfaceToken == DMS_STUB_INTERFACE_TOKEN;
61 }
62
StartRemoteShareFormInner(MessageParcel & data,MessageParcel & reply)63 int32_t DistributedSchedStub::StartRemoteShareFormInner(MessageParcel &data, MessageParcel &reply)
64 {
65 std::string deviceId = data.ReadString();
66 shared_ptr<AppExecFwk::FormShareInfo> formShareInfo(data.ReadParcelable<AppExecFwk::FormShareInfo>());
67 if (formShareInfo == nullptr) {
68 return ERR_NULL_OBJECT;
69 }
70
71 int32_t result = StartRemoteShareForm(deviceId, *formShareInfo);
72 return result;
73 }
74 } // namespace DistributedSchedule
75 } // namespace OHOS