1a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski/*
27a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski * Copyright (C) 2016 The Android Open Source Project
3a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski *
4a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
5a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * you may not use this file except in compliance with the License.
6a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * You may obtain a copy of the License at
7a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski *
8a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
9a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski *
10a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * Unless required by applicable law or agreed to in writing, software
11a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
12a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * See the License for the specific language governing permissions and
14a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * limitations under the License.
15a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski */
16a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
174c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski#include "androidfw/AttributeFinder.h"
18a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
194c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski#include "android-base/macros.h"
204c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski#include "gtest/gtest.h"
21a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
224c67a475a334e4f65238d439a3339195e03c03beAdam Lesinskinamespace android {
23a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
244c67a475a334e4f65238d439a3339195e03c03beAdam Lesinskiclass MockAttributeFinder
254c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski    : public BackTrackingAttributeFinder<MockAttributeFinder, int> {
267a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski public:
274c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski  MockAttributeFinder(const uint32_t* attrs, int len)
284c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski      : BackTrackingAttributeFinder(0, len) {
297a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski    attrs_ = new uint32_t[len];
307a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski    memcpy(attrs_, attrs, sizeof(*attrs) * len);
317a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  }
32a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
337a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  ~MockAttributeFinder() { delete attrs_; }
34a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
357a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  inline uint32_t GetAttribute(const int index) const { return attrs_[index]; }
36a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
377a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski private:
387a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  uint32_t* attrs_;
395dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski};
405dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski
414c67a475a334e4f65238d439a3339195e03c03beAdam Lesinskistatic const uint32_t kSortedAttributes[] = {0x01010000, 0x01010001, 0x01010002,
424c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski                                             0x01010004, 0x02010001, 0x02010010,
434c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski                                             0x7f010001};
447a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
457a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinskistatic const uint32_t kPackageUnsortedAttributes[] = {
464c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski    0x02010001, 0x02010010, 0x01010000, 0x01010001,
474c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski    0x01010002, 0x01010004, 0x7f010001};
487a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
494c67a475a334e4f65238d439a3339195e03c03beAdam Lesinskistatic const uint32_t kSinglePackageAttributes[] = {0x7f010007, 0x7f01000a,
504c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski                                                    0x7f01000d, 0x00000000};
517a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
52a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, IteratesSequentially) {
537a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  const int end = arraysize(kSortedAttributes);
547a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  MockAttributeFinder finder(kSortedAttributes, end);
557a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
567a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(0, finder.Find(0x01010000));
577a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(1, finder.Find(0x01010001));
587a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(2, finder.Find(0x01010002));
597a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(3, finder.Find(0x01010004));
607a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(4, finder.Find(0x02010001));
617a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(5, finder.Find(0x02010010));
627a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(6, finder.Find(0x7f010001));
637a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x7f010002));
64a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}
65a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
66a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, PackagesAreOutOfOrder) {
677a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  const int end = arraysize(kSortedAttributes);
687a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  MockAttributeFinder finder(kSortedAttributes, end);
697a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
707a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(6, finder.Find(0x7f010001));
717a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x7f010002));
727a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(4, finder.Find(0x02010001));
737a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(5, finder.Find(0x02010010));
747a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(0, finder.Find(0x01010000));
757a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(1, finder.Find(0x01010001));
767a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(2, finder.Find(0x01010002));
777a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(3, finder.Find(0x01010004));
78a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}
79a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
80a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, SomeAttributesAreNotFound) {
817a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  const int end = arraysize(kSortedAttributes);
827a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  MockAttributeFinder finder(kSortedAttributes, end);
837a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
847a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(0, finder.Find(0x01010000));
857a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(1, finder.Find(0x01010001));
867a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(2, finder.Find(0x01010002));
877a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010003));
887a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(3, finder.Find(0x01010004));
897a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010005));
907a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010006));
917a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(4, finder.Find(0x02010001));
927a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x02010002));
93a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}
94a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski
95a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) {
967a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  const int end = arraysize(kPackageUnsortedAttributes);
977a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  MockAttributeFinder finder(kPackageUnsortedAttributes, end);
987a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
997a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(2, finder.Find(0x01010000));
1007a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(3, finder.Find(0x01010001));
1017a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(4, finder.Find(0x01010002));
1027a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010003));
1037a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(5, finder.Find(0x01010004));
1047a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010005));
1057a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x01010006));
1067a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(0, finder.Find(0x02010001));
1077a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x02010002));
1087a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(1, finder.Find(0x02010010));
1097a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(6, finder.Find(0x7f010001));
110a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}
1115dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski
1125dce5e67dbdcd14882edf3f64fba671c77577ee4Adam LesinskiTEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) {
1137a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  const int end = arraysize(kSinglePackageAttributes);
1147a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  MockAttributeFinder finder(kSinglePackageAttributes, end);
1157a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski
1167a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100f4));
1177a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100f5));
1187a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100f6));
1197a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100f7));
1207a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100f8));
1217a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(end, finder.Find(0x010100fa));
1227a37b74d37ff79e805c9e97d977e07bfec753c5aAdam Lesinski  EXPECT_EQ(0, finder.Find(0x7f010007));
1235dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski}
1244c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski
1254c67a475a334e4f65238d439a3339195e03c03beAdam Lesinski}  // namespace android
126