blob: b268d5add14130ce1771febe0077f9637f44d660 [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 <cstdio> // fclose
18#include <memory>
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020019#include <regex>
Mårten Kongstad02751232018-04-27 13:16:32 +020020#include <sstream>
21#include <string>
22
Winson62ac8b52019-12-04 08:36:48 -080023#include "TestConstants.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070024#include "TestHelpers.h"
Winson62ac8b52019-12-04 08:36:48 -080025#include "android-base/stringprintf.h"
26#include "androidfw/ResourceTypes.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027#include "gmock/gmock.h"
28#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020029#include "idmap2/Idmap.h"
30#include "idmap2/RawPrintVisitor.h"
31
Winson62ac8b52019-12-04 08:36:48 -080032using android::base::StringPrintf;
Mårten Kongstad02751232018-04-27 13:16:32 +020033using ::testing::NotNull;
34
Winson62ac8b52019-12-04 08:36:48 -080035using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
36
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010037namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020038
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020039#define ASSERT_CONTAINS_REGEX(pattern, str) \
40 do { \
41 ASSERT_TRUE(std::regex_search(str, std::regex(pattern))) \
42 << "pattern '" << pattern << "' not found in\n--------\n" \
43 << str << "--------"; \
44 } while (0)
45
Ryan Mitchella7070132020-05-13 14:17:52 -070046#define ADDRESS "[0-9a-f]{8}: "
47
Mårten Kongstad02751232018-04-27 13:16:32 +020048TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020049 fclose(stderr); // silence expected warnings
50
Mårten Kongstad02751232018-04-27 13:16:32 +020051 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
52 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
53 ASSERT_THAT(target_apk, NotNull());
54
55 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
56 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
57 ASSERT_THAT(overlay_apk, NotNull());
58
Winson62ac8b52019-12-04 08:36:48 -080059 const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, PolicyFlags::PUBLIC,
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070060 /* enforce_overlayable */ true);
Mårten Kongstadce424902019-03-01 08:35:37 +010061 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020062
63 std::stringstream stream;
64 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010065 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020066
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020067 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
Ryan Mitchella7070132020-05-13 14:17:52 -070068 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 version\n", stream.str());
Winson62ac8b52019-12-04 08:36:48 -080069 ASSERT_CONTAINS_REGEX(
70 StringPrintf(ADDRESS "%s target crc\n", android::idmap2::TestConstants::TARGET_CRC_STRING),
71 stream.str());
72 ASSERT_CONTAINS_REGEX(
73 StringPrintf(ADDRESS "%s overlay crc\n", android::idmap2::TestConstants::OVERLAY_CRC_STRING),
74 stream.str());
Ryan Mitchella7070132020-05-13 14:17:52 -070075 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 fulfilled policies: public\n", stream.str());
76 ASSERT_CONTAINS_REGEX(ADDRESS " 01 enforce overlayable\n", stream.str());
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020077 ASSERT_CONTAINS_REGEX(ADDRESS " 7f target package id\n", stream.str());
78 ASSERT_CONTAINS_REGEX(ADDRESS " 7f overlay package id\n", stream.str());
79 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 target entry count\n", stream.str());
80 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
81 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
82 ASSERT_CONTAINS_REGEX(ADDRESS "00000008 string pool index offset\n", stream.str());
83 ASSERT_CONTAINS_REGEX(ADDRESS "000000b4 string pool byte length\n", stream.str());
84 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
85 ASSERT_CONTAINS_REGEX(ADDRESS " 07 type: reference \\(dynamic\\)\n", stream.str());
86 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 value: integer/int1\n", stream.str());
87 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 overlay id: integer/int1\n", stream.str());
88 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
Mårten Kongstad02751232018-04-27 13:16:32 +020089}
90
91TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
92 fclose(stderr); // silence expected warnings from libandroidfw
93
94 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
95 std::istringstream raw_stream(raw);
96
Mårten Kongstadce424902019-03-01 08:35:37 +010097 const auto idmap = Idmap::FromBinaryStream(raw_stream);
98 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020099
100 std::stringstream stream;
101 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +0100102 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +0200103
Ryan Mitchella7070132020-05-13 14:17:52 -0700104 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
105 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 version\n", stream.str());
106 ASSERT_CONTAINS_REGEX(ADDRESS "00001234 target crc\n", stream.str());
107 ASSERT_CONTAINS_REGEX(ADDRESS "00005678 overlay crc\n", stream.str());
108 ASSERT_CONTAINS_REGEX(ADDRESS "00000011 fulfilled policies: public|signature\n", stream.str());
109 ASSERT_CONTAINS_REGEX(ADDRESS " 01 enforce overlayable\n", stream.str());
110 ASSERT_CONTAINS_REGEX(ADDRESS " 7f target package id\n", stream.str());
111 ASSERT_CONTAINS_REGEX(ADDRESS " 7f overlay package id\n", stream.str());
112 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 target entry count\n", stream.str());
113 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 overlay entry count\n", stream.str());
114 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 string pool index offset\n", stream.str());
115 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 string pool byte length\n", stream.str());
116 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
117 ASSERT_CONTAINS_REGEX(ADDRESS " 01 type: reference\n", stream.str());
118 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 value\n", stream.str());
119 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 overlay id\n", stream.str());
120 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200121}
122
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100123} // namespace android::idmap2