1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/traced/probes/ftrace/event_info.h"
18
19 #include "perfetto/protozero/proto_utils.h"
20 #include "test/gtest_and_gmock.h"
21
22 namespace perfetto {
23 namespace {
24 using protozero::proto_utils::ProtoSchemaType;
25
TEST(EventInfoTest,GetStaticEventInfoValidations)26 TEST(EventInfoTest, GetStaticEventInfoValidations) {
27 std::vector<Event> events = GetStaticEventInfo();
28 for (const Event& event : events) {
29 // For each event the following fields should be filled
30 // statically:
31
32 // Non-empty name, group, and proto field id.
33 ASSERT_TRUE(event.name);
34 ASSERT_TRUE(event.group);
35 ASSERT_TRUE(event.proto_field_id);
36
37 // Ftrace id and size should be zeroed.
38 ASSERT_FALSE(event.ftrace_event_id);
39 ASSERT_FALSE(event.size);
40
41 for (const Field& field : event.fields) {
42 // Non-empty name, proto field id, and proto field type.
43 ASSERT_TRUE(field.ftrace_name);
44 ASSERT_TRUE(field.proto_field_id);
45 ASSERT_TRUE(static_cast<int>(field.proto_field_type));
46
47 // Other fields should be zeroed.
48 ASSERT_FALSE(field.ftrace_offset);
49 ASSERT_FALSE(field.ftrace_size);
50 ASSERT_FALSE(field.strategy);
51 ASSERT_FALSE(field.ftrace_type);
52 }
53 }
54 }
55
TEST(EventInfoTest,GetStaticCommonFieldsInfoValidations)56 TEST(EventInfoTest, GetStaticCommonFieldsInfoValidations) {
57 std::vector<Field> fields = GetStaticCommonFieldsInfo();
58 for (const Field& field : fields) {
59 // Non-empty name, group, and proto field id.
60 ASSERT_TRUE(field.ftrace_name);
61 ASSERT_TRUE(field.proto_field_id);
62 ASSERT_TRUE(static_cast<int>(field.proto_field_type));
63
64 // Other fields should be zeroed.
65 ASSERT_FALSE(field.ftrace_offset);
66 ASSERT_FALSE(field.ftrace_size);
67 ASSERT_FALSE(field.strategy);
68 ASSERT_FALSE(field.ftrace_type);
69 }
70 }
71
TEST(EventInfoTest,SetTranslationStrategyValidations)72 TEST(EventInfoTest, SetTranslationStrategyValidations) {
73 TranslationStrategy strategy = kUint32ToUint32;
74 ASSERT_FALSE(SetTranslationStrategy(kFtraceCString, ProtoSchemaType::kUint64,
75 &strategy));
76 ASSERT_EQ(strategy, kUint32ToUint32);
77 ASSERT_TRUE(SetTranslationStrategy(kFtraceCString, ProtoSchemaType::kString,
78 &strategy));
79 ASSERT_EQ(strategy, kCStringToString);
80 ASSERT_TRUE(
81 SetTranslationStrategy(kFtracePid32, ProtoSchemaType::kInt32, &strategy));
82 ASSERT_EQ(strategy, kPid32ToInt32);
83 ASSERT_TRUE(SetTranslationStrategy(kFtraceInode32, ProtoSchemaType::kUint64,
84 &strategy));
85 ASSERT_EQ(strategy, kInode32ToUint64);
86 ASSERT_TRUE(SetTranslationStrategy(kFtraceInode64, ProtoSchemaType::kUint64,
87 &strategy));
88 ASSERT_EQ(strategy, kInode64ToUint64);
89 ASSERT_TRUE(SetTranslationStrategy(kFtraceDevId32, ProtoSchemaType::kUint64,
90 &strategy));
91 ASSERT_EQ(strategy, kDevId32ToUint64);
92 ASSERT_TRUE(SetTranslationStrategy(kFtraceDevId64, ProtoSchemaType::kUint64,
93 &strategy));
94 ASSERT_EQ(strategy, kDevId64ToUint64);
95 ASSERT_TRUE(SetTranslationStrategy(kFtraceCommonPid32,
96 ProtoSchemaType::kInt32, &strategy));
97 ASSERT_EQ(strategy, kCommonPid32ToInt32);
98 }
99
100 } // namespace
101 } // namespace perfetto
102