1 /* Copyright (c) 2016 Google, Inc.
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details. */
5 #include <utest/utest.h>
6 #include <parlib/dtls.h>
11 /* <--- Begin definition of test cases ---> */
13 bool test_get_without_set(void)
18 key = dtls_key_create(0);
19 got_val = get_dtls(key);
20 UT_ASSERT_FMT("Expected 0, got %p", got_val == 0, 0, got_val);
25 /* This catches a bug, but it had to have a set happen at some point. */
26 bool test_get_after_reset(void)
29 void *set_val = (void*)0x15;
32 key = dtls_key_create(0);
33 set_dtls(key, set_val);
36 key = dtls_key_create(0);
37 got_val = get_dtls(key);
38 UT_ASSERT_FMT("Expected 0, got %p", got_val == 0, 0, got_val);
43 bool test_set_and_get(void)
46 void *set_val = (void*)0x15;
49 key = dtls_key_create(0);
50 set_dtls(key, set_val);
51 got_val = get_dtls(key);
52 UT_ASSERT_FMT("Expected %p, got %p", got_val == set_val, set_val, got_val);
57 bool test_set_twice(void)
60 void *set_val = (void*)0x15;
63 key = dtls_key_create(0);
64 set_dtls(key, set_val);
65 set_dtls(key, set_val + 1);
70 static dtls_key_t sfd_global_key;
72 static void setting_dtor(void *arg)
74 set_dtls(sfd_global_key, (void*)0xf00);
77 /* Users can set from a destructor. In some implementations of pthread keys,
78 * you can't safely set_specific from within a destructor without the risk of an
79 * infinite loop or storage loss. Our DTLS implementation shouldn't be doing
80 * either, though we can't check for storage loss. */
81 bool test_set_from_dtor(void)
83 void *set_val = (void*)0x15;
85 sfd_global_key = dtls_key_create(setting_dtor);
86 set_dtls(sfd_global_key, set_val);
91 /* <--- End definition of test cases ---> */
93 struct utest utests[] = {
94 UTEST_REG(get_without_set),
95 UTEST_REG(get_after_reset),
96 UTEST_REG(set_and_get),
98 UTEST_REG(set_from_dtor),
100 int num_utests = sizeof(utests) / sizeof(struct utest);
102 int main(int argc, char *argv[])
104 // Run test suite passing it all the args as whitelist of what tests to run.
105 char **whitelist = &argv[1];
106 int whitelist_len = argc - 1;
108 RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);