Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1 | // 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-mac | 756cd48 | 2017-11-21 21:58:44 -0800 | [diff] [blame] | 16 | #include "src/metrics/MetricsManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <iostream> |
| 23 | |
| 24 | using namespace android; |
| 25 | using namespace android::os::statsd; |
| 26 | using namespace testing; |
| 27 | using namespace std; |
| 28 | |
| 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace statsd { |
| 32 | |
| 33 | static ostream& operator<<(ostream& os, const StatsdConfig& config) { |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 34 | return os << "StatsdConfig{name=" << config.name().c_str() << "}"; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace statsd |
| 38 | } // namespace os |
| 39 | } // namespace android |
| 40 | |
| 41 | /** |
| 42 | * Mock ConfigListener |
| 43 | */ |
| 44 | class MockListener : public ConfigListener { |
| 45 | public: |
| 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 | */ |
| 53 | MATCHER_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-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 60 | MATCHER_P(StatsdConfigEq, name, "") { |
| 61 | return arg.name() == name; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Yangster-mac | 756cd48 | 2017-11-21 21:58:44 -0800 | [diff] [blame] | 64 | TEST(ConfigManagerTest, TestFakeConfig) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 65 | auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, "test"), |
| 66 | build_fake_config(), 1000, new UidMap()); |
Yangster-mac | 756cd48 | 2017-11-21 21:58:44 -0800 | [diff] [blame] | 67 | EXPECT_TRUE(metricsManager->isConfigValid()); |
| 68 | } |
| 69 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Test the addOrUpdate and remove methods |
| 72 | */ |
| 73 | TEST(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-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 80 | config91.set_name("91"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 81 | StatsdConfig config92; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 82 | config92.set_name("92"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 83 | StatsdConfig config93; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 84 | config93.set_name("93"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 85 | StatsdConfig config94; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 86 | config94.set_name("94"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 87 | |
| 88 | { |
| 89 | InSequence s; |
| 90 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 91 | manager->Startup(); |
| 92 | |
| 93 | // Add another one |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 94 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("91"))) |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 95 | .RetiresOnSaturation(); |
| 96 | manager->UpdateConfig(ConfigKey(1, "zzz"), config91); |
| 97 | |
| 98 | // Update It |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 99 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("92"))) |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 100 | .RetiresOnSaturation(); |
| 101 | manager->UpdateConfig(ConfigKey(1, "zzz"), config92); |
| 102 | |
| 103 | // Add one with the same uid but a different name |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 104 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "yyy"), StatsdConfigEq("93"))) |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 105 | .RetiresOnSaturation(); |
| 106 | manager->UpdateConfig(ConfigKey(1, "yyy"), config93); |
| 107 | |
| 108 | // Add one with the same name but a different uid |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 109 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, "zzz"), StatsdConfigEq("94"))) |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 110 | .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 | */ |
| 136 | TEST(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 Chen | 3c0b95c | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 144 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(5); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 145 | 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 | } |