blob: 781ff7b51637c470d7601622fc9897e4f8d05afc [file] [log] [blame]
Adam Lesinski2ae4a872015-11-02 16:10:55 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "link/ManifestFixer.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "test/Test.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080020
Adam Lesinski87f1e0f2017-06-27 16:21:58 -070021using ::android::StringPiece;
Adam Lesinskic6284372017-12-04 13:46:23 -080022using ::testing::Eq;
23using ::testing::Gt;
24using ::testing::IsNull;
25using ::testing::Ne;
Adam Lesinski87f1e0f2017-06-27 16:21:58 -070026using ::testing::NotNull;
Adam Lesinskic6284372017-12-04 13:46:23 -080027using ::testing::StrEq;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinski2ae4a872015-11-02 16:10:55 -080029namespace aapt {
30
31struct ManifestFixerTest : public ::testing::Test {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::unique_ptr<IAaptContext> mContext;
Adam Lesinski2ae4a872015-11-02 16:10:55 -080033
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 void SetUp() override {
35 mContext =
36 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 .SetCompilationPackage("android")
38 .SetPackageId(0x01)
39 .SetNameManglerPolicy(NameManglerPolicy{"android"})
40 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 "android:attr/package", ResourceId(0x01010000),
44 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 .SetTypeMask(android::ResTable_map::TYPE_STRING)
46 .Build())
47 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 "android:attr/minSdkVersion", ResourceId(0x01010001),
49 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 .SetTypeMask(android::ResTable_map::TYPE_STRING |
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 android::ResTable_map::TYPE_INTEGER)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 .Build())
53 .AddSymbol(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 "android:attr/targetSdkVersion", ResourceId(0x01010002),
55 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 .SetTypeMask(android::ResTable_map::TYPE_STRING |
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 android::ResTable_map::TYPE_INTEGER)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 .Build())
59 .AddSymbol("android:string/str", ResourceId(0x01060000))
60 .Build())
61 .Build();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 std::unique_ptr<xml::XmlResource> Verify(const StringPiece& str) {
65 return VerifyWithOptions(str, {});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 std::unique_ptr<xml::XmlResource> VerifyWithOptions(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 const StringPiece& str, const ManifestFixerOptions& options) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::unique_ptr<xml::XmlResource> doc = test::BuildXmlDom(str);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ManifestFixer fixer(options);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (fixer.Consume(mContext.get(), doc.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return doc;
Adam Lesinski2ae4a872015-11-02 16:10:55 -080074 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return {};
76 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080077};
78
79TEST_F(ManifestFixerTest, EnsureManifestIsRootTag) {
Adam Lesinskic6284372017-12-04 13:46:23 -080080 EXPECT_THAT(Verify("<other-tag />"), IsNull());
81 EXPECT_THAT(Verify("<ns:manifest xmlns:ns=\"com\" />"), IsNull());
82 EXPECT_THAT(Verify("<manifest package=\"android\"></manifest>"), NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -080083}
84
85TEST_F(ManifestFixerTest, EnsureManifestHasPackage) {
Adam Lesinskic6284372017-12-04 13:46:23 -080086 EXPECT_THAT(Verify("<manifest package=\"android\" />"), NotNull());
87 EXPECT_THAT(Verify("<manifest package=\"com.android\" />"), NotNull());
88 EXPECT_THAT(Verify("<manifest package=\"com.android.google\" />"), NotNull());
89 EXPECT_THAT(Verify("<manifest package=\"com.android.google.Class$1\" />"), IsNull());
90 EXPECT_THAT(Verify("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" "
91 "android:package=\"com.android\" />"),
92 IsNull());
93 EXPECT_THAT(Verify("<manifest package=\"@string/str\" />"), IsNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -080094}
95
Adam Lesinski5119e512016-12-05 19:48:20 -080096TEST_F(ManifestFixerTest, AllowMetaData) {
Adam Lesinski86d67df2017-01-31 13:47:27 -080097 auto doc = Verify(R"EOF(
Adam Lesinski5119e512016-12-05 19:48:20 -080098 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
99 package="android">
100 <meta-data />
101 <application>
102 <meta-data />
103 <activity android:name=".Hi"><meta-data /></activity>
104 <activity-alias android:name=".Ho"><meta-data /></activity-alias>
Adam Lesinski86d67df2017-01-31 13:47:27 -0800105 <receiver android:name=".OffTo"><meta-data /></receiver>
106 <provider android:name=".Work"><meta-data /></provider>
107 <service android:name=".We"><meta-data /></service>
Adam Lesinski5119e512016-12-05 19:48:20 -0800108 </application>
Adam Lesinski86d67df2017-01-31 13:47:27 -0800109 <instrumentation android:name=".Go"><meta-data /></instrumentation>
Adam Lesinski5119e512016-12-05 19:48:20 -0800110 </manifest>)EOF");
Adam Lesinskic6284372017-12-04 13:46:23 -0800111 ASSERT_THAT(doc, NotNull());
Adam Lesinski5119e512016-12-05 19:48:20 -0800112}
113
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800114TEST_F(ManifestFixerTest, UseDefaultSdkVersionsIfNonePresent) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000115 ManifestFixerOptions options;
116 options.min_sdk_version_default = std::string("8");
117 options.target_sdk_version_default = std::string("22");
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800118
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800120 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
121 package="android">
122 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="21" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 </manifest>)EOF",
124 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800125 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 xml::Element* el;
128 xml::Attribute* attr;
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800129
Adam Lesinski6b372992017-08-09 10:54:23 -0700130 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800131 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800133 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800135 ASSERT_THAT(attr, NotNull());
136 EXPECT_THAT(attr->value, StrEq("7"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800138 ASSERT_THAT(attr, NotNull());
139 EXPECT_THAT(attr->value, StrEq("21"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800142 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
143 package="android">
144 <uses-sdk android:targetSdkVersion="21" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 </manifest>)EOF",
146 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800147 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800148
Adam Lesinski6b372992017-08-09 10:54:23 -0700149 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800150 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800152 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800154 ASSERT_THAT(attr, NotNull());
155 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800157 ASSERT_THAT(attr, NotNull());
158 EXPECT_THAT(attr->value, StrEq("21"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800159
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800161 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
162 package="android">
163 <uses-sdk />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 </manifest>)EOF",
165 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800166 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800167
Adam Lesinski6b372992017-08-09 10:54:23 -0700168 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800169 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800171 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700172 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800173 ASSERT_THAT(attr, NotNull());
174 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800176 ASSERT_THAT(attr, NotNull());
177 EXPECT_THAT(attr->value, StrEq("22"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800178
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 doc = VerifyWithOptions(R"EOF(
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800180 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 package="android" />)EOF",
182 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800183 ASSERT_THAT(doc, NotNull());
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800184
Adam Lesinski6b372992017-08-09 10:54:23 -0700185 el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800186 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 el = el->FindChild({}, "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800188 ASSERT_THAT(el, NotNull());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 attr = el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800190 ASSERT_THAT(attr, NotNull());
191 EXPECT_THAT(attr->value, StrEq("8"));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 attr = el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion");
Adam Lesinskic6284372017-12-04 13:46:23 -0800193 ASSERT_THAT(attr, NotNull());
194 EXPECT_THAT(attr->value, StrEq("22"));
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800195}
196
Adam Lesinskie343eb12016-10-27 16:31:58 -0700197TEST_F(ManifestFixerTest, UsesSdkMustComeBeforeApplication) {
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000198 ManifestFixerOptions options;
199 options.min_sdk_version_default = std::string("8");
200 options.target_sdk_version_default = std::string("22");
Adam Lesinskie343eb12016-10-27 16:31:58 -0700201 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
202 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
203 package="android">
204 <application android:name=".MainApplication" />
205 </manifest>)EOF",
206 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800207 ASSERT_THAT(doc, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700208
Adam Lesinski6b372992017-08-09 10:54:23 -0700209 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800210 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700211 ASSERT_EQ("manifest", manifest_el->name);
212
213 xml::Element* application_el = manifest_el->FindChild("", "application");
Adam Lesinskic6284372017-12-04 13:46:23 -0800214 ASSERT_THAT(application_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700215
216 xml::Element* uses_sdk_el = manifest_el->FindChild("", "uses-sdk");
Adam Lesinskic6284372017-12-04 13:46:23 -0800217 ASSERT_THAT(uses_sdk_el, NotNull());
Adam Lesinskie343eb12016-10-27 16:31:58 -0700218
219 // Check that the uses_sdk_el comes before application_el in the children
220 // vector.
221 // Since there are no namespaces here, these children are direct descendants
222 // of manifest.
223 auto uses_sdk_iter =
224 std::find_if(manifest_el->children.begin(), manifest_el->children.end(),
225 [&](const std::unique_ptr<xml::Node>& child) {
226 return child.get() == uses_sdk_el;
227 });
228
229 auto application_iter =
230 std::find_if(manifest_el->children.begin(), manifest_el->children.end(),
231 [&](const std::unique_ptr<xml::Node>& child) {
232 return child.get() == application_el;
233 });
234
Adam Lesinskic6284372017-12-04 13:46:23 -0800235 ASSERT_THAT(uses_sdk_iter, Ne(manifest_el->children.end()));
236 ASSERT_THAT(application_iter, Ne(manifest_el->children.end()));
Adam Lesinskie343eb12016-10-27 16:31:58 -0700237
238 // The distance should be positive, meaning uses_sdk_iter comes before
239 // application_iter.
Adam Lesinskic6284372017-12-04 13:46:23 -0800240 EXPECT_THAT(std::distance(uses_sdk_iter, application_iter), Gt(0));
Adam Lesinskie343eb12016-10-27 16:31:58 -0700241}
242
Adam Lesinski52364f72016-01-11 13:10:24 -0800243TEST_F(ManifestFixerTest, RenameManifestPackageAndFullyQualifyClasses) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 options.rename_manifest_package = std::string("com.android");
Adam Lesinski52364f72016-01-11 13:10:24 -0800246
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800248 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
249 package="android">
Adam Lesinski23034b92017-11-29 16:27:44 -0800250 <uses-split android:name="feature_a" />
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700251 <application android:name=".MainApplication" text="hello">
252 <activity android:name=".activity.Start" />
253 <receiver android:name="com.google.android.Receiver" />
Adam Lesinski52364f72016-01-11 13:10:24 -0800254 </application>
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 </manifest>)EOF",
256 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800257 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800258
Adam Lesinski23034b92017-11-29 16:27:44 -0800259 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800260 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800261
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 xml::Attribute* attr = nullptr;
Adam Lesinski52364f72016-01-11 13:10:24 -0800263
Adam Lesinski23034b92017-11-29 16:27:44 -0800264 attr = manifest_el->FindAttribute({}, "package");
Adam Lesinskic6284372017-12-04 13:46:23 -0800265 ASSERT_THAT(attr, NotNull());
266 EXPECT_THAT(attr->value, StrEq("com.android"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800267
Adam Lesinski23034b92017-11-29 16:27:44 -0800268 xml::Element* uses_split_el = manifest_el->FindChild({}, "uses-split");
Adam Lesinskic6284372017-12-04 13:46:23 -0800269 ASSERT_THAT(uses_split_el, NotNull());
Adam Lesinski23034b92017-11-29 16:27:44 -0800270 attr = uses_split_el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800271 ASSERT_THAT(attr, NotNull());
272 // This should NOT have been affected.
273 EXPECT_THAT(attr->value, StrEq("feature_a"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800274
Adam Lesinski23034b92017-11-29 16:27:44 -0800275 xml::Element* application_el = manifest_el->FindChild({}, "application");
Adam Lesinskic6284372017-12-04 13:46:23 -0800276 ASSERT_THAT(application_el, NotNull());
Adam Lesinski23034b92017-11-29 16:27:44 -0800277
278 attr = application_el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800279 ASSERT_THAT(attr, NotNull());
280 EXPECT_THAT(attr->value, StrEq("android.MainApplication"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800281
Adam Lesinski23034b92017-11-29 16:27:44 -0800282 attr = application_el->FindAttribute({}, "text");
Adam Lesinskic6284372017-12-04 13:46:23 -0800283 ASSERT_THAT(attr, NotNull());
284 EXPECT_THAT(attr->value, StrEq("hello"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800285
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 xml::Element* el;
Adam Lesinski23034b92017-11-29 16:27:44 -0800287 el = application_el->FindChild({}, "activity");
Adam Lesinskic6284372017-12-04 13:46:23 -0800288 ASSERT_THAT(el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800289
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 attr = el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800291 ASSERT_THAT(el, NotNull());
292 EXPECT_THAT(attr->value, StrEq("android.activity.Start"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800293
Adam Lesinski23034b92017-11-29 16:27:44 -0800294 el = application_el->FindChild({}, "receiver");
Adam Lesinskic6284372017-12-04 13:46:23 -0800295 ASSERT_THAT(el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800296
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700297 attr = el->FindAttribute(xml::kSchemaAndroid, "name");
Adam Lesinskic6284372017-12-04 13:46:23 -0800298 ASSERT_THAT(el, NotNull());
299 EXPECT_THAT(attr->value, StrEq("com.google.android.Receiver"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800300}
301
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302TEST_F(ManifestFixerTest,
303 RenameManifestInstrumentationPackageAndFullyQualifyTarget) {
304 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 options.rename_instrumentation_target_package = std::string("com.android");
Adam Lesinski52364f72016-01-11 13:10:24 -0800306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800308 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
309 package="android">
Adam Lesinski86d67df2017-01-31 13:47:27 -0800310 <instrumentation android:name=".TestRunner" android:targetPackage="android" />
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 </manifest>)EOF",
312 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800313 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800314
Adam Lesinski6b372992017-08-09 10:54:23 -0700315 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800316 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800317
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 xml::Element* instrumentation_el =
319 manifest_el->FindChild({}, "instrumentation");
Adam Lesinskic6284372017-12-04 13:46:23 -0800320 ASSERT_THAT(instrumentation_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800321
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700322 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 instrumentation_el->FindAttribute(xml::kSchemaAndroid, "targetPackage");
Adam Lesinskic6284372017-12-04 13:46:23 -0800324 ASSERT_THAT(attr, NotNull());
325 EXPECT_THAT(attr->value, StrEq("com.android"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800326}
327
Roshan Piusae12ce42020-04-25 16:08:55 -0700328TEST_F(ManifestFixerTest,
329 RenameManifestOverlayPackageAndFullyQualifyTarget) {
330 ManifestFixerOptions options;
331 options.rename_overlay_target_package = std::string("com.android");
332
333 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
334 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
335 package="android">
336 <overlay android:targetName="Customization" android:targetPackage="android" />
337 </manifest>)EOF",
338 options);
339 ASSERT_THAT(doc, NotNull());
340
341 xml::Element* manifest_el = doc->root.get();
342 ASSERT_THAT(manifest_el, NotNull());
343
344 xml::Element* overlay_el =
345 manifest_el->FindChild({}, "overlay");
346 ASSERT_THAT(overlay_el, NotNull());
347
348 xml::Attribute* attr =
349 overlay_el->FindAttribute(xml::kSchemaAndroid, "targetPackage");
350 ASSERT_THAT(attr, NotNull());
351 EXPECT_THAT(attr->value, StrEq("com.android"));
352}
353
Adam Lesinski52364f72016-01-11 13:10:24 -0800354TEST_F(ManifestFixerTest, UseDefaultVersionNameAndCode) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700355 ManifestFixerOptions options;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700356 options.version_name_default = std::string("Beta");
357 options.version_code_default = std::string("0x10000000");
Ryan Mitchell704090e2018-07-31 14:59:25 -0700358 options.version_code_major_default = std::string("0x20000000");
Adam Lesinski52364f72016-01-11 13:10:24 -0800359
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700360 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Adam Lesinski52364f72016-01-11 13:10:24 -0800361 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700362 package="android" />)EOF",
363 options);
Adam Lesinskic6284372017-12-04 13:46:23 -0800364 ASSERT_THAT(doc, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800365
Adam Lesinski6b372992017-08-09 10:54:23 -0700366 xml::Element* manifest_el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800367 ASSERT_THAT(manifest_el, NotNull());
Adam Lesinski52364f72016-01-11 13:10:24 -0800368
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700369 xml::Attribute* attr =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700370 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
Adam Lesinskic6284372017-12-04 13:46:23 -0800371 ASSERT_THAT(attr, NotNull());
372 EXPECT_THAT(attr->value, StrEq("Beta"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800373
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700374 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
Adam Lesinskic6284372017-12-04 13:46:23 -0800375 ASSERT_THAT(attr, NotNull());
376 EXPECT_THAT(attr->value, StrEq("0x10000000"));
Ryan Mitchell704090e2018-07-31 14:59:25 -0700377
378 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
379 ASSERT_THAT(attr, NotNull());
380 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Adam Lesinski52364f72016-01-11 13:10:24 -0800381}
382
Colin Crossdcd58c42018-05-25 22:46:35 -0700383TEST_F(ManifestFixerTest, DontUseDefaultVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700384 ManifestFixerOptions options;
385 options.version_name_default = std::string("Beta");
386 options.version_code_default = std::string("0x10000000");
387 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700388
Ryan Mitchell704090e2018-07-31 14:59:25 -0700389 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
390 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
391 package="android"
392 android:versionCode="0x00000001"
393 android:versionCodeMajor="0x00000002"
394 android:versionName="Alpha" />)EOF",
395 options);
396 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700397
Ryan Mitchell704090e2018-07-31 14:59:25 -0700398 xml::Element* manifest_el = doc->root.get();
399 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700400
Ryan Mitchell704090e2018-07-31 14:59:25 -0700401 xml::Attribute* attr =
402 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
403 ASSERT_THAT(attr, NotNull());
404 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700405
Ryan Mitchell704090e2018-07-31 14:59:25 -0700406 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
407 ASSERT_THAT(attr, NotNull());
408 EXPECT_THAT(attr->value, StrEq("0x00000001"));
409
410 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
411 ASSERT_THAT(attr, NotNull());
412 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700413}
414
415TEST_F(ManifestFixerTest, ReplaceVersionNameAndCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700416 ManifestFixerOptions options;
417 options.replace_version = true;
418 options.version_name_default = std::string("Beta");
419 options.version_code_default = std::string("0x10000000");
420 options.version_code_major_default = std::string("0x20000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700421
Ryan Mitchell704090e2018-07-31 14:59:25 -0700422 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
423 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
424 package="android"
425 android:versionCode="0x00000001"
426 android:versionCodeMajor="0x00000002"
427 android:versionName="Alpha" />)EOF",
428 options);
429 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700430
Ryan Mitchell704090e2018-07-31 14:59:25 -0700431 xml::Element* manifest_el = doc->root.get();
432 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700433
Ryan Mitchell704090e2018-07-31 14:59:25 -0700434 xml::Attribute* attr =
435 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
436 ASSERT_THAT(attr, NotNull());
437 EXPECT_THAT(attr->value, StrEq("Beta"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700438
Ryan Mitchell704090e2018-07-31 14:59:25 -0700439 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
440 ASSERT_THAT(attr, NotNull());
441 EXPECT_THAT(attr->value, StrEq("0x10000000"));
442
443 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
444 ASSERT_THAT(attr, NotNull());
445 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700446}
447
448TEST_F(ManifestFixerTest, ReplaceVersionName) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700449 ManifestFixerOptions options;
450 options.replace_version = true;
451 options.version_name_default = std::string("Beta");
Colin Crossdcd58c42018-05-25 22:46:35 -0700452
Colin Crossdcd58c42018-05-25 22:46:35 -0700453
Ryan Mitchell704090e2018-07-31 14:59:25 -0700454 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
455 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
456 package="android"
457 android:versionCode="0x00000001"
458 android:versionCodeMajor="0x00000002"
459 android:versionName="Alpha" />)EOF",
460 options);
461 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700462
Ryan Mitchell704090e2018-07-31 14:59:25 -0700463 xml::Element* manifest_el = doc->root.get();
464 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700465
Ryan Mitchell704090e2018-07-31 14:59:25 -0700466 xml::Attribute* attr =
467 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
468 ASSERT_THAT(attr, NotNull());
469 EXPECT_THAT(attr->value, StrEq("Beta"));
470
471 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
472 ASSERT_THAT(attr, NotNull());
473 EXPECT_THAT(attr->value, StrEq("0x00000001"));
474
475 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
476 ASSERT_THAT(attr, NotNull());
477 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700478}
479
480TEST_F(ManifestFixerTest, ReplaceVersionCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700481 ManifestFixerOptions options;
482 options.replace_version = true;
483 options.version_code_default = std::string("0x10000000");
Colin Crossdcd58c42018-05-25 22:46:35 -0700484
Ryan Mitchell704090e2018-07-31 14:59:25 -0700485 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
486 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
487 package="android"
488 android:versionCode="0x00000001"
489 android:versionCodeMajor="0x00000002"
490 android:versionName="Alpha" />)EOF",
491 options);
492 ASSERT_THAT(doc, NotNull());
493
494 xml::Element* manifest_el = doc->root.get();
495 ASSERT_THAT(manifest_el, NotNull());
496
497 xml::Attribute* attr =
498 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
499 ASSERT_THAT(attr, NotNull());
500 EXPECT_THAT(attr->value, StrEq("Alpha"));
501
502 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
503 ASSERT_THAT(attr, NotNull());
504 EXPECT_THAT(attr->value, StrEq("0x10000000"));
505
506 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
507 ASSERT_THAT(attr, NotNull());
508 EXPECT_THAT(attr->value, StrEq("0x00000002"));
509}
510
511TEST_F(ManifestFixerTest, ReplaceVersionCodeMajor) {
512 ManifestFixerOptions options;
513 options.replace_version = true;
514 options.version_code_major_default = std::string("0x20000000");
515
516 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
Colin Crossdcd58c42018-05-25 22:46:35 -0700517 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
Ryan Mitchell704090e2018-07-31 14:59:25 -0700518 package="android"
519 android:versionCode="0x00000001"
520 android:versionCodeMajor="0x00000002"
521 android:versionName="Alpha" />)EOF",
522 options);
523 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700524
Ryan Mitchell704090e2018-07-31 14:59:25 -0700525 xml::Element* manifest_el = doc->root.get();
526 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700527
Ryan Mitchell704090e2018-07-31 14:59:25 -0700528 xml::Attribute* attr =
529 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
530 ASSERT_THAT(attr, NotNull());
531 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700532
Ryan Mitchell704090e2018-07-31 14:59:25 -0700533 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
534 ASSERT_THAT(attr, NotNull());
535 EXPECT_THAT(attr->value, StrEq("0x00000001"));
536
537 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
538 ASSERT_THAT(attr, NotNull());
539 EXPECT_THAT(attr->value, StrEq("0x20000000"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700540}
541
542TEST_F(ManifestFixerTest, DontReplaceVersionNameOrCode) {
Ryan Mitchell704090e2018-07-31 14:59:25 -0700543 ManifestFixerOptions options;
544 options.replace_version = true;
Colin Crossdcd58c42018-05-25 22:46:35 -0700545
Ryan Mitchell704090e2018-07-31 14:59:25 -0700546 std::unique_ptr<xml::XmlResource> doc = VerifyWithOptions(R"EOF(
547 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
548 package="android"
549 android:versionCode="0x00000001"
550 android:versionCodeMajor="0x00000002"
551 android:versionName="Alpha" />)EOF",
552 options);
553 ASSERT_THAT(doc, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700554
Ryan Mitchell704090e2018-07-31 14:59:25 -0700555 xml::Element* manifest_el = doc->root.get();
556 ASSERT_THAT(manifest_el, NotNull());
Colin Crossdcd58c42018-05-25 22:46:35 -0700557
Ryan Mitchell704090e2018-07-31 14:59:25 -0700558 xml::Attribute* attr =
559 manifest_el->FindAttribute(xml::kSchemaAndroid, "versionName");
560 ASSERT_THAT(attr, NotNull());
561 EXPECT_THAT(attr->value, StrEq("Alpha"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700562
Ryan Mitchell704090e2018-07-31 14:59:25 -0700563 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode");
564 ASSERT_THAT(attr, NotNull());
565 EXPECT_THAT(attr->value, StrEq("0x00000001"));
566
567 attr = manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor");
568 ASSERT_THAT(attr, NotNull());
569 EXPECT_THAT(attr->value, StrEq("0x00000002"));
Colin Crossdcd58c42018-05-25 22:46:35 -0700570}
571
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700572TEST_F(ManifestFixerTest, EnsureManifestAttributesAreTyped) {
Adam Lesinskic6284372017-12-04 13:46:23 -0800573 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"hello\" />"), IsNull());
574 EXPECT_THAT(Verify("<manifest package=\"android\" coreApp=\"1dp\" />"), IsNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700575
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700576 std::unique_ptr<xml::XmlResource> doc =
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700577 Verify("<manifest package=\"android\" coreApp=\"true\" />");
Adam Lesinskic6284372017-12-04 13:46:23 -0800578 ASSERT_THAT(doc, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700579
Adam Lesinski6b372992017-08-09 10:54:23 -0700580 xml::Element* el = doc->root.get();
Adam Lesinskic6284372017-12-04 13:46:23 -0800581 ASSERT_THAT(el, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700582
Adam Lesinskic6284372017-12-04 13:46:23 -0800583 EXPECT_THAT(el->name, StrEq("manifest"));
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700584
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700585 xml::Attribute* attr = el->FindAttribute("", "coreApp");
Adam Lesinskic6284372017-12-04 13:46:23 -0800586 ASSERT_THAT(attr, NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700587
Adam Lesinskic6284372017-12-04 13:46:23 -0800588 EXPECT_THAT(attr->compiled_value, NotNull());
589 EXPECT_THAT(ValueCast<BinaryPrimitive>(attr->compiled_value.get()), NotNull());
Adam Lesinski6b17d2c2016-08-10 11:37:06 -0700590}
591
Adam Lesinski86d67df2017-01-31 13:47:27 -0800592TEST_F(ManifestFixerTest, UsesFeatureMustHaveNameOrGlEsVersion) {
593 std::string input = R"EOF(
594 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
595 package="android">
596 <uses-feature android:name="feature" />
597 <uses-feature android:glEsVersion="1" />
598 <feature-group />
599 <feature-group>
600 <uses-feature android:name="feature_in_group" />
601 <uses-feature android:glEsVersion="2" />
602 </feature-group>
603 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800604 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800605
606 input = R"EOF(
607 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
608 package="android">
609 <uses-feature android:name="feature" android:glEsVersion="1" />
610 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800611 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800612
613 input = R"EOF(
614 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
615 package="android">
616 <uses-feature />
617 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800618 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800619
620 input = R"EOF(
621 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
622 package="android">
623 <feature-group>
624 <uses-feature android:name="feature" android:glEsVersion="1" />
625 </feature-group>
626 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800627 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800628
629 input = R"EOF(
630 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
631 package="android">
632 <feature-group>
633 <uses-feature />
634 </feature-group>
635 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800636 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski86d67df2017-01-31 13:47:27 -0800637}
638
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700639TEST_F(ManifestFixerTest, ApplicationInjectDebuggable) {
640 ManifestFixerOptions options;
641 options.debug_mode = true;
642
643 std::string no_d = R"(
644 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
645 package="android">
646 <application>
647 </application>
648 </manifest>)";
649
650 std::string false_d = R"(
651 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
652 package="android">
653 <application android:debuggable="false">
654 </application>
655 </manifest>)";
656
657 std::string true_d = R"(
658 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
659 package="android">
660 <application android:debuggable="true">
661 </application>
662 </manifest>)";
663
664 // Inject the debuggable attribute when the attribute is not present and the
665 // flag is present
666 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(no_d, options);
667 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
668 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
669
670 // Set the debuggable flag to true if the attribute is false and the flag is
671 // present
672 manifest = VerifyWithOptions(false_d, options);
673 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
674 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
675
676 // Keep debuggable flag true if the attribute is true and the flag is present
677 manifest = VerifyWithOptions(true_d, options);
678 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
679 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
680
681 // Do not inject the debuggable attribute when the attribute is not present
682 // and the flag is not present
683 manifest = Verify(no_d);
684 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
685 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
686
687 // Do not set the debuggable flag to true if the attribute is false and the
688 // flag is not present
689 manifest = Verify(false_d);
690 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
691 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), IsNull());
692
693 // Keep debuggable flag true if the attribute is true and the flag is not
694 // present
695 manifest = Verify(true_d);
696 EXPECT_THAT(manifest->root.get()->FindChildWithAttribute(
697 {}, "application", xml::kSchemaAndroid, "debuggable", "true"), NotNull());
698}
699
Chris Craik335b5652019-04-04 12:46:47 -0700700TEST_F(ManifestFixerTest, ApplicationProfileable) {
701 std::string shell = R"(
702 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
703 package="android">
704 <application>
705 <profileable android:shell="true"/>
706 </application>
707 </manifest>)";
708 EXPECT_THAT(Verify(shell), NotNull());
709 std::string noshell = R"(
710 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
711 package="android">
712 <application>
713 <profileable/>
714 </application>
715 </manifest>)";
716 EXPECT_THAT(Verify(noshell), NotNull());
717}
Ryan Mitchelle5b38a62018-03-23 13:35:00 -0700718
Adam Lesinski63699b12017-05-08 18:36:33 -0700719TEST_F(ManifestFixerTest, IgnoreNamespacedElements) {
720 std::string input = R"EOF(
721 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
722 package="android">
723 <special:tag whoo="true" xmlns:special="http://google.com" />
724 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800725 EXPECT_THAT(Verify(input), NotNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700726}
727
728TEST_F(ManifestFixerTest, DoNotIgnoreNonNamespacedElements) {
729 std::string input = R"EOF(
730 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
731 package="android">
732 <tag whoo="true" />
733 </manifest>)EOF";
Adam Lesinskic6284372017-12-04 13:46:23 -0800734 EXPECT_THAT(Verify(input), IsNull());
Adam Lesinski63699b12017-05-08 18:36:33 -0700735}
736
Adam Lesinski87f1e0f2017-06-27 16:21:58 -0700737TEST_F(ManifestFixerTest, SupportKeySets) {
738 std::string input = R"(
739 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
740 package="android">
741 <key-sets>
742 <key-set android:name="old-set">
743 <public-key android:name="old-key" android:value="some+old+key" />
744 </key-set>
745 <key-set android:name="new-set">
746 <public-key android:name="new-key" android:value="some+new+key" />
747 </key-set>
748 <upgrade-key-set android:name="old-set" />
749 <upgrade-key-set android:name="new-set" />
750 </key-sets>
751 </manifest>)";
752 EXPECT_THAT(Verify(input), NotNull());
753}
754
Adam Lesinskic6284372017-12-04 13:46:23 -0800755TEST_F(ManifestFixerTest, InsertCompileSdkVersions) {
Donald Chai6e497352019-05-19 21:07:50 -0700756 std::string input = R"(<manifest package="com.pkg" />)";
Adam Lesinskic6284372017-12-04 13:46:23 -0800757 ManifestFixerOptions options;
758 options.compile_sdk_version = {"28"};
759 options.compile_sdk_version_codename = {"P"};
760
761 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
762 ASSERT_THAT(manifest, NotNull());
763
Donald Chai6e497352019-05-19 21:07:50 -0700764 // There should be a declaration of kSchemaAndroid, even when the input
765 // didn't have one.
766 EXPECT_EQ(manifest->root->namespace_decls.size(), 1);
767 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
768 EXPECT_EQ(manifest->root->namespace_decls[0].uri, xml::kSchemaAndroid);
769
Adam Lesinskic6284372017-12-04 13:46:23 -0800770 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
771 ASSERT_THAT(attr, NotNull());
772 EXPECT_THAT(attr->value, StrEq("28"));
773
774 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
775 ASSERT_THAT(attr, NotNull());
776 EXPECT_THAT(attr->value, StrEq("P"));
Ryan Mitchellaada89c2019-02-12 08:06:26 -0800777
778 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
779 ASSERT_THAT(attr, NotNull());
780 EXPECT_THAT(attr->value, StrEq("28"));
781
782 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
783 ASSERT_THAT(attr, NotNull());
784 EXPECT_THAT(attr->value, StrEq("P"));
785}
786
787TEST_F(ManifestFixerTest, OverrideCompileSdkVersions) {
788 std::string input = R"(
789 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android"
790 compileSdkVersion="27" compileSdkVersionCodename="O"
791 platformBuildVersionCode="27" platformBuildVersionName="O"/>)";
792 ManifestFixerOptions options;
793 options.compile_sdk_version = {"28"};
794 options.compile_sdk_version_codename = {"P"};
795
796 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
797 ASSERT_THAT(manifest, NotNull());
798
799 xml::Attribute* attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersion");
800 ASSERT_THAT(attr, NotNull());
801 EXPECT_THAT(attr->value, StrEq("28"));
802
803 attr = manifest->root->FindAttribute(xml::kSchemaAndroid, "compileSdkVersionCodename");
804 ASSERT_THAT(attr, NotNull());
805 EXPECT_THAT(attr->value, StrEq("P"));
806
807 attr = manifest->root->FindAttribute("", "platformBuildVersionCode");
808 ASSERT_THAT(attr, NotNull());
809 EXPECT_THAT(attr->value, StrEq("28"));
810
811 attr = manifest->root->FindAttribute("", "platformBuildVersionName");
812 ASSERT_THAT(attr, NotNull());
813 EXPECT_THAT(attr->value, StrEq("P"));
Adam Lesinskic6284372017-12-04 13:46:23 -0800814}
815
Donald Chai6e497352019-05-19 21:07:50 -0700816TEST_F(ManifestFixerTest, AndroidPrefixAlreadyUsed) {
817 std::string input =
818 R"(<manifest package="com.pkg"
819 xmlns:android="http://schemas.android.com/apk/prv/res/android"
820 android:private_attr="foo" />)";
821 ManifestFixerOptions options;
822 options.compile_sdk_version = {"28"};
823 options.compile_sdk_version_codename = {"P"};
824
825 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
826 ASSERT_THAT(manifest, NotNull());
827
828 // Make sure that we don't redefine "android".
829 EXPECT_EQ(manifest->root->namespace_decls.size(), 2);
830 EXPECT_EQ(manifest->root->namespace_decls[0].prefix, "android");
831 EXPECT_EQ(manifest->root->namespace_decls[0].uri,
832 "http://schemas.android.com/apk/prv/res/android");
833 EXPECT_EQ(manifest->root->namespace_decls[1].prefix, "android0");
834 EXPECT_EQ(manifest->root->namespace_decls[1].uri, xml::kSchemaAndroid);
835}
836
Izabela Orlowskaad9e1322017-12-19 16:22:42 +0000837TEST_F(ManifestFixerTest, UnexpectedElementsInManifest) {
838 std::string input = R"(
839 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
840 package="android">
841 <beep/>
842 </manifest>)";
843 ManifestFixerOptions options;
844 options.warn_validation = true;
845
846 // Unexpected element should result in a warning if the flag is set to 'true'.
847 std::unique_ptr<xml::XmlResource> manifest = VerifyWithOptions(input, options);
848 ASSERT_THAT(manifest, NotNull());
849
850 // Unexpected element should result in an error if the flag is set to 'false'.
851 options.warn_validation = false;
852 manifest = VerifyWithOptions(input, options);
853 ASSERT_THAT(manifest, IsNull());
854
855 // By default the flag should be set to 'false'.
856 manifest = Verify(input);
857 ASSERT_THAT(manifest, IsNull());
858}
859
Adam Lesinskifca5e422017-12-20 15:03:36 -0800860TEST_F(ManifestFixerTest, UsesLibraryMustHaveNonEmptyName) {
861 std::string input = R"(
862 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
863 package="android">
864 <application>
865 <uses-library android:name="" />
866 </application>
867 </manifest>)";
868 EXPECT_THAT(Verify(input), IsNull());
869
870 input = R"(
871 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
872 package="android">
873 <application>
874 <uses-library />
875 </application>
876 </manifest>)";
877 EXPECT_THAT(Verify(input), IsNull());
878
879 input = R"(
880 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
881 package="android">
882 <application>
883 <uses-library android:name="blahhh" />
884 </application>
885 </manifest>)";
886 EXPECT_THAT(Verify(input), NotNull());
887}
888
Todd Kennedyce3e1292020-10-29 17:14:24 -0700889TEST_F(ManifestFixerTest, ApplicationPropertyAttributeRequired) {
890 std::string input = R"(
891 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
892 package="android">
893 <application>
894 <property android:name="" />
895 </application>
896 </manifest>)";
897 EXPECT_THAT(Verify(input), IsNull());
898}
899
900TEST_F(ManifestFixerTest, ApplicationPropertyOnlyOneAttributeDefined) {
901 std::string input = R"(
902 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
903 package="android">
904 <application>
905 <property android:name="" android:value="" android:resource="" />
906 </application>
907 </manifest>)";
908 EXPECT_THAT(Verify(input), IsNull());
909
910 input = R"(
911 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
912 package="android">
913 <application>
914 <property android:name="" android:resource="" />
915 </application>
916 </manifest>)";
917 EXPECT_THAT(Verify(input), NotNull());
918
919 input = R"(
920 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
921 package="android">
922 <application>
923 <property android:name="" android:value="" />
924 </application>
925 </manifest>)";
926 EXPECT_THAT(Verify(input), NotNull());
927}
928
929TEST_F(ManifestFixerTest, ComponentPropertyOnlyOneAttributeDefined) {
930 std::string input = R"(
931 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
932 package="android">
933 <application>
934 <activity android:name=".MyActivity">
935 <property android:name="" android:value="" android:resource="" />
936 </activity>
937 </application>
938 </manifest>)";
939 EXPECT_THAT(Verify(input), IsNull());
940
941 input = R"(
942 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
943 package="android">
944 <application>
945 <activity android:name=".MyActivity">
946 <property android:name="" android:value="" />
947 </activity>
948 </application>
949 </manifest>)";
950 EXPECT_THAT(Verify(input), NotNull());
951
952 input = R"(
953 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
954 package="android">
955 <application>
956 <activity android:name=".MyActivity">
957 <property android:name="" android:resource="" />
958 </activity>
959 </application>
960 </manifest>)";
961 EXPECT_THAT(Verify(input), NotNull());
962}
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700963} // namespace aapt