From 129b7312400a69b995b67fd2feb3f915b7f20555 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 21 Oct 2025 16:33:55 -0500 Subject: Reformat tests.c --- src/tests.c | 56 +++++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) (limited to 'src/tests.c') diff --git a/src/tests.c b/src/tests.c index 2b98b3d..a7531e8 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4,56 +4,47 @@ #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 - +void test_maketmp(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; + const char * const dir = ".", * const testmsg = "we do a little testing\n"; 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"); + if(fd < 0) ERROR(1, errno, " FAIL: Couldn't make temp file at %s", , dir); + if(write(fd, testmsg, strlen(testmsg)) < 0) error(1, errno, " FAIL: write broke"); - asprintf(&path, "/proc/self/fd/%d", fd); - linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); - free(path); + if(linkto("./test_maketmp.test", fd)) ERROR(1, errno, " FAIL: Could not link into filesystem",); // 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",); - if(close(fd) < 0) - error(1, errno, "close broke"); - //*/// - - //*// Example code for getting a password using genpassword - checksodium(); + return; +} +void test_genpassword(void) { + // Example code for getting a password using genpassword char *password = NULL; genpassword(&password, 20); - printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); + if(!password) ERROR(1, EINVAL, " FAIL: Couldn't get a password",); + + printf("%s\n", password); free(password); - /*/// - //*/// Example code for generating a password, derriving a secret key from it, and storing things properly + return; +} + +void test_libsodium_password(void) { + // 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, " FAIL: Could not generate password", ); - 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..."); + ERROR(1, errno, " FAIL: Couldn't hash generated password",); // 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 @@ -65,8 +56,9 @@ void test_encryption(void) { // Check if the password from the user is correct char *uin = NULL; int size = -1; + printf("Please enter your password: "); if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) - error(1, errno, "Could not read from stdin"); + ERROR(1, errno, " FAIL: Could not read from stdin",); sodium_mlock(uin, size); printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); @@ -79,6 +71,8 @@ void test_encryption(void) { } int main(void) { - test_encryption(); + test_maketmp(); + test_genpassword(); + test_libsodium_password(); return 0; } \ No newline at end of file -- cgit v1.2.3