blob: 4c12b0397769ee5da1b91c0efc68eec1fdf942f1 [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);
Yao Chen80235402017-11-13 20:42:25 -080039 addEvent.write(100); // parent UID
40 addEvent.write(101); // isolated UID
41 addEvent.write(1); // Indicates creation.
David Chen21582962017-11-01 17:32:46 -070042 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 Chen80235402017-11-13 20:42:25 -080050 removeEvent.write(100); // parent UID
51 removeEvent.write(101); // isolated UID
52 removeEvent.write(0); // Indicates removal.
David Chen21582962017-11-01 17:32:46 -070053 removeEvent.init();
54 p.OnLogEvent(removeEvent);
55 EXPECT_EQ(101, m->getParentUidOrSelf(101));
56}
57
David Chende701692017-10-05 13:16:02 -070058TEST(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
76TEST(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 Chend6896892017-10-25 11:49:03 -070097
98TEST(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 Chende701692017-10-05 13:16:02 -0700144#else
145GTEST_LOG_(INFO) << "This test does nothing.\n";
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700146#endif
David Chend6896892017-10-25 11:49:03 -0700147
148} // namespace statsd
149} // namespace os
150} // namespace android