blob: c28ce2e02ea98e541a54bcdc8d758c4c3e8d74da [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
30using ::testing::IsNull;
31using ::testing::NotNull;
32
33namespace android {
34namespace idmap2 {
35
36TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
37 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
38 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
39 ASSERT_THAT(target_apk, NotNull());
40
41 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
42 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
43 ASSERT_THAT(overlay_apk, NotNull());
44
45 std::stringstream error;
46 std::unique_ptr<const Idmap> idmap =
47 Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, error);
48 ASSERT_THAT(idmap, NotNull());
49
50 std::stringstream stream;
51 RawPrintVisitor visitor(stream);
52 idmap->accept(&visitor);
53
54 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
55 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
56 ASSERT_NE(stream.str().find("00000008: f5ad1d1d target crc\n"), std::string::npos);
57 ASSERT_NE(stream.str().find("0000000c: d470336b overlay crc\n"), std::string::npos);
58 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f010000 -> 0x7f010000 integer/int1\n"),
59 std::string::npos);
60}
61
62TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
63 fclose(stderr); // silence expected warnings from libandroidfw
64
65 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
66 std::istringstream raw_stream(raw);
67
68 std::stringstream error;
69 std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(raw_stream, error);
70 ASSERT_THAT(idmap, NotNull());
71
72 std::stringstream stream;
73 RawPrintVisitor visitor(stream);
74 idmap->accept(&visitor);
75
76 ASSERT_NE(stream.str().find("00000000: 504d4449 magic\n"), std::string::npos);
77 ASSERT_NE(stream.str().find("00000004: 00000001 version\n"), std::string::npos);
78 ASSERT_NE(stream.str().find("00000008: 00001234 target crc\n"), std::string::npos);
79 ASSERT_NE(stream.str().find("0000000c: 00005678 overlay crc\n"), std::string::npos);
80 ASSERT_NE(stream.str().find("0000021c: 00000000 0x7f020000 -> 0x7f020000\n"), std::string::npos);
81}
82
83} // namespace idmap2
84} // namespace android