| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /* Host test for the saved-WiFi list (wifi_store.c). The syscall layer is
- * replaced by a small in-memory FAT file, so the real load/save/remember/
- * forget paths run unchanged. */
- #include <stdio.h>
- #include <string.h>
- #include "syscall.h"
- /* Skip the real ucmd_lib.h (its wrappers are inline `int 0x80` calls) and
- * provide the two syscalls the store uses. */
- #define UCMD_LIB_H
- static unsigned char flash[8192];
- static unsigned int flash_len;
- static int flash_present;
- static int fake_syscall1(int num, int a1)
- {
- (void)a1;
- if (num == SYS_UNLINK) {
- flash_present = 0;
- flash_len = 0;
- return 0;
- }
- return -1;
- }
- static int fake_syscall3(int num, int a1, int a2, int a3)
- {
- (void)a1;
- if (num == SYS_READ_FILE) {
- unsigned int n;
- if (!flash_present)
- return -1;
- n = flash_len < (unsigned int)a3 ? flash_len : (unsigned int)a3;
- memcpy((void *)a2, flash, n);
- return (int)n;
- }
- if (num == SYS_WRITE_FILE) {
- if ((unsigned int)a3 > sizeof(flash))
- return -1;
- memcpy(flash, (const void *)a2, (unsigned int)a3);
- flash_len = (unsigned int)a3;
- flash_present = 1;
- return 0;
- }
- return -1;
- }
- #define u_syscall1 fake_syscall1
- #define u_syscall3 fake_syscall3
- #include "wifi_store.c"
- static int failures;
- static void check(int ok, const char *what)
- {
- if (!ok) {
- printf("FAIL: %s\n", what);
- failures++;
- }
- }
- static struct wifi_cfg rec(const char *ssid, const char *pass, int extra)
- {
- struct wifi_cfg c;
- unsigned int i;
- memset(&c, 0, sizeof(c));
- c.magic = WIFI_CFG_MAGIC;
- c.ssid_len = (unsigned char)strlen(ssid);
- c.pass_len = (unsigned char)strlen(pass);
- for (i = 0; i < c.ssid_len; i++)
- c.ssid[i] = (unsigned char)ssid[i];
- for (i = 0; i < c.pass_len; i++)
- c.pass[i] = pass[i];
- c.pass[63] = (char)('a' + extra % 26);
- return c;
- }
- static int is(const struct wifi_cfg *c, const char *ssid, const char *pass)
- {
- return c->ssid_len == strlen(ssid) &&
- memcmp(c->ssid, ssid, c->ssid_len) == 0 &&
- c->pass_len == strlen(pass) &&
- memcmp(c->pass, pass, c->pass_len) == 0;
- }
- static void reset(void)
- {
- memset(flash, 0, sizeof(flash));
- flash_len = 0;
- flash_present = 0;
- }
- int main(void)
- {
- struct wifi_cfg list[WIFI_CFG_MAX];
- struct wifi_cfg r;
- struct wifi_cfg_list *l;
- int i, n;
- /* An empty list removes the file. */
- reset();
- check(wifi_store_save(NULL, 0) == 0, "save empty");
- check(!flash_present, "empty file removed");
- /* Newest first. */
- check(wifi_store_remember(&(struct wifi_cfg){0}) < 0,
- "reject invalid record");
- r = rec("alpha", "pass-one", 0);
- check(wifi_store_remember(&r) == 0, "remember alpha");
- r = rec("bravo", "pass-two", 1);
- check(wifi_store_remember(&r) == 0, "remember bravo");
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == 2, "two records");
- check(is(&list[0], "bravo", "pass-two"), "bravo first");
- check(is(&list[1], "alpha", "pass-one"), "alpha second");
- /* Reconnecting updates the passphrase and moves the record to front. */
- r = rec("alpha", "new-pass", 2);
- check(wifi_store_remember(&r) == 0, "update alpha");
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == 2, "still two records");
- check(is(&list[0], "alpha", "new-pass"), "alpha updated and first");
- check(is(&list[1], "bravo", "pass-two"), "bravo second");
- check(wifi_store_find(list, n, (const unsigned char *)"alpha", 5) == 0,
- "find alpha");
- check(wifi_store_find(list, n, (const unsigned char *)"zulu", 4) == -1,
- "find missing");
- /* Full list drops the least recently used entry. */
- reset();
- for (i = 0; i < WIFI_CFG_MAX + 1; i++) {
- char ssid[16];
- snprintf(ssid, sizeof(ssid), "net%d", i);
- r = rec(ssid, "password", i);
- check(wifi_store_remember(&r) == 0, "remember full-list entry");
- }
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == WIFI_CFG_MAX, "list capped");
- check(is(&list[0], "net16", "password"), "newest kept");
- check(is(&list[WIFI_CFG_MAX - 1], "net1", "password"),
- "oldest dropped");
- /* Forget one record; forgetting a missing SSID reports not found. */
- check(wifi_store_forget((const unsigned char *)"net16", 5) == 1,
- "forget present");
- check(wifi_store_forget((const unsigned char *)"net16", 5) == 0,
- "forget missing");
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == WIFI_CFG_MAX - 1, "forget shrinks the list");
- check(is(&list[0], "net15", "password"), "next record promoted");
- /* The old one-record file is still read. */
- reset();
- r = rec("legacy", "oldpass", 0);
- memcpy(flash, &r, sizeof(r));
- flash_len = sizeof(r);
- flash_present = 1;
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == 1 && is(&list[0], "legacy", "oldpass"),
- "legacy record accepted");
- /* A truncated header and duplicate SSIDs are handled. */
- reset();
- memcpy(flash, &(unsigned int){ WIFI_CFG_LIST_MAGIC }, 4);
- flash_len = 8;
- flash_present = 1;
- check(wifi_store_load(list, WIFI_CFG_MAX) == 0, "truncated header");
- reset();
- l = (struct wifi_cfg_list *)flash;
- l->magic = WIFI_CFG_LIST_MAGIC;
- l->version = WIFI_CFG_LIST_VER;
- l->count = 2;
- l->net[0] = rec("dup", "pass", 0);
- l->net[1] = rec("dup", "pass", 0);
- flash_len = 12 + 2 * sizeof(struct wifi_cfg);
- flash_present = 1;
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == 1 && is(&list[0], "dup", "pass"), "duplicate dropped");
- /* A header that promises more records than the file holds is clamped
- * to the bytes actually present. */
- reset();
- l = (struct wifi_cfg_list *)flash;
- l->magic = WIFI_CFG_LIST_MAGIC;
- l->version = WIFI_CFG_LIST_VER;
- l->count = 5;
- l->net[0] = rec("first", "pass", 0);
- l->net[1] = rec("second", "pass", 0);
- flash_len = 12 + 2 * sizeof(struct wifi_cfg);
- flash_present = 1;
- n = wifi_store_load(list, WIFI_CFG_MAX);
- check(n == 2 && is(&list[0], "first", "pass") &&
- is(&list[1], "second", "pass"), "short list clamped");
- if (!failures)
- printf("wifi_store_test: all checks passed\n");
- return failures ? 1 : 0;
- }
|