blob: c243d745e56825a8193cc8730e0e50c6e531e739 [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 <cstdio> // fclose
18#include <memory>
19#include <sstream>
20#include <string>
21
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070022#include "TestHelpers.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020023#include "gmock/gmock.h"
24#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020025#include "idmap2/Idmap.h"
26#include "idmap2/RawPrintVisitor.h"
27
Mårten Kongstad02751232018-04-27 13:16:32 +020028using ::testing::NotNull;
29
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
33 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
34 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
35 ASSERT_THAT(target_apk, NotNull());
36
37 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
38 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
39 ASSERT_THAT(overlay_apk, NotNull());
40
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070041 const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, PolicyFlags::POLICY_PUBLIC,
42 /* enforce_overlayable */ true);
Mårten Kongstadce424902019-03-01 08:35:37 +010043 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020044
45 std::stringstream stream;
46 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010047 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020048
49 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
50 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
Ryan Mitchell939df092019-04-09 17:13:50 -070051 ASSERT_NE(stream.str().find("00000008: 76a20829 target crc\n"), std::string::npos);
Ryan Mitchell9e4f52b2019-09-19 12:15:52 -070052 ASSERT_NE(stream.str().find("0000000c: c054fb26 overlay crc\n"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +020053 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f010000 -> 0x7f010000 integer/int1\n"),
54 std::string::npos);
55}
56
57TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
58 fclose(stderr); // silence expected warnings from libandroidfw
59
60 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
61 std::istringstream raw_stream(raw);
62
Mårten Kongstadce424902019-03-01 08:35:37 +010063 const auto idmap = Idmap::FromBinaryStream(raw_stream);
64 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020065
66 std::stringstream stream;
67 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010068 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020069
70 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
71 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
72 ASSERT_NE(stream.str().find("00000008: 00001234 target crc\n"), std::string::npos);
73 ASSERT_NE(stream.str().find("0000000c: 00005678 overlay crc\n"), std::string::npos);
74 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f020000 -> 0x7f020000\n"), std::string::npos);
75}
76
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010077} // namespace android::idmap2