1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 #include <gtest/gtest.h> 16 17 #include "ftrace_field_parser.h" 18 #include "securec.h" 19 20 using FTRACE_NS::FIELD_TYPE_FIXEDCSTRING; 21 using FTRACE_NS::FIELD_TYPE_INT32; 22 using FTRACE_NS::FieldFormat; 23 using FTRACE_NS::FtraceFieldParser; 24 using testing::ext::TestSize; 25 26 namespace { 27 constexpr int NROUNDS = 20; 28 29 class FtraceFieldParserTest : public ::testing::Test { 30 protected: 31 std::vector<uint8_t> buffer_; 32 std::vector<FieldFormat> formats_; 33 AppendInt(int value)34 void AppendInt(int value) 35 { 36 size_t offset = buffer_.size(); 37 buffer_.resize(buffer_.size() + sizeof(value)); 38 if (memcpy_s(&buffer_[offset], buffer_.capacity() - offset, &value, sizeof(value))) { 39 EXPECT_TRUE(false); 40 return; 41 } 42 FieldFormat format = {}; 43 format.offset = offset; 44 format.size = sizeof(value); 45 format.isSigned = true; 46 format.filedType = FIELD_TYPE_INT32; 47 formats_.push_back(format); 48 } 49 AppendString(const std::string & str)50 void AppendString(const std::string& str) 51 { 52 size_t offset = buffer_.size(); 53 buffer_.resize(buffer_.size() + str.size()); 54 if (memcpy_s(&buffer_[offset], buffer_.capacity() - offset, &str[0], str.size())) { 55 EXPECT_TRUE(false); 56 return; 57 } 58 FieldFormat format = {}; 59 format.offset = offset; 60 format.size = str.size(); 61 format.isSigned = true; 62 format.filedType = FIELD_TYPE_FIXEDCSTRING; 63 formats_.push_back(format); 64 } 65 SetUp()66 void SetUp() override {} 67 TearDown()68 void TearDown() override {} 69 }; 70 71 /* 72 * @tc.name: ParseIntField1 73 * @tc.desc: test FtraceFieldParser::ParseIntField with normal case. 74 * @tc.type: FUNC 75 */ 76 HWTEST_F(FtraceFieldParserTest, ParseIntField1, TestSize.Level1) 77 { 78 for (int i = 0; i < NROUNDS; i++) { 79 int expect = i + 1; 80 AppendInt(expect); 81 int actual = FtraceFieldParser::ParseIntField<int>(formats_[i], buffer_.data(), buffer_.size()); 82 EXPECT_EQ(actual, expect); 83 } 84 } 85 86 /* 87 * @tc.name: ParseIntField2 88 * @tc.desc: test FtraceFieldParser::ParseIntField with normal case. 89 * @tc.type: FUNC 90 */ 91 HWTEST_F(FtraceFieldParserTest, ParseIntField2, TestSize.Level1) 92 { 93 for (int i = 0; i < NROUNDS; i++) { 94 int expect = i + 1; 95 AppendInt(expect); 96 int actual = FtraceFieldParser::ParseIntField<int>(formats_, i, buffer_.data(), buffer_.size()); 97 EXPECT_EQ(actual, expect); 98 } 99 } 100 101 /* 102 * @tc.name: ParseStrField1 103 * @tc.desc: test FtraceFieldParser::ParseIntField with normal case. 104 * @tc.type: FUNC 105 */ 106 HWTEST_F(FtraceFieldParserTest, ParseStrField1, TestSize.Level1) 107 { 108 for (int i = 0; i < NROUNDS; i++) { 109 std::string expect = std::to_string(i + 1); 110 AppendString(expect); 111 std::string actual = FtraceFieldParser::ParseStrField(formats_[i], buffer_.data(), buffer_.size()); 112 EXPECT_EQ(actual, expect); 113 } 114 } 115 116 /* 117 * @tc.name: ParseStrField2 118 * @tc.desc: test FtraceFieldParser::ParseIntField with normal case. 119 * @tc.type: FUNC 120 */ 121 HWTEST_F(FtraceFieldParserTest, ParseStrField2, TestSize.Level1) 122 { 123 for (int i = 0; i < NROUNDS; i++) { 124 std::string expect = std::to_string(i + 1); 125 AppendString(expect); 126 std::string actual = FtraceFieldParser::ParseStrField(formats_, i, buffer_.data(), buffer_.size()); 127 EXPECT_EQ(actual, expect); 128 } 129 } 130 } // namespace 131