blob: 3d923e246e716a65468a2862e33f01bffdb42c1b [file] [log] [blame]
Joe Onorato9fc9edf2017-10-15 20:08:52 -07001// 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"
Yangster-mac756cd482017-11-21 21:58:44 -080016#include "src/metrics/MetricsManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017
18#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20
21#include <stdio.h>
22#include <iostream>
23
24using namespace android;
25using namespace android::os::statsd;
26using namespace testing;
27using namespace std;
28
29namespace android {
30namespace os {
31namespace statsd {
32
33static ostream& operator<<(ostream& os, const StatsdConfig& config) {
Yangster-macd1815dc2017-11-13 21:43:15 -080034 return os << "StatsdConfig{name=" << config.name().c_str() << "}";
Joe Onorato9fc9edf2017-10-15 20:08:52 -070035}
36
37} // namespace statsd
38} // namespace os
39} // namespace android
40
41/**
42 * Mock ConfigListener
43 */
44class MockListener : public ConfigListener {
45public:
46 MOCK_METHOD2(OnConfigUpdated, void(const ConfigKey& key, const StatsdConfig& config));
47 MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
48};
49
50/**
51 * Validate that the ConfigKey is the one we wanted.
52 */
53MATCHER_P2(ConfigKeyEq, uid, name, "") {
54 return arg.GetUid() == uid && arg.GetName() == name;
55}
56
57/**
58 * Validate that the StatsdConfig is the one we wanted.
59 */
Yangster-macd1815dc2017-11-13 21:43:15 -080060MATCHER_P(StatsdConfigEq, name, "") {
61 return arg.name() == name;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070062}
63
Yangster-mac756cd482017-11-21 21:58:44 -080064TEST(ConfigManagerTest, TestFakeConfig) {
Yao Chend10f7b12017-12-18 12:53:50 -080065 auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, "test"),
66 build_fake_config(), 1000, new UidMap());
Yangster-mac756cd482017-11-21 21:58:44 -080067 EXPECT_TRUE(metricsManager->isConfigValid());
68}
69
Joe Onorato9fc9edf2017-10-15 20:08:52 -070070/**
71 * Test the addOrUpdate and remove methods
72 */
73TEST(ConfigManagerTest, TestAddUpdateRemove) {
74 sp<MockListener> listener = new StrictMock<MockListener>();
75
76 sp<ConfigManager> manager = new ConfigManager();
77 manager->AddListener(listener);
78
79 StatsdConfig config91;
Yangster-macd1815dc2017-11-13 21:43:15 -080080 config91.set_name("91");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 StatsdConfig config92;
Yangster-macd1815dc2017-11-13 21:43:15 -080082 config92.set_name("92");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070083 StatsdConfig config93;
Yangster-macd1815dc2017-11-13 21:43:15 -080084 config93.set_name("93");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070085 StatsdConfig config94;
Yangster-macd1815dc2017-11-13 21:43:15 -080086 config94.set_name("94");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070087
88 {
89 InSequence s;
90
Joe Onorato9fc9edf2017-10-15 20:08:52 -070091 manager->Startup();
92
93 // Add another one
Yangster-macd1815dc2017-11-13 21:43:15 -080094 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("91")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070095 .RetiresOnSaturation();
96 manager->UpdateConfig(ConfigKey(1, "zzz"), config91);
97
98 // Update It
Yangster-macd1815dc2017-11-13 21:43:15 -080099 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("92")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700100 .RetiresOnSaturation();
101 manager->UpdateConfig(ConfigKey(1, "zzz"), config92);
102
103 // Add one with the same uid but a different name
Yangster-macd1815dc2017-11-13 21:43:15 -0800104 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "yyy"), StatsdConfigEq("93")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700105 .RetiresOnSaturation();
106 manager->UpdateConfig(ConfigKey(1, "yyy"), config93);
107
108 // Add one with the same name but a different uid
Yangster-macd1815dc2017-11-13 21:43:15 -0800109 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, "zzz"), StatsdConfigEq("94")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700110 .RetiresOnSaturation();
111 manager->UpdateConfig(ConfigKey(2, "zzz"), config94);
112
113 // Remove (1,yyy)
114 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "yyy")))
115 .RetiresOnSaturation();
116 manager->RemoveConfig(ConfigKey(1, "yyy"));
117
118 // Remove (2,zzz)
119 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")))
120 .RetiresOnSaturation();
121 manager->RemoveConfig(ConfigKey(2, "zzz"));
122
123 // Remove (1,zzz)
124 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "zzz")))
125 .RetiresOnSaturation();
126 manager->RemoveConfig(ConfigKey(1, "zzz"));
127
128 // Remove (2,zzz) again and we shouldn't get the callback
129 manager->RemoveConfig(ConfigKey(2, "zzz"));
130 }
131}
132
133/**
134 * Test removing all of the configs for a uid.
135 */
136TEST(ConfigManagerTest, TestRemoveUid) {
137 sp<MockListener> listener = new StrictMock<MockListener>();
138
139 sp<ConfigManager> manager = new ConfigManager();
140 manager->AddListener(listener);
141
142 StatsdConfig config;
143
Yao Chen3c0b95c2017-12-16 14:34:20 -0800144 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(5);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700145 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "xxx")));
146 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "yyy")));
147 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")));
148
149 manager->Startup();
150 manager->UpdateConfig(ConfigKey(1, "aaa"), config);
151 manager->UpdateConfig(ConfigKey(2, "xxx"), config);
152 manager->UpdateConfig(ConfigKey(2, "yyy"), config);
153 manager->UpdateConfig(ConfigKey(2, "zzz"), config);
154 manager->UpdateConfig(ConfigKey(3, "bbb"), config);
155
156 manager->RemoveConfigs(2);
157}