blob: f31cb94e53e4f46b52428d4ec71d49240459bf7f [file] [log] [blame]
David Brazdil49f8a4d2021-03-04 09:57:33 +00001/*
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
17#include <sys/socket.h>
18#include <unistd.h>
19
20// Needs to be included after sys/socket.h
21#include <linux/vm_sockets.h>
22
23#include <iostream>
Andrew Walbrana89fc132021-03-17 17:08:36 +000024#include <optional>
David Brazdil49f8a4d2021-03-04 09:57:33 +000025
26#include "android-base/file.h"
27#include "android-base/logging.h"
28#include "android-base/parseint.h"
29#include "android-base/unique_fd.h"
Andrew Walbran3a5a9212021-05-04 17:09:08 +000030#include "android/system/virtmanager/VirtualMachineConfig.h"
David Brazdil49f8a4d2021-03-04 09:57:33 +000031#include "virt/VirtualizationTest.h"
32
33using namespace android::base;
Andrew Walbran06b5f5c2021-03-31 12:34:13 +000034using namespace android::os;
David Brazdil49f8a4d2021-03-04 09:57:33 +000035
36namespace virt {
37
38static constexpr int kGuestPort = 45678;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000039static constexpr const char kVmKernelPath[] = "/data/local/tmp/virt-test/kernel";
40static constexpr const char kVmInitrdPath[] = "/data/local/tmp/virt-test/initramfs";
41static constexpr const char kVmParams[] = "rdinit=/bin/init bin/vsock_client 2 45678 HelloWorld";
David Brazdil49f8a4d2021-03-04 09:57:33 +000042static constexpr const char kTestMessage[] = "HelloWorld";
43
44TEST_F(VirtualizationTest, TestVsock) {
45 binder::Status status;
46
47 unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
48 ASSERT_GE(server_fd, 0) << strerror(errno);
49
50 struct sockaddr_vm server_sa = (struct sockaddr_vm){
51 .svm_family = AF_VSOCK,
52 .svm_port = kGuestPort,
53 .svm_cid = VMADDR_CID_ANY,
54 };
55
56 int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
57 ASSERT_EQ(ret, 0) << strerror(errno);
58
59 LOG(INFO) << "Listening on port " << kGuestPort << "...";
60 ret = TEMP_FAILURE_RETRY(listen(server_fd, 1));
61 ASSERT_EQ(ret, 0) << strerror(errno);
62
Andrew Walbran3a5a9212021-05-04 17:09:08 +000063 VirtualMachineConfig config;
64 config.kernel = ParcelFileDescriptor(unique_fd(open(kVmKernelPath, O_RDONLY | O_CLOEXEC)));
65 config.initrd = ParcelFileDescriptor(unique_fd(open(kVmInitrdPath, O_RDONLY | O_CLOEXEC)));
66 config.params = String16(kVmParams);
67
David Brazdil49f8a4d2021-03-04 09:57:33 +000068 sp<IVirtualMachine> vm;
Andrew Walbran3a5a9212021-05-04 17:09:08 +000069 status = mVirtManager->startVm(config, std::nullopt, &vm);
David Brazdil49f8a4d2021-03-04 09:57:33 +000070 ASSERT_TRUE(status.isOk()) << "Error starting VM: " << status;
71
72 int32_t cid;
73 status = vm->getCid(&cid);
74 ASSERT_TRUE(status.isOk()) << "Error getting CID: " << status;
75 LOG(INFO) << "VM starting with CID " << cid;
76
77 LOG(INFO) << "Accepting connection...";
78 struct sockaddr_vm client_sa;
79 socklen_t client_sa_len = sizeof(client_sa);
80 unique_fd client_fd(
81 TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len)));
82 ASSERT_GE(client_fd, 0) << strerror(errno);
83 LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port;
84
85 LOG(INFO) << "Reading message from the client...";
86 std::string msg;
87 ASSERT_TRUE(ReadFdToString(client_fd, &msg));
88
89 LOG(INFO) << "Received message: " << msg;
90 ASSERT_EQ(msg, kTestMessage);
91}
92
93} // namespace virt