Neels Hofmeyr | 37984bd | 2016-03-30 11:22:24 +0200 | [diff] [blame^] | 1 | #include <stdbool.h> |
| 2 | |
| 3 | #include <osmocom/core/application.h> |
| 4 | #include <osmocom/core/logging.h> |
| 5 | |
| 6 | #include <openbsc/debug.h> |
| 7 | #include <openbsc/gsm_data.h> |
| 8 | #include <openbsc/gsm_subscriber.h> |
| 9 | #include <openbsc/auth.h> |
| 10 | |
| 11 | /* override, requires '-Wl,--wrap=db_get_authinfo_for_subscr' */ |
| 12 | int __real_db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 13 | struct gsm_subscriber *subscr); |
| 14 | |
| 15 | int test_get_authinfo_rc = 0; |
| 16 | struct gsm_auth_info test_auth_info = {0}; |
| 17 | struct gsm_auth_info default_auth_info = { |
| 18 | .auth_algo = AUTH_ALGO_COMP128v1, |
| 19 | .a3a8_ki_len = 16, |
| 20 | .a3a8_ki = { 0 } |
| 21 | }; |
| 22 | |
| 23 | int __wrap_db_get_authinfo_for_subscr(struct gsm_auth_info *ainfo, |
| 24 | struct gsm_subscriber *subscr) |
| 25 | { |
| 26 | *ainfo = test_auth_info; |
| 27 | printf("wrapped: db_get_authinfo_for_subscr(): rc = %d\n", test_get_authinfo_rc); |
| 28 | return test_get_authinfo_rc; |
| 29 | } |
| 30 | |
| 31 | /* override, requires '-Wl,--wrap=db_get_lastauthtuple_for_subscr' */ |
| 32 | int __real_db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 33 | struct gsm_subscriber *subscr); |
| 34 | |
| 35 | int test_get_lastauthtuple_rc = 0; |
| 36 | struct gsm_auth_tuple test_last_auth_tuple = { 0 }; |
| 37 | struct gsm_auth_tuple default_auth_tuple = { 0 }; |
| 38 | |
| 39 | int __wrap_db_get_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 40 | struct gsm_subscriber *subscr) |
| 41 | { |
| 42 | *atuple = test_last_auth_tuple; |
| 43 | printf("wrapped: db_get_lastauthtuple_for_subscr(): rc = %d\n", test_get_lastauthtuple_rc); |
| 44 | return test_get_lastauthtuple_rc; |
| 45 | } |
| 46 | |
| 47 | /* override, requires '-Wl,--wrap=db_sync_lastauthtuple_for_subscr' */ |
| 48 | int __real_db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 49 | struct gsm_subscriber *subscr); |
| 50 | int test_sync_lastauthtuple_rc = 0; |
| 51 | int __wrap_db_sync_lastauthtuple_for_subscr(struct gsm_auth_tuple *atuple, |
| 52 | struct gsm_subscriber *subscr) |
| 53 | { |
| 54 | test_last_auth_tuple = *atuple; |
| 55 | printf("wrapped: db_sync_lastauthtuple_for_subscr(): rc = %d\n", test_sync_lastauthtuple_rc); |
| 56 | return test_sync_lastauthtuple_rc; |
| 57 | } |
| 58 | |
| 59 | int auth_get_tuple_for_subscr_verbose(struct gsm_auth_tuple *atuple, |
| 60 | struct gsm_subscriber *subscr, |
| 61 | int key_seq) |
| 62 | { |
| 63 | int auth_action; |
| 64 | auth_action = auth_get_tuple_for_subscr(atuple, subscr, key_seq); |
| 65 | printf("auth_get_tuple_for_subscr(key_seq=%d) --> auth_action == %s\n", |
| 66 | key_seq, auth_action_str(auth_action)); |
| 67 | return auth_action; |
| 68 | } |
| 69 | |
| 70 | /* override libssl RAND_bytes() to get testable crypto results */ |
| 71 | int RAND_bytes(uint8_t *rand, int len) |
| 72 | { |
| 73 | memset(rand, 23, len); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static void test_error() |
| 78 | { |
| 79 | int auth_action; |
| 80 | |
| 81 | struct gsm_auth_tuple atuple = {0}; |
| 82 | struct gsm_subscriber subscr = {0}; |
| 83 | int key_seq = 0; |
| 84 | |
| 85 | printf("\n* test_error()\n"); |
| 86 | |
| 87 | /* any error (except -ENOENT) */ |
| 88 | test_get_authinfo_rc = -EIO; |
| 89 | auth_action = auth_get_tuple_for_subscr_verbose(&atuple, &subscr, |
| 90 | key_seq); |
| 91 | OSMO_ASSERT(auth_action == -1); |
| 92 | } |
| 93 | |
| 94 | static void test_auth_not_avail() |
| 95 | { |
| 96 | int auth_action; |
| 97 | |
| 98 | struct gsm_auth_tuple atuple = {0}; |
| 99 | struct gsm_subscriber subscr = {0}; |
| 100 | int key_seq = 0; |
| 101 | |
| 102 | printf("\n* test_auth_not_avail()\n"); |
| 103 | |
| 104 | /* no entry */ |
| 105 | test_get_authinfo_rc = -ENOENT; |
| 106 | auth_action = auth_get_tuple_for_subscr_verbose(&atuple, &subscr, |
| 107 | key_seq); |
| 108 | OSMO_ASSERT(auth_action == AUTH_NOT_AVAIL); |
| 109 | } |
| 110 | |
| 111 | int main(void) |
| 112 | { |
| 113 | osmo_init_logging(&log_info); |
| 114 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 115 | |
| 116 | test_error(); |
| 117 | test_auth_not_avail(); |
| 118 | return 0; |
| 119 | } |