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 "iam_hat_test.h" 17 18 #include <cstdint> 19 #include <string> 20 #include <vector> 21 22 #include "parcel.h" 23 #include "securec.h" 24 using namespace std; 25 26 27 #define LOG_LABEL UserIam::Common::LABEL_IAM_COMMON 28 29 namespace OHOS { 30 namespace UserIam { 31 namespace Common { 32 namespace { 33 constexpr int32_t MAX_DATA_LEN = 200; FillTestBuffer(Parcel & parcel,void * p,uint32_t len)34 void FillTestBuffer(Parcel &parcel, void *p, uint32_t len) 35 { 36 if (len == 0) { 37 return; 38 } 39 40 auto buffer = parcel.ReadBuffer(len); 41 if (buffer == nullptr) { 42 cout << "ReadBuffer len " << len << " fail!" << endl; 43 return; 44 } 45 46 if (memcpy_s(p, len, buffer, len) != EOK) { 47 cout << "memcpy_s fail" << endl; 48 return; 49 } 50 51 return; 52 } 53 } // namespace 54 FillTestUint8Vector(Parcel & parcel,std::vector<uint8_t> & data)55 void FillTestUint8Vector(Parcel &parcel, std::vector<uint8_t> &data) 56 { 57 uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; 58 uint32_t memLen = len * sizeof(uint8_t); 59 data.resize(len); 60 FillTestBuffer(parcel, static_cast<void *>(&data[0]), memLen); 61 cout << "fill vector len " << len << "ok" << endl; 62 } 63 FillTestInt8Vector(Parcel & parcel,std::vector<int8_t> & data)64 void FillTestInt8Vector(Parcel &parcel, std::vector<int8_t> &data) 65 { 66 uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; 67 uint32_t memLen = len * sizeof(int8_t); 68 data.resize(len); 69 FillTestBuffer(parcel, static_cast<void *>(&data[0]), memLen); 70 cout << "fill vector len " << len << "ok" << endl; 71 } 72 FillTestUint32Vector(Parcel & parcel,std::vector<uint32_t> & data)73 void FillTestUint32Vector(Parcel &parcel, std::vector<uint32_t> &data) 74 { 75 uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; 76 uint32_t memLen = len * sizeof(uint32_t); 77 data.resize(len); 78 FillTestBuffer(parcel, static_cast<void *>(&data[0]), memLen); 79 cout << "fill vector len " << len << "ok" << endl; 80 } 81 FillTestUint64Vector(Parcel & parcel,std::vector<uint64_t> & data)82 void FillTestUint64Vector(Parcel &parcel, std::vector<uint64_t> &data) 83 { 84 uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN; 85 uint32_t memLen = len * sizeof(uint64_t); 86 data.resize(len); 87 FillTestBuffer(parcel, static_cast<void *>(&data[0]), memLen); 88 cout << "fill vector len " << len << "ok" << endl; 89 } 90 FillTestString(Parcel & parcel,std::string & str)91 void FillTestString(Parcel &parcel, std::string &str) 92 { 93 uint32_t len = parcel.ReadUint32() % MAX_DATA_LEN + 1; 94 uint32_t memLen = len * sizeof(char); 95 std::vector<char> data(len, 0); 96 FillTestBuffer(parcel, static_cast<void *>(&data[0]), memLen - 1); 97 str = std::string(&data[0]); 98 cout << "fill string len " << (len - 1) << " ok" << endl; 99 } 100 } // namespace Common 101 } // namespace UserIam 102 } // namespace OHOS