diff options
Diffstat (limited to 'src/tests.c')
| -rw-r--r-- | src/tests.c | 56 |
1 files changed, 25 insertions, 31 deletions
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 @@ | |||
| 4 | #include "encryption.c" | 4 | #include "encryption.c" |
| 5 | #include "threadpool.c" | 5 | #include "threadpool.c" |
| 6 | 6 | ||
| 7 | void test_encryption(void) { | 7 | void test_maketmp(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 | 8 | // 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"; | 9 | const char * const dir = ".", * const testmsg = "we do a little testing\n"; |
| 13 | char *path = NULL; | ||
| 14 | 10 | ||
| 15 | int fd = maketmp(dir); | 11 | int fd = maketmp(dir); |
| 16 | if(fd < 0) | 12 | if(fd < 0) ERROR(1, errno, "<test_maketmp> FAIL: Couldn't make temp file at %s", , dir); |
| 17 | error(1, errno, "Couldn't make temp file at %s", dir); | 13 | if(write(fd, testmsg, strlen(testmsg)) < 0) error(1, errno, "<test_maketmp> FAIL: write broke"); |
| 18 | |||
| 19 | if(write(fd, testmsg, strlen(testmsg)) < 0) | ||
| 20 | error(1, errno, "write broke"); | ||
| 21 | 14 | ||
| 22 | asprintf(&path, "/proc/self/fd/%d", fd); | 15 | if(linkto("./test_maketmp.test", fd)) ERROR(1, errno, "<test_maketmp> FAIL: Could not link into filesystem",); |
| 23 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 24 | free(path); | ||
| 25 | 16 | ||
| 26 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | 17 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages |
| 18 | if(close(fd) < 0) ERROR(1, errno, "close broke",); | ||
| 27 | 19 | ||
| 28 | if(close(fd) < 0) | 20 | return; |
| 29 | error(1, errno, "close broke"); | 21 | } |
| 30 | //*/// | ||
| 31 | |||
| 32 | //*// Example code for getting a password using genpassword | ||
| 33 | checksodium(); | ||
| 34 | 22 | ||
| 23 | void test_genpassword(void) { | ||
| 24 | // Example code for getting a password using genpassword | ||
| 35 | char *password = NULL; | 25 | char *password = NULL; |
| 36 | genpassword(&password, 20); | 26 | genpassword(&password, 20); |
| 37 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); | 27 | if(!password) ERROR(1, EINVAL, "<test_genpassword> FAIL: Couldn't get a password",); |
| 28 | |||
| 29 | printf("%s\n", password); | ||
| 38 | free(password); | 30 | free(password); |
| 39 | /*/// | ||
| 40 | 31 | ||
| 41 | //*/// Example code for generating a password, derriving a secret key from it, and storing things properly | 32 | return; |
| 33 | } | ||
| 34 | |||
| 35 | void test_libsodium_password(void) { | ||
| 36 | // Example code for generating a password, derriving a secret key from it, and storing things properly | ||
| 42 | 37 | ||
| 43 | // Initialization | 38 | // Initialization |
| 44 | checksodium(); | ||
| 45 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; | 39 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; |
| 40 | if(genpassword(&pass, 20) < 0) ERROR(1, 0, "<test_libsodium_password> FAIL: Could not generate password", ); | ||
| 46 | 41 | ||
| 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); | 42 | sodium_mlock(pass, strlen(pass) + 1); |
| 52 | printf("Password:%s\n", pass); | 43 | printf("Password:%s\n", pass); |
| 53 | 44 | ||
| 54 | // Store the password | 45 | // Store the password |
| 55 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) | 46 | 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..."); | 47 | ERROR(1, errno, "<test_libsodium_password> FAIL: Couldn't hash generated password",); |
| 57 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly | 48 | // 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 | 49 | // 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 | 50 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release |
| @@ -65,8 +56,9 @@ void test_encryption(void) { | |||
| 65 | 56 | ||
| 66 | // Check if the password from the user is correct | 57 | // Check if the password from the user is correct |
| 67 | char *uin = NULL; int size = -1; | 58 | char *uin = NULL; int size = -1; |
| 59 | printf("Please enter your password: "); | ||
| 68 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) | 60 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) |
| 69 | error(1, errno, "Could not read from stdin"); | 61 | ERROR(1, errno, "<test_libsodium_password> FAIL: Could not read from stdin",); |
| 70 | sodium_mlock(uin, size); | 62 | sodium_mlock(uin, size); |
| 71 | 63 | ||
| 72 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); | 64 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); |
| @@ -79,6 +71,8 @@ void test_encryption(void) { | |||
| 79 | } | 71 | } |
| 80 | 72 | ||
| 81 | int main(void) { | 73 | int main(void) { |
| 82 | test_encryption(); | 74 | test_maketmp(); |
| 75 | test_genpassword(); | ||
| 76 | test_libsodium_password(); | ||
| 83 | return 0; | 77 | return 0; |
| 84 | } \ No newline at end of file | 78 | } \ No newline at end of file |
