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