blob: d5f4612ca2024c3a78d62c5344dcacb32f015aa1 [file] [log] [blame]
Kalesh Singha851f3d2020-12-01 11:47:01 -05001/*
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>
Kalesh Singh11fdc972021-01-22 16:59:15 -050024#include <vintf/VintfObject.h>
Kalesh Singha851f3d2020-12-01 11:47:01 -050025
26using aidl::android::hardware::memtrack::DeviceInfo;
27using aidl::android::hardware::memtrack::IMemtrack;
28using aidl::android::hardware::memtrack::MemtrackRecord;
29using aidl::android::hardware::memtrack::MemtrackType;
Kalesh Singh11fdc972021-01-22 16:59:15 -050030using android::vintf::KernelVersion;
31using android::vintf::RuntimeInfo;
32using android::vintf::VintfObject;
Kalesh Singha851f3d2020-12-01 11:47:01 -050033
34class MemtrackAidlTest : public testing::TestWithParam<std::string> {
35 public:
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
47TEST_P(MemtrackAidlTest, GetMemoryInvalidPid) {
48 int pid = -1;
49 MemtrackType type = MemtrackType::OTHER;
50 std::vector<MemtrackRecord> records;
51
52 auto status = memtrack_->getMemory(pid, type, &records);
53
54 EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT);
55}
56
57TEST_P(MemtrackAidlTest, GetMemoryInvalidType) {
58 int pid = 1;
59 MemtrackType type = MemtrackType::NUM_TYPES;
60 std::vector<MemtrackRecord> records;
61
62 auto status = memtrack_->getMemory(pid, type, &records);
63
64 EXPECT_EQ(status.getExceptionCode(), EX_UNSUPPORTED_OPERATION);
65}
66
67TEST_P(MemtrackAidlTest, GetMemory) {
68 int pid = 1;
69 MemtrackType type = MemtrackType::OTHER;
70 std::vector<MemtrackRecord> records;
71
72 auto status = memtrack_->getMemory(pid, type, &records);
73
74 EXPECT_TRUE(status.isOk());
75}
76
77TEST_P(MemtrackAidlTest, GetGpuDeviceInfo) {
78 std::vector<DeviceInfo> device_info;
79
80 auto status = memtrack_->getGpuDeviceInfo(&device_info);
81
Kalesh Singha259c592021-02-02 15:43:58 -050082 // Devices with < 5.4 kernels aren't required to provide an implementation of
Kalesh Singh11fdc972021-01-22 16:59:15 -050083 // getGpuDeviceInfo(), and can return EX_UNSUPPORTED_OPERATION
84 if (status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
Kalesh Singha259c592021-02-02 15:43:58 -050085 KernelVersion min_kernel_version = KernelVersion(5, 4, 0);
Kalesh Singh11fdc972021-01-22 16:59:15 -050086 KernelVersion kernel_version = VintfObject::GetInstance()
87 ->getRuntimeInfo(RuntimeInfo::FetchFlag::CPU_VERSION)
88 ->kernelVersion();
89 EXPECT_LT(kernel_version, min_kernel_version)
90 << "Devices with 5.10 or later kernels must implement getGpuDeviceInfo()";
91 return;
92 }
93
Kalesh Singha851f3d2020-12-01 11:47:01 -050094 EXPECT_TRUE(status.isOk());
Kalesh Singh11fdc972021-01-22 16:59:15 -050095 EXPECT_FALSE(device_info.empty());
96 for (auto device : device_info) {
97 EXPECT_FALSE(device.name.empty());
98 }
Kalesh Singha851f3d2020-12-01 11:47:01 -050099}
100
101GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemtrackAidlTest);
102INSTANTIATE_TEST_SUITE_P(PerInstance, MemtrackAidlTest,
103 testing::ValuesIn(android::getAidlHalInstanceNames(IMemtrack::descriptor)),
104 android::PrintInstanceNameToString);
105
106int main(int argc, char** argv) {
107 ::testing::InitGoogleTest(&argc, argv);
108 ABinderProcess_setThreadPoolMaxThreadCount(1);
109 ABinderProcess_startThreadPool();
110 return RUN_ALL_TESTS();
111}