blob: 8905f50eaf84fbc491ef96111e3aa80e1f27322f [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;
Kalesh Singha851f3d2020-12-01 11:47:01 -050049
Kalesh Singh1bcbf852021-02-12 11:29:50 -050050 for (MemtrackType type : ndk::enum_range<MemtrackType>()) {
51 std::vector<MemtrackRecord> records;
Kalesh Singha851f3d2020-12-01 11:47:01 -050052
Kalesh Singh1bcbf852021-02-12 11:29:50 -050053 auto status = memtrack_->getMemory(pid, type, &records);
54
55 EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT);
56 }
Kalesh Singha851f3d2020-12-01 11:47:01 -050057}
58
59TEST_P(MemtrackAidlTest, GetMemoryInvalidType) {
60 int pid = 1;
Kalesh Singh1bcbf852021-02-12 11:29:50 -050061 MemtrackType type = static_cast<MemtrackType>(-1);
Kalesh Singha851f3d2020-12-01 11:47:01 -050062 std::vector<MemtrackRecord> records;
63
64 auto status = memtrack_->getMemory(pid, type, &records);
65
66 EXPECT_EQ(status.getExceptionCode(), EX_UNSUPPORTED_OPERATION);
67}
68
69TEST_P(MemtrackAidlTest, GetMemory) {
70 int pid = 1;
Kalesh Singh1bcbf852021-02-12 11:29:50 -050071 for (MemtrackType type : ndk::enum_range<MemtrackType>()) {
72 std::vector<MemtrackRecord> records;
Kalesh Singha851f3d2020-12-01 11:47:01 -050073
Kalesh Singh1bcbf852021-02-12 11:29:50 -050074 auto status = memtrack_->getMemory(pid, type, &records);
Kalesh Singha851f3d2020-12-01 11:47:01 -050075
Kalesh Singh1bcbf852021-02-12 11:29:50 -050076 EXPECT_TRUE(status.isOk());
77 }
Kalesh Singha851f3d2020-12-01 11:47:01 -050078}
79
80TEST_P(MemtrackAidlTest, GetGpuDeviceInfo) {
81 std::vector<DeviceInfo> device_info;
82
83 auto status = memtrack_->getGpuDeviceInfo(&device_info);
84
Kalesh Singha259c592021-02-02 15:43:58 -050085 // Devices with < 5.4 kernels aren't required to provide an implementation of
Kalesh Singh11fdc972021-01-22 16:59:15 -050086 // getGpuDeviceInfo(), and can return EX_UNSUPPORTED_OPERATION
87 if (status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
Kalesh Singha259c592021-02-02 15:43:58 -050088 KernelVersion min_kernel_version = KernelVersion(5, 4, 0);
Kalesh Singh11fdc972021-01-22 16:59:15 -050089 KernelVersion kernel_version = VintfObject::GetInstance()
90 ->getRuntimeInfo(RuntimeInfo::FetchFlag::CPU_VERSION)
91 ->kernelVersion();
92 EXPECT_LT(kernel_version, min_kernel_version)
Kalesh Singh1bcbf852021-02-12 11:29:50 -050093 << "Devices with 5.4 or later kernels must implement getGpuDeviceInfo()";
Kalesh Singh11fdc972021-01-22 16:59:15 -050094 return;
95 }
96
Kalesh Singha851f3d2020-12-01 11:47:01 -050097 EXPECT_TRUE(status.isOk());
Kalesh Singh11fdc972021-01-22 16:59:15 -050098 EXPECT_FALSE(device_info.empty());
99 for (auto device : device_info) {
100 EXPECT_FALSE(device.name.empty());
101 }
Kalesh Singha851f3d2020-12-01 11:47:01 -0500102}
103
104GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemtrackAidlTest);
105INSTANTIATE_TEST_SUITE_P(PerInstance, MemtrackAidlTest,
106 testing::ValuesIn(android::getAidlHalInstanceNames(IMemtrack::descriptor)),
107 android::PrintInstanceNameToString);
108
109int main(int argc, char** argv) {
110 ::testing::InitGoogleTest(&argc, argv);
111 ABinderProcess_setThreadPoolMaxThreadCount(1);
112 ABinderProcess_startThreadPool();
113 return RUN_ALL_TESTS();
114}