From be4d490e96de3d20cc6c9921f09534cde0ca0765 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 23 May 2026 15:26:52 +0530 Subject: Phase 8c-8f: Extract portal, stratum_client, mint_health, client into tollgate_core New component modules: - tollgate_core_portal.c/h: template rendering, usage calc, captive URI detection - tollgate_core_stratum_client.c/h: Stratum V1 JSON protocol parsing, message building - tollgate_core_mint_health.c/h: mint health state tracking, recovery threshold logic - tollgate_core_client.c/h: discovery/session/usage JSON parsing, renewal check, price calc Main shims updated to delegate pure logic to component: - captive_portal.c: uses tollgate_core_portal_render(), format_usage(), is_captive_uri() - stratum_client.c: uses tollgate_core_stratum_parse_notify/build_*() - mint_health.c: uses tollgate_core_mint_health_update/is_reachable/mark_unreachable() - tollgate_client.c: uses tollgate_core_client_parse_*/should_renew/calc_price_per_min() 4 new unit tests (test_portal, test_stratum_client, test_mint_health_core, test_client_core) All 25 unit tests pass. ESP-IDF build passes (68% partition free). --- tests/unit/test_stratum_client.c | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/unit/test_stratum_client.c (limited to 'tests/unit/test_stratum_client.c') diff --git a/tests/unit/test_stratum_client.c b/tests/unit/test_stratum_client.c new file mode 100644 index 0000000..302fcba --- /dev/null +++ b/tests/unit/test_stratum_client.c @@ -0,0 +1,125 @@ +#include "test_framework.h" +#include "../../components/tollgate_core/src/tollgate_core_stratum_client.h" +#include +#include +#include +#include + +int main(void) +{ + printf("=== test_stratum_client ===\n"); + + printf("\n--- hex_to_bytes ---\n"); + { + const char *hex = "0a1b2c3d"; + uint8_t out[4] = {0}; + tollgate_core_stratum_hex_to_bytes(hex, out, 4); + ASSERT_EQ_INT(0x0a, (int)out[0], "byte 0"); + ASSERT_EQ_INT(0x1b, (int)out[1], "byte 1"); + ASSERT_EQ_INT(0x2c, (int)out[2], "byte 2"); + ASSERT_EQ_INT(0x3d, (int)out[3], "byte 3"); + } + + printf("\n--- hex_to_bytes zeros ---\n"); + { + const char *hex = "00000000"; + uint8_t out[4] = {0xff, 0xff, 0xff, 0xff}; + tollgate_core_stratum_hex_to_bytes(hex, out, 4); + ASSERT_EQ_INT(0, (int)out[0], "zero byte 0"); + ASSERT_EQ_INT(0, (int)out[1], "zero byte 1"); + } + + printf("\n--- parse_notify valid ---\n"); + { + cJSON *params = cJSON_Parse("[\"123\",\"0000000000000000000000000000000000000000000000000000000000000000\"," + "\"\"" + ",\"\"" + ",\"\"" + ",\"20000000\"" + ",\"1705e3c4\"" + ",\"65a3b2c1\"]"); + tollgate_stratum_job_t job = {0}; + uint32_t nbits = 0; + bool ok = tollgate_core_stratum_parse_notify(params, &job, &nbits); + ASSERT(ok, "parse_notify returns true"); + ASSERT_EQ_INT(123, (int)job.job_id, "job_id parsed"); + ASSERT(job.valid, "job is valid"); + ASSERT_EQ_INT(0x20000000, (int)job.version, "version parsed"); + ASSERT_EQ_INT(0x1705e3c4, (int)job.nbits, "nbits parsed"); + ASSERT_EQ_INT(0x65a3b2c1, (int)job.ntime, "ntime parsed"); + ASSERT_EQ_INT(0x1705e3c4, (int)nbits, "out_nbits set"); + ASSERT_EQ_INT(32, job.target_len, "target_len is 32"); + cJSON_Delete(params); + } + + printf("\n--- parse_notify too few params ---\n"); + { + cJSON *params = cJSON_Parse("[\"123\",\"abcd\"]"); + tollgate_stratum_job_t job = {0}; + bool ok = tollgate_core_stratum_parse_notify(params, &job, NULL); + ASSERT(!ok, "parse_notify returns false for insufficient params"); + cJSON_Delete(params); + } + + printf("\n--- parse_notify NULL ---\n"); + { + tollgate_stratum_job_t job = {0}; + bool ok = tollgate_core_stratum_parse_notify(NULL, &job, NULL); + ASSERT(!ok, "parse_notify returns false for NULL"); + } + + printf("\n--- parse_difficulty valid ---\n"); + { + cJSON *params = cJSON_Parse("[512]"); + uint64_t diff = 0; + bool ok = tollgate_core_stratum_parse_difficulty(params, &diff); + ASSERT(ok, "parse_difficulty returns true"); + ASSERT_EQ_UINT64(512, (unsigned long long)diff, "difficulty parsed"); + cJSON_Delete(params); + } + + printf("\n--- parse_difficulty invalid ---\n"); + { + cJSON *params = cJSON_Parse("[]"); + uint64_t diff = 0; + bool ok = tollgate_core_stratum_parse_difficulty(params, &diff); + ASSERT(!ok, "parse_difficulty returns false for empty array"); + cJSON_Delete(params); + } + + printf("\n--- build_subscribe ---\n"); + { + char buf[256]; + int len = tollgate_core_stratum_build_subscribe(buf, sizeof(buf), 1); + ASSERT(len > 0, "build_subscribe returns positive length"); + ASSERT(strstr(buf, "mining.subscribe") != NULL, "contains method"); + ASSERT(strstr(buf, "\"id\":1") != NULL, "contains id"); + ASSERT(strstr(buf, "TollGate/1.0") != NULL, "contains user agent"); + ASSERT(buf[len - 1] == '\n', "ends with newline"); + } + + printf("\n--- build_authorize ---\n"); + { + char buf[512]; + int len = tollgate_core_stratum_build_authorize(buf, sizeof(buf), 2, "user1", "pass1"); + ASSERT(len > 0, "build_authorize returns positive length"); + ASSERT(strstr(buf, "mining.authorize") != NULL, "contains method"); + ASSERT(strstr(buf, "user1") != NULL, "contains user"); + ASSERT(strstr(buf, "pass1") != NULL, "contains pass"); + } + + printf("\n--- build_submit ---\n"); + { + char buf[512]; + int len = tollgate_core_stratum_build_submit(buf, sizeof(buf), 3, "worker1", + 100, 0x65a3b2c1, 0x12345678, 0x20000000); + ASSERT(len > 0, "build_submit returns positive length"); + ASSERT(strstr(buf, "mining.submit") != NULL, "contains method"); + ASSERT(strstr(buf, "worker1") != NULL, "contains worker"); + ASSERT(strstr(buf, "100") != NULL, "contains job_id"); + ASSERT(strstr(buf, "65a3b2c1") != NULL, "contains ntime"); + ASSERT(strstr(buf, "12345678") != NULL, "contains nonce"); + } + + TEST_SUMMARY(); +} -- cgit v1.2.3