4 // posix for clock/timer functions
5 #define _POSIX_C_SOURCE 200112L
13 bool get_monotonic_timestamp(double *ts);
15 int main(int argc, char **argv){
21 fprintf(stderr, "Usage: %s DATABASE\n", argv[0]);
24 rc = sqlite3_open(argv[1], &db);
26 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
31 pw_init_context(db, &ctx);
33 char *u1 = "foo@example.com", *p1 = "password123",
34 *u1c = "FOO@EXAMPLE.COM";
38 ok(PW_OK == pw_set(ctx, u1, p1), "pw_set");
40 ok(PW_OK == pw_check(ctx, u1, p1),
41 "pw_check accepts the same credentials");
43 ok(PW_OK == pw_check(ctx, u1c, p1),
44 "pw_check also accepts mixed case user");
45 ok(PW_BAD_PASS == pw_check(ctx, u1, "wrong"),
46 "pw_check fails for wrong pass");
47 ok(PW_NO_USER == pw_check(ctx, "fake", "wrong"),
48 "pw_check fails for missing user");
53 ok(PW_OK == pw_token_create(ctx, u1, &tok, &tok_len),
54 "pw_token_create can build one");
55 cmp_ok(tok_len, "==", 16,
56 "token has expected length");
63 diag("check password 100 times");
64 get_monotonic_timestamp(&start);
65 for (i = 0; i < 100; i++)
66 pw_check(ctx, u1, p1);
67 get_monotonic_timestamp(&end);
68 t_found = end - start;
70 diag("check 100 times for missing user");
71 get_monotonic_timestamp(&start);
72 for (i = 0; i < 100; i++)
73 pw_check(ctx, "fake", "wrong");
74 get_monotonic_timestamp(&end);
75 t_notfound = end - start;
78 fabs( (t_found - t_notfound) / ((t_found + t_notfound)/2) );
80 cmp_ok(pct_diff, "<", 1.0, "less than 1%% time difference");
91 // idea for using double representation taken from
92 // https://github.com/solemnwarning/timespec
93 #define NSEC_PER_SEC 1000000000
94 bool get_monotonic_timestamp(double *ts)
97 int rc = clock_gettime(CLOCK_MONOTONIC, &now);
101 ((double)now.tv_sec) +
102 (((double)now.tv_nsec) / NSEC_PER_SEC)