blob: 9cdc86ca181ad4c33b65cac47c4d1f3c9231175e [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 <sstream>
19#include <string>
Mårten Kongstadce424902019-03-01 08:35:37 +010020#include <utility>
Mårten Kongstad02751232018-04-27 13:16:32 +020021
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070022#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020023#include "androidfw/ApkAssets.h"
24#include "androidfw/Idmap.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070025#include "gmock/gmock.h"
26#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020027#include "idmap2/BinaryStreamVisitor.h"
28#include "idmap2/Idmap.h"
29
Mårten Kongstad02751232018-04-27 13:16:32 +020030using ::testing::NotNull;
31
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010032namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020033
34TEST(BinaryStreamVisitorTests, CreateBinaryStreamViaBinaryStreamVisitor) {
35 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
36 std::istringstream raw_stream(raw);
37
Mårten Kongstadce424902019-03-01 08:35:37 +010038 auto result1 = Idmap::FromBinaryStream(raw_stream);
39 ASSERT_TRUE(result1);
40 const auto idmap1 = std::move(*result1);
Mårten Kongstad02751232018-04-27 13:16:32 +020041
42 std::stringstream stream;
43 BinaryStreamVisitor visitor(stream);
44 idmap1->accept(&visitor);
45
Mårten Kongstadce424902019-03-01 08:35:37 +010046 auto result2 = Idmap::FromBinaryStream(stream);
47 ASSERT_TRUE(result2);
48 const auto idmap2 = std::move(*result2);
Mårten Kongstad02751232018-04-27 13:16:32 +020049
50 ASSERT_EQ(idmap1->GetHeader()->GetTargetCrc(), idmap2->GetHeader()->GetTargetCrc());
51 ASSERT_EQ(idmap1->GetHeader()->GetTargetPath(), idmap2->GetHeader()->GetTargetPath());
Mårten Kongstadb8779022018-11-29 09:53:17 +010052 ASSERT_EQ(idmap1->GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020053 ASSERT_EQ(idmap1->GetData().size(), idmap2->GetData().size());
54
55 const auto& data1 = idmap1->GetData()[0];
56 const auto& data2 = idmap2->GetData()[0];
57
58 ASSERT_EQ(data1->GetHeader()->GetTargetPackageId(), data2->GetHeader()->GetTargetPackageId());
Mårten Kongstadb8779022018-11-29 09:53:17 +010059 ASSERT_EQ(data1->GetTypeEntries().size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020060 ASSERT_EQ(data1->GetTypeEntries().size(), data2->GetTypeEntries().size());
61 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(0), data2->GetTypeEntries()[0]->GetEntry(0));
62 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(1), data2->GetTypeEntries()[0]->GetEntry(1));
63 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(2), data2->GetTypeEntries()[0]->GetEntry(2));
64 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(0), data2->GetTypeEntries()[1]->GetEntry(0));
65 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(1), data2->GetTypeEntries()[1]->GetEntry(1));
66 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(2), data2->GetTypeEntries()[1]->GetEntry(2));
67}
68
69TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) {
70 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
71 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
72 ASSERT_THAT(target_apk, NotNull());
73
74 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
75 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
76 ASSERT_THAT(overlay_apk, NotNull());
77
Mårten Kongstadce424902019-03-01 08:35:37 +010078 const auto idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080079 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
Mårten Kongstadce424902019-03-01 08:35:37 +010080 PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
81 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020082
83 std::stringstream stream;
84 BinaryStreamVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010085 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020086 const std::string str = stream.str();
87 const StringPiece data(str);
88 std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(data);
89 ASSERT_THAT(loaded_idmap, NotNull());
90 ASSERT_EQ(loaded_idmap->TargetPackageId(), 0x7f);
91
92 const IdmapEntry_header* header = loaded_idmap->GetEntryMapForType(0x01);
93 ASSERT_THAT(header, NotNull());
94
95 EntryId entry;
96 bool success = LoadedIdmap::Lookup(header, 0x0000, &entry);
97 ASSERT_TRUE(success);
98 ASSERT_EQ(entry, 0x0000);
99
100 header = loaded_idmap->GetEntryMapForType(0x02);
101 ASSERT_THAT(header, NotNull());
102
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800103 success = LoadedIdmap::Lookup(header, 0x0000, &entry); // string/a
Mårten Kongstad02751232018-04-27 13:16:32 +0200104 ASSERT_FALSE(success);
105
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800106 success = LoadedIdmap::Lookup(header, 0x0001, &entry); // string/b
107 ASSERT_FALSE(success);
108
109 success = LoadedIdmap::Lookup(header, 0x0002, &entry); // string/c
110 ASSERT_FALSE(success);
111
Ryan Mitchella3628462019-01-14 12:19:40 -0800112 success = LoadedIdmap::Lookup(header, 0x0003, &entry); // string/other
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800113 ASSERT_FALSE(success);
114
Ryan Mitchella3628462019-01-14 12:19:40 -0800115 success = LoadedIdmap::Lookup(header, 0x0004, &entry); // string/not_overlayable
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800116 ASSERT_FALSE(success);
117
Ryan Mitchella3628462019-01-14 12:19:40 -0800118 success = LoadedIdmap::Lookup(header, 0x0005, &entry); // string/policy_product
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800119 ASSERT_FALSE(success);
120
Ryan Mitchella3628462019-01-14 12:19:40 -0800121 success = LoadedIdmap::Lookup(header, 0x0006, &entry); // string/policy_public
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800122 ASSERT_FALSE(success);
123
Ryan Mitchella3628462019-01-14 12:19:40 -0800124 success = LoadedIdmap::Lookup(header, 0x0007, &entry); // string/policy_system
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800125 ASSERT_FALSE(success);
126
Ryan Mitchella3628462019-01-14 12:19:40 -0800127 success = LoadedIdmap::Lookup(header, 0x0008, &entry); // string/policy_system_vendor
128 ASSERT_FALSE(success);
129
Winsonb4100202019-02-06 12:05:32 -0800130 success = LoadedIdmap::Lookup(header, 0x0009, &entry); // string/policy_signature
131 ASSERT_FALSE(success);
132
133 success = LoadedIdmap::Lookup(header, 0x000a, &entry); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200134 ASSERT_TRUE(success);
135 ASSERT_EQ(entry, 0x0000);
136
Winsonb4100202019-02-06 12:05:32 -0800137 success = LoadedIdmap::Lookup(header, 0x000b, &entry); // string/str2
Mårten Kongstad02751232018-04-27 13:16:32 +0200138 ASSERT_FALSE(success);
139
Winsonb4100202019-02-06 12:05:32 -0800140 success = LoadedIdmap::Lookup(header, 0x000c, &entry); // string/str3
Mårten Kongstad02751232018-04-27 13:16:32 +0200141 ASSERT_TRUE(success);
142 ASSERT_EQ(entry, 0x0001);
143
Winsonb4100202019-02-06 12:05:32 -0800144 success = LoadedIdmap::Lookup(header, 0x000d, &entry); // string/str4
Mårten Kongstad02751232018-04-27 13:16:32 +0200145 ASSERT_TRUE(success);
146 ASSERT_EQ(entry, 0x0002);
147
Winsonb4100202019-02-06 12:05:32 -0800148 success = LoadedIdmap::Lookup(header, 0x000e, &entry); // string/x
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800149 ASSERT_FALSE(success);
150
Winsonb4100202019-02-06 12:05:32 -0800151 success = LoadedIdmap::Lookup(header, 0x000f, &entry); // string/y
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800152 ASSERT_FALSE(success);
153
Winsonb4100202019-02-06 12:05:32 -0800154 success = LoadedIdmap::Lookup(header, 0x0010, &entry); // string/z
Mårten Kongstad02751232018-04-27 13:16:32 +0200155 ASSERT_FALSE(success);
156}
157
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100158} // namespace android::idmap2