From f537c9cbfe5f471b207551e796387751064cc7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Thu, 15 Feb 2024 20:33:56 +0100 Subject: [PATCH] Test socket from GitHub action --- .github/workflows/smoketest.yaml | 4 +++- libs/circle-newlib | 2 +- samples/05-smoketest/kernel.cpp | 36 +++++++++++++++++++++++++---- samples/05-smoketest/socket_test.sh | 11 +++++++++ 4 files changed, 47 insertions(+), 6 deletions(-) create mode 100755 samples/05-smoketest/socket_test.sh diff --git a/.github/workflows/smoketest.yaml b/.github/workflows/smoketest.yaml index 7b3ed7f..d0f4724 100644 --- a/.github/workflows/smoketest.yaml +++ b/.github/workflows/smoketest.yaml @@ -111,6 +111,8 @@ jobs: # Extract blank SD card image rm -f sdcard.img unzip sdcard.img.zip + # Script that interacts with the socket in the smoke test + ./socket_test.sh & # Guard against endless loop in QEMU run (120 CPU secs) ulimit -t 120 - $CFG_QEMU_BIN -M ${{ matrix.config.qemu-machine }} $CFG_QEMU_OPT $CFG_QEMU_IMG -display none -nographic -semihosting -sd sdcard.img + $CFG_QEMU_BIN -M ${{ matrix.config.qemu-machine }} $CFG_QEMU_OPT $CFG_QEMU_IMG -display none -nographic -semihosting -sd sdcard.img -netdev user,id=net0,hostfwd=tcp::5000-:5000 -device usb-net,netdev=net0 diff --git a/libs/circle-newlib b/libs/circle-newlib index 771185e..537abfc 160000 --- a/libs/circle-newlib +++ b/libs/circle-newlib @@ -1 +1 @@ -Subproject commit 771185eac66ed9361c80b65d72143a38b70db08b +Subproject commit 537abfca2e0c8899b53ed5d70dfd9ee06fdd61db diff --git a/samples/05-smoketest/kernel.cpp b/samples/05-smoketest/kernel.cpp index a27e817..6e3c12a 100644 --- a/samples/05-smoketest/kernel.cpp +++ b/samples/05-smoketest/kernel.cpp @@ -839,9 +839,10 @@ void CKernel::SocketTest(void) struct sockaddr_in server_address; + /* The sin_port and sin_addr members shall be in network byte order. */ server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); - server_address.sin_port = htons(1234); + server_address.sin_port = htons(5000); int const bind_result = bind(fd, reinterpret_cast(&server_address), sizeof(server_address)); @@ -861,8 +862,6 @@ void CKernel::SocketTest(void) Report("listen() on file descriptor %d succeeded", fd); -#if 0 - // TODO this currently hangs even when a connection attempt is made. int connected_fd = accept(fd, nullptr, nullptr); if (connected_fd == -1) @@ -872,11 +871,40 @@ void CKernel::SocketTest(void) Report("accept() on file descriptor %d returned %d", fd, connected_fd); + char buffer[100]; + size_t const bufsiz = sizeof(buffer); + auto read_result = read(connected_fd, buffer, bufsiz); + if (read_result < 0) + { + PErrorExit("read (connected_fd) failed"); + } + + // Terminate string, strip newline if present + if (read_result == bufsiz) + { + read_result -= 1; + } + if (read_result > 0 && buffer[read_result - 1] == '\n') + { + read_result -= 1; + } + buffer[read_result] = 0; + + Report("Read from socket succeeded: \"%s\"\n", buffer); + + strcpy(buffer, "Response from server"); + auto const write_result = write(connected_fd, buffer, strlen(buffer)); + if (write_result < 0) + { + PErrorExit("write (connected_fd) failed"); + } + + Report("Write of %d bytes to socket succeeded\n", static_cast(write_result)); + if (close(connected_fd) < 0) { PErrorExit("close (connected_fd) failed"); } -#endif if (close(fd) < 0) { diff --git a/samples/05-smoketest/socket_test.sh b/samples/05-smoketest/socket_test.sh new file mode 100755 index 0000000..f7eb0c6 --- /dev/null +++ b/samples/05-smoketest/socket_test.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +echo "Waiting for QEMU to open socket 5000..." + +while ! nc -z localhost 5000; do + sleep 0.1 # wait for 1/10 of the second before check again +done + +echo "Socket test" | nc -v localhost 5000 + +echo "Socket test completed" \ No newline at end of file