• 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 <utils/String8.h>
20 
21 #include <gtest/gtest.h>
22 
23 namespace android {
24 
createTypeData()25 void* createTypeData() {
26     ResTable_type t;
27     memset(&t, 0, sizeof(t));
28     t.header.type = RES_TABLE_TYPE_TYPE;
29     t.header.headerSize = sizeof(t);
30     t.id = 1;
31     t.entryCount = 3;
32 
33     uint32_t offsets[3];
34     t.entriesStart = t.header.headerSize + sizeof(offsets);
35     t.header.size = t.entriesStart;
36 
37     offsets[0] = 0;
38     ResTable_entry e1;
39     memset(&e1, 0, sizeof(e1));
40     e1.size = sizeof(e1);
41     e1.key.index = 0;
42     t.header.size += sizeof(e1);
43 
44     Res_value v1;
45     memset(&v1, 0, sizeof(v1));
46     t.header.size += sizeof(v1);
47 
48     offsets[1] = ResTable_type::NO_ENTRY;
49 
50     offsets[2] = sizeof(e1) + sizeof(v1);
51     ResTable_entry e2;
52     memset(&e2, 0, sizeof(e2));
53     e2.size = sizeof(e2);
54     e2.key.index = 1;
55     t.header.size += sizeof(e2);
56 
57     Res_value v2;
58     memset(&v2, 0, sizeof(v2));
59     t.header.size += sizeof(v2);
60 
61     uint8_t* data = (uint8_t*)malloc(t.header.size);
62     uint8_t* p = data;
63     memcpy(p, &t, sizeof(t));
64     p += sizeof(t);
65     memcpy(p, offsets, sizeof(offsets));
66     p += sizeof(offsets);
67     memcpy(p, &e1, sizeof(e1));
68     p += sizeof(e1);
69     memcpy(p, &v1, sizeof(v1));
70     p += sizeof(v1);
71     memcpy(p, &e2, sizeof(e2));
72     p += sizeof(e2);
73     memcpy(p, &v2, sizeof(v2));
74     p += sizeof(v2);
75     return data;
76 }
77 
TEST(TypeVariantIteratorTest,shouldIterateOverTypeWithoutErrors)78 TEST(TypeVariantIteratorTest, shouldIterateOverTypeWithoutErrors) {
79     ResTable_type* data = (ResTable_type*) createTypeData();
80 
81     TypeVariant v(data);
82 
83     TypeVariant::iterator iter = v.beginEntries();
84     ASSERT_EQ(uint32_t(0), iter.index());
85     ASSERT_TRUE(NULL != *iter);
86     ASSERT_EQ(uint32_t(0), iter->key.index);
87     ASSERT_NE(v.endEntries(), iter);
88 
89     iter++;
90 
91     ASSERT_EQ(uint32_t(1), iter.index());
92     ASSERT_TRUE(NULL == *iter);
93     ASSERT_NE(v.endEntries(), iter);
94 
95     iter++;
96 
97     ASSERT_EQ(uint32_t(2), iter.index());
98     ASSERT_TRUE(NULL != *iter);
99     ASSERT_EQ(uint32_t(1), iter->key.index);
100     ASSERT_NE(v.endEntries(), iter);
101 
102     iter++;
103 
104     ASSERT_EQ(v.endEntries(), iter);
105 
106     free(data);
107 }
108 
109 } // namespace android
110