upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests
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
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')
-rw-r--r--tests/unit/Makefile16
-rw-r--r--tests/unit/test_cashu.c38
-rwxr-xr-xtests/unit/test_firewall_sandboxbin111016 -> 108304 bytes
-rw-r--r--tests/unit/test_firewall_sandbox.c75
-rw-r--r--tests/unit/test_mcp_handler.c11
-rwxr-xr-xtests/unit/test_mining_paymentbin111000 -> 108304 bytes
-rw-r--r--tests/unit/test_mining_payment.c68
-rw-r--r--tests/unit/test_session.c122
-rwxr-xr-xtests/unit/test_session_payment_methodbin117792 -> 110536 bytes
-rw-r--r--tests/unit/test_session_payment_method.c67
-rwxr-xr-xtests/unit/test_stratum_proxybin113304 -> 110600 bytes
-rw-r--r--tests/unit/test_stratum_proxy.c6
12 files changed, 179 insertions, 224 deletions
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index 5fdc315..3890243 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -61,12 +61,12 @@ test_identity: test_identity.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ)
61test_nostr_event: test_nostr_event.c $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) 61test_nostr_event: test_nostr_event.c $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ)
62 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS) 62 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS)
63 63
64test_cashu: test_cashu.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 64test_cashu: test_cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
65 $(CC) $(CFLAGS) -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h $^ -o $@ $(LDFLAGS) 65 $(CC) $(CFLAGS) -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h $^ -o $@ $(LDFLAGS)
66 66
67TG_CORE_OBJS_CFLAGS := -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h -include stubs/esp_crt_bundle.h 67TG_CORE_OBJS_CFLAGS := -include stubs/mint_health.h -include stubs/esp_netif.h -include stubs/esp_log.h -include stubs/esp_http_client.h -include stubs/esp_crt_bundle.h -include stubs/esp_crt_bundle.h
68 68
69test_session: test_session.c $(REPO_ROOT)/main/session.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 69test_session: test_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
70 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 70 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
71 71
72test_tollgate_client: test_tollgate_client.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c 72test_tollgate_client: test_tollgate_client.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c
@@ -79,7 +79,7 @@ test_lightning_payout: test_lightning_payout.c
79 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 79 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
80 80
81test_mcp_handler: test_mcp_handler.c $(REPO_ROOT)/main/mcp_handler.c 81test_mcp_handler: test_mcp_handler.c $(REPO_ROOT)/main/mcp_handler.c
82 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/mcp_handler.c -o $@ $(LDFLAGS) 82 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main -I $(TG_CORE_INC) -I $(TG_CORE_INC)/.. $< $(REPO_ROOT)/main/mcp_handler.c -o $@ $(LDFLAGS)
83 83
84test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) 84test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ)
85 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS) 85 $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) $< $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS)
@@ -96,19 +96,19 @@ test_beacon_price: test_beacon_price.c $(REPO_ROOT)/main/beacon_price.c
96test_market: test_market.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c 96test_market: test_market.c $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c
97 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c -o $@ $(LDFLAGS) 97 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $< $(REPO_ROOT)/main/market.c $(REPO_ROOT)/main/beacon_price.c -o $@ $(LDFLAGS)
98 98
99test_mining_payment: test_mining_payment.c $(REPO_ROOT)/main/mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 99test_mining_payment: test_mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
100 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 100 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
101 101
102test_stratum_proxy: test_stratum_proxy.c $(REPO_ROOT)/main/stratum_proxy.c $(REPO_ROOT)/main/mining_payment.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 102test_stratum_proxy: test_stratum_proxy.c $(REPO_ROOT)/main/stratum_proxy.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
103 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 103 $(CC) $(CFLAGS) -I $(REPO_ROOT)/main $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
104 104
105test_session_payment_method: test_session_payment_method.c $(REPO_ROOT)/main/session.c $(REPO_ROOT)/main/cashu.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 105test_session_payment_method: test_session_payment_method.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
106 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 106 $(CC) $(CFLAGS) $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
107 107
108test_tollgate_client_mining: test_tollgate_client_mining.c 108test_tollgate_client_mining: test_tollgate_client_mining.c
109 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) 109 $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
110 110
111test_firewall_sandbox: test_firewall_sandbox.c $(REPO_ROOT)/main/firewall.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c 111test_firewall_sandbox: test_firewall_sandbox.c $(TG_CORE_SRC)/tollgate_core_firewall.c $(TG_CORE_SRC)/tollgate_core.c $(TG_CORE_SRC)/tollgate_core_session.c $(TG_CORE_SRC)/tollgate_core_cashu.c $(TG_CORE_SRC)/tollgate_core_mining.c $(TG_CORE_SRC)/tollgate_core_dns.c $(TG_CORE_SRC)/tollgate_core_stratum_proxy.c
112 $(CC) $(CFLAGS) -include stubs/dns_server.h $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS) 112 $(CC) $(CFLAGS) -include stubs/dns_server.h $(TG_CORE_OBJS_CFLAGS) $^ -o $@ $(LDFLAGS)
113 113
114test_relay_selector: test_relay_selector.c 114test_relay_selector: test_relay_selector.c
diff --git a/tests/unit/test_cashu.c b/tests/unit/test_cashu.c
index 021d1b7..2b86a34 100644
--- a/tests/unit/test_cashu.c
+++ b/tests/unit/test_cashu.c
@@ -1,5 +1,5 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/cashu.h" 2#include "tollgate_core_cashu.h"
3#include "../../main/config.h" 3#include "../../main/config.h"
4#include <string.h> 4#include <string.h>
5#include <stdio.h> 5#include <stdio.h>
@@ -32,38 +32,38 @@ int main(void)
32 } 32 }
33 g_test_config.accepted_mint_count = 4; 33 g_test_config.accepted_mint_count = 4;
34 34
35 printf("\n--- cashu_calculate_allotment_ms ---\n"); 35 printf("\n--- tollgate_core_cashu_calculate_allotment ---\n");
36 uint64_t a1 = cashu_calculate_allotment_ms(21, 21, 60000); 36 uint64_t a1 = tollgate_core_cashu_calculate_allotment(21, 21, 60000);
37 ASSERT_EQ_INT(60000, (int)a1, "21 sats at 21 sats/min = 60000ms"); 37 ASSERT_EQ_INT(60000, (int)a1, "21 sats at 21 sats/min = 60000ms");
38 38
39 uint64_t a2 = cashu_calculate_allotment_ms(42, 21, 60000); 39 uint64_t a2 = tollgate_core_cashu_calculate_allotment(42, 21, 60000);
40 ASSERT_EQ_INT(120000, (int)a2, "42 sats at 21 sats/min = 120000ms"); 40 ASSERT_EQ_INT(120000, (int)a2, "42 sats at 21 sats/min = 120000ms");
41 41
42 uint64_t a3 = cashu_calculate_allotment_ms(1, 21, 60000); 42 uint64_t a3 = tollgate_core_cashu_calculate_allotment(1, 21, 60000);
43 ASSERT_EQ_INT(0, (int)a3, "1 sat at 21 sats/min = 0ms (rounds down)"); 43 ASSERT_EQ_INT(0, (int)a3, "1 sat at 21 sats/min = 0ms (rounds down)");
44 44
45 uint64_t a4 = cashu_calculate_allotment_ms(100, 10, 30000); 45 uint64_t a4 = tollgate_core_cashu_calculate_allotment(100, 10, 30000);
46 ASSERT_EQ_INT(300000, (int)a4, "100 sats at 10 sats/30s = 300000ms"); 46 ASSERT_EQ_INT(300000, (int)a4, "100 sats at 10 sats/30s = 300000ms");
47 47
48 printf("\n--- cashu_is_mint_accepted (multi-mint) ---\n"); 48 printf("\n--- tollgate_core_cashu_is_mint_accepted (multi-mint) ---\n");
49 ASSERT(cashu_is_mint_accepted("https://testnut.cashu.space"), "testnut.cashu.space accepted"); 49 ASSERT(tollgate_core_cashu_is_mint_accepted("https://testnut.cashu.space", "https://testnut.cashu.space"), "testnut.cashu.space accepted");
50 ASSERT(cashu_is_mint_accepted("https://mint.minibits.cash/Bitcoin"), "minibits accepted"); 50 ASSERT(tollgate_core_cashu_is_mint_accepted("https://mint.minibits.cash/Bitcoin", "https://mint.minibits.cash/Bitcoin"), "minibits accepted");
51 ASSERT(cashu_is_mint_accepted("https://mint.coinos.io"), "coinos accepted"); 51 ASSERT(tollgate_core_cashu_is_mint_accepted("https://mint.coinos.io", "https://mint.coinos.io"), "coinos accepted");
52 ASSERT(cashu_is_mint_accepted("https://21mint.me"), "21mint accepted"); 52 ASSERT(tollgate_core_cashu_is_mint_accepted("https://21mint.me", "https://21mint.me"), "21mint accepted");
53 ASSERT(!cashu_is_mint_accepted("https://evil.mint.example.com"), "evil mint rejected"); 53 ASSERT(!tollgate_core_cashu_is_mint_accepted("https://evil.mint.example.com", "https://testnut.cashu.space"), "evil mint rejected");
54 ASSERT(!cashu_is_mint_accepted(""), "empty string rejected"); 54 ASSERT(!tollgate_core_cashu_is_mint_accepted("", "https://testnut.cashu.space"), "empty string rejected");
55 ASSERT(!cashu_is_mint_accepted(NULL), "NULL rejected"); 55 ASSERT(!tollgate_core_cashu_is_mint_accepted(NULL, "https://testnut.cashu.space"), "NULL rejected");
56 56
57 printf("\n--- cashu_decode_token with garbage ---\n"); 57 printf("\n--- tollgate_core_cashu_decode_token with garbage ---\n");
58 cashu_token_t token; 58 tg_cashu_token_t token;
59 memset(&token, 0, sizeof(token)); 59 memset(&token, 0, sizeof(token));
60 esp_err_t ret = cashu_decode_token("garbage", &token); 60 esp_err_t ret = tollgate_core_cashu_decode_token("garbage", &token);
61 ASSERT(ret != ESP_OK, "Garbage input returns error"); 61 ASSERT(ret != ESP_OK, "Garbage input returns error");
62 62
63 ret = cashu_decode_token("", &token); 63 ret = tollgate_core_cashu_decode_token("", &token);
64 ASSERT(ret != ESP_OK, "Empty string returns error"); 64 ASSERT(ret != ESP_OK, "Empty string returns error");
65 65
66 ret = cashu_decode_token("cashuA!!invalid-base64!!", &token); 66 ret = tollgate_core_cashu_decode_token("cashuA!!invalid-base64!!", &token);
67 ASSERT(ret != ESP_OK, "Invalid base64url returns error"); 67 ASSERT(ret != ESP_OK, "Invalid base64url returns error");
68 68
69 TEST_SUMMARY(); 69 TEST_SUMMARY();
diff --git a/tests/unit/test_firewall_sandbox b/tests/unit/test_firewall_sandbox
index 0c3aa59..9da996d 100755
--- a/tests/unit/test_firewall_sandbox
+++ b/tests/unit/test_firewall_sandbox
Binary files differ
diff --git a/tests/unit/test_firewall_sandbox.c b/tests/unit/test_firewall_sandbox.c
index 66a491b..14025ac 100644
--- a/tests/unit/test_firewall_sandbox.c
+++ b/tests/unit/test_firewall_sandbox.c
@@ -1,5 +1,6 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/firewall.h" 2#include "tollgate_core_firewall.h"
3#include "tollgate_core.h"
3#include <stdio.h> 4#include <stdio.h>
4#include <string.h> 5#include <string.h>
5 6
@@ -7,9 +8,9 @@ int main(void)
7{ 8{
8 printf("=== test_firewall_sandbox ===\n"); 9 printf("=== test_firewall_sandbox ===\n");
9 10
10 printf("\n--- FW_MAX_MAC_LEN is 18 ---\n"); 11 printf("\n--- TG_FW_MAX_MAC_LEN is 18 ---\n");
11 { 12 {
12 ASSERT_EQ_INT(18, FW_MAX_MAC_LEN, "MAC length is 18 (17 chars + null)"); 13 ASSERT_EQ_INT(18, TG_FW_MAX_MAC_LEN, "MAC length is 18 (17 chars + null)");
13 } 14 }
14 15
15 printf("\n--- esp_ip4_addr_t available ---\n"); 16 printf("\n--- esp_ip4_addr_t available ---\n");
@@ -19,75 +20,75 @@ int main(void)
19 ASSERT(ip.addr == 0x0102A8C0, "ip4_addr stores value"); 20 ASSERT(ip.addr == 0x0102A8C0, "ip4_addr stores value");
20 } 21 }
21 22
22 printf("\n--- firewall_set_mining_port / set_sandbox_mint_access compile ---\n"); 23 printf("\n--- tollgate_core_fw_set_sandbox_ports / set_sandbox_mint_access compile ---\n");
23 { 24 {
24 firewall_set_mining_port(3333); 25 tollgate_core_fw_set_sandbox_ports(3333);
25 firewall_set_mining_port(4033); 26 tollgate_core_fw_set_sandbox_ports(4033);
26 firewall_set_sandbox_mint_access(true); 27 tollgate_core_fw_set_sandbox_mint_access(true);
27 firewall_set_sandbox_mint_access(false); 28 tollgate_core_fw_set_sandbox_mint_access(false);
28 ASSERT(true, "setters compile and run without crash"); 29 ASSERT(true, "setters compile and run without crash");
29 } 30 }
30 31
31 printf("\n--- firewall_init + client management ---\n"); 32 printf("\n--- tollgate_core_fw_init + client management ---\n");
32 { 33 {
33 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 34 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
34 esp_err_t ret = firewall_init(ap_ip); 35 esp_err_t ret = tollgate_core_fw_init(ap_ip);
35 ASSERT_EQ_INT(ESP_OK, (int)ret, "firewall_init succeeds"); 36 ASSERT_EQ_INT(ESP_OK, (int)ret, "tollgate_core_fw_init succeeds");
36 ASSERT_EQ_INT(0, firewall_client_count(), "no clients after init"); 37 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "no clients after init");
37 38
38 firewall_grant_access(0x0201A8C0); 39 tollgate_core_fw_grant(0x0201A8C0);
39 ASSERT_EQ_INT(1, firewall_client_count(), "1 client after grant"); 40 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "1 client after grant");
40 ASSERT(firewall_is_client_allowed(0x0201A8C0), "client is allowed"); 41 ASSERT(tollgate_core_fw_is_allowed(0x0201A8C0), "client is allowed");
41 42
42 firewall_revoke_access(0x0201A8C0); 43 tollgate_core_fw_revoke(0x0201A8C0);
43 ASSERT_EQ_INT(0, firewall_client_count(), "0 clients after revoke"); 44 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "0 clients after revoke");
44 ASSERT(!firewall_is_client_allowed(0x0201A8C0), "client not allowed after revoke"); 45 ASSERT(!tollgate_core_fw_is_allowed(0x0201A8C0), "client not allowed after revoke");
45 } 46 }
46 47
47 printf("\n--- grant same IP twice ---\n"); 48 printf("\n--- grant same IP twice ---\n");
48 { 49 {
49 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 50 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
50 firewall_init(ap_ip); 51 tollgate_core_fw_init(ap_ip);
51 52
52 firewall_grant_access(0x0301A8C0); 53 tollgate_core_fw_grant(0x0301A8C0);
53 firewall_grant_access(0x0301A8C0); 54 tollgate_core_fw_grant(0x0301A8C0);
54 ASSERT_EQ_INT(1, firewall_client_count(), "duplicate grant does not double count"); 55 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "duplicate grant does not double count");
55 } 56 }
56 57
57 printf("\n--- revoke non-existent ---\n"); 58 printf("\n--- revoke non-existent ---\n");
58 { 59 {
59 firewall_revoke_access(0x99999999); 60 tollgate_core_fw_revoke(0x99999999);
60 ASSERT_EQ_INT(1, firewall_client_count(), "revoke non-existent no effect"); 61 ASSERT_EQ_INT(1, tollgate_core_fw_client_count(), "revoke non-existent no effect");
61 } 62 }
62 63
63 printf("\n--- revoke_all ---\n"); 64 printf("\n--- revoke_all ---\n");
64 { 65 {
65 firewall_grant_access(0x0401A8C0); 66 tollgate_core_fw_grant(0x0401A8C0);
66 firewall_grant_access(0x0501A8C0); 67 tollgate_core_fw_grant(0x0501A8C0);
67 ASSERT_EQ_INT(3, firewall_client_count(), "3 clients"); 68 ASSERT_EQ_INT(3, tollgate_core_fw_client_count(), "3 clients");
68 firewall_revoke_all(); 69 tollgate_core_fw_revoke_all();
69 ASSERT_EQ_INT(0, firewall_client_count(), "0 after revoke_all"); 70 ASSERT_EQ_INT(0, tollgate_core_fw_client_count(), "0 after revoke_all");
70 } 71 }
71 72
72 printf("\n--- max clients (10) ---\n"); 73 printf("\n--- max clients (10) ---\n");
73 { 74 {
74 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A }; 75 esp_ip4_addr_t ap_ip = { .addr = 0x012FA80A };
75 firewall_init(ap_ip); 76 tollgate_core_fw_init(ap_ip);
76 77
77 for (int i = 0; i < 10; i++) { 78 for (int i = 0; i < 10; i++) {
78 firewall_grant_access(0x0A000000 + i); 79 tollgate_core_fw_grant(0x0A000000 + i);
79 } 80 }
80 ASSERT_EQ_INT(10, firewall_client_count(), "10 clients at max"); 81 ASSERT_EQ_INT(10, tollgate_core_fw_client_count(), "10 clients at max");
81 82
82 firewall_grant_access(0x0A000100); 83 tollgate_core_fw_grant(0x0A000100);
83 ASSERT_EQ_INT(10, firewall_client_count(), "still 10 after exceeding max"); 84 ASSERT_EQ_INT(10, tollgate_core_fw_client_count(), "still 10 after exceeding max");
84 } 85 }
85 86
86 printf("\n--- is_mac_allowed (no MACs resolved in stub) ---\n"); 87 printf("\n--- is_mac_allowed (no MACs resolved in stub) ---\n");
87 { 88 {
88 firewall_init((esp_ip4_addr_t){ .addr = 0x012FA80A }); 89 tollgate_core_fw_init((esp_ip4_addr_t){ .addr = 0x012FA80A });
89 firewall_grant_access(0x0601A8C0); 90 tollgate_core_fw_grant(0x0601A8C0);
90 ASSERT(!firewall_is_mac_allowed(""), "empty MAC not allowed"); 91 ASSERT(!tollgate_core_fw_is_mac_allowed(""), "empty MAC not allowed");
91 } 92 }
92 93
93 TEST_SUMMARY(); 94 TEST_SUMMARY();
diff --git a/tests/unit/test_mcp_handler.c b/tests/unit/test_mcp_handler.c
index 05e9e38..61d1a8b 100644
--- a/tests/unit/test_mcp_handler.c
+++ b/tests/unit/test_mcp_handler.c
@@ -1,7 +1,8 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "mcp_handler.h" 2#include "mcp_handler.h"
3#include "config.h" 3#include "config.h"
4#include "session.h" 4#include "tollgate_core_session.h"
5#include "tollgate_core.h"
5#include "nucula_wallet.h" 6#include "nucula_wallet.h"
6#include "cJSON.h" 7#include "cJSON.h"
7#include <string.h> 8#include <string.h>
@@ -41,15 +42,15 @@ esp_err_t nucula_wallet_melt(const char *bolt11, uint64_t max_fee) {
41 return g_wallet_melt_rc; 42 return g_wallet_melt_rc;
42} 43}
43 44
44static session_t g_test_sessions[SESSION_MAX_CLIENTS]; 45static tg_session_t g_test_sessions[TG_SESSION_MAX_CLIENTS];
45static int g_test_session_count = 0; 46static int g_test_session_count = 0;
46 47
47session_t *cvm_get_sessions_array(void) { 48tg_session_t *tollgate_core_session_get_array(void) {
48 return g_test_sessions; 49 return g_test_sessions;
49} 50}
50 51
51int cvm_get_sessions_count(void) { 52int tollgate_core_session_get_array_size(void) {
52 return SESSION_MAX_CLIENTS; 53 return TG_SESSION_MAX_CLIENTS;
53} 54}
54 55
55static void test_mcp_parse_tool(void) 56static void test_mcp_parse_tool(void)
diff --git a/tests/unit/test_mining_payment b/tests/unit/test_mining_payment
index dd4e781..d179de4 100755
--- a/tests/unit/test_mining_payment
+++ b/tests/unit/test_mining_payment
Binary files differ
diff --git a/tests/unit/test_mining_payment.c b/tests/unit/test_mining_payment.c
index c3834fd..25932e5 100644
--- a/tests/unit/test_mining_payment.c
+++ b/tests/unit/test_mining_payment.c
@@ -1,5 +1,5 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/mining_payment.h" 2#include "tollgate_core_mining.h"
3#include <stdio.h> 3#include <stdio.h>
4#include <string.h> 4#include <string.h>
5#include <math.h> 5#include <math.h>
@@ -8,84 +8,84 @@ int main(void)
8{ 8{
9 printf("=== test_mining_payment ===\n"); 9 printf("=== test_mining_payment ===\n");
10 10
11 mining_payment_init(); 11 tollgate_core_mining_init();
12 12
13 printf("\n--- mining_nbits_to_difficulty ---\n"); 13 printf("\n--- tollgate_core_mining_nbits_to_difficulty ---\n");
14 uint64_t d1 = mining_nbits_to_difficulty(0); 14 uint64_t d1 = tollgate_core_mining_nbits_to_difficulty(0);
15 ASSERT(d1 == UINT64_MAX, "nbits=0 returns max"); 15 ASSERT(d1 == UINT64_MAX, "nbits=0 returns max");
16 16
17 uint64_t d2 = mining_nbits_to_difficulty(0x170309E2); 17 uint64_t d2 = tollgate_core_mining_nbits_to_difficulty(0x170309E2);
18 ASSERT(d2 > 0, "mainnet nbits gives positive difficulty"); 18 ASSERT(d2 > 0, "mainnet nbits gives positive difficulty");
19 printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2); 19 printf(" INFO: difficulty for 0x170309E2 = %llu\n", (unsigned long long)d2);
20 20
21 uint64_t d3 = mining_nbits_to_difficulty(0x1d00ffff); 21 uint64_t d3 = tollgate_core_mining_nbits_to_difficulty(0x1d00ffff);
22 ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1"); 22 ASSERT(d3 == 1, "pseudotransaction nbits = difficulty 1");
23 23
24 printf("\n--- mining_calculate_hashprice ---\n"); 24 printf("\n--- tollgate_core_mining_calc_hashprice ---\n");
25 double hp1 = mining_calculate_hashprice(0); 25 double hp1 = tollgate_core_mining_calc_hashprice(0);
26 ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0"); 26 ASSERT(hp1 == 0.0, "nbits=0 gives hashprice 0");
27 27
28 double hp2 = mining_calculate_hashprice(0x170309E2); 28 double hp2 = tollgate_core_mining_calc_hashprice(0x170309E2);
29 ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice"); 29 ASSERT(hp2 > 0.0, "mainnet nbits gives positive hashprice");
30 printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2); 30 printf(" INFO: hashprice for 0x170309E2 = %.6f sat/GH/s/day\n", hp2);
31 31
32 printf("\n--- mining_calculate_hashprice_override ---\n"); 32 printf("\n--- tollgate_core_mining_calc_hashprice_override ---\n");
33 double hp3 = mining_calculate_hashprice_override(1000); 33 double hp3 = tollgate_core_mining_calc_hashprice_override(1000);
34 ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value"); 34 ASSERT(fabs(hp3 - 1000.0) < 0.001, "override returns exact value");
35 35
36 printf("\n--- mining_set_current_nbits ---\n"); 36 printf("\n--- tollgate_core_mining_set_current_nbits ---\n");
37 mining_set_current_nbits(0x170309E2); 37 tollgate_core_mining_set_current_nbits(0x170309E2);
38 double hp4 = mining_get_current_hashprice(); 38 double hp4 = tollgate_core_mining_get_current_hashprice();
39 ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated"); 39 ASSERT(fabs(hp4 - hp2) < 0.0001, "stored hashprice matches calculated");
40 40
41 printf("\n--- mining_get_or_create_client ---\n"); 41 printf("\n--- tollgate_core_mining_get_or_create_client ---\n");
42 mining_client_stats_t *c1 = mining_get_or_create_client(0x0A010203); 42 tollgate_mining_client_stats_t *c1 = tollgate_core_mining_get_or_create_client(0x0A010203);
43 ASSERT(c1 != NULL, "created client 1"); 43 ASSERT(c1 != NULL, "created client 1");
44 ASSERT(c1->ip == 0x0A010203, "client 1 IP matches"); 44 ASSERT(c1->ip == 0x0A010203, "client 1 IP matches");
45 ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares"); 45 ASSERT(c1->shares_accepted == 0, "client 1 starts with 0 shares");
46 46
47 mining_client_stats_t *c2 = mining_get_or_create_client(0x0A010204); 47 tollgate_mining_client_stats_t *c2 = tollgate_core_mining_get_or_create_client(0x0A010204);
48 ASSERT(c2 != NULL, "created client 2"); 48 ASSERT(c2 != NULL, "created client 2");
49 ASSERT(c2->ip == 0x0A010204, "client 2 IP matches"); 49 ASSERT(c2->ip == 0x0A010204, "client 2 IP matches");
50 50
51 mining_client_stats_t *c1_again = mining_get_or_create_client(0x0A010203); 51 tollgate_mining_client_stats_t *c1_again = tollgate_core_mining_get_or_create_client(0x0A010203);
52 ASSERT(c1_again == c1, "same IP returns same client"); 52 ASSERT(c1_again == c1, "same IP returns same client");
53 53
54 printf("\n--- mining_update_hashrate ---\n"); 54 printf("\n--- tollgate_core_mining_update_hashrate ---\n");
55 mining_update_hashrate(0x0A010203, true); 55 tollgate_core_mining_update_hashrate(0x0A010203, true);
56 mining_update_hashrate(0x0A010203, true); 56 tollgate_core_mining_update_hashrate(0x0A010203, true);
57 mining_update_hashrate(0x0A010203, false); 57 tollgate_core_mining_update_hashrate(0x0A010203, false);
58 58
59 const mining_client_stats_t *c1_stats = mining_get_client_stats(0x0A010203); 59 const tollgate_mining_client_stats_t *c1_stats = tollgate_core_mining_get_client_stats(0x0A010203);
60 ASSERT(c1_stats != NULL, "client 1 stats found"); 60 ASSERT(c1_stats != NULL, "client 1 stats found");
61 ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted"); 61 ASSERT(c1_stats->shares_accepted == 2, "client 1 has 2 accepted");
62 ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected"); 62 ASSERT(c1_stats->shares_rejected == 1, "client 1 has 1 rejected");
63 63
64 printf("\n--- mining_get_client_stats ---\n"); 64 printf("\n--- tollgate_core_mining_get_client_stats ---\n");
65 const mining_client_stats_t *notfound = mining_get_client_stats(0xFFFFFFFF); 65 const tollgate_mining_client_stats_t *notfound = tollgate_core_mining_get_client_stats(0xFFFFFFFF);
66 ASSERT(notfound == NULL, "nonexistent client returns NULL"); 66 ASSERT(notfound == NULL, "nonexistent client returns NULL");
67 67
68 printf("\n--- mining_shares_to_allotment_ms ---\n"); 68 printf("\n--- tollgate_core_mining_shares_to_allotment_ms ---\n");
69 uint64_t allot1 = mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000); 69 uint64_t allot1 = tollgate_core_mining_shares_to_allotment_ms(0.0, 100.0, 21, 60000);
70 ASSERT(allot1 == 0, "zero hashrate = zero allotment"); 70 ASSERT(allot1 == 0, "zero hashrate = zero allotment");
71 71
72 uint64_t allot2 = mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000); 72 uint64_t allot2 = tollgate_core_mining_shares_to_allotment_ms(1.0, 100.0, 21, 60000);
73 ASSERT(allot2 > 0, "positive hashrate gives positive allotment"); 73 ASSERT(allot2 > 0, "positive hashrate gives positive allotment");
74 printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2); 74 printf(" INFO: 1 GH/s at 100 sat/GH/s/day, 21 sats/60s => %llu ms\n", (unsigned long long)allot2);
75 75
76 uint64_t allot3 = mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000); 76 uint64_t allot3 = tollgate_core_mining_shares_to_allotment_ms(10.0, 50.0, 10, 30000);
77 ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment"); 77 ASSERT(allot3 > 0, "10 GH/s at 50 sat gives positive allotment");
78 printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3); 78 printf(" INFO: 10 GH/s at 50 sat/GH/s/day, 10 sats/30s => %llu ms\n", (unsigned long long)allot3);
79 79
80 printf("\n--- mining_shares_to_allotment_bytes ---\n"); 80 printf("\n--- tollgate_core_mining_shares_to_allotment_bytes ---\n");
81 uint64_t ab1 = mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576); 81 uint64_t ab1 = tollgate_core_mining_shares_to_allotment_bytes(0.0, 100.0, 21, 1048576);
82 ASSERT(ab1 == 0, "zero hashrate = zero bytes"); 82 ASSERT(ab1 == 0, "zero hashrate = zero bytes");
83 83
84 uint64_t ab2 = mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576); 84 uint64_t ab2 = tollgate_core_mining_shares_to_allotment_bytes(1.0, 100.0, 21, 1048576);
85 ASSERT(ab2 > 0, "positive hashrate gives positive bytes"); 85 ASSERT(ab2 > 0, "positive hashrate gives positive bytes");
86 86
87 printf("\n--- mining_validate_share (stub) ---\n"); 87 printf("\n--- tollgate_core_mining_validate_share (stub) ---\n");
88 esp_err_t vr = mining_validate_share(NULL, 0, NULL, 0); 88 esp_err_t vr = tollgate_core_mining_validate_share(NULL, 0, NULL, 0);
89 ASSERT(vr == ESP_OK, "validate share stub returns OK"); 89 ASSERT(vr == ESP_OK, "validate share stub returns OK");
90 90
91 TEST_SUMMARY(); 91 TEST_SUMMARY();
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}
diff --git a/tests/unit/test_session_payment_method b/tests/unit/test_session_payment_method
index 258fb01..c82ae27 100755
--- a/tests/unit/test_session_payment_method
+++ b/tests/unit/test_session_payment_method
Binary files differ
diff --git a/tests/unit/test_session_payment_method.c b/tests/unit/test_session_payment_method.c
index 0239140..819c875 100644
--- a/tests/unit/test_session_payment_method.c
+++ b/tests/unit/test_session_payment_method.c
@@ -1,10 +1,12 @@
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>
9#include <limits.h>
8 10
9static tollgate_config_t g_test_config; 11static tollgate_config_t g_test_config;
10 12
@@ -12,62 +14,41 @@ const tollgate_config_t *tollgate_config_get(void) {
12 return &g_test_config; 14 return &g_test_config;
13} 15}
14 16
15esp_err_t firewall_get_mac_for_ip(uint32_t ip, char *mac_out, size_t size) {
16 (void)ip;
17 snprintf(mac_out, size, "AA:BB:CC:DD:EE:FF");
18 return 0;
19}
20
21static uint32_t g_granted_ips[32];
22static int g_granted_count = 0;
23
24void firewall_grant_access(uint32_t ip) {
25 if (g_granted_count < 32) g_granted_ips[g_granted_count++] = ip;
26}
27
28void firewall_revoke_access(uint32_t ip) {
29 (void)ip;
30}
31
32int main(void) 17int main(void)
33{ 18{
34 printf("=== test_session_payment_method ===\n"); 19 printf("=== test_session_payment_method ===\n");
35 memset(&g_test_config, 0, sizeof(g_test_config)); 20 memset(&g_test_config, 0, sizeof(g_test_config));
36 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); 21 strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1);
37 g_granted_count = 0;
38 22
39 printf("\n--- session_create sets PAYMENT_METHOD_CASHU ---\n"); 23 printf("\n--- tollgate_core_session_create sets TG_PAYMENT_CASHU ---\n");
40 session_manager_init(); 24 tollgate_core_session_init();
41 session_t *s1 = session_create(0x0A010001, 60000); 25 tg_session_t *s1 = tollgate_core_session_create(0x0A010001, 60000);
42 ASSERT(s1 != NULL, "session created"); 26 ASSERT(s1 != NULL, "session created");
43 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s1->payment_method, "cashu session has PAYMENT_METHOD_CASHU"); 27 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s1->payment_method, "cashu session has TG_PAYMENT_CASHU");
44 28
45 printf("\n--- session_create_bytes sets PAYMENT_METHOD_BYTES ---\n"); 29 printf("\n--- tollgate_core_session_create_bytes sets TG_PAYMENT_BYTES ---\n");
46 session_manager_init(); 30 tollgate_core_session_init();
47 g_granted_count = 0; 31 tg_session_t *s2 = tollgate_core_session_create_bytes(0x0A010002, 1048576);
48 session_t *s2 = session_create_bytes(0x0A010002, 1048576);
49 ASSERT(s2 != NULL, "bytes session created"); 32 ASSERT(s2 != NULL, "bytes session created");
50 ASSERT_EQ_INT(PAYMENT_METHOD_BYTES, (int)s2->payment_method, "bytes session has PAYMENT_METHOD_BYTES"); 33 ASSERT_EQ_INT(TG_PAYMENT_BYTES, (int)s2->payment_method, "bytes session has TG_PAYMENT_BYTES");
51 ASSERT_EQ_UINT64(1048576, s2->allotment_bytes, "allotment_bytes set"); 34 ASSERT_EQ_UINT64(1048576, s2->allotment_bytes, "allotment_bytes set");
52 ASSERT_EQ_UINT64(0, s2->bytes_consumed, "bytes_consumed starts at 0"); 35 ASSERT_EQ_UINT64(0, s2->bytes_consumed, "bytes_consumed starts at 0");
53 36
54 printf("\n--- payment_method_t enum values are distinct ---\n"); 37 printf("\n--- tg_payment_method_t enum values are distinct ---\n");
55 ASSERT(PAYMENT_METHOD_CASHU != PAYMENT_METHOD_MINING, "CASHU != MINING"); 38 ASSERT(TG_PAYMENT_CASHU != TG_PAYMENT_MINING, "CASHU != MINING");
56 ASSERT(PAYMENT_METHOD_CASHU != PAYMENT_METHOD_BYTES, "CASHU != BYTES"); 39 ASSERT(TG_PAYMENT_CASHU != TG_PAYMENT_BYTES, "CASHU != BYTES");
57 ASSERT(PAYMENT_METHOD_MINING != PAYMENT_METHOD_BYTES, "MINING != BYTES"); 40 ASSERT(TG_PAYMENT_MINING != TG_PAYMENT_BYTES, "MINING != BYTES");
58 41
59 printf("\n--- session extend preserves payment_method ---\n"); 42 printf("\n--- session extend preserves payment_method ---\n");
60 session_manager_init(); 43 tollgate_core_session_init();
61 g_granted_count = 0; 44 tg_session_t *s3 = tollgate_core_session_create(0x0A010003, 60000);
62 session_t *s3 = session_create(0x0A010003, 60000); 45 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s3->payment_method, "initially CASHU");
63 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s3->payment_method, "initially CASHU"); 46 tollgate_core_session_extend(s3, 30000);
64 session_extend(s3, 30000); 47 ASSERT_EQ_INT(TG_PAYMENT_CASHU, (int)s3->payment_method, "still CASHU after extend");
65 ASSERT_EQ_INT(PAYMENT_METHOD_CASHU, (int)s3->payment_method, "still CASHU after extend");
66 48
67 printf("\n--- bytes session allotment_ms is INT64_MAX ---\n"); 49 printf("\n--- bytes session allotment_ms is INT64_MAX ---\n");
68 session_manager_init(); 50 tollgate_core_session_init();
69 g_granted_count = 0; 51 tg_session_t *s4 = tollgate_core_session_create_bytes(0x0A010004, 2097152);
70 session_t *s4 = session_create_bytes(0x0A010004, 2097152);
71 ASSERT(s4->allotment_ms == INT64_MAX, "bytes session has INT64_MAX allotment_ms"); 52 ASSERT(s4->allotment_ms == INT64_MAX, "bytes session has INT64_MAX allotment_ms");
72 53
73 TEST_SUMMARY(); 54 TEST_SUMMARY();
diff --git a/tests/unit/test_stratum_proxy b/tests/unit/test_stratum_proxy
index 542d82f..ca6a07a 100755
--- a/tests/unit/test_stratum_proxy
+++ b/tests/unit/test_stratum_proxy
Binary files differ
diff --git a/tests/unit/test_stratum_proxy.c b/tests/unit/test_stratum_proxy.c
index 7788911..6ad9d9e 100644
--- a/tests/unit/test_stratum_proxy.c
+++ b/tests/unit/test_stratum_proxy.c
@@ -1,6 +1,6 @@
1#include "test_framework.h" 1#include "test_framework.h"
2#include "../../main/stratum_proxy.h" 2#include "../../main/stratum_proxy.h"
3#include "../../main/mining_payment.h" 3#include "tollgate_core_mining.h"
4#include <stdio.h> 4#include <stdio.h>
5#include <string.h> 5#include <string.h>
6 6
@@ -8,7 +8,7 @@ int main(void)
8{ 8{
9 printf("=== test_stratum_proxy ===\n"); 9 printf("=== test_stratum_proxy ===\n");
10 10
11 mining_payment_init(); 11 tollgate_core_mining_init();
12 12
13 printf("\n--- stratum_proxy_set_job / get_current_job ---\n"); 13 printf("\n--- stratum_proxy_set_job / get_current_job ---\n");
14 { 14 {
@@ -42,7 +42,7 @@ int main(void)
42 42
43 printf("\n--- stratum_proxy_get_stats ---\n"); 43 printf("\n--- stratum_proxy_get_stats ---\n");
44 { 44 {
45 mining_set_current_nbits(0x170309E2); 45 tollgate_core_mining_set_current_nbits(0x170309E2);
46 stratum_proxy_set_job(&(stratum_job_t){ 46 stratum_proxy_set_job(&(stratum_job_t){
47 .job_id = 1, 47 .job_id = 1,
48 .nbits = 0x170309E2, 48 .nbits = 0x170309E2,