diff options
| author | Your Name <you@example.com> | 2026-05-17 05:27:06 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 05:27:06 +0530 |
| commit | fdf662f8f1a1a3b38fe4d251982fffab8e9bf664 (patch) | |
| tree | 2413bdc936b757adf4849a522b7df2a5c8eb0aec /tests/unit/test_mcp_handler.c | |
| parent | edd125d0e3fe5fe7c0edf30c429723f3b0120c68 (diff) | |
Phase 7: MCP handler (25 tests), NIP-04 encrypt/decrypt (15 tests), CVM server skeleton
- mcp_handler.c/h: 4 tools (get_config, set_config, get_balance, wallet_send)
- nip04.c/h: AES-256-CBC + ECDH with 0x02 compressed pubkey prefix
- Fixed IV copy bug: mbedTLS AES-CBC modifies IV in-place
- Base64 encode/decode for ciphertext transport
- PKCS7 padding
- cvm_server.c/h: Nostr DM listener with FreeRTOS task
- config: cvm_enabled, cvm_relays fields
- 156 total tests passing across 10 test binaries
Diffstat (limited to 'tests/unit/test_mcp_handler.c')
| -rw-r--r-- | tests/unit/test_mcp_handler.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/unit/test_mcp_handler.c b/tests/unit/test_mcp_handler.c new file mode 100644 index 0000000..aaa199d --- /dev/null +++ b/tests/unit/test_mcp_handler.c | |||
| @@ -0,0 +1,148 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "mcp_handler.h" | ||
| 3 | #include "config.h" | ||
| 4 | #include "nucula_wallet.h" | ||
| 5 | #include "cJSON.h" | ||
| 6 | #include <string.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | |||
| 9 | static tollgate_config_t g_test_config; | ||
| 10 | static uint64_t g_wallet_balance = 0; | ||
| 11 | static int g_wallet_proof_count = 0; | ||
| 12 | static int g_wallet_send_rc = 0; | ||
| 13 | static char g_wallet_send_token[256] = "cashuA_test_token"; | ||
| 14 | |||
| 15 | const tollgate_config_t *tollgate_config_get(void) { | ||
| 16 | return &g_test_config; | ||
| 17 | } | ||
| 18 | |||
| 19 | uint64_t nucula_wallet_balance(void) { | ||
| 20 | return g_wallet_balance; | ||
| 21 | } | ||
| 22 | |||
| 23 | int nucula_wallet_proof_count(void) { | ||
| 24 | return g_wallet_proof_count; | ||
| 25 | } | ||
| 26 | |||
| 27 | int nucula_wallet_send(uint64_t amount, char *token_out, size_t token_max) { | ||
| 28 | (void)amount; | ||
| 29 | (void)token_max; | ||
| 30 | if (g_wallet_send_rc == 0) { | ||
| 31 | strncpy(token_out, g_wallet_send_token, token_max - 1); | ||
| 32 | } | ||
| 33 | return g_wallet_send_rc; | ||
| 34 | } | ||
| 35 | |||
| 36 | static void test_mcp_parse_tool(void) | ||
| 37 | { | ||
| 38 | printf("\n=== MCP tool parsing ===\n"); | ||
| 39 | ASSERT_EQ_INT(MCP_TOOL_GET_CONFIG, mcp_parse_tool("get_config"), "get_config"); | ||
| 40 | ASSERT_EQ_INT(MCP_TOOL_SET_CONFIG, mcp_parse_tool("set_config"), "set_config"); | ||
| 41 | ASSERT_EQ_INT(MCP_TOOL_GET_BALANCE, mcp_parse_tool("get_balance"), "get_balance"); | ||
| 42 | ASSERT_EQ_INT(MCP_TOOL_WALLET_SEND, mcp_parse_tool("wallet_send"), "wallet_send"); | ||
| 43 | ASSERT_EQ_INT(MCP_TOOL_UNKNOWN, mcp_parse_tool("foo"), "unknown tool"); | ||
| 44 | ASSERT_EQ_INT(MCP_TOOL_UNKNOWN, mcp_parse_tool(NULL), "NULL tool"); | ||
| 45 | } | ||
| 46 | |||
| 47 | static void test_mcp_get_config(void) | ||
| 48 | { | ||
| 49 | printf("\n=== MCP get_config ===\n"); | ||
| 50 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 51 | strncpy(g_test_config.ap_ssid, "TollGate-TEST", sizeof(g_test_config.ap_ssid) - 1); | ||
| 52 | strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1); | ||
| 53 | g_test_config.price_per_step = 21; | ||
| 54 | g_test_config.step_size_ms = 60000; | ||
| 55 | g_test_config.step_size_bytes = 22020096; | ||
| 56 | strncpy(g_test_config.mint_url, "https://testnut.cashu.space", sizeof(g_test_config.mint_url) - 1); | ||
| 57 | |||
| 58 | mcp_response_t resp = mcp_handle_get_config(); | ||
| 59 | ASSERT(resp.success, "get_config succeeds"); | ||
| 60 | |||
| 61 | cJSON *result = cJSON_Parse(resp.result_json); | ||
| 62 | ASSERT(result != NULL, "result is valid JSON"); | ||
| 63 | ASSERT_EQ_STR("bytes", cJSON_GetObjectItem(result, "metric")->valuestring, "metric=bytes"); | ||
| 64 | ASSERT_EQ_INT(21, cJSON_GetObjectItem(result, "price_per_step")->valueint, "price=21"); | ||
| 65 | cJSON_Delete(result); | ||
| 66 | } | ||
| 67 | |||
| 68 | static void test_mcp_set_config(void) | ||
| 69 | { | ||
| 70 | printf("\n=== MCP set_config ===\n"); | ||
| 71 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 72 | g_test_config.price_per_step = 21; | ||
| 73 | |||
| 74 | const char *params = "{\"price_per_step\":42,\"metric\":\"milliseconds\"}"; | ||
| 75 | mcp_response_t resp = mcp_handle_set_config(params); | ||
| 76 | ASSERT(resp.success, "set_config succeeds"); | ||
| 77 | ASSERT_EQ_INT(42, g_test_config.price_per_step, "price updated to 42"); | ||
| 78 | ASSERT_EQ_STR("milliseconds", g_test_config.metric, "metric updated"); | ||
| 79 | |||
| 80 | resp = mcp_handle_set_config("not json"); | ||
| 81 | ASSERT(!resp.success, "invalid JSON fails"); | ||
| 82 | } | ||
| 83 | |||
| 84 | static void test_mcp_get_balance(void) | ||
| 85 | { | ||
| 86 | printf("\n=== MCP get_balance ===\n"); | ||
| 87 | g_wallet_balance = 500; | ||
| 88 | g_wallet_proof_count = 8; | ||
| 89 | |||
| 90 | mcp_response_t resp = mcp_handle_get_balance(); | ||
| 91 | ASSERT(resp.success, "get_balance succeeds"); | ||
| 92 | |||
| 93 | cJSON *result = cJSON_Parse(resp.result_json); | ||
| 94 | ASSERT(result != NULL, "result is valid JSON"); | ||
| 95 | ASSERT_EQ_INT(500, (int)cJSON_GetObjectItem(result, "balance_sats")->valuedouble, "balance=500"); | ||
| 96 | ASSERT_EQ_INT(8, cJSON_GetObjectItem(result, "proof_count")->valueint, "proofs=8"); | ||
| 97 | cJSON_Delete(result); | ||
| 98 | } | ||
| 99 | |||
| 100 | static void test_mcp_wallet_send(void) | ||
| 101 | { | ||
| 102 | printf("\n=== MCP wallet_send ===\n"); | ||
| 103 | g_wallet_send_rc = 0; | ||
| 104 | strncpy(g_wallet_send_token, "cashuA_send_test", sizeof(g_wallet_send_token) - 1); | ||
| 105 | |||
| 106 | const char *params = "{\"amount\":21}"; | ||
| 107 | mcp_response_t resp = mcp_handle_wallet_send(params); | ||
| 108 | ASSERT(resp.success, "wallet_send succeeds"); | ||
| 109 | |||
| 110 | cJSON *result = cJSON_Parse(resp.result_json); | ||
| 111 | ASSERT(result != NULL, "result is valid JSON"); | ||
| 112 | ASSERT_EQ_STR("cashuA_send_test", cJSON_GetObjectItem(result, "token")->valuestring, "token matches"); | ||
| 113 | cJSON_Delete(result); | ||
| 114 | |||
| 115 | printf("\n--- wallet_send missing amount ---\n"); | ||
| 116 | resp = mcp_handle_wallet_send("{}"); | ||
| 117 | ASSERT(!resp.success, "missing amount fails"); | ||
| 118 | |||
| 119 | printf("\n--- wallet_send send fails ---\n"); | ||
| 120 | g_wallet_send_rc = -1; | ||
| 121 | resp = mcp_handle_wallet_send("{\"amount\":100}"); | ||
| 122 | ASSERT(!resp.success, "send failure reported"); | ||
| 123 | } | ||
| 124 | |||
| 125 | static void test_mcp_dispatch(void) | ||
| 126 | { | ||
| 127 | printf("\n=== MCP dispatch ===\n"); | ||
| 128 | mcp_request_t req = {0}; | ||
| 129 | req.tool = MCP_TOOL_UNKNOWN; | ||
| 130 | strncpy(req.method, "bogus", sizeof(req.method) - 1); | ||
| 131 | mcp_response_t resp = mcp_dispatch(&req); | ||
| 132 | ASSERT(!resp.success, "unknown tool dispatch fails"); | ||
| 133 | |||
| 134 | resp = mcp_dispatch(NULL); | ||
| 135 | ASSERT(!resp.success, "NULL request dispatch fails"); | ||
| 136 | } | ||
| 137 | |||
| 138 | int main(void) | ||
| 139 | { | ||
| 140 | printf("=== test_mcp_handler ===\n"); | ||
| 141 | test_mcp_parse_tool(); | ||
| 142 | test_mcp_get_config(); | ||
| 143 | test_mcp_set_config(); | ||
| 144 | test_mcp_get_balance(); | ||
| 145 | test_mcp_wallet_send(); | ||
| 146 | test_mcp_dispatch(); | ||
| 147 | TEST_SUMMARY(); | ||
| 148 | } | ||