From 379b0712783c6e1dfece5550aee79bf39c1931a6 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 21 Oct 2025 13:30:40 -0500 Subject: Create tests file --- .gitignore | 1 + src/Makefile | 15 +++++++--- src/encryption.c | 73 ------------------------------------------------ src/tests.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 77 deletions(-) create mode 100644 src/tests.c diff --git a/.gitignore b/.gitignore index 1d93676..b5404e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ main +tests encryption ll a.out diff --git a/src/Makefile b/src/Makefile index e8c04f9..de0c9f6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -16,7 +16,7 @@ SOURCES := $(wildcard *.c) TIMESTAMP_DIR := .timestamps TIMESTAMPS := $(patsubst %.c,$(TIMESTAMP_DIR)/%.t,$(SOURCES)) -.PHONY: all c clean val +.PHONY: all c clean v val t test .DELETE_ON_ERROR: .ONESHELL: @@ -28,12 +28,19 @@ $(TIMESTAMP_DIR): $(TIMESTAMPS): $(TIMESTAMP_DIR)/%.t: %.c | $(TIMESTAMP_DIR) touch $@ -main: main.c $(TIMESTAMPS) +main tests: %: %.c $(TIMESTAMPS) $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@ + +# Phony rules + c clean: -rm -rvf main $(TIMESTAMP_DIR) $(wildcard *.test*) $(wildcard *.enc) -val: +v val: $(MAKE) all - valgrind --leak-check=yes ./main \ No newline at end of file + valgrind --leak-check=yes ./main + +t test: + $(MAKE) tests + valgrind --leak-check=yes ./tests \ No newline at end of file diff --git a/src/encryption.c b/src/encryption.c index 382f6d5..aafb972 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -634,77 +634,4 @@ ctqueue * cryptscan() { return res; } -/* -int main(void) { - // Example code for creating a temp file, writing to it, then linking it back into the fs - const char *dir = ".", *testmsg = "we do a little testing\n"; - char *path = NULL; - - int fd = maketmp(dir); - if(fd < 0) - error(1, errno, "Couldn't make temp file at %s", dir); - - if(write(fd, testmsg, strlen(testmsg)) < 0) - error(1, errno, "write broke"); - - asprintf(&path, "/proc/self/fd/%d", fd); - linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); - free(path); - - // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages - - if(close(fd) < 0) - error(1, errno, "close broke"); - //*/// - - /*// Example code for getting a password using genpassword - checksodium(); - - char *password = NULL; - genpassword(&password, 20); - printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); - free(password); - //*/// - - /*// Example code for generating a password, derriving a secret key from it, and storing things properly - - // Initialization - checksodium(); - char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; - - if(genpassword(&pass, 20) < 0) { - error(1, 0, "Could not generate password, quitting..."); - abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever - } - sodium_mlock(pass, strlen(pass) + 1); - printf("Password:%s\n", pass); - - // Store the password - if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) - error(1, errno, "Couldn't generate password, quitting..."); - // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly - // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is - // barely noticable. I might do MODERATE for testing and SENSITIVE for release - - sodium_munlock(pass, strlen(pass) + 1); - free(pass); - - printf("Hashed password: %s\n", hpass); - - // Check if the password from the user is correct - char *uin = NULL; int size = -1; - if((size = readwholebuffer(&uin, 1, STDIN_FILENO)) < 0) - error(1, errno, "Could not read from stdin"); - sodium_mlock(uin, size); - - printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); - - - sodium_munlock(uin, strlen(uin) + 1); - free(uin); - - return 0; -} -*/ - #endif \ No newline at end of file diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 0000000..882fdc2 --- /dev/null +++ b/src/tests.c @@ -0,0 +1,84 @@ +#define _GNU_SOURCE + +#include "shared.c" +#include "encryption.c" +#include "threadpool.c" + +void test_encryption(void) { + // TODO: Figure out if I care about this test existing or not. Currently, this has just been + // copied from ecryption.c & slapped + + // Example code for creating a temp file, writing to it, then linking it back into the fs + const char *dir = ".", *testmsg = "we do a little testing\n"; + char *path = NULL; + + int fd = maketmp(dir); + if(fd < 0) + error(1, errno, "Couldn't make temp file at %s", dir); + + if(write(fd, testmsg, strlen(testmsg)) < 0) + error(1, errno, "write broke"); + + asprintf(&path, "/proc/self/fd/%d", fd); + linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); + free(path); + + // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages + + if(close(fd) < 0) + error(1, errno, "close broke"); + //*/// + + //*// Example code for getting a password using genpassword + checksodium(); + + char *password = NULL; + genpassword(&password, 20); + printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); + free(password); + /*/// + + //*/// Example code for generating a password, derriving a secret key from it, and storing things properly + + // Initialization + checksodium(); + char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; + + if(genpassword(&pass, 20) < 0) { + error(1, 0, "Could not generate password, quitting..."); + abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever + } + sodium_mlock(pass, strlen(pass) + 1); + printf("Password:%s\n", pass); + + // Store the password + if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) + error(1, errno, "Couldn't generate password, quitting..."); + // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly + // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is + // barely noticable. I might do MODERATE for testing and SENSITIVE for release + + sodium_munlock(pass, strlen(pass) + 1); + free(pass); + + printf("Hashed password: %s\n", hpass); + + // Check if the password from the user is correct + char *uin = NULL; int size = -1; + if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) + error(1, errno, "Could not read from stdin"); + sodium_mlock(uin, size); + + printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); + + + sodium_munlock(uin, strlen(uin) + 1); + free(uin); + + return; +} + +int main(void) { + test_encryption(); + return 0; +} \ No newline at end of file -- cgit v1.2.3