blob: 7372148f0f0ec3c523f076b3c747f6a460679852 [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
22#include "gmock/gmock.h"
23#include "gtest/gtest.h"
24
25#include "idmap2/Idmap.h"
26#include "idmap2/RawPrintVisitor.h"
27
28#include "TestHelpers.h"
29
Mårten Kongstad02751232018-04-27 13:16:32 +020030using ::testing::NotNull;
31
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010032namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020033
34TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
35 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
36 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
37 ASSERT_THAT(target_apk, NotNull());
38
39 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
40 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
41 ASSERT_THAT(overlay_apk, NotNull());
42
Mårten Kongstadce424902019-03-01 08:35:37 +010043 const auto idmap =
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080044 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
Mårten Kongstadce424902019-03-01 08:35:37 +010045 PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
46 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020047
48 std::stringstream stream;
49 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010050 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020051
52 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
53 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
Winsonb4100202019-02-06 12:05:32 -080054 ASSERT_NE(stream.str().find("00000008: d513ca1b target crc\n"), std::string::npos);
55 ASSERT_NE(stream.str().find("0000000c: 8635c2ed overlay crc\n"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +020056 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f010000 -> 0x7f010000 integer/int1\n"),
57 std::string::npos);
58}
59
60TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
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
Mårten Kongstadce424902019-03-01 08:35:37 +010066 const auto idmap = Idmap::FromBinaryStream(raw_stream);
67 ASSERT_TRUE(idmap);
Mårten Kongstad02751232018-04-27 13:16:32 +020068
69 std::stringstream stream;
70 RawPrintVisitor visitor(stream);
Mårten Kongstadce424902019-03-01 08:35:37 +010071 (*idmap)->accept(&visitor);
Mårten Kongstad02751232018-04-27 13:16:32 +020072
73 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
74 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
75 ASSERT_NE(stream.str().find("00000008: 00001234 target crc\n"), std::string::npos);
76 ASSERT_NE(stream.str().find("0000000c: 00005678 overlay crc\n"), std::string::npos);
77 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f020000 -> 0x7f020000\n"), std::string::npos);
78}
79
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010080} // namespace android::idmap2