Files
frameworks_base/cmds/idmap2/tests/RawPrintVisitorTests.cpp
Ryan Mitchell e753ffef54 Idmap format changes for bidirectional lookup
This change modifies the idmap file format to allow for target resources
to map to arbitrary type/value combinations and to allow overlay
resources to be mapped back to target resource ids so references to
overlay resources can appear as references to target resources at
runtime.

The mappings of target resources to overlay resources and vice-versa are
both encoded as sparse arrays. Instead of looking up a resource by
indexing into an array that maps to the overlay resource id, the runtime
will binary search over the sparse array to find the type and value that
overlays the target resource.

Bug: 135943783
Test: idmap2_tests
Change-Id: I5d5344cdb7fe35f4f2e8d6781016299dea5d1e20
2019-10-17 11:37:24 -07:00

99 lines
4.8 KiB
C++

/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio> // fclose
#include <memory>
#include <sstream>
#include <string>
#include "TestHelpers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "idmap2/Idmap.h"
#include "idmap2/RawPrintVisitor.h"
using ::testing::NotNull;
namespace android::idmap2 {
TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
ASSERT_THAT(target_apk, NotNull());
const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
ASSERT_THAT(overlay_apk, NotNull());
const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, PolicyFlags::POLICY_PUBLIC,
/* enforce_overlayable */ true);
ASSERT_TRUE(idmap);
std::stringstream stream;
RawPrintVisitor visitor(stream);
(*idmap)->accept(&visitor);
ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000004: 00000002 version\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000008: 76a20829 target crc\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000000c: e3c188b6 overlay crc\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000210: 7f target package id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000211: 7f overlay package id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000212: 00000004 target entry count\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000216: 00000004 overlay entry count\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000021a: 00000008 string pool index offset\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000021e: 000000b4 string pool byte length\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000222: 7f010000 target id: integer/int1\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000226: 07 type: reference (dynamic)\n"),
std::string::npos);
ASSERT_NE(stream.str().find("00000227: 7f010000 value: integer/int1\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000246: 7f010000 overlay id: integer/int1\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000024a: 7f010000 target id: integer/int1\n"), std::string::npos);
}
TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
fclose(stderr); // silence expected warnings from libandroidfw
std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
std::istringstream raw_stream(raw);
const auto idmap = Idmap::FromBinaryStream(raw_stream);
ASSERT_TRUE(idmap);
std::stringstream stream;
RawPrintVisitor visitor(stream);
(*idmap)->accept(&visitor);
ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000004: 00000002 version\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000008: 00001234 target crc\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000000c: 00005678 overlay crc\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000210: 7f target package id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000211: 7f overlay package id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000212: 00000003 target entry count\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000216: 00000003 overlay entry count\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000021a: 00000000 string pool index offset\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000021e: 00000000 string pool byte length\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000222: 7f020000 target id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000226: 01 type: reference\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000227: 7f020000 value\n"), std::string::npos);
ASSERT_NE(stream.str().find("0000023d: 7f020000 overlay id\n"), std::string::npos);
ASSERT_NE(stream.str().find("00000241: 7f020000 target id\n"), std::string::npos);
}
} // namespace android::idmap2