• 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 #include <androidfw/TypeWrappers.h>
19 #include <androidfw/Util.h>
20 
21 #include <optional>
22 #include <vector>
23 
24 #include <gtest/gtest.h>
25 
26 namespace android {
27 
28 using ResValueVector = std::vector<std::optional<Res_value>>;
29 
30 // create a ResTable_type in memory
createTypeTable(const ResValueVector & in_values,bool compact_entry,bool short_offsets,bool sparse)31 static util::unique_cptr<ResTable_type> createTypeTable(
32     const ResValueVector& in_values, bool compact_entry, bool short_offsets, bool sparse)
33 {
34     ResValueVector sparse_values;
35     if (sparse) {
36       std::ranges::copy_if(in_values, std::back_inserter(sparse_values),
37                            [](auto&& v) { return v.has_value(); });
38     }
39     const ResValueVector& values = sparse ? sparse_values : in_values;
40 
41     ResTable_type t{};
42     t.header.type = RES_TABLE_TYPE_TYPE;
43     t.header.headerSize = sizeof(t);
44     t.header.size = sizeof(t);
45     t.id = 1;
46     t.flags = sparse
47                   ? ResTable_type::FLAG_SPARSE
48                   : short_offsets ? ResTable_type::FLAG_OFFSET16 : 0;
49 
50     t.header.size += values.size() *
51                      (sparse ? sizeof(ResTable_sparseTypeEntry) :
52                          short_offsets ? sizeof(uint16_t) : sizeof(uint32_t));
53     t.entriesStart = t.header.size;
54     t.entryCount = values.size();
55 
56     size_t entry_size = compact_entry ? sizeof(ResTable_entry)
57                                       : sizeof(ResTable_entry) + sizeof(Res_value);
58     for (auto const v : values) {
59         t.header.size += v ? entry_size : 0;
60     }
61 
62     uint8_t* data = (uint8_t *)malloc(t.header.size);
63     uint8_t* p_header = data;
64     uint8_t* p_offsets = data + t.header.headerSize;
65     uint8_t* p_entries = data + t.entriesStart;
66 
67     memcpy(p_header, &t, sizeof(t));
68 
69     size_t i = 0, entry_offset = 0;
70     uint32_t sparse_index = 0;
71 
72     for (auto const& v : in_values) {
73         if (sparse) {
74             if (!v) {
75                 ++i;
76                 continue;
77             }
78             const auto p = reinterpret_cast<ResTable_sparseTypeEntry*>(p_offsets) + sparse_index++;
79             p->idx = i;
80             p->offset = (entry_offset >> 2) & 0xffffu;
81         } else if (short_offsets) {
82             uint16_t *p = reinterpret_cast<uint16_t *>(p_offsets) + i;
83             *p = v ? (entry_offset >> 2) & 0xffffu : 0xffffu;
84         } else {
85             uint32_t *p = reinterpret_cast<uint32_t *>(p_offsets) + i;
86             *p = v ? entry_offset : ResTable_type::NO_ENTRY;
87         }
88 
89         if (v) {
90             ResTable_entry entry{};
91             if (compact_entry) {
92                 entry.compact.key = i;
93                 entry.compact.flags = ResTable_entry::FLAG_COMPACT | (v->dataType << 8);
94                 entry.compact.data = v->data;
95                 memcpy(p_entries, &entry, sizeof(entry)); p_entries += sizeof(entry);
96                 entry_offset += sizeof(entry);
97             } else {
98                 Res_value value{};
99                 entry.full.size = sizeof(entry);
100                 entry.full.key.index = i;
101                 value = *v;
102                 memcpy(p_entries, &entry, sizeof(entry)); p_entries += sizeof(entry);
103                 memcpy(p_entries, &value, sizeof(value)); p_entries += sizeof(value);
104                 entry_offset += sizeof(entry) + sizeof(value);
105             }
106         }
107         i++;
108     }
109     return util::unique_cptr<ResTable_type>{reinterpret_cast<ResTable_type*>(data)};
110 }
111 
TEST(TypeVariantIteratorTest,shouldIterateOverTypeWithoutErrors)112 TEST(TypeVariantIteratorTest, shouldIterateOverTypeWithoutErrors) {
113     ResValueVector values;
114 
115     values.push_back(std::nullopt);
116     values.push_back(Res_value{});
117     values.push_back(std::nullopt);
118     values.push_back(Res_value{});
119     values.push_back(Res_value{ sizeof(Res_value), 0, Res_value::TYPE_STRING, 0x12345678});
120     values.push_back(std::nullopt);
121     values.push_back(std::nullopt);
122     values.push_back(std::nullopt);
123     values.push_back(Res_value{ sizeof(Res_value), 0, Res_value::TYPE_STRING, 0x87654321});
124     values.push_back(std::nullopt);
125 
126     // test for combinations of compact_entry and short_offsets
127     for (size_t i = 0; i < 8; i++) {
128         bool compact_entry = i & 0x1, short_offsets = i & 0x2, sparse = i & 0x4;
129         auto data = createTypeTable(values, compact_entry, short_offsets, sparse);
130         TypeVariant v(data.get());
131 
132         TypeVariant::iterator iter = v.beginEntries();
133         ASSERT_EQ(uint32_t(0), iter.index());
134         ASSERT_TRUE(NULL == *iter);
135         ASSERT_NE(v.endEntries(), iter);
136 
137         ++iter;
138 
139         ASSERT_EQ(uint32_t(1), iter.index());
140         ASSERT_TRUE(NULL != *iter);
141         ASSERT_EQ(uint32_t(1), iter->key());
142         ASSERT_NE(v.endEntries(), iter);
143 
144         iter++;
145 
146         ASSERT_EQ(uint32_t(2), iter.index());
147         ASSERT_TRUE(NULL == *iter);
148         ASSERT_NE(v.endEntries(), iter);
149 
150         ++iter;
151 
152         ASSERT_EQ(uint32_t(3), iter.index());
153         ASSERT_TRUE(NULL != *iter);
154         ASSERT_EQ(uint32_t(3), iter->key());
155         ASSERT_NE(v.endEntries(), iter);
156 
157         iter++;
158 
159         ASSERT_EQ(uint32_t(4), iter.index());
160         ASSERT_TRUE(NULL != *iter);
161         ASSERT_EQ(iter->is_compact(), compact_entry);
162         ASSERT_EQ(uint32_t(4), iter->key());
163         ASSERT_EQ(uint32_t(0x12345678), iter->value().data);
164         ASSERT_EQ(Res_value::TYPE_STRING, iter->value().dataType);
165 
166         ++iter;
167 
168         ASSERT_EQ(uint32_t(5), iter.index());
169         ASSERT_TRUE(NULL == *iter);
170         ASSERT_NE(v.endEntries(), iter);
171 
172         ++iter;
173 
174         ASSERT_EQ(uint32_t(6), iter.index());
175         ASSERT_TRUE(NULL == *iter);
176         ASSERT_NE(v.endEntries(), iter);
177 
178         ++iter;
179 
180         ASSERT_EQ(uint32_t(7), iter.index());
181         ASSERT_TRUE(NULL == *iter);
182         ASSERT_NE(v.endEntries(), iter);
183 
184         iter++;
185 
186         ASSERT_EQ(uint32_t(8), iter.index());
187         ASSERT_TRUE(NULL != *iter);
188         ASSERT_EQ(iter->is_compact(), compact_entry);
189         ASSERT_EQ(uint32_t(8), iter->key());
190         ASSERT_EQ(uint32_t(0x87654321), iter->value().data);
191         ASSERT_EQ(Res_value::TYPE_STRING, iter->value().dataType);
192 
193         ++iter;
194 
195         ASSERT_EQ(uint32_t(9), iter.index());
196         ASSERT_TRUE(NULL == *iter);
197         if (sparse) {
198           // Sparse iterator doesn't know anything beyond the last entry.
199           ASSERT_EQ(v.endEntries(), iter);
200         } else {
201           ASSERT_NE(v.endEntries(), iter);
202         }
203 
204         ++iter;
205 
206         ASSERT_EQ(v.endEntries(), iter);
207     }
208 }
209 
210 } // namespace android
211