Jeremy Meyer | 5fd34ea | 2022-12-15 18:43:36 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 "androidfw/ApkParsing.h" |
| 18 | |
| 19 | #include "android-base/test_utils.h" |
| 20 | |
| 21 | #include "TestHelpers.h" |
| 22 | |
| 23 | using ::testing::Eq; |
| 24 | using ::testing::IsNull; |
| 25 | using ::testing::NotNull; |
| 26 | |
| 27 | namespace android { |
| 28 | TEST(ApkParsingTest, ValidArm64Path) { |
| 29 | const char* path = "lib/arm64-v8a/library.so"; |
| 30 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 31 | ASSERT_THAT(lastSlash, NotNull()); |
| 32 | ASSERT_THAT(lastSlash, Eq(path + 13)); |
| 33 | } |
| 34 | |
| 35 | TEST(ApkParsingTest, ValidArm64PathButSuppressed) { |
| 36 | const char* path = "lib/arm64-v8a/library.so"; |
| 37 | auto lastSlash = util::ValidLibraryPathLastSlash(path, true, false); |
| 38 | ASSERT_THAT(lastSlash, IsNull()); |
| 39 | } |
| 40 | |
| 41 | TEST(ApkParsingTest, ValidArm32Path) { |
| 42 | const char* path = "lib/armeabi-v7a/library.so"; |
| 43 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 44 | ASSERT_THAT(lastSlash, NotNull()); |
| 45 | ASSERT_THAT(lastSlash, Eq(path + 15)); |
| 46 | } |
| 47 | |
| 48 | TEST(ApkParsingTest, InvalidMustStartWithLib) { |
| 49 | const char* path = "lib/arm64-v8a/random.so"; |
| 50 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 51 | ASSERT_THAT(lastSlash, IsNull()); |
| 52 | } |
| 53 | |
| 54 | TEST(ApkParsingTest, InvalidMustEndInSo) { |
| 55 | const char* path = "lib/arm64-v8a/library.txt"; |
| 56 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 57 | ASSERT_THAT(lastSlash, IsNull()); |
| 58 | } |
| 59 | |
| 60 | TEST(ApkParsingTest, InvalidCharacter) { |
| 61 | const char* path = "lib/arm64-v8a/lib#.so"; |
| 62 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 63 | ASSERT_THAT(lastSlash, IsNull()); |
| 64 | } |
| 65 | |
| 66 | TEST(ApkParsingTest, InvalidSubdirectories) { |
| 67 | const char* path = "lib/arm64-v8a/anything/library.so"; |
| 68 | auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false); |
| 69 | ASSERT_THAT(lastSlash, IsNull()); |
| 70 | } |
| 71 | } |