diff options
| author | Your Name <you@example.com> | 2026-05-18 22:49:34 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 22:49:34 +0530 |
| commit | eb2ae72d91a4b2ec0333a2e6d18eeeeee6b60464 (patch) | |
| tree | eff643bcd00ed44b5d7fd93084be1391970d6bd4 /tests | |
| parent | 910fe47b2ed1284d93ff4d274f5c2341cb0d2d0b (diff) | |
feat: add WiFi setup state machine + config_add_wifi with unit tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/Makefile | 5 | ||||
| -rw-r--r-- | tests/unit/test_wifi_setup.c | 121 |
2 files changed, 125 insertions, 1 deletions
diff --git a/tests/unit/Makefile b/tests/unit/Makefile index b978891..0faf47e 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile | |||
| @@ -22,7 +22,7 @@ LDFLAGS := -lmbedcrypto -lcjson -lm | |||
| 22 | 22 | ||
| 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o | 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o |
| 24 | 24 | ||
| 25 | TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_touch test_keyboard | 25 | TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_touch test_keyboard test_wifi_setup |
| 26 | 26 | ||
| 27 | .PHONY: all test clean $(TESTS) | 27 | .PHONY: all test clean $(TESTS) |
| 28 | 28 | ||
| @@ -87,5 +87,8 @@ test_touch: test_touch.c $(REPO_ROOT)/main/touch.c | |||
| 87 | test_keyboard: test_keyboard.c $(REPO_ROOT)/main/keyboard.c | 87 | test_keyboard: test_keyboard.c $(REPO_ROOT)/main/keyboard.c |
| 88 | $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/keyboard.c -o $@ $(LDFLAGS) | 88 | $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/keyboard.c -o $@ $(LDFLAGS) |
| 89 | 89 | ||
| 90 | test_wifi_setup: test_wifi_setup.c $(REPO_ROOT)/main/wifi_setup.c | ||
| 91 | $(CC) $(CFLAGS) $< $(REPO_ROOT)/main/wifi_setup.c -o $@ $(LDFLAGS) | ||
| 92 | |||
| 90 | clean: | 93 | clean: |
| 91 | rm -f $(TESTS) $(SECP256K1_OBJ) | 94 | rm -f $(TESTS) $(SECP256K1_OBJ) |
diff --git a/tests/unit/test_wifi_setup.c b/tests/unit/test_wifi_setup.c new file mode 100644 index 0000000..5f1b8f0 --- /dev/null +++ b/tests/unit/test_wifi_setup.c | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/wifi_setup.h" | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | int main(void) | ||
| 6 | { | ||
| 7 | printf("=== test_wifi_setup ===\n"); | ||
| 8 | |||
| 9 | wifi_setup_t setup; | ||
| 10 | wifi_setup_init(&setup); | ||
| 11 | |||
| 12 | ASSERT_EQ_INT(SETUP_SCAN, (int)setup.state, "Init state = SCAN"); | ||
| 13 | ASSERT_EQ_INT(0, setup.ap_count, "Init ap_count = 0"); | ||
| 14 | ASSERT_EQ_INT(-1, setup.selected_ap, "Init selected_ap = -1"); | ||
| 15 | |||
| 16 | wifi_ap_info_t test_aps[3] = { | ||
| 17 | {"FastNet", -30, true}, | ||
| 18 | {"SlowNet", -70, true}, | ||
| 19 | {"OpenNet", -50, false}, | ||
| 20 | }; | ||
| 21 | wifi_setup_set_aps(&setup, test_aps, 3); | ||
| 22 | |||
| 23 | ASSERT_EQ_INT(SETUP_LIST, (int)setup.state, "After set_aps: state = LIST"); | ||
| 24 | ASSERT_EQ_INT(3, setup.ap_count, "After set_aps: ap_count = 3"); | ||
| 25 | ASSERT_EQ_INT(3, wifi_setup_visible_count(&setup), "Visible count = 3"); | ||
| 26 | |||
| 27 | { | ||
| 28 | const wifi_ap_info_t *ap = wifi_setup_get_visible(&setup, 0); | ||
| 29 | ASSERT(ap != NULL, "Visible AP 0 is not NULL"); | ||
| 30 | ASSERT_EQ_STR("FastNet", ap->ssid, "AP 0 = FastNet"); | ||
| 31 | ASSERT_EQ_INT(-30, ap->rssi, "AP 0 RSSI = -30"); | ||
| 32 | ASSERT(ap->secured, "AP 0 is secured"); | ||
| 33 | } | ||
| 34 | |||
| 35 | { | ||
| 36 | const wifi_ap_info_t *ap = wifi_setup_get_visible(&setup, 2); | ||
| 37 | ASSERT(ap != NULL, "Visible AP 2 is not NULL"); | ||
| 38 | ASSERT_EQ_STR("OpenNet", ap->ssid, "AP 2 = OpenNet"); | ||
| 39 | ASSERT(!ap->secured, "AP 2 is open"); | ||
| 40 | } | ||
| 41 | |||
| 42 | { | ||
| 43 | const wifi_ap_info_t *ap = wifi_setup_get_visible(&setup, 3); | ||
| 44 | ASSERT(ap == NULL, "Out of range returns NULL"); | ||
| 45 | } | ||
| 46 | |||
| 47 | setup_state_t s = wifi_setup_handle_select(&setup, 0); | ||
| 48 | ASSERT_EQ_INT(SETUP_PASSWORD, (int)s, "Select AP 0: state = PASSWORD"); | ||
| 49 | ASSERT_EQ_INT(0, setup.selected_ap, "Selected AP index = 0"); | ||
| 50 | ASSERT_EQ_STR("FastNet", setup.selected_ssid, "Selected SSID = FastNet"); | ||
| 51 | |||
| 52 | wifi_setup_handle_connect(&setup); | ||
| 53 | ASSERT_EQ_INT(SETUP_CONNECTING, (int)setup.state, "Connect: state = CONNECTING"); | ||
| 54 | |||
| 55 | s = wifi_setup_handle_connect_result(&setup, true, "192.168.1.42"); | ||
| 56 | ASSERT_EQ_INT(SETUP_SUCCESS, (int)s, "Connect success: state = SUCCESS"); | ||
| 57 | ASSERT_EQ_STR("192.168.1.42", setup.connect_ip, "Connect IP stored"); | ||
| 58 | |||
| 59 | wifi_setup_init(&setup); | ||
| 60 | wifi_setup_set_aps(&setup, test_aps, 3); | ||
| 61 | wifi_setup_handle_select(&setup, 1); | ||
| 62 | wifi_setup_handle_connect(&setup); | ||
| 63 | |||
| 64 | s = wifi_setup_handle_connect_result(&setup, false, NULL); | ||
| 65 | ASSERT_EQ_INT(SETUP_FAILED, (int)s, "Connect fail: state = FAILED"); | ||
| 66 | ASSERT(setup.connect_failed_auth, "Failed auth flag set"); | ||
| 67 | |||
| 68 | s = wifi_setup_handle_retry(&setup); | ||
| 69 | ASSERT_EQ_INT(SETUP_PASSWORD, (int)s, "Retry: state = PASSWORD"); | ||
| 70 | ASSERT(!setup.connect_failed_auth, "Retry clears auth flag"); | ||
| 71 | |||
| 72 | wifi_setup_init(&setup); | ||
| 73 | wifi_setup_set_aps(&setup, test_aps, 3); | ||
| 74 | wifi_setup_handle_select(&setup, 0); | ||
| 75 | wifi_setup_handle_connect(&setup); | ||
| 76 | wifi_setup_handle_connect_result(&setup, false, NULL); | ||
| 77 | |||
| 78 | s = wifi_setup_handle_change_network(&setup); | ||
| 79 | ASSERT_EQ_INT(SETUP_LIST, (int)s, "Change network: state = LIST"); | ||
| 80 | |||
| 81 | wifi_setup_init(&setup); | ||
| 82 | s = wifi_setup_handle_cancel(&setup); | ||
| 83 | ASSERT_EQ_INT(SETUP_CANCELLED, (int)s, "Cancel: state = CANCELLED"); | ||
| 84 | |||
| 85 | wifi_setup_init(&setup); | ||
| 86 | wifi_ap_info_t many_aps[12]; | ||
| 87 | for (int i = 0; i < 12; i++) { | ||
| 88 | snprintf(many_aps[i].ssid, sizeof(many_aps[i].ssid), "Net%d", i); | ||
| 89 | many_aps[i].rssi = -30 - i * 5; | ||
| 90 | many_aps[i].secured = true; | ||
| 91 | } | ||
| 92 | wifi_setup_set_aps(&setup, many_aps, 12); | ||
| 93 | ASSERT_EQ_INT(12, setup.ap_count, "12 APs stored"); | ||
| 94 | ASSERT_EQ_INT(8, wifi_setup_visible_count(&setup), "Only 8 visible"); | ||
| 95 | |||
| 96 | { | ||
| 97 | const wifi_ap_info_t *ap = wifi_setup_get_visible(&setup, 7); | ||
| 98 | ASSERT(ap != NULL, "8th visible AP exists"); | ||
| 99 | ASSERT_EQ_STR("Net7", ap->ssid, "8th visible = Net7"); | ||
| 100 | } | ||
| 101 | |||
| 102 | wifi_setup_init(&setup); | ||
| 103 | s = wifi_setup_handle_select(&setup, 0); | ||
| 104 | ASSERT_EQ_INT(SETUP_SCAN, (int)s, "Select in SCAN state = no change"); | ||
| 105 | |||
| 106 | s = wifi_setup_handle_select(&setup, -1); | ||
| 107 | ASSERT_EQ_INT(SETUP_SCAN, (int)s, "Select invalid idx = no change"); | ||
| 108 | |||
| 109 | wifi_setup_init(&setup); | ||
| 110 | wifi_setup_set_aps(&setup, test_aps, 3); | ||
| 111 | s = wifi_setup_handle_retry(&setup); | ||
| 112 | ASSERT_EQ_INT(SETUP_LIST, (int)s, "Retry in LIST state = no change"); | ||
| 113 | |||
| 114 | s = wifi_setup_handle_change_network(&setup); | ||
| 115 | ASSERT_EQ_INT(SETUP_LIST, (int)s, "Change network in LIST state = no change"); | ||
| 116 | |||
| 117 | ASSERT_EQ_INT(0, wifi_setup_visible_count(NULL), "NULL setup returns 0"); | ||
| 118 | ASSERT(NULL == wifi_setup_get_visible(NULL, 0), "NULL setup returns NULL AP"); | ||
| 119 | |||
| 120 | TEST_SUMMARY(); | ||
| 121 | } | ||