Skip to content

Commit

Permalink
Test socket from GitHub action
Browse files Browse the repository at this point in the history
  • Loading branch information
smuehlst committed Feb 15, 2024
1 parent b34d8dd commit f537c9c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/smoketest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion libs/circle-newlib
36 changes: 32 additions & 4 deletions samples/05-smoketest/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<struct sockaddr *>(&server_address), sizeof(server_address));

Expand All @@ -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)
Expand All @@ -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<int>(write_result));

if (close(connected_fd) < 0)
{
PErrorExit("close (connected_fd) failed");
}
#endif

if (close(fd) < 0)
{
Expand Down
11 changes: 11 additions & 0 deletions samples/05-smoketest/socket_test.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit f537c9c

Please sign in to comment.