diff options
Diffstat (limited to 'tests/unit/test_session.c')
| -rw-r--r-- | tests/unit/test_session.c | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c index 5b22a62..548be0d 100644 --- a/tests/unit/test_session.c +++ b/tests/unit/test_session.c | |||
| @@ -1,9 +1,17 @@ | |||
| 1 | #include "test_framework.h" | 1 | #include "test_framework.h" |
| 2 | #include "../../main/session.h" | 2 | #include "../../main/session.h" |
| 3 | #include "../../main/firewall.h" | 3 | #include "../../main/firewall.h" |
| 4 | #include "../../main/config.h" | ||
| 5 | #include "../../main/cashu.h" | ||
| 4 | #include <string.h> | 6 | #include <string.h> |
| 5 | #include <stdio.h> | 7 | #include <stdio.h> |
| 6 | 8 | ||
| 9 | static tollgate_config_t g_test_config; | ||
| 10 | |||
| 11 | const tollgate_config_t *tollgate_config_get(void) { | ||
| 12 | return &g_test_config; | ||
| 13 | } | ||
| 14 | |||
| 7 | static uint32_t g_granted_ips[32]; | 15 | static uint32_t g_granted_ips[32]; |
| 8 | static int g_granted_count = 0; | 16 | static int g_granted_count = 0; |
| 9 | static uint32_t g_revoked_ips[32]; | 17 | static uint32_t g_revoked_ips[32]; |
| @@ -23,9 +31,11 @@ void firewall_revoke_access(uint32_t ip) { | |||
| 23 | if (g_revoked_count < 32) g_revoked_ips[g_revoked_count++] = ip; | 31 | if (g_revoked_count < 32) g_revoked_ips[g_revoked_count++] = ip; |
| 24 | } | 32 | } |
| 25 | 33 | ||
| 26 | int main(void) | 34 | static void test_sessions(void) |
| 27 | { | 35 | { |
| 28 | printf("=== test_session ===\n"); | 36 | printf("=== test_session ===\n"); |
| 37 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 38 | strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); | ||
| 29 | 39 | ||
| 30 | g_granted_count = 0; | 40 | g_granted_count = 0; |
| 31 | g_revoked_count = 0; | 41 | g_revoked_count = 0; |
| @@ -87,6 +97,61 @@ int main(void) | |||
| 87 | session_create(0x0A000001, 60000, st, 1); | 97 | session_create(0x0A000001, 60000, st, 1); |
| 88 | session_tick(); | 98 | session_tick(); |
| 89 | ASSERT_EQ_INT(1, session_active_count(), "Session still active after tick (not expired)"); | 99 | ASSERT_EQ_INT(1, session_active_count(), "Session still active after tick (not expired)"); |
| 100 | } | ||
| 101 | |||
| 102 | void test_bytes_sessions(void) | ||
| 103 | { | ||
| 104 | printf("\n=== Bytes-based sessions ===\n"); | ||
| 105 | session_manager_init(); | ||
| 106 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 107 | strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1); | ||
| 108 | |||
| 109 | const char *sec[] = {"bytes_secret"}; | ||
| 110 | uint64_t allotment = 22020096; | ||
| 111 | session_t *s = session_create_bytes(0x0A010001, allotment, sec, 1); | ||
| 112 | ASSERT(s != NULL, "bytes session created"); | ||
| 113 | ASSERT_EQ_INT(1, session_active_count(), "1 active bytes session"); | ||
| 114 | |||
| 115 | ASSERT(!session_is_expired(s), "not expired at 0 consumed"); | ||
| 90 | 116 | ||
| 91 | TEST_SUMMARY(); | 117 | session_add_bytes(0x0A010001, 10000000); |
| 118 | ASSERT(!session_is_expired(s), "not expired at 10MB of 21MB"); | ||
| 119 | ASSERT_EQ_UINT64(10000000, s->bytes_consumed, "consumed 10MB"); | ||
| 120 | |||
| 121 | session_add_bytes(0x0A010001, 12200996); | ||
| 122 | ASSERT(session_is_expired(s), "expired after consuming all allotment"); | ||
| 123 | ASSERT_EQ_UINT64(22200996, s->bytes_consumed, "consumed 22.2MB"); | ||
| 124 | |||
| 125 | session_add_bytes(0x0A010001, 1000); | ||
| 126 | ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "consumption keeps growing past expiry"); | ||
| 127 | |||
| 128 | printf("\n--- Bytes session for unknown IP does nothing ---\n"); | ||
| 129 | session_add_bytes(0x0B0B0B0B, 9999); | ||
| 130 | ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "unknown IP no effect"); | ||
| 131 | |||
| 132 | printf("\n--- Mixed metric: milliseconds still works ---\n"); | ||
| 133 | session_manager_init(); | ||
| 134 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 135 | strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); | ||
| 136 | const char *ms_sec[] = {"ms_secret"}; | ||
| 137 | session_t *ms = session_create(0x0A020001, 60000, ms_sec, 1); | ||
| 138 | ASSERT(ms != NULL, "ms session created"); | ||
| 139 | ASSERT(!session_is_expired(ms), "ms session not expired immediately"); | ||
| 140 | |||
| 141 | printf("\n--- cashu_calculate_allotment dispatch ---\n"); | ||
| 142 | uint64_t a = cashu_calculate_allotment(21, 21, "milliseconds", 60000); | ||
| 143 | ASSERT_EQ_UINT64(60000, a, "21 sats / 21 per step * 60000ms = 60000ms"); | ||
| 144 | a = cashu_calculate_allotment(42, 21, "bytes", 22020096); | ||
| 145 | ASSERT_EQ_UINT64(44040192, a, "42 sats / 21 per step * 21MB = 42MB"); | ||
| 146 | a = cashu_calculate_allotment(10, 21, "bytes", 22020096); | ||
| 147 | ASSERT_EQ_UINT64(0, a, "10 sats < 21 per step = 0 allotment"); | ||
| 148 | |||
| 149 | printf("\n=== ALL BYTES SESSION TESTS PASSED ===\n"); | ||
| 150 | } | ||
| 151 | |||
| 152 | int main(void) | ||
| 153 | { | ||
| 154 | test_sessions(); | ||
| 155 | test_bytes_sessions(); | ||
| 156 | return g_tests_failed > 0 ? 1 : 0; | ||
| 92 | } | 157 | } |