wifi_store_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Host test for the saved-WiFi list (wifi_store.c). The syscall layer is
  2. * replaced by a small in-memory FAT file, so the real load/save/remember/
  3. * forget paths run unchanged. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "syscall.h"
  7. /* Skip the real ucmd_lib.h (its wrappers are inline `int 0x80` calls) and
  8. * provide the two syscalls the store uses. */
  9. #define UCMD_LIB_H
  10. static unsigned char flash[8192];
  11. static unsigned int flash_len;
  12. static int flash_present;
  13. static int fake_syscall1(int num, int a1)
  14. {
  15. (void)a1;
  16. if (num == SYS_UNLINK) {
  17. flash_present = 0;
  18. flash_len = 0;
  19. return 0;
  20. }
  21. return -1;
  22. }
  23. static int fake_syscall3(int num, int a1, int a2, int a3)
  24. {
  25. (void)a1;
  26. if (num == SYS_READ_FILE) {
  27. unsigned int n;
  28. if (!flash_present)
  29. return -1;
  30. n = flash_len < (unsigned int)a3 ? flash_len : (unsigned int)a3;
  31. memcpy((void *)a2, flash, n);
  32. return (int)n;
  33. }
  34. if (num == SYS_WRITE_FILE) {
  35. if ((unsigned int)a3 > sizeof(flash))
  36. return -1;
  37. memcpy(flash, (const void *)a2, (unsigned int)a3);
  38. flash_len = (unsigned int)a3;
  39. flash_present = 1;
  40. return 0;
  41. }
  42. return -1;
  43. }
  44. #define u_syscall1 fake_syscall1
  45. #define u_syscall3 fake_syscall3
  46. #include "wifi_store.c"
  47. static int failures;
  48. static void check(int ok, const char *what)
  49. {
  50. if (!ok) {
  51. printf("FAIL: %s\n", what);
  52. failures++;
  53. }
  54. }
  55. static struct wifi_cfg rec(const char *ssid, const char *pass, int extra)
  56. {
  57. struct wifi_cfg c;
  58. unsigned int i;
  59. memset(&c, 0, sizeof(c));
  60. c.magic = WIFI_CFG_MAGIC;
  61. c.ssid_len = (unsigned char)strlen(ssid);
  62. c.pass_len = (unsigned char)strlen(pass);
  63. for (i = 0; i < c.ssid_len; i++)
  64. c.ssid[i] = (unsigned char)ssid[i];
  65. for (i = 0; i < c.pass_len; i++)
  66. c.pass[i] = pass[i];
  67. c.pass[63] = (char)('a' + extra % 26);
  68. return c;
  69. }
  70. static int is(const struct wifi_cfg *c, const char *ssid, const char *pass)
  71. {
  72. return c->ssid_len == strlen(ssid) &&
  73. memcmp(c->ssid, ssid, c->ssid_len) == 0 &&
  74. c->pass_len == strlen(pass) &&
  75. memcmp(c->pass, pass, c->pass_len) == 0;
  76. }
  77. static void reset(void)
  78. {
  79. memset(flash, 0, sizeof(flash));
  80. flash_len = 0;
  81. flash_present = 0;
  82. }
  83. int main(void)
  84. {
  85. struct wifi_cfg list[WIFI_CFG_MAX];
  86. struct wifi_cfg r;
  87. struct wifi_cfg_list *l;
  88. int i, n;
  89. /* An empty list removes the file. */
  90. reset();
  91. check(wifi_store_save(NULL, 0) == 0, "save empty");
  92. check(!flash_present, "empty file removed");
  93. /* Newest first. */
  94. check(wifi_store_remember(&(struct wifi_cfg){0}) < 0,
  95. "reject invalid record");
  96. r = rec("alpha", "pass-one", 0);
  97. check(wifi_store_remember(&r) == 0, "remember alpha");
  98. r = rec("bravo", "pass-two", 1);
  99. check(wifi_store_remember(&r) == 0, "remember bravo");
  100. n = wifi_store_load(list, WIFI_CFG_MAX);
  101. check(n == 2, "two records");
  102. check(is(&list[0], "bravo", "pass-two"), "bravo first");
  103. check(is(&list[1], "alpha", "pass-one"), "alpha second");
  104. /* Reconnecting updates the passphrase and moves the record to front. */
  105. r = rec("alpha", "new-pass", 2);
  106. check(wifi_store_remember(&r) == 0, "update alpha");
  107. n = wifi_store_load(list, WIFI_CFG_MAX);
  108. check(n == 2, "still two records");
  109. check(is(&list[0], "alpha", "new-pass"), "alpha updated and first");
  110. check(is(&list[1], "bravo", "pass-two"), "bravo second");
  111. check(wifi_store_find(list, n, (const unsigned char *)"alpha", 5) == 0,
  112. "find alpha");
  113. check(wifi_store_find(list, n, (const unsigned char *)"zulu", 4) == -1,
  114. "find missing");
  115. /* Full list drops the least recently used entry. */
  116. reset();
  117. for (i = 0; i < WIFI_CFG_MAX + 1; i++) {
  118. char ssid[16];
  119. snprintf(ssid, sizeof(ssid), "net%d", i);
  120. r = rec(ssid, "password", i);
  121. check(wifi_store_remember(&r) == 0, "remember full-list entry");
  122. }
  123. n = wifi_store_load(list, WIFI_CFG_MAX);
  124. check(n == WIFI_CFG_MAX, "list capped");
  125. check(is(&list[0], "net16", "password"), "newest kept");
  126. check(is(&list[WIFI_CFG_MAX - 1], "net1", "password"),
  127. "oldest dropped");
  128. /* Forget one record; forgetting a missing SSID reports not found. */
  129. check(wifi_store_forget((const unsigned char *)"net16", 5) == 1,
  130. "forget present");
  131. check(wifi_store_forget((const unsigned char *)"net16", 5) == 0,
  132. "forget missing");
  133. n = wifi_store_load(list, WIFI_CFG_MAX);
  134. check(n == WIFI_CFG_MAX - 1, "forget shrinks the list");
  135. check(is(&list[0], "net15", "password"), "next record promoted");
  136. /* The old one-record file is still read. */
  137. reset();
  138. r = rec("legacy", "oldpass", 0);
  139. memcpy(flash, &r, sizeof(r));
  140. flash_len = sizeof(r);
  141. flash_present = 1;
  142. n = wifi_store_load(list, WIFI_CFG_MAX);
  143. check(n == 1 && is(&list[0], "legacy", "oldpass"),
  144. "legacy record accepted");
  145. /* A truncated header and duplicate SSIDs are handled. */
  146. reset();
  147. memcpy(flash, &(unsigned int){ WIFI_CFG_LIST_MAGIC }, 4);
  148. flash_len = 8;
  149. flash_present = 1;
  150. check(wifi_store_load(list, WIFI_CFG_MAX) == 0, "truncated header");
  151. reset();
  152. l = (struct wifi_cfg_list *)flash;
  153. l->magic = WIFI_CFG_LIST_MAGIC;
  154. l->version = WIFI_CFG_LIST_VER;
  155. l->count = 2;
  156. l->net[0] = rec("dup", "pass", 0);
  157. l->net[1] = rec("dup", "pass", 0);
  158. flash_len = 12 + 2 * sizeof(struct wifi_cfg);
  159. flash_present = 1;
  160. n = wifi_store_load(list, WIFI_CFG_MAX);
  161. check(n == 1 && is(&list[0], "dup", "pass"), "duplicate dropped");
  162. /* A header that promises more records than the file holds is clamped
  163. * to the bytes actually present. */
  164. reset();
  165. l = (struct wifi_cfg_list *)flash;
  166. l->magic = WIFI_CFG_LIST_MAGIC;
  167. l->version = WIFI_CFG_LIST_VER;
  168. l->count = 5;
  169. l->net[0] = rec("first", "pass", 0);
  170. l->net[1] = rec("second", "pass", 0);
  171. flash_len = 12 + 2 * sizeof(struct wifi_cfg);
  172. flash_present = 1;
  173. n = wifi_store_load(list, WIFI_CFG_MAX);
  174. check(n == 2 && is(&list[0], "first", "pass") &&
  175. is(&list[1], "second", "pass"), "short list clamped");
  176. if (!failures)
  177. printf("wifi_store_test: all checks passed\n");
  178. return failures ? 1 : 0;
  179. }