• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         memcpy_s(&buffer_[offset], buffer_.capacity() - offset, &value, sizeof(value));
40         FieldFormat format = {};
41         format.offset = offset;
42         format.size = sizeof(value);
43         format.isSigned = true;
44         format.filedType = FIELD_TYPE_INT32;
45         formats_.push_back(format);
46     }
47 
AppendString(const std::string & str)48     void AppendString(const std::string& str)
49     {
50         size_t offset = buffer_.size();
51         buffer_.resize(buffer_.size() + str.size());
52         memcpy_s(&buffer_[offset], buffer_.capacity() - offset, &str[0], str.size());
53         FieldFormat format = {};
54         format.offset = offset;
55         format.size = str.size();
56         format.isSigned = true;
57         format.filedType = FIELD_TYPE_FIXEDCSTRING;
58         formats_.push_back(format);
59     }
60 
SetUp()61     void SetUp() override {}
62 
TearDown()63     void TearDown() override {}
64 };
65 
66 /*
67  * @tc.name: ParseIntField1
68  * @tc.desc: test FtraceFieldParser::ParseIntField with normal case.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(FtraceFieldParserTest, ParseIntField1, TestSize.Level1)
72 {
73     for (int i = 0; i < NROUNDS; i++) {
74         int expect = i + 1;
75         AppendInt(expect);
76         int actual = FtraceFieldParser::ParseIntField<int>(formats_[i], buffer_.data(), buffer_.size());
77         EXPECT_EQ(actual, expect);
78     }
79 }
80 
81 /*
82  * @tc.name: ParseIntField2
83  * @tc.desc: test FtraceFieldParser::ParseIntField with normal case.
84  * @tc.type: FUNC
85  */
86 HWTEST_F(FtraceFieldParserTest, ParseIntField2, TestSize.Level1)
87 {
88     for (int i = 0; i < NROUNDS; i++) {
89         int expect = i + 1;
90         AppendInt(expect);
91         int actual = FtraceFieldParser::ParseIntField<int>(formats_, i, buffer_.data(), buffer_.size());
92         EXPECT_EQ(actual, expect);
93     }
94 }
95 
96 /*
97  * @tc.name: ParseStrField1
98  * @tc.desc: test FtraceFieldParser::ParseIntField with normal case.
99  * @tc.type: FUNC
100  */
101 HWTEST_F(FtraceFieldParserTest, ParseStrField1, TestSize.Level1)
102 {
103     for (int i = 0; i < NROUNDS; i++) {
104         std::string expect = std::to_string(i + 1);
105         AppendString(expect);
106         std::string actual = FtraceFieldParser::ParseStrField(formats_[i], buffer_.data(), buffer_.size());
107         EXPECT_EQ(actual, expect);
108     }
109 }
110 
111 /*
112  * @tc.name: ParseStrField2
113  * @tc.desc: test FtraceFieldParser::ParseIntField with normal case.
114  * @tc.type: FUNC
115  */
116 HWTEST_F(FtraceFieldParserTest, ParseStrField2, TestSize.Level1)
117 {
118     for (int i = 0; i < NROUNDS; i++) {
119         std::string expect = std::to_string(i + 1);
120         AppendString(expect);
121         std::string actual = FtraceFieldParser::ParseStrField(formats_, i, buffer_.data(), buffer_.size());
122         EXPECT_EQ(actual, expect);
123     }
124 }
125 } // namespace
126