diff options
Diffstat (limited to 'tests/unit/test_cashu.c')
| -rw-r--r-- | tests/unit/test_cashu.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/unit/test_cashu.c b/tests/unit/test_cashu.c new file mode 100644 index 0000000..cec8e08 --- /dev/null +++ b/tests/unit/test_cashu.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/cashu.h" | ||
| 3 | #include "../../main/config.h" | ||
| 4 | #include <string.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | |||
| 8 | static tollgate_config_t g_test_config; | ||
| 9 | |||
| 10 | const tollgate_config_t *tollgate_config_get(void) { | ||
| 11 | return &g_test_config; | ||
| 12 | } | ||
| 13 | |||
| 14 | int main(void) | ||
| 15 | { | ||
| 16 | printf("=== test_cashu ===\n"); | ||
| 17 | |||
| 18 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 19 | strncpy(g_test_config.mint_url, "https://testnut.cashu.space", sizeof(g_test_config.mint_url) - 1); | ||
| 20 | g_test_config.price_per_step = 21; | ||
| 21 | g_test_config.step_size_ms = 60000; | ||
| 22 | |||
| 23 | printf("\n--- cashu_calculate_allotment_ms ---\n"); | ||
| 24 | uint64_t a1 = cashu_calculate_allotment_ms(21, 21, 60000); | ||
| 25 | ASSERT_EQ_INT(60000, (int)a1, "21 sats at 21 sats/min = 60000ms"); | ||
| 26 | |||
| 27 | uint64_t a2 = cashu_calculate_allotment_ms(42, 21, 60000); | ||
| 28 | ASSERT_EQ_INT(120000, (int)a2, "42 sats at 21 sats/min = 120000ms"); | ||
| 29 | |||
| 30 | uint64_t a3 = cashu_calculate_allotment_ms(1, 21, 60000); | ||
| 31 | ASSERT_EQ_INT(0, (int)a3, "1 sat at 21 sats/min = 0ms (rounds down)"); | ||
| 32 | |||
| 33 | uint64_t a4 = cashu_calculate_allotment_ms(100, 10, 30000); | ||
| 34 | ASSERT_EQ_INT(300000, (int)a4, "100 sats at 10 sats/30s = 300000ms"); | ||
| 35 | |||
| 36 | printf("\n--- cashu_is_mint_accepted ---\n"); | ||
| 37 | ASSERT(cashu_is_mint_accepted("https://testnut.cashu.space"), "testnut.cashu.space accepted"); | ||
| 38 | ASSERT(!cashu_is_mint_accepted("https://evil.mint.example.com"), "evil mint rejected"); | ||
| 39 | ASSERT(!cashu_is_mint_accepted(""), "empty string rejected"); | ||
| 40 | |||
| 41 | printf("\n--- cashu_decode_token with garbage ---\n"); | ||
| 42 | cashu_token_t token; | ||
| 43 | memset(&token, 0, sizeof(token)); | ||
| 44 | esp_err_t ret = cashu_decode_token("garbage", &token); | ||
| 45 | ASSERT(ret != ESP_OK, "Garbage input returns error"); | ||
| 46 | |||
| 47 | ret = cashu_decode_token("", &token); | ||
| 48 | ASSERT(ret != ESP_OK, "Empty string returns error"); | ||
| 49 | |||
| 50 | ret = cashu_decode_token("cashuA!!invalid-base64!!", &token); | ||
| 51 | ASSERT(ret != ESP_OK, "Invalid base64url returns error"); | ||
| 52 | |||
| 53 | TEST_SUMMARY(); | ||
| 54 | } | ||