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 #include "utils.h"
24 
25 static std::unique_ptr<Command> InjectCmd() { return CreateCommandInstance("inject"); }
26 
27 TEST(cmd_inject, smoke) {
28   TemporaryFile tmpfile;
29   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
30                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "-o", tmpfile.path}));
31   std::string data;
32   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
33   // Test that we can find instr range in etm_test_loop binary.
34   ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
35   std::string expected_data;
36   ASSERT_TRUE(android::base::ReadFileToString(
37       GetTestData(std::string("etm") + OS_PATH_SEPARATOR + "perf_inject.data"), &expected_data));
38   ASSERT_EQ(data, expected_data);
39 }
40 
41 TEST(cmd_inject, binary_option) {
42   // Test that data for etm_test_loop is generated when selected by --binary.
43   TemporaryFile tmpfile;
44   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
45                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary", "etm_test_loop",
46                                 "-o", tmpfile.path}));
47   std::string data;
48   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
49   ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
50 
51   // Test that data for etm_test_loop is generated when selected by regex.
52   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
53                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary", "etm_t.*_loop",
54                                 "-o", tmpfile.path}));
55   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
56   ASSERT_NE(data.find("etm_test_loop"), std::string::npos);
57 
58   // Test that data for etm_test_loop isn't generated when not selected by --binary.
59   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
60                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary",
61                                 "no_etm_test_loop", "-o", tmpfile.path}));
62   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
63   ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
64 
65   // Test that data for etm_test_loop isn't generated when not selected by regex.
66   ASSERT_TRUE(InjectCmd()->Run({"--symdir", GetTestDataDir() + "etm", "-i",
67                                 GetTestData(PERF_DATA_ETM_TEST_LOOP), "--binary",
68                                 "no_etm_test_.*", "-o", tmpfile.path}));
69   ASSERT_TRUE(android::base::ReadFileToString(tmpfile.path, &data));
70   ASSERT_EQ(data.find("etm_test_loop"), std::string::npos);
71 }
72