Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapters/io: Add checker infrastructure for lab 10 #147

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion chapters/io/ipc/drills/tasks/named-pipes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
9 changes: 9 additions & 0 deletions chapters/io/ipc/drills/tasks/named-pipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
54 changes: 54 additions & 0 deletions chapters/io/ipc/drills/tasks/named-pipes/solution/tests/checker.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion chapters/io/ipc/drills/tasks/network-socket/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
16 changes: 16 additions & 0 deletions chapters/io/ipc/drills/tasks/network-socket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion chapters/io/ipc/drills/tasks/receive-challenges/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
4 changes: 3 additions & 1 deletion chapters/io/ipc/drills/tasks/unix-socket/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
9 changes: 9 additions & 0 deletions chapters/io/ipc/drills/tasks/unix-socket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
53 changes: 53 additions & 0 deletions chapters/io/ipc/drills/tasks/unix-socket/solution/tests/checker.sh
Original file line number Diff line number Diff line change
@@ -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
Loading