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