|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 wolfSSL Inc. |
| 3 | + * |
| 4 | + * This file is part of wolfHSM. |
| 5 | + * |
| 6 | + * wolfHSM is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * wolfHSM is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +/* |
| 20 | + * test/wh_test_timeout.c |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#include "wolfhsm/wh_settings.h" |
| 27 | +#include "wolfhsm/wh_timeout.h" |
| 28 | +#include "wolfhsm/wh_error.h" |
| 29 | + |
| 30 | +#include "wh_test_common.h" |
| 31 | +#include "wh_test_timeout.h" |
| 32 | + |
| 33 | +static void whTest_TimeoutCb(void* ctx) |
| 34 | +{ |
| 35 | + int* counter = (int*)ctx; |
| 36 | + if (counter != NULL) { |
| 37 | + (*counter)++; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int whTest_Timeout(void) |
| 42 | +{ |
| 43 | + int cb_count = 0; |
| 44 | + whTimeoutConfig cfg; |
| 45 | + whTimeoutCtx timeout[1]; |
| 46 | + |
| 47 | + cfg.timeoutUs = 1; |
| 48 | + cfg.expiredCb = whTest_TimeoutCb; |
| 49 | + cfg.cbCtx = &cb_count; |
| 50 | + |
| 51 | + wh_Timeout_Init(timeout, &cfg); |
| 52 | + WH_TEST_ASSERT_RETURN(timeout->startUs == 0); |
| 53 | + WH_TEST_ASSERT_RETURN(timeout->timeoutUs == cfg.timeoutUs); |
| 54 | + WH_TEST_ASSERT_RETURN(timeout->expiredCb == cfg.expiredCb); |
| 55 | + WH_TEST_ASSERT_RETURN(timeout->cbCtx == cfg.cbCtx); |
| 56 | + |
| 57 | + wh_Timeout_Start(timeout); |
| 58 | + WH_TEST_ASSERT_RETURN(timeout->timeoutUs > 0); |
| 59 | + |
| 60 | + wh_Timeout_Stop(timeout); |
| 61 | + WH_TEST_ASSERT_RETURN(timeout->startUs == 0); |
| 62 | + WH_TEST_ASSERT_RETURN(timeout->timeoutUs == 0); |
| 63 | + |
| 64 | + /* No expiration when disabled */ |
| 65 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Expired(timeout) == 0); |
| 66 | + |
| 67 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Init(0, 0) == WH_ERROR_BADARGS); |
| 68 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Set(0, 0) == WH_ERROR_BADARGS); |
| 69 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Start(0) == WH_ERROR_BADARGS); |
| 70 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Stop(0) == WH_ERROR_BADARGS); |
| 71 | + WH_TEST_ASSERT_RETURN(wh_Timeout_Expired(0) == 0); |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
0 commit comments