blob: 2698ac0a734d7d8fdf9e8586e381b8d13bbcec29 [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 =
81 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, error);
82 ASSERT_THAT(idmap, NotNull());
83
84 std::stringstream stream;
85 BinaryStreamVisitor visitor(stream);
86 idmap->accept(&visitor);
87 const std::string str = stream.str();
88 const StringPiece data(str);
89 std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(data);
90 ASSERT_THAT(loaded_idmap, NotNull());
91 ASSERT_EQ(loaded_idmap->TargetPackageId(), 0x7f);
92
93 const IdmapEntry_header* header = loaded_idmap->GetEntryMapForType(0x01);
94 ASSERT_THAT(header, NotNull());
95
96 EntryId entry;
97 bool success = LoadedIdmap::Lookup(header, 0x0000, &entry);
98 ASSERT_TRUE(success);
99 ASSERT_EQ(entry, 0x0000);
100
101 header = loaded_idmap->GetEntryMapForType(0x02);
102 ASSERT_THAT(header, NotNull());
103
104 success = LoadedIdmap::Lookup(header, 0x0002, &entry);
105 ASSERT_FALSE(success);
106
107 success = LoadedIdmap::Lookup(header, 0x0003, &entry);
108 ASSERT_TRUE(success);
109 ASSERT_EQ(entry, 0x0000);
110
111 success = LoadedIdmap::Lookup(header, 0x0004, &entry);
112 ASSERT_FALSE(success);
113
114 success = LoadedIdmap::Lookup(header, 0x0005, &entry);
115 ASSERT_TRUE(success);
116 ASSERT_EQ(entry, 0x0001);
117
118 success = LoadedIdmap::Lookup(header, 0x0006, &entry);
119 ASSERT_TRUE(success);
120 ASSERT_EQ(entry, 0x0002);
121
122 success = LoadedIdmap::Lookup(header, 0x0007, &entry);
123 ASSERT_FALSE(success);
124}
125
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100126} // namespace android::idmap2