Felipe Leme | 4b89d22 | 2017-05-26 14:55:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #define LOG_TAG "dumpstate_hidl_hal_test" |
| 18 | |
| 19 | #include <android/hardware/dumpstate/1.0/IDumpstateDevice.h> |
| 20 | #include <cutils/native_handle.h> |
| 21 | #include <log/log.h> |
| 22 | |
| 23 | #include <VtsHalHidlTargetTestBase.h> |
| 24 | |
| 25 | using ::android::hardware::dumpstate::V1_0::IDumpstateDevice; |
| 26 | using ::android::hardware::Return; |
| 27 | using ::android::sp; |
| 28 | |
| 29 | class DumpstateHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 30 | public: |
| 31 | virtual void SetUp() override { |
| 32 | dumpstate = ::testing::VtsHalHidlTargetTestBase::getService<IDumpstateDevice>(); |
| 33 | ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance"; |
| 34 | } |
| 35 | |
| 36 | sp<IDumpstateDevice> dumpstate; |
| 37 | }; |
| 38 | |
| 39 | // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer. |
| 40 | TEST_F(DumpstateHidlTest, TestNullHandle) { |
| 41 | Return<void> status = dumpstate->dumpstateBoard(nullptr); |
| 42 | |
| 43 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 44 | } |
| 45 | |
| 46 | // Negative test: make sure dumpstateBoard() ignores a handle with no FD. |
| 47 | TEST_F(DumpstateHidlTest, TestHandleWithNoFd) { |
| 48 | native_handle_t* handle = native_handle_create(0, 0); |
| 49 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 50 | |
| 51 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 52 | |
| 53 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 54 | |
| 55 | native_handle_close(handle); |
| 56 | native_handle_delete(handle); |
| 57 | } |
| 58 | |
| 59 | // Positive test: make sure dumpstateBoard() writes something to the FD. |
| 60 | TEST_F(DumpstateHidlTest, TestOk) { |
| 61 | FILE* file = tmpfile(); |
| 62 | ASSERT_NE(nullptr, file) << "Could not create temp file: " << strerror(errno); |
| 63 | |
| 64 | native_handle_t* handle = native_handle_create(1, 0); |
| 65 | ASSERT_NE(handle, nullptr) << "Could not create native_handle"; |
| 66 | handle->data[0] = fileno(file); |
| 67 | |
| 68 | Return<void> status = dumpstate->dumpstateBoard(handle); |
| 69 | ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description(); |
| 70 | |
| 71 | // Check that at least one byte was written |
| 72 | rewind(file); // can not fail |
| 73 | char buff; |
| 74 | int read = fread(&buff, sizeof(buff), 1, file); |
| 75 | ASSERT_EQ(1, read) << "dumped nothing"; |
| 76 | |
| 77 | EXPECT_EQ(0, fclose(file)) << errno; |
| 78 | |
| 79 | native_handle_close(handle); |
| 80 | native_handle_delete(handle); |
| 81 | } |
| 82 | |
| 83 | int main(int argc, char** argv) { |
| 84 | ::testing::InitGoogleTest(&argc, argv); |
| 85 | int status = RUN_ALL_TESTS(); |
| 86 | ALOGI("Test result = %d", status); |
| 87 | return status; |
| 88 | } |