• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "ResourceUtils.h"
18 
19 #include "SdkConstants.h"
20 #include "Resource.h"
21 #include "test/Test.h"
22 
23 using ::aapt::test::ValueEq;
24 using ::android::Res_value;
25 using ::android::ResTable_map;
26 using ::testing::Eq;
27 using ::testing::NotNull;
28 using ::testing::Pointee;
29 
30 namespace aapt {
31 
TEST(ResourceUtilsTest,ParseBool)32 TEST(ResourceUtilsTest, ParseBool) {
33   EXPECT_THAT(ResourceUtils::ParseBool("true"), Eq(std::optional<bool>(true)));
34   EXPECT_THAT(ResourceUtils::ParseBool("TRUE"), Eq(std::optional<bool>(true)));
35   EXPECT_THAT(ResourceUtils::ParseBool("True"), Eq(std::optional<bool>(true)));
36 
37   EXPECT_THAT(ResourceUtils::ParseBool("false"), Eq(std::optional<bool>(false)));
38   EXPECT_THAT(ResourceUtils::ParseBool("FALSE"), Eq(std::optional<bool>(false)));
39   EXPECT_THAT(ResourceUtils::ParseBool("False"), Eq(std::optional<bool>(false)));
40 
41   EXPECT_THAT(ResourceUtils::ParseBool(" False\n "), Eq(std::optional<bool>(false)));
42 }
43 
TEST(ResourceUtilsTest,ParseResourceName)44 TEST(ResourceUtilsTest, ParseResourceName) {
45   ResourceNameRef actual;
46   bool actual_priv = false;
47   EXPECT_TRUE(ResourceUtils::ParseResourceName("android:color/foo", &actual, &actual_priv));
48   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
49   EXPECT_FALSE(actual_priv);
50 
51   EXPECT_TRUE(ResourceUtils::ParseResourceName("color/foo", &actual, &actual_priv));
52   EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
53   EXPECT_FALSE(actual_priv);
54 
55   EXPECT_TRUE(ResourceUtils::ParseResourceName("*android:color/foo", &actual, &actual_priv));
56   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
57   EXPECT_TRUE(actual_priv);
58 
59   EXPECT_FALSE(ResourceUtils::ParseResourceName(android::StringPiece(), &actual, &actual_priv));
60 }
61 
TEST(ResourceUtilsTest,ParseReferenceWithNoPackage)62 TEST(ResourceUtilsTest, ParseReferenceWithNoPackage) {
63   ResourceNameRef actual;
64   bool create = false;
65   bool private_ref = false;
66   EXPECT_TRUE(ResourceUtils::ParseReference("@color/foo", &actual, &create, &private_ref));
67   EXPECT_THAT(actual, Eq(ResourceNameRef({}, ResourceType::kColor, "foo")));
68   EXPECT_FALSE(create);
69   EXPECT_FALSE(private_ref);
70 }
71 
TEST(ResourceUtilsTest,ParseReferenceWithPackage)72 TEST(ResourceUtilsTest, ParseReferenceWithPackage) {
73   ResourceNameRef actual;
74   bool create = false;
75   bool private_ref = false;
76   EXPECT_TRUE(ResourceUtils::ParseReference("@android:color/foo", &actual, &create, &private_ref));
77   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
78   EXPECT_FALSE(create);
79   EXPECT_FALSE(private_ref);
80 }
81 
TEST(ResourceUtilsTest,ParseReferenceWithSurroundingWhitespace)82 TEST(ResourceUtilsTest, ParseReferenceWithSurroundingWhitespace) {
83   ResourceNameRef actual;
84   bool create = false;
85   bool private_ref = false;
86   EXPECT_TRUE(ResourceUtils::ParseReference("\t @android:color/foo\n \n\t", &actual, &create, &private_ref));
87   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kColor, "foo")));
88   EXPECT_FALSE(create);
89   EXPECT_FALSE(private_ref);
90 }
91 
TEST(ResourceUtilsTest,ParseAutoCreateIdReference)92 TEST(ResourceUtilsTest, ParseAutoCreateIdReference) {
93   ResourceNameRef actual;
94   bool create = false;
95   bool private_ref = false;
96   EXPECT_TRUE(ResourceUtils::ParseReference("@+android:id/foo", &actual, &create, &private_ref));
97   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
98   EXPECT_TRUE(create);
99   EXPECT_FALSE(private_ref);
100 }
101 
TEST(ResourceUtilsTest,ParsePrivateReference)102 TEST(ResourceUtilsTest, ParsePrivateReference) {
103   ResourceNameRef actual;
104   bool create = false;
105   bool private_ref = false;
106   EXPECT_TRUE(ResourceUtils::ParseReference("@*android:id/foo", &actual, &create, &private_ref));
107   EXPECT_THAT(actual, Eq(ResourceNameRef("android", ResourceType::kId, "foo")));
108   EXPECT_FALSE(create);
109   EXPECT_TRUE(private_ref);
110 }
111 
TEST(ResourceUtilsTest,ParseBinaryDynamicReference)112 TEST(ResourceUtilsTest, ParseBinaryDynamicReference) {
113   android::Res_value value = {};
114   value.data = android::util::HostToDevice32(0x01);
115   value.dataType = android::Res_value::TYPE_DYNAMIC_REFERENCE;
116   std::unique_ptr<Item> item = ResourceUtils::ParseBinaryResValue(ResourceType::kId,
117                                                                   android::ConfigDescription(),
118                                                                   android::ResStringPool(), value,
119                                                                   nullptr);
120 
121   Reference* ref = ValueCast<Reference>(item.get());
122   EXPECT_TRUE(ref->is_dynamic);
123   EXPECT_EQ(ref->id.value().id, 0x01);
124 }
125 
TEST(ResourceUtilsTest,FailToParseAutoCreateNonIdReference)126 TEST(ResourceUtilsTest, FailToParseAutoCreateNonIdReference) {
127   bool create = false;
128   bool private_ref = false;
129   ResourceNameRef actual;
130   EXPECT_FALSE(ResourceUtils::ParseReference("@+android:color/foo", &actual, &create, &private_ref));
131 }
132 
TEST(ResourceUtilsTest,ParseAttributeReferences)133 TEST(ResourceUtilsTest, ParseAttributeReferences) {
134   EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android"));
135   EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:foo"));
136   EXPECT_TRUE(ResourceUtils::IsAttributeReference("?attr/foo"));
137   EXPECT_TRUE(ResourceUtils::IsAttributeReference("?android:attr/foo"));
138 }
139 
TEST(ResourceUtilsTest,FailParseIncompleteReference)140 TEST(ResourceUtilsTest, FailParseIncompleteReference) {
141   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?style/foo"));
142   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:style/foo"));
143   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:"));
144   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?android:attr/"));
145   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/"));
146   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:attr/foo"));
147   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/"));
148   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?:/foo"));
149   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?attr/"));
150   EXPECT_FALSE(ResourceUtils::IsAttributeReference("?/foo"));
151 }
152 
TEST(ResourceUtilsTest,ParseStyleParentReference)153 TEST(ResourceUtilsTest, ParseStyleParentReference) {
154   const ResourceName kAndroidStyleFooName("android", ResourceType::kStyle, "foo");
155   const ResourceName kStyleFooName({}, ResourceType::kStyle, "foo");
156 
157   std::string err_str;
158   std::optional<Reference> ref =
159       ResourceUtils::ParseStyleParentReference("@android:style/foo", &err_str);
160   ASSERT_TRUE(ref);
161   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
162 
163   ref = ResourceUtils::ParseStyleParentReference("@style/foo", &err_str);
164   ASSERT_TRUE(ref);
165   EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
166 
167   ref = ResourceUtils::ParseStyleParentReference("?android:style/foo", &err_str);
168   ASSERT_TRUE(ref);
169   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
170 
171   ref = ResourceUtils::ParseStyleParentReference("?style/foo", &err_str);
172   ASSERT_TRUE(ref);
173   EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
174 
175   ref = ResourceUtils::ParseStyleParentReference("android:style/foo", &err_str);
176   ASSERT_TRUE(ref);
177   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
178 
179   ref = ResourceUtils::ParseStyleParentReference("android:foo", &err_str);
180   ASSERT_TRUE(ref);
181   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
182 
183   ref = ResourceUtils::ParseStyleParentReference("@android:foo", &err_str);
184   ASSERT_TRUE(ref);
185   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
186 
187   ref = ResourceUtils::ParseStyleParentReference("foo", &err_str);
188   ASSERT_TRUE(ref);
189   EXPECT_THAT(ref.value().name, Eq(kStyleFooName));
190 
191   ref = ResourceUtils::ParseStyleParentReference("*android:style/foo", &err_str);
192   ASSERT_TRUE(ref);
193   EXPECT_THAT(ref.value().name, Eq(kAndroidStyleFooName));
194   EXPECT_TRUE(ref.value().private_reference);
195 }
196 
TEST(ResourceUtilsTest,ParseEmptyFlag)197 TEST(ResourceUtilsTest, ParseEmptyFlag) {
198   std::unique_ptr<Attribute> attr = test::AttributeBuilder()
199                                         .SetTypeMask(ResTable_map::TYPE_FLAGS)
200                                         .AddItem("one", 0x01)
201                                         .AddItem("two", 0x02)
202                                         .Build();
203 
204   std::unique_ptr<BinaryPrimitive> result = ResourceUtils::TryParseFlagSymbol(attr.get(), "");
205   ASSERT_THAT(result, NotNull());
206   EXPECT_THAT(result->value.data, Eq(0u));
207 }
208 
TEST(ResourceUtilsTest,NullIsEmptyReference)209 TEST(ResourceUtilsTest, NullIsEmptyReference) {
210   ASSERT_THAT(ResourceUtils::MakeNull(), Pointee(ValueEq(Reference())));
211   ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@null"), Pointee(ValueEq(Reference())));
212 }
213 
TEST(ResourceUtilsTest,EmptyIsBinaryPrimitive)214 TEST(ResourceUtilsTest, EmptyIsBinaryPrimitive) {
215   ASSERT_THAT(ResourceUtils::MakeEmpty(), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
216   ASSERT_THAT(ResourceUtils::TryParseNullOrEmpty("@empty"), Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_NULL, Res_value::DATA_NULL_EMPTY))));
217 }
218 
TEST(ResourceUtilsTest,ItemsWithWhitespaceAreParsedCorrectly)219 TEST(ResourceUtilsTest, ItemsWithWhitespaceAreParsedCorrectly) {
220   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
221   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " 12\n   ",
222                                                       ResTable_map::TYPE_INTEGER),
223               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_DEC, 12u))));
224   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " true\n   ",
225                                                       ResTable_map::TYPE_BOOLEAN),
226               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_INT_BOOLEAN, 0xffffffffu))));
227 
228   const float expected_float = 12.0f;
229   const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
230   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), " 12.0\n   ",
231                                                       ResTable_map::TYPE_FLOAT),
232               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
233 }
234 
TEST(ResourceUtilsTest,FloatAndBigIntegerParsedCorrectly)235 TEST(ResourceUtilsTest, FloatAndBigIntegerParsedCorrectly) {
236   std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
237   const float expected_float = 0.125f;
238   const uint32_t expected_float_flattened = *(uint32_t*)&expected_float;
239   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "0.125",
240                                                       ResTable_map::TYPE_FLOAT),
241               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, expected_float_flattened))));
242 
243   const float special_float = 1.0f;
244   const uint32_t special_float_flattened = *(uint32_t*)&special_float;
245   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1.0",
246                                                       ResTable_map::TYPE_FLOAT),
247               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, special_float_flattened))));
248 
249   EXPECT_EQ(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1099511627776",
250                                                     ResTable_map::TYPE_INTEGER),
251             std::unique_ptr<Item>(nullptr));
252 
253   const float big_float = 1099511627776.0f;
254   const uint32_t big_flattened = *(uint32_t*)&big_float;
255   EXPECT_THAT(ResourceUtils::TryParseItemForAttribute(context->GetDiagnostics(), "1099511627776",
256                                                       ResTable_map::TYPE_FLOAT),
257               Pointee(ValueEq(BinaryPrimitive(Res_value::TYPE_FLOAT, big_flattened))));
258 }
259 
TEST(ResourceUtilsTest,ParseSdkVersionWithCodename)260 TEST(ResourceUtilsTest, ParseSdkVersionWithCodename) {
261   EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q"), Eq(std::optional<int>(10000)));
262   EXPECT_THAT(ResourceUtils::ParseSdkVersion("Q.fingerprint"), Eq(std::optional<int>(10000)));
263 
264   EXPECT_THAT(ResourceUtils::ParseSdkVersion("R"), Eq(std::optional<int>(10000)));
265   EXPECT_THAT(ResourceUtils::ParseSdkVersion("R.fingerprint"), Eq(std::optional<int>(10000)));
266 }
267 
TEST(ResourceUtilsTest,StringBuilderWhitespaceRemoval)268 TEST(ResourceUtilsTest, StringBuilderWhitespaceRemoval) {
269   EXPECT_THAT(ResourceUtils::StringBuilder()
270                   .AppendText("    hey guys ")
271                   .AppendText(" this is so cool ")
272                   .to_string(),
273               Eq(" hey guys this is so cool "));
274   EXPECT_THAT(ResourceUtils::StringBuilder()
275                   .AppendText(" \" wow,  so many \t ")
276                   .AppendText("spaces. \"what? ")
277                   .to_string(),
278               Eq("  wow,  so many \t spaces. what? "));
279   EXPECT_THAT(ResourceUtils::StringBuilder()
280                   .AppendText("  where \t ")
281                   .AppendText(" \nis the pie?")
282                   .to_string(),
283               Eq(" where is the pie?"));
284 }
285 
TEST(ResourceUtilsTest,StringBuilderEscaping)286 TEST(ResourceUtilsTest, StringBuilderEscaping) {
287   EXPECT_THAT(ResourceUtils::StringBuilder()
288                   .AppendText("hey guys\\n ")
289                   .AppendText(" this \\t is so\\\\ cool")
290                   .to_string(),
291               Eq("hey guys\n this \t is so\\ cool"));
292   EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\@\\?\\#\\\\\\'").to_string(),
293               Eq("@?#\\\'"));
294 }
295 
TEST(ResourceUtilsTest,StringBuilderMisplacedQuote)296 TEST(ResourceUtilsTest, StringBuilderMisplacedQuote) {
297   ResourceUtils::StringBuilder builder;
298   EXPECT_FALSE(builder.AppendText("they're coming!"));
299 }
300 
TEST(ResourceUtilsTest,StringBuilderUnicodeCodes)301 TEST(ResourceUtilsTest, StringBuilderUnicodeCodes) {
302   EXPECT_THAT(ResourceUtils::StringBuilder().AppendText("\\u00AF\\u0AF0 woah").to_string(),
303               Eq("\u00AF\u0AF0 woah"));
304   EXPECT_FALSE(ResourceUtils::StringBuilder().AppendText("\\u00 yo"));
305 }
306 
TEST(ResourceUtilsTest,StringBuilderPreserveSpaces)307 TEST(ResourceUtilsTest, StringBuilderPreserveSpaces) {
308   EXPECT_THAT(ResourceUtils::StringBuilder(true /*preserve_spaces*/).AppendText("\"").to_string(),
309               Eq("\""));
310 }
311 
312 }  // namespace aapt
313