diff options
Diffstat (limited to 'tests/unit/test_market.c')
| -rw-r--r-- | tests/unit/test_market.c | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/tests/unit/test_market.c b/tests/unit/test_market.c new file mode 100644 index 0000000..c19d26e --- /dev/null +++ b/tests/unit/test_market.c | |||
| @@ -0,0 +1,177 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/beacon_price.h" | ||
| 3 | #include "../../main/market.h" | ||
| 4 | #include "../../main/config.h" | ||
| 5 | #include "../../main/identity.h" | ||
| 6 | #include <string.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | #include <stdlib.h> | ||
| 9 | |||
| 10 | static tollgate_config_t g_test_config; | ||
| 11 | static tollgate_identity_t g_test_identity; | ||
| 12 | |||
| 13 | const tollgate_config_t *tollgate_config_get(void) { return &g_test_config; } | ||
| 14 | const tollgate_identity_t *identity_get(void) { return &g_test_identity; } | ||
| 15 | |||
| 16 | static void build_test_ie(tollgate_price_ie_t *ie, uint16_t price, uint32_t step, uint8_t metric, | ||
| 17 | const char *geohash, const char *mint_url, const char *npub_hex) | ||
| 18 | { | ||
| 19 | memset(ie, 0, sizeof(*ie)); | ||
| 20 | ie->element_id = 0xDD; | ||
| 21 | ie->length = 4 + TOLLGATE_IE_PAYLOAD_SIZE; | ||
| 22 | ie->vendor_oui[0] = TOLLGATE_OUI_0; | ||
| 23 | ie->vendor_oui[1] = TOLLGATE_OUI_1; | ||
| 24 | ie->vendor_oui[2] = TOLLGATE_OUI_2; | ||
| 25 | ie->vendor_oui_type = TOLLGATE_IE_TYPE; | ||
| 26 | |||
| 27 | ie->payload.version = TOLLGATE_IE_VERSION; | ||
| 28 | ie->payload.metric = metric; | ||
| 29 | ie->payload.price_per_step = price; | ||
| 30 | ie->payload.step_size = step; | ||
| 31 | |||
| 32 | if (mint_url) beacon_price_hash_mint(mint_url, ie->payload.mint_hash); | ||
| 33 | if (npub_hex) beacon_price_hash_npub(npub_hex, ie->payload.npub_hash); | ||
| 34 | |||
| 35 | uint8_t gh_len = (uint8_t)strnlen(geohash, TOLLGATE_IE_GEOHASH_MAX); | ||
| 36 | ie->payload.geohash_len = gh_len; | ||
| 37 | memcpy(ie->payload.geohash, geohash, gh_len); | ||
| 38 | } | ||
| 39 | |||
| 40 | static void reset_market(void) | ||
| 41 | { | ||
| 42 | market_t *m = (market_t *)market_get(); | ||
| 43 | memset(m, 0, sizeof(*m)); | ||
| 44 | } | ||
| 45 | |||
| 46 | int main(void) | ||
| 47 | { | ||
| 48 | printf("=== test_market ===\n"); | ||
| 49 | |||
| 50 | memset(&g_test_config, 0, sizeof(g_test_config)); | ||
| 51 | g_test_config.market_enabled = true; | ||
| 52 | g_test_config.market_scan_interval_s = 30; | ||
| 53 | strncpy(g_test_config.metric, "milliseconds", sizeof(g_test_config.metric) - 1); | ||
| 54 | |||
| 55 | memset(&g_test_identity, 0, sizeof(g_test_identity)); | ||
| 56 | strncpy(g_test_identity.npub_hex, "0000000000000000000000000000000000000000000000000000000000000001", 64); | ||
| 57 | g_test_identity.initialized = true; | ||
| 58 | |||
| 59 | printf("\n--- parse vendor IE (valid) ---\n"); | ||
| 60 | { | ||
| 61 | reset_market(); | ||
| 62 | tollgate_price_ie_t ie; | ||
| 63 | build_test_ie(&ie, 21, 60000, 0, "u281w0dfz", | ||
| 64 | "https://testnut.cashu.space", | ||
| 65 | "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); | ||
| 66 | |||
| 67 | uint8_t bssid[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x01}; | ||
| 68 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -45); | ||
| 69 | |||
| 70 | const market_t *m = market_get(); | ||
| 71 | ASSERT_EQ_INT(1, m->count, "one entry added"); | ||
| 72 | ASSERT(m->entries[0].valid, "entry is valid"); | ||
| 73 | ASSERT_EQ_INT(21, m->entries[0].price_per_step, "price is 21"); | ||
| 74 | ASSERT_EQ_INT(60000, (int)m->entries[0].step_size, "step_size is 60000"); | ||
| 75 | ASSERT_EQ_INT(0, m->entries[0].metric, "metric is 0 (time)"); | ||
| 76 | ASSERT_EQ_INT(-45, m->entries[0].rssi, "rssi is -45"); | ||
| 77 | ASSERT(memcmp(m->entries[0].bssid, bssid, 6) == 0, "bssid matches"); | ||
| 78 | } | ||
| 79 | |||
| 80 | printf("\n--- parse vendor IE (ignore self) ---\n"); | ||
| 81 | { | ||
| 82 | reset_market(); | ||
| 83 | tollgate_price_ie_t ie; | ||
| 84 | build_test_ie(&ie, 21, 60000, 0, "u281w0dfz", | ||
| 85 | "https://testnut.cashu.space", | ||
| 86 | g_test_identity.npub_hex); | ||
| 87 | |||
| 88 | uint8_t bssid[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x02}; | ||
| 89 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -50); | ||
| 90 | |||
| 91 | const market_t *m = market_get(); | ||
| 92 | ASSERT_EQ_INT(0, m->count, "self-entry ignored"); | ||
| 93 | } | ||
| 94 | |||
| 95 | printf("\n--- parse vendor IE (wrong OUI) ---\n"); | ||
| 96 | { | ||
| 97 | reset_market(); | ||
| 98 | tollgate_price_ie_t ie; | ||
| 99 | build_test_ie(&ie, 21, 60000, 0, "u281w0dfz", | ||
| 100 | "https://testnut.cashu.space", | ||
| 101 | "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); | ||
| 102 | ie.vendor_oui[0] = 0x00; | ||
| 103 | |||
| 104 | uint8_t bssid[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x03}; | ||
| 105 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -40); | ||
| 106 | |||
| 107 | const market_t *m = market_get(); | ||
| 108 | ASSERT_EQ_INT(0, m->count, "wrong OUI rejected"); | ||
| 109 | } | ||
| 110 | |||
| 111 | printf("\n--- market_find_cheapest ---\n"); | ||
| 112 | { | ||
| 113 | reset_market(); | ||
| 114 | |||
| 115 | tollgate_price_ie_t ie1, ie2, ie3; | ||
| 116 | build_test_ie(&ie1, 21, 60000, 0, "u281w0dfz", | ||
| 117 | "https://testnut.cashu.space", "aaa...npub1"); | ||
| 118 | build_test_ie(&ie2, 10, 60000, 0, "u281w0dfz", | ||
| 119 | "https://testnut.cashu.space", "bbb...npub2"); | ||
| 120 | build_test_ie(&ie3, 50, 60000, 0, "u281w0dfz", | ||
| 121 | "https://testnut.cashu.space", "ccc...npub3"); | ||
| 122 | |||
| 123 | uint8_t bssid1[6] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; | ||
| 124 | uint8_t bssid2[6] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02}; | ||
| 125 | uint8_t bssid3[6] = {0x03, 0x03, 0x03, 0x03, 0x03, 0x03}; | ||
| 126 | |||
| 127 | market_parse_vendor_ie(bssid1, (vendor_ie_data_t *)&ie1, -45); | ||
| 128 | market_parse_vendor_ie(bssid2, (vendor_ie_data_t *)&ie2, -50); | ||
| 129 | market_parse_vendor_ie(bssid3, (vendor_ie_data_t *)&ie3, -55); | ||
| 130 | |||
| 131 | const market_t *m = market_get(); | ||
| 132 | ASSERT_EQ_INT(3, m->count, "three entries"); | ||
| 133 | |||
| 134 | strncpy((char *)m->entries[0].ssid, "TollGate-A", 32); | ||
| 135 | strncpy((char *)m->entries[1].ssid, "TollGate-B", 32); | ||
| 136 | strncpy((char *)m->entries[2].ssid, "TollGate-C", 32); | ||
| 137 | |||
| 138 | int cheapest = market_find_cheapest(); | ||
| 139 | ASSERT(cheapest >= 0, "found a cheapest entry"); | ||
| 140 | ASSERT_EQ_INT(10, m->entries[cheapest].price_per_step, "cheapest is 10 sats"); | ||
| 141 | } | ||
| 142 | |||
| 143 | printf("\n--- update existing entry ---\n"); | ||
| 144 | { | ||
| 145 | reset_market(); | ||
| 146 | tollgate_price_ie_t ie; | ||
| 147 | build_test_ie(&ie, 21, 60000, 0, "u281w0dfz", | ||
| 148 | "https://testnut.cashu.space", "npub1"); | ||
| 149 | uint8_t bssid[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; | ||
| 150 | |||
| 151 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -45); | ||
| 152 | ASSERT_EQ_INT(1, market_get()->count, "first add"); | ||
| 153 | |||
| 154 | build_test_ie(&ie, 15, 60000, 0, "u281w0dfz", | ||
| 155 | "https://testnut.cashu.space", "npub1"); | ||
| 156 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -47); | ||
| 157 | ASSERT_EQ_INT(1, market_get()->count, "update doesn't increase count"); | ||
| 158 | ASSERT_EQ_INT(15, market_get()->entries[0].price_per_step, "price updated to 15"); | ||
| 159 | } | ||
| 160 | |||
| 161 | printf("\n--- geohash preserved ---\n"); | ||
| 162 | { | ||
| 163 | reset_market(); | ||
| 164 | tollgate_price_ie_t ie; | ||
| 165 | build_test_ie(&ie, 21, 60000, 0, "u281w0dfz", | ||
| 166 | "https://testnut.cashu.space", "npub1"); | ||
| 167 | uint8_t bssid[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; | ||
| 168 | |||
| 169 | market_parse_vendor_ie(bssid, (vendor_ie_data_t *)&ie, -40); | ||
| 170 | |||
| 171 | const market_t *m = market_get(); | ||
| 172 | ASSERT(m->entries[0].valid, "entry valid"); | ||
| 173 | ASSERT_EQ_STR("u281w0dfz", m->entries[0].geohash, "geohash is u281w0dfz"); | ||
| 174 | } | ||
| 175 | |||
| 176 | TEST_SUMMARY(); | ||
| 177 | } | ||