1 /*
2  * Copyright (C) 2018 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 "common_runtime_test.h"
18 #include "exec_utils.h"
19 
20 namespace art {
21 
22 class DexAnalyzeTest : public CommonRuntimeTest {
23  public:
GetDexAnalyzePath()24   std::string GetDexAnalyzePath() {
25     return GetArtBinDir() + "/dexanalyze";
26   }
27 
DexAnalyzeExec(const std::vector<std::string> & args,bool expect_success)28   void DexAnalyzeExec(const std::vector<std::string>& args, bool expect_success) {
29     std::string binary = GetDexAnalyzePath();
30     CHECK(OS::FileExists(binary.c_str())) << binary << " should be a valid file path";
31     std::vector<std::string> argv;
32     argv.push_back(binary);
33     argv.insert(argv.end(), args.begin(), args.end());
34     std::string error_msg;
35     ASSERT_EQ(::art::Exec(argv, &error_msg), expect_success) << error_msg;
36   }
37 };
38 
TEST_F(DexAnalyzeTest,NoInputFileGiven)39 TEST_F(DexAnalyzeTest, NoInputFileGiven) {
40   DexAnalyzeExec({ "-a" }, /*expect_success=*/ false);
41 }
42 
TEST_F(DexAnalyzeTest,CantOpenInput)43 TEST_F(DexAnalyzeTest, CantOpenInput) {
44   DexAnalyzeExec({ "-a", "/non/existent/path" }, /*expect_success=*/ false);
45 }
46 
TEST_F(DexAnalyzeTest,TestAnalyzeMultidex)47 TEST_F(DexAnalyzeTest, TestAnalyzeMultidex) {
48   DexAnalyzeExec({ "-a", GetTestDexFileName("MultiDex") }, /*expect_success=*/ true);
49 }
50 
TEST_F(DexAnalyzeTest,TestAnalizeCoreDex)51 TEST_F(DexAnalyzeTest, TestAnalizeCoreDex) {
52   DexAnalyzeExec({ "-a", GetLibCoreDexFileNames()[0] }, /*expect_success=*/ true);
53 }
54 
TEST_F(DexAnalyzeTest,TestInvalidArg)55 TEST_F(DexAnalyzeTest, TestInvalidArg) {
56   DexAnalyzeExec({ "-invalid-option" }, /*expect_success=*/ false);
57 }
58 
59 }  // namespace art
60