Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [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 | /* |
| 18 | * End-to-end test to ensure that mapping of vsoc regions works on the guest. |
| 19 | */ |
| 20 | |
| 21 | #include "guest/vsoc/lib/e2e_test_common.h" |
| 22 | #include "common/vsoc/lib/e2e_test_region_view.h" |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | template <typename View> |
| 28 | void DeathTestView() { |
| 29 | disable_tombstones(); |
| 30 | // View::GetInstance should never return. |
| 31 | EXPECT_FALSE(!!View::GetInstance()); |
| 32 | } |
| 33 | |
| 34 | // Here is a summary of the two regions interrupt and write test: |
| 35 | // 1. Write our strings to the first region |
| 36 | // 2. Ensure that our peer hasn't signalled the second region. That would |
| 37 | // indicate that it didn't wait for our interrupt. |
| 38 | // 3. Send the interrupt on the first region |
| 39 | // 4. Wait for our peer's interrupt on the first region |
| 40 | // 5. Confirm that we can see our peer's writes in the first region |
| 41 | // 6. Initialize our strings in the second region |
| 42 | // 7. Send an interrupt on the second region to our peer |
| 43 | // 8. Wait for our peer's interrupt on the second region |
| 44 | // 9. Confirm that we can see our peer's writes in the second region |
| 45 | // 10. Repeat the process for signaling. |
| 46 | // 11. Confirm that no interrupt is pending in the first region |
| 47 | // 12. Confirm that no interrupt is pending in the second region |
| 48 | |
| 49 | template <typename View> |
| 50 | void SetGuestStrings(View* in) { |
| 51 | size_t num_data = in->string_size(); |
| 52 | EXPECT_LE(2U, num_data); |
| 53 | for (size_t i = 0; i < num_data; ++i) { |
| 54 | EXPECT_TRUE(!in->guest_string(i)[0] || |
| 55 | !strcmp(in->guest_string(i), View::Layout::guest_pattern)); |
| 56 | in->set_guest_string(i, View::Layout::guest_pattern); |
| 57 | EXPECT_STREQ(in->guest_string(i), View::Layout::guest_pattern); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | template <typename View> |
| 62 | void CheckPeerStrings(View* in) { |
| 63 | size_t num_data = in->string_size(); |
| 64 | EXPECT_LE(2U, num_data); |
| 65 | for (size_t i = 0; i < num_data; ++i) { |
| 66 | EXPECT_STREQ(View::Layout::host_pattern, in->host_string(i)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | TEST(RegionTest, BasicPeerTests) { |
| 71 | auto primary = vsoc::E2EPrimaryRegionView::GetInstance(); |
| 72 | auto secondary = vsoc::E2ESecondaryRegionView::GetInstance(); |
| 73 | ASSERT_TRUE(!!primary); |
| 74 | ASSERT_TRUE(!!secondary); |
| 75 | LOG(INFO) << "Regions are open"; |
| 76 | SetGuestStrings(primary); |
| 77 | LOG(INFO) << "Primary guest strings are set"; |
| 78 | EXPECT_FALSE(secondary->HasIncomingInterrupt()); |
| 79 | LOG(INFO) << "Verified no early second interrupt"; |
| 80 | EXPECT_TRUE(primary->MaybeInterruptPeer()); |
| 81 | LOG(INFO) << "Interrupt sent. Waiting for first interrupt from peer"; |
| 82 | primary->WaitForInterrupt(); |
| 83 | LOG(INFO) << "First interrupt received"; |
| 84 | CheckPeerStrings(primary); |
| 85 | LOG(INFO) << "Verified peer's primary strings"; |
| 86 | SetGuestStrings(secondary); |
| 87 | LOG(INFO) << "Secondary guest strings are set"; |
| 88 | EXPECT_TRUE(secondary->MaybeInterruptPeer()); |
| 89 | LOG(INFO) << "Second interrupt sent"; |
| 90 | secondary->WaitForInterrupt(); |
| 91 | LOG(INFO) << "Second interrupt received"; |
| 92 | CheckPeerStrings(secondary); |
| 93 | LOG(INFO) << "Verified peer's secondary strings"; |
| 94 | |
| 95 | // Test signals |
| 96 | EXPECT_FALSE(secondary->HasIncomingInterrupt()); |
| 97 | LOG(INFO) << "Verified no early second signal"; |
| 98 | primary->SendSignal(vsoc::layout::Sides::Peer, |
| 99 | &primary->data()->guest_to_host_signal); |
| 100 | LOG(INFO) << "Signal sent. Waiting for first signal from peer"; |
| 101 | primary->WaitForInterrupt(); |
| 102 | int count = 0; // counts the number of signals received. |
| 103 | primary->ProcessSignalsFromPeer( |
| 104 | [&primary, &count](uint32_t offset) { |
| 105 | ++count; |
| 106 | EXPECT_TRUE(offset == primary->host_to_guest_signal_offset()); |
| 107 | }); |
| 108 | EXPECT_TRUE(count == 1); |
| 109 | LOG(INFO) << "Signal received on primary region"; |
| 110 | secondary->SendSignal(vsoc::layout::Sides::Peer, |
| 111 | &secondary->data()->guest_to_host_signal); |
| 112 | LOG(INFO) << "Signal sent. Waiting for second signal from peer"; |
| 113 | secondary->WaitForInterrupt(); |
| 114 | count = 0; |
| 115 | secondary->ProcessSignalsFromPeer( |
| 116 | [&secondary, &count](uint32_t offset) { |
| 117 | ++count; |
| 118 | EXPECT_TRUE(offset == secondary->host_to_guest_signal_offset()); |
| 119 | }); |
| 120 | EXPECT_TRUE(count == 1); |
| 121 | LOG(INFO) << "Signal received on secondary region"; |
| 122 | |
| 123 | EXPECT_FALSE(primary->HasIncomingInterrupt()); |
| 124 | EXPECT_FALSE(secondary->HasIncomingInterrupt()); |
| 125 | LOG(INFO) << "PASS: BasicPeerTests"; |
| 126 | } |
| 127 | |
| 128 | TEST(RegionTest, MissingRegionDeathTest) { |
| 129 | // EXPECT_DEATH creates a child for the test, so we do it out here. |
| 130 | // DeathTestGuestRegion will actually do the deadly call after ensuring |
| 131 | // that we don't create an unwanted tombstone. |
| 132 | EXPECT_EXIT(DeathTestView<vsoc::E2EUnfindableRegionView>(), |
| 133 | testing::ExitedWithCode(2), |
| 134 | ".*" DEATH_TEST_MESSAGE ".*"); |
| 135 | } |
| 136 | |
| 137 | int main(int argc, char** argv) { |
| 138 | if (argc == 2) { |
| 139 | // gtest tries to leave temporary files in the current directory, so make the |
| 140 | // current directory something that we control. |
| 141 | if (chdir(argv[1]) != 0) { |
| 142 | abort(); |
| 143 | } |
| 144 | } |
| 145 | android::base::InitLogging(argv); |
| 146 | testing::InitGoogleTest(&argc, argv); |
| 147 | int rval = RUN_ALL_TESTS(); |
| 148 | if (!rval) { |
| 149 | auto region = vsoc::E2EPrimaryRegionView::GetInstance(); |
| 150 | region->guest_status(vsoc::layout::e2e_test::E2E_MEMORY_FILLED); |
| 151 | LOG(INFO) << "stage_1_guest_region_e2e_tests PASSED"; |
| 152 | } else { |
| 153 | LOG(ERROR) << "stage_1_guest_region_e2e_tests FAILED"; |
| 154 | } |
| 155 | return rval; |
| 156 | } |