1 /*
2  * Copyright (C) 2018 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 #include "utils/String8.h"
19 
20 #include "gtest/gtest.h"
21 namespace android {
22 
TEST(DynamicRefTableTest,LookupSharedLibSelfReferences)23 TEST(DynamicRefTableTest, LookupSharedLibSelfReferences) {
24   // Shared library
25   DynamicRefTable shared_table(0x02, /* appAsLib */ false);
26   shared_table.addMapping(0x00, 0x02);
27   Res_value value;
28   value.dataType = Res_value::TYPE_REFERENCE;
29   value.data = 0x00010000;
30   ASSERT_EQ(shared_table.lookupResourceValue(&value), NO_ERROR);
31   EXPECT_EQ(value.data, 0x02010000);
32 
33   // App loaded as a shared library
34   DynamicRefTable shared_app_table(0x02, /* appAsLib */ true);
35   shared_app_table.addMapping(0x7f, 0x02);
36   Res_value value2;
37   value2.dataType = Res_value::TYPE_REFERENCE;
38   value2.data = 0x7f010000;
39   ASSERT_EQ(shared_app_table.lookupResourceValue(&value2), NO_ERROR);
40   EXPECT_EQ(value2.data, 0x02010000);
41 };
42 
TEST(DynamicRefTableTest,LookupSharedLibSelfAttributes)43 TEST(DynamicRefTableTest, LookupSharedLibSelfAttributes) {
44   // Shared library
45   DynamicRefTable shared_table(0x03, /* appAsLib */ false);
46   shared_table.addMapping(0x00, 0x03);
47   Res_value value;
48   value.dataType = Res_value::TYPE_ATTRIBUTE;
49   value.data = 0x00010000;
50   ASSERT_EQ(shared_table.lookupResourceValue(&value), NO_ERROR);
51   EXPECT_EQ(value.data, 0x03010000);
52 
53   // App loaded as a shared library
54   DynamicRefTable shared_app_table(0x04, /* appAsLib */ true);
55   shared_app_table.addMapping(0x7f, 0x04);
56   Res_value value2;
57   value2.dataType = Res_value::TYPE_ATTRIBUTE;
58   value2.data = 0x7f010000;
59   ASSERT_EQ(shared_app_table.lookupResourceValue(&value2), NO_ERROR);
60   EXPECT_EQ(value2.data, 0x04010000);
61 };
62 
TEST(DynamicRefTableTest,LookupDynamicReferences)63 TEST(DynamicRefTableTest, LookupDynamicReferences) {
64   // Shared library
65   DynamicRefTable shared_table(0x2, /* appAsLib */ false);
66   shared_table.addMapping(0x00, 0x02);
67   shared_table.addMapping(0x03, 0x05);
68   Res_value value;
69   value.dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
70   value.data = 0x03010000;
71   ASSERT_EQ(shared_table.lookupResourceValue(&value), NO_ERROR);
72   EXPECT_EQ(value.data, 0x05010000);
73 
74   // Regular application
75   DynamicRefTable app_table(0x7f, /* appAsLib */ false);
76   app_table.addMapping(0x03, 0x05);
77   Res_value value3;
78   value3.dataType = Res_value::TYPE_DYNAMIC_REFERENCE;
79   value3.data = 0x03010000;
80   ASSERT_EQ(app_table.lookupResourceValue(&value3), NO_ERROR);
81   EXPECT_EQ(value3.data, 0x05010000);
82 };
83 
TEST(DynamicRefTableTest,LookupDynamicAttributes)84 TEST(DynamicRefTableTest, LookupDynamicAttributes) {
85 // App loaded as a shared library
86   DynamicRefTable shared_app_table(0x2, /* appAsLib */ true);
87   shared_app_table.addMapping(0x03, 0x05);
88   shared_app_table.addMapping(0x7f, 0x2);
89   Res_value value2;
90   value2.dataType = Res_value::TYPE_DYNAMIC_ATTRIBUTE;
91   value2.data = 0x03010000;
92   ASSERT_EQ(shared_app_table.lookupResourceValue(&value2), NO_ERROR);
93   EXPECT_EQ(value2.data, 0x05010000);
94 }
95 
TEST(DynamicRefTableTest,DoNotLookupNonDynamicReferences)96 TEST(DynamicRefTableTest, DoNotLookupNonDynamicReferences) {
97   // Regular application
98   DynamicRefTable app_table(0x7f, /* appAsLib */ false);
99   Res_value value;
100   value.dataType = Res_value::TYPE_REFERENCE;
101   value.data = 0x03010000;
102   ASSERT_EQ(app_table.lookupResourceValue(&value), NO_ERROR);
103   EXPECT_EQ(value.data, 0x03010000);
104 };
105 
TEST(DynamicRefTableTest,DoNotLookupNonDynamicAttributes)106 TEST(DynamicRefTableTest, DoNotLookupNonDynamicAttributes) {
107   // App with custom package id
108   DynamicRefTable custom_app_table(0x8f, /* appAsLib */ false);
109   Res_value value2;
110   value2.dataType = Res_value::TYPE_ATTRIBUTE;
111   value2.data = 0x03010000;
112   ASSERT_EQ(custom_app_table.lookupResourceValue(&value2), NO_ERROR);
113   EXPECT_EQ(value2.data, 0x03010000);
114 };
115 
116 } // namespace android