[Spice-devel] [PATCH spice-streaming-agent v3 1/5] Add a unit test for the stream port

Lukáš Hrázký lhrazky at redhat.com
Fri May 18 12:32:05 UTC 2018


Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
---
 src/unittests/.gitignore           |  1 +
 src/unittests/Makefile.am          |  8 ++++++
 src/unittests/test-stream-port.cpp | 55 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 src/unittests/test-stream-port.cpp

diff --git a/src/unittests/.gitignore b/src/unittests/.gitignore
index 22f1335..ef9e31b 100644
--- a/src/unittests/.gitignore
+++ b/src/unittests/.gitignore
@@ -2,4 +2,5 @@
 /test-*.log
 /test-*.trs
 /test-mjpeg-fallback
+/test-stream-port
 /test-suite.log
diff --git a/src/unittests/Makefile.am b/src/unittests/Makefile.am
index 9047b80..c9de603 100644
--- a/src/unittests/Makefile.am
+++ b/src/unittests/Makefile.am
@@ -20,11 +20,13 @@ AM_CXXFLAGS = \
 check_PROGRAMS = \
 	hexdump \
 	test-mjpeg-fallback \
+	test-stream-port \
 	$(NULL)
 
 TESTS = \
 	test-hexdump.sh \
 	test-mjpeg-fallback \
+	test-stream-port \
 	$(NULL)
 
 noinst_PROGRAMS = \
@@ -50,6 +52,12 @@ test_mjpeg_fallback_LDADD = \
 	$(JPEG_LIBS) \
 	$(NULL)
 
+test_stream_port_SOURCES = \
+	test-stream-port.cpp \
+	../stream-port.cpp \
+	../error.cpp \
+	$(NULL)
+
 EXTRA_DIST = \
 	test-hexdump.sh \
 	hexdump1.in \
diff --git a/src/unittests/test-stream-port.cpp b/src/unittests/test-stream-port.cpp
new file mode 100644
index 0000000..688fa2b
--- /dev/null
+++ b/src/unittests/test-stream-port.cpp
@@ -0,0 +1,55 @@
+/* The unit test for the low-level stream port IO.
+ *
+ * \copyright
+ * Copyright 2018 Red Hat Inc. All rights reserved.
+ */
+
+#define CATCH_CONFIG_MAIN
+#include <catch/catch.hpp>
+#include <sys/socket.h>
+
+#include "stream-port.hpp"
+
+
+namespace ssa = spice::streaming_agent;
+
+/*
+ * Note the semantics of a socketpair may be different from the virtio port
+ * that is actually used for the real interface.
+ */
+SCENARIO("test basic IO on the stream port", "[port][io]") {
+    GIVEN("An open port (socketpair)") {
+        int fd[2];
+        const char *src_buf = "brekeke";
+        const size_t src_size = strlen(src_buf);
+
+        socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
+
+        WHEN("reading data in one go") {
+            CHECK(write(fd[0], src_buf, src_size) == src_size);
+            char buf[10];
+            ssa::read_all(fd[1], buf, src_size);
+            CHECK(std::string(buf, src_size) == src_buf);
+        }
+
+        WHEN("reading data in two steps") {
+            CHECK(write(fd[0], src_buf, src_size) == src_size);
+            char buf[10];
+            ssa::read_all(fd[1], buf, 3);
+            CHECK(std::string(buf, 3) == "bre");
+            ssa::read_all(fd[1], buf, 4);
+            CHECK(std::string(buf, 4) == "keke");
+        }
+
+        WHEN("writing data") {
+            ssa::write_all(fd[1], src_buf, src_size);
+            char buf[10];
+            CHECK(read(fd[0], buf, src_size) == src_size);
+            CHECK(std::string(buf, src_size) == src_buf);
+        }
+
+        // clean up the descriptors in case they are still open
+        close(fd[0]);
+        close(fd[1]);
+    }
+}
-- 
2.16.2



More information about the Spice-devel mailing list