From 5611a4f193dde7845f19f172f12d4f9f0bd81735 Mon Sep 17 00:00:00 2001 From: Netrunner Date: Sun, 14 Apr 2024 22:56:53 +0100 Subject: [PATCH 1/2] fix sonic tests --- .github/workflows/test.yaml | 5 +++- camel/src/camel.c | 54 ++++++++++++++++++++----------------- makefiles/test.mk | 2 +- sonic/tests/client/makefile | 8 ++++-- sonic/tests/server/makefile | 12 ++++++--- 5 files changed, 49 insertions(+), 32 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e8c9f2d..c057018 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -17,6 +17,8 @@ jobs: run: make dev - name: run tests run: make test + - name: test libs + run: make test-libs build-macos: runs-on: macos-latest @@ -28,5 +30,6 @@ jobs: run: make dev - name: run tests run: make test + - name: test libs + run: make test-libs - diff --git a/camel/src/camel.c b/camel/src/camel.c index 7787afe..03b0d2a 100644 --- a/camel/src/camel.c +++ b/camel/src/camel.c @@ -13,6 +13,7 @@ #include "../include/tuwi.h" #include +#include #include #include @@ -25,39 +26,42 @@ #include bool __files_equal(char *first_file, char *second_file) { - char command1[1024]; // Buffer for the first checksum command - char command2[1024]; // Buffer for the second checksum command + FILE *file1 = fopen(first_file, "rb"); + FILE *file2 = fopen(second_file, "rb"); - // Create the shell command to calculate the checksum of file1 - snprintf(command1, sizeof(command1), "md5sum \"%s\" 2>/dev/null", first_file); + if (file1 == NULL || file2 == NULL) { + perror("Error opening file"); + PANIC("could not open files"); + } + + int byte1; + int byte2; + bool equal = true; - // Create the shell command to calculate the checksum of file2 - snprintf(command2, sizeof(command2), "md5sum \"%s\" 2>/dev/null", - second_file); + while (1) { + byte1 = fgetc(file1); + byte2 = fgetc(file2); - // Execute the shell commands using popen and capture their output - FILE *fp1 = popen(command1, "r"); - FILE *fp2 = popen(command2, "r"); + if (byte1 == EOF || byte2 == EOF) { + break; + } - if (fp1 == NULL || fp2 == NULL) { - perror("popen"); - exit(EXIT_FAILURE); + if (byte1 != byte2) { + equal = false; + break; + } } - char checksum1[32], checksum2[32]; // Buffer to store checksums - fgets(checksum1, sizeof(checksum1), fp1); - fgets(checksum2, sizeof(checksum2), fp2); + // Check if both files have reached EOF at the same time + if (byte1 != EOF || byte2 != EOF) { + LOG(INFO, "THIS!"); + equal = false; + } - // Close the file pointers - pclose(fp1); - pclose(fp2); + fclose(file1); + fclose(file2); - // Compare the checksums - if (strcmp(checksum1, checksum2) == 0) { - return true; // Checksums match - } else { - return false; // Checksums do not match - } + return equal; } char *__read_file(char *path) { diff --git a/makefiles/test.mk b/makefiles/test.mk index 8a91a3c..e59ab72 100644 --- a/makefiles/test.mk +++ b/makefiles/test.mk @@ -1,5 +1,5 @@ test-libs: - cd lib/sonic && make test + cd ./sonic && make test test: build-tests run-tests diff --git a/sonic/tests/client/makefile b/sonic/tests/client/makefile index 2d1c4a3..5ea0d16 100644 --- a/sonic/tests/client/makefile +++ b/sonic/tests/client/makefile @@ -4,7 +4,7 @@ LDFLAGS += -lasan -lubsan endif CFLAGS += -LDFLAGS += -lssl -lcrypto +LDFLAGS += $$(pkg-config --libs openssl) $(TARGET_DIR)/libnetlibc.a TARGET_DIR = ../../target @@ -18,7 +18,7 @@ LIBSONIC = $(TARGET_DIR)/libsonic-sanitized.a camel: cd $(ROOT_DIR) && $(MAKE) camel -build: +build: $(TARGET_DIR)/libnetlibc.a cd $(ROOT_DIR) && $(MAKE) build-sanitized target-dir: @@ -36,6 +36,10 @@ target-dir: mkdir -p $(TARGET_DIR)/tests/client/integration mkdir -p $(TARGET_DIR)/tests/client/fuzz +$(TARGET_DIR)/libnetlibc.a: + cd ../../../netlibc/ && make + cp ../../../netlibc/target/libnetlibc.a $(TARGET_DIR)/libnetlibc.a + # FUNCTIONAL ======================================================================================== $(TARGET_DIR)/tests/client/functional/local_requests/large_test_file.bin: diff --git a/sonic/tests/server/makefile b/sonic/tests/server/makefile index ef7d77a..5e88819 100644 --- a/sonic/tests/server/makefile +++ b/sonic/tests/server/makefile @@ -4,7 +4,7 @@ LDFLAGS += -lasan -lubsan endif CFLAGS += -LDFLAGS += -lssl -lcrypto +LDFLAGS += $$(pkg-config --libs openssl) $(TARGET_DIR)/libnetlibc.a TARGET_DIR = ../../target @@ -19,7 +19,7 @@ LIBSONIC = $(TARGET_DIR)/libsonic-sanitized.a camel: cd $(ROOT_DIR) && $(MAKE) camel -build: +build: $(TARGET_DIR)/libnetlibc.a cd $(ROOT_DIR) && $(MAKE) build-sanitized target-dir: @@ -34,7 +34,13 @@ target-dir: mkdir -p $(TARGET_DIR)/tests/server/unit mkdir -p $(TARGET_DIR)/tests/server/integration mkdir -p $(TARGET_DIR)/tests/server/fuzz - + + +$(TARGET_DIR)/libnetlibc.a: + cd ../../../netlibc/ && make + cp ../../../netlibc/target/libnetlibc.a $(TARGET_DIR)/libnetlibc.a + + # FUNCTIONAL ======================================================================================== $(TARGET_DIR)/tests/server/functional/serve/large_test_file.bin: From 48fa8c26f5493606c1adf60ed43ee9301c8c26bd Mon Sep 17 00:00:00 2001 From: Netrunner Date: Sun, 14 Apr 2024 23:18:28 +0100 Subject: [PATCH 2/2] fix sonic netlibc dependency error --- sonic/tests/client/makefile | 44 ++++++++++++++++++------------------- sonic/tests/server/makefile | 12 +++++----- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/sonic/tests/client/makefile b/sonic/tests/client/makefile index 5ea0d16..6595c92 100644 --- a/sonic/tests/client/makefile +++ b/sonic/tests/client/makefile @@ -3,7 +3,7 @@ CFLAGS += -fsanitize=address,undefined,leak,undefined LDFLAGS += -lasan -lubsan endif -CFLAGS += +CFLAGS += -g -I ../../../netlibc/include LDFLAGS += $$(pkg-config --libs openssl) $(TARGET_DIR)/libnetlibc.a TARGET_DIR = ../../target @@ -58,31 +58,31 @@ build-functional-tests: target-dir build camel large-test-file $(MAKE) build-functional-test-internet-requests build-functional-test-local-requests: - $(CC) -g ./functional/local_requests/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/test - $(CC) -g ./functional/local_requests/local_server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/local_server + $(CC) $(CFLAGS) ./functional/local_requests/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/test + $(CC) $(CFLAGS) ./functional/local_requests/local_server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/local_server - $(CC) -g ./functional/local_requests/get_200.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/get_200 - $(CC) -g ./functional/local_requests/get_404.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/get_404 - $(CC) -g ./functional/local_requests/post_with_body.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/post_with_body - $(CC) -g ./functional/local_requests/post_with_headers.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/post_with_headers - $(CC) -g ./functional/local_requests/receive_headers.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/receive_headers - $(CC) -g ./functional/local_requests/response_callback.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/response_callback + $(CC) $(CFLAGS) ./functional/local_requests/get_200.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/get_200 + $(CC) $(CFLAGS) ./functional/local_requests/get_404.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/get_404 + $(CC) $(CFLAGS) ./functional/local_requests/post_with_body.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/post_with_body + $(CC) $(CFLAGS) ./functional/local_requests/post_with_headers.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/post_with_headers + $(CC) $(CFLAGS) ./functional/local_requests/receive_headers.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/receive_headers + $(CC) $(CFLAGS) ./functional/local_requests/response_callback.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/local_requests/response_callback build-functional-test-internet-requests: - $(CC) -g ./functional/internet_requests/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/test + $(CC) $(CFLAGS) ./functional/internet_requests/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/test - $(CC) -g ./functional/internet_requests/get_200.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/get_200 - $(CC) -g ./functional/internet_requests/with_subdomain.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/with_subdomain - $(CC) -g ./functional/internet_requests/ip_and_port.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/ip_and_port - $(CC) -g ./functional/internet_requests/no_https.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/no_https + $(CC) $(CFLAGS) ./functional/internet_requests/get_200.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/get_200 + $(CC) $(CFLAGS) ./functional/internet_requests/with_subdomain.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/with_subdomain + $(CC) $(CFLAGS) ./functional/internet_requests/ip_and_port.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/ip_and_port + $(CC) $(CFLAGS) ./functional/internet_requests/no_https.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/internet_requests/no_https build-functional-test-redirects: - $(CC) -g ./functional/redirects/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/test - $(CC) -g ./functional/redirects/local_server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/local_server + $(CC) $(CFLAGS) ./functional/redirects/test.c $(TARGET_DIR)/camel.a $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/test + $(CC) $(CFLAGS) ./functional/redirects/local_server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/local_server - $(CC) -g ./functional/redirects/many_redirects.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/many_redirects - $(CC) -g ./functional/redirects/hops_limit.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/hops_limit - $(CC) -g ./functional/redirects/no_redirect.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/no_redirect + $(CC) $(CFLAGS) ./functional/redirects/many_redirects.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/many_redirects + $(CC) $(CFLAGS) ./functional/redirects/hops_limit.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/hops_limit + $(CC) $(CFLAGS) ./functional/redirects/no_redirect.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/functional/redirects/no_redirect #======================================================================================== @@ -93,7 +93,7 @@ run-unit-tests: cd $(TARGET_DIR)/tests/client/unit/ && ./test_client build-unit-tests: target-dir build camel - $(CC) -g ./unit/test_client.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/unit/test_client + $(CC) $(CFLAGS) ./unit/test_client.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/unit/test_client #======================================================================================== @@ -103,7 +103,7 @@ run-integration-tests: cd $(TARGET_DIR)/tests/client/integration/ && ./test build-integration-tests: target-dir build camel - $(CC) -g ./integration/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/integration/test + $(CC) $(CFLAGS) ./integration/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/integration/test #======================================================================================== @@ -113,7 +113,7 @@ run-fuzz-tests: cd $(TARGET_DIR)/tests/client/fuzz/ && ./test build-fuzz-tests: target-dir build camel - $(CC) -g ./fuzz/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/fuzz/test + $(CC) $(CFLAGS) ./fuzz/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/client/fuzz/test #======================================================================================== diff --git a/sonic/tests/server/makefile b/sonic/tests/server/makefile index 5e88819..7a58114 100644 --- a/sonic/tests/server/makefile +++ b/sonic/tests/server/makefile @@ -3,7 +3,7 @@ CFLAGS += -fsanitize=address,undefined,leak,undefined LDFLAGS += -lasan -lubsan endif -CFLAGS += +CFLAGS += -g -I ../../../netlibc/include LDFLAGS += $$(pkg-config --libs openssl) $(TARGET_DIR)/libnetlibc.a @@ -55,8 +55,8 @@ build-functional-tests: target-dir build camel large-test-file $(MAKE) build-functional-test-serve build-functional-test-serve: - $(CC) -g ./functional/serve/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/functional/serve/test - $(CC) -g ./functional/serve/server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/functional/serve/server + $(CC) $(CFLAGS) ./functional/serve/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/functional/serve/test + $(CC) $(CFLAGS) ./functional/serve/server.c $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/functional/serve/server #======================================================================================== @@ -67,7 +67,7 @@ run-unit-tests: cd $(TARGET_DIR)/tests/server/unit/ && ./test_server build-unit-tests: target-dir build camel - $(CC) -g ./unit/test_server.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/unit/test_server + $(CC) $(CFLAGS) ./unit/test_server.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/unit/test_server #======================================================================================== @@ -77,7 +77,7 @@ run-integration-tests: cd $(TARGET_DIR)/tests/server/integration/ && ./test build-integration-tests: target-dir build camel - $(CC) -g ./integration/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/integration/test + $(CC) $(CFLAGS) ./integration/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/integration/test #======================================================================================== @@ -87,7 +87,7 @@ run-fuzz-tests: cd $(TARGET_DIR)/tests/server/fuzz/ && ./test build-fuzz-tests: target-dir build camel - $(CC) -g ./fuzz/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/fuzz/test + $(CC) $(CFLAGS) ./fuzz/test.c $(TARGET_DIR)/camel.a $(LIBSONIC) $(LDFLAGS) -o $(TARGET_DIR)/tests/server/fuzz/test #========================================================================================