diff options
Diffstat (limited to 'tests/unit/test_lnurl_pay.c')
| -rw-r--r-- | tests/unit/test_lnurl_pay.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/unit/test_lnurl_pay.c b/tests/unit/test_lnurl_pay.c new file mode 100644 index 0000000..d630b9c --- /dev/null +++ b/tests/unit/test_lnurl_pay.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include <cjson/cJSON.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <math.h> | ||
| 8 | |||
| 9 | static const char *SAMPLE_LNURLP_RESPONSE = | ||
| 10 | "{\"callback\":\"https://coinos.io/lnurlp/callback/abc123\"," | ||
| 11 | "\"maxSendable\":1000000000," | ||
| 12 | "\"minSendable\":1000," | ||
| 13 | "\"metadata\":\"[[\\\"text/identifier\\\",\\\"TollGate@coinos.io\\\"]]\"," | ||
| 14 | "\"tag\":\"payRequest\"}"; | ||
| 15 | |||
| 16 | static const char *SAMPLE_CALLBACK_RESPONSE = | ||
| 17 | "{\"pr\":\"lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan7s4g2e65tr58a50k2jdhz3l8eqc5x5z5f9e3u70fxnh00x09ml0q CONSTANTS\"}"; | ||
| 18 | |||
| 19 | static const char *SAMPLE_MISSING_AT = "no-at-sign.com"; | ||
| 20 | static const char *SAMPLE_MISSING_CALLBACK = "{\"tag\":\"payRequest\"}"; | ||
| 21 | static const char *SAMPLE_MISSING_PR = "{\"status\":\"ERROR\"}"; | ||
| 22 | |||
| 23 | static bool test_parse_lnurlp_response(void) | ||
| 24 | { | ||
| 25 | cJSON *root = cJSON_Parse(SAMPLE_LNURLP_RESPONSE); | ||
| 26 | if (!root) return false; | ||
| 27 | |||
| 28 | cJSON *callback = cJSON_GetObjectItemCaseSensitive(root, "callback"); | ||
| 29 | bool ok = (callback && cJSON_IsString(callback) && | ||
| 30 | strcmp(callback->valuestring, "https://coinos.io/lnurlp/callback/abc123") == 0); | ||
| 31 | |||
| 32 | cJSON *min_sendable = cJSON_GetObjectItemCaseSensitive(root, "minSendable"); | ||
| 33 | ok = ok && (min_sendable && cJSON_IsNumber(min_sendable) && min_sendable->valuedouble == 1000); | ||
| 34 | |||
| 35 | cJSON *max_sendable = cJSON_GetObjectItemCaseSensitive(root, "maxSendable"); | ||
| 36 | ok = ok && (max_sendable && cJSON_IsNumber(max_sendable)); | ||
| 37 | |||
| 38 | cJSON_Delete(root); | ||
| 39 | return ok; | ||
| 40 | } | ||
| 41 | |||
| 42 | static bool test_parse_callback_response(void) | ||
| 43 | { | ||
| 44 | cJSON *root = cJSON_Parse(SAMPLE_CALLBACK_RESPONSE); | ||
| 45 | if (!root) return false; | ||
| 46 | |||
| 47 | cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr"); | ||
| 48 | bool ok = (pr && cJSON_IsString(pr) && strncmp(pr->valuestring, "lnbc", 4) == 0); | ||
| 49 | |||
| 50 | cJSON_Delete(root); | ||
| 51 | return ok; | ||
| 52 | } | ||
| 53 | |||
| 54 | static bool test_lightning_address_parse(void) | ||
| 55 | { | ||
| 56 | const char *addr = "TollGate@coinos.io"; | ||
| 57 | const char *at = strchr(addr, '@'); | ||
| 58 | if (!at) return false; | ||
| 59 | |||
| 60 | size_t user_len = at - addr; | ||
| 61 | if (user_len != 8) return false; | ||
| 62 | |||
| 63 | char username[64]; | ||
| 64 | memcpy(username, addr, user_len); | ||
| 65 | username[user_len] = '\0'; | ||
| 66 | |||
| 67 | if (strcmp(username, "TollGate") != 0) return false; | ||
| 68 | if (strcmp(at + 1, "coinos.io") != 0) return false; | ||
| 69 | |||
| 70 | return true; | ||
| 71 | } | ||
| 72 | |||
| 73 | static bool test_amount_validation(void) | ||
| 74 | { | ||
| 75 | uint64_t min_msat = 1000; | ||
| 76 | uint64_t max_msat = 1000000000; | ||
| 77 | uint64_t amount_msat = 21 * 1000; | ||
| 78 | |||
| 79 | bool ok = (amount_msat >= min_msat && amount_msat <= max_msat); | ||
| 80 | ok = ok && !(0 >= min_msat); | ||
| 81 | ok = ok && !(2000000000ULL <= max_msat); | ||
| 82 | return ok; | ||
| 83 | } | ||
| 84 | |||
| 85 | int main(void) | ||
| 86 | { | ||
| 87 | printf("=== test_lnurl_pay ===\n"); | ||
| 88 | |||
| 89 | printf("\n--- LNURL-pay response parsing ---\n"); | ||
| 90 | ASSERT(test_parse_lnurlp_response(), "parse lnurlp response: callback + min/max"); | ||
| 91 | |||
| 92 | printf("\n--- Callback response parsing ---\n"); | ||
| 93 | ASSERT(test_parse_callback_response(), "parse callback response: extract bolt11 'pr'"); | ||
| 94 | |||
| 95 | printf("\n--- Lightning address parsing ---\n"); | ||
| 96 | ASSERT(test_lightning_address_parse(), "split 'TollGate@coinos.io' into user + domain"); | ||
| 97 | |||
| 98 | printf("\n--- Amount validation ---\n"); | ||
| 99 | ASSERT(test_amount_validation(), "21 sats (21000 msat) within [1000, 1000000000]"); | ||
| 100 | |||
| 101 | printf("\n--- Missing @ in address ---\n"); | ||
| 102 | { | ||
| 103 | const char *addr = "no-at-sign.com"; | ||
| 104 | const char *at = strchr(addr, '@'); | ||
| 105 | ASSERT(at == NULL, "no @ returns NULL"); | ||
| 106 | } | ||
| 107 | |||
| 108 | printf("\n--- Missing callback in response ---\n"); | ||
| 109 | { | ||
| 110 | cJSON *root = cJSON_Parse(SAMPLE_MISSING_CALLBACK); | ||
| 111 | cJSON *cb = cJSON_GetObjectItemCaseSensitive(root, "callback"); | ||
| 112 | ASSERT(!cb, "missing callback detected"); | ||
| 113 | cJSON_Delete(root); | ||
| 114 | } | ||
| 115 | |||
| 116 | printf("\n--- Missing pr in callback response ---\n"); | ||
| 117 | { | ||
| 118 | cJSON *root = cJSON_Parse(SAMPLE_MISSING_PR); | ||
| 119 | cJSON *pr = cJSON_GetObjectItemCaseSensitive(root, "pr"); | ||
| 120 | ASSERT(!pr, "missing pr detected"); | ||
| 121 | cJSON_Delete(root); | ||
| 122 | } | ||
| 123 | |||
| 124 | TEST_SUMMARY(); | ||
| 125 | } | ||