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/AttributeFinder.h>
18
19#include <gtest/gtest.h>
20
21using android::BackTrackingAttributeFinder;
22
23class MockAttributeFinder : public BackTrackingAttributeFinder<MockAttributeFinder, int> {
24public:
25    MockAttributeFinder(const uint32_t* attrs, int len)
26        : BackTrackingAttributeFinder(0, len) {
27        mAttrs = new uint32_t[len];
28        memcpy(mAttrs, attrs, sizeof(*attrs) * len);
29    }
30
31    ~MockAttributeFinder() {
32        delete mAttrs;
33    }
34
35    inline uint32_t getAttribute(const int index) const {
36        return mAttrs[index];
37    }
38
39private:
40    uint32_t* mAttrs;
41};
42
43static const uint32_t sortedAttributes[] = {
44        0x01010000, 0x01010001, 0x01010002, 0x01010004,
45        0x02010001, 0x02010010, 0x7f010001
46};
47
48static const uint32_t packageUnsortedAttributes[] = {
49        0x02010001, 0x02010010, 0x01010000, 0x01010001,
50        0x01010002, 0x01010004, 0x7f010001
51};
52
53static const uint32_t singlePackageAttributes[] = {
54        0x7f010007, 0x7f01000a, 0x7f01000d, 0x00000000
55};
56
57TEST(AttributeFinderTest, IteratesSequentially) {
58    const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes);
59    MockAttributeFinder finder(sortedAttributes, end);
60
61    EXPECT_EQ(0, finder.find(0x01010000));
62    EXPECT_EQ(1, finder.find(0x01010001));
63    EXPECT_EQ(2, finder.find(0x01010002));
64    EXPECT_EQ(3, finder.find(0x01010004));
65    EXPECT_EQ(4, finder.find(0x02010001));
66    EXPECT_EQ(5, finder.find(0x02010010));
67    EXPECT_EQ(6, finder.find(0x7f010001));
68    EXPECT_EQ(end, finder.find(0x7f010002));
69}
70
71TEST(AttributeFinderTest, PackagesAreOutOfOrder) {
72    const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes);
73    MockAttributeFinder finder(sortedAttributes, end);
74
75    EXPECT_EQ(6, finder.find(0x7f010001));
76    EXPECT_EQ(end, finder.find(0x7f010002));
77    EXPECT_EQ(4, finder.find(0x02010001));
78    EXPECT_EQ(5, finder.find(0x02010010));
79    EXPECT_EQ(0, finder.find(0x01010000));
80    EXPECT_EQ(1, finder.find(0x01010001));
81    EXPECT_EQ(2, finder.find(0x01010002));
82    EXPECT_EQ(3, finder.find(0x01010004));
83}
84
85TEST(AttributeFinderTest, SomeAttributesAreNotFound) {
86    const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes);
87    MockAttributeFinder finder(sortedAttributes, end);
88
89    EXPECT_EQ(0, finder.find(0x01010000));
90    EXPECT_EQ(1, finder.find(0x01010001));
91    EXPECT_EQ(2, finder.find(0x01010002));
92    EXPECT_EQ(end, finder.find(0x01010003));
93    EXPECT_EQ(3, finder.find(0x01010004));
94    EXPECT_EQ(end, finder.find(0x01010005));
95    EXPECT_EQ(end, finder.find(0x01010006));
96    EXPECT_EQ(4, finder.find(0x02010001));
97    EXPECT_EQ(end, finder.find(0x02010002));
98}
99
100TEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) {
101    const int end = sizeof(packageUnsortedAttributes) / sizeof(*packageUnsortedAttributes);
102    MockAttributeFinder finder(packageUnsortedAttributes, end);
103
104    EXPECT_EQ(2, finder.find(0x01010000));
105    EXPECT_EQ(3, finder.find(0x01010001));
106    EXPECT_EQ(4, finder.find(0x01010002));
107    EXPECT_EQ(end, finder.find(0x01010003));
108    EXPECT_EQ(5, finder.find(0x01010004));
109    EXPECT_EQ(end, finder.find(0x01010005));
110    EXPECT_EQ(end, finder.find(0x01010006));
111    EXPECT_EQ(0, finder.find(0x02010001));
112    EXPECT_EQ(end, finder.find(0x02010002));
113    EXPECT_EQ(1, finder.find(0x02010010));
114    EXPECT_EQ(6, finder.find(0x7f010001));
115}
116
117TEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) {
118    const int end = sizeof(singlePackageAttributes) / sizeof(*singlePackageAttributes);
119    MockAttributeFinder finder(singlePackageAttributes, end);
120
121    EXPECT_EQ(end, finder.find(0x010100f4));
122    EXPECT_EQ(end, finder.find(0x010100f5));
123    EXPECT_EQ(end, finder.find(0x010100f6));
124    EXPECT_EQ(end, finder.find(0x010100f7));
125    EXPECT_EQ(end, finder.find(0x010100f8));
126    EXPECT_EQ(end, finder.find(0x010100fa));
127    EXPECT_EQ(0, finder.find(0x7f010007));
128}
129