blob: 1f6bf49f5f0e1bdaafb752a861d6abbc8e939d55 [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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>
Mårten Kongstad02751232018-04-27 13:16:32 +020019
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070020#include "TestHelpers.h"
21#include "androidfw/ApkAssets.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020022#include "gmock/gmock.h"
23#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024#include "idmap2/ResourceUtils.h"
Mårten Kongstad0f763112018-11-19 14:14:37 +010025#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020026
Mårten Kongstad02751232018-04-27 13:16:32 +020027using ::testing::NotNull;
28
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010029namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020030
31class ResourceUtilsTests : public Idmap2Tests {
32 protected:
33 void SetUp() override {
34 Idmap2Tests::SetUp();
35
36 apk_assets_ = ApkAssets::Load(GetTargetApkPath());
37 ASSERT_THAT(apk_assets_, NotNull());
38
39 am_.SetApkAssets({apk_assets_.get()});
40 }
41
42 const AssetManager2& GetAssetManager() {
43 return am_;
44 }
45
46 private:
47 AssetManager2 am_;
48 std::unique_ptr<const ApkAssets> apk_assets_;
49};
50
51TEST_F(ResourceUtilsTests, ResToTypeEntryName) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010052 Result<std::string> name = utils::ResToTypeEntryName(GetAssetManager(), 0x7f010000U);
Mårten Kongstad0f763112018-11-19 14:14:37 +010053 ASSERT_TRUE(name);
54 ASSERT_EQ(*name, "integer/int1");
Mårten Kongstad02751232018-04-27 13:16:32 +020055}
56
57TEST_F(ResourceUtilsTests, ResToTypeEntryNameNoSuchResourceId) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010058 Result<std::string> name = utils::ResToTypeEntryName(GetAssetManager(), 0x7f123456U);
Mårten Kongstad0f763112018-11-19 14:14:37 +010059 ASSERT_FALSE(name);
Mårten Kongstad02751232018-04-27 13:16:32 +020060}
61
Ryan Mitchellfb4d09c2020-12-07 16:06:04 -080062TEST_F(ResourceUtilsTests, InvalidValidOverlayNameInvalidAttributes) {
63 auto info = utils::ExtractOverlayManifestInfo(GetTestDataPath() + "/overlay/overlay-invalid.apk",
64 "InvalidName");
65 ASSERT_FALSE(info);
66}
67
68TEST_F(ResourceUtilsTests, ValidOverlayNameInvalidAttributes) {
69 auto info = utils::ExtractOverlayManifestInfo(GetTestDataPath() + "/overlay/overlay-invalid.apk",
70 "ValidName");
71 ASSERT_FALSE(info);
72}
73
74TEST_F(ResourceUtilsTests, ValidOverlayNameAndTargetPackageInvalidAttributes) {
75 auto info = utils::ExtractOverlayManifestInfo(GetTestDataPath() + "/overlay/overlay-invalid.apk",
76 "ValidNameAndTargetPackage");
77 ASSERT_TRUE(info);
78 ASSERT_EQ("ValidNameAndTargetPackage", info->name);
79 ASSERT_EQ("Valid", info->target_package);
80 ASSERT_EQ("", info->target_name); // Attribute resource id could not be found
81 ASSERT_EQ(0, info->resource_mapping); // Attribute resource id could not be found
82}
83
84}// namespace android::idmap2