| 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 <string> |
| 19 | |
| 20 | #include "R.h" |
| 21 | #include "TestConstants.h" |
| 22 | #include "TestHelpers.h" |
| 23 | #include "androidfw/ApkAssets.h" |
| 24 | #include "androidfw/ResourceTypes.h" |
| 25 | #include "gmock/gmock.h" |
| 26 | #include "gtest/gtest.h" |
| 27 | #include "idmap2/Idmap.h" |
| 28 | #include "idmap2/PrettyPrintVisitor.h" |
| 29 | |
| 30 | using android::base::StringPrintf; |
| 31 | |
| 32 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 33 | |
| 34 | namespace android::idmap2 { |
| 35 | |
| 36 | TEST(PrettyPrintVisitorTests, CreatePrettyPrintVisitor) { |
| 37 | const std::string target_apk_path(GetTestDataPath() + "/target/target.apk"); |
| 38 | auto target = TargetResourceContainer::FromPath(target_apk_path); |
| 39 | ASSERT_TRUE(target); |
| 40 | |
| 41 | const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk"); |
| 42 | auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path); |
| 43 | ASSERT_TRUE(overlay); |
| 44 | |
| 45 | const auto idmap = Idmap::FromContainers(**target, **overlay, TestConstants::OVERLAY_NAME_DEFAULT, |
| 46 | PolicyFlags::PUBLIC, /* enforce_overlayable */ true); |
| 47 | ASSERT_TRUE(idmap); |
| 48 | |
| 49 | std::stringstream stream; |
| 50 | PrettyPrintVisitor visitor(stream); |
| 51 | (*idmap)->accept(&visitor); |
| 52 | |
| 53 | ASSERT_NE(stream.str().find("target path : "), std::string::npos); |
| 54 | ASSERT_NE(stream.str().find("overlay path : "), std::string::npos); |
| 55 | ASSERT_NE(stream.str().find(StringPrintf("0x%08x -> 0x%08x (integer/int1 -> integer/int1)\n", |
| 56 | R::target::integer::int1, R::overlay::integer::int1)), |
| 57 | std::string::npos); |
| 58 | } |
| 59 | |
| 60 | TEST(PrettyPrintVisitorTests, CreatePrettyPrintVisitorWithoutAccessToApks) { |
| 61 | fclose(stderr); // silence expected warnings from libandroidfw |
| 62 | |
| 63 | std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen); |
| 64 | std::istringstream raw_stream(raw); |
| 65 | |
| 66 | const auto idmap = Idmap::FromBinaryStream(raw_stream); |
| 67 | ASSERT_TRUE(idmap); |
| 68 | |
| 69 | std::stringstream stream; |
| 70 | PrettyPrintVisitor visitor(stream); |
| 71 | (*idmap)->accept(&visitor); |
| 72 | |
| 73 | ASSERT_NE(stream.str().find("target path : "), std::string::npos); |
| 74 | ASSERT_NE(stream.str().find("overlay path : "), std::string::npos); |
| 75 | ASSERT_NE(stream.str().find("0x7f020000 -> 0x7f020000 (\?\?\? -> \?\?\?)\n"), std::string::npos); |
| 76 | } |
| 77 | |
| 78 | } // namespace android::idmap2 |