blob: 3b9dbe97e8944774d09c79225999db89b81b8fe1 [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
34namespace android {
35namespace idmap2 {
36
37TEST(BinaryStreamVisitorTests, CreateBinaryStreamViaBinaryStreamVisitor) {
38 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
39 std::istringstream raw_stream(raw);
40
41 std::stringstream error;
42 std::unique_ptr<const Idmap> idmap1 = Idmap::FromBinaryStream(raw_stream, error);
43 ASSERT_THAT(idmap1, NotNull());
44
45 std::stringstream stream;
46 BinaryStreamVisitor visitor(stream);
47 idmap1->accept(&visitor);
48
49 std::unique_ptr<const Idmap> idmap2 = Idmap::FromBinaryStream(stream, error);
50 ASSERT_THAT(idmap2, NotNull());
51
52 ASSERT_EQ(idmap1->GetHeader()->GetTargetCrc(), idmap2->GetHeader()->GetTargetCrc());
53 ASSERT_EQ(idmap1->GetHeader()->GetTargetPath(), idmap2->GetHeader()->GetTargetPath());
Mårten Kongstadb8779022018-11-29 09:53:17 +010054 ASSERT_EQ(idmap1->GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020055 ASSERT_EQ(idmap1->GetData().size(), idmap2->GetData().size());
56
57 const auto& data1 = idmap1->GetData()[0];
58 const auto& data2 = idmap2->GetData()[0];
59
60 ASSERT_EQ(data1->GetHeader()->GetTargetPackageId(), data2->GetHeader()->GetTargetPackageId());
Mårten Kongstadb8779022018-11-29 09:53:17 +010061 ASSERT_EQ(data1->GetTypeEntries().size(), 2U);
Mårten Kongstad02751232018-04-27 13:16:32 +020062 ASSERT_EQ(data1->GetTypeEntries().size(), data2->GetTypeEntries().size());
63 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(0), data2->GetTypeEntries()[0]->GetEntry(0));
64 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(1), data2->GetTypeEntries()[0]->GetEntry(1));
65 ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(2), data2->GetTypeEntries()[0]->GetEntry(2));
66 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(0), data2->GetTypeEntries()[1]->GetEntry(0));
67 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(1), data2->GetTypeEntries()[1]->GetEntry(1));
68 ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(2), data2->GetTypeEntries()[1]->GetEntry(2));
69}
70
71TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) {
72 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
73 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
74 ASSERT_THAT(target_apk, NotNull());
75
76 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
77 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
78 ASSERT_THAT(overlay_apk, NotNull());
79
80 std::stringstream error;
81 std::unique_ptr<const Idmap> idmap =
82 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, error);
83 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
105 success = LoadedIdmap::Lookup(header, 0x0002, &entry);
106 ASSERT_FALSE(success);
107
108 success = LoadedIdmap::Lookup(header, 0x0003, &entry);
109 ASSERT_TRUE(success);
110 ASSERT_EQ(entry, 0x0000);
111
112 success = LoadedIdmap::Lookup(header, 0x0004, &entry);
113 ASSERT_FALSE(success);
114
115 success = LoadedIdmap::Lookup(header, 0x0005, &entry);
116 ASSERT_TRUE(success);
117 ASSERT_EQ(entry, 0x0001);
118
119 success = LoadedIdmap::Lookup(header, 0x0006, &entry);
120 ASSERT_TRUE(success);
121 ASSERT_EQ(entry, 0x0002);
122
123 success = LoadedIdmap::Lookup(header, 0x0007, &entry);
124 ASSERT_FALSE(success);
125}
126
127} // namespace idmap2
128} // namespace android