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 "dmslite_parser.h" 19 #include "dmslite_tlv_common.h" 20 21 using namespace testing::ext; 22 23 namespace OHOS { 24 namespace DistributedSchedule { 25 class TlvParseTest : public testing::Test { 26 protected: SetUpTestCase()27 static void SetUpTestCase() { } TearDownTestCase()28 static void TearDownTestCase() { } SetUp()29 virtual void SetUp() { } TearDown()30 virtual void TearDown() { } 31 RunTest(const uint8_t * buffer,uint16_t bufferLen,const TlvParseCallback onTlvParseDone,const StartAbilityCallback onStartAbilityDone)32 static void RunTest(const uint8_t *buffer, uint16_t bufferLen, 33 const TlvParseCallback onTlvParseDone, const StartAbilityCallback onStartAbilityDone) 34 { 35 IDmsFeatureCallback dmsFeatureCallback = { 36 .onTlvParseDone = onTlvParseDone, 37 .onStartAbilityDone = onStartAbilityDone 38 }; 39 40 CommuMessage commuMessage; 41 commuMessage.payloadLength = bufferLen; 42 commuMessage.payload = buffer; 43 44 ProcessCommuMsg(&commuMessage, &dmsFeatureCallback); 45 } 46 }; 47 48 /** 49 * @tc.name: NormalPackage_001 50 * @tc.desc: normal package with small bundle name and ability name 51 * @tc.type: FUNC 52 * @tc.require: SR000ELTHO 53 */ 54 HWTEST_F(TlvParseTest, NormalPackage_001, TestSize.Level1) { 55 uint8_t buffer[] = { 56 0x01, 0x02, 0x00, 0x01, 57 0x02, 0x02, 0x00, 0x64, 58 0x03, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 59 0x2e, 0x6c, 0x61, 0x75, 0x6e, 0x63, 0x68, 0x65, 0x72, 0x00, 60 0x04, 0x0c, 0x4d, 0x61, 0x69, 0x6e, 0x41, 0x62, 0x69, 0x6c, 0x69, 0x74, 61 0x79, 0x00, 62 0x05, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 0x65, 0x79, 0x00 63 }; 64 __anon16bcb0e40102(int8_t errCode, const void *dmsMsg) 65 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 66 const TlvNode *tlvHead = reinterpret_cast<const TlvNode *>(dmsMsg); 67 EXPECT_EQ(errCode, DMS_TLV_SUCCESS); 68 EXPECT_EQ(UnMarshallUint16(tlvHead, COMMAND_ID), 1); 69 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_ABILITY_NAME)), "MainAbility"); 70 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLER_SIGNATURE)), "publickey"); 71 }; 72 73 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 74 } 75 76 77 /** 78 * @tc.name: NormalPackageWithLongBundleName 79 * @tc.desc: normal package with 255 bytes long(upper boundary) bundle name 80 * @tc.type: FUNC 81 * @tc.require: AR000ENCTK 82 */ 83 84 HWTEST_F(TlvParseTest, NormalPackage_002, TestSize.Level1) { 85 uint8_t buffer[] = { 86 0x01, 0x02, 0x00, 0x01, 87 0x02, 0x02, 0x00, 0x64, 88 0x03, 0x82, 0x00, 0x63, 0x6f, 0x6d, 0x2e, 0x61, 0x62, 0x63, 0x64, 0x65, 89 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 90 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x41, 0x42, 0x43, 91 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 92 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 93 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 94 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 95 0x7a, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 96 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 97 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 98 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 99 0x76, 0x77, 0x78, 0x79, 0x7a, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 100 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 101 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 102 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 103 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x41, 0x42, 0x43, 104 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 105 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 106 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 107 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 108 0x7a, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x2e, 109 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 0x00, 110 0x04, 0x0c, 0x4d, 0x61, 0x69, 0x6e, 0x41, 0x62, 0x69, 0x6c, 0x69, 0x74, 111 0x79, 0x00, 112 0x05, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 0x65, 0x79, 0x00 113 }; 114 __anon16bcb0e40202(int8_t errCode, const void *dmsMsg) 115 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 116 const TlvNode *tlvHead = reinterpret_cast<const TlvNode *>(dmsMsg); 117 EXPECT_EQ(errCode, DMS_TLV_SUCCESS); 118 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_ABILITY_NAME)), "MainAbility"); 119 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLER_SIGNATURE)), "publickey"); 120 }; 121 122 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 123 } 124 125 /** 126 * @tc.name: AbnormalPackageOutOfOrder_001 127 * @tc.desc: abnormal package with node type sequence in disorder 128 * @tc.type: FUNC 129 * @tc.require: AR000ELTIG 130 */ 131 HWTEST_F(TlvParseTest, AbnormalPackageOutOfOrder_001, TestSize.Level1) { 132 uint8_t buffer[] = { 133 0x02, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x68, 0x75, 0x61, 0x77, 134 0x65, 0x69, 0x2e, 0x6c, 0x61, 0x75, 0x6e, 0x63, 0x68, 0x65, 135 0x72, 0x00, 0x01, 0x02, 0x00, 0x00, 0x03, 0x0d, 0x4d, 0x61, 136 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 137 0x00 138 }; 139 __anon16bcb0e40302(int8_t errCode, const void *dmsMsg) 140 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 141 EXPECT_EQ(errCode, DMS_TLV_ERR_OUT_OF_ORDER); 142 }; 143 144 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 145 } 146 147 /** 148 * @tc.name: AbnormalPackageBadNodeNum_001 149 * @tc.desc: abnormal package without node 150 * @tc.type: FUNC 151 * @tc.require: AR000E0DGE 152 */ 153 HWTEST_F(TlvParseTest, AbnormalPackageBadNodeNum_001, TestSize.Level1) { 154 uint8_t buffer[] = { }; 155 __anon16bcb0e40402(int8_t errCode, const void *dmsMsg) 156 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 157 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 158 }; 159 160 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 161 } 162 163 /** 164 * @tc.name: AbnormalPackageBadNodeNum_002 165 * @tc.desc: abnormal package with only one mandatory node 166 * @tc.type: FUNC 167 * @tc.require: AR000E0DE5 168 */ 169 HWTEST_F(TlvParseTest, AbnormalPackageBadNodeNum_002, TestSize.Level1) { 170 uint8_t buffer[] = { 171 0x01, 0x02, 0x00, 0x00 172 }; 173 __anon16bcb0e40502(int8_t errCode, const void *dmsMsg) 174 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 175 EXPECT_EQ(errCode, DMS_TLV_ERR_BAD_NODE_NUM); 176 }; 177 178 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 179 } 180 181 /** 182 * @tc.name: AbnormalPackageBadLength_001 183 * @tc.desc: abnormal package tlv node without value 184 * @tc.type: FUNC 185 * @tc.require: SR000E0DR9 186 */ 187 HWTEST_F(TlvParseTest, AbnormalPackageBadLength_001, TestSize.Level1) { 188 uint8_t buffer[] = { 189 0x01, 0x02 190 }; 191 __anon16bcb0e40602(int8_t errCode, const void *dmsMsg) 192 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 193 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 194 }; 195 196 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 197 } 198 199 /** 200 * @tc.name: AbnormalPackageBadLength_002 201 * @tc.desc: abnormal package tlv node with zero-size length 202 * @tc.type: FUNC 203 * @tc.require: AR000E0ECR 204 */ 205 HWTEST_F(TlvParseTest, AbnormalPackageBadLength_002, TestSize.Level1) { 206 uint8_t buffer[] = { 207 0x01, 0x02, 0x00, 0x00, 0x02, 0x00, 0x03, 0x0d, 0x4d, 0x61, 208 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 209 0x00, 0x04, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 210 0x65, 0x79, 0x00 211 }; 212 __anon16bcb0e40702(int8_t errCode, const void *dmsMsg) 213 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 214 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 215 }; 216 217 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 218 } 219 220 /** 221 * @tc.name: AbnormalPackageBadLength_003 222 * @tc.desc: abnormal package with mismatched buffer size 223 * @tc.type: FUNC 224 * @tc.require: AR000E0DE0 225 */ 226 HWTEST_F(TlvParseTest, AbnormalPackageBadLength_003, TestSize.Level1) { 227 uint8_t buffer[] = { 228 0x01, 0x02, 0x00, 0x00, 0x02, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 229 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x6c, 0x61, 0x75, 230 0x6e, 0x63, 0x68, 0x65, 0x72, 0x00, 0x03, 0x0d, 0x4d, 0x61, 231 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 232 0x00, 0x04, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 233 0x65, 0x79, 0x00 234 }; 235 __anon16bcb0e40802(int8_t errCode, const void *dmsMsg) 236 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 237 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 238 }; 239 240 RunTest(buffer, sizeof(buffer) + 1, onTlvParseDone, nullptr); 241 } 242 243 /** 244 * @tc.name: AbnormalPackageBadLength_004 245 * @tc.desc: abnormal package with mismatched buffer size 246 * @tc.type: FUNC 247 * @tc.require: AR000E0DE0 248 */ 249 HWTEST_F(TlvParseTest, AbnormalPackageBadLength_004, TestSize.Level1) { 250 uint8_t buffer[] = { 251 0x01, 0x02, 0x00, 0x00, 0x02, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 252 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x6c, 0x61, 0x75, 253 0x6e, 0x63, 0x68, 0x65, 0x72, 0x00, 0x03, 0x0d, 0x4d, 0x61, 254 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 255 0x00, 0x04, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 256 0x65, 0x79, 0x00 257 }; 258 __anon16bcb0e40902(int8_t errCode, const void *dmsMsg) 259 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 260 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 261 }; 262 263 RunTest(buffer, sizeof(buffer) - 1, onTlvParseDone, nullptr); 264 } 265 266 /** 267 * @tc.name: AbnormalPackageBadLength_005 268 * @tc.desc: abnormal package with mismatched buffer size 269 * @tc.type: FUNC 270 * @tc.require: AR000E0DE0 271 */ 272 HWTEST_F(TlvParseTest, AbnormalPackageBadLength_005, TestSize.Level1) { 273 uint8_t buffer[] = { 274 0x01, 0x02, 0x00, 0x00, 0x02, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 275 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x6c, 0x61, 0x75, 276 0x6e, 0x63, 0x68, 0x65, 0x72, 0x00, 0x03, 0x0d, 0x4d, 0x61, 277 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 278 0x00, 0x04, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 279 0x65, 0x79, 0x00 280 }; 281 __anon16bcb0e40a02(int8_t errCode, const void *dmsMsg) 282 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 283 EXPECT_EQ(errCode, DMS_TLV_ERR_LEN); 284 }; 285 286 RunTest(buffer, 0, onTlvParseDone, nullptr); 287 } 288 289 /** 290 * @tc.name: AbnormalPackageBadSource_001 291 * @tc.desc: abnormal package string field with no '\0' in the ending 292 * @tc.type: FUNC 293 * @tc.require: AR000E0DE0 294 */ 295 HWTEST_F(TlvParseTest, AbnormalPackageBadSource_001, TestSize.Level1) { 296 uint8_t buffer[] = { 297 0x01, 0x02, 0x00, 0x00, 0x02, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 298 0x68, 0x75, 0x61, 0x77, 0x65, 0x69, 0x2e, 0x6c, 0x61, 0x75, 299 0x6e, 0x63, 0x68, 0x65, 0x72, 0xff, 0x03, 0x0d, 0x4d, 0x61, 300 0x69, 0x6e, 0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x74, 0x79, 301 0xff, 0x04, 0x0a, 0x70, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x6B, 302 0x65, 0x79, 0xff 303 }; 304 __anon16bcb0e40b02(int8_t errCode, const void *dmsMsg) 305 auto onTlvParseDone = [] (int8_t errCode, const void *dmsMsg) { 306 const TlvNode *tlvHead = reinterpret_cast<const TlvNode *>(dmsMsg); 307 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_BUNDLE_NAME)), ""); 308 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLEE_ABILITY_NAME)), ""); 309 EXPECT_EQ(std::string(UnMarshallString(tlvHead, CALLER_SIGNATURE)), ""); 310 }; 311 312 RunTest(buffer, sizeof(buffer), onTlvParseDone, nullptr); 313 } 314 } 315 } 316