diff options
| author | @syxhe <https://t.me/syxhe> | 2025-10-21 13:30:40 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-10-21 13:30:40 -0500 |
| commit | 379b0712783c6e1dfece5550aee79bf39c1931a6 (patch) | |
| tree | 2b38bcf87201a204f14efc69075d86e3271bad98 /src/tests.c | |
| parent | 40bf034f6e5fdb261ebb9781fdde7a12ff402eda (diff) | |
Create tests file
Diffstat (limited to 'src/tests.c')
| -rw-r--r-- | src/tests.c | 84 |
1 files changed, 84 insertions, 0 deletions
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 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | |||
| 3 | #include "shared.c" | ||
| 4 | #include "encryption.c" | ||
| 5 | #include "threadpool.c" | ||
| 6 | |||
| 7 | void test_encryption(void) { | ||
| 8 | // TODO: Figure out if I care about this test existing or not. Currently, this has just been | ||
| 9 | // copied from ecryption.c & slapped | ||
| 10 | |||
| 11 | // Example code for creating a temp file, writing to it, then linking it back into the fs | ||
| 12 | const char *dir = ".", *testmsg = "we do a little testing\n"; | ||
| 13 | char *path = NULL; | ||
| 14 | |||
| 15 | int fd = maketmp(dir); | ||
| 16 | if(fd < 0) | ||
| 17 | error(1, errno, "Couldn't make temp file at %s", dir); | ||
| 18 | |||
| 19 | if(write(fd, testmsg, strlen(testmsg)) < 0) | ||
| 20 | error(1, errno, "write broke"); | ||
| 21 | |||
| 22 | asprintf(&path, "/proc/self/fd/%d", fd); | ||
| 23 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 24 | free(path); | ||
| 25 | |||
| 26 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | ||
| 27 | |||
| 28 | if(close(fd) < 0) | ||
| 29 | error(1, errno, "close broke"); | ||
| 30 | //*/// | ||
| 31 | |||
| 32 | //*// Example code for getting a password using genpassword | ||
| 33 | checksodium(); | ||
| 34 | |||
| 35 | char *password = NULL; | ||
| 36 | genpassword(&password, 20); | ||
| 37 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); | ||
| 38 | free(password); | ||
| 39 | /*/// | ||
| 40 | |||
| 41 | //*/// Example code for generating a password, derriving a secret key from it, and storing things properly | ||
| 42 | |||
| 43 | // Initialization | ||
| 44 | checksodium(); | ||
| 45 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; | ||
| 46 | |||
| 47 | if(genpassword(&pass, 20) < 0) { | ||
| 48 | error(1, 0, "Could not generate password, quitting..."); | ||
| 49 | abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever | ||
| 50 | } | ||
| 51 | sodium_mlock(pass, strlen(pass) + 1); | ||
| 52 | printf("Password:%s\n", pass); | ||
| 53 | |||
| 54 | // Store the password | ||
| 55 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) | ||
| 56 | error(1, errno, "Couldn't generate password, quitting..."); | ||
| 57 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly | ||
| 58 | // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is | ||
| 59 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release | ||
| 60 | |||
| 61 | sodium_munlock(pass, strlen(pass) + 1); | ||
| 62 | free(pass); | ||
| 63 | |||
| 64 | printf("Hashed password: %s\n", hpass); | ||
| 65 | |||
| 66 | // Check if the password from the user is correct | ||
| 67 | char *uin = NULL; int size = -1; | ||
| 68 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) | ||
| 69 | error(1, errno, "Could not read from stdin"); | ||
| 70 | sodium_mlock(uin, size); | ||
| 71 | |||
| 72 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); | ||
| 73 | |||
| 74 | |||
| 75 | sodium_munlock(uin, strlen(uin) + 1); | ||
| 76 | free(uin); | ||
| 77 | |||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | int main(void) { | ||
| 82 | test_encryption(); | ||
| 83 | return 0; | ||
| 84 | } \ No newline at end of file | ||
