Kalesh Singh | 3265ebe | 2021-03-27 03:31:54 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 <aidl/android/hardware/memtrack/DeviceInfo.h> |
| 18 | #include <aidl/android/hardware/memtrack/IMemtrack.h> |
| 19 | #include <aidl/android/hardware/memtrack/MemtrackRecord.h> |
| 20 | #include <aidl/android/hardware/memtrack/MemtrackType.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <unistd.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 | |
| 31 | class MemtrackProxyTest : public ::testing::Test { |
| 32 | public: |
| 33 | virtual void SetUp() override { |
| 34 | const char* kMemtrackProxyService = "memtrack.proxy"; |
| 35 | auto memtrackProxyBinder = |
| 36 | ndk::SpAIBinder(AServiceManager_waitForService(kMemtrackProxyService)); |
| 37 | memtrack_proxy_ = IMemtrack::fromBinder(memtrackProxyBinder); |
| 38 | ASSERT_NE(memtrack_proxy_, nullptr); |
| 39 | } |
| 40 | |
| 41 | std::shared_ptr<IMemtrack> memtrack_proxy_; |
| 42 | }; |
| 43 | |
| 44 | TEST_F(MemtrackProxyTest, GetMemoryForInvalidPid) { |
| 45 | int pid = -1; |
| 46 | |
| 47 | for (MemtrackType type : ndk::enum_range<MemtrackType>()) { |
| 48 | std::vector<MemtrackRecord> records; |
| 49 | |
| 50 | auto status = memtrack_proxy_->getMemory(pid, type, &records); |
| 51 | |
| 52 | EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | TEST_F(MemtrackProxyTest, GetMemoryForCallingPid) { |
| 57 | int pid = getpid(); |
| 58 | |
| 59 | for (MemtrackType type : ndk::enum_range<MemtrackType>()) { |
| 60 | std::vector<MemtrackRecord> records; |
| 61 | |
| 62 | auto status = memtrack_proxy_->getMemory(pid, type, &records); |
| 63 | |
| 64 | EXPECT_TRUE(status.isOk()); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | TEST_F(MemtrackProxyTest, GetMemoryForOtherPid) { |
| 69 | int pid = 1; |
| 70 | |
| 71 | for (MemtrackType type : ndk::enum_range<MemtrackType>()) { |
| 72 | std::vector<MemtrackRecord> records; |
| 73 | |
| 74 | auto status = memtrack_proxy_->getMemory(pid, type, &records); |
| 75 | |
| 76 | // Test is run as root |
| 77 | EXPECT_TRUE(status.isOk()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | int main(int argc, char** argv) { |
| 82 | ::testing::InitGoogleTest(&argc, argv); |
| 83 | return RUN_ALL_TESTS(); |
| 84 | } |