blob: 0e0e25f4f0f3619ab9058c9fa52e52cadbcadeab [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>
20
21#include "gmock/gmock.h"
22#include "gtest/gtest.h"
23
24#include "androidfw/ApkAssets.h"
25#include "androidfw/Idmap.h"
26
27#include "idmap2/BinaryStreamVisitor.h"
28#include "idmap2/Idmap.h"
29
30#include "TestHelpers.h"
31
Mårten Kongstad02751232018-04-27 13:16:32 +020032using ::testing::NotNull;
33
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010034namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020035
36TEST(BinaryStreamVisitorTests, CreateBinaryStreamViaBinaryStreamVisitor) {
37 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
38 std::istringstream raw_stream(raw);
39
40 std::stringstream error;
41 std::unique_ptr<const Idmap> idmap1 = Idmap::FromBinaryStream(raw_stream, error);
42 ASSERT_THAT(idmap1, NotNull());
43
44 std::stringstream stream;
45 BinaryStreamVisitor visitor(stream);
46 idmap1->accept(&visitor);
47
48 std::unique_ptr<const Idmap> idmap2 = Idmap::FromBinaryStream(stream, error);
49 ASSERT_THAT(idmap2, NotNull());
50
51 ASSERT_EQ(idmap1->GetHeader()->GetTargetCrc(), idmap2->GetHeader()->GetTargetCrc());
52 ASSERT_EQ(idmap1->GetHeader()->GetTargetPath(), idmap2->GetHeader()->GetTargetPath());
Mårten Kongstadb8779022018-11-29 09:53:17 +010053 ASSERT_EQ(idmap1->GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020054 ASSERT_EQ(idmap1->GetData().size(), idmap2->GetData().size());
55
56 const auto& data1 = idmap1->GetData()[0];
57 const auto& data2 = idmap2->GetData()[0];
58
59 ASSERT_EQ(data1->GetHeader()->GetTargetPackageId(), data2->GetHeader()->GetTargetPackageId());
Mårten Kongstadb8779022018-11-29 09:53:17 +010060 ASSERT_EQ(data1->GetTypeEntries().size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020061 ASSERT_EQ(data1->GetTypeEntries().size(), data2->GetTypeEntries().size());
62 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(0), data2->GetTypeEntries()[0]->GetEntry(0));
63 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(1), data2->GetTypeEntries()[0]->GetEntry(1));
64 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(2), data2->GetTypeEntries()[0]->GetEntry(2));
65 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(0), data2->GetTypeEntries()[1]->GetEntry(0));
66 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(1), data2->GetTypeEntries()[1]->GetEntry(1));
67 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(2), data2->GetTypeEntries()[1]->GetEntry(2));
68}
69
70TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) {
71 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
72 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
73 ASSERT_THAT(target_apk, NotNull());
74
75 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
76 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
77 ASSERT_THAT(overlay_apk, NotNull());
78
79 std::stringstream error;
80 std::unique_ptr<const Idmap> idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080081 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
82 PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true, error);
Mårten Kongstad02751232018-04-27 13:16:32 +020083 ASSERT_THAT(idmap, NotNull());
84
85 std::stringstream stream;
86 BinaryStreamVisitor visitor(stream);
87 idmap->accept(&visitor);
88 const std::string str = stream.str();
89 const StringPiece data(str);
90 std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(data);
91 ASSERT_THAT(loaded_idmap, NotNull());
92 ASSERT_EQ(loaded_idmap->TargetPackageId(), 0x7f);
93
94 const IdmapEntry_header* header = loaded_idmap->GetEntryMapForType(0x01);
95 ASSERT_THAT(header, NotNull());
96
97 EntryId entry;
98 bool success = LoadedIdmap::Lookup(header, 0x0000, &entry);
99 ASSERT_TRUE(success);
100 ASSERT_EQ(entry, 0x0000);
101
102 header = loaded_idmap->GetEntryMapForType(0x02);
103 ASSERT_THAT(header, NotNull());
104
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800105 success = LoadedIdmap::Lookup(header, 0x0000, &entry); // string/a
Mårten Kongstad02751232018-04-27 13:16:32 +0200106 ASSERT_FALSE(success);
107
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800108 success = LoadedIdmap::Lookup(header, 0x0001, &entry); // string/b
109 ASSERT_FALSE(success);
110
111 success = LoadedIdmap::Lookup(header, 0x0002, &entry); // string/c
112 ASSERT_FALSE(success);
113
Ryan Mitchella3628462019-01-14 12:19:40 -0800114 success = LoadedIdmap::Lookup(header, 0x0003, &entry); // string/other
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800115 ASSERT_FALSE(success);
116
Ryan Mitchella3628462019-01-14 12:19:40 -0800117 success = LoadedIdmap::Lookup(header, 0x0004, &entry); // string/not_overlayable
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800118 ASSERT_FALSE(success);
119
Ryan Mitchella3628462019-01-14 12:19:40 -0800120 success = LoadedIdmap::Lookup(header, 0x0005, &entry); // string/policy_product
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800121 ASSERT_FALSE(success);
122
Ryan Mitchella3628462019-01-14 12:19:40 -0800123 success = LoadedIdmap::Lookup(header, 0x0006, &entry); // string/policy_public
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800124 ASSERT_FALSE(success);
125
Ryan Mitchella3628462019-01-14 12:19:40 -0800126 success = LoadedIdmap::Lookup(header, 0x0007, &entry); // string/policy_system
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800127 ASSERT_FALSE(success);
128
Ryan Mitchella3628462019-01-14 12:19:40 -0800129 success = LoadedIdmap::Lookup(header, 0x0008, &entry); // string/policy_system_vendor
130 ASSERT_FALSE(success);
131
132 success = LoadedIdmap::Lookup(header, 0x0009, &entry); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200133 ASSERT_TRUE(success);
134 ASSERT_EQ(entry, 0x0000);
135
Ryan Mitchella3628462019-01-14 12:19:40 -0800136 success = LoadedIdmap::Lookup(header, 0x000a, &entry); // string/str2
Mårten Kongstad02751232018-04-27 13:16:32 +0200137 ASSERT_FALSE(success);
138
Ryan Mitchella3628462019-01-14 12:19:40 -0800139 success = LoadedIdmap::Lookup(header, 0x000b, &entry); // string/str3
Mårten Kongstad02751232018-04-27 13:16:32 +0200140 ASSERT_TRUE(success);
141 ASSERT_EQ(entry, 0x0001);
142
Ryan Mitchella3628462019-01-14 12:19:40 -0800143 success = LoadedIdmap::Lookup(header, 0x000c, &entry); // string/str4
Mårten Kongstad02751232018-04-27 13:16:32 +0200144 ASSERT_TRUE(success);
145 ASSERT_EQ(entry, 0x0002);
146
Ryan Mitchella3628462019-01-14 12:19:40 -0800147 success = LoadedIdmap::Lookup(header, 0x000d, &entry); // string/x
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800148 ASSERT_FALSE(success);
149
Ryan Mitchella3628462019-01-14 12:19:40 -0800150 success = LoadedIdmap::Lookup(header, 0x000e, &entry); // string/y
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800151 ASSERT_FALSE(success);
152
Ryan Mitchella3628462019-01-14 12:19:40 -0800153 success = LoadedIdmap::Lookup(header, 0x000f, &entry); // string/z
Mårten Kongstad02751232018-04-27 13:16:32 +0200154 ASSERT_FALSE(success);
155}
156
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100157} // namespace android::idmap2