Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | /* |
| 18 | * Stand-alone tests for the ioctls in the vsoc driver. |
| 19 | */ |
| 20 | |
| 21 | #include "uapi/vsoc_shm.h" |
| 22 | #include <atomic> |
| 23 | #include <stdint.h> |
| 24 | #include "common/vsoc/lib/e2e_test_region_view.h" |
| 25 | #include "guest/vsoc/lib/manager_region_view.h" |
| 26 | |
| 27 | #include <android-base/logging.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | void BasicWaitForSignal(vsoc::E2EPrimaryRegionView* region, |
| 31 | uint32_t expected_start, |
| 32 | uint32_t expected_finish) { |
| 33 | ASSERT_EQ(expected_start, region->read_guest_self_register()); |
| 34 | int rval = region->wait_guest_self_register(expected_start); |
| 35 | EXPECT_LE(0, rval); |
| 36 | EXPECT_GT(5, rval); |
| 37 | EXPECT_EQ(expected_finish, region->read_guest_self_register()); |
| 38 | } |
| 39 | |
| 40 | TEST(FutexTest, BasicFutexTests) { |
| 41 | constexpr uint32_t INITIAL_SIGNAL = 0; |
| 42 | constexpr uint32_t SILENT_UPDATE_SIGNAL = 1; |
| 43 | constexpr uint32_t WAKE_SIGNAL = 2; |
| 44 | auto region = vsoc::E2EPrimaryRegionView::GetInstance(); |
| 45 | ASSERT_TRUE(region != NULL); |
| 46 | LOG(INFO) << "Regions are open"; |
| 47 | region->write_guest_self_register(INITIAL_SIGNAL); |
| 48 | std::thread waiter(BasicWaitForSignal, region, INITIAL_SIGNAL, WAKE_SIGNAL); |
| 49 | sleep(1); |
| 50 | LOG(INFO) << "Still waiting. Trying to wake wrong address"; |
| 51 | region->signal_guest_to_host_register(); |
| 52 | sleep(1); |
| 53 | LOG(INFO) << "Still waiting. Trying to wake without update"; |
| 54 | region->signal_guest_self_register(); |
| 55 | sleep(1); |
| 56 | LOG(INFO) << "Still waiting. Trying to wake without signal"; |
| 57 | region->write_guest_self_register(SILENT_UPDATE_SIGNAL); |
| 58 | sleep(1); |
| 59 | LOG(INFO) << "Still waiting. Trying to wake with signal"; |
| 60 | region->write_guest_self_register(WAKE_SIGNAL); |
| 61 | region->signal_guest_self_register(); |
| 62 | waiter.join(); |
| 63 | LOG(INFO) << "Wake worked"; |
| 64 | LOG(INFO) << "PASS: BasicPeerTests"; |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char* argv[]) { |
| 68 | android::base::InitLogging(argv); |
| 69 | testing::InitGoogleTest(&argc, argv); |
| 70 | return RUN_ALL_TESTS(); |
| 71 | } |