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 "androidfw/StringPool.h"
18 
19 #include <string>
20 
21 #include "androidfw/IDiagnostics.h"
22 #include "androidfw/StringPiece.h"
23 #include "androidfw/Util.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 
27 using ::android::StringPiece;
28 using ::android::StringPiece16;
29 using ::testing::Eq;
30 using ::testing::Ne;
31 using ::testing::NotNull;
32 using ::testing::Pointee;
33 
34 namespace android {
35 
TEST(StringPoolTest,InsertOneString)36 TEST(StringPoolTest, InsertOneString) {
37   StringPool pool;
38 
39   StringPool::Ref ref = pool.MakeRef("wut");
40   EXPECT_THAT(*ref, Eq("wut"));
41 }
42 
TEST(StringPoolTest,InsertTwoUniqueStrings)43 TEST(StringPoolTest, InsertTwoUniqueStrings) {
44   StringPool pool;
45 
46   StringPool::Ref ref_a = pool.MakeRef("wut");
47   StringPool::Ref ref_b = pool.MakeRef("hey");
48 
49   EXPECT_THAT(*ref_a, Eq("wut"));
50   EXPECT_THAT(*ref_b, Eq("hey"));
51 }
52 
TEST(StringPoolTest,DoNotInsertNewDuplicateString)53 TEST(StringPoolTest, DoNotInsertNewDuplicateString) {
54   StringPool pool;
55 
56   StringPool::Ref ref_a = pool.MakeRef("wut");
57   StringPool::Ref ref_b = pool.MakeRef("wut");
58 
59   EXPECT_THAT(*ref_a, Eq("wut"));
60   EXPECT_THAT(*ref_b, Eq("wut"));
61   EXPECT_THAT(pool.size(), Eq(1u));
62 }
63 
TEST(StringPoolTest,DoNotDedupeSameStringDifferentPriority)64 TEST(StringPoolTest, DoNotDedupeSameStringDifferentPriority) {
65   StringPool pool;
66 
67   StringPool::Ref ref_a = pool.MakeRef("wut", StringPool::Context(0x81010001));
68   StringPool::Ref ref_b = pool.MakeRef("wut", StringPool::Context(0x81010002));
69 
70   EXPECT_THAT(*ref_a, Eq("wut"));
71   EXPECT_THAT(*ref_b, Eq("wut"));
72   EXPECT_THAT(pool.size(), Eq(2u));
73 }
74 
TEST(StringPoolTest,MaintainInsertionOrderIndex)75 TEST(StringPoolTest, MaintainInsertionOrderIndex) {
76   StringPool pool;
77 
78   StringPool::Ref ref_a = pool.MakeRef("z");
79   StringPool::Ref ref_b = pool.MakeRef("a");
80   StringPool::Ref ref_c = pool.MakeRef("m");
81 
82   EXPECT_THAT(ref_a.index(), Eq(0u));
83   EXPECT_THAT(ref_b.index(), Eq(1u));
84   EXPECT_THAT(ref_c.index(), Eq(2u));
85 }
86 
TEST(StringPoolTest,PruneStringsWithNoReferences)87 TEST(StringPoolTest, PruneStringsWithNoReferences) {
88   StringPool pool;
89 
90   StringPool::Ref ref_a = pool.MakeRef("foo");
91 
92   {
93     StringPool::Ref ref_b = pool.MakeRef("wut");
94     EXPECT_THAT(*ref_b, Eq("wut"));
95     EXPECT_THAT(pool.size(), Eq(2u));
96     pool.Prune();
97     EXPECT_THAT(pool.size(), Eq(2u));
98   }
99   EXPECT_THAT(pool.size(), Eq(2u));
100 
101   {
102     StringPool::Ref ref_c = pool.MakeRef("bar");
103     EXPECT_THAT(pool.size(), Eq(3u));
104 
105     pool.Prune();
106     EXPECT_THAT(pool.size(), Eq(2u));
107   }
108   EXPECT_THAT(pool.size(), Eq(2u));
109 
110   pool.Prune();
111   EXPECT_THAT(pool.size(), Eq(1u));
112 }
113 
TEST(StringPoolTest,SortAndMaintainIndexesInStringReferences)114 TEST(StringPoolTest, SortAndMaintainIndexesInStringReferences) {
115   StringPool pool;
116 
117   StringPool::Ref ref_a = pool.MakeRef("z");
118   StringPool::Ref ref_b = pool.MakeRef("a");
119   StringPool::Ref ref_c = pool.MakeRef("m");
120 
121   EXPECT_THAT(*ref_a, Eq("z"));
122   EXPECT_THAT(ref_a.index(), Eq(0u));
123 
124   EXPECT_THAT(*ref_b, Eq("a"));
125   EXPECT_THAT(ref_b.index(), Eq(1u));
126 
127   EXPECT_THAT(*ref_c, Eq("m"));
128   EXPECT_THAT(ref_c.index(), Eq(2u));
129 
130   pool.Sort();
131 
132   EXPECT_THAT(*ref_a, Eq("z"));
133   EXPECT_THAT(ref_a.index(), Eq(2u));
134 
135   EXPECT_THAT(*ref_b, Eq("a"));
136   EXPECT_THAT(ref_b.index(), Eq(0u));
137 
138   EXPECT_THAT(*ref_c, Eq("m"));
139   EXPECT_THAT(ref_c.index(), Eq(1u));
140 }
141 
TEST(StringPoolTest,SortAndStillDedupe)142 TEST(StringPoolTest, SortAndStillDedupe) {
143   StringPool pool;
144 
145   StringPool::Ref ref_a = pool.MakeRef("z");
146   StringPool::Ref ref_b = pool.MakeRef("a");
147   StringPool::Ref ref_c = pool.MakeRef("m");
148 
149   pool.Sort();
150 
151   StringPool::Ref ref_d = pool.MakeRef("z");
152   StringPool::Ref ref_e = pool.MakeRef("a");
153   StringPool::Ref ref_f = pool.MakeRef("m");
154 
155   EXPECT_THAT(ref_d.index(), Eq(ref_a.index()));
156   EXPECT_THAT(ref_e.index(), Eq(ref_b.index()));
157   EXPECT_THAT(ref_f.index(), Eq(ref_c.index()));
158 }
159 
TEST(StringPoolTest,AddStyles)160 TEST(StringPoolTest, AddStyles) {
161   StringPool pool;
162 
163   StringPool::StyleRef ref = pool.MakeRef(StyleString{{"android"}, {Span{{"b"}, 2, 6}}});
164   EXPECT_THAT(ref.index(), Eq(0u));
165   EXPECT_THAT(ref->value, Eq("android"));
166   ASSERT_THAT(ref->spans.size(), Eq(1u));
167 
168   const StringPool::Span& span = ref->spans.front();
169   EXPECT_THAT(*span.name, Eq("b"));
170   EXPECT_THAT(span.first_char, Eq(2u));
171   EXPECT_THAT(span.last_char, Eq(6u));
172 }
173 
TEST(StringPoolTest,DoNotDedupeStyleWithSameStringAsNonStyle)174 TEST(StringPoolTest, DoNotDedupeStyleWithSameStringAsNonStyle) {
175   StringPool pool;
176 
177   StringPool::Ref ref = pool.MakeRef("android");
178 
179   StyleString str{{"android"}, {}};
180   StringPool::StyleRef style_ref = pool.MakeRef(StyleString{{"android"}, {}});
181 
182   EXPECT_THAT(ref.index(), Ne(style_ref.index()));
183 }
184 
TEST(StringPoolTest,StylesAndStringsAreSeparateAfterSorting)185 TEST(StringPoolTest, StylesAndStringsAreSeparateAfterSorting) {
186   StringPool pool;
187 
188   StringPool::StyleRef ref_a = pool.MakeRef(StyleString{{"beta"}, {}});
189   StringPool::Ref ref_b = pool.MakeRef("alpha");
190   StringPool::StyleRef ref_c = pool.MakeRef(StyleString{{"alpha"}, {}});
191 
192   EXPECT_THAT(ref_b.index(), Ne(ref_c.index()));
193 
194   pool.Sort();
195 
196   EXPECT_THAT(ref_c.index(), Eq(0u));
197   EXPECT_THAT(ref_a.index(), Eq(1u));
198   EXPECT_THAT(ref_b.index(), Eq(2u));
199 }
200 
TEST(StringPoolTest,FlattenEmptyStringPoolUtf8)201 TEST(StringPoolTest, FlattenEmptyStringPoolUtf8) {
202   using namespace android;  // For NO_ERROR on Windows.
203   NoOpDiagnostics diag;
204 
205   StringPool pool;
206   BigBuffer buffer(1024);
207   StringPool::FlattenUtf8(&buffer, pool, &diag);
208 
209   std::unique_ptr<uint8_t[]> data = android::util::Copy(buffer);
210   ResStringPool test;
211   ASSERT_THAT(test.setTo(data.get(), buffer.size()), Eq(NO_ERROR));
212 }
213 
TEST(StringPoolTest,FlattenOddCharactersUtf16)214 TEST(StringPoolTest, FlattenOddCharactersUtf16) {
215   using namespace android;  // For NO_ERROR on Windows.
216   NoOpDiagnostics diag;
217 
218   StringPool pool;
219   pool.MakeRef("\u093f");
220   BigBuffer buffer(1024);
221   StringPool::FlattenUtf16(&buffer, pool, &diag);
222 
223   std::unique_ptr<uint8_t[]> data = android::util::Copy(buffer);
224   ResStringPool test;
225   ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
226   auto str = test.stringAt(0);
227   ASSERT_TRUE(str.has_value());
228   EXPECT_THAT(str->size(), Eq(1u));
229   EXPECT_THAT(str->data(), Pointee(Eq(u'\u093f')));
230   EXPECT_THAT(str->data()[1], Eq(0u));
231 }
232 
233 constexpr const char* sLongString =
234     "バッテリーを長持ちさせるため、バッテリーセーバーは端末のパフォーマンスを抑"
235     "え、バイブレーション、位置情報サービス、大半のバックグラウンドデータを制限"
236     "します。メール、SMSや、同期を使 "
237     "用するその他のアプリは、起動しても更新されないことがあります。バッテリーセ"
238     "ーバーは端末の充電中は自動的にOFFになります。";
239 
TEST(StringPoolTest,Flatten)240 TEST(StringPoolTest, Flatten) {
241   using namespace android;  // For NO_ERROR on Windows.
242   NoOpDiagnostics diag;
243 
244   StringPool pool;
245 
246   StringPool::Ref ref_a = pool.MakeRef("hello");
247   StringPool::Ref ref_b = pool.MakeRef("goodbye");
248   StringPool::Ref ref_c = pool.MakeRef(sLongString);
249   StringPool::Ref ref_d = pool.MakeRef("");
250   StringPool::StyleRef ref_e =
251       pool.MakeRef(StyleString{{"style"}, {Span{{"b"}, 0, 1}, Span{{"i"}, 2, 3}}});
252 
253   // Styles are always first.
254   EXPECT_THAT(ref_e.index(), Eq(0u));
255 
256   EXPECT_THAT(ref_a.index(), Eq(1u));
257   EXPECT_THAT(ref_b.index(), Eq(2u));
258   EXPECT_THAT(ref_c.index(), Eq(3u));
259   EXPECT_THAT(ref_d.index(), Eq(4u));
260 
261   BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)};
262   StringPool::FlattenUtf8(&buffers[0], pool, &diag);
263   StringPool::FlattenUtf16(&buffers[1], pool, &diag);
264 
265   // Test both UTF-8 and UTF-16 buffers.
266   for (const BigBuffer& buffer : buffers) {
267     std::unique_ptr<uint8_t[]> data = android::util::Copy(buffer);
268 
269     ResStringPool test;
270     ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
271 
272     EXPECT_THAT(android::util::GetString(test, 1), Eq("hello"));
273     EXPECT_THAT(android::util::GetString16(test, 1), Eq(u"hello"));
274 
275     EXPECT_THAT(android::util::GetString(test, 2), Eq("goodbye"));
276     EXPECT_THAT(android::util::GetString16(test, 2), Eq(u"goodbye"));
277 
278     EXPECT_THAT(android::util::GetString(test, 3), Eq(sLongString));
279     EXPECT_THAT(android::util::GetString16(test, 3), Eq(util::Utf8ToUtf16(sLongString)));
280 
281     EXPECT_TRUE(test.stringAt(4).has_value() || test.string8At(4).has_value());
282 
283     EXPECT_THAT(android::util::GetString(test, 0), Eq("style"));
284     EXPECT_THAT(android::util::GetString16(test, 0), Eq(u"style"));
285 
286     auto span_result = test.styleAt(0);
287     ASSERT_TRUE(span_result.has_value());
288 
289     const ResStringPool_span* span = span_result->unsafe_ptr();
290     EXPECT_THAT(android::util::GetString(test, span->name.index), Eq("b"));
291     EXPECT_THAT(android::util::GetString16(test, span->name.index), Eq(u"b"));
292     EXPECT_THAT(span->firstChar, Eq(0u));
293     EXPECT_THAT(span->lastChar, Eq(1u));
294     span++;
295 
296     ASSERT_THAT(span->name.index, Ne(ResStringPool_span::END));
297     EXPECT_THAT(android::util::GetString(test, span->name.index), Eq("i"));
298     EXPECT_THAT(android::util::GetString16(test, span->name.index), Eq(u"i"));
299     EXPECT_THAT(span->firstChar, Eq(2u));
300     EXPECT_THAT(span->lastChar, Eq(3u));
301     span++;
302 
303     EXPECT_THAT(span->name.index, Eq(ResStringPool_span::END));
304   }
305 }
306 
TEST(StringPoolTest,ModifiedUTF8)307 TEST(StringPoolTest, ModifiedUTF8) {
308   using namespace android;  // For NO_ERROR on Windows.
309   NoOpDiagnostics diag;
310   StringPool pool;
311   StringPool::Ref ref_a = pool.MakeRef("\xF0\x90\x90\x80");          // �� (U+10400)
312   StringPool::Ref ref_b = pool.MakeRef("foo \xF0\x90\x90\xB7 bar");  // �� (U+10437)
313   StringPool::Ref ref_c = pool.MakeRef("\xF0\x90\x90\x80\xF0\x90\x90\xB7");
314 
315   BigBuffer buffer(1024);
316   StringPool::FlattenUtf8(&buffer, pool, &diag);
317   std::unique_ptr<uint8_t[]> data = android::util::Copy(buffer);
318 
319   // Check that the codepoints are encoded using two three-byte surrogate pairs
320   ResStringPool test;
321   ASSERT_EQ(test.setTo(data.get(), buffer.size()), NO_ERROR);
322   auto str = test.string8At(0);
323   ASSERT_TRUE(str.has_value());
324   EXPECT_THAT(*str, Eq("\xED\xA0\x81\xED\xB0\x80"));
325 
326   str = test.string8At(1);
327   ASSERT_TRUE(str.has_value());
328   EXPECT_THAT(*str, Eq("foo \xED\xA0\x81\xED\xB0\xB7 bar"));
329 
330   str = test.string8At(2);
331   ASSERT_TRUE(str.has_value());
332   EXPECT_THAT(*str, Eq("\xED\xA0\x81\xED\xB0\x80\xED\xA0\x81\xED\xB0\xB7"));
333 
334   // Check that retrieving the strings returns the original UTF-8 character bytes
335   EXPECT_THAT(android::util::GetString(test, 0), Eq("\xF0\x90\x90\x80"));
336   EXPECT_THAT(android::util::GetString(test, 1), Eq("foo \xF0\x90\x90\xB7 bar"));
337   EXPECT_THAT(android::util::GetString(test, 2), Eq("\xF0\x90\x90\x80\xF0\x90\x90\xB7"));
338 }
339 
TEST(StringPoolTest,MaxEncodingLength)340 TEST(StringPoolTest, MaxEncodingLength) {
341   NoOpDiagnostics diag;
342   using namespace android;  // For NO_ERROR on Windows.
343   ResStringPool test;
344 
345   StringPool pool;
346   pool.MakeRef("aaaaaaaaaa");
347   BigBuffer buffers[2] = {BigBuffer(1024), BigBuffer(1024)};
348 
349   // Make sure a UTF-8 string under the maximum length does not produce an error
350   EXPECT_THAT(StringPool::FlattenUtf8(&buffers[0], pool, &diag), Eq(true));
351   std::unique_ptr<uint8_t[]> data = android::util::Copy(buffers[0]);
352   test.setTo(data.get(), buffers[0].size());
353   EXPECT_THAT(android::util::GetString(test, 0), Eq("aaaaaaaaaa"));
354 
355   // Make sure a UTF-16 string under the maximum length does not produce an error
356   EXPECT_THAT(StringPool::FlattenUtf16(&buffers[1], pool, &diag), Eq(true));
357   data = android::util::Copy(buffers[1]);
358   test.setTo(data.get(), buffers[1].size());
359   EXPECT_THAT(android::util::GetString16(test, 0), Eq(u"aaaaaaaaaa"));
360 
361   StringPool pool2;
362   std::string longStr(50000, 'a');
363   pool2.MakeRef("this fits1");
364   pool2.MakeRef(longStr);
365   pool2.MakeRef("this fits2");
366   BigBuffer buffers2[2] = {BigBuffer(1024), BigBuffer(1024)};
367 
368   // Make sure a string that exceeds the maximum length of UTF-8 produces an
369   // error and writes a shorter error string instead
370   EXPECT_THAT(StringPool::FlattenUtf8(&buffers2[0], pool2, &diag), Eq(false));
371   data = android::util::Copy(buffers2[0]);
372   test.setTo(data.get(), buffers2[0].size());
373   EXPECT_THAT(android::util::GetString(test, 0), "this fits1");
374   EXPECT_THAT(android::util::GetString(test, 1), "STRING_TOO_LARGE");
375   EXPECT_THAT(android::util::GetString(test, 2), "this fits2");
376 
377   // Make sure a string that a string that exceeds the maximum length of UTF-8
378   // but not UTF-16 does not error for UTF-16
379   StringPool pool3;
380   std::u16string longStr16(50000, 'a');
381   pool3.MakeRef(longStr);
382   EXPECT_THAT(StringPool::FlattenUtf16(&buffers2[1], pool3, &diag), Eq(true));
383   data = android::util::Copy(buffers2[1]);
384   test.setTo(data.get(), buffers2[1].size());
385   EXPECT_THAT(android::util::GetString16(test, 0), Eq(longStr16));
386 }
387 
388 }  // namespace android
389