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 <android-base/file.h>
18 #include <gtest/gtest.h>
19 
20 #include "command.h"
21 #include "get_test_data.h"
22 #include "test_util.h"
23 
InjectCmd()24 static std::unique_ptr<Command> InjectCmd() { return CreateCommandInstance("inject"); }
25 
TEST(cmd_inject,smoke)26 TEST(cmd_inject, smoke) {
27   TemporaryFile tmpfile;
28   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
29                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "-o", tmpfile.path}));
30   std::string data;
31   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
32   // Test that we can find instr range in etm_test_loop binary.
33   ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
34 }
35 
TEST(cmd_inject,binary_option)36 TEST(cmd_inject, binary_option) {
37   // Test that data for etm_test_loop is generated when selected by --binary.
38   TemporaryFile tmpfile;
39   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
40                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary", "etm_test_loop",
41                                 "-o", tmpfile.path}));
42   std::string data;
43   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
44   ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
45 
46   // Test that data for etm_test_loop isn't generated when not selected by --binary.
47   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
48                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary",
49                                 "no_etm_test_loop", "-o", tmpfile.path}));
50   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
51   ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
52 }
53