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