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 ASSERT_EQ(FillWant(&want, "ohos.dms.example", "MainAbility"), 0); 87 88 CallerInfo callerInfo = { 89 .uid = 0 90 }; 91 StartRemoteAbility(&want, &callerInfo, nullptr); __anona2e3d7a90102(int8_t errCode, const void *dmsMsg) 92 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 93 const TlvNode *tlvHead = reinterpret_cast<const TlvNode *>(dmsMsg); 94 EXPECT_EQ(errCode, DMS_TLV_SUCCESS); 95 EXPECT_EQ(UnMarshallUint16(tlvHead, COMMAND_ID), 1); 96 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_BUNDLE_NAME)), "ohos.dms.example"); 97 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_ABILITY_NAME)), "MainAbility"); 98 }; 99 RunTest((const uint8_t *)GetPacketBufPtr(), GetPacketSize(), onTlvParseDone, nullptr); 100 101 ClearWant(&want); 102 CleanBuild(); 103 } 104 105 /** 106 * @tc.name: StartRemoteAbility_002 107 * @tc.desc: Start remote ability with bundle name and ability name 108 * @tc.type: FUNC 109 * @tc.require: SR000FKTD0 AR000FM5TN 110 */ 111 HWTEST_F(FamgrTest, StartRemoteAbility_002, TestSize.Level1) { 112 PreprareBuild(); 113 114 Want want; 115 ASSERT_EQ(FillWant(&want, "ohos.dms.example", "MainAbility"), 0); 116 StartRemoteAbility(&want, nullptr, nullptr); __anona2e3d7a90202(int8_t errCode, const void *dmsMsg) 117 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 118 EXPECT_NE(errCode, DMS_TLV_SUCCESS); 119 }; 120 RunTest((const uint8_t *)GetPacketBufPtr(), GetPacketSize(), onTlvParseDone, nullptr); 121 122 ClearWant(&want); 123 CleanBuild(); 124 } 125 } 126 } 127