1 /*
2  * Copyright (c) 2020 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 
18 #include "ability_manager.h"
19 #include "dmsfwk_interface.h"
20 #include "dmslite_famgr.h"
21 #include "dmslite_packet.h"
22 #include "dmslite_parser.h"
23 #include "dmslite_tlv_common.h"
24 
25 #include "ohos_errno.h"
26 #include "securec.h"
27 
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace DistributedSchedule {
32 class FamgrTest : public testing::Test {
33 protected:
SetUpTestCase()34     static void SetUpTestCase() { }
TearDownTestCase()35     static void TearDownTestCase() { }
SetUp()36     virtual void SetUp() { }
TearDown()37     virtual void TearDown() { }
38 
FillWant(Want * want,const char * bundleName,const char * abilityName)39     static int8_t FillWant(Want *want, const char *bundleName, const char *abilityName)
40     {
41         if (memset_s(want, sizeof(Want), 0x00, sizeof(Want)) != EOK) {
42             return DMS_EC_FAILURE;
43         }
44         ElementName element;
45         if (memset_s(&element, sizeof(ElementName), 0x00, sizeof(ElementName)) != EOK) {
46             return DMS_EC_FAILURE;
47         }
48 
49         if (!(SetElementBundleName(&element, bundleName)
50             && SetElementAbilityName(&element, abilityName)
51             && SetWantElement(want, element))) {
52             ClearElement(&element);
53             ClearWant(want);
54             return DMS_EC_FAILURE;
55         }
56         ClearElement(&element);
57         return DMS_EC_SUCCESS;
58     }
59 
RunTest(const uint8_t * buffer,uint16_t bufferLen,const TlvParseCallback onTlvParseDone,const StartAbilityCallback onStartAbilityDone)60     static void RunTest(const uint8_t *buffer, uint16_t bufferLen,
61         const TlvParseCallback onTlvParseDone, const StartAbilityCallback onStartAbilityDone)
62     {
63         IDmsFeatureCallback dmsFeatureCallback = {
64             .onTlvParseDone = onTlvParseDone,
65             .onStartAbilityDone = onStartAbilityDone
66         };
67 
68         CommuMessage commuMessage;
69         commuMessage.payloadLength = bufferLen;
70         commuMessage.payload = buffer;
71 
72         ProcessCommuMsg(&commuMessage, &dmsFeatureCallback);
73     }
74 };
75 
76 /**
77  * @tc.name: StartRemoteAbility_001
78  * @tc.desc: Start remote ability with bundle name and ability name
79  * @tc.type: FUNC
80  * @tc.require: SR000FKTLR AR000FKVSU
81  */
82 HWTEST_F(FamgrTest, StartRemoteAbility_001, TestSize.Level1) {
83     PreprareBuild();
84 
85     Want want;
86     if (FillWant(&want, "ohos.dms.example", "MainAbility") != 0) {
87         return;
88     }
89 
90     CallerInfo callerInfo = {
91         .uid = 0
92     };
93     StartRemoteAbility(&want, &callerInfo, nullptr);
__anon55093ba10102(int8_t errCode, const void *dmsMsg) 94     auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) {
95         const TlvNode *tlvHead = reinterpret_cast<const TlvNode *>(dmsMsg);
96         EXPECT_EQ(errCode, DMS_TLV_SUCCESS);
97         EXPECT_EQ(UnMarshallUint16(tlvHead, COMMAND_ID), 1);
98         EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_BUNDLE_NAME)), "ohos.dms.example");
99         EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_ABILITY_NAME)), "MainAbility");
100     };
101     RunTest((const uint8_t *)GetPacketBufPtr(), GetPacketSize(), onTlvParseDone, nullptr);
102 
103     ClearWant(&want);
104     CleanBuild();
105 }
106 
107 /**
108  * @tc.name: StartRemoteAbility_002
109  * @tc.desc: Start remote ability with bundle name and ability name
110  * @tc.type: FUNC
111  * @tc.require: SR000FKTD0 AR000FM5TN
112  */
113 HWTEST_F(FamgrTest, StartRemoteAbility_002, TestSize.Level1) {
114     PreprareBuild();
115 
116     Want want;
117     if (FillWant(&want, "ohos.dms.example", "MainAbility") != 0) {
118         return;
119     }
120     StartRemoteAbility(&want, nullptr, nullptr);
__anon55093ba10202(int8_t errCode, const void *dmsMsg) 121     auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) {
122         EXPECT_NE(errCode, DMS_TLV_SUCCESS);
123     };
124     RunTest((const uint8_t *)GetPacketBufPtr(), GetPacketSize(), onTlvParseDone, nullptr);
125 
126     ClearWant(&want);
127     CleanBuild();
128 }
129 }
130 }
131