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