blob: 9455304a1af6a359161544b1fe3111370aa96e28 [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:
Yangster-macc04feba2018-04-02 14:37:33 -070047 MOCK_METHOD3(OnConfigUpdated, void(const int64_t timestampNs, const ConfigKey& key,
48 const StatsdConfig& config));
Joe Onorato9fc9edf2017-10-15 20:08:52 -070049 MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
50};
51
52/**
53 * Validate that the ConfigKey is the one we wanted.
54 */
Yangster-mac94e197c2018-01-02 16:03:03 -080055MATCHER_P2(ConfigKeyEq, uid, id, "") {
56 return arg.GetUid() == uid && (long long)arg.GetId() == (long long)id;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057}
58
59/**
60 * Validate that the StatsdConfig is the one we wanted.
61 */
Yangster-mac94e197c2018-01-02 16:03:03 -080062MATCHER_P(StatsdConfigEq, id, 0) {
63 return (long long)arg.id() == (long long)id;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070064}
65
Yangster-mac94e197c2018-01-02 16:03:03 -080066const int64_t testConfigId = 12345;
67
Joe Onorato9fc9edf2017-10-15 20:08:52 -070068/**
69 * Test the addOrUpdate and remove methods
70 */
71TEST(ConfigManagerTest, TestAddUpdateRemove) {
72 sp<MockListener> listener = new StrictMock<MockListener>();
73
74 sp<ConfigManager> manager = new ConfigManager();
75 manager->AddListener(listener);
76
77 StatsdConfig config91;
Yangster-mac94e197c2018-01-02 16:03:03 -080078 config91.set_id(91);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070079 StatsdConfig config92;
Yangster-mac94e197c2018-01-02 16:03:03 -080080 config92.set_id(92);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 StatsdConfig config93;
Yangster-mac94e197c2018-01-02 16:03:03 -080082 config93.set_id(93);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070083 StatsdConfig config94;
Yangster-mac94e197c2018-01-02 16:03:03 -080084 config94.set_id(94);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070085
86 {
87 InSequence s;
88
yro6e304ec2018-01-16 21:00:30 -080089 manager->StartupForTest();
Joe Onorato9fc9edf2017-10-15 20:08:52 -070090
91 // Add another one
Yangster-macc04feba2018-04-02 14:37:33 -070092 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")),
Yangster-mac94e197c2018-01-02 16:03:03 -080093 StatsdConfigEq(91)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -080095 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070096
97 // Update It
Yangster-macc04feba2018-04-02 14:37:33 -070098 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, ConfigKeyEq(1, StringToId("zzz")),
Yangster-mac94e197c2018-01-02 16:03:03 -080099 StatsdConfigEq(92)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700100 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800101 manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700102
103 // Add one with the same uid but a different name
Yangster-macc04feba2018-04-02 14:37:33 -0700104 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, ConfigKeyEq(1, StringToId("yyy")),
Yangster-mac94e197c2018-01-02 16:03:03 -0800105 StatsdConfigEq(93)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700106 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800107 manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700108
109 // Add one with the same name but a different uid
Yangster-macc04feba2018-04-02 14:37:33 -0700110 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, ConfigKeyEq(2, StringToId("zzz")),
Yangster-mac94e197c2018-01-02 16:03:03 -0800111 StatsdConfigEq(94)))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700112 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800113 manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114
115 // Remove (1,yyy)
Yangster-mac94e197c2018-01-02 16:03:03 -0800116 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("yyy"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800118 manager->RemoveConfig(ConfigKey(1, StringToId("yyy")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700119
120 // Remove (2,zzz)
Yangster-mac94e197c2018-01-02 16:03:03 -0800121 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700122 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800123 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700124
125 // Remove (1,zzz)
Yangster-mac94e197c2018-01-02 16:03:03 -0800126 EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("zzz"))))
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700127 .RetiresOnSaturation();
Yangster-mac94e197c2018-01-02 16:03:03 -0800128 manager->RemoveConfig(ConfigKey(1, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700129
130 // Remove (2,zzz) again and we shouldn't get the callback
Yangster-mac94e197c2018-01-02 16:03:03 -0800131 manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700132 }
133}
134
135/**
136 * Test removing all of the configs for a uid.
137 */
138TEST(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
Yangster-macc04feba2018-04-02 14:37:33 -0700146 EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _, _)).Times(5);
Yangster-mac94e197c2018-01-02 16:03:03 -0800147 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"))));
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700150
yro469cd802018-01-04 14:57:45 -0800151 manager->StartupForTest();
Yangster-mac94e197c2018-01-02 16:03:03 -0800152 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);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700157
158 manager->RemoveConfigs(2);
159}