blob: f570522dcd0aa262a05b7e91a65395cbc9de8486 [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"
16
17#include <gmock/gmock.h>
18#include <gtest/gtest.h>
19
20#include <stdio.h>
21#include <iostream>
22
23using namespace android;
24using namespace android::os::statsd;
25using namespace testing;
26using namespace std;
27
28namespace android {
29namespace os {
30namespace statsd {
31
32static ostream& operator<<(ostream& os, const StatsdConfig& config) {
Yangster-macd1815dc2017-11-13 21:43:15 -080033 return os << "StatsdConfig{name=" << config.name().c_str() << "}";
Joe Onorato9fc9edf2017-10-15 20:08:52 -070034}
35
36} // namespace statsd
37} // namespace os
38} // namespace android
39
40/**
41 * Mock ConfigListener
42 */
43class MockListener : public ConfigListener {
44public:
45 MOCK_METHOD2(OnConfigUpdated, void(const ConfigKey& key, const StatsdConfig& config));
46 MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
47};
48
49/**
50 * Validate that the ConfigKey is the one we wanted.
51 */
52MATCHER_P2(ConfigKeyEq, uid, name, "") {
53 return arg.GetUid() == uid && arg.GetName() == name;
54}
55
56/**
57 * Validate that the StatsdConfig is the one we wanted.
58 */
Yangster-macd1815dc2017-11-13 21:43:15 -080059MATCHER_P(StatsdConfigEq, name, "") {
60 return arg.name() == name;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061}
62
63/**
64 * Test the addOrUpdate and remove methods
65 */
66TEST(ConfigManagerTest, TestAddUpdateRemove) {
67 sp<MockListener> listener = new StrictMock<MockListener>();
68
69 sp<ConfigManager> manager = new ConfigManager();
70 manager->AddListener(listener);
71
72 StatsdConfig config91;
Yangster-macd1815dc2017-11-13 21:43:15 -080073 config91.set_name("91");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074 StatsdConfig config92;
Yangster-macd1815dc2017-11-13 21:43:15 -080075 config92.set_name("92");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070076 StatsdConfig config93;
Yangster-macd1815dc2017-11-13 21:43:15 -080077 config93.set_name("93");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070078 StatsdConfig config94;
Yangster-macd1815dc2017-11-13 21:43:15 -080079 config94.set_name("94");
Joe Onorato9fc9edf2017-10-15 20:08:52 -070080
81 {
82 InSequence s;
83
84 // The built-in fake one.
85 // TODO: Remove this when we get rid of the fake one, and make this
86 // test loading one from disk somewhere.
87 EXPECT_CALL(*(listener.get()),
Yao Chenff263282017-11-18 12:03:42 -080088 OnConfigUpdated(ConfigKeyEq(1000, "fake"), StatsdConfigEq("12345")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070089 .RetiresOnSaturation();
90 manager->Startup();
91
92 // Add another one
Yangster-macd1815dc2017-11-13 21:43:15 -080093 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("91")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094 .RetiresOnSaturation();
95 manager->UpdateConfig(ConfigKey(1, "zzz"), config91);
96
97 // Update It
Yangster-macd1815dc2017-11-13 21:43:15 -080098 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("92")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070099 .RetiresOnSaturation();
100 manager->UpdateConfig(ConfigKey(1, "zzz"), config92);
101
102 // Add one with the same uid but a different name
Yangster-macd1815dc2017-11-13 21:43:15 -0800103 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "yyy"), StatsdConfigEq("93")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700104 .RetiresOnSaturation();
105 manager->UpdateConfig(ConfigKey(1, "yyy"), config93);
106
107 // Add one with the same name but a different uid
Yangster-macd1815dc2017-11-13 21:43:15 -0800108 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, "zzz"), StatsdConfigEq("94")))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700109 .RetiresOnSaturation();
110 manager->UpdateConfig(ConfigKey(2, "zzz"), config94);
111
112 // Remove (1,yyy)
113 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "yyy")))
114 .RetiresOnSaturation();
115 manager->RemoveConfig(ConfigKey(1, "yyy"));
116
117 // Remove (2,zzz)
118 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")))
119 .RetiresOnSaturation();
120 manager->RemoveConfig(ConfigKey(2, "zzz"));
121
122 // Remove (1,zzz)
123 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "zzz")))
124 .RetiresOnSaturation();
125 manager->RemoveConfig(ConfigKey(1, "zzz"));
126
127 // Remove (2,zzz) again and we shouldn't get the callback
128 manager->RemoveConfig(ConfigKey(2, "zzz"));
129 }
130}
131
132/**
133 * Test removing all of the configs for a uid.
134 */
135TEST(ConfigManagerTest, TestRemoveUid) {
136 sp<MockListener> listener = new StrictMock<MockListener>();
137
138 sp<ConfigManager> manager = new ConfigManager();
139 manager->AddListener(listener);
140
141 StatsdConfig config;
142
143 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(6);
144 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "xxx")));
145 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "yyy")));
146 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")));
147
148 manager->Startup();
149 manager->UpdateConfig(ConfigKey(1, "aaa"), config);
150 manager->UpdateConfig(ConfigKey(2, "xxx"), config);
151 manager->UpdateConfig(ConfigKey(2, "yyy"), config);
152 manager->UpdateConfig(ConfigKey(2, "zzz"), config);
153 manager->UpdateConfig(ConfigKey(3, "bbb"), config);
154
155 manager->RemoveConfigs(2);
156}