-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.cpp
63 lines (57 loc) · 2.09 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "pasta.h"
void test_hash_and_verify() {
char *hash;
hash = hash_password("hello123");
assert (verify_password(hash, "hello123"));
assert (!verify_password(hash, "hello1234"));
free_string(hash);
}
void test_migrate() {
char *old_hash = (char *)"$2a$10$vI8aWBnW3fID.ZQ4/zo1G.q1lRps.9cGLcZEiGDMVr5yUP1KUOYTa";
char *hash;
HashUpdateFfi *res = migrate_hash(old_hash);
switch(res->tag) {
case HashUpdateFfi::Tag::Updated: hash = res->updated._0; break;
case HashUpdateFfi::Tag::Ok: assert (false && "Expected a password migration");
case HashUpdateFfi::Tag::Failed: assert (false && "Problem migrating password");
}
assert (strcmp(old_hash, hash) != 0);
printf("New hash: %s\n", hash);
free_string(hash);
hash = (char *)"$2a$10$vI8aWBnW3fID.ZQ4/zo1G.q1lRps.9cGLcZEiGDMVr5yUP1KUOYTa";
char *newhash;
res = verify_password_update_hash(hash, "my password");
switch(res->tag) {
case HashUpdateFfi::Tag::Updated: newhash = res->updated._0;
case HashUpdateFfi::Tag::Ok: printf("Password verified\n"); break;
case HashUpdateFfi::Tag::Failed: assert (false && "Password failed");
}
printf("New hash: %s\n", newhash);
assert (strcmp(newhash, hash) != 0);
assert (verify_password(newhash, "my password"));
// free_string(hash) // dont need to free this since it's static
free_string(newhash);
assert (verify_password_update_hash(hash, "not my password")->tag == HashUpdateFfi::Tag::Failed);
}
void test_config() {
Primitive *prim = default_bcrypt();
Config *config = config_with_primitive(prim);
char *hash = config_hash_password(config, "hello123");
assert (config_verify_password(config, hash, "hello123"));
assert (!config_verify_password(config, hash, "hunter2"));
}
void test_edge_cases() {
hash_password("");
hash_password("\x00");
}
int main(void) {
test_hash_and_verify();
test_migrate();
test_config();
test_edge_cases();
printf("\x1b[1;32mC++ test passed\x1b[m\n");
return 0;
}