1 /*
2 * Copyright 2020 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 "dumpsys/filter.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <list>
23 #include <queue>
24
25 #include "dumpsys/dumpsys_test_data.h"
26 #include "test_data/bar.h"
27 #include "test_data/baz.h"
28 #include "test_data/foo.h"
29 #include "test_data/qux.h"
30 #include "test_data/root.h"
31
32 using namespace bluetooth;
33
34 namespace testing {
35
36 class DumpsysFilterTest : public Test {
37 protected:
SetUp()38 void SetUp() override {
39 test_data_classes_.push_back(std::make_unique<BarTestDataClass>());
40 test_data_classes_.push_back(std::make_unique<BazTestDataClass>());
41 test_data_classes_.push_back(std::make_unique<FooTestDataClass>());
42 test_data_classes_.push_back(std::make_unique<QuxTestDataClass>());
43 }
44
TearDown()45 void TearDown() override {}
46
47 std::list<std::unique_ptr<DumpsysTestDataClass>> test_data_classes_;
48
49 std::string PopulateTestSchema();
50 };
51
PopulateTestSchema()52 std::string DumpsysFilterTest::PopulateTestSchema() {
53 flatbuffers::FlatBufferBuilder fb_builder(1024);
54
55 auto string_private = fb_builder.CreateString("String private");
56 auto string_opaque = fb_builder.CreateString("String opaque");
57 auto string_anonymized = fb_builder.CreateString("String anonymized");
58 auto string_any = fb_builder.CreateString("String any");
59
60 std::queue<TableAddFunction> queue;
61 for (auto& test_data_class : test_data_classes_) {
62 queue.push(test_data_class->GetTable(fb_builder));
63 }
64
65 testing::DumpsysTestDataRootBuilder builder(fb_builder);
66
67 builder.add_string_private(string_private);
68 builder.add_string_opaque(string_opaque);
69 builder.add_string_anonymized(string_anonymized);
70 builder.add_string_any(string_any);
71
72 builder.add_int_private(123);
73 builder.add_int_opaque(456);
74 builder.add_int_anonymized(789);
75 builder.add_int_any(0xabc);
76
77 while (!queue.empty()) {
78 queue.front()(&builder);
79 queue.pop();
80 }
81 fb_builder.Finish(builder.Finish());
82
83 return std::string(fb_builder.GetBufferPointer(), fb_builder.GetBufferPointer() + fb_builder.GetSize());
84 }
85
TEST_F(DumpsysFilterTest,filter_as_developer)86 TEST_F(DumpsysFilterTest, filter_as_developer) {
87 std::string dumpsys_data = PopulateTestSchema();
88 dumpsys::ReflectionSchema reflection_schema(testing::GetBundledSchemaData());
89
90 dumpsys::FilterSchema(reflection_schema, &dumpsys_data);
91
92 const testing::DumpsysTestDataRoot* data_root = GetDumpsysTestDataRoot(dumpsys_data.data());
93
94 ASSERT_TRUE(data_root->string_private()->str() == "String private");
95 ASSERT_TRUE(data_root->string_opaque()->str() == "String opaque");
96 ASSERT_TRUE(data_root->string_anonymized()->str() == "String anonymized");
97 ASSERT_TRUE(data_root->string_any()->str() == "String any");
98
99 ASSERT_TRUE(data_root->int_private() == 123);
100 ASSERT_TRUE(data_root->int_opaque() == 456);
101 ASSERT_TRUE(data_root->int_anonymized() == 789);
102 ASSERT_TRUE(data_root->int_any() == 0xabc);
103
104 ASSERT_EQ(nullptr, data_root->bar_module_data());
105
106 const testing::FooTestSchema* foo = data_root->foo_module_data();
107
108 ASSERT_EQ(123, foo->foo_int_private());
109 ASSERT_EQ(123, foo->foo_int_opaque());
110 ASSERT_EQ(123, foo->foo_int_anonymized());
111 ASSERT_EQ(123, foo->foo_int_any());
112 ASSERT_STREQ("123", foo->foo_int_string()->c_str());
113
114 ASSERT_FLOAT_EQ(123.456, foo->foo_float_private());
115 ASSERT_FLOAT_EQ(123.456, foo->foo_float_opaque());
116 ASSERT_FLOAT_EQ(123.456, foo->foo_float_anonymized());
117 ASSERT_FLOAT_EQ(123.456, foo->foo_float_any());
118 ASSERT_STREQ("123.456", foo->foo_float_string()->c_str());
119 }
120
121 } // namespace testing
122