• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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/ResourceTypes.h"
18 
19 #include "utils/String16.h"
20 #include "utils/String8.h"
21 
22 #include "TestHelpers.h"
23 #include "data/basic/R.h"
24 
25 using ::com::android::basic::R;
26 
27 namespace android {
28 
29 class IdmapTest : public ::testing::Test {
30  protected:
SetUp()31   void SetUp() override {
32     std::string contents;
33     ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", "resources.arsc",
34                                         &contents));
35     ASSERT_EQ(NO_ERROR, target_table_.add(contents.data(), contents.size(), 0, true));
36 
37     ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/overlay/overlay.apk",
38                                         "resources.arsc", &overlay_data_));
39     ResTable overlay_table;
40     ASSERT_EQ(NO_ERROR, overlay_table.add(overlay_data_.data(), overlay_data_.size()));
41 
42     char target_name[256] = "com.android.basic";
43     ASSERT_EQ(NO_ERROR, overlay_table.createIdmap(target_table_, 0, 0, target_name, target_name,
44                                                   &data_, &data_size_));
45   }
46 
TearDown()47   void TearDown() override {
48     ::free(data_);
49   }
50 
51   ResTable target_table_;
52   std::string overlay_data_;
53   void* data_ = nullptr;
54   size_t data_size_ = 0;
55 };
56 
TEST_F(IdmapTest,CanLoadIdmap)57 TEST_F(IdmapTest, CanLoadIdmap) {
58   ASSERT_EQ(NO_ERROR,
59             target_table_.add(overlay_data_.data(), overlay_data_.size(), data_, data_size_));
60 }
61 
TEST_F(IdmapTest,OverlayOverridesResourceValue)62 TEST_F(IdmapTest, OverlayOverridesResourceValue) {
63   Res_value val;
64   ssize_t block = target_table_.getResource(R::string::test2, &val, false);
65   ASSERT_GE(block, 0);
66   ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
67   const ResStringPool* pool = target_table_.getTableStringBlock(block);
68   ASSERT_TRUE(pool != NULL);
69   ASSERT_LT(val.data, pool->size());
70 
71   size_t str_len;
72   const char16_t* target_str16 = pool->stringAt(val.data, &str_len);
73   ASSERT_TRUE(target_str16 != NULL);
74   ASSERT_EQ(String16("test2"), String16(target_str16, str_len));
75 
76   ASSERT_EQ(NO_ERROR,
77             target_table_.add(overlay_data_.data(), overlay_data_.size(), data_, data_size_));
78 
79   ssize_t new_block = target_table_.getResource(R::string::test2, &val, false);
80   ASSERT_GE(new_block, 0);
81   ASSERT_NE(block, new_block);
82   ASSERT_EQ(Res_value::TYPE_STRING, val.dataType);
83   pool = target_table_.getTableStringBlock(new_block);
84   ASSERT_TRUE(pool != NULL);
85   ASSERT_LT(val.data, pool->size());
86 
87   target_str16 = pool->stringAt(val.data, &str_len);
88   ASSERT_TRUE(target_str16 != NULL);
89   ASSERT_EQ(String16("test2-overlay"), String16(target_str16, str_len));
90 }
91 
TEST_F(IdmapTest,OverlaidResourceHasSameName)92 TEST_F(IdmapTest, OverlaidResourceHasSameName) {
93   ASSERT_EQ(NO_ERROR,
94             target_table_.add(overlay_data_.data(), overlay_data_.size(), data_, data_size_));
95 
96   ResTable::resource_name res_name;
97   ASSERT_TRUE(target_table_.getResourceName(R::array::integerArray1, true, &res_name));
98 
99   ASSERT_TRUE(res_name.package != NULL);
100   ASSERT_TRUE(res_name.type != NULL);
101   ASSERT_TRUE(res_name.name8 != NULL);
102 
103   EXPECT_EQ(String16("com.android.basic"), String16(res_name.package, res_name.packageLen));
104   EXPECT_EQ(String16("array"), String16(res_name.type, res_name.typeLen));
105   EXPECT_EQ(String8("integerArray1"), String8(res_name.name8, res_name.nameLen));
106 }
107 
108 constexpr const uint32_t kNonOverlaidResourceId = 0x7fff0000u;
109 
TEST_F(IdmapTest,OverlayDoesNotIncludeNonOverlaidResources)110 TEST_F(IdmapTest, OverlayDoesNotIncludeNonOverlaidResources) {
111   // First check that the resource we're trying to not include when overlaid is present when
112   // the overlay is loaded as a standalone APK.
113   ResTable table;
114   ASSERT_EQ(NO_ERROR, table.add(overlay_data_.data(), overlay_data_.size(), 0, true));
115 
116   Res_value val;
117   ssize_t block = table.getResource(kNonOverlaidResourceId, &val, false /*mayBeBag*/);
118   ASSERT_GE(block, 0);
119 
120   // Now add the overlay and verify that the unoverlaid resource is gone.
121   ASSERT_EQ(NO_ERROR,
122             target_table_.add(overlay_data_.data(), overlay_data_.size(), data_, data_size_));
123   block = target_table_.getResource(kNonOverlaidResourceId, &val, false /*mayBeBag*/);
124   ASSERT_LT(block, 0);
125 }
126 
127 }  // namespace
128