1 /*
2  * Copyright (C) 2019 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 <sys/types.h>
18 
19 #include <memory>
20 #include <string>
21 #include <string_view>
22 
23 #include <android-base/file.h>
24 #include <dex/dex_file.h>
25 #include <gtest/gtest.h>
26 
27 #include "art_api/dex_file_support.h"
28 #include "dex_file_test_data.h"
29 
30 namespace art_api {
31 namespace dex {
32 
TEST(DexFileTest,create)33 TEST(DexFileTest, create) {
34   size_t size = sizeof(kDexData);
35   std::unique_ptr<DexFile> dex_file;
36   EXPECT_TRUE(DexFile::Create(kDexData, size, &size, "", &dex_file).Ok());
37   EXPECT_EQ(size, sizeof(kDexData));
38   EXPECT_NE(dex_file, nullptr);
39 }
40 
TEST(DexFileTest,create_header_too_small)41 TEST(DexFileTest, create_header_too_small) {
42   size_t size = sizeof(art::DexFile::Header) - 1;
43   std::unique_ptr<DexFile> dex_file;
44   DexFile::Error error = DexFile::Create(kDexData, size, &size, "", &dex_file);
45   EXPECT_FALSE(error.Ok());
46   EXPECT_EQ(error.Code(), ADEXFILE_ERROR_NOT_ENOUGH_DATA);
47   EXPECT_STREQ(error.ToString(), "Not enough data. Incomplete dex file.");
48   EXPECT_EQ(size, sizeof(art::DexFile::Header));
49   EXPECT_EQ(dex_file, nullptr);
50 }
51 
TEST(DexFileTest,create_file_too_small)52 TEST(DexFileTest, create_file_too_small) {
53   size_t size = sizeof(art::DexFile::Header);
54   std::unique_ptr<DexFile> dex_file;
55   DexFile::Error error = DexFile::Create(kDexData, size, &size, "", &dex_file);
56   EXPECT_FALSE(error.Ok());
57   EXPECT_EQ(error.Code(), ADEXFILE_ERROR_NOT_ENOUGH_DATA);
58   EXPECT_STREQ(error.ToString(), "Not enough data. Incomplete dex file.");
59   EXPECT_EQ(size, sizeof(kDexData));
60   EXPECT_EQ(dex_file, nullptr);
61 }
62 
GetTestDexData()63 static std::unique_ptr<DexFile> GetTestDexData() {
64   size_t size = sizeof(kDexData);
65   std::unique_ptr<DexFile> dex_file;
66   EXPECT_TRUE(DexFile::Create(kDexData, size, &size, "", &dex_file).Ok());
67   EXPECT_EQ(size, sizeof(kDexData));
68   EXPECT_NE(dex_file, nullptr);
69   return dex_file;
70 }
71 
TEST(DexFileTest,findMethodAtOffset)72 TEST(DexFileTest, findMethodAtOffset) {
73   std::unique_ptr<DexFile> dex_file = GetTestDexData();
74   ASSERT_NE(dex_file, nullptr);
75 
76   bool found_init = false;
77   auto check_init = [&](const DexFile::Method& method) {
78     size_t size;
79     size_t offset = method.GetCodeOffset(&size);
80     EXPECT_EQ(offset, 0x100u);
81     EXPECT_EQ(size, 8u);
82     EXPECT_STREQ(method.GetName(), "<init>");
83     EXPECT_STREQ(method.GetQualifiedName(), "Main.<init>");
84     EXPECT_STREQ(method.GetQualifiedName(true), "void Main.<init>()");
85     EXPECT_STREQ(method.GetClassDescriptor(), "LMain;");
86     found_init = true;
87   };
88   EXPECT_EQ(dex_file->FindMethodAtOffset(0x102, check_init), 1u);
89   EXPECT_TRUE(found_init);
90 
91   bool found_main = false;
92   auto check_main = [&](const DexFile::Method& method) {
93     size_t size;
94     size_t offset = method.GetCodeOffset(&size);
95     EXPECT_EQ(offset, 0x118u);
96     EXPECT_EQ(size, 2u);
97     EXPECT_STREQ(method.GetName(), "main");
98     EXPECT_STREQ(method.GetQualifiedName(), "Main.main");
99     EXPECT_STREQ(method.GetQualifiedName(true), "void Main.main(java.lang.String[])");
100     EXPECT_STREQ(method.GetClassDescriptor(), "LMain;");
101     found_main = true;
102   };
103   EXPECT_EQ(dex_file->FindMethodAtOffset(0x118, check_main), 1u);
104   EXPECT_TRUE(found_main);
105 }
106 
TEST(DexFileTest,get_method_info_for_offset_boundaries)107 TEST(DexFileTest, get_method_info_for_offset_boundaries) {
108   std::unique_ptr<DexFile> dex_file = GetTestDexData();
109   ASSERT_NE(dex_file, nullptr);
110 
111   EXPECT_EQ(dex_file->FindMethodAtOffset(0x99, [](auto){}), 0);
112   EXPECT_EQ(dex_file->FindMethodAtOffset(0x100, [](auto){}), 1);
113   EXPECT_EQ(dex_file->FindMethodAtOffset(0x107, [](auto){}), 1);
114   EXPECT_EQ(dex_file->FindMethodAtOffset(0x108, [](auto){}), 0);
115   EXPECT_EQ(dex_file->FindMethodAtOffset(0x100000, [](auto){}), 0);
116 }
117 
TEST(DexFileTest,get_all_method_infos_without_signature)118 TEST(DexFileTest, get_all_method_infos_without_signature) {
119   std::unique_ptr<DexFile> dex_file = GetTestDexData();
120   ASSERT_NE(dex_file, nullptr);
121 
122   std::vector<std::string> names;
123   auto add = [&](const DexFile::Method& method) { names.push_back(method.GetQualifiedName()); };
124   EXPECT_EQ(dex_file->ForEachMethod(add), 2u);
125   EXPECT_EQ(names, std::vector<std::string>({"Main.<init>", "Main.main"}));
126 }
127 
128 }  // namespace dex
129 }  // namespace art_api
130