Vsock end-to-end integration test
Add VsockTest host-side test that runs a vsock server in Android and
spawns a VM with a client that connects to it. The client sends a
message to the server. The host-side driver validates that the message
received by the server matches the message given to the client via
kernel command-line arguments.
Bug: 168589743
Test: atest VirtualizationHostTestCases
Change-Id: Id6d1f1c5ff10066a73606ee5b14387ba4474a3f8
diff --git a/tests/hostside/native/vsock/Android.bp b/tests/hostside/native/vsock/Android.bp
new file mode 100644
index 0000000..cbee98d
--- /dev/null
+++ b/tests/hostside/native/vsock/Android.bp
@@ -0,0 +1,35 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_test {
+ name: "virt_hostside_tests_vsock_server",
+ srcs: ["server.cc"],
+ static_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_executable: true,
+ test_suites: ["device-tests"],
+}
+
+cc_binary {
+ name: "virt_hostside_tests_vsock_client",
+ srcs: ["client.cc"],
+ static_libs: [
+ "libbase",
+ "liblog",
+ ],
+ static_executable: true,
+ installable: false,
+}
diff --git a/tests/hostside/native/vsock/client.cc b/tests/hostside/native/vsock/client.cc
new file mode 100644
index 0000000..7a72e11
--- /dev/null
+++ b/tests/hostside/native/vsock/client.cc
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/socket.h>
+
+// Needs to be included after sys/socket.h
+#include <linux/vm_sockets.h>
+
+#include <iostream>
+
+#include "android-base/file.h"
+#include "android-base/logging.h"
+#include "android-base/parseint.h"
+#include "android-base/unique_fd.h"
+
+using namespace android::base;
+
+int main(int argc, const char *argv[]) {
+ SetLogger(StderrLogger);
+
+ unsigned int cid, port;
+ if (argc != 4 || !ParseUint(argv[1], &cid) || !ParseUint(argv[2], &port)) {
+ LOG(ERROR) << "Usage: " << argv[0] << " <cid> <port> <msg>";
+ return EXIT_FAILURE;
+ }
+ std::string msg(argv[3]);
+
+ unique_fd fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
+ if (fd < 0) {
+ PLOG(ERROR) << "socket";
+ return EXIT_FAILURE;
+ }
+
+ struct sockaddr_vm sa = (struct sockaddr_vm){
+ .svm_family = AF_VSOCK,
+ .svm_port = port,
+ .svm_cid = cid,
+ };
+
+ LOG(INFO) << "Connecting to CID " << cid << " on port " << port << "...";
+ int ret = TEMP_FAILURE_RETRY(connect(fd, (struct sockaddr *)&sa, sizeof(sa)));
+ if (ret < 0) {
+ PLOG(ERROR) << "connect";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Sending message to server...";
+ if (!WriteStringToFd(msg, fd)) {
+ PLOG(ERROR) << "WriteStringToFd";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Exiting...";
+ return EXIT_SUCCESS;
+}
diff --git a/tests/hostside/native/vsock/server.cc b/tests/hostside/native/vsock/server.cc
new file mode 100644
index 0000000..d4a99d2
--- /dev/null
+++ b/tests/hostside/native/vsock/server.cc
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/socket.h>
+#include <unistd.h>
+
+// Needs to be included after sys/socket.h
+#include <linux/vm_sockets.h>
+
+#include <iostream>
+
+#include "android-base/file.h"
+#include "android-base/logging.h"
+#include "android-base/parseint.h"
+#include "android-base/unique_fd.h"
+
+using namespace android::base;
+
+int main(int argc, const char *argv[]) {
+ unsigned int port;
+ if (argc != 2 || !ParseUint(argv[1], &port)) {
+ LOG(ERROR) << "Usage: " << argv[0] << " <port>";
+ return EXIT_FAILURE;
+ }
+
+ unique_fd server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)));
+ if (server_fd < 0) {
+ PLOG(ERROR) << "socket";
+ return EXIT_FAILURE;
+ }
+
+ struct sockaddr_vm server_sa = (struct sockaddr_vm){
+ .svm_family = AF_VSOCK,
+ .svm_port = port,
+ .svm_cid = VMADDR_CID_ANY,
+ };
+
+ int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
+ if (ret != 0) {
+ PLOG(ERROR) << "bind";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Listening on port " << port << "...";
+ ret = TEMP_FAILURE_RETRY(listen(server_fd, 1));
+ if (ret != 0) {
+ PLOG(ERROR) << "listen";
+ return EXIT_FAILURE;
+ }
+
+ LOG(INFO) << "Accepting connection...";
+ struct sockaddr_vm client_sa;
+ socklen_t client_sa_len = sizeof(client_sa);
+ unique_fd client_fd(
+ TEMP_FAILURE_RETRY(accept(server_fd, (struct sockaddr *)&client_sa, &client_sa_len)));
+ if (client_fd < 0) {
+ PLOG(ERROR) << "accept";
+ return EXIT_FAILURE;
+ }
+ LOG(INFO) << "Connection from CID " << client_sa.svm_cid << " on port " << client_sa.svm_port;
+
+ LOG(INFO) << "Reading message from the client...";
+ std::string msg;
+ if (!ReadFdToString(client_fd, &msg)) {
+ PLOG(ERROR) << "ReadFdToString";
+ return EXIT_FAILURE;
+ }
+
+ // Print the received message to stdout.
+ std::cout << msg << std::endl;
+
+ LOG(INFO) << "Exiting...";
+ return EXIT_SUCCESS;
+}