From 32844ac7c4a135659714aabf7d2414f156176d72 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 01:39:03 +0530 Subject: test_cashu (10/10) + test_session (18/18): all 86 unit tests passing - Expand esp_http_client.h stub: full config struct + method enum + init/perform/cleanup - Add portTICK_PERIOD_MS + esp_err_to_name to stubs - session.c: reject duplicate spent secrets in session_create (double-spend protection) - .gitignore: add test binaries --- .gitignore | 5 +++++ main/session.c | 7 ++++++ tests/unit/stubs/esp_err.h | 2 ++ tests/unit/stubs/esp_http_client.h | 43 +++++++++++++++++++++++++++++++++++- tests/unit/stubs/freertos/FreeRTOS.h | 1 + 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 67133d5..e50f050 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,8 @@ __pycache__/ *.map NAND/ main/config.json +tests/unit/test_geohash +tests/unit/test_identity +tests/unit/test_nostr_event +tests/unit/test_cashu +tests/unit/test_session diff --git a/main/session.c b/main/session.c index 5d2efee..521b74a 100644 --- a/main/session.c +++ b/main/session.c @@ -50,6 +50,13 @@ session_t *session_create(uint32_t client_ip, uint64_t allotment_ms, return existing; } + for (int i = 0; i < secret_count; i++) { + if (session_is_secret_spent(spent_secrets[i])) { + ESP_LOGW(TAG, "Duplicate secret rejected"); + return NULL; + } + } + if (s_session_count >= SESSION_MAX_CLIENTS) { for (int i = 0; i < SESSION_MAX_CLIENTS; i++) { if (!s_sessions[i].active || session_is_expired(&s_sessions[i])) { diff --git a/tests/unit/stubs/esp_err.h b/tests/unit/stubs/esp_err.h index 84c3734..9bedb72 100644 --- a/tests/unit/stubs/esp_err.h +++ b/tests/unit/stubs/esp_err.h @@ -13,6 +13,8 @@ typedef int esp_err_t; #define ESP_ERR_NO_MEM 0x101 #define ESP_ERR_NOT_FOUND 0x104 +static inline const char *esp_err_to_name(esp_err_t err) { (void)err; return "ESP_OK"; } + #define ESP_ERROR_CHECK(x) do { if ((x) != 0) { fprintf(stderr, "ESP_ERROR_CHECK failed: 0x%x\n", (int)(x)); abort(); } } while(0) #endif diff --git a/tests/unit/stubs/esp_http_client.h b/tests/unit/stubs/esp_http_client.h index 4169714..288ce3b 100644 --- a/tests/unit/stubs/esp_http_client.h +++ b/tests/unit/stubs/esp_http_client.h @@ -2,11 +2,52 @@ #define STUBS_ESP_HTTP_CLIENT_H #include "esp_err.h" +#include +#include -typedef void *esp_http_client_handle_t; +typedef enum { + HTTP_METHOD_GET = 0, + HTTP_METHOD_POST, + HTTP_METHOD_PUT, + HTTP_METHOD_DELETE, +} esp_http_client_method_t; + +typedef void *esp_crt_bundle_attach_fn_t(void *conf); typedef struct { + const char *url; + esp_http_client_method_t method; + int timeout_ms; + esp_crt_bundle_attach_fn_t *crt_bundle_attach; int cert_pem; } esp_http_client_config_t; +typedef void *esp_http_client_handle_t; + +static inline esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *cfg) { + (void)cfg; + return (void *)malloc(1); +} +static inline esp_err_t esp_http_client_set_header(esp_http_client_handle_t c, const char *k, const char *v) { + (void)c; (void)k; (void)v; return ESP_OK; +} +static inline esp_err_t esp_http_client_open(esp_http_client_handle_t c, int len) { + (void)c; (void)len; return ESP_OK; +} +static inline int esp_http_client_write(esp_http_client_handle_t c, const char *buf, int len) { + (void)c; (void)buf; return len; +} +static inline int esp_http_client_fetch_headers(esp_http_client_handle_t c) { + (void)c; return 0; +} +static inline int esp_http_client_get_status_code(esp_http_client_handle_t c) { + (void)c; return 200; +} +static inline int esp_http_client_read(esp_http_client_handle_t c, char *buf, int len) { + (void)c; (void)buf; (void)len; return 0; +} +static inline esp_err_t esp_http_client_cleanup(esp_http_client_handle_t c) { + free(c); return ESP_OK; +} + #endif diff --git a/tests/unit/stubs/freertos/FreeRTOS.h b/tests/unit/stubs/freertos/FreeRTOS.h index 0fee758..696da87 100644 --- a/tests/unit/stubs/freertos/FreeRTOS.h +++ b/tests/unit/stubs/freertos/FreeRTOS.h @@ -6,6 +6,7 @@ static inline uint32_t xTaskGetTickCount(void) { return 0; } static inline void vTaskDelay(uint32_t ticks) { (void)ticks; } #define pdMS_TO_TICKS(ms) ((ms) / 10) +#define portTICK_PERIOD_MS 10 #define portMAX_DELAY 0xFFFFFFFF #endif -- cgit v1.2.3