From c52712471fb92472b6450d832de81a0f99dc0dba Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Sun, 7 Jul 2024 20:05:32 -0700 Subject: [PATCH] manual testing --- .gitea/workflows/jobs.yaml | 8 ++------ .gitignore | 1 + Makefile | 5 ++++- src/network.c | 28 +++++++++++++++++++++------- tests/netmanual.c | 15 +++++++++++++++ 5 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/netmanual.c diff --git a/.gitea/workflows/jobs.yaml b/.gitea/workflows/jobs.yaml index 5cf1842..b01099a 100644 --- a/.gitea/workflows/jobs.yaml +++ b/.gitea/workflows/jobs.yaml @@ -14,15 +14,11 @@ jobs: - name: Install dependencies run: | sudo apt-get update - sudo apt-get install -y libbsd-dev cmake build-essential + sudo apt-get install -y libbsd-dev build-essential - name: Build and test run: | - mkdir build - cd build - cmake .. - make - ./tests + make tests docs: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 04ab71d..221cf55 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ test tcptest testrunner .idea +netmanual diff --git a/Makefile b/Makefile index f6b6d93..4c62e57 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY : clean tests all +.PHONY : clean tests all manual CFLAGS = -std=c99 -Iinclude -pedantic WARNINGS= -Wall -Wextra @@ -30,6 +30,7 @@ clean: rm -f $(TARGET) rm -f testrunner rm -f tcptest + rm -f netmanual rm -f compile_commands.json tests: @@ -37,3 +38,5 @@ tests: cc $(CFLAGS) -o tcptest tests/tcptest.c src/*.c ./run_tests.sh +manual: + cc $(CFLAGS) -o netmanual tests/netmanual.c src/*.c diff --git a/src/network.c b/src/network.c index 40c71e2..637ed17 100644 --- a/src/network.c +++ b/src/network.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -115,13 +116,26 @@ void handler_tcp_echo(Server *s) { printf("Received connection from %s\n", buf); // Start child process - if (!fork()) { - close(s->fd); // Child doesn't need the initial socket - if (send(new_fd, "Hello, world!", 13, 0) == -1) { - fprintf(stderr, "Failed to send hello world\n"); - } - close(new_fd); - _exit(0); + if (!fork()) { + close(s->fd); // Child doesn't need the initial socket + + char recv_buf[256]; + int r = recv(new_fd, recv_buf, 256, 0); + if (r == -1) { + fprintf(stderr, "Failed to recv. Errno: %d\n", errno); + goto CHILD_END; + } else if (r == 0) { + fprintf(stderr, "Client closed connection\n"); + goto CHILD_END; + } + + if (send(new_fd, recv_buf, strlen(recv_buf), 0) == -1) { + fprintf(stderr, "Failed to send echo\n"); + } + +CHILD_END: + close(new_fd); + _exit(0); } // End child process diff --git a/tests/netmanual.c b/tests/netmanual.c new file mode 100644 index 0000000..60ede69 --- /dev/null +++ b/tests/netmanual.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "lfnetwork.h" + +int main(int argc, char **argv) { + Server *server = new_server(SERVERTYPE_TCP, "18632", handler_tcp_echo); + serve(server, DEFAULT_BACKLOG); + delete_server(server); +}