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:
Diffstat (limited to 'tests/unit/test_session.c')
-rw-r--r--tests/unit/test_session.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c
new file mode 100644
index 0000000..5b22a62
--- /dev/null
+++ b/tests/unit/test_session.c
@@ -0,0 +1,92 @@
1#include "test_framework.h"
2#include "../../main/session.h"
3#include "../../main/firewall.h"
4#include <string.h>
5#include <stdio.h>
6
7static uint32_t g_granted_ips[32];
8static int g_granted_count = 0;
9static uint32_t g_revoked_ips[32];
10static int g_revoked_count = 0;
11
12esp_err_t firewall_get_mac_for_ip(uint32_t ip, char *mac_out, size_t size) {
13 (void)ip;
14 snprintf(mac_out, size, "AA:BB:CC:DD:EE:FF");
15 return 0;
16}
17
18void firewall_grant_access(uint32_t ip) {
19 if (g_granted_count < 32) g_granted_ips[g_granted_count++] = ip;
20}
21
22void firewall_revoke_access(uint32_t ip) {
23 if (g_revoked_count < 32) g_revoked_ips[g_revoked_count++] = ip;
24}
25
26int main(void)
27{
28 printf("=== test_session ===\n");
29
30 g_granted_count = 0;
31 g_revoked_count = 0;
32
33 printf("\n--- session_manager_init ---\n");
34 esp_err_t ret = session_manager_init();
35 ASSERT_EQ_INT(0, ret, "session_manager_init succeeds");
36 ASSERT_EQ_INT(0, session_active_count(), "No sessions after init");
37
38 printf("\n--- session_create ---\n");
39 const char *secrets[] = {"secret1", "secret2"};
40 session_t *s = session_create(0x0A01A8C0, 60000, secrets, 2);
41 ASSERT(s != NULL, "session_create returns non-NULL");
42 ASSERT_EQ_INT(1, session_active_count(), "1 session after create");
43 ASSERT_EQ_INT(1, g_granted_count, "firewall_grant_access was called");
44
45 printf("\n--- session_find_by_ip ---\n");
46 session_t *found = session_find_by_ip(0x0A01A8C0);
47 ASSERT(found == s, "session_find_by_ip returns the created session");
48 ASSERT(session_find_by_ip(0x01020304) == NULL, "session_find_by_ip returns NULL for unknown IP");
49
50 printf("\n--- session_is_secret_spent ---\n");
51 ASSERT(session_is_secret_spent("secret1"), "secret1 is marked spent");
52 ASSERT(session_is_secret_spent("secret2"), "secret2 is marked spent");
53 ASSERT(!session_is_secret_spent("secret_unknown"), "unknown secret is not spent");
54
55 printf("\n--- Duplicate secret rejected ---\n");
56 const char *dup_secrets[] = {"secret1"};
57 g_granted_count = 0;
58 session_t *dup = session_create(0x0B01A8C0, 60000, dup_secrets, 1);
59 ASSERT(dup == NULL, "Duplicate secret returns NULL");
60 ASSERT_EQ_INT(0, g_granted_count, "No new firewall grant for duplicate");
61
62 printf("\n--- session_extend ---\n");
63 uint64_t old_allotment = s->allotment_ms;
64 session_extend(s, 30000);
65 ASSERT(s->allotment_ms == old_allotment + 30000, "Allotment extended by 30000ms");
66
67 printf("\n--- session_revoke ---\n");
68 g_revoked_count = 0;
69 session_revoke(s);
70 ASSERT_EQ_INT(1, g_revoked_count, "firewall_revoke_access was called");
71 ASSERT_EQ_INT(0, session_active_count(), "No active sessions after revoke");
72
73 printf("\n--- session_revoke_all ---\n");
74 const char *s1[] = {"s1"};
75 const char *s2[] = {"s2"};
76 session_create(0x01000001, 60000, s1, 1);
77 session_create(0x01000002, 60000, s2, 1);
78 ASSERT_EQ_INT(2, session_active_count(), "2 sessions created");
79
80 g_revoked_count = 0;
81 session_revoke_all();
82 ASSERT_EQ_INT(0, session_active_count(), "No sessions after revoke_all");
83
84 printf("\n--- session_tick does not crash ---\n");
85 session_manager_init();
86 const char *st[] = {"tick_secret"};
87 session_create(0x0A000001, 60000, st, 1);
88 session_tick();
89 ASSERT_EQ_INT(1, session_active_count(), "Session still active after tick (not expired)");
90
91 TEST_SUMMARY();
92}