blob: 3eac5d213c22b3f5d64f0f882004f337f19e4bfd [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"
Yangster-mac94e197c2018-01-02 16:03:03 -080017#include "statsd_test_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
22#include <stdio.h>
23#include <iostream>
24
25using namespace android;
26using namespace android::os::statsd;
27using namespace testing;
28using namespace std;
29
30namespace android {
31namespace os {
32namespace statsd {
33
34static ostream& operator<<(ostream& os, const StatsdConfig& config) {
Yangster-mac94e197c2018-01-02 16:03:03 -080035 return os << "StatsdConfig{id=" << config.id() << "}";
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036}
37
38} // namespace statsd
39} // namespace os
40} // namespace android
41
42/**
43 * Mock ConfigListener
44 */
45class MockListener : public ConfigListener {
46public:
47 MOCK_METHOD2(OnConfigUpdated, void(const ConfigKey& key, const StatsdConfig& config));
48 MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
49};
50
51/**
52 * Validate that the ConfigKey is the one we wanted.
53 */
Yangster-mac94e197c2018-01-02 16:03:03 -080054MATCHER_P2(ConfigKeyEq, uid, id, "") {
55 return arg.GetUid() == uid && (long long)arg.GetId() == (long long)id;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070056}
57
58/**
59 * Validate that the StatsdConfig is the one we wanted.
60 */
Yangster-mac94e197c2018-01-02 16:03:03 -080061MATCHER_P(StatsdConfigEq, id, 0) {
62 return (long long)arg.id() == (long long)id;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070063}
64
Yangster-mac94e197c2018-01-02 16:03:03 -080065const int64_t testConfigId = 12345;
66
Yangster-mac756cd482017-11-21 21:58:44 -080067TEST(ConfigManagerTest, TestFakeConfig) {
Yangster-mac94e197c2018-01-02 16:03:03 -080068 auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, testConfigId),
Yao Chend10f7b12017-12-18 12:53:50 -080069 build_fake_config(), 1000, new UidMap());
Yangster-mac756cd482017-11-21 21:58:44 -080070 EXPECT_TRUE(metricsManager->isConfigValid());
71}
72
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073/**
74 * Test the addOrUpdate and remove methods
75 */
76TEST(ConfigManagerTest, TestAddUpdateRemove) {
77 sp<MockListener> listener = new StrictMock<MockListener>();
78
79 sp<ConfigManager> manager = new ConfigManager();
80 manager->AddListener(listener);
81
82 StatsdConfig config91;
Yangster-mac94e197c2018-01-02 16:03:03 -080083 config91.set_id(91);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070084 StatsdConfig config92;
Yangster-mac94e197c2018-01-02 16:03:03 -080085 config92.set_id(92);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070086 StatsdConfig config93;
Yangster-mac94e197c2018-01-02 16:03:03 -080087 config93.set_id(93);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070088 StatsdConfig config94;
Yangster-mac94e197c2018-01-02 16:03:03 -080089 config94.set_id(94);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070090
91 {
92 InSequence s;
93
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094 manager->Startup();
95
96 // Add another one
Yangster-mac94e197c2018-01-02 16:03:03 -080097 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
98 StatsdConfigEq(91)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070099 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800100 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700101
102 // Update It
Yangster-mac94e197c2018-01-02 16:03:03 -0800103 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
104 StatsdConfigEq(92)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700105 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800106 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700107
108 // Add one with the same uid but a different name
Yangster-mac94e197c2018-01-02 16:03:03 -0800109 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("yyy")),
110 StatsdConfigEq(93)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700111 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800112 manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700113
114 // Add one with the same name but a different uid
Yangster-mac94e197c2018-01-02 16:03:03 -0800115 EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, StringToId("zzz")),
116 StatsdConfigEq(94)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800118 manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119
120 // Remove (1,yyy)
Yangster-mac94e197c2018-01-02 16:03:03 -0800121 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("yyy"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700122 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800123 manager->RemoveConfig(ConfigKey(1, StringToId("yyy")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124
125 // Remove (2,zzz)
Yangster-mac94e197c2018-01-02 16:03:03 -0800126 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700127 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800128 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700129
130 // Remove (1,zzz)
Yangster-mac94e197c2018-01-02 16:03:03 -0800131 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("zzz"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700132 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800133 manager->RemoveConfig(ConfigKey(1, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700134
135 // Remove (2,zzz) again and we shouldn't get the callback
Yangster-mac94e197c2018-01-02 16:03:03 -0800136 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137 }
138}
139
140/**
141 * Test removing all of the configs for a uid.
142 */
143TEST(ConfigManagerTest, TestRemoveUid) {
144 sp<MockListener> listener = new StrictMock<MockListener>();
145
146 sp<ConfigManager> manager = new ConfigManager();
147 manager->AddListener(listener);
148
149 StatsdConfig config;
150
Yao Chen3c0b95c2017-12-16 14:34:20 -0800151 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(5);
Yangster-mac94e197c2018-01-02 16:03:03 -0800152 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("xxx"))));
153 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("yyy"))));
154 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700155
156 manager->Startup();
Yangster-mac94e197c2018-01-02 16:03:03 -0800157 manager->UpdateConfig(ConfigKey(1, StringToId("aaa")), config);
158 manager->UpdateConfig(ConfigKey(2, StringToId("xxx")), config);
159 manager->UpdateConfig(ConfigKey(2, StringToId("yyy")), config);
160 manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config);
161 manager->UpdateConfig(ConfigKey(3, StringToId("bbb")), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700162
163 manager->RemoveConfigs(2);
164}