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 {
TEST(ApkParsingTest,ValidArm64Path)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 
TEST(ApkParsingTest,ValidArm64PathButSuppressed)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 
TEST(ApkParsingTest,ValidArm32Path)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 
TEST(ApkParsingTest,InvalidMustStartWithLib)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 
TEST(ApkParsingTest,InvalidMustEndInSo)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 
TEST(ApkParsingTest,InvalidCharacter)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 
TEST(ApkParsingTest,InvalidSubdirectories)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 
TEST(ApkParsingTest,InvalidFileAtRoot)72 TEST(ApkParsingTest, InvalidFileAtRoot) {
73   const char* path = "lib/library.so";
74   auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false);
75   ASSERT_THAT(lastSlash, IsNull());
76 }
77 
TEST(ApkParsingTest,InvalidPrefix)78 TEST(ApkParsingTest, InvalidPrefix) {
79   const char* path = "assets/libhello.so";
80   auto lastSlash = util::ValidLibraryPathLastSlash(path, false, false);
81   ASSERT_THAT(lastSlash, IsNull());
82 }
83 }