From d6bfa5ea968c5d6adc6e38e2da431c4f0db37071 Mon Sep 17 00:00:00 2001 From: Viktor09 Date: Mon, 9 Dec 2024 01:54:03 +0200 Subject: [PATCH] chapters/io: Add checker infrastructure for lab 10 Pending to get a response Signed-off-by: Viktor09 --- .../io/ipc/drills/tasks/named-pipes/Makefile | 4 +- .../io/ipc/drills/tasks/named-pipes/README.md | 9 +++ .../named-pipes/solution/{ => src}/Makefile | 0 .../solution/{ => src}/named_pipe.c | 3 +- .../solution/{ => src}/utils/log/CPPLINT.cfg | 0 .../solution/{ => src}/utils/log/log.c | 0 .../solution/{ => src}/utils/log/log.h | 0 .../solution/{ => src}/utils/utils.h | 0 .../named-pipes/solution/tests/checker.sh | 54 +++++++++++++ .../ipc/drills/tasks/network-socket/Makefile | 4 +- .../ipc/drills/tasks/network-socket/README.md | 16 ++++ .../solution/{ => src}/Makefile | 0 .../solution/{ => src}/tcp_socket.c | 0 .../solution/{ => src}/udp_socket.c | 0 .../solution/{ => src}/utils/log/CPPLINT.cfg | 0 .../solution/{ => src}/utils/log/log.c | 0 .../solution/{ => src}/utils/log/log.h | 0 .../solution/{ => src}/utils/utils.h | 0 .../network-socket/solution/tests/checker.sh | 80 +++++++++++++++++++ .../drills/tasks/receive-challenges/Makefile | 2 +- .../solution/{ => src}/Makefile | 0 .../solution/{ => src}/receive_fifo.c | 0 .../{ => src}/receive_net_dgram_socket.c | 0 .../solution/{ => src}/receive_pipe.c | 0 .../solution/{ => src}/receive_unix_socket.c | 0 .../solution/{ => src}/send_fd_14.c | 0 .../solution/{ => src}/send_fifo.c | 0 .../{ => src}/send_net_dgram_socket.c | 0 .../solution/{ => src}/send_unix_socket.c | 0 .../solution/{ => src}/utils/log/CPPLINT.cfg | 0 .../solution/{ => src}/utils/log/log.c | 0 .../solution/{ => src}/utils/log/log.h | 0 .../solution/{ => src}/utils/utils.h | 0 .../io/ipc/drills/tasks/unix-socket/Makefile | 4 +- .../io/ipc/drills/tasks/unix-socket/README.md | 9 +++ .../unix-socket/solution/{ => src}/Makefile | 0 .../solution/{ => src}/unix_socket.c | 0 .../solution/{ => src}/utils/log/CPPLINT.cfg | 0 .../solution/{ => src}/utils/log/log.c | 0 .../solution/{ => src}/utils/log/log.h | 0 .../solution/{ => src}/utils/utils.h | 0 .../unix-socket/solution/tests/checker.sh | 53 ++++++++++++ 42 files changed, 232 insertions(+), 6 deletions(-) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/Makefile (100%) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/named_pipe.c (97%) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/utils/log/CPPLINT.cfg (100%) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/utils/log/log.c (100%) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/utils/log/log.h (100%) rename chapters/io/ipc/drills/tasks/named-pipes/solution/{ => src}/utils/utils.h (100%) create mode 100755 chapters/io/ipc/drills/tasks/named-pipes/solution/tests/checker.sh rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/Makefile (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/tcp_socket.c (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/udp_socket.c (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/utils/log/CPPLINT.cfg (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/utils/log/log.c (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/utils/log/log.h (100%) rename chapters/io/ipc/drills/tasks/network-socket/solution/{ => src}/utils/utils.h (100%) create mode 100755 chapters/io/ipc/drills/tasks/network-socket/solution/tests/checker.sh rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/Makefile (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/receive_fifo.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/receive_net_dgram_socket.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/receive_pipe.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/receive_unix_socket.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/send_fd_14.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/send_fifo.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/send_net_dgram_socket.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/send_unix_socket.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/utils/log/CPPLINT.cfg (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/utils/log/log.c (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/utils/log/log.h (100%) rename chapters/io/ipc/drills/tasks/receive-challenges/solution/{ => src}/utils/utils.h (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/Makefile (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/unix_socket.c (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/utils/log/CPPLINT.cfg (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/utils/log/log.c (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/utils/log/log.h (100%) rename chapters/io/ipc/drills/tasks/unix-socket/solution/{ => src}/utils/utils.h (100%) create mode 100755 chapters/io/ipc/drills/tasks/unix-socket/solution/tests/checker.sh diff --git a/chapters/io/ipc/drills/tasks/named-pipes/Makefile b/chapters/io/ipc/drills/tasks/named-pipes/Makefile index ff78d246ea..0e1fcebe87 100644 --- a/chapters/io/ipc/drills/tasks/named-pipes/Makefile +++ b/chapters/io/ipc/drills/tasks/named-pipes/Makefile @@ -3,7 +3,9 @@ SCRIPT = generate_skels.py skels: mkdir -p support/ - $(PYTHON) $(SCRIPT) --input ./solution --output ./support + $(PYTHON) $(SCRIPT) --input ./solution/src --output ./support/src + mkdir -p support/tests + cp solution/tests/checker.sh support/tests/checker.sh clean: rm -rf support/ diff --git a/chapters/io/ipc/drills/tasks/named-pipes/README.md b/chapters/io/ipc/drills/tasks/named-pipes/README.md index 18659cd20d..ff3f0a1f5b 100644 --- a/chapters/io/ipc/drills/tasks/named-pipes/README.md +++ b/chapters/io/ipc/drills/tasks/named-pipes/README.md @@ -15,3 +15,12 @@ Both the sender and receiver are created from the same binary: run without argum **Bonus**: Run two receivers and a single sender in different terminals. You may notice some "strange" behavior due to how named pipes manage data with multiple readers. For more on this, see [this Stack Overflow thread](https://stackoverflow.com/a/69325284). + +1. Inside the `tests/` directory, you will need to run `checker.sh`. + The output for a successful implementation should look like this: + +```bash +./checker.sh +Test for FIFO creation: PASSED +Test for send and receive: PASSED +``` diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/Makefile b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/Makefile similarity index 100% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/Makefile rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/Makefile diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/named_pipe.c b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/named_pipe.c similarity index 97% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/named_pipe.c rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/named_pipe.c index fcac6907c1..b6ac4363e8 100644 --- a/chapters/io/ipc/drills/tasks/named-pipes/solution/named_pipe.c +++ b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/named_pipe.c @@ -21,8 +21,7 @@ void create_fifo_if_needed(void) int rc = access(fifo_path, R_OK | W_OK); if (rc < 0) { - rc = unlink(fifo_path); - DIE(rc < 0, "unlink"); + unlink(fifo_path); rc = mkfifo(fifo_path, 0666); DIE(rc < 0, "mkfifo"); diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/CPPLINT.cfg b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/CPPLINT.cfg similarity index 100% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/CPPLINT.cfg rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/CPPLINT.cfg diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/log.c b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/log.c similarity index 100% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/log.c rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/log.c diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/log.h b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/log.h similarity index 100% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/utils/log/log.h rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/log/log.h diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/utils/utils.h b/chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/utils.h similarity index 100% rename from chapters/io/ipc/drills/tasks/named-pipes/solution/utils/utils.h rename to chapters/io/ipc/drills/tasks/named-pipes/solution/src/utils/utils.h diff --git a/chapters/io/ipc/drills/tasks/named-pipes/solution/tests/checker.sh b/chapters/io/ipc/drills/tasks/named-pipes/solution/tests/checker.sh new file mode 100755 index 0000000000..415ed88ae3 --- /dev/null +++ b/chapters/io/ipc/drills/tasks/named-pipes/solution/tests/checker.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +FIFO="my-fifo" +EXEC="../src/named_pipe" +INPUT="test_input.txt" +OUTPUT="test_output.txt" + +cd ../src || exit +make +cd ../tests || exit +echo + +rm -f $FIFO $INPUT $OUTPUT + +test_create_fifo() { + + $EXEC > /dev/null 2>&1 & + sleep 1 + + if [[ -p $FIFO ]]; then + echo "Test for FIFO creation: PASSED" + else + echo "Test for FIFO creation: FAILED" + fi + + pkill -f $EXEC 2>/dev/null +} + +test_send_receive() { + STRING="OS{We make the rullz!!}" + echo "$STRING" > $INPUT + + $EXEC > $OUTPUT & + sleep 1 + + $EXEC -s < $INPUT + sleep 1 + + if grep -q "$STRING" $OUTPUT; then + echo "Test for send and receive: PASSED" + else + echo "Test for send and receive: FAILED" + fi + + pkill -f $EXEC 2>/dev/null + rm -f $INPUT $OUTPUT +} + + +test_create_fifo +test_send_receive + +rm -f $FIFO diff --git a/chapters/io/ipc/drills/tasks/network-socket/Makefile b/chapters/io/ipc/drills/tasks/network-socket/Makefile index ff78d246ea..0e1fcebe87 100644 --- a/chapters/io/ipc/drills/tasks/network-socket/Makefile +++ b/chapters/io/ipc/drills/tasks/network-socket/Makefile @@ -3,7 +3,9 @@ SCRIPT = generate_skels.py skels: mkdir -p support/ - $(PYTHON) $(SCRIPT) --input ./solution --output ./support + $(PYTHON) $(SCRIPT) --input ./solution/src --output ./support/src + mkdir -p support/tests + cp solution/tests/checker.sh support/tests/checker.sh clean: rm -rf support/ diff --git a/chapters/io/ipc/drills/tasks/network-socket/README.md b/chapters/io/ipc/drills/tasks/network-socket/README.md index bce4e6633e..5eed4c374a 100644 --- a/chapters/io/ipc/drills/tasks/network-socket/README.md +++ b/chapters/io/ipc/drills/tasks/network-socket/README.md @@ -18,4 +18,20 @@ Both the sender and receiver are created from the same binary: run without argum Open `udp_socket.c` and complete the TODOs for `sender_loop()` and `receiver_loop()` functions. The workflow is similar, but `listen()`, `accept()`, and `connect()` are not required for datagram sockets. +1. Inside the `tests/` directory, you will need to run `checker.sh`. + The output for a successful implementation should look like this: + +```bash +./checker.sh +Test for TCP state: PASSED +[Sender]: OS{Hello OS enjoyers!!} +[Sender]: +Test for TCP receiving the message: PASSED + +Test for UDP state: PASSED +[Sender]: OS{Are you enjoying this lab?!} +[Sender]: +Test for UDP receiving the message: PASSED +``` + If you're having difficulties solving this exercise, go through [this reading material](../../../reading/network-sockets.md). diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/Makefile b/chapters/io/ipc/drills/tasks/network-socket/solution/src/Makefile similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/Makefile rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/Makefile diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/tcp_socket.c b/chapters/io/ipc/drills/tasks/network-socket/solution/src/tcp_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/tcp_socket.c rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/tcp_socket.c diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/udp_socket.c b/chapters/io/ipc/drills/tasks/network-socket/solution/src/udp_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/udp_socket.c rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/udp_socket.c diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/CPPLINT.cfg b/chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/CPPLINT.cfg similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/CPPLINT.cfg rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/CPPLINT.cfg diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/log.c b/chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/log.c similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/log.c rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/log.c diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/log.h b/chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/log.h similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/utils/log/log.h rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/log/log.h diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/utils/utils.h b/chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/utils.h similarity index 100% rename from chapters/io/ipc/drills/tasks/network-socket/solution/utils/utils.h rename to chapters/io/ipc/drills/tasks/network-socket/solution/src/utils/utils.h diff --git a/chapters/io/ipc/drills/tasks/network-socket/solution/tests/checker.sh b/chapters/io/ipc/drills/tasks/network-socket/solution/tests/checker.sh new file mode 100755 index 0000000000..1735c593ce --- /dev/null +++ b/chapters/io/ipc/drills/tasks/network-socket/solution/tests/checker.sh @@ -0,0 +1,80 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +TCP="../src/tcp_socket" +UDP="../src/udp_socket" + +TCP_LOG="tcp_server.log" +UDP_LOG="udp_server.log" + +cd ../src || exit +make +cd ../tests || exit +echo + +test_tcp_all() { + + $TCP > "$TCP_LOG" 2>&1 & + TCP_PID=$! + sleep 1 + + IP="127.0.0.1" + PORT="5000" + + if netstat -tuln | grep -q "$IP:$PORT"; then + echo "Test for TCP state: PASSED" + else + echo "Test for TCP state: FAILED" + fi + + STRING="OS{Hello OS enjoyers!!}" + echo "$STRING" | $TCP -s + sleep 1 + + if grep -q "\[Receiver\]: $STRING" "$TCP_LOG"; then + echo "Test for TCP receiving the message: PASSED" + else + echo "Test for TCP receiving the message: FAILED" + fi + + kill "$TCP_PID" 2>/dev/null + wait "$TCP_PID" 2>/dev/null + rm -f "$TCP_LOG" + echo +} + +test_udp_all() { + + $UDP > "$UDP_LOG" 2>&1 & + UDP_PID=$! + sleep 1 + + IP="127.0.0.1" + PORT="5000" + + if netstat -tuln | grep -q "$IP:$PORT"; then + echo "Test for UDP state: PASSED" + else + echo "Test for UDP state: FAILED" + fi + + STRING="OS{Are you enjoying this lab?!}" + echo "$STRING" | $UDP -s + sleep 1 + + if grep -q "\[Receiver\]: $STRING" "$UDP_LOG"; then + echo "Test for UDP receiving the message: PASSED" + else + echo "Test for UDP receiving the message: FAILED" + fi + + kill "$UDP_PID" 2>/dev/null + wait "$UDP_PID" 2>/dev/null + rm -f "$UDP_LOG" + echo +} + +test_tcp_all +test_udp_all + +rm -f "$INPUT" "$OUTPUT" diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/Makefile b/chapters/io/ipc/drills/tasks/receive-challenges/Makefile index ff78d246ea..9a1d5f4058 100644 --- a/chapters/io/ipc/drills/tasks/receive-challenges/Makefile +++ b/chapters/io/ipc/drills/tasks/receive-challenges/Makefile @@ -3,7 +3,7 @@ SCRIPT = generate_skels.py skels: mkdir -p support/ - $(PYTHON) $(SCRIPT) --input ./solution --output ./support + $(PYTHON) $(SCRIPT) --input ./solution/src --output ./support/src clean: rm -rf support/ diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/Makefile b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/Makefile similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/Makefile rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/Makefile diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_fifo.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_fifo.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_fifo.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_fifo.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_net_dgram_socket.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_net_dgram_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_net_dgram_socket.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_net_dgram_socket.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_pipe.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_pipe.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_pipe.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_pipe.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_unix_socket.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_unix_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/receive_unix_socket.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/receive_unix_socket.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/send_fd_14.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_fd_14.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/send_fd_14.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_fd_14.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/send_fifo.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_fifo.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/send_fifo.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_fifo.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/send_net_dgram_socket.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_net_dgram_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/send_net_dgram_socket.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_net_dgram_socket.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/send_unix_socket.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_unix_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/send_unix_socket.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/send_unix_socket.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/CPPLINT.cfg b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/CPPLINT.cfg similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/CPPLINT.cfg rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/CPPLINT.cfg diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/log.c b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/log.c similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/log.c rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/log.c diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/log.h b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/log.h similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/log/log.h rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/log/log.h diff --git a/chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/utils.h b/chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/utils.h similarity index 100% rename from chapters/io/ipc/drills/tasks/receive-challenges/solution/utils/utils.h rename to chapters/io/ipc/drills/tasks/receive-challenges/solution/src/utils/utils.h diff --git a/chapters/io/ipc/drills/tasks/unix-socket/Makefile b/chapters/io/ipc/drills/tasks/unix-socket/Makefile index ff78d246ea..0e1fcebe87 100644 --- a/chapters/io/ipc/drills/tasks/unix-socket/Makefile +++ b/chapters/io/ipc/drills/tasks/unix-socket/Makefile @@ -3,7 +3,9 @@ SCRIPT = generate_skels.py skels: mkdir -p support/ - $(PYTHON) $(SCRIPT) --input ./solution --output ./support + $(PYTHON) $(SCRIPT) --input ./solution/src --output ./support/src + mkdir -p support/tests + cp solution/tests/checker.sh support/tests/checker.sh clean: rm -rf support/ diff --git a/chapters/io/ipc/drills/tasks/unix-socket/README.md b/chapters/io/ipc/drills/tasks/unix-socket/README.md index c7b67d6b52..0b45e16e62 100644 --- a/chapters/io/ipc/drills/tasks/unix-socket/README.md +++ b/chapters/io/ipc/drills/tasks/unix-socket/README.md @@ -14,4 +14,13 @@ Both the sender and receiver are created from the same binary: run without argum Instead of connecting, you will **listen** for and **accept** incoming connections. When `accept()` receives a connection request, it will return a new socket file descriptor that you can use to receive messages via `recv()`. +1. Inside the `tests/` directory, you will need to run `checker.sh`. + The output for a successful implementation should look like this: + +```bash +./checker.sh +Test for socket creation: PASSED +Test for send and receive: PASSED +``` + If you're having difficulties solving this exercise, go through [this reading material](../../../reading/unix-sockets.md). diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/Makefile b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/Makefile similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/Makefile rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/Makefile diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/unix_socket.c b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/unix_socket.c similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/unix_socket.c rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/unix_socket.c diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/CPPLINT.cfg b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/CPPLINT.cfg similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/CPPLINT.cfg rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/CPPLINT.cfg diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/log.c b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/log.c similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/log.c rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/log.c diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/log.h b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/log.h similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/utils/log/log.h rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/log/log.h diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/utils/utils.h b/chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/utils.h similarity index 100% rename from chapters/io/ipc/drills/tasks/unix-socket/solution/utils/utils.h rename to chapters/io/ipc/drills/tasks/unix-socket/solution/src/utils/utils.h diff --git a/chapters/io/ipc/drills/tasks/unix-socket/solution/tests/checker.sh b/chapters/io/ipc/drills/tasks/unix-socket/solution/tests/checker.sh new file mode 100755 index 0000000000..58f7f7bcaa --- /dev/null +++ b/chapters/io/ipc/drills/tasks/unix-socket/solution/tests/checker.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +SOCKET="my-socket" +EXEC="../src/unix_socket" +INPUT="test_input.txt" +OUTPUT="test_output.txt" + +cd ../src || exit +make +cd ../tests || exit +echo + +rm -f $SOCKET $INPUT $OUTPUT + +test_create_socket() { + + $EXEC > /dev/null 2>&1 & + sleep 1 + + if [[ -e $SOCKET ]]; then + echo "Test for socket creation: PASSED" + else + echo "Test for socket creation: FAILED" + fi + + pkill -f $EXEC 2>/dev/null +} + +test_send_receive() { + STRING="OS{Welcome to the hood!}" + echo "$STRING" > $INPUT + + $EXEC > $OUTPUT 2>&1 & + sleep 1 + + $EXEC -s < $INPUT > /dev/null 2>&1 + sleep 1 + + if grep -q "$STRING" $OUTPUT; then + echo "Test for send and receive: PASSED" + else + echo "Test for send and receive: FAILED" + fi + + pkill -f $EXEC 2>/dev/null + rm -f $INPUT $OUTPUT +} + +test_create_socket +test_send_receive + +rm -f $SOCKET