1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/config/ConfigManager.h"
#include "src/metrics/MetricsManager.h"
#include "statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <iostream>
using namespace android;
using namespace android::os::statsd;
using namespace testing;
using namespace std;
namespace android {
namespace os {
namespace statsd {
static ostream& operator<<(ostream& os, const StatsdConfig& config) {
return os << "StatsdConfig{id=" << config.id() << "}";
}
} // namespace statsd
} // namespace os
} // namespace android
/**
* Mock ConfigListener
*/
class MockListener : public ConfigListener {
public:
MOCK_METHOD2(OnConfigUpdated, void(const ConfigKey& key, const StatsdConfig& config));
MOCK_METHOD1(OnConfigRemoved, void(const ConfigKey& key));
};
/**
* Validate that the ConfigKey is the one we wanted.
*/
MATCHER_P2(ConfigKeyEq, uid, id, "") {
return arg.GetUid() == uid && (long long)arg.GetId() == (long long)id;
}
/**
* Validate that the StatsdConfig is the one we wanted.
*/
MATCHER_P(StatsdConfigEq, id, 0) {
return (long long)arg.id() == (long long)id;
}
const int64_t testConfigId = 12345;
TEST(ConfigManagerTest, TestFakeConfig) {
auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, testConfigId),
build_fake_config(), 1000, new UidMap());
EXPECT_TRUE(metricsManager->isConfigValid());
}
/**
* Test the addOrUpdate and remove methods
*/
TEST(ConfigManagerTest, TestAddUpdateRemove) {
sp<MockListener> listener = new StrictMock<MockListener>();
sp<ConfigManager> manager = new ConfigManager();
manager->AddListener(listener);
StatsdConfig config91;
config91.set_id(91);
StatsdConfig config92;
config92.set_id(92);
StatsdConfig config93;
config93.set_id(93);
StatsdConfig config94;
config94.set_id(94);
{
InSequence s;
manager->StartupForTest();
// Add another one
EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
StatsdConfigEq(91)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
// Update It
EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
StatsdConfigEq(92)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
// Add one with the same uid but a different name
EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("yyy")),
StatsdConfigEq(93)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
// Add one with the same name but a different uid
EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, StringToId("zzz")),
StatsdConfigEq(94)))
.RetiresOnSaturation();
manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
// Remove (1,yyy)
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("yyy"))))
.RetiresOnSaturation();
manager->RemoveConfig(ConfigKey(1, StringToId("yyy")));
// Remove (2,zzz)
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))))
.RetiresOnSaturation();
manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
// Remove (1,zzz)
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("zzz"))))
.RetiresOnSaturation();
manager->RemoveConfig(ConfigKey(1, StringToId("zzz")));
// Remove (2,zzz) again and we shouldn't get the callback
manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
}
}
/**
* Test removing all of the configs for a uid.
*/
TEST(ConfigManagerTest, TestRemoveUid) {
sp<MockListener> listener = new StrictMock<MockListener>();
sp<ConfigManager> manager = new ConfigManager();
manager->AddListener(listener);
StatsdConfig config;
EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(5);
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("xxx"))));
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("yyy"))));
EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))));
manager->StartupForTest();
manager->UpdateConfig(ConfigKey(1, StringToId("aaa")), config);
manager->UpdateConfig(ConfigKey(2, StringToId("xxx")), config);
manager->UpdateConfig(ConfigKey(2, StringToId("yyy")), config);
manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config);
manager->UpdateConfig(ConfigKey(3, StringToId("bbb")), config);
manager->RemoveConfigs(2);
}
|