1 /*
2 * Test WebAuth Application Server token cache support.
3 *
4 * Written by Russ Allbery <eagle@eyrie.org>
5 * Copyright 2012
6 * The Board of Trustees of the Leland Stanford Junior University
7 *
8 * See LICENSE for licensing terms.
9 */
10
11 #include <config.h>
12 #include <portable/system.h>
13
14 #include <time.h>
15
16 #include <tests/tap/basic.h>
17 #include <tests/tap/string.h>
18 #include <webauth/basic.h>
19 #include <webauth/keys.h>
20 #include <webauth/was.h>
21
22
23 int
main(void)24 main(void)
25 {
26 struct webauth_context *ctx;
27 struct webauth_was_token_cache cache, cache2;
28 struct webauth_key *key;
29 time_t now;
30 char *tmpdir, *path;
31 int s;
32
33 plan(20);
34
35 if (webauth_context_init(&ctx, NULL) != WA_ERR_NONE)
36 bail("cannot initialize WebAuth context");
37
38 /* Create some random data to store in a token cache. */
39 now = time(NULL);
40 s = webauth_key_create(ctx, WA_KEY_AES, WA_AES_128, NULL, &key);
41 if (s != WA_ERR_NONE)
42 bail("cannot create key: %s", webauth_error_message(ctx, s));
43 cache.token = (char *) "asdfghjkl;v=1";
44 cache.key_type = key->type;
45 cache.key_data = key->data;
46 cache.key_data_len = key->length;
47 cache.created = now;
48 cache.expires = now + 10;
49 cache.last_renewal = now;
50 cache.next_renewal = now + 5;
51
52 /* Test storing that data in a cache file. */
53 tmpdir = test_tmpdir();
54 basprintf(&path, "%s/token-cache", tmpdir);
55 s = webauth_was_token_cache_write(ctx, &cache, path);
56 is_int(WA_ERR_NONE, s, "Writing token cache succeeds");
57 is_int(0, access(path, R_OK), "...and file now exists");
58
59 /* Read the data back in. */
60 memset(&cache2, 0, sizeof(cache2));
61 s = webauth_was_token_cache_read(ctx, path, &cache2);
62 is_int(WA_ERR_NONE, s, "Reading token cache succeeds");
63 is_string(cache.token, cache2.token, "...and token is correct");
64 is_int(cache.key_type, cache2.key_type, "...and key type is correct");
65 is_int(cache.key_data_len, cache2.key_data_len,
66 "...and key length is correct");
67 ok(memcmp(cache.key_data, cache2.key_data, cache.key_data_len) == 0,
68 "...and key data is correct");
69 is_int(cache.created, cache2.created, "...and created is correct");
70 is_int(cache.expires, cache2.expires, "...and expires is correct");
71 is_int(cache.last_renewal, cache2.last_renewal,
72 "...and last renewal is correct");
73 is_int(cache.next_renewal, cache2.next_renewal,
74 "...and next renewal is correct");
75 unlink(path);
76 free(path);
77
78 /* Read in a known service token and ensure that we can decode it. */
79 path = test_file_path("data/service-token");
80 memset(&cache, 0, sizeof(cache));
81 s = webauth_was_token_cache_read(ctx, path, &cache);
82 if (s != WA_ERR_NONE)
83 diag("failed: %s", webauth_error_message(ctx, s));
84 is_int(WA_ERR_NONE, s, "Reading known token succeeds");
85 ok(cache.token != NULL, "...and token is non-NULL");
86 is_int(WA_KEY_AES, cache.key_type, "...and key type is correct");
87 is_int(WA_AES_128, cache.key_data_len, "...and key length is correct");
88 ok(cache.key_data != NULL, "...and key data is non-NULL");
89 is_int(1346791413, cache.created, "...and created is correct");
90 is_int(1349383413, cache.expires, "...and expires is correct");
91 is_int(0, cache.last_renewal, "...and last renewal is correct");
92 is_int(1349124213, cache.next_renewal, "...and next renewal is correct");
93
94 /* Clean up. */
95 test_file_path_free(path);
96 test_tmpdir_free(tmpdir);
97 webauth_context_free(ctx);
98 return 0;
99 }
100