blob: b22fdafb09bbbb1ec35ebe11f57bded2f1d18084 [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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070023#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020024#include "gmock/gmock.h"
25#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020026#include "idmap2/Idmap.h"
27#include "idmap2/RawPrintVisitor.h"
28
Mårten Kongstad02751232018-04-27 13:16:32 +020029using ::testing::NotNull;
30
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010031namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020032
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020033#define ASSERT_CONTAINS_REGEX(pattern, str) \
34 do { \
35 ASSERT_TRUE(std::regex_search(str, std::regex(pattern))) \
36 << "pattern '" << pattern << "' not found in\n--------\n" \
37 << str << "--------"; \
38 } while (0)
39
Mårten Kongstad02751232018-04-27 13:16:32 +020040TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020041 fclose(stderr); // silence expected warnings
42
Mårten Kongstad02751232018-04-27 13:16:32 +020043 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
44 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
45 ASSERT_THAT(target_apk, NotNull());
46
47 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
48 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
49 ASSERT_THAT(overlay_apk, NotNull());
50
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070051 const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, PolicyFlags::POLICY_PUBLIC,
52 /* enforce_overlayable */ true);
Mårten Kongstadce424902019-03-01 08:35:37 +010053 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020054
55 std::stringstream stream;
56 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010057 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020058
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020059#define ADDRESS "[0-9a-f]{8}: "
60 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
61 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 version\n", stream.str());
62 ASSERT_CONTAINS_REGEX(ADDRESS "76a20829 target crc\n", stream.str());
63 ASSERT_CONTAINS_REGEX(ADDRESS "c054fb26 overlay crc\n", stream.str());
64 ASSERT_CONTAINS_REGEX(ADDRESS " 7f target package id\n", stream.str());
65 ASSERT_CONTAINS_REGEX(ADDRESS " 7f overlay package id\n", stream.str());
66 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 target entry count\n", stream.str());
67 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
68 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
69 ASSERT_CONTAINS_REGEX(ADDRESS "00000008 string pool index offset\n", stream.str());
70 ASSERT_CONTAINS_REGEX(ADDRESS "000000b4 string pool byte length\n", stream.str());
71 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
72 ASSERT_CONTAINS_REGEX(ADDRESS " 07 type: reference \\(dynamic\\)\n", stream.str());
73 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 value: integer/int1\n", stream.str());
74 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 overlay id: integer/int1\n", stream.str());
75 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
76#undef ADDRESS
Mårten Kongstad02751232018-04-27 13:16:32 +020077}
78
79TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
80 fclose(stderr); // silence expected warnings from libandroidfw
81
82 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
83 std::istringstream raw_stream(raw);
84
Mårten Kongstadce424902019-03-01 08:35:37 +010085 const auto idmap = Idmap::FromBinaryStream(raw_stream);
86 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020087
88 std::stringstream stream;
89 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010090 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020091
92 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020093 ASSERT_NE(stream.str().find("00000004: 00000003 version\n"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +020094 ASSERT_NE(stream.str().find("00000008: 00001234 target crc\n"), std::string::npos);
95 ASSERT_NE(stream.str().find("0000000c: 00005678 overlay crc\n"), std::string::npos);
Mårten Kongstadd7e8a532019-10-11 08:32:04 +020096 ASSERT_NE(stream.str().find("0000021c: 7f target package id\n"), std::string::npos);
97 ASSERT_NE(stream.str().find("0000021d: 7f overlay package id\n"), std::string::npos);
98 ASSERT_NE(stream.str().find("0000021e: 00000003 target entry count\n"), std::string::npos);
99 ASSERT_NE(stream.str().find("00000222: 00000003 overlay entry count\n"), std::string::npos);
100 ASSERT_NE(stream.str().find("00000226: 00000000 string pool index offset\n"), std::string::npos);
101 ASSERT_NE(stream.str().find("0000022a: 00000000 string pool byte length\n"), std::string::npos);
102 ASSERT_NE(stream.str().find("0000022e: 7f020000 target id\n"), std::string::npos);
103 ASSERT_NE(stream.str().find("00000232: 01 type: reference\n"), std::string::npos);
104 ASSERT_NE(stream.str().find("00000233: 7f020000 value\n"), std::string::npos);
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700105
Mårten Kongstadd7e8a532019-10-11 08:32:04 +0200106 ASSERT_NE(stream.str().find("00000249: 7f020000 overlay id\n"), std::string::npos);
107 ASSERT_NE(stream.str().find("0000024d: 7f020000 target id\n"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200108}
109
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100110} // namespace android::idmap2