1 /*
2  * Copyright (C) 2020 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 #include <aidl/Gtest.h>
17 #include <aidl/Vintf.h>
18 
19 #include <aidl/android/hardware/memtrack/DeviceInfo.h>
20 #include <aidl/android/hardware/memtrack/IMemtrack.h>
21 #include <aidl/android/hardware/memtrack/MemtrackType.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 #include <vintf/VintfObject.h>
25 
26 using aidl::android::hardware::memtrack::DeviceInfo;
27 using aidl::android::hardware::memtrack::IMemtrack;
28 using aidl::android::hardware::memtrack::MemtrackRecord;
29 using aidl::android::hardware::memtrack::MemtrackType;
30 using android::vintf::KernelVersion;
31 using android::vintf::RuntimeInfo;
32 using android::vintf::VintfObject;
33 
34 class MemtrackAidlTest : public testing::TestWithParam<std::string> {
35   public:
SetUp()36     virtual void SetUp() override {
37         const auto instance = GetParam();
38         ASSERT_TRUE(AServiceManager_isDeclared(instance.c_str()));
39         auto memtrackBinder = ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str()));
40         memtrack_ = IMemtrack::fromBinder(memtrackBinder);
41         ASSERT_NE(memtrack_, nullptr);
42     }
43 
44     std::shared_ptr<IMemtrack> memtrack_;
45 };
46 
TEST_P(MemtrackAidlTest,GetMemoryInvalidPid)47 TEST_P(MemtrackAidlTest, GetMemoryInvalidPid) {
48     int pid = -1;
49 
50     for (MemtrackType type : ndk::enum_range<MemtrackType>()) {
51         std::vector<MemtrackRecord> records;
52 
53         auto status = memtrack_->getMemory(pid, type, &records);
54 
55         EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT);
56     }
57 }
58 
TEST_P(MemtrackAidlTest,GetMemoryInvalidType)59 TEST_P(MemtrackAidlTest, GetMemoryInvalidType) {
60     int pid = 1;
61     MemtrackType type = static_cast<MemtrackType>(-1);
62     std::vector<MemtrackRecord> records;
63 
64     auto status = memtrack_->getMemory(pid, type, &records);
65 
66     EXPECT_EQ(status.getExceptionCode(), EX_UNSUPPORTED_OPERATION);
67 }
68 
TEST_P(MemtrackAidlTest,GetMemory)69 TEST_P(MemtrackAidlTest, GetMemory) {
70     int pid = 1;
71     for (MemtrackType type : ndk::enum_range<MemtrackType>()) {
72         std::vector<MemtrackRecord> records;
73 
74         auto status = memtrack_->getMemory(pid, type, &records);
75 
76         EXPECT_TRUE(status.isOk());
77     }
78 }
79 
TEST_P(MemtrackAidlTest,GetGpuDeviceInfo)80 TEST_P(MemtrackAidlTest, GetGpuDeviceInfo) {
81     std::vector<DeviceInfo> device_info;
82 
83     auto status = memtrack_->getGpuDeviceInfo(&device_info);
84 
85     // Devices with < 5.4 kernels aren't required to provide an implementation of
86     // getGpuDeviceInfo(), and can return EX_UNSUPPORTED_OPERATION
87     if (status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
88         KernelVersion min_kernel_version = KernelVersion(5, 4, 0);
89         KernelVersion kernel_version = VintfObject::GetInstance()
90                                                ->getRuntimeInfo(RuntimeInfo::FetchFlag::CPU_VERSION)
91                                                ->kernelVersion();
92         EXPECT_LT(kernel_version, min_kernel_version)
93                 << "Devices with 5.4 or later kernels must implement getGpuDeviceInfo()";
94         return;
95     }
96 
97     EXPECT_TRUE(status.isOk());
98     EXPECT_FALSE(device_info.empty());
99     for (auto device : device_info) {
100         EXPECT_FALSE(device.name.empty());
101     }
102 }
103 
104 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemtrackAidlTest);
105 INSTANTIATE_TEST_SUITE_P(PerInstance, MemtrackAidlTest,
106                          testing::ValuesIn(android::getAidlHalInstanceNames(IMemtrack::descriptor)),
107                          android::PrintInstanceNameToString);
108 
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110     ::testing::InitGoogleTest(&argc, argv);
111     ABinderProcess_setThreadPoolMaxThreadCount(1);
112     ABinderProcess_startThreadPool();
113     return RUN_ALL_TESTS();
114 }
115