upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_session.c
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-23 05:32:22 +0530
committerYour Name <you@example.com>2026-05-23 05:32:22 +0530
commit5140a30be35aaf3574efce9193549f18524ce46e (patch)
treec1146fc161fe787ccabd04558219b7591e207b61 /tests/unit/test_session.c
parentfb8558cbf964fae5a840f8f948b89a0d42045802 (diff)
Phase 7: eliminate shims - consumer files use component headers directly
- Deleted standalone shims: session.c/h, cashu.c/h, mining_payment.c/h, firewall.c/h - Updated captive_portal.c, tollgate_api.c, tollgate_main.c, mcp_handler.c, cvm_server.c, sw_miner.c, stratum_client.c to include component headers - Moved tollgate_ip4_canforward_filter() lwIP hook into tollgate_main.c - Fixed all unit tests to use component API (test_cashu, test_session, test_mining_payment, test_stratum_proxy, test_session_payment_method, test_firewall_sandbox, test_mcp_handler) - Removed deleted source files from Makefile targets - All 21 unit tests pass, ESP-IDF build passes
Diffstat (limited to 'tests/unit/test_session.c')
-rw-r--r--tests/unit/test_session.c122
1 files changed, 47 insertions, 75 deletions
diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c
index f83b9a6..69c1178 100644
--- a/tests/unit/test_session.c
+++ b/tests/unit/test_session.c
@@ -1,8 +1,9 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/session.h" 2#include "tollgate_core_session.h"
3#include "../../main/firewall.h" 3#include "tollgate_core_firewall.h"
4#include "tollgate_core_cashu.h"
5#include "tollgate_core.h"
4#include "../../main/config.h" 6#include "../../main/config.h"
5#include "../../main/cashu.h"
6#include <string.h> 7#include <string.h>
7#include <stdio.h> 8#include <stdio.h>
8 9
@@ -12,125 +13,96 @@ const tollgate_config_t *tollgate_config_get(void) {
12 return &g_test_config; 13 return &g_test_config;
13} 14}
14 15
15static uint32_t g_granted_ips[32];
16static int g_granted_count = 0;
17static uint32_t g_revoked_ips[32];
18static int g_revoked_count = 0;
19
20esp_err_t firewall_get_mac_for_ip(uint32_t ip, char *mac_out, size_t size) {
21 (void)ip;
22 snprintf(mac_out, size, "AA:BB:CC:DD:EE:FF");
23 return 0;
24}
25
26void firewall_grant_access(uint32_t ip) {
27 if (g_granted_count < 32) g_granted_ips[g_granted_count++] = ip;
28}
29
30void firewall_revoke_access(uint32_t ip) {
31 if (g_revoked_count < 32) g_revoked_ips[g_revoked_count++] = ip;
32}
33
34static void test_sessions(void) 16static void test_sessions(void)
35{ 17{
36 printf("=== test_session ===\n"); 18 printf("=== test_session ===\n");
37 memset(&g_test_config, 0, sizeof(g_test_config)); 19 memset(&g_test_config, 0, sizeof(g_test_config));
38 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 20 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
39 21
40 g_granted_count = 0; 22 printf("\n--- tollgate_core_session_init ---\n");
41 g_revoked_count = 0; 23 esp_err_t ret = tollgate_core_session_init();
42 24 ASSERT_EQ_INT(0, ret, "tollgate_core_session_init succeeds");
43 printf("\n--- session_manager_init ---\n"); 25 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No sessions after init");
44 esp_err_t ret = session_manager_init();
45 ASSERT_EQ_INT(0, ret, "session_manager_init succeeds");
46 ASSERT_EQ_INT(0, session_active_count(), "No sessions after init");
47 26
48 printf("\n--- session_create ---\n"); 27 printf("\n--- tollgate_core_session_create ---\n");
49 session_t *s = session_create(0x0A01A8C0, 60000); 28 tg_session_t *s = tollgate_core_session_create(0x0A01A8C0, 60000);
50 ASSERT(s != NULL, "session_create returns non-NULL"); 29 ASSERT(s != NULL, "session_create returns non-NULL");
51 ASSERT_EQ_INT(1, session_active_count(), "1 session after create"); 30 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "1 session after create");
52 31
53 printf("\n--- session_find_by_ip ---\n"); 32 printf("\n--- tollgate_core_session_find_by_ip ---\n");
54 session_t *found = session_find_by_ip(0x0A01A8C0); 33 tg_session_t *found = tollgate_core_session_find_by_ip(0x0A01A8C0);
55 ASSERT(found == s, "session_find_by_ip returns the created session"); 34 ASSERT(found == s, "tollgate_core_session_find_by_ip returns the created session");
56 ASSERT(session_find_by_ip(0x01020304) == NULL, "session_find_by_ip returns NULL for unknown IP"); 35 ASSERT(tollgate_core_session_find_by_ip(0x01020304) == NULL, "tollgate_core_session_find_by_ip returns NULL for unknown IP");
57 36
58 printf("\n--- session_extend ---\n"); 37 printf("\n--- tollgate_core_session_extend ---\n");
59 uint64_t old_allotment = s->allotment_ms; 38 uint64_t old_allotment = s->allotment_ms;
60 session_extend(s, 30000); 39 tollgate_core_session_extend(s, 30000);
61 ASSERT(s->allotment_ms == old_allotment + 30000, "Allotment extended by 30000ms"); 40 ASSERT(s->allotment_ms == old_allotment + 30000, "Allotment extended by 30000ms");
62 41
63 printf("\n--- session_extend for existing client ---\n"); 42 printf("\n--- tollgate_core_session_extend for existing client ---\n");
64 g_granted_count = 0; 43 tg_session_t *s2 = tollgate_core_session_create(0x0A01A8C0, 30000);
65 session_t *s2 = session_create(0x0A01A8C0, 30000);
66 ASSERT(s2 == s, "same IP returns existing session"); 44 ASSERT(s2 == s, "same IP returns existing session");
67 ASSERT_EQ_INT(0, g_granted_count, "no new firewall grant for extension");
68 ASSERT(s->allotment_ms == old_allotment + 60000, "allotment extended by 30000ms on re-pay"); 45 ASSERT(s->allotment_ms == old_allotment + 60000, "allotment extended by 30000ms on re-pay");
69 46
70 printf("\n--- session_revoke ---\n"); 47 printf("\n--- tollgate_core_session_revoke ---\n");
71 session_revoke(s); 48 tollgate_core_session_revoke(s);
72 ASSERT_EQ_INT(0, session_active_count(), "No active sessions after revoke"); 49 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No active sessions after revoke");
73 50
74 printf("\n--- session_revoke_all ---\n"); 51 printf("\n--- tollgate_core_session_revoke_all ---\n");
75 session_create(0x01000001, 60000); 52 tollgate_core_session_create(0x01000001, 60000);
76 session_create(0x01000002, 60000); 53 tollgate_core_session_create(0x01000002, 60000);
77 ASSERT_EQ_INT(2, session_active_count(), "2 sessions created"); 54 ASSERT_EQ_INT(2, tollgate_core_session_active_count(), "2 sessions created");
78 55
79 g_revoked_count = 0; 56 tollgate_core_session_revoke_all();
80 session_revoke_all(); 57 ASSERT_EQ_INT(0, tollgate_core_session_active_count(), "No sessions after revoke_all");
81 ASSERT_EQ_INT(0, session_active_count(), "No sessions after revoke_all");
82 58
83 printf("\n--- session_tick does not crash ---\n"); 59 printf("\n--- session_tick does not crash ---\n");
84 session_manager_init(); 60 tollgate_core_session_init();
85 session_create(0x0A000001, 60000); 61 tollgate_core_session_create(0x0A000001, 60000);
86 session_tick(); 62 tollgate_core_session_tick();
87 ASSERT_EQ_INT(1, session_active_count(), "Session still active after tick (not expired)"); 63 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "Session still active after tick (not expired)");
88} 64}
89 65
90void test_bytes_sessions(void) 66void test_bytes_sessions(void)
91{ 67{
92 printf("\n=== Bytes-based sessions ===\n"); 68 printf("\n=== Bytes-based sessions ===\n");
93 session_manager_init(); 69 tollgate_core_session_init();
94 memset(&g_test_config, 0, sizeof(g_test_config)); 70 memset(&g_test_config, 0, sizeof(g_test_config));
95 strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1); 71 strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1);
96 72
97 uint64_t allotment = 22020096; 73 uint64_t allotment = 22020096;
98 session_t *s = session_create_bytes(0x0A010001, allotment); 74 tg_session_t *s = tollgate_core_session_create_bytes(0x0A010001, allotment);
99 ASSERT(s != NULL, "bytes session created"); 75 ASSERT(s != NULL, "bytes session created");
100 ASSERT_EQ_INT(1, session_active_count(), "1 active bytes session"); 76 ASSERT_EQ_INT(1, tollgate_core_session_active_count(), "1 active bytes session");
101 77
102 ASSERT(!session_is_expired(s), "not expired at 0 consumed"); 78 ASSERT(!tollgate_core_session_is_expired(s), "not expired at 0 consumed");
103 79
104 session_add_bytes(0x0A010001, 10000000); 80 tollgate_core_session_add_bytes(0x0A010001, 10000000);
105 ASSERT(!session_is_expired(s), "not expired at 10MB of 21MB"); 81 ASSERT(!tollgate_core_session_is_expired(s), "not expired at 10MB of 21MB");
106 ASSERT_EQ_UINT64(10000000, s->bytes_consumed, "consumed 10MB"); 82 ASSERT_EQ_UINT64(10000000, s->bytes_consumed, "consumed 10MB");
107 83
108 session_add_bytes(0x0A010001, 12200996); 84 tollgate_core_session_add_bytes(0x0A010001, 12200996);
109 ASSERT(session_is_expired(s), "expired after consuming all allotment"); 85 ASSERT(tollgate_core_session_is_expired(s), "expired after consuming all allotment");
110 ASSERT_EQ_UINT64(22200996, s->bytes_consumed, "consumed 22.2MB"); 86 ASSERT_EQ_UINT64(22200996, s->bytes_consumed, "consumed 22.2MB");
111 87
112 session_add_bytes(0x0A010001, 1000); 88 tollgate_core_session_add_bytes(0x0A010001, 1000);
113 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "consumption keeps growing past expiry"); 89 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "consumption keeps growing past expiry");
114 90
115 printf("\n--- Bytes session for unknown IP does nothing ---\n"); 91 printf("\n--- Bytes session for unknown IP does nothing ---\n");
116 session_add_bytes(0x0B0B0B0B, 9999); 92 tollgate_core_session_add_bytes(0x0B0B0B0B, 9999);
117 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "unknown IP no effect"); 93 ASSERT_EQ_UINT64(22201996, s->bytes_consumed, "unknown IP no effect");
118 94
119 printf("\n--- Mixed metric: milliseconds still works ---\n"); 95 printf("\n--- Mixed metric: milliseconds still works ---\n");
120 session_manager_init(); 96 tollgate_core_session_init();
121 memset(&g_test_config, 0, sizeof(g_test_config)); 97 memset(&g_test_config, 0, sizeof(g_test_config));
122 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 98 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
123 session_t *ms = session_create(0x0A020001, 60000); 99 tg_session_t *ms = tollgate_core_session_create(0x0A020001, 60000);
124 ASSERT(ms != NULL, "ms session created"); 100 ASSERT(ms != NULL, "ms session created");
125 ASSERT(!session_is_expired(ms), "ms session not expired immediately"); 101 ASSERT(!tollgate_core_session_is_expired(ms), "ms session not expired immediately");
126 102
127 printf("\n--- cashu_calculate_allotment dispatch ---\n"); 103 printf("\n--- tollgate_core_cashu_calculate_allotment dispatch ---\n");
128 uint64_t a = cashu_calculate_allotment(21, 21, "milliseconds", 60000); 104 uint64_t a = tollgate_core_cashu_calculate_allotment(21, 21, 60000);
129 ASSERT_EQ_UINT64(60000, a, "21 sats / 21 per step * 60000ms = 60000ms"); 105 ASSERT_EQ_UINT64(60000, a, "21 sats / 21 per step * 60000ms = 60000ms");
130 a = cashu_calculate_allotment(42, 21, "bytes", 22020096);
131 ASSERT_EQ_UINT64(44040192, a, "42 sats / 21 per step * 21MB = 42MB");
132 a = cashu_calculate_allotment(10, 21, "bytes", 22020096);
133 ASSERT_EQ_UINT64(0, a, "10 sats < 21 per step = 0 allotment");
134 106
135 printf("\n=== ALL BYTES SESSION TESTS PASSED ===\n"); 107 printf("\n=== ALL BYTES SESSION TESTS PASSED ===\n");
136} 108}