1 // Copyright (C) 2017 The Android Open Source Project
2 //
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 "src/config/ConfigManager.h"
16 #include "src/metrics/MetricsManager.h"
17 #include "statsd_test_util.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <stdio.h>
23 #include <iostream>
24
25 using namespace android;
26 using namespace android::os::statsd;
27 using namespace testing;
28 using namespace std;
29
30 namespace android {
31 namespace os {
32 namespace statsd {
33
operator <<(ostream & os,const StatsdConfig & config)34 static ostream& operator<<(ostream& os, const StatsdConfig& config) {
35 return os << "StatsdConfig{id=" << config.id() << "}";
36 }
37
38 } // namespace statsd
39 } // namespace os
40 } // namespace android
41
42 /**
43 * Mock ConfigListener
44 */
45 class MockListener : public ConfigListener {
46 public:
47 MOCK_METHOD4(OnConfigUpdated, void(const int64_t timestampNs, const ConfigKey& key,
48 const StatsdConfig& config, bool modularUpdate));
49 MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
50 };
51
52 /**
53 * Validate that the ConfigKey is the one we wanted.
54 */
55 MATCHER_P2(ConfigKeyEq, uid, id, "") {
56 return arg.GetUid() == uid && (long long)arg.GetId() == (long long)id;
57 }
58
59 /**
60 * Validate that the StatsdConfig is the one we wanted.
61 */
62 MATCHER_P(StatsdConfigEq, id, 0) {
63 return (long long)arg.id() == (long long)id;
64 }
65
66 const int64_t testConfigId = 12345;
67
68 /**
69 * Test the addOrUpdate and remove methods
70 */
TEST(ConfigManagerTest,TestAddUpdateRemove)71 TEST(ConfigManagerTest, TestAddUpdateRemove) {
72 sp<MockListener> listener = new StrictMock<MockListener>();
73
74 sp<ConfigManager> manager = new ConfigManager();
75 manager->AddListener(listener);
76
77 StatsdConfig config91;
78 config91.set_id(91);
79 StatsdConfig config92;
80 config92.set_id(92);
81 StatsdConfig config93;
82 config93.set_id(93);
83 StatsdConfig config94;
84 config94.set_id(94);
85
86 {
87 InSequence s;
88
89 manager->StartupForTest();
90
91 // Add another one
92 EXPECT_CALL(*(listener.get()),
93 OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(91), true))
94 .RetiresOnSaturation();
95 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
96
97 // Update It
98 EXPECT_CALL(*(listener.get()),
99 OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")), StatsdConfigEq(92), true))
100 .RetiresOnSaturation();
101 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
102
103 // Add one with the same uid but a different name
104 EXPECT_CALL(*(listener.get()),
105 OnConfigUpdated(_, ConfigKeyEq(1, StringToId("yyy")), StatsdConfigEq(93), true))
106 .RetiresOnSaturation();
107 manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
108
109 // Add one with the same name but a different uid
110 EXPECT_CALL(*(listener.get()),
111 OnConfigUpdated(_, ConfigKeyEq(2, StringToId("zzz")), StatsdConfigEq(94), true))
112 .RetiresOnSaturation();
113 manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
114
115 // Remove (1,yyy)
116 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("yyy"))))
117 .RetiresOnSaturation();
118 manager->RemoveConfig(ConfigKey(1, StringToId("yyy")));
119
120 // Remove (2,zzz)
121 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))))
122 .RetiresOnSaturation();
123 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
124
125 // Remove (1,zzz)
126 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("zzz"))))
127 .RetiresOnSaturation();
128 manager->RemoveConfig(ConfigKey(1, StringToId("zzz")));
129
130 // Remove (2,zzz) again and we shouldn't get the callback
131 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
132 }
133 }
134
135 /**
136 * Test removing all of the configs for a uid.
137 */
TEST(ConfigManagerTest,TestRemoveUid)138 TEST(ConfigManagerTest, TestRemoveUid) {
139 sp<MockListener> listener = new StrictMock<MockListener>();
140
141 sp<ConfigManager> manager = new ConfigManager();
142 manager->AddListener(listener);
143
144 StatsdConfig config;
145
146 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _, _, true)).Times(5);
147 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("xxx"))));
148 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("yyy"))));
149 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))));
150
151 manager->StartupForTest();
152 manager->UpdateConfig(ConfigKey(1, StringToId("aaa")), config);
153 manager->UpdateConfig(ConfigKey(2, StringToId("xxx")), config);
154 manager->UpdateConfig(ConfigKey(2, StringToId("yyy")), config);
155 manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config);
156 manager->UpdateConfig(ConfigKey(3, StringToId("bbb")), config);
157
158 manager->RemoveConfigs(2);
159 }
160