Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 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 | |
| 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 | class NoopDiagnostics : public IDiagnostics { |
| 47 | public: |
| 48 | void Log(Level level, DiagMessageActual& actualMsg) override { |
| 49 | } |
| 50 | }; |
| 51 | static NoopDiagnostics noop_diag; |
| 52 | |
| 53 | TEST_F(ApkInfoTest, ApkInfoWithBadging) { |
| 54 | auto apk_path = file::BuildPath( |
| 55 | {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"}); |
| 56 | auto out_info_path = GetTestPath("apk_info.pb"); |
| 57 | |
| 58 | ApkInfoCommand command(&noop_diag); |
| 59 | command.Execute({"-o", out_info_path, apk_path}, &std::cerr); |
| 60 | |
| 61 | auto expected_path = |
| 62 | file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", |
| 63 | "components_expected_proto.txt"}); |
| 64 | AssertProducedAndExpectedInfo(out_info_path, expected_path); |
| 65 | } |
| 66 | |
| 67 | TEST_F(ApkInfoTest, FullApkInfo) { |
| 68 | auto apk_path = file::BuildPath( |
| 69 | {android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", "components.apk"}); |
| 70 | auto out_info_path = GetTestPath("apk_info.pb"); |
| 71 | |
| 72 | ApkInfoCommand command(&noop_diag); |
| 73 | command.Execute({"-o", out_info_path, "--include-resource-table", "--include-xml", |
| 74 | "AndroidManifest.xml", "--include-xml", "res/oy.xml", apk_path}, |
| 75 | &std::cerr); |
| 76 | |
| 77 | auto expected_path = |
| 78 | file::BuildPath({android::base::GetExecutableDirectory(), "integration-tests", "DumpTest", |
| 79 | "components_full_proto.txt"}); |
| 80 | AssertProducedAndExpectedInfo(out_info_path, expected_path); |
| 81 | } |
| 82 | |
| 83 | } // namespace aapt |