1 // Copyright (C) 2022 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 "statsd_test_util.h"
16 
17 #include "gtest_matchers.h"
18 #include "src/stats_log.pb.h"
19 
20 namespace android {
21 namespace os {
22 namespace statsd {
23 
24 #ifdef __ANDROID__
25 
26 namespace {
27 const string appName = "app1";
28 const int32_t uid = 1000;
29 const int64_t version = 1;
30 const string versionString = "v1";
31 const string installer = "com.android.vending";
32 }  // anonymous namespace
33 
TEST(StatsdTestUtil_PackageInfo,TestBuildPackageInfo)34 TEST(StatsdTestUtil_PackageInfo, TestBuildPackageInfo) {
35     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString,
36                                                /* installer */ nullopt, /* certHash */ {},
37                                                /* deleted */ false, /* hashStrings */ false,
38                                                /* installerIndex */ nullopt);
39 
40     EXPECT_THAT(packageInfo.version(), Eq(version));
41     EXPECT_THAT(packageInfo.uid(), Eq(uid));
42     EXPECT_THAT(packageInfo.deleted(), Eq(false));
43 }
44 
45 // Set up parameterized test for testing with different values for pacakage certificate hashes.
46 class StatsdTestUtil_PackageInfo_CertificateHash : public TestWithParam<vector<uint8_t>> {};
47 INSTANTIATE_TEST_SUITE_P(
48         CertificateHashSizes, StatsdTestUtil_PackageInfo_CertificateHash,
49         Values(vector<uint8_t>(), vector<uint8_t>{'a'}, vector<uint8_t>{'a', 'b'}),
__anon0437423a0202(const TestParamInfo<StatsdTestUtil_PackageInfo_CertificateHash::ParamType>& info) 50         [](const TestParamInfo<StatsdTestUtil_PackageInfo_CertificateHash::ParamType>& info) {
51             if (info.param.empty()) {
52                 return string("empty");
53             }
54             return string(info.param.begin(), info.param.end());
55         });
TEST_P(StatsdTestUtil_PackageInfo_CertificateHash,TestBuildPackageInfoCertificateHash)56 TEST_P(StatsdTestUtil_PackageInfo_CertificateHash, TestBuildPackageInfoCertificateHash) {
57     const vector<uint8_t>& certHash = GetParam();
58     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString,
59                                                /* installer */ nullopt, certHash,
60                                                /* deleted */ false, /* hashStrings */ false,
61                                                /* installerIndex */ nullopt);
62 
63     EXPECT_THAT(packageInfo.has_truncated_certificate_hash(), !certHash.empty());
64     string expectedCertHash(certHash.begin(), certHash.end());
65     EXPECT_THAT(packageInfo.truncated_certificate_hash(), Eq(expectedCertHash));
66 }
67 
TEST(StatsdTestUtil_PackageInfo,TestBuildPackageInfoHashStrings)68 TEST(StatsdTestUtil_PackageInfo, TestBuildPackageInfoHashStrings) {
69     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString,
70                                                /* installer */ nullopt, /* certHash */ {},
71                                                /* deleted */ false, /* hashStrings */ true,
72                                                /* installerIndex */ nullopt);
73 
74     EXPECT_TRUE(packageInfo.has_name_hash());
75     EXPECT_THAT(packageInfo.name_hash(), Eq(Hash64(appName)));
76     EXPECT_FALSE(packageInfo.has_name());
77 
78     EXPECT_TRUE(packageInfo.has_version_string_hash());
79     EXPECT_THAT(packageInfo.version_string_hash(), Eq(Hash64(versionString)));
80     EXPECT_FALSE(packageInfo.has_version_string());
81 }
82 
TEST(StatsdTestUtil_PackageInfo,TestBuildPackageInfoNoHashStrings)83 TEST(StatsdTestUtil_PackageInfo, TestBuildPackageInfoNoHashStrings) {
84     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString,
85                                                /* installer */ nullopt, /* certHash */ {},
86                                                /* deleted */ false, /* hashStrings */ false,
87                                                /* installerIndex */ nullopt);
88 
89     EXPECT_TRUE(packageInfo.has_name());
90     EXPECT_THAT(packageInfo.name(), Eq(appName));
91     EXPECT_FALSE(packageInfo.has_name_hash());
92 
93     EXPECT_TRUE(packageInfo.has_version_string());
94     EXPECT_THAT(packageInfo.version_string(), Eq(versionString));
95     EXPECT_FALSE(packageInfo.has_version_string_hash());
96 }
97 
98 // Test with multiple permutations of installerIndex(uint32_t) and hashString(bool) tuples.
99 class StatsdTestUtil_PackageInfo_InstallerIndexAndHashStrings
100     : public TestWithParam<tuple<optional<uint32_t>, bool>> {};
101 INSTANTIATE_TEST_SUITE_P(InstallerIndexAndHashStrings,
102                          StatsdTestUtil_PackageInfo_InstallerIndexAndHashStrings,
103                          Combine(Values(optional(2), nullopt), Bool()));
TEST_P(StatsdTestUtil_PackageInfo_InstallerIndexAndHashStrings,TestBuildPackageInfoNoInstaller)104 TEST_P(StatsdTestUtil_PackageInfo_InstallerIndexAndHashStrings, TestBuildPackageInfoNoInstaller) {
105     const auto& [installerIndex, hashStrings] = GetParam();
106 
107     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString,
108                                                /* installer */ nullopt, /* certHash */ {},
109                                                /* deleted */ false, hashStrings, installerIndex);
110 
111     EXPECT_FALSE(packageInfo.has_installer_index());
112     EXPECT_FALSE(packageInfo.has_installer_hash());
113     EXPECT_FALSE(packageInfo.has_installer());
114 }
115 
116 // Set up parameterized test for testing with different boolean values for hashStrings parameter
117 // in buildPackageInfo()/buildPackageInfos()
118 class StatsdTestUtil_PackageInfo_HashStrings : public TestWithParam<bool> {};
119 INSTANTIATE_TEST_SUITE_P(HashStrings, StatsdTestUtil_PackageInfo_HashStrings, Bool(),
120                          PrintToStringParamName());
TEST_P(StatsdTestUtil_PackageInfo_HashStrings,TestBuildPackageInfoWithInstallerAndInstallerIndex)121 TEST_P(StatsdTestUtil_PackageInfo_HashStrings, TestBuildPackageInfoWithInstallerAndInstallerIndex) {
122     const bool hashStrings = GetParam();
123     PackageInfo packageInfo = buildPackageInfo(appName, uid, version, versionString, installer,
124                                                /* certHash */ {}, /* deleted */ false, hashStrings,
125                                                /* installerIndex */ 1);
126 
127     EXPECT_THAT(packageInfo.installer_index(), Eq(1));
128     EXPECT_FALSE(packageInfo.has_installer_hash());
129     EXPECT_FALSE(packageInfo.has_installer());
130 }
131 
TEST(StatsdTestUtil_PackageInfo,TestBuildPackageInfoWithInstallerNoInstallerIndexHashStrings)132 TEST(StatsdTestUtil_PackageInfo, TestBuildPackageInfoWithInstallerNoInstallerIndexHashStrings) {
133     PackageInfo packageInfo =
134             buildPackageInfo(appName, uid, version, versionString, installer, /* certHash */ {},
135                              /* deleted */ false, /* hashStrings */ true,
136                              /* installerIndex */ nullopt);
137 
138     EXPECT_FALSE(packageInfo.has_installer_index());
139     EXPECT_THAT(packageInfo.installer_hash(), Eq(Hash64(installer)));
140     EXPECT_FALSE(packageInfo.has_installer());
141 }
142 
TEST(StatsdTestUtil_PackageInfo,TestBuildPackageInfoWithInstallerNoInstallerIndexNoHashStrings)143 TEST(StatsdTestUtil_PackageInfo, TestBuildPackageInfoWithInstallerNoInstallerIndexNoHashStrings) {
144     PackageInfo packageInfo =
145             buildPackageInfo(appName, uid, version, versionString, installer, /* certHash */ {},
146                              /* deleted */ false, /* hashStrings */ false,
147                              /* installerIndex */ nullopt);
148 
149     EXPECT_FALSE(packageInfo.has_installer_index());
150     EXPECT_THAT(packageInfo.installer(), Eq(installer));
151     EXPECT_FALSE(packageInfo.has_installer_hash());
152 }
153 
TEST_P(StatsdTestUtil_PackageInfo_HashStrings,TestBuildPackageInfosEmptyOptionalParams)154 TEST_P(StatsdTestUtil_PackageInfo_HashStrings, TestBuildPackageInfosEmptyOptionalParams) {
155     const bool hashStrings = GetParam();
156     vector<PackageInfo> packageInfos = buildPackageInfos(
157             {appName}, {uid}, {version}, {versionString}, /* installers */ {}, /* certHashes */ {},
158             /* deleted */ {false}, /* installerIndices */ {}, hashStrings);
159 
160     PackageInfo packageInfo = buildPackageInfo(
161             appName, uid, version, versionString, /* installer */ nullopt, /* certHash */ {},
162             /* deleted */ false, hashStrings, /* installerIndex */ nullopt);
163 
164     EXPECT_THAT(packageInfos, Pointwise(EqPackageInfo(), {packageInfo}));
165 }
166 
TEST_P(StatsdTestUtil_PackageInfo_HashStrings,TestBuildPackageInfosNonEmptyOptionalParams)167 TEST_P(StatsdTestUtil_PackageInfo_HashStrings, TestBuildPackageInfosNonEmptyOptionalParams) {
168     const bool hashStrings = GetParam();
169     vector<PackageInfo> packageInfos = buildPackageInfos(
170             {appName}, {uid}, {version}, {versionString}, {installer}, /* certHashes */ {{'a'}},
171             /* deleted */ {false}, /* installerIndices */ {3}, hashStrings);
172 
173     PackageInfo packageInfo =
174             buildPackageInfo(appName, uid, version, versionString, installer, /* certHash */ {'a'},
175                              /* deleted */ false, hashStrings, /* installerIndex */ 3);
176 
177     EXPECT_THAT(packageInfos, Pointwise(EqPackageInfo(), {packageInfo}));
178 }
179 
180 #else
181 GTEST_LOG_(INFO) << "This test does nothing.\n";
182 #endif
183 
184 }  // namespace statsd
185 }  // namespace os
186 }  // namespace android
187