| 1 | /* |
| 2 | * Copyright (C) 2018 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 <memory> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "R.h" |
| 21 | #include "TestHelpers.h" |
| 22 | #include "androidfw/ApkAssets.h" |
| 23 | #include "gmock/gmock.h" |
| 24 | #include "gtest/gtest.h" |
| 25 | #include "idmap2/ResourceContainer.h" |
| 26 | #include "idmap2/ResourceUtils.h" |
| 27 | #include "idmap2/Result.h" |
| 28 | |
| 29 | using ::testing::NotNull; |
| 30 | |
| 31 | namespace android::idmap2 { |
| 32 | |
| 33 | class ResourceUtilsTests : public Idmap2Tests { |
| 34 | protected: |
| 35 | void SetUp() override { |
| 36 | Idmap2Tests::SetUp(); |
| 37 | |
| 38 | apk_assets_ = ApkAssets::Load(GetTargetApkPath()); |
| 39 | ASSERT_THAT(apk_assets_, NotNull()); |
| 40 | |
| 41 | am_.SetApkAssets({apk_assets_.get()}); |
| 42 | } |
| 43 | |
| 44 | const AssetManager2& GetAssetManager() { |
| 45 | return am_; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | AssetManager2 am_; |
| 50 | std::unique_ptr<const ApkAssets> apk_assets_; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(ResourceUtilsTests, ResToTypeEntryName) { |
| 54 | Result<std::string> name = utils::ResToTypeEntryName(GetAssetManager(), R::target::integer::int1); |
| 55 | ASSERT_TRUE(name) << name.GetErrorMessage(); |
| 56 | ASSERT_EQ(*name, "integer/int1"); |
| 57 | } |
| 58 | |
| 59 | TEST_F(ResourceUtilsTests, ResToTypeEntryNameNoSuchResourceId) { |
| 60 | Result<std::string> name = utils::ResToTypeEntryName(GetAssetManager(), 0x7f123456U); |
| 61 | ASSERT_FALSE(name); |
| 62 | } |
| 63 | |
| 64 | TEST_F(ResourceUtilsTests, InvalidValidOverlayNameInvalidAttributes) { |
| 65 | auto overlay = |
| 66 | OverlayResourceContainer::FromPath(GetTestDataPath() + "/overlay/overlay-invalid.apk"); |
| 67 | ASSERT_TRUE(overlay); |
| 68 | |
| 69 | auto info = (*overlay)->FindOverlayInfo("InvalidName"); |
| 70 | ASSERT_FALSE(info); |
| 71 | } |
| 72 | |
| 73 | TEST_F(ResourceUtilsTests, ValidOverlayNameInvalidAttributes) { |
| 74 | auto overlay = |
| 75 | OverlayResourceContainer::FromPath(GetTestDataPath() + "/overlay/overlay-invalid.apk"); |
| 76 | ASSERT_TRUE(overlay); |
| 77 | |
| 78 | auto info = (*overlay)->FindOverlayInfo("ValidName"); |
| 79 | ASSERT_FALSE(info); |
| 80 | } |
| 81 | |
| 82 | TEST_F(ResourceUtilsTests, ValidOverlayNameAndTargetPackageInvalidAttributes) { |
| 83 | auto overlay = |
| 84 | OverlayResourceContainer::FromPath(GetTestDataPath() + "/overlay/overlay-invalid.apk"); |
| 85 | ASSERT_TRUE(overlay); |
| 86 | |
| 87 | auto info = (*overlay)->FindOverlayInfo("ValidNameAndTargetPackage"); |
| 88 | ASSERT_TRUE(info); |
| 89 | ASSERT_EQ("ValidNameAndTargetPackage", info->name); |
| 90 | ASSERT_EQ("Valid", info->target_package); |
| 91 | ASSERT_EQ("", info->target_name); // Attribute resource id could not be found |
| 92 | ASSERT_EQ(0, info->resource_mapping); // Attribute resource id could not be found |
| 93 | } |
| 94 | |
| 95 | } // namespace android::idmap2 |