David Chen | de70169 | 2017-10-05 13:16:02 -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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 15 | #include "packages/UidMap.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 16 | #include "StatsLogProcessor.h" |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 17 | #include "config/ConfigKey.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 18 | #include "logd/LogEvent.h" |
| 19 | #include "statslog.h" |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 20 | |
| 21 | #include <gtest/gtest.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | |
| 25 | using namespace android; |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef __ANDROID__ |
| 32 | const string kApp1 = "app1.sharing.1"; |
| 33 | const string kApp2 = "app2.sharing.1"; |
| 34 | |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 35 | TEST(UidMapTest, TestIsolatedUID) { |
| 36 | sp<UidMap> m = new UidMap(); |
| 37 | StatsLogProcessor p(m, nullptr); |
| 38 | LogEvent addEvent(android::util::ISOLATED_UID_CHANGED, 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame^] | 39 | addEvent.write(100); // parent UID |
| 40 | addEvent.write(101); // isolated UID |
| 41 | addEvent.write(1); // Indicates creation. |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 42 | addEvent.init(); |
| 43 | |
| 44 | EXPECT_EQ(101, m->getParentUidOrSelf(101)); |
| 45 | |
| 46 | p.OnLogEvent(addEvent); |
| 47 | EXPECT_EQ(100, m->getParentUidOrSelf(101)); |
| 48 | |
| 49 | LogEvent removeEvent(android::util::ISOLATED_UID_CHANGED, 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame^] | 50 | removeEvent.write(100); // parent UID |
| 51 | removeEvent.write(101); // isolated UID |
| 52 | removeEvent.write(0); // Indicates removal. |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame] | 53 | removeEvent.init(); |
| 54 | p.OnLogEvent(removeEvent); |
| 55 | EXPECT_EQ(101, m->getParentUidOrSelf(101)); |
| 56 | } |
| 57 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 58 | TEST(UidMapTest, TestMatching) { |
| 59 | UidMap m; |
| 60 | vector<int32_t> uids; |
| 61 | vector<int32_t> versions; |
| 62 | vector<String16> apps; |
| 63 | |
| 64 | uids.push_back(1000); |
| 65 | uids.push_back(1000); |
| 66 | apps.push_back(String16(kApp1.c_str())); |
| 67 | apps.push_back(String16(kApp2.c_str())); |
| 68 | versions.push_back(4); |
| 69 | versions.push_back(5); |
| 70 | m.updateMap(uids, versions, apps); |
| 71 | EXPECT_TRUE(m.hasApp(1000, kApp1)); |
| 72 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
| 73 | EXPECT_FALSE(m.hasApp(1000, "not.app")); |
| 74 | } |
| 75 | |
| 76 | TEST(UidMapTest, TestAddAndRemove) { |
| 77 | UidMap m; |
| 78 | vector<int32_t> uids; |
| 79 | vector<int32_t> versions; |
| 80 | vector<String16> apps; |
| 81 | |
| 82 | uids.push_back(1000); |
| 83 | uids.push_back(1000); |
| 84 | apps.push_back(String16(kApp1.c_str())); |
| 85 | apps.push_back(String16(kApp2.c_str())); |
| 86 | versions.push_back(4); |
| 87 | versions.push_back(5); |
| 88 | m.updateMap(uids, versions, apps); |
| 89 | |
| 90 | m.updateApp(String16(kApp1.c_str()), 1000, 40); |
| 91 | EXPECT_EQ(40, m.getAppVersion(1000, kApp1)); |
| 92 | |
| 93 | m.removeApp(String16(kApp1.c_str()), 1000); |
| 94 | EXPECT_FALSE(m.hasApp(1000, kApp1)); |
| 95 | EXPECT_TRUE(m.hasApp(1000, kApp2)); |
| 96 | } |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 97 | |
| 98 | TEST(UidMapTest, TestClearingOutput) { |
| 99 | UidMap m; |
| 100 | |
| 101 | ConfigKey config1(1, "config1"); |
| 102 | ConfigKey config2(1, "config2"); |
| 103 | |
| 104 | m.OnConfigUpdated(config1); |
| 105 | |
| 106 | vector<int32_t> uids; |
| 107 | vector<int32_t> versions; |
| 108 | vector<String16> apps; |
| 109 | uids.push_back(1000); |
| 110 | uids.push_back(1000); |
| 111 | apps.push_back(String16(kApp1.c_str())); |
| 112 | apps.push_back(String16(kApp2.c_str())); |
| 113 | versions.push_back(4); |
| 114 | versions.push_back(5); |
| 115 | m.updateMap(1, uids, versions, apps); |
| 116 | |
| 117 | UidMapping results = m.getOutput(2, config1); |
| 118 | EXPECT_EQ(1, results.snapshots_size()); |
| 119 | |
| 120 | // It should be cleared now |
| 121 | results = m.getOutput(3, config1); |
| 122 | EXPECT_EQ(0, results.snapshots_size()); |
| 123 | |
| 124 | // Now add another configuration. |
| 125 | m.OnConfigUpdated(config2); |
| 126 | m.updateApp(5, String16(kApp1.c_str()), 1000, 40); |
| 127 | results = m.getOutput(6, config1); |
| 128 | EXPECT_EQ(0, results.snapshots_size()); |
| 129 | EXPECT_EQ(1, results.changes_size()); |
| 130 | |
| 131 | // Now we still haven't been able to delete anything |
| 132 | m.updateApp(7, String16(kApp2.c_str()), 1001, 41); |
| 133 | results = m.getOutput(8, config1); |
| 134 | EXPECT_EQ(0, results.snapshots_size()); |
| 135 | EXPECT_EQ(2, results.changes_size()); |
| 136 | |
| 137 | results = m.getOutput(9, config2); |
| 138 | EXPECT_EQ(0, results.snapshots_size()); |
| 139 | EXPECT_EQ(2, results.changes_size()); |
| 140 | // At this point both should be cleared. |
| 141 | EXPECT_EQ(0, m.mOutput.snapshots_size()); |
| 142 | EXPECT_EQ(0, m.mOutput.changes_size()); |
| 143 | } |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 144 | #else |
| 145 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 146 | #endif |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 147 | |
| 148 | } // namespace statsd |
| 149 | } // namespace os |
| 150 | } // namespace android |