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