Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | using ::testing::IsNull; |
| 33 | using ::testing::NotNull; |
| 34 | |
| 35 | namespace android { |
| 36 | namespace idmap2 { |
| 37 | |
| 38 | TEST(BinaryStreamVisitorTests, CreateBinaryStreamViaBinaryStreamVisitor) { |
| 39 | std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len); |
| 40 | std::istringstream raw_stream(raw); |
| 41 | |
| 42 | std::stringstream error; |
| 43 | std::unique_ptr<const Idmap> idmap1 = Idmap::FromBinaryStream(raw_stream, error); |
| 44 | ASSERT_THAT(idmap1, NotNull()); |
| 45 | |
| 46 | std::stringstream stream; |
| 47 | BinaryStreamVisitor visitor(stream); |
| 48 | idmap1->accept(&visitor); |
| 49 | |
| 50 | std::unique_ptr<const Idmap> idmap2 = Idmap::FromBinaryStream(stream, error); |
| 51 | ASSERT_THAT(idmap2, NotNull()); |
| 52 | |
| 53 | ASSERT_EQ(idmap1->GetHeader()->GetTargetCrc(), idmap2->GetHeader()->GetTargetCrc()); |
| 54 | ASSERT_EQ(idmap1->GetHeader()->GetTargetPath(), idmap2->GetHeader()->GetTargetPath()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame^] | 55 | ASSERT_EQ(idmap1->GetData().size(), 1U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 56 | ASSERT_EQ(idmap1->GetData().size(), idmap2->GetData().size()); |
| 57 | |
| 58 | const auto& data1 = idmap1->GetData()[0]; |
| 59 | const auto& data2 = idmap2->GetData()[0]; |
| 60 | |
| 61 | ASSERT_EQ(data1->GetHeader()->GetTargetPackageId(), data2->GetHeader()->GetTargetPackageId()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame^] | 62 | ASSERT_EQ(data1->GetTypeEntries().size(), 2U); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 63 | ASSERT_EQ(data1->GetTypeEntries().size(), data2->GetTypeEntries().size()); |
| 64 | ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(0), data2->GetTypeEntries()[0]->GetEntry(0)); |
| 65 | ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(1), data2->GetTypeEntries()[0]->GetEntry(1)); |
| 66 | ASSERT_EQ(data1->GetTypeEntries()[0]->GetEntry(2), data2->GetTypeEntries()[0]->GetEntry(2)); |
| 67 | ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(0), data2->GetTypeEntries()[1]->GetEntry(0)); |
| 68 | ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(1), data2->GetTypeEntries()[1]->GetEntry(1)); |
| 69 | ASSERT_EQ(data1->GetTypeEntries()[1]->GetEntry(2), data2->GetTypeEntries()[1]->GetEntry(2)); |
| 70 | } |
| 71 | |
| 72 | TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) { |
| 73 | const std::string target_apk_path(GetTestDataPath() + "/target/target.apk"); |
| 74 | std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path); |
| 75 | ASSERT_THAT(target_apk, NotNull()); |
| 76 | |
| 77 | const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk"); |
| 78 | std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path); |
| 79 | ASSERT_THAT(overlay_apk, NotNull()); |
| 80 | |
| 81 | std::stringstream error; |
| 82 | std::unique_ptr<const Idmap> idmap = |
| 83 | Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, error); |
| 84 | ASSERT_THAT(idmap, NotNull()); |
| 85 | |
| 86 | std::stringstream stream; |
| 87 | BinaryStreamVisitor visitor(stream); |
| 88 | idmap->accept(&visitor); |
| 89 | const std::string str = stream.str(); |
| 90 | const StringPiece data(str); |
| 91 | std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(data); |
| 92 | ASSERT_THAT(loaded_idmap, NotNull()); |
| 93 | ASSERT_EQ(loaded_idmap->TargetPackageId(), 0x7f); |
| 94 | |
| 95 | const IdmapEntry_header* header = loaded_idmap->GetEntryMapForType(0x01); |
| 96 | ASSERT_THAT(header, NotNull()); |
| 97 | |
| 98 | EntryId entry; |
| 99 | bool success = LoadedIdmap::Lookup(header, 0x0000, &entry); |
| 100 | ASSERT_TRUE(success); |
| 101 | ASSERT_EQ(entry, 0x0000); |
| 102 | |
| 103 | header = loaded_idmap->GetEntryMapForType(0x02); |
| 104 | ASSERT_THAT(header, NotNull()); |
| 105 | |
| 106 | success = LoadedIdmap::Lookup(header, 0x0002, &entry); |
| 107 | ASSERT_FALSE(success); |
| 108 | |
| 109 | success = LoadedIdmap::Lookup(header, 0x0003, &entry); |
| 110 | ASSERT_TRUE(success); |
| 111 | ASSERT_EQ(entry, 0x0000); |
| 112 | |
| 113 | success = LoadedIdmap::Lookup(header, 0x0004, &entry); |
| 114 | ASSERT_FALSE(success); |
| 115 | |
| 116 | success = LoadedIdmap::Lookup(header, 0x0005, &entry); |
| 117 | ASSERT_TRUE(success); |
| 118 | ASSERT_EQ(entry, 0x0001); |
| 119 | |
| 120 | success = LoadedIdmap::Lookup(header, 0x0006, &entry); |
| 121 | ASSERT_TRUE(success); |
| 122 | ASSERT_EQ(entry, 0x0002); |
| 123 | |
| 124 | success = LoadedIdmap::Lookup(header, 0x0007, &entry); |
| 125 | ASSERT_FALSE(success); |
| 126 | } |
| 127 | |
| 128 | } // namespace idmap2 |
| 129 | } // namespace android |