1 /*
2 * Copyright (C) 2022 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 "Dump.h"
18
19 #include "LoadedApk.h"
20 #include "io/StringStream.h"
21 #include "test/Test.h"
22 #include "text/Printer.h"
23
24 using ::aapt::io::StringOutputStream;
25 using ::aapt::text::Printer;
26 using testing::Eq;
27 using testing::Ne;
28
29 namespace aapt {
30
31 using DumpTest = CommandTestFixture;
32
33 static android::NoOpDiagnostics noop_diag;
34
DumpBadgingToString(LoadedApk * loaded_apk,std::string * output,bool include_meta_data=false,bool only_permissions=false)35 void DumpBadgingToString(LoadedApk* loaded_apk, std::string* output, bool include_meta_data = false,
36 bool only_permissions = false) {
37 StringOutputStream output_stream(output);
38 Printer printer(&output_stream);
39
40 DumpBadgingCommand command(&printer, &noop_diag);
41 command.SetIncludeMetaData(include_meta_data);
42 command.SetOnlyPermissions(only_permissions);
43 ASSERT_EQ(command.Dump(loaded_apk), 0);
44 output_stream.Flush();
45 }
46
TEST_F(DumpTest,DumpBadging)47 TEST_F(DumpTest, DumpBadging) {
48 auto apk_path = file::BuildPath(
49 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "minimal.apk"});
50 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
51
52 std::string output;
53 DumpBadgingToString(loaded_apk.get(), &output);
54
55 std::string expected;
56 auto expected_path = file::BuildPath({android::base::GetExecutableDirectory(),
57 "integration-tests", "DumpTest", "minimal_expected.txt"});
58 ::android::base::ReadFileToString(expected_path, &expected);
59 ASSERT_EQ(output, expected);
60 }
61
TEST_F(DumpTest,DumpBadgingMultipleUsesSdkTakesLatest)62 TEST_F(DumpTest, DumpBadgingMultipleUsesSdkTakesLatest) {
63 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests",
64 "DumpTest", "multiple_uses_sdk.apk"});
65 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
66
67 std::string output;
68 DumpBadgingToString(loaded_apk.get(), &output);
69
70 std::string expected;
71 auto expected_path =
72 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
73 "multiple_uses_sdk_expected.txt"});
74 ::android::base::ReadFileToString(expected_path, &expected);
75 ASSERT_EQ(output, expected);
76 }
77
TEST_F(DumpTest,DumpBadgingAllComponents)78 TEST_F(DumpTest, DumpBadgingAllComponents) {
79 auto apk_path = file::BuildPath(
80 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
81 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
82
83 std::string output;
84 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ true);
85
86 std::string expected;
87 auto expected_path =
88 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
89 "components_expected.txt"});
90 ::android::base::ReadFileToString(expected_path, &expected);
91 ASSERT_EQ(output, expected);
92 }
93
TEST_F(DumpTest,DumpBadgingPermissionsOnly)94 TEST_F(DumpTest, DumpBadgingPermissionsOnly) {
95 auto apk_path = file::BuildPath(
96 {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
97 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
98
99 std::string output;
100 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ false,
101 /* only_permissions= */ true);
102
103 std::string expected;
104 auto expected_path =
105 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
106 "components_permissions_expected.txt"});
107 ::android::base::ReadFileToString(expected_path, &expected);
108 ASSERT_EQ(output, expected);
109 }
110
TEST_F(DumpTest,DumpBadgingApkBuiltWithAaptAndTagsInWrongPlace)111 TEST_F(DumpTest, DumpBadgingApkBuiltWithAaptAndTagsInWrongPlace) {
112 auto apk_path = file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests",
113 "DumpTest", "built_with_aapt.apk"});
114 auto loaded_apk = LoadedApk::LoadApkFromPath(apk_path, &noop_diag);
115
116 std::string output;
117 DumpBadgingToString(loaded_apk.get(), &output, /* include_meta_data= */ false,
118 /* only_permissions= */ false);
119
120 std::string expected;
121 auto expected_path =
122 file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
123 "built_with_aapt_expected.txt"});
124 ::android::base::ReadFileToString(expected_path, &expected);
125 ASSERT_EQ(output, expected);
126 }
127
128 } // namespace aapt
129