1a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski/* 2a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski * Copyright (C) 2014 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 17a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski#include <androidfw/AttributeFinder.h> 18a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 19a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski#include <gtest/gtest.h> 20a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 21a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskiusing android::BackTrackingAttributeFinder; 22a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 23a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskiclass MockAttributeFinder : public BackTrackingAttributeFinder<MockAttributeFinder, int> { 24a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskipublic: 25a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski MockAttributeFinder(const uint32_t* attrs, int len) 26a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski : BackTrackingAttributeFinder(0, len) { 27a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski mAttrs = new uint32_t[len]; 28a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski memcpy(mAttrs, attrs, sizeof(*attrs) * len); 29a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski } 30a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 31a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski ~MockAttributeFinder() { 32a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski delete mAttrs; 33a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski } 34a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 35a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski inline uint32_t getAttribute(const int index) const { 36a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski return mAttrs[index]; 37a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski } 38a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 39a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskiprivate: 40a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski uint32_t* mAttrs; 41a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}; 42a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 43a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskistatic const uint32_t sortedAttributes[] = { 44a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 0x01010000, 0x01010001, 0x01010002, 0x01010004, 45a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 0x02010001, 0x02010010, 0x7f010001 46a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}; 47a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 48a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinskistatic const uint32_t packageUnsortedAttributes[] = { 49a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 0x02010001, 0x02010010, 0x01010000, 0x01010001, 50a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 0x01010002, 0x01010004, 0x7f010001 51a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski}; 52a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 535dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinskistatic const uint32_t singlePackageAttributes[] = { 545dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski 0x7f010007, 0x7f01000a, 0x7f01000d, 0x00000000 555dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski}; 565dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski 57a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, IteratesSequentially) { 58a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes); 59a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski MockAttributeFinder finder(sortedAttributes, end); 60a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 61a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(0, finder.find(0x01010000)); 62a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(1, finder.find(0x01010001)); 63a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(2, finder.find(0x01010002)); 64a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(3, finder.find(0x01010004)); 65a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(4, finder.find(0x02010001)); 66a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(5, finder.find(0x02010010)); 67a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(6, finder.find(0x7f010001)); 68a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x7f010002)); 69a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski} 70a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 71a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, PackagesAreOutOfOrder) { 72a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes); 73a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski MockAttributeFinder finder(sortedAttributes, end); 74a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 75a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(6, finder.find(0x7f010001)); 76a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x7f010002)); 77a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(4, finder.find(0x02010001)); 78a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(5, finder.find(0x02010010)); 79a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(0, finder.find(0x01010000)); 80a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(1, finder.find(0x01010001)); 81a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(2, finder.find(0x01010002)); 82a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(3, finder.find(0x01010004)); 83a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski} 84a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 85a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, SomeAttributesAreNotFound) { 86a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski const int end = sizeof(sortedAttributes) / sizeof(*sortedAttributes); 87a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski MockAttributeFinder finder(sortedAttributes, end); 88a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 89a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(0, finder.find(0x01010000)); 90a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(1, finder.find(0x01010001)); 91a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(2, finder.find(0x01010002)); 92a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010003)); 93a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(3, finder.find(0x01010004)); 94a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010005)); 95a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010006)); 96a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(4, finder.find(0x02010001)); 97a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x02010002)); 98a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski} 99a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 100a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam LesinskiTEST(AttributeFinderTest, FindAttributesInPackageUnsortedAttributeList) { 101a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski const int end = sizeof(packageUnsortedAttributes) / sizeof(*packageUnsortedAttributes); 102a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski MockAttributeFinder finder(packageUnsortedAttributes, end); 103a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski 104a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(2, finder.find(0x01010000)); 105a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(3, finder.find(0x01010001)); 106a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(4, finder.find(0x01010002)); 107a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010003)); 108a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(5, finder.find(0x01010004)); 109a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010005)); 110a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x01010006)); 111a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(0, finder.find(0x02010001)); 112a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(end, finder.find(0x02010002)); 113a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(1, finder.find(0x02010010)); 114a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski EXPECT_EQ(6, finder.find(0x7f010001)); 115a7d1d73a477fe512d9ea69ee2883084630ec24c4Adam Lesinski} 1165dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski 1175dce5e67dbdcd14882edf3f64fba671c77577ee4Adam LesinskiTEST(AttributeFinderTest, FindAttributesInSinglePackageAttributeList) { 1185dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski const int end = sizeof(singlePackageAttributes) / sizeof(*singlePackageAttributes); 1195dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski MockAttributeFinder finder(singlePackageAttributes, end); 1205dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski 1215dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100f4)); 1225dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100f5)); 1235dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100f6)); 1245dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100f7)); 1255dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100f8)); 1265dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(end, finder.find(0x010100fa)); 1275dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski EXPECT_EQ(0, finder.find(0x7f010007)); 1285dce5e67dbdcd14882edf3f64fba671c77577ee4Adam Lesinski} 129