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