test_platform_config.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
  2. #include "Config.h"
  3. #include "DAC.pb.h"
  4. #include "DAC_test_extra.pb.h"
  5. #include "DAC_test_extra2.pb.h"
  6. #include "Locking.h"
  7. #include "PBW.h"
  8. #include "State.pb.h"
  9. #include "WifiList.h"
  10. #include "configuration.pb.h"
  11. #include "esp_log.h"
  12. #include "test_common_init.h"
  13. #include "tools.h"
  14. #include "unity.h"
  15. using namespace System;
  16. // Helper macro to stringify the expanded value of a macro
  17. #define STRINGIFY(x) #x
  18. #define TOSTRING(x) STRINGIFY(x)
  19. // Use the helper macro to stringify LOG_LOCAL_LEVEL
  20. #pragma message("The current log local level value is " TOSTRING(LOG_LOCAL_LEVEL))
  21. #define AUTH_MODE_INDEX(i) ((start_auth_mode + i) % WIFI_AUTH_MAX == 0 ? (wifi_auth_mode_t)1 : (wifi_auth_mode_t)(start_auth_mode + i))
  22. static const char* config_file_name = "settings.bin";
  23. static const uint8_t bssid[6] = {0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56};
  24. uint8_t fill_last = 0x56;
  25. uint8_t start_rssi = 70;
  26. uint8_t start_channel = 2;
  27. auto start_auth_mode = WIFI_AUTH_WEP;
  28. uint8_t fill_bssid[6] = {0xAB, 0xCD, 0xEF, 0x12, 0x34, fill_last};
  29. static const char* char_bssid = "AB:CD:EF:12:34:56";
  30. static const char* TAG = "test_platform_config";
  31. const char* password = "TestPassword";
  32. bool HasMemoryUsageIncreased(int round) {
  33. static const size_t thresholdInternal = 500; // Example threshold for internal memory
  34. static const size_t thresholdSPIRAM = 1000; // Example threshold for SPI RAM
  35. size_t postTestFreeInternal = 0;
  36. size_t postTestFreeSPIRAM = 0;
  37. static size_t initialFreeInternal = 0;
  38. static size_t initialFreeSPIRAM = 0;
  39. auto minFreeInternal = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
  40. auto minFreeSPIRAM = heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM);
  41. if (round > 0) {
  42. postTestFreeInternal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  43. postTestFreeSPIRAM = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  44. minFreeInternal = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
  45. minFreeSPIRAM = heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM);
  46. printf("Memory usage summary (after round %d): "
  47. "Internal: %zu->%zu bytes. Min free: %zu. Increase: %d bytes. "
  48. "SPIRAM: %zu->%zu bytes. Min free: %zu. Increase: %d bytes\n",
  49. round, initialFreeInternal, postTestFreeInternal, minFreeInternal, initialFreeInternal - postTestFreeInternal, initialFreeSPIRAM,
  50. postTestFreeSPIRAM, minFreeSPIRAM, initialFreeSPIRAM - postTestFreeSPIRAM);
  51. int32_t diffInternal = initialFreeInternal > postTestFreeInternal ? initialFreeInternal - postTestFreeInternal : 0;
  52. int32_t diffSPIRAM = initialFreeSPIRAM > postTestFreeSPIRAM ? initialFreeSPIRAM - postTestFreeSPIRAM : 0;
  53. if (diffSPIRAM > 0 || diffInternal > 0) {
  54. ESP_LOGW(TAG, "Internal increase: %d, SPIRAM: %d", diffInternal, diffSPIRAM);
  55. return true;
  56. }
  57. } else {
  58. initialFreeInternal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  59. initialFreeSPIRAM = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  60. printf("Memory usage at start: "
  61. "Internal: %zu bytes. Min free: %zu. "
  62. "SPIRAM: %zu bytes. Min free: %zu.\n",
  63. initialFreeInternal, minFreeInternal, initialFreeSPIRAM, minFreeSPIRAM);
  64. }
  65. return false;
  66. }
  67. void advanceTime(int seconds) {
  68. struct timeval tv;
  69. gettimeofday(&tv, NULL);
  70. tv.tv_sec += seconds;
  71. tv.tv_usec += seconds * 100;
  72. settimeofday((const timeval*)&tv, 0);
  73. }
  74. wifi_event_sta_connected_t getMockConnectedEvent(int i = 0) {
  75. wifi_event_sta_connected_t mock = {};
  76. mock.authmode = WIFI_AUTH_WPA3_PSK;
  77. memset(mock.ssid, 0x00, sizeof(mock.ssid));
  78. memset(mock.bssid, 0x00, sizeof(mock.bssid));
  79. snprintf((char*)mock.ssid, sizeof(mock.ssid), "SSID_%d", i);
  80. fill_bssid[5] = fill_last + i;
  81. memcpy(mock.bssid, fill_bssid, sizeof(mock.bssid));
  82. mock.channel = 6 + i;
  83. mock.ssid_len = strlen((char*)mock.ssid);
  84. return mock;
  85. }
  86. wifi_ap_record_t getMockAPRec(int i = 0) {
  87. wifi_ap_record_t mock = {};
  88. mock.primary = start_channel + i; // Set some channel
  89. mock.rssi = start_rssi + i; // Set some RSSI value
  90. memset(mock.ssid, 0x00, sizeof(mock.ssid));
  91. memset(mock.bssid, 0x00, sizeof(mock.bssid));
  92. snprintf((char*)mock.ssid, sizeof(mock.ssid), "SSID_%d", i);
  93. fill_bssid[5] = fill_last + i;
  94. memcpy(mock.bssid, fill_bssid, sizeof(mock.bssid));
  95. mock.second = WIFI_SECOND_CHAN_ABOVE;
  96. mock.authmode = AUTH_MODE_INDEX(i); /**< authmode of AP */
  97. mock.pairwise_cipher = WIFI_CIPHER_TYPE_AES_GMAC128; /**< pairwise cipher of AP */
  98. mock.group_cipher = WIFI_CIPHER_TYPE_TKIP_CCMP; /**< group cipher of AP */
  99. mock.ant = WIFI_ANT_ANT1; /**< antenna used to receive beacon from AP */
  100. mock.phy_11b = 1; /**< bit: 0 flag to identify if 11b mode is enabled or not */
  101. mock.phy_11g = 0; /**< bit: 1 flag to identify if 11g mode is enabled or not */
  102. mock.phy_11n = 1; /**< bit: 2 flag to identify if 11n mode is enabled or not */
  103. mock.phy_lr = 0; /**< bit: 3 flag to identify if low rate is enabled or not */
  104. mock.wps = 1; /**< bit: 4 flag to identify if WPS is supported or not */
  105. mock.ftm_responder = 0; /**< bit: 5 flag to identify if FTM is supported in responder mode */
  106. mock.ftm_initiator = 1; /**< bit: 6 flag to identify if FTM is supported in initiator mode */
  107. return mock;
  108. }
  109. wifi_sta_config_t getMockSTA(int i = 0) {
  110. wifi_sta_config_t mock = {};
  111. memset(mock.ssid, 0x00, sizeof(mock.ssid));
  112. memset(mock.bssid, 0x00, sizeof(mock.bssid));
  113. snprintf((char*)mock.password, sizeof(mock.password), "Password_%d", i);
  114. snprintf((char*)mock.ssid, sizeof(mock.ssid), "SSID_%d", i);
  115. fill_bssid[5] = fill_last + i;
  116. memcpy(mock.bssid, fill_bssid, sizeof(mock.bssid));
  117. mock.channel = start_channel + i;
  118. mock.failure_retry_cnt = 2 + i;
  119. mock.listen_interval = 4 + i;
  120. mock.mbo_enabled = 1;
  121. mock.scan_method = WIFI_ALL_CHANNEL_SCAN;
  122. return mock;
  123. }
  124. sys_net_wifi_entry getMockEntry(int i = 0) {
  125. sys_net_wifi_entry mock = sys_net_wifi_entry_init_default;
  126. mock.auth_type = WifiList::GetAuthType(AUTH_MODE_INDEX(i));
  127. snprintf((char*)mock.password, sizeof(mock.password), "Password_%d", i);
  128. snprintf((char*)mock.ssid, sizeof(mock.ssid), "SSID_%d", i);
  129. fill_bssid[5] = fill_last + i;
  130. WifiList::FormatBSSID(mock.bssid, sizeof(mock.bssid), fill_bssid);
  131. mock.channel = start_channel + i;
  132. mock.connected = false;
  133. mock.has_last_seen = false;
  134. mock.has_last_try = false;
  135. mock.radio_type_count = 3;
  136. mock.radio_type = new sys_net_radio_types[mock.radio_type_count];
  137. mock.radio_type[0] = sys_net_radio_types_PHY_11B;
  138. mock.radio_type[1] = sys_net_radio_types_PHY_11G;
  139. mock.radio_type[2] = sys_net_radio_types_PHY_11N;
  140. mock.rssi = start_rssi + i;
  141. return mock;
  142. }
  143. void FillSSIDs(WifiList& manager, int count, int start=1) {
  144. for (int i = start; i <= count; i++) {
  145. auto mock = getMockSTA(i);
  146. auto& entry = manager.AddUpdate(&mock);
  147. entry.rssi = 70 + i;
  148. entry.connected = true;
  149. }
  150. }
  151. void FillSSIDFromAPRec(WifiList& manager, int count, int start=1) {
  152. for (int i = start; i <= count; i++) {
  153. auto mock = getMockAPRec(i);
  154. auto& entry = manager.AddUpdate(&mock);
  155. entry.rssi = 70 + i;
  156. entry.connected = true;
  157. }
  158. }
  159. void FillSSIDFromMockEntry(WifiList& manager, int count, int start=1) {
  160. for (int i = start; i <= count; i++) {
  161. auto mock = getMockEntry(i);
  162. auto& entry = manager.AddUpdate(&mock);
  163. entry.rssi = 70 + i;
  164. entry.connected = true;
  165. WifiList::Release(mock);
  166. }
  167. }
  168. void eraseConfigFile() {
  169. PB<sys_config> wrapper(std::string("config"), &sys_config_msg, sizeof(sys_config_msg), false);
  170. erase_path(wrapper.GetFileName().c_str(), false);
  171. }
  172. TEST_CASE("Test empty target settings empty", "[platform_config]") {
  173. PB<sys_config> wrapper(std::string("Config"), &sys_config_msg, sizeof(sys_config_msg), false);
  174. sys_config* conf = wrapper.get();
  175. assert(conf != nullptr);
  176. conf->target = strdup_psram("");
  177. #ifdef CONFIG_FW_PLATFORM_NAME
  178. system_set_string(&sys_config_msg, sys_config_target_tag, conf, CONFIG_FW_PLATFORM_NAME);
  179. TEST_ASSERT_TRUE(strcmp(conf->target, CONFIG_FW_PLATFORM_NAME) == 0);
  180. #endif
  181. }
  182. TEST_CASE("Test init from default config", "[platform_config]") {
  183. struct stat fileInformation;
  184. PB<sys_config> wrapper(std::string("config"), &sys_config_msg, sizeof(sys_config_msg), false);
  185. sys_config* confprt = wrapper.get();
  186. auto filename = wrapper.GetFileName();
  187. erase_path(filename.c_str(), false);
  188. TEST_ASSERT_FALSE(get_file_info(&fileInformation, config_file_name));
  189. TEST_ASSERT_TRUE(strlen(STR_OR_BLANK(confprt->target)) == 0);
  190. TEST_ASSERT_FALSE(confprt->has_dev);
  191. }
  192. const sys_config* GetTestConfig() {
  193. static sys_config test_config;
  194. memset(&test_config, 0x00, sizeof(test_config));
  195. // Assuming test_config is an instance of sys_config or a similar structure
  196. test_config.has_dev = true;
  197. test_config.dev.has_spi = true;
  198. test_config.dev.spi.mosi = 4;
  199. test_config.dev.spi.clk = 5;
  200. test_config.dev.spi.dc = 18;
  201. test_config.dev.spi.host = sys_dev_common_hosts_Host1;
  202. test_config.dev.has_dac = true;
  203. test_config.dev.dac.bck = 25;
  204. test_config.dev.dac.ws = 26;
  205. test_config.dev.dac.dout = 33;
  206. test_config.dev.dac.model = sys_dac_models_WM8978;
  207. test_config.dev.has_display = true;
  208. test_config.dev.display.has_common = true;
  209. test_config.dev.display.common.width = 256;
  210. test_config.dev.display.common.height = 64;
  211. test_config.dev.display.common.HFlip = false;
  212. test_config.dev.display.common.VFlip = false;
  213. test_config.dev.display.common.rotate = false;
  214. test_config.dev.display.common.driver = sys_display_drivers_SSD1322;
  215. test_config.dev.display.common.reset = 21;
  216. test_config.dev.display.which_dispType = sys_display_config_spi_tag;
  217. test_config.dev.display.dispType.spi.cs = 19;
  218. test_config.dev.display.dispType.spi.speed = 8000000;
  219. test_config.dev.has_rotary = true;
  220. test_config.dev.rotary.A = 23;
  221. test_config.dev.rotary.B = 22;
  222. test_config.dev.rotary.SW = 34;
  223. test_config.dev.rotary.volume = true;
  224. test_config.dev.rotary.longpress = true;
  225. test_config.has_names = true;
  226. strcpy(test_config.names.device, "test_name");
  227. if (!test_config.target) {
  228. test_config.target = strdup_psram("test_target");
  229. }
  230. return &test_config;
  231. }
  232. void check_sys_config_structure(sys_config* config) {
  233. auto check = GetTestConfig();
  234. // Test SPI configuration
  235. TEST_ASSERT_EQUAL(check->dev.has_spi, config->dev.has_spi);
  236. TEST_ASSERT_EQUAL(check->dev.spi.mosi, config->dev.spi.mosi);
  237. TEST_ASSERT_EQUAL(check->dev.spi.clk, config->dev.spi.clk);
  238. TEST_ASSERT_EQUAL(check->dev.spi.dc, config->dev.spi.dc);
  239. TEST_ASSERT_EQUAL(check->dev.spi.host, config->dev.spi.host);
  240. TEST_ASSERT_EQUAL(check->dev.has_dac, config->dev.has_dac);
  241. TEST_ASSERT_EQUAL(check->dev.dac.bck, config->dev.dac.bck);
  242. TEST_ASSERT_EQUAL(check->dev.dac.ws, config->dev.dac.ws);
  243. TEST_ASSERT_EQUAL(check->dev.dac.dout, config->dev.dac.dout);
  244. TEST_ASSERT_EQUAL(check->dev.dac.model, config->dev.dac.model);
  245. TEST_ASSERT_EQUAL(check->dev.has_display, config->dev.has_display);
  246. TEST_ASSERT_EQUAL(check->dev.display.common.width, config->dev.display.common.width);
  247. TEST_ASSERT_EQUAL(check->dev.display.common.height, config->dev.display.common.height);
  248. TEST_ASSERT_EQUAL(check->dev.display.common.HFlip, config->dev.display.common.HFlip);
  249. TEST_ASSERT_EQUAL(check->dev.display.common.VFlip, config->dev.display.common.VFlip);
  250. TEST_ASSERT_EQUAL(check->dev.display.common.rotate, config->dev.display.common.rotate);
  251. TEST_ASSERT_EQUAL(check->dev.display.common.driver, config->dev.display.common.driver);
  252. TEST_ASSERT_EQUAL(check->dev.display.common.reset, config->dev.display.common.reset);
  253. TEST_ASSERT_EQUAL(check->dev.display.which_dispType, config->dev.display.which_dispType);
  254. TEST_ASSERT_EQUAL(check->dev.display.dispType.spi.cs, config->dev.display.dispType.spi.cs);
  255. TEST_ASSERT_EQUAL(check->dev.display.dispType.spi.speed, config->dev.display.dispType.spi.speed);
  256. TEST_ASSERT_EQUAL(check->dev.has_rotary, config->dev.has_rotary);
  257. TEST_ASSERT_EQUAL(check->dev.rotary.A, config->dev.rotary.A);
  258. TEST_ASSERT_EQUAL(check->dev.rotary.B, config->dev.rotary.B);
  259. TEST_ASSERT_EQUAL(check->dev.rotary.SW, config->dev.rotary.SW);
  260. TEST_ASSERT_EQUAL(check->dev.rotary.volume, config->dev.rotary.volume);
  261. TEST_ASSERT_EQUAL(check->dev.rotary.longpress, config->dev.rotary.longpress);
  262. TEST_ASSERT_EQUAL_STRING(check->names.device, config->names.device);
  263. TEST_ASSERT_EQUAL_STRING(check->target, config->target);
  264. }
  265. TEST_CASE("Test change platform", "[platform_config]") {
  266. struct stat fileInformation;
  267. eraseConfigFile();
  268. std::stringstream test_target_file;
  269. test_target_file << spiffs_base_path << "/targets/" << GetTestConfig()->target << "/config.bin";
  270. PlatformConfig::ProtoWrapperHelper::CommitFile(test_target_file.str().c_str(), &sys_config_msg, GetTestConfig());
  271. // first ensure that the target state file exists.
  272. TEST_ASSERT_TRUE(get_file_info(&fileInformation, test_target_file.str().c_str()));
  273. TEST_ASSERT_TRUE(fileInformation.st_size > 0);
  274. platform = nullptr;
  275. // here we must use the configurator object
  276. // since we're testing some config_ functions
  277. std::string expectedTarget = "ESP32";
  278. TEST_ASSERT_NULL(platform);
  279. config_load();
  280. TEST_ASSERT_NOT_NULL(platform);
  281. TEST_ASSERT_NOT_NULL(platform->target)
  282. TEST_ASSERT_EQUAL_STRING(expectedTarget.c_str(), platform->target);
  283. config_set_target_reset(GetTestConfig()->target);
  284. check_sys_config_structure(platform);
  285. TEST_ASSERT_EQUAL_STRING(platform->target, GetTestConfig()->target);
  286. TEST_ASSERT_TRUE(erase_path(test_target_file.str().c_str(), false));
  287. TEST_ASSERT_FALSE(get_file_info(&fileInformation, test_target_file.str().c_str()));
  288. }
  289. // TEST_CASE("Test load state", "[platform_config]") {
  290. // eraseConfigFile();
  291. // const char* urlvalue = "http://somerandomurl";
  292. // config_load();
  293. // system_set_string(&sys_state_data_msg, sys_state_data_ota_url_tag, sys_state, urlvalue);
  294. // TEST_ASSERT_EQUAL_STRING(urlvalue, sys_state->ota_url);
  295. // ESP_LOGI(TAG, "Raising state change");
  296. // config_raise_state_changed();
  297. // // create an async timer lambda to trigger the commit after 1 second so
  298. // //we can test waitcommit
  299. // config_commit_config
  300. // TEST_CASE("Test Raise State Change", "[platform_config]") {
  301. // // config_load();
  302. // ESP_LOGI(TAG, "Raising state change");
  303. // TEST_ASSERT_FALSE(configurator.HasStateChanges());
  304. // TEST_ASSERT_FALSE(configurator.HasChanges());
  305. // config_raise_state_changed();
  306. // TEST_ASSERT_TRUE(configurator.HasStateChanges());
  307. // TEST_ASSERT_FALSE(configurator.HasChanges());
  308. // configurator.ResetStateModified();
  309. // TEST_ASSERT_FALSE(configurator.HasStateChanges());
  310. // TEST_ASSERT_FALSE(configurator.HasChanges());
  311. // }
  312. // TEST_CASE("Test Raise Change", "[platform_config]") {
  313. // // config_load();
  314. // ESP_LOGI(TAG, "Raising change");
  315. // PlatformConfig::PB wrapper =
  316. // PlatformConfig::PB("config", "", &sys_config_msg, Init_sys_config);
  317. // TEST_ASSERT_FALSE(configurator.HasStateChanges());
  318. // TEST_ASSERT_FALSE(configurator.HasChanges());
  319. // config_raise_changed();
  320. // TEST_ASSERT_TRUE(configurator.HasChanges());
  321. // TEST_ASSERT_FALSE(configurator.HasStateChanges());
  322. // configurator.ResetModified();
  323. // TEST_ASSERT_FALSE(configurator.HasChanges());
  324. // TEST_ASSERT_FALSE(configurator.HasStateChanges());
  325. // }
  326. TEST_CASE("Test Lock Unlock", "[platform_config]") {
  327. auto lock = PlatformConfig::Locking("test");
  328. TEST_ASSERT_FALSE(lock.IsLocked());
  329. TEST_ASSERT_TRUE(lock.Lock());
  330. TEST_ASSERT_TRUE(lock.Lock());
  331. TEST_ASSERT_TRUE(lock.IsLocked());
  332. lock.Unlock();
  333. TEST_ASSERT_TRUE(lock.IsLocked());
  334. lock.Unlock();
  335. TEST_ASSERT_FALSE(lock.IsLocked());
  336. }
  337. TEST_CASE("Recovery not updating message definition", "[platform_config]") {
  338. is_recovery_running = false;
  339. auto name = std::string("extra");
  340. struct stat struct_info_extra;
  341. struct stat struct_info_reco;
  342. // create instance with fresh definition
  343. erase_path(PBHelper::GetDefFileName(name).c_str(), false);
  344. PB<sys_dac_extra_config> wrapper_extra(name.c_str(), &sys_dac_extra_config_msg, sizeof(sys_dac_extra_config_msg));
  345. TEST_ASSERT_TRUE(get_file_info(&struct_info_extra, wrapper_extra.GetDefFileName().c_str()));
  346. TEST_ASSERT_TRUE(struct_info_extra.st_size > 0);
  347. auto& extra = wrapper_extra.Root();
  348. extra.dummy1 = 20;
  349. extra.dummy2 = 30;
  350. extra.has_dummy3 = true;
  351. extra.dummy3.level = sys_gpio_lvl_HIGH;
  352. extra.dummy3.pin = 22;
  353. wrapper_extra.CommitChanges();
  354. is_recovery_running = true;
  355. PB<sys_dac_config> wrapper(name, &sys_dac_config_msg, sizeof(sys_dac_config_msg));
  356. TEST_ASSERT_TRUE(get_file_info(&struct_info_reco, wrapper.GetDefFileName().c_str()));
  357. TEST_ASSERT_TRUE(struct_info_reco.st_size == struct_info_extra.st_size);
  358. TEST_ASSERT_EQUAL(wrapper_extra.GetDataSize(), wrapper.GetDataSize());
  359. wrapper.LoadFile(true);
  360. sys_dac_extra_config* config = reinterpret_cast<sys_dac_extra_config*>(wrapper.get());
  361. TEST_ASSERT_EQUAL(config->dummy1, extra.dummy1);
  362. TEST_ASSERT_EQUAL(config->dummy2, extra.dummy2);
  363. TEST_ASSERT_EQUAL(config->has_dummy3, extra.has_dummy3);
  364. TEST_ASSERT_EQUAL(config->dummy3.level, extra.dummy3.level);
  365. TEST_ASSERT_EQUAL(config->dummy3.pin, extra.dummy3.pin);
  366. config->bck = 55;
  367. wrapper.CommitChanges();
  368. PB<sys_dac_extra_config> check_structure(name.c_str(), &sys_dac_extra_config_msg, sizeof(sys_dac_extra_config_msg));
  369. check_structure.LoadFile(true);
  370. auto config_check = check_structure.get();
  371. TEST_ASSERT_EQUAL(config->bck, check_structure.get()->bck);
  372. TEST_ASSERT_EQUAL(config_check->dummy1, extra.dummy1);
  373. TEST_ASSERT_EQUAL(config_check->dummy2, extra.dummy2);
  374. TEST_ASSERT_EQUAL(config_check->has_dummy3, extra.has_dummy3);
  375. TEST_ASSERT_EQUAL(config_check->dummy3.level, extra.dummy3.level);
  376. TEST_ASSERT_EQUAL(config_check->dummy3.pin, extra.dummy3.pin);
  377. // not simulate an update
  378. is_recovery_running = false;
  379. PB<sys_dac_extra2_config> extra2(name.c_str(), &sys_dac_extra2_config_msg, sizeof(sys_dac_extra2_config_msg));
  380. extra2.LoadFile(true);
  381. auto config_extra2 = extra2.get();
  382. config_extra2->has_dummy4 = true;
  383. config_extra2->dummy4.pin = 99;
  384. extra2.CommitChanges();
  385. is_recovery_running = true;
  386. PB<sys_dac_config> wrapper2(name, &sys_dac_config_msg, sizeof(sys_dac_config_msg));
  387. wrapper2.LoadFile(true);
  388. wrapper2.Root().bck = 88;
  389. wrapper2.CommitChanges();
  390. is_recovery_running = false;
  391. extra2.LoadFile(true);
  392. TEST_ASSERT_EQUAL(config_extra2->bck, 88);
  393. TEST_ASSERT_TRUE(config_extra2->has_dummy4);
  394. TEST_ASSERT_EQUAL(config_extra2->dummy4.pin, 99);
  395. }
  396. TEST_CASE("String conversion from uint8_t array", "[WifiCredentialsManager]") {
  397. // Prepare test data
  398. const uint8_t testData[] = {'T', 'e', 's', 't', '\0', 'D', 'a', 't', 'a'};
  399. const size_t testDataLength = 9; // Including '\0'
  400. // Call the method
  401. std::string result = WifiList::toString(testData, testDataLength);
  402. // Assert expectations
  403. TEST_ASSERT_EQUAL_STRING_LEN("Test", result.c_str(), result.length());
  404. }
  405. TEST_CASE("Get SSID from wifi_sta_config_t", "[WifiCredentialsManager]") {
  406. // Prepare test data
  407. wifi_sta_config_t testConfig = getMockSTA(1);
  408. // Call the method
  409. std::string result = WifiList::GetSSID(&testConfig);
  410. TEST_ASSERT_EQUAL_STRING("SSID_1", result.c_str());
  411. }
  412. TEST_CASE("Get SSID from wifi_event_sta_connected_t", "[WifiCredentialsManager]") {
  413. wifi_event_sta_connected_t testEvent = {};
  414. const char* ssid = "EventSSID";
  415. memcpy(testEvent.ssid, ssid, strlen(ssid) + 1); // Including '\0'
  416. std::string result = WifiList::GetSSID(&testEvent);
  417. TEST_ASSERT_EQUAL_STRING(ssid, result.c_str());
  418. }
  419. TEST_CASE("Get SSID from wifi_ap_record_t", "[WifiCredentialsManager]") {
  420. wifi_ap_record_t testRecord = {};
  421. const char* ssid = "RecordSSID";
  422. memcpy(testRecord.ssid, ssid, strlen(ssid) + 1); // Including '\0'
  423. std::string result = WifiList::GetSSID(&testRecord);
  424. TEST_ASSERT_EQUAL_STRING(ssid, result.c_str());
  425. }
  426. TEST_CASE("Get Password from wifi_sta_config_t", "[WifiCredentialsManager]") {
  427. wifi_sta_config_t testConfig = {};
  428. memcpy(testConfig.password, password, strlen(password) + 1); // Including '\0'
  429. std::string result = WifiList::GetPassword(&testConfig);
  430. TEST_ASSERT_EQUAL_STRING(password, result.c_str());
  431. }
  432. TEST_CASE("Get BSSID from wifi_event_sta_connected_t", "[WifiCredentialsManager]") {
  433. wifi_event_sta_connected_t testEvent = {};
  434. memcpy(testEvent.bssid, bssid, sizeof(testEvent.bssid));
  435. std::string result = WifiCredentialsManager::GetBSSID(&testEvent);
  436. TEST_ASSERT_EQUAL_STRING(char_bssid, result.c_str());
  437. }
  438. TEST_CASE("Format BSSID from uint8_t array", "[WifiCredentialsManager]") {
  439. char buffer[18] = {0};
  440. WifiCredentialsManager::FormatBSSID(buffer, sizeof(buffer), bssid);
  441. TEST_ASSERT_EQUAL_STRING(char_bssid, buffer);
  442. }
  443. TEST_CASE("Update timestamp", "[WifiCredentialsManager]") {
  444. WifiCredentialsManager manager("test_manager");
  445. // Test with a non-null timestamp and an uninitialized flag
  446. google_protobuf_Timestamp ts = {0, 0};
  447. bool has_flag = false;
  448. bool result = manager.UpdateTimeStamp(&ts, has_flag);
  449. TEST_ASSERT_TRUE(result); // Check if the method returns true for change
  450. TEST_ASSERT_TRUE(has_flag); // Check if the flag is set to true
  451. TEST_ASSERT_NOT_EQUAL(0, ts.seconds); // Assuming gettimeofday() gives a non-zero time
  452. TEST_ASSERT_NOT_EQUAL(0, ts.nanos);
  453. // Store the updated values for comparison
  454. long prev_seconds = ts.seconds;
  455. long prev_nanos = ts.nanos;
  456. advanceTime(2);
  457. // Call the method again with the same timestamp
  458. result = manager.UpdateTimeStamp(&ts, has_flag);
  459. // Since the timestamp should be updated, check if the new values are different
  460. TEST_ASSERT_TRUE(result);
  461. TEST_ASSERT_TRUE(has_flag);
  462. TEST_ASSERT_NOT_EQUAL(prev_seconds, ts.seconds);
  463. TEST_ASSERT_NOT_EQUAL(prev_nanos, ts.nanos);
  464. // Test with a null timestamp
  465. result = manager.UpdateTimeStamp(nullptr, has_flag);
  466. // The method should return false, and the flag should remain unchanged
  467. TEST_ASSERT_FALSE(result);
  468. TEST_ASSERT_TRUE(has_flag); // Flag remains unchanged
  469. advanceTime(-2);
  470. }
  471. TEST_CASE("Update wifi entry", "[WifiCredentialsManager]") {
  472. WifiCredentialsManager manager("test_manager");
  473. sys_net_wifi_entry existingEntry = getMockEntry();
  474. sys_net_wifi_entry updatedEntry = getMockEntry();
  475. // Modify updatedEntry with different values
  476. strcpy(updatedEntry.password, "NewPassword");
  477. updatedEntry.channel = 5;
  478. updatedEntry.auth_type = sys_net_auth_types_WPA2_PSK;
  479. delete[] updatedEntry.radio_type;
  480. updatedEntry.radio_type_count = 1;
  481. updatedEntry.radio_type = new sys_net_radio_types[updatedEntry.radio_type_count]; // Dynamic allocation for radio_type
  482. updatedEntry.radio_type[0] = sys_net_radio_types_PHY_11N;
  483. updatedEntry.rssi = 42;
  484. updatedEntry.connected = true;
  485. // Call the Update method
  486. bool result = manager.Update(existingEntry, updatedEntry);
  487. // Assert that the Update method returns true
  488. TEST_ASSERT_TRUE(result);
  489. // Assert that the existingEntry is updated correctly
  490. TEST_ASSERT_EQUAL_STRING("NewPassword", existingEntry.password);
  491. TEST_ASSERT_EQUAL(updatedEntry.channel, existingEntry.channel);
  492. TEST_ASSERT_EQUAL(sys_net_auth_types_WPA2_PSK, existingEntry.auth_type);
  493. TEST_ASSERT_EQUAL(sys_net_radio_types_PHY_11N, existingEntry.radio_type[0]);
  494. TEST_ASSERT_EQUAL(updatedEntry.radio_type_count, existingEntry.radio_type_count);
  495. TEST_ASSERT_EQUAL(updatedEntry.rssi, existingEntry.rssi);
  496. TEST_ASSERT_TRUE(existingEntry.connected);
  497. // Clean up dynamically allocated memory
  498. delete[] updatedEntry.radio_type;
  499. delete[] existingEntry.radio_type;
  500. }
  501. TEST_CASE("Update wifi entry from AP record", "[WifiCredentialsManager]") {
  502. WifiCredentialsManager manager("test_manager");
  503. // Create a mock wifi_ap_record_t
  504. wifi_ap_record_t mockAP = {};
  505. // Initialize mockAP with test data
  506. // ...
  507. // Add the initial entry
  508. sys_net_wifi_entry initialEntry = manager.ToSTAEntry(&mockAP);
  509. initialEntry.connected = false; // Initially not connected
  510. manager.AddUpdate(initialEntry);
  511. // Now call the update with the same AP but with 'connected' status
  512. bool result = manager.Update(&mockAP, true);
  513. // Assert that the update was successful
  514. TEST_ASSERT_TRUE(result);
  515. // Retrieve the entry and check if it's updated
  516. auto updatedEntry = manager.Get(&mockAP);
  517. TEST_ASSERT_NOT_NULL(updatedEntry);
  518. TEST_ASSERT_TRUE(updatedEntry->connected); // Check if connected is now true
  519. manager.Clear();
  520. }
  521. TEST_CASE("Format radio types", "[WifiCredentialsManager]") {
  522. WifiCredentialsManager manager("test_manager");
  523. // Create an array of radio types for testing
  524. sys_net_radio_types radioTypes[] = {sys_net_radio_types_PHY_11B, sys_net_radio_types_PHY_11G, sys_net_radio_types_PHY_11N, sys_net_radio_types_LR,
  525. sys_net_radio_types_WPS, sys_net_radio_types_FTM_RESPONDER, sys_net_radio_types_FTM_INITIATOR, sys_net_radio_types_UNKNOWN};
  526. pb_size_t count = sizeof(radioTypes) / sizeof(radioTypes[0]);
  527. // Call the formatRadioTypes method
  528. std::string formattedString = manager.formatRadioTypes(radioTypes, count);
  529. // Define the expected result
  530. std::string expectedResult = "B,G,N,L,W,FR,FI,U";
  531. // Assert that the formatted string is as expected
  532. TEST_ASSERT_EQUAL_STRING(expectedResult.c_str(), formattedString.c_str());
  533. manager.Clear();
  534. }
  535. TEST_CASE("Update wifi entry from STA config", "[WifiCredentialsManager]") {
  536. WifiCredentialsManager manager("test_manager");
  537. // Create a mock wifi_sta_config_t
  538. wifi_sta_config_t mockSTA = getMockSTA();
  539. // Create and add an initial entry
  540. sys_net_wifi_entry initialEntry = manager.ToSTAEntry(&mockSTA);
  541. initialEntry.connected = false; // Initially not connected
  542. manager.AddUpdate(initialEntry);
  543. // Modify the mockSTA for the update
  544. mockSTA.channel = 6; // Change the channel
  545. // Now call the update with the modified mockSTA and 'connected' status
  546. bool result = manager.Update(&mockSTA, true);
  547. // Assert that the update was successful
  548. TEST_ASSERT_TRUE(result);
  549. // Retrieve the updated entry and check if it's correctly updated
  550. auto updatedEntry = manager.Get(&mockSTA);
  551. TEST_ASSERT_NOT_NULL(updatedEntry);
  552. TEST_ASSERT_EQUAL(mockSTA.channel, updatedEntry->channel);
  553. TEST_ASSERT_TRUE(updatedEntry->connected); // Check if connected is now true
  554. manager.Clear();
  555. }
  556. TEST_CASE("Convert AP record to STA entry", "[WifiCredentialsManager]") {
  557. int testindex = 1;
  558. WifiCredentialsManager manager("test_manager");
  559. // Create a mock wifi_ap_record_t
  560. wifi_ap_record_t mockAP = getMockAPRec(testindex);
  561. sys_net_wifi_entry entry; // Create an entry to be populated
  562. // Call ToSTAEntry
  563. sys_net_wifi_entry& resultEntry = manager.ToSTAEntry(&mockAP, entry);
  564. // Assert that the entry is populated correctly
  565. TEST_ASSERT_EQUAL_STRING("SSID_1", resultEntry.ssid);
  566. TEST_ASSERT_EQUAL(start_channel + testindex, resultEntry.channel);
  567. TEST_ASSERT_EQUAL(start_rssi + testindex, resultEntry.rssi);
  568. TEST_ASSERT_EQUAL(WifiList::GetAuthType(AUTH_MODE_INDEX(testindex)), resultEntry.auth_type);
  569. TEST_ASSERT_EQUAL(4, resultEntry.radio_type_count);
  570. std::string formattedString = manager.formatRadioTypes(resultEntry.radio_type, resultEntry.radio_type_count);
  571. // Define the expected result
  572. std::string expectedResult = "B,N,W,FI";
  573. TEST_ASSERT_EQUAL_STRING(expectedResult.c_str(), formattedString.c_str());
  574. char bssid_buffer[20] = {};
  575. WifiList::FormatBSSID(bssid_buffer, sizeof(bssid_buffer), mockAP.bssid);
  576. TEST_ASSERT_EQUAL_STRING((char*)bssid_buffer, resultEntry.bssid);
  577. manager.Clear();
  578. }
  579. TEST_CASE("Remove entries from manager instance", "[WifiCredentialsManager]") {
  580. WifiCredentialsManager manager("test_manager");
  581. FillSSIDs(manager, 4);
  582. TEST_ASSERT_EQUAL(4, manager.GetCount());
  583. TEST_ASSERT_TRUE(manager.RemoveCredential("SSID_1"));
  584. TEST_ASSERT_EQUAL(3, manager.GetCount());
  585. TEST_ASSERT_FALSE(manager.RemoveCredential("SSID_1"));
  586. TEST_ASSERT_EQUAL(3, manager.GetCount());
  587. manager.Clear();
  588. TEST_ASSERT_EQUAL(0, manager.GetCount());
  589. FillSSIDs(manager, 4);
  590. TEST_ASSERT_EQUAL(4, manager.GetCount());
  591. TEST_ASSERT_TRUE(manager.RemoveCredential(std::string("SSID_1")));
  592. TEST_ASSERT_FALSE(manager.RemoveCredential(std::string("SSID_1")));
  593. TEST_ASSERT_TRUE(manager.RemoveCredential("SSID_2"));
  594. TEST_ASSERT_FALSE(manager.RemoveCredential(std::string("SSID_2")));
  595. TEST_ASSERT_EQUAL(2, manager.GetCount());
  596. manager.Clear();
  597. TEST_ASSERT_EQUAL(0, manager.GetCount());
  598. }
  599. TEST_CASE("Reset all entries", "[WifiCredentialsManager]") {
  600. WifiCredentialsManager manager("test_manager");
  601. FillSSIDs(manager, 4);
  602. for (auto& e : manager) {
  603. TEST_ASSERT_TRUE(e.second.rssi > 0);
  604. TEST_ASSERT_TRUE(e.second.connected);
  605. }
  606. manager.ResetRSSI();
  607. for (auto& e : manager) {
  608. TEST_ASSERT_TRUE(e.second.rssi == 0);
  609. TEST_ASSERT_TRUE(e.second.connected);
  610. }
  611. manager.Clear();
  612. FillSSIDs(manager, 4);
  613. manager.ResetConnected();
  614. for (auto& e : manager) {
  615. TEST_ASSERT_TRUE(e.second.rssi > 0);
  616. TEST_ASSERT_FALSE(e.second.connected);
  617. }
  618. }
  619. TEST_CASE("Getting/setting Connected", "[WifiCredentialsManager]") {
  620. WifiCredentialsManager manager("test_manager");
  621. auto conn_mock_4 = getMockConnectedEvent(4);
  622. auto conn_mock_5 = getMockConnectedEvent(5);
  623. auto entry_5 = getMockEntry(5);
  624. char bssid_buffer[20] = {};
  625. FillSSIDs(manager, 4);
  626. for (auto& e : manager) {
  627. if (strcmp(e.second.ssid, "SSID_3") != 0) {
  628. e.second.connected = false;
  629. }
  630. }
  631. auto entry = manager.GetConnected();
  632. TEST_ASSERT_EQUAL_STRING("SSID_3", entry->ssid);
  633. TEST_ASSERT_FALSE(manager.SetConnected(&conn_mock_5, true));
  634. manager.AddUpdate(&entry_5);
  635. WifiCredentialsManager::Release(entry_5);
  636. TEST_ASSERT_TRUE(manager.SetConnected(&conn_mock_5, true));
  637. entry = manager.GetConnected();
  638. TEST_ASSERT_EQUAL_STRING_LEN((char*)conn_mock_5.ssid, entry->ssid, conn_mock_5.ssid_len);
  639. WifiCredentialsManager::FormatBSSID(bssid_buffer, sizeof(bssid_buffer), conn_mock_5.bssid);
  640. TEST_ASSERT_EQUAL_STRING((char*)bssid_buffer, entry->bssid);
  641. TEST_ASSERT_EQUAL(WifiList::GetAuthType(conn_mock_5.authmode), entry->auth_type);
  642. TEST_ASSERT_EQUAL(conn_mock_5.channel, entry->channel);
  643. TEST_ASSERT_TRUE(manager.SetConnected(&conn_mock_4, true));
  644. entry = manager.GetConnected();
  645. TEST_ASSERT_EQUAL_STRING_LEN((char*)conn_mock_4.ssid, entry->ssid, conn_mock_5.ssid_len);
  646. WifiList::FormatBSSID(bssid_buffer, sizeof(bssid_buffer), conn_mock_4.bssid);
  647. TEST_ASSERT_EQUAL_STRING((char*)bssid_buffer, entry->bssid);
  648. TEST_ASSERT_EQUAL(WifiList::GetAuthType(conn_mock_4.authmode), entry->auth_type);
  649. TEST_ASSERT_EQUAL(conn_mock_4.channel, entry->channel);
  650. manager.ResetConnected();
  651. TEST_ASSERT_NULL(manager.GetConnected());
  652. manager.Clear();
  653. }
  654. TEST_CASE("Getting by index/by name", "[WifiCredentialsManager]") {
  655. char buffer[25] = {};
  656. WifiCredentialsManager manager("test_manager");
  657. FillSSIDs(manager, 4);
  658. for (int i = 0; i < manager.GetCount(); i++) {
  659. sprintf(buffer, "SSID_%d", i + 1);
  660. auto mockap = getMockAPRec(i + 1);
  661. auto mockSTA = getMockSTA(i + 1);
  662. auto entry = manager.GetIndex(i);
  663. TEST_ASSERT_EQUAL_STRING(buffer, entry->ssid);
  664. auto pEntry = manager.Get(std::string(buffer));
  665. TEST_ASSERT_EQUAL_STRING(buffer, pEntry->ssid);
  666. pEntry = manager.Get(buffer);
  667. TEST_ASSERT_EQUAL_STRING(buffer, pEntry->ssid);
  668. pEntry = manager.Get(&mockap);
  669. TEST_ASSERT_EQUAL_STRING(buffer, pEntry->ssid);
  670. pEntry = manager.Get(&mockSTA);
  671. TEST_ASSERT_EQUAL_STRING(buffer, pEntry->ssid);
  672. }
  673. auto entry = manager.GetStrongestSTA();
  674. TEST_ASSERT_EQUAL_STRING("SSID_4", entry.ssid);
  675. manager.Clear();
  676. }
  677. TEST_CASE("Update last try", "[WifiCredentialsManager]") {
  678. char buffer[25] = {};
  679. google_protobuf_Timestamp ts;
  680. bool flag;
  681. WifiCredentialsManager::UpdateTimeStamp(&ts, flag);
  682. // now advance the time to ensure tests are a success
  683. advanceTime(2);
  684. WifiList manager("test_manager");
  685. FillSSIDs(manager, 4);
  686. for (int i = 0; i < manager.GetCount(); i++) {
  687. sprintf(buffer, "SSID_%d", i + 1);
  688. auto mockap = getMockAPRec(i + 1);
  689. auto mockSTA = getMockSTA(i + 1);
  690. auto entry = manager.GetIndex(i);
  691. entry->has_last_try = false;
  692. memset(&entry->last_try, 0x00, sizeof(entry->last_try));
  693. manager.UpdateLastTry(entry);
  694. TEST_ASSERT_TRUE(entry->has_last_try);
  695. TEST_ASSERT_TRUE(entry->last_try.seconds >= ts.seconds);
  696. entry->has_last_try = false;
  697. memset(&entry->last_try, 0x00, sizeof(entry->last_try));
  698. manager.UpdateLastTry(std::string(buffer));
  699. TEST_ASSERT_TRUE(entry->has_last_try);
  700. TEST_ASSERT_TRUE(entry->last_try.seconds >= ts.seconds);
  701. entry->has_last_try = false;
  702. memset(&entry->last_try, 0x00, sizeof(entry->last_try));
  703. manager.UpdateLastTry(&mockap);
  704. TEST_ASSERT_TRUE(entry->has_last_try);
  705. TEST_ASSERT_TRUE(entry->last_try.seconds >= ts.seconds);
  706. entry->has_last_try = false;
  707. memset(&entry->last_try, 0x00, sizeof(entry->last_try));
  708. manager.UpdateLastTry(&mockSTA);
  709. TEST_ASSERT_TRUE(entry->has_last_try);
  710. TEST_ASSERT_TRUE(entry->last_try.seconds >= ts.seconds);
  711. }
  712. auto entry = manager.GetStrongestSTA();
  713. TEST_ASSERT_EQUAL_STRING("SSID_4", entry.ssid);
  714. manager.Clear();
  715. advanceTime(-2);
  716. }
  717. TEST_CASE("Update last seen", "[WifiCredentialsManager]") {
  718. char buffer[25] = {};
  719. google_protobuf_Timestamp ts;
  720. bool flag;
  721. WifiCredentialsManager::UpdateTimeStamp(&ts, flag);
  722. // now advance the time to ensure tests are a success
  723. advanceTime(2);
  724. WifiList manager("test_manager");
  725. FillSSIDs(manager, 4);
  726. for (int i = 0; i < manager.GetCount(); i++) {
  727. sprintf(buffer, "SSID_%d", i + 1);
  728. auto mockap = getMockAPRec(i + 1);
  729. auto mockSTA = getMockSTA(i + 1);
  730. auto entry = manager.GetIndex(i);
  731. entry->has_last_seen = false;
  732. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  733. manager.UpdateLastSeen(entry);
  734. TEST_ASSERT_TRUE(entry->has_last_seen);
  735. TEST_ASSERT_TRUE(entry->last_seen.seconds >= ts.seconds);
  736. entry->has_last_seen = false;
  737. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  738. manager.UpdateLastSeen(std::string(buffer));
  739. TEST_ASSERT_TRUE(entry->has_last_seen);
  740. TEST_ASSERT_TRUE(entry->last_seen.seconds >= ts.seconds);
  741. entry->has_last_seen = false;
  742. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  743. manager.UpdateLastSeen(&mockap);
  744. TEST_ASSERT_TRUE(entry->has_last_seen);
  745. TEST_ASSERT_TRUE(entry->last_seen.seconds >= ts.seconds);
  746. entry->has_last_seen = false;
  747. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  748. manager.UpdateLastSeen(&mockSTA);
  749. TEST_ASSERT_TRUE(entry->has_last_seen);
  750. TEST_ASSERT_TRUE(entry->last_seen.seconds >= ts.seconds);
  751. }
  752. manager.Clear();
  753. advanceTime(-2);
  754. }
  755. TEST_CASE("Memory leak test", "[WifiCredentialsManager]") {
  756. char buffer[25] = {};
  757. char err_buffer[55] = {};
  758. int fillqty=20;
  759. google_protobuf_Timestamp ts;
  760. bool flag;
  761. WifiCredentialsManager::UpdateTimeStamp(&ts, flag);
  762. // now advance the time to ensure tests are a success
  763. advanceTime(2);
  764. HasMemoryUsageIncreased(0);
  765. for (int runs = 0; runs < 100; runs++) {
  766. WifiCredentialsManager manager("test_manager");
  767. FillSSIDs(manager, fillqty);
  768. for (int i = 1; i <= manager.GetCount(); i++) {
  769. sprintf(buffer, "SSID_%d", i);
  770. sprintf(err_buffer,"Round %d, SSID_%d",runs,i);
  771. auto mockap = getMockAPRec(i);
  772. auto mockSTA = getMockSTA(i);
  773. auto entry = manager.Get(buffer);
  774. TEST_ASSERT_EQUAL_STRING(buffer,entry->ssid);
  775. entry->has_last_seen = false;
  776. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  777. manager.UpdateLastSeen(entry);
  778. TEST_ASSERT_TRUE(entry->has_last_seen);
  779. TEST_ASSERT_TRUE(entry->last_seen.seconds >= ts.seconds);
  780. entry->has_last_seen = false;
  781. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  782. manager.UpdateLastSeen(std::string(buffer));
  783. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  784. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  785. entry->has_last_seen = false;
  786. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  787. manager.UpdateLastSeen(&mockap);
  788. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  789. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  790. entry->has_last_seen = false;
  791. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  792. manager.UpdateLastSeen(&mockSTA);
  793. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  794. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  795. }
  796. manager.Clear();
  797. }
  798. TEST_ASSERT_FALSE(HasMemoryUsageIncreased(1));
  799. for (int runs = 0; runs < 100; runs++) {
  800. WifiCredentialsManager manager("test_manager");
  801. FillSSIDFromAPRec(manager, fillqty);
  802. for (int i = 1; i <= manager.GetCount(); i++) {
  803. sprintf(buffer, "SSID_%d", i);
  804. sprintf(err_buffer,"Round %d, SSID_%d",runs,i);
  805. auto mockap = getMockAPRec(i);
  806. auto mockSTA = getMockSTA(i);
  807. auto entry = manager.GetIndex(i-1);
  808. entry->has_last_seen = false;
  809. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  810. manager.UpdateLastSeen(entry);
  811. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  812. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  813. entry->has_last_seen = false;
  814. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  815. manager.UpdateLastSeen(std::string(buffer));
  816. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  817. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  818. entry->has_last_seen = false;
  819. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  820. manager.UpdateLastSeen(&mockap);
  821. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  822. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  823. entry->has_last_seen = false;
  824. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  825. manager.UpdateLastSeen(&mockSTA);
  826. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  827. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  828. }
  829. manager.Clear();
  830. }
  831. TEST_ASSERT_FALSE(HasMemoryUsageIncreased(2));
  832. for (int runs = 0; runs < 100; runs++) {
  833. WifiCredentialsManager manager("test_manager");
  834. FillSSIDFromMockEntry(manager, fillqty);
  835. for (int i = 1; i <= manager.GetCount(); i++) {
  836. sprintf(buffer, "SSID_%d", i);
  837. sprintf(err_buffer,"Round %d, SSID_%d",runs,i);
  838. auto mockap = getMockAPRec(i);
  839. auto mockSTA = getMockSTA(i);
  840. auto entry = manager.GetIndex(i-1);
  841. entry->has_last_seen = false;
  842. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  843. manager.UpdateLastSeen(entry);
  844. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  845. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  846. entry->has_last_seen = false;
  847. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  848. manager.UpdateLastSeen(std::string(buffer));
  849. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  850. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  851. entry->has_last_seen = false;
  852. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  853. manager.UpdateLastSeen(&mockap);
  854. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  855. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  856. entry->has_last_seen = false;
  857. memset(&entry->last_seen, 0x00, sizeof(entry->last_seen));
  858. manager.UpdateLastSeen(&mockSTA);
  859. TEST_ASSERT_TRUE_MESSAGE(entry->has_last_seen,err_buffer);
  860. TEST_ASSERT_TRUE_MESSAGE(entry->last_seen.seconds >= ts.seconds,err_buffer);
  861. }
  862. manager.Clear();
  863. }
  864. advanceTime(-2);
  865. TEST_ASSERT_FALSE(HasMemoryUsageIncreased(3));
  866. }