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 "ApkInfo.h"
18 
19 #include "ApkInfo.pb.h"
20 #include "LoadedApk.h"
21 #include "android-base/unique_fd.h"
22 #include "io/StringStream.h"
23 #include "test/Test.h"
24 
25 using testing::Eq;
26 using testing::Ne;
27 
28 namespace aapt {
29 
30 using ApkInfoTest = CommandTestFixture;
31 
AssertProducedAndExpectedInfo(const std::string & produced_path,const std::string & expected_path)32 void AssertProducedAndExpectedInfo(const std::string& produced_path,
33                                    const std::string& expected_path) {
34   android::base::unique_fd fd(open(produced_path.c_str(), O_RDONLY));
35   ASSERT_NE(fd.get(), -1);
36 
37   pb::ApkInfo produced_apk_info;
38   produced_apk_info.ParseFromFileDescriptor(fd.get());
39 
40   std::string expected;
41   ::android::base::ReadFileToString(expected_path, &expected);
42 
43   EXPECT_EQ(produced_apk_info.DebugString(), expected);
44 }
45 
46 static android::NoOpDiagnostics noop_diag;
47 
TEST_F(ApkInfoTest,ApkInfoWithBadging)48 TEST_F(ApkInfoTest, ApkInfoWithBadging) {
49   auto apk_path = file::BuildPath(
50       {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
51   auto out_info_path = GetTestPath("apk_info.pb");
52 
53   ApkInfoCommand command(&noop_diag);
54   command.Execute({"-o", out_info_path, apk_path}, &std::cerr);
55 
56   auto expected_path =
57       file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
58                        "components_expected_proto.txt"});
59   AssertProducedAndExpectedInfo(out_info_path, expected_path);
60 }
61 
TEST_F(ApkInfoTest,FullApkInfo)62 TEST_F(ApkInfoTest, FullApkInfo) {
63   auto apk_path = file::BuildPath(
64       {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"});
65   auto out_info_path = GetTestPath("apk_info.pb");
66 
67   ApkInfoCommand command(&noop_diag);
68   command.Execute({"-o", out_info_path, "--include-resource-table", "--include-xml",
69                    "AndroidManifest.xml", "--include-xml", "res/oy.xml", apk_path},
70                   &std::cerr);
71 
72   auto expected_path =
73       file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest",
74                        "components_full_proto.txt"});
75   AssertProducedAndExpectedInfo(out_info_path, expected_path);
76 }
77 
78 }  // namespace aapt
79