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, "") {
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     EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("aaa"))));
151     EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(3, StringToId("bbb"))));
152 
153     manager->StartupForTest();
154     manager->UpdateConfig(ConfigKey(1, StringToId("aaa")), config);
155     manager->UpdateConfig(ConfigKey(2, StringToId("xxx")), config);
156     manager->UpdateConfig(ConfigKey(2, StringToId("yyy")), config);
157     manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config);
158     manager->UpdateConfig(ConfigKey(3, StringToId("bbb")), config);
159 
160     manager->RemoveConfigs(2);
161     manager->RemoveConfigs(1);
162     manager->RemoveConfigs(3);
163 }
164 
TEST(ConfigManagerTest,TestSendRestrictedMetricsChangedBroadcast)165 TEST(ConfigManagerTest, TestSendRestrictedMetricsChangedBroadcast) {
166     const string configPackage = "pkg";
167     const int64_t configId = 123;
168     const int32_t callingUid = 456;
169     const vector<int64_t> metricIds = {100, 200, 999};
170 
171     shared_ptr<MockPendingIntentRef> pir = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
172     EXPECT_CALL(*pir, sendRestrictedMetricsChangedBroadcast(metricIds))
173             .Times(1)
174             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
175 
176     sp<ConfigManager> manager = new ConfigManager();
177     manager->SetRestrictedMetricsChangedReceiver(configPackage, configId, callingUid, pir);
178 
179     manager->SendRestrictedMetricsBroadcast({configPackage}, configId, {callingUid}, metricIds);
180 }
181 
TEST(ConfigManagerTest,TestSendRestrictedMetricsChangedBroadcastMultipleConfigs)182 TEST(ConfigManagerTest, TestSendRestrictedMetricsChangedBroadcastMultipleConfigs) {
183     const string package1 = "pkg1", package2 = "pkg2", package3 = "pkg3";
184     const int64_t configId = 123;
185     const int32_t callingUid1 = 456, callingUid2 = 789, callingUid3 = 1111;
186     const vector<int64_t> metricIds1 = {100, 200, 999}, metricIds2 = {101}, metricIds3 = {202, 101};
187 
188     shared_ptr<MockPendingIntentRef> pir1 = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
189     EXPECT_CALL(*pir1, sendRestrictedMetricsChangedBroadcast(metricIds1))
190             .Times(3)
191             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
192     EXPECT_CALL(*pir1, sendRestrictedMetricsChangedBroadcast(metricIds2))
193             .Times(1)
194             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
195 
196     shared_ptr<MockPendingIntentRef> pir2 = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
197     EXPECT_CALL(*pir2, sendRestrictedMetricsChangedBroadcast(metricIds1))
198             .Times(1)
199             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
200 
201     shared_ptr<MockPendingIntentRef> pir3 = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
202     EXPECT_CALL(*pir3, sendRestrictedMetricsChangedBroadcast(metricIds1))
203             .Times(1)
204             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
205     EXPECT_CALL(*pir3, sendRestrictedMetricsChangedBroadcast(metricIds2))
206             .Times(1)
207             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
208 
209     shared_ptr<MockPendingIntentRef> pir4 = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
210     EXPECT_CALL(*pir4, sendRestrictedMetricsChangedBroadcast(metricIds1))
211             .Times(2)
212             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
213 
214     shared_ptr<MockPendingIntentRef> pir5 = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
215     EXPECT_CALL(*pir5, sendRestrictedMetricsChangedBroadcast(metricIds3))
216             .Times(1)
217             .WillRepeatedly(Invoke([](const vector<int64_t>&) { return Status::ok(); }));
218 
219     sp<ConfigManager> manager = new ConfigManager();
220     manager->SetRestrictedMetricsChangedReceiver(package1, configId, callingUid1, pir1);
221     manager->SetRestrictedMetricsChangedReceiver(package2, configId, callingUid2, pir2);
222     manager->SetRestrictedMetricsChangedReceiver(package1, configId, callingUid2, pir3);
223     manager->SetRestrictedMetricsChangedReceiver(package2, configId, callingUid1, pir4);
224     manager->SetRestrictedMetricsChangedReceiver(package3, configId, callingUid3, pir5);
225 
226     // Invoke pir 1, 2, 3, 4.
227     manager->SendRestrictedMetricsBroadcast({package1, package2}, configId,
228                                             {callingUid1, callingUid2}, metricIds1);
229     // Invoke pir 1 only
230     manager->SendRestrictedMetricsBroadcast({package1}, configId, {callingUid1}, metricIds1);
231     // Invoke pir 1, 4.
232     manager->SendRestrictedMetricsBroadcast({package1, package2}, configId, {callingUid1},
233                                             metricIds1);
234     // Invoke pir 1, 3
235     manager->SendRestrictedMetricsBroadcast({package1}, configId, {callingUid1, callingUid2},
236                                             metricIds2);
237     // Invoke pir 5
238     manager->SendRestrictedMetricsBroadcast({package3}, configId, {callingUid1, callingUid3},
239                                             metricIds3);
240 }
241 
TEST(ConfigManagerTest,TestRemoveRestrictedMetricsChangedBroadcast)242 TEST(ConfigManagerTest, TestRemoveRestrictedMetricsChangedBroadcast) {
243     const string configPackage = "pkg";
244     const int64_t configId = 123;
245     const int32_t callingUid = 456;
246     const vector<int64_t> metricIds = {100, 200, 999};
247 
248     shared_ptr<MockPendingIntentRef> pir = SharedRefBase::make<StrictMock<MockPendingIntentRef>>();
249     EXPECT_CALL(*pir, sendRestrictedMetricsChangedBroadcast(metricIds)).Times(0);
250 
251     sp<ConfigManager> manager = new ConfigManager();
252     manager->SetRestrictedMetricsChangedReceiver(configPackage, configId, callingUid, pir);
253     manager->RemoveRestrictedMetricsChangedReceiver(configPackage, configId, callingUid);
254 
255     manager->SendRestrictedMetricsBroadcast({configPackage}, configId, {callingUid}, metricIds);
256 }