1 /*
2 * Copyright (c) 2021 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 "utils/DMSTestBase.h"
18 #include "utils/dms_packet.h"
19 #include "distributed_service_interface.h"
20
21 using namespace testing::ext;
22
23 static const int BYTE_CAPA = 127;
24 static const int NODE_MAX_LENGTH = 1009;
25
RunTest(const uint8_t *buffer, uint16_t bufferLen, const TlvParseCallback onTlvParseDone, const StartAbilityCallback onStartAbilityDone)26 static int8_t RunTest(const uint8_t *buffer, uint16_t bufferLen, const TlvParseCallback onTlvParseDone,
27 const StartAbilityCallback onStartAbilityDone)
28 {
29 IDmsFeatureCallback dmsFeatureCallback = {
30 .onTlvParseDone = onTlvParseDone,
31 .onStartAbilityDone = onStartAbilityDone
32 };
33
34 CommuInterInfo interInfo;
35 interInfo.payloadLength = bufferLen;
36 interInfo.payload = buffer;
37
38 return DmsLiteProcessCommuMsg(&interInfo, &dmsFeatureCallback);
39 }
40
41 class MsgParserLenFuncTest : public testing::Test {
42 protected:
43 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)44 static void SetUpTestCase(void)
45 {
46 SystemInitProxy();
47 }
48 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)49 static void TearDownTestCase(void)
50 {
51 }
52 // Testcase setup
SetUp()53 virtual void SetUp()
54 {
55 }
56 // Testcase teardown
TearDown()57 virtual void TearDown()
58 {
59 }
60 };
61
62 /**
63 * @tc.number : DMSLite_DMS_MsgParserLen_0010
64 * @tc.name : Package which node has 127 bytes value can be parsed correctly
65 * @tc.desc : [C- SOFTWARE -0200]
66 */
HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0010, Function | MediumTest | Level2)67 HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0010, Function | MediumTest | Level2) {
68 std::string tLenStr = GetStringByLen(BYTE_CAPA - 1); // - \0
69 std::string bundleName = tLenStr;
70 std::string abilityName = tLenStr;
71 std::string signature = tLenStr;
72
73 char buffer[PACKET_DATA_SIZE] = {0};
74 uint16_t dataSize = 0;
75 DmsPacket dmsPacket {buffer, PACKET_DATA_SIZE};
76 DmsMsgInfo dmsMsgInfo = {
77 DMSLITE_COMMAND::START_ABILITY_FROM_REMOTE,
78 bundleName,
79 abilityName,
80 signature
81 };
82 if (!dmsPacket.BuildDmsPacket(dmsMsgInfo, dataSize)) {
83 printf("[hcpptest]E BuildDmsPacket failed");
84 }
85
86 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) {
87 reinterpret_cast<const TlvDmsMsgInfo *>(dmsMsg);
88
89 printf("[hcpptest]errCode:%d \n", errCode);
90 EXPECT_EQ(errCode, DMS_TLV_SUCCESS);
91 };
92 RunTest((uint8_t *)buffer, dataSize, onTlvParseDone, nullptr);
93 }
94
95 /**
96 * @tc.number : DMSLite_DMS_MsgParserLen_0020
97 * @tc.name : Package which node has 128 bytes can be parsed correctly
98 * @tc.desc : [C- SOFTWARE -0200]
99 */
HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0020, Function | MediumTest | Level2)100 HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0020, Function | MediumTest | Level2) {
101 // > 127, length need 2 bytes
102 std::string tLenStr = GetStringByLen(BYTE_CAPA);
103 std::string bundleName = tLenStr;
104 std::string abilityName = tLenStr;
105 std::string signature = tLenStr;
106
107 char buffer[PACKET_DATA_SIZE] = {0};
108 uint16_t dataSize = 0;
109 DmsPacket dmsPacket {buffer, PACKET_DATA_SIZE};
110 DmsMsgInfo dmsMsgInfo = {
111 DMSLITE_COMMAND::START_ABILITY_FROM_REMOTE,
112 bundleName,
113 abilityName,
114 signature
115 };
116 if (!dmsPacket.BuildDmsPacket(dmsMsgInfo, dataSize)) {
117 printf("[hcpptest]E BuildDmsPacket failed");
118 }
119
120 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) {
121 reinterpret_cast<const TlvDmsMsgInfo *>(dmsMsg);
122
123 printf("[hcpptest]errCode:%d \n", errCode);
124 EXPECT_EQ(errCode, DMS_TLV_SUCCESS);
125 };
126 RunTest((uint8_t *)buffer, dataSize, onTlvParseDone, nullptr);
127 }
128
129 /**
130 * @tc.number : DMSLite_DMS_MsgParserLen_0030
131 * @tc.name : Package which node has 1010 bytes can be parsed correctly
132 * @tc.desc : [C- SOFTWARE -0200]
133 */
HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0030, Function | MediumTest | Level2)134 HWTEST_F(MsgParserLenFuncTest, testMsgParserLen0030, Function | MediumTest | Level2) {
135 std::string bundleName = GetStringByLen(NODE_MAX_LENGTH);
136 std::string abilityName = "a";
137 std::string signature = "a";
138
139 char buffer[PACKET_DATA_SIZE] = {0};
140 uint16_t dataSize = 0;
141 DmsPacket dmsPacket {buffer, PACKET_DATA_SIZE};
142 DmsMsgInfo dmsMsgInfo = {
143 DMSLITE_COMMAND::START_ABILITY_FROM_REMOTE,
144 bundleName,
145 abilityName,
146 signature
147 };
148 if (!dmsPacket.BuildDmsPacket(dmsMsgInfo, dataSize)) {
149 printf("[hcpptest]E BuildDmsPacket failed \n");
150 }
151 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) {
152 reinterpret_cast<const TlvDmsMsgInfo *>(dmsMsg);
153 printf("[hcpptest]errCode:%d \n", errCode);
154 EXPECT_EQ(errCode, DMS_TLV_SUCCESS);
155 };
156 RunTest((uint8_t *)buffer, dataSize, onTlvParseDone, nullptr);
157 }