upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/unit/test_session_payment_method.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test_session_payment_method.c')
-rw-r--r--tests/unit/test_session_payment_method.c67
1 files changed, 24 insertions, 43 deletions
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();