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" |
| 16 | |
| 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <iostream> |
| 22 | |
| 23 | using namespace android; |
| 24 | using namespace android::os::statsd; |
| 25 | using namespace testing; |
| 26 | using namespace std; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
| 32 | static ostream& operator<<(ostream& os, const StatsdConfig& config) { |
| 33 | return os << "StatsdConfig{id=" << config.config_id() << "}"; |
| 34 | } |
| 35 | |
| 36 | } // namespace statsd |
| 37 | } // namespace os |
| 38 | } // namespace android |
| 39 | |
| 40 | /** |
| 41 | * Mock ConfigListener |
| 42 | */ |
| 43 | class MockListener : public ConfigListener { |
| 44 | public: |
| 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 | */ |
| 52 | MATCHER_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 | */ |
| 59 | MATCHER_P(StatsdConfigEq, configId, "") { |
| 60 | return arg.config_id() == configId; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Test the addOrUpdate and remove methods |
| 65 | */ |
| 66 | TEST(ConfigManagerTest, TestAddUpdateRemove) { |
| 67 | sp<MockListener> listener = new StrictMock<MockListener>(); |
| 68 | |
| 69 | sp<ConfigManager> manager = new ConfigManager(); |
| 70 | manager->AddListener(listener); |
| 71 | |
| 72 | StatsdConfig config91; |
| 73 | config91.set_config_id(91); |
| 74 | StatsdConfig config92; |
| 75 | config92.set_config_id(92); |
| 76 | StatsdConfig config93; |
| 77 | config93.set_config_id(93); |
| 78 | StatsdConfig config94; |
| 79 | config94.set_config_id(94); |
| 80 | |
| 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()), |
| 88 | OnConfigUpdated(ConfigKeyEq(0, "fake"), StatsdConfigEq(12345))) |
| 89 | .RetiresOnSaturation(); |
| 90 | manager->Startup(); |
| 91 | |
| 92 | // Add another one |
| 93 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq(91))) |
| 94 | .RetiresOnSaturation(); |
| 95 | manager->UpdateConfig(ConfigKey(1, "zzz"), config91); |
| 96 | |
| 97 | // Update It |
| 98 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq(92))) |
| 99 | .RetiresOnSaturation(); |
| 100 | manager->UpdateConfig(ConfigKey(1, "zzz"), config92); |
| 101 | |
| 102 | // Add one with the same uid but a different name |
| 103 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "yyy"), StatsdConfigEq(93))) |
| 104 | .RetiresOnSaturation(); |
| 105 | manager->UpdateConfig(ConfigKey(1, "yyy"), config93); |
| 106 | |
| 107 | // Add one with the same name but a different uid |
| 108 | EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, "zzz"), StatsdConfigEq(94))) |
| 109 | .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 | */ |
| 135 | TEST(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 | } |