test_nvs.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "catch.hpp"
  14. #include "nvs.hpp"
  15. #include "nvs_test_api.h"
  16. #ifdef CONFIG_NVS_ENCRYPTION
  17. #include "nvs_encr.hpp"
  18. #endif
  19. #include "spi_flash_emulation.h"
  20. #include <sstream>
  21. #include <iostream>
  22. #include <fstream>
  23. #include <dirent.h>
  24. #include <unistd.h>
  25. #include <sys/wait.h>
  26. #include <string.h>
  27. #define TEST_ESP_ERR(rc, res) CHECK((rc) == (res))
  28. #define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK)
  29. using namespace std;
  30. using namespace nvs;
  31. stringstream s_perf;
  32. void dumpBytes(const uint8_t* data, size_t count)
  33. {
  34. for (uint32_t i = 0; i < count; ++i) {
  35. if (i % 32 == 0) {
  36. printf("%08x ", i);
  37. }
  38. printf("%02x ", data[i]);
  39. if ((i + 1) % 32 == 0) {
  40. printf("\n");
  41. }
  42. }
  43. }
  44. TEST_CASE("crc32 behaves as expected", "[nvs]")
  45. {
  46. Item item1;
  47. item1.datatype = ItemType::I32;
  48. item1.nsIndex = 1;
  49. item1.crc32 = 0;
  50. item1.chunkIndex = 0xff;
  51. fill_n(item1.key, sizeof(item1.key), 0xbb);
  52. fill_n(item1.data, sizeof(item1.data), 0xaa);
  53. auto crc32_1 = item1.calculateCrc32();
  54. Item item2 = item1;
  55. item2.crc32 = crc32_1;
  56. CHECK(crc32_1 == item2.calculateCrc32());
  57. item2 = item1;
  58. item2.nsIndex = 2;
  59. CHECK(crc32_1 != item2.calculateCrc32());
  60. item2 = item1;
  61. item2.datatype = ItemType::U32;
  62. CHECK(crc32_1 != item2.calculateCrc32());
  63. item2 = item1;
  64. strncpy(item2.key, "foo", Item::MAX_KEY_LENGTH);
  65. CHECK(crc32_1 != item2.calculateCrc32());
  66. }
  67. TEST_CASE("starting with empty flash, page is in uninitialized state", "[nvs]")
  68. {
  69. SpiFlashEmulator emu(1);
  70. Page page;
  71. CHECK(page.state() == Page::PageState::INVALID);
  72. CHECK(page.load(0) == ESP_OK);
  73. CHECK(page.state() == Page::PageState::UNINITIALIZED);
  74. }
  75. TEST_CASE("can distinguish namespaces", "[nvs]")
  76. {
  77. SpiFlashEmulator emu(1);
  78. Page page;
  79. CHECK(page.load(0) == ESP_OK);
  80. int32_t val1 = 0x12345678;
  81. CHECK(page.writeItem(1, ItemType::I32, "intval1", &val1, sizeof(val1)) == ESP_OK);
  82. int32_t val2 = 0x23456789;
  83. CHECK(page.writeItem(2, ItemType::I32, "intval1", &val2, sizeof(val2)) == ESP_OK);
  84. int32_t readVal;
  85. CHECK(page.readItem(2, ItemType::I32, "intval1", &readVal, sizeof(readVal)) == ESP_OK);
  86. CHECK(readVal == val2);
  87. }
  88. TEST_CASE("reading with different type causes type mismatch error", "[nvs]")
  89. {
  90. SpiFlashEmulator emu(1);
  91. Page page;
  92. CHECK(page.load(0) == ESP_OK);
  93. int32_t val = 0x12345678;
  94. CHECK(page.writeItem(1, ItemType::I32, "intval1", &val, sizeof(val)) == ESP_OK);
  95. CHECK(page.readItem(1, ItemType::U32, "intval1", &val, sizeof(val)) == ESP_ERR_NVS_TYPE_MISMATCH);
  96. }
  97. TEST_CASE("when page is erased, it's state becomes UNITIALIZED", "[nvs]")
  98. {
  99. SpiFlashEmulator emu(1);
  100. Page page;
  101. CHECK(page.load(0) == ESP_OK);
  102. int32_t val = 0x12345678;
  103. CHECK(page.writeItem(1, ItemType::I32, "intval1", &val, sizeof(val)) == ESP_OK);
  104. CHECK(page.erase() == ESP_OK);
  105. CHECK(page.state() == Page::PageState::UNINITIALIZED);
  106. }
  107. TEST_CASE("when writing and erasing, used/erased counts are updated correctly", "[nvs]")
  108. {
  109. SpiFlashEmulator emu(1);
  110. Page page;
  111. CHECK(page.load(0) == ESP_OK);
  112. CHECK(page.getUsedEntryCount() == 0);
  113. CHECK(page.getErasedEntryCount() == 0);
  114. uint32_t foo1 = 0;
  115. CHECK(page.writeItem(1, "foo1", foo1) == ESP_OK);
  116. CHECK(page.getUsedEntryCount() == 1);
  117. CHECK(page.writeItem(2, "foo1", foo1) == ESP_OK);
  118. CHECK(page.getUsedEntryCount() == 2);
  119. CHECK(page.eraseItem<uint32_t>(2, "foo1") == ESP_OK);
  120. CHECK(page.getUsedEntryCount() == 1);
  121. CHECK(page.getErasedEntryCount() == 1);
  122. for (size_t i = 0; i < Page::ENTRY_COUNT - 2; ++i) {
  123. char name[16];
  124. snprintf(name, sizeof(name), "i%ld", (long int)i);
  125. CHECK(page.writeItem(1, name, i) == ESP_OK);
  126. }
  127. CHECK(page.getUsedEntryCount() == Page::ENTRY_COUNT - 1);
  128. CHECK(page.getErasedEntryCount() == 1);
  129. for (size_t i = 0; i < Page::ENTRY_COUNT - 2; ++i) {
  130. char name[16];
  131. snprintf(name, sizeof(name), "i%ld", (long int)i);
  132. CHECK(page.eraseItem(1, itemTypeOf<size_t>(), name) == ESP_OK);
  133. }
  134. CHECK(page.getUsedEntryCount() == 1);
  135. CHECK(page.getErasedEntryCount() == Page::ENTRY_COUNT - 1);
  136. }
  137. TEST_CASE("when page is full, adding an element fails", "[nvs]")
  138. {
  139. SpiFlashEmulator emu(1);
  140. Page page;
  141. CHECK(page.load(0) == ESP_OK);
  142. for (size_t i = 0; i < Page::ENTRY_COUNT; ++i) {
  143. char name[16];
  144. snprintf(name, sizeof(name), "i%ld", (long int)i);
  145. CHECK(page.writeItem(1, name, i) == ESP_OK);
  146. }
  147. CHECK(page.writeItem(1, "foo", 64UL) == ESP_ERR_NVS_PAGE_FULL);
  148. }
  149. TEST_CASE("page maintains its seq number")
  150. {
  151. SpiFlashEmulator emu(1);
  152. {
  153. Page page;
  154. CHECK(page.load(0) == ESP_OK);
  155. CHECK(page.setSeqNumber(123) == ESP_OK);
  156. int32_t val = 42;
  157. CHECK(page.writeItem(1, ItemType::I32, "dummy", &val, sizeof(val)) == ESP_OK);
  158. }
  159. {
  160. Page page;
  161. CHECK(page.load(0) == ESP_OK);
  162. uint32_t seqno;
  163. CHECK(page.getSeqNumber(seqno) == ESP_OK);
  164. CHECK(seqno == 123);
  165. }
  166. }
  167. TEST_CASE("can write and read variable length data", "[nvs]")
  168. {
  169. SpiFlashEmulator emu(1);
  170. Page page;
  171. CHECK(page.load(0) == ESP_OK);
  172. const char str[] = "foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234";
  173. size_t len = strlen(str);
  174. CHECK(page.writeItem(1, "stuff1", 42) == ESP_OK);
  175. CHECK(page.writeItem(1, "stuff2", 1) == ESP_OK);
  176. CHECK(page.writeItem(1, ItemType::SZ, "foobaar", str, len + 1) == ESP_OK);
  177. CHECK(page.writeItem(1, "stuff3", 2) == ESP_OK);
  178. CHECK(page.writeItem(1, ItemType::BLOB, "baz", str, len) == ESP_OK);
  179. CHECK(page.writeItem(1, "stuff4", 0x7abbccdd) == ESP_OK);
  180. char buf[sizeof(str) + 16];
  181. int32_t value;
  182. CHECK(page.readItem(1, "stuff1", value) == ESP_OK);
  183. CHECK(value == 42);
  184. CHECK(page.readItem(1, "stuff2", value) == ESP_OK);
  185. CHECK(value == 1);
  186. CHECK(page.readItem(1, "stuff3", value) == ESP_OK);
  187. CHECK(value == 2);
  188. CHECK(page.readItem(1, "stuff4", value) == ESP_OK);
  189. CHECK(value == 0x7abbccdd);
  190. fill_n(buf, sizeof(buf), 0xff);
  191. CHECK(page.readItem(1, ItemType::SZ, "foobaar", buf, sizeof(buf)) == ESP_OK);
  192. CHECK(memcmp(buf, str, strlen(str) + 1) == 0);
  193. fill_n(buf, sizeof(buf), 0xff);
  194. CHECK(page.readItem(1, ItemType::BLOB, "baz", buf, sizeof(buf)) == ESP_OK);
  195. CHECK(memcmp(buf, str, strlen(str)) == 0);
  196. }
  197. TEST_CASE("different key names are distinguished even if the pointer is the same", "[nvs]")
  198. {
  199. SpiFlashEmulator emu(1);
  200. Page page;
  201. TEST_ESP_OK(page.load(0));
  202. TEST_ESP_OK(page.writeItem(1, "i1", 1));
  203. TEST_ESP_OK(page.writeItem(1, "i2", 2));
  204. int32_t value;
  205. char keyname[10] = {0};
  206. for (int i = 0; i < 2; ++i) {
  207. strncpy(keyname, "i1", sizeof(keyname) - 1);
  208. TEST_ESP_OK(page.readItem(1, keyname, value));
  209. CHECK(value == 1);
  210. strncpy(keyname, "i2", sizeof(keyname) - 1);
  211. TEST_ESP_OK(page.readItem(1, keyname, value));
  212. CHECK(value == 2);
  213. }
  214. }
  215. TEST_CASE("Page validates key size", "[nvs]")
  216. {
  217. SpiFlashEmulator emu(4);
  218. Page page;
  219. TEST_ESP_OK(page.load(0));
  220. // 16-character key fails
  221. TEST_ESP_ERR(page.writeItem(1, "0123456789123456", 1), ESP_ERR_NVS_KEY_TOO_LONG);
  222. // 15-character key is okay
  223. TEST_ESP_OK(page.writeItem(1, "012345678912345", 1));
  224. }
  225. TEST_CASE("Page validates blob size", "[nvs]")
  226. {
  227. SpiFlashEmulator emu(4);
  228. Page page;
  229. TEST_ESP_OK(page.load(0));
  230. char buf[2048] = { 0 };
  231. // There are two potential errors here:
  232. // - not enough space in the page (because one value has been written already)
  233. // - value is too long
  234. // Check that the second one is actually returned.
  235. TEST_ESP_ERR(page.writeItem(1, ItemType::BLOB, "2", buf, Page::ENTRY_COUNT * Page::ENTRY_SIZE), ESP_ERR_NVS_VALUE_TOO_LONG);
  236. // Should fail as well
  237. TEST_ESP_ERR(page.writeItem(1, ItemType::BLOB, "2", buf, Page::CHUNK_MAX_SIZE + 1), ESP_ERR_NVS_VALUE_TOO_LONG);
  238. TEST_ESP_OK(page.writeItem(1, ItemType::BLOB, "2", buf, Page::CHUNK_MAX_SIZE));
  239. }
  240. TEST_CASE("Page handles invalid CRC of variable length items", "[nvs][cur]")
  241. {
  242. SpiFlashEmulator emu(4);
  243. {
  244. Page page;
  245. TEST_ESP_OK(page.load(0));
  246. char buf[128] = {0};
  247. TEST_ESP_OK(page.writeItem(1, ItemType::BLOB, "1", buf, sizeof(buf)));
  248. }
  249. // corrupt header of the item (64 is the offset of the first item in page)
  250. uint32_t overwrite_buf = 0;
  251. emu.write(64, &overwrite_buf, 4);
  252. // load page again
  253. {
  254. Page page;
  255. TEST_ESP_OK(page.load(0));
  256. }
  257. }
  258. class HashListTestHelper : public HashList
  259. {
  260. public:
  261. size_t getBlockCount()
  262. {
  263. return mBlockList.size();
  264. }
  265. };
  266. TEST_CASE("HashList is cleaned up as soon as items are erased", "[nvs]")
  267. {
  268. HashListTestHelper hashlist;
  269. // Add items
  270. const size_t count = 128;
  271. for (size_t i = 0; i < count; ++i) {
  272. char key[16];
  273. snprintf(key, sizeof(key), "i%ld", (long int)i);
  274. Item item(1, ItemType::U32, 1, key);
  275. hashlist.insert(item, i);
  276. }
  277. INFO("Added " << count << " items, " << hashlist.getBlockCount() << " blocks");
  278. // Remove them in reverse order
  279. for (size_t i = count; i > 0; --i) {
  280. hashlist.erase(i - 1, true);
  281. }
  282. CHECK(hashlist.getBlockCount() == 0);
  283. // Add again
  284. for (size_t i = 0; i < count; ++i) {
  285. char key[16];
  286. snprintf(key, sizeof(key), "i%ld", (long int)i);
  287. Item item(1, ItemType::U32, 1, key);
  288. hashlist.insert(item, i);
  289. }
  290. INFO("Added " << count << " items, " << hashlist.getBlockCount() << " blocks");
  291. // Remove them in the same order
  292. for (size_t i = 0; i < count; ++i) {
  293. hashlist.erase(i, true);
  294. }
  295. CHECK(hashlist.getBlockCount() == 0);
  296. }
  297. TEST_CASE("can init PageManager in empty flash", "[nvs]")
  298. {
  299. SpiFlashEmulator emu(4);
  300. PageManager pm;
  301. CHECK(pm.load(0, 4) == ESP_OK);
  302. }
  303. TEST_CASE("PageManager adds page in the correct order", "[nvs]")
  304. {
  305. const size_t pageCount = 8;
  306. SpiFlashEmulator emu(pageCount);
  307. uint32_t pageNo[pageCount] = { -1U, 50, 11, -1U, 23, 22, 24, 49};
  308. for (uint32_t i = 0; i < pageCount; ++i) {
  309. Page p;
  310. p.load(i);
  311. if (pageNo[i] != -1U) {
  312. p.setSeqNumber(pageNo[i]);
  313. p.writeItem(1, "foo", 10U);
  314. }
  315. }
  316. PageManager pageManager;
  317. CHECK(pageManager.load(0, pageCount) == ESP_OK);
  318. uint32_t lastSeqNo = 0;
  319. for (auto it = std::begin(pageManager); it != std::end(pageManager); ++it) {
  320. uint32_t seqNo;
  321. CHECK(it->getSeqNumber(seqNo) == ESP_OK);
  322. CHECK(seqNo > lastSeqNo);
  323. }
  324. }
  325. TEST_CASE("can init storage in empty flash", "[nvs]")
  326. {
  327. SpiFlashEmulator emu(8);
  328. Storage storage;
  329. emu.setBounds(4, 8);
  330. CHECK(storage.init(4, 4) == ESP_OK);
  331. s_perf << "Time to init empty storage (4 sectors): " << emu.getTotalTime() << " us" << std::endl;
  332. }
  333. TEST_CASE("storage doesn't add duplicates within one page", "[nvs]")
  334. {
  335. SpiFlashEmulator emu(8);
  336. Storage storage;
  337. emu.setBounds(4, 8);
  338. CHECK(storage.init(4, 4) == ESP_OK);
  339. int bar = 0;
  340. CHECK(storage.writeItem(1, "bar", bar) == ESP_OK);
  341. CHECK(storage.writeItem(1, "bar", bar) == ESP_OK);
  342. Page page;
  343. page.load(4);
  344. CHECK(page.getUsedEntryCount() == 1);
  345. CHECK(page.getErasedEntryCount() == 1);
  346. }
  347. TEST_CASE("can write one item a thousand times", "[nvs]")
  348. {
  349. SpiFlashEmulator emu(8);
  350. Storage storage;
  351. emu.setBounds(4, 8);
  352. CHECK(storage.init(4, 4) == ESP_OK);
  353. for (size_t i = 0; i < Page::ENTRY_COUNT * 4 * 2; ++i) {
  354. REQUIRE(storage.writeItem(1, "i", static_cast<int>(i)) == ESP_OK);
  355. }
  356. s_perf << "Time to write one item a thousand times: " << emu.getTotalTime() << " us (" << emu.getEraseOps() << " " << emu.getWriteOps() << " " << emu.getReadOps() << " " << emu.getWriteBytes() << " " << emu.getReadBytes() << ")" << std::endl;
  357. }
  358. TEST_CASE("storage doesn't add duplicates within multiple pages", "[nvs]")
  359. {
  360. SpiFlashEmulator emu(8);
  361. Storage storage;
  362. emu.setBounds(4, 8);
  363. CHECK(storage.init(4, 4) == ESP_OK);
  364. int bar = 0;
  365. CHECK(storage.writeItem(1, "bar", bar) == ESP_OK);
  366. for (size_t i = 0; i < Page::ENTRY_COUNT; ++i) {
  367. CHECK(storage.writeItem(1, "foo", static_cast<int>(bar)) == ESP_OK);
  368. }
  369. CHECK(storage.writeItem(1, "bar", bar) == ESP_OK);
  370. Page page;
  371. page.load(4);
  372. CHECK(page.findItem(1, itemTypeOf<int>(), "bar") == ESP_ERR_NVS_NOT_FOUND);
  373. page.load(5);
  374. CHECK(page.findItem(1, itemTypeOf<int>(), "bar") == ESP_OK);
  375. }
  376. TEST_CASE("storage can find items on second page if first is not fully written and has cached search data", "[nvs]")
  377. {
  378. SpiFlashEmulator emu(3);
  379. Storage storage;
  380. CHECK(storage.init(0, 3) == ESP_OK);
  381. int bar = 0;
  382. uint8_t bigdata[(Page::CHUNK_MAX_SIZE - Page::ENTRY_SIZE)/2] = {0};
  383. // write one big chunk of data
  384. ESP_ERROR_CHECK(storage.writeItem(0, ItemType::BLOB, "1", bigdata, sizeof(bigdata)));
  385. // write another big chunk of data
  386. ESP_ERROR_CHECK(storage.writeItem(0, ItemType::BLOB, "2", bigdata, sizeof(bigdata)));
  387. // write third one; it will not fit into the first page
  388. ESP_ERROR_CHECK(storage.writeItem(0, ItemType::BLOB, "3", bigdata, sizeof(bigdata)));
  389. size_t size;
  390. ESP_ERROR_CHECK(storage.getItemDataSize(0, ItemType::BLOB, "1", size));
  391. CHECK(size == sizeof(bigdata));
  392. ESP_ERROR_CHECK(storage.getItemDataSize(0, ItemType::BLOB, "3", size));
  393. CHECK(size == sizeof(bigdata));
  394. }
  395. TEST_CASE("can write and read variable length data lots of times", "[nvs]")
  396. {
  397. SpiFlashEmulator emu(8);
  398. Storage storage;
  399. emu.setBounds(4, 8);
  400. CHECK(storage.init(4, 4) == ESP_OK);
  401. const char str[] = "foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234";
  402. char buf[sizeof(str) + 16];
  403. size_t len = strlen(str);
  404. for (size_t i = 0; i < Page::ENTRY_COUNT * 4 * 2; ++i) {
  405. CAPTURE(i);
  406. CHECK(storage.writeItem(1, ItemType::SZ, "foobaar", str, len + 1) == ESP_OK);
  407. CHECK(storage.writeItem(1, "foo", static_cast<uint32_t>(i)) == ESP_OK);
  408. uint32_t value;
  409. CHECK(storage.readItem(1, "foo", value) == ESP_OK);
  410. CHECK(value == i);
  411. fill_n(buf, sizeof(buf), 0xff);
  412. CHECK(storage.readItem(1, ItemType::SZ, "foobaar", buf, sizeof(buf)) == ESP_OK);
  413. CHECK(memcmp(buf, str, strlen(str) + 1) == 0);
  414. }
  415. s_perf << "Time to write one string and one integer a thousand times: " << emu.getTotalTime() << " us (" << emu.getEraseOps() << " " << emu.getWriteOps() << " " << emu.getReadOps() << " " << emu.getWriteBytes() << " " << emu.getReadBytes() << ")" << std::endl;
  416. }
  417. TEST_CASE("can get length of variable length data", "[nvs]")
  418. {
  419. SpiFlashEmulator emu(8);
  420. emu.randomize(200);
  421. Storage storage;
  422. emu.setBounds(4, 8);
  423. CHECK(storage.init(4, 4) == ESP_OK);
  424. const char str[] = "foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234foobar1234";
  425. size_t len = strlen(str);
  426. CHECK(storage.writeItem(1, ItemType::SZ, "foobaar", str, len + 1) == ESP_OK);
  427. size_t dataSize;
  428. CHECK(storage.getItemDataSize(1, ItemType::SZ, "foobaar", dataSize) == ESP_OK);
  429. CHECK(dataSize == len + 1);
  430. CHECK(storage.writeItem(2, ItemType::BLOB, "foobaar", str, len) == ESP_OK);
  431. CHECK(storage.getItemDataSize(2, ItemType::BLOB, "foobaar", dataSize) == ESP_OK);
  432. CHECK(dataSize == len);
  433. }
  434. TEST_CASE("can create namespaces", "[nvs]")
  435. {
  436. SpiFlashEmulator emu(8);
  437. Storage storage;
  438. emu.setBounds(4, 8);
  439. CHECK(storage.init(4, 4) == ESP_OK);
  440. uint8_t nsi;
  441. CHECK(storage.createOrOpenNamespace("wifi", false, nsi) == ESP_ERR_NVS_NOT_FOUND);
  442. CHECK(storage.createOrOpenNamespace("wifi", true, nsi) == ESP_OK);
  443. Page page;
  444. page.load(4);
  445. CHECK(page.findItem(Page::NS_INDEX, ItemType::U8, "wifi") == ESP_OK);
  446. }
  447. TEST_CASE("storage may become full", "[nvs]")
  448. {
  449. SpiFlashEmulator emu(8);
  450. Storage storage;
  451. emu.setBounds(4, 8);
  452. CHECK(storage.init(4, 4) == ESP_OK);
  453. for (size_t i = 0; i < Page::ENTRY_COUNT * 3; ++i) {
  454. char name[Item::MAX_KEY_LENGTH + 1];
  455. snprintf(name, sizeof(name), "key%05d", static_cast<int>(i));
  456. REQUIRE(storage.writeItem(1, name, static_cast<int>(i)) == ESP_OK);
  457. }
  458. REQUIRE(storage.writeItem(1, "foo", 10) == ESP_ERR_NVS_NOT_ENOUGH_SPACE);
  459. }
  460. TEST_CASE("can modify an item on a page which will be erased", "[nvs]")
  461. {
  462. SpiFlashEmulator emu(2);
  463. Storage storage;
  464. CHECK(storage.init(0, 2) == ESP_OK);
  465. for (size_t i = 0; i < Page::ENTRY_COUNT * 3 + 1; ++i) {
  466. REQUIRE(storage.writeItem(1, "foo", 42U) == ESP_OK);
  467. }
  468. }
  469. TEST_CASE("can erase items", "[nvs]")
  470. {
  471. SpiFlashEmulator emu(3);
  472. Storage storage;
  473. CHECK(storage.init(0, 3) == ESP_OK);
  474. for (size_t i = 0; i < Page::ENTRY_COUNT * 2 - 3; ++i) {
  475. char name[Item::MAX_KEY_LENGTH + 1];
  476. snprintf(name, sizeof(name), "key%05d", static_cast<int>(i));
  477. REQUIRE(storage.writeItem(3, name, static_cast<int>(i)) == ESP_OK);
  478. }
  479. CHECK(storage.writeItem(1, "foo", 32) == ESP_OK);
  480. CHECK(storage.writeItem(2, "foo", 64) == ESP_OK);
  481. CHECK(storage.eraseItem(2, "foo") == ESP_OK);
  482. int val;
  483. CHECK(storage.readItem(1, "foo", val) == ESP_OK);
  484. CHECK(val == 32);
  485. CHECK(storage.eraseNamespace(3) == ESP_OK);
  486. CHECK(storage.readItem(2, "foo", val) == ESP_ERR_NVS_NOT_FOUND);
  487. CHECK(storage.readItem(3, "key00222", val) == ESP_ERR_NVS_NOT_FOUND);
  488. }
  489. TEST_CASE("nvs api tests", "[nvs]")
  490. {
  491. SpiFlashEmulator emu(10);
  492. emu.randomize(100);
  493. nvs_handle handle_1;
  494. const uint32_t NVS_FLASH_SECTOR = 6;
  495. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  496. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  497. TEST_ESP_ERR(nvs_open("namespace1", NVS_READWRITE, &handle_1), ESP_ERR_NVS_NOT_INITIALIZED);
  498. for (uint16_t i = NVS_FLASH_SECTOR; i <NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN; ++i) {
  499. spi_flash_erase_sector(i);
  500. }
  501. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  502. TEST_ESP_ERR(nvs_open("namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND);
  503. // TEST_ESP_ERR(nvs_set_i32(handle_1, "foo", 0x12345678), ESP_ERR_NVS_READ_ONLY);
  504. // nvs_close(handle_1);
  505. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle_1));
  506. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x12345678));
  507. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x23456789));
  508. nvs_handle handle_2;
  509. TEST_ESP_OK(nvs_open("namespace2", NVS_READWRITE, &handle_2));
  510. TEST_ESP_OK(nvs_set_i32(handle_2, "foo", 0x3456789a));
  511. const char* str = "value 0123456789abcdef0123456789abcdef";
  512. TEST_ESP_OK(nvs_set_str(handle_2, "key", str));
  513. int32_t v1;
  514. TEST_ESP_OK(nvs_get_i32(handle_1, "foo", &v1));
  515. CHECK(0x23456789 == v1);
  516. int32_t v2;
  517. TEST_ESP_OK(nvs_get_i32(handle_2, "foo", &v2));
  518. CHECK(0x3456789a == v2);
  519. char buf[strlen(str) + 1];
  520. size_t buf_len = sizeof(buf);
  521. size_t buf_len_needed;
  522. TEST_ESP_OK(nvs_get_str(handle_2, "key", NULL, &buf_len_needed));
  523. CHECK(buf_len_needed == buf_len);
  524. size_t buf_len_short = buf_len - 1;
  525. TEST_ESP_ERR(ESP_ERR_NVS_INVALID_LENGTH, nvs_get_str(handle_2, "key", buf, &buf_len_short));
  526. CHECK(buf_len_short == buf_len);
  527. size_t buf_len_long = buf_len + 1;
  528. TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len_long));
  529. CHECK(buf_len_long == buf_len);
  530. TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len));
  531. CHECK(0 == strcmp(buf, str));
  532. nvs_close(handle_1);
  533. nvs_close(handle_2);
  534. }
  535. TEST_CASE("wifi test", "[nvs]")
  536. {
  537. SpiFlashEmulator emu(10);
  538. emu.randomize(10);
  539. const uint32_t NVS_FLASH_SECTOR = 5;
  540. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  541. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  542. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  543. nvs_handle misc_handle;
  544. TEST_ESP_OK(nvs_open("nvs.net80211", NVS_READWRITE, &misc_handle));
  545. char log[33];
  546. size_t log_size = sizeof(log);
  547. TEST_ESP_ERR(nvs_get_str(misc_handle, "log", log, &log_size), ESP_ERR_NVS_NOT_FOUND);
  548. strcpy(log, "foobarbazfizzz");
  549. TEST_ESP_OK(nvs_set_str(misc_handle, "log", log));
  550. nvs_handle net80211_handle;
  551. TEST_ESP_OK(nvs_open("nvs.net80211", NVS_READWRITE, &net80211_handle));
  552. uint8_t opmode = 2;
  553. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "wifi.opmode", &opmode), ESP_ERR_NVS_NOT_FOUND);
  554. TEST_ESP_OK(nvs_set_u8(net80211_handle, "wifi.opmode", opmode));
  555. uint8_t country = 0;
  556. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "wifi.country", &opmode), ESP_ERR_NVS_NOT_FOUND);
  557. TEST_ESP_OK(nvs_set_u8(net80211_handle, "wifi.country", opmode));
  558. char ssid[36];
  559. size_t size = sizeof(ssid);
  560. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.ssid", ssid, &size), ESP_ERR_NVS_NOT_FOUND);
  561. strcpy(ssid, "my android AP");
  562. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.ssid", ssid, size));
  563. char mac[6];
  564. size = sizeof(mac);
  565. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.mac", mac, &size), ESP_ERR_NVS_NOT_FOUND);
  566. memset(mac, 0xab, 6);
  567. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.mac", mac, size));
  568. uint8_t authmode = 1;
  569. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "sta.authmode", &authmode), ESP_ERR_NVS_NOT_FOUND);
  570. TEST_ESP_OK(nvs_set_u8(net80211_handle, "sta.authmode", authmode));
  571. char pswd[65];
  572. size = sizeof(pswd);
  573. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.pswd", pswd, &size), ESP_ERR_NVS_NOT_FOUND);
  574. strcpy(pswd, "`123456788990-=");
  575. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.pswd", pswd, size));
  576. char pmk[32];
  577. size = sizeof(pmk);
  578. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.pmk", pmk, &size), ESP_ERR_NVS_NOT_FOUND);
  579. memset(pmk, 1, size);
  580. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.pmk", pmk, size));
  581. uint8_t chan = 1;
  582. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "sta.chan", &chan), ESP_ERR_NVS_NOT_FOUND);
  583. TEST_ESP_OK(nvs_set_u8(net80211_handle, "sta.chan", chan));
  584. uint8_t autoconn = 1;
  585. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "auto.conn", &autoconn), ESP_ERR_NVS_NOT_FOUND);
  586. TEST_ESP_OK(nvs_set_u8(net80211_handle, "auto.conn", autoconn));
  587. uint8_t bssid_set = 1;
  588. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "bssid.set", &bssid_set), ESP_ERR_NVS_NOT_FOUND);
  589. TEST_ESP_OK(nvs_set_u8(net80211_handle, "bssid.set", bssid_set));
  590. char bssid[6];
  591. size = sizeof(bssid);
  592. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.bssid", bssid, &size), ESP_ERR_NVS_NOT_FOUND);
  593. memset(mac, 0xcd, 6);
  594. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.bssid", bssid, size));
  595. uint8_t phym = 3;
  596. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "sta.phym", &phym), ESP_ERR_NVS_NOT_FOUND);
  597. TEST_ESP_OK(nvs_set_u8(net80211_handle, "sta.phym", phym));
  598. uint8_t phybw = 2;
  599. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "sta.phybw", &phybw), ESP_ERR_NVS_NOT_FOUND);
  600. TEST_ESP_OK(nvs_set_u8(net80211_handle, "sta.phybw", phybw));
  601. char apsw[2];
  602. size = sizeof(apsw);
  603. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.apsw", apsw, &size), ESP_ERR_NVS_NOT_FOUND);
  604. memset(apsw, 0x2, size);
  605. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.apsw", apsw, size));
  606. char apinfo[700];
  607. size = sizeof(apinfo);
  608. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "sta.apinfo", apinfo, &size), ESP_ERR_NVS_NOT_FOUND);
  609. memset(apinfo, 0, size);
  610. TEST_ESP_OK(nvs_set_blob(net80211_handle, "sta.apinfo", apinfo, size));
  611. size = sizeof(ssid);
  612. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "ap.ssid", ssid, &size), ESP_ERR_NVS_NOT_FOUND);
  613. strcpy(ssid, "ESP_A2F340");
  614. TEST_ESP_OK(nvs_set_blob(net80211_handle, "ap.ssid", ssid, size));
  615. size = sizeof(mac);
  616. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "ap.mac", mac, &size), ESP_ERR_NVS_NOT_FOUND);
  617. memset(mac, 0xac, 6);
  618. TEST_ESP_OK(nvs_set_blob(net80211_handle, "ap.mac", mac, size));
  619. size = sizeof(pswd);
  620. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "ap.passwd", pswd, &size), ESP_ERR_NVS_NOT_FOUND);
  621. strcpy(pswd, "");
  622. TEST_ESP_OK(nvs_set_blob(net80211_handle, "ap.passwd", pswd, size));
  623. size = sizeof(pmk);
  624. TEST_ESP_ERR(nvs_get_blob(net80211_handle, "ap.pmk", pmk, &size), ESP_ERR_NVS_NOT_FOUND);
  625. memset(pmk, 1, size);
  626. TEST_ESP_OK(nvs_set_blob(net80211_handle, "ap.pmk", pmk, size));
  627. chan = 6;
  628. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "ap.chan", &chan), ESP_ERR_NVS_NOT_FOUND);
  629. TEST_ESP_OK(nvs_set_u8(net80211_handle, "ap.chan", chan));
  630. authmode = 0;
  631. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "ap.authmode", &authmode), ESP_ERR_NVS_NOT_FOUND);
  632. TEST_ESP_OK(nvs_set_u8(net80211_handle, "ap.authmode", authmode));
  633. uint8_t hidden = 0;
  634. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "ap.hidden", &hidden), ESP_ERR_NVS_NOT_FOUND);
  635. TEST_ESP_OK(nvs_set_u8(net80211_handle, "ap.hidden", hidden));
  636. uint8_t max_conn = 4;
  637. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "ap.max.conn", &max_conn), ESP_ERR_NVS_NOT_FOUND);
  638. TEST_ESP_OK(nvs_set_u8(net80211_handle, "ap.max.conn", max_conn));
  639. uint8_t bcn_interval = 2;
  640. TEST_ESP_ERR(nvs_get_u8(net80211_handle, "bcn_interval", &bcn_interval), ESP_ERR_NVS_NOT_FOUND);
  641. TEST_ESP_OK(nvs_set_u8(net80211_handle, "bcn_interval", bcn_interval));
  642. s_perf << "Time to simulate nvs init with wifi libs: " << emu.getTotalTime() << " us (" << emu.getEraseOps() << "E " << emu.getWriteOps() << "W " << emu.getReadOps() << "R " << emu.getWriteBytes() << "Wb " << emu.getReadBytes() << "Rb)" << std::endl;
  643. }
  644. TEST_CASE("can init storage from flash with random contents", "[nvs]")
  645. {
  646. SpiFlashEmulator emu(10);
  647. emu.randomize(42);
  648. nvs_handle handle;
  649. const uint32_t NVS_FLASH_SECTOR = 5;
  650. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  651. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  652. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  653. TEST_ESP_OK(nvs_open("nvs.net80211", NVS_READWRITE, &handle));
  654. uint8_t opmode = 2;
  655. if (nvs_get_u8(handle, "wifi.opmode", &opmode) != ESP_OK) {
  656. TEST_ESP_OK(nvs_set_u8(handle, "wifi.opmode", opmode));
  657. }
  658. }
  659. TEST_CASE("nvs api tests, starting with random data in flash", "[nvs][long]")
  660. {
  661. const size_t testIters = 3000;
  662. int lastPercent = -1;
  663. for (size_t count = 0; count < testIters; ++count) {
  664. int percentDone = (int) (count * 100 / testIters);
  665. if (percentDone != lastPercent) {
  666. lastPercent = percentDone;
  667. printf("%d%%\n", percentDone);
  668. }
  669. SpiFlashEmulator emu(10);
  670. emu.randomize(static_cast<uint32_t>(count));
  671. const uint32_t NVS_FLASH_SECTOR = 6;
  672. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  673. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  674. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  675. nvs_handle handle_1;
  676. TEST_ESP_ERR(nvs_open("namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND);
  677. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle_1));
  678. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x12345678));
  679. for (size_t i = 0; i < 500; ++i) {
  680. nvs_handle handle_2;
  681. TEST_ESP_OK(nvs_open("namespace2", NVS_READWRITE, &handle_2));
  682. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x23456789 % (i + 1)));
  683. TEST_ESP_OK(nvs_set_i32(handle_2, "foo", static_cast<int32_t>(i)));
  684. const char* str = "value 0123456789abcdef0123456789abcdef %09d";
  685. char str_buf[128];
  686. snprintf(str_buf, sizeof(str_buf), str, i + count * 1024);
  687. TEST_ESP_OK(nvs_set_str(handle_2, "key", str_buf));
  688. int32_t v1;
  689. TEST_ESP_OK(nvs_get_i32(handle_1, "foo", &v1));
  690. CHECK(0x23456789 % (i + 1) == v1);
  691. int32_t v2;
  692. TEST_ESP_OK(nvs_get_i32(handle_2, "foo", &v2));
  693. CHECK(static_cast<int32_t>(i) == v2);
  694. char buf[128];
  695. size_t buf_len = sizeof(buf);
  696. TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len));
  697. CHECK(0 == strcmp(buf, str_buf));
  698. nvs_close(handle_2);
  699. }
  700. nvs_close(handle_1);
  701. }
  702. }
  703. extern "C" void nvs_dump(const char *partName);
  704. class RandomTest {
  705. static const size_t nKeys = 11;
  706. int32_t v1 = 0, v2 = 0;
  707. uint64_t v3 = 0, v4 = 0;
  708. static const size_t strBufLen = 1024;
  709. static const size_t smallBlobLen = Page::CHUNK_MAX_SIZE / 3;
  710. static const size_t largeBlobLen = Page::CHUNK_MAX_SIZE * 3;
  711. char v5[strBufLen], v6[strBufLen], v7[strBufLen], v8[strBufLen], v9[strBufLen];
  712. uint8_t v10[smallBlobLen], v11[largeBlobLen];
  713. bool written[nKeys];
  714. public:
  715. RandomTest()
  716. {
  717. std::fill_n(written, nKeys, false);
  718. }
  719. template<typename TGen>
  720. esp_err_t doRandomThings(nvs_handle handle, TGen gen, size_t& count) {
  721. const char* keys[] = {"foo", "bar", "longkey_0123456", "another key", "param1", "param2", "param3", "param4", "param5", "singlepage", "multipage"};
  722. const ItemType types[] = {ItemType::I32, ItemType::I32, ItemType::U64, ItemType::U64, ItemType::SZ, ItemType::SZ, ItemType::SZ, ItemType::SZ, ItemType::SZ, ItemType::BLOB, ItemType::BLOB};
  723. void* values[] = {&v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, &v10, &v11};
  724. const size_t nKeys = sizeof(keys) / sizeof(keys[0]);
  725. static_assert(nKeys == sizeof(types) / sizeof(types[0]), "");
  726. static_assert(nKeys == sizeof(values) / sizeof(values[0]), "");
  727. auto randomRead = [&](size_t index) -> esp_err_t {
  728. switch (types[index]) {
  729. case ItemType::I32:
  730. {
  731. int32_t val;
  732. auto err = nvs_get_i32(handle, keys[index], &val);
  733. if (err == ESP_ERR_FLASH_OP_FAIL) {
  734. return err;
  735. }
  736. if (!written[index]) {
  737. REQUIRE(err == ESP_ERR_NVS_NOT_FOUND);
  738. }
  739. else {
  740. REQUIRE(err == ESP_OK);
  741. REQUIRE(val == *reinterpret_cast<int32_t*>(values[index]));
  742. }
  743. break;
  744. }
  745. case ItemType::U64:
  746. {
  747. uint64_t val;
  748. auto err = nvs_get_u64(handle, keys[index], &val);
  749. if (err == ESP_ERR_FLASH_OP_FAIL) {
  750. return err;
  751. }
  752. if (!written[index]) {
  753. REQUIRE(err == ESP_ERR_NVS_NOT_FOUND);
  754. }
  755. else {
  756. REQUIRE(err == ESP_OK);
  757. REQUIRE(val == *reinterpret_cast<uint64_t*>(values[index]));
  758. }
  759. break;
  760. }
  761. case ItemType::SZ:
  762. {
  763. char buf[strBufLen];
  764. size_t len = strBufLen;
  765. auto err = nvs_get_str(handle, keys[index], buf, &len);
  766. if (err == ESP_ERR_FLASH_OP_FAIL) {
  767. return err;
  768. }
  769. if (!written[index]) {
  770. REQUIRE(err == ESP_ERR_NVS_NOT_FOUND);
  771. }
  772. else {
  773. REQUIRE(err == ESP_OK);
  774. REQUIRE(strncmp(buf, reinterpret_cast<const char*>(values[index]), strBufLen) == 0);
  775. }
  776. break;
  777. }
  778. case ItemType::BLOB:
  779. {
  780. uint32_t blobBufLen = 0;
  781. if(strncmp(keys[index],"singlepage", sizeof("singlepage")) == 0) {
  782. blobBufLen = smallBlobLen ;
  783. } else {
  784. blobBufLen = largeBlobLen ;
  785. }
  786. uint8_t buf[blobBufLen];
  787. memset(buf, 0, blobBufLen);
  788. size_t len = blobBufLen;
  789. auto err = nvs_get_blob(handle, keys[index], buf, &len);
  790. if (err == ESP_ERR_FLASH_OP_FAIL) {
  791. return err;
  792. }
  793. if (!written[index]) {
  794. REQUIRE(err == ESP_ERR_NVS_NOT_FOUND);
  795. }
  796. else {
  797. REQUIRE(err == ESP_OK);
  798. REQUIRE(memcmp(buf, reinterpret_cast<const uint8_t*>(values[index]), blobBufLen) == 0);
  799. }
  800. break;
  801. }
  802. default:
  803. assert(0);
  804. }
  805. return ESP_OK;
  806. };
  807. auto randomWrite = [&](size_t index) -> esp_err_t {
  808. switch (types[index]) {
  809. case ItemType::I32:
  810. {
  811. int32_t val = static_cast<int32_t>(gen());
  812. auto err = nvs_set_i32(handle, keys[index], val);
  813. if (err == ESP_ERR_FLASH_OP_FAIL) {
  814. return err;
  815. }
  816. if (err == ESP_ERR_NVS_REMOVE_FAILED) {
  817. written[index] = true;
  818. *reinterpret_cast<int32_t*>(values[index]) = val;
  819. return ESP_ERR_FLASH_OP_FAIL;
  820. }
  821. REQUIRE(err == ESP_OK);
  822. written[index] = true;
  823. *reinterpret_cast<int32_t*>(values[index]) = val;
  824. break;
  825. }
  826. case ItemType::U64:
  827. {
  828. uint64_t val = static_cast<uint64_t>(gen());
  829. auto err = nvs_set_u64(handle, keys[index], val);
  830. if (err == ESP_ERR_FLASH_OP_FAIL) {
  831. return err;
  832. }
  833. if (err == ESP_ERR_NVS_REMOVE_FAILED) {
  834. written[index] = true;
  835. *reinterpret_cast<uint64_t*>(values[index]) = val;
  836. return ESP_ERR_FLASH_OP_FAIL;
  837. }
  838. REQUIRE(err == ESP_OK);
  839. written[index] = true;
  840. *reinterpret_cast<uint64_t*>(values[index]) = val;
  841. break;
  842. }
  843. case ItemType::SZ:
  844. {
  845. char buf[strBufLen];
  846. size_t len = strBufLen;
  847. size_t strLen = gen() % (strBufLen - 1);
  848. std::generate_n(buf, strLen, [&]() -> char {
  849. const char c = static_cast<char>(gen() % 127);
  850. return (c < 32) ? 32 : c;
  851. });
  852. buf[strLen] = 0;
  853. auto err = nvs_set_str(handle, keys[index], buf);
  854. if (err == ESP_ERR_FLASH_OP_FAIL) {
  855. return err;
  856. }
  857. if (err == ESP_ERR_NVS_REMOVE_FAILED) {
  858. written[index] = true;
  859. strncpy(reinterpret_cast<char*>(values[index]), buf, strBufLen);
  860. return ESP_ERR_FLASH_OP_FAIL;
  861. }
  862. REQUIRE(err == ESP_OK);
  863. written[index] = true;
  864. strncpy(reinterpret_cast<char*>(values[index]), buf, strBufLen);
  865. break;
  866. }
  867. case ItemType::BLOB:
  868. {
  869. uint32_t blobBufLen = 0;
  870. if(strncmp(keys[index],"singlepage", sizeof("singlepage")) == 0) {
  871. blobBufLen = smallBlobLen ;
  872. } else {
  873. blobBufLen = largeBlobLen ;
  874. }
  875. uint8_t buf[blobBufLen];
  876. memset(buf, 0, blobBufLen);
  877. size_t blobLen = gen() % blobBufLen;
  878. std::generate_n(buf, blobLen, [&]() -> uint8_t {
  879. return static_cast<uint8_t>(gen() % 256);
  880. });
  881. auto err = nvs_set_blob(handle, keys[index], buf, blobLen);
  882. if (err == ESP_ERR_FLASH_OP_FAIL) {
  883. return err;
  884. }
  885. if (err == ESP_ERR_NVS_REMOVE_FAILED) {
  886. written[index] = true;
  887. memcpy(reinterpret_cast<uint8_t*>(values[index]), buf, blobBufLen);
  888. return ESP_ERR_FLASH_OP_FAIL;
  889. }
  890. REQUIRE(err == ESP_OK);
  891. written[index] = true;
  892. memcpy(reinterpret_cast<char*>(values[index]), buf, blobBufLen);
  893. break;
  894. }
  895. default:
  896. assert(0);
  897. }
  898. return ESP_OK;
  899. };
  900. for (; count != 0; --count) {
  901. size_t index = gen() % (nKeys);
  902. switch (gen() % 3) {
  903. case 0: // read, 1/3
  904. if (randomRead(index) == ESP_ERR_FLASH_OP_FAIL) {
  905. return ESP_ERR_FLASH_OP_FAIL;
  906. }
  907. break;
  908. default: // write, 2/3
  909. if (randomWrite(index) == ESP_ERR_FLASH_OP_FAIL) {
  910. return ESP_ERR_FLASH_OP_FAIL;
  911. }
  912. break;
  913. }
  914. }
  915. return ESP_OK;
  916. }
  917. esp_err_t handleExternalWriteAtIndex(uint8_t index, const void* value, const size_t len ) {
  918. if(index == 9) { /* This is only done for small-page blobs for now*/
  919. if(len > smallBlobLen) {
  920. return ESP_FAIL;
  921. }
  922. memcpy(v10, value, len);
  923. written[index] = true;
  924. return ESP_OK;
  925. } else {
  926. return ESP_FAIL;
  927. }
  928. }
  929. };
  930. TEST_CASE("monkey test", "[nvs][monkey]")
  931. {
  932. std::random_device rd;
  933. std::mt19937 gen(rd());
  934. uint32_t seed = 3;
  935. gen.seed(seed);
  936. SpiFlashEmulator emu(10);
  937. emu.randomize(seed);
  938. emu.clearStats();
  939. const uint32_t NVS_FLASH_SECTOR = 2;
  940. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 8;
  941. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  942. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  943. nvs_handle handle;
  944. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  945. RandomTest test;
  946. size_t count = 1000;
  947. CHECK(test.doRandomThings(handle, gen, count) == ESP_OK);
  948. s_perf << "Monkey test: nErase=" << emu.getEraseOps() << " nWrite=" << emu.getWriteOps() << std::endl;
  949. }
  950. TEST_CASE("test recovery from sudden poweroff", "[long][nvs][recovery][monkey]")
  951. {
  952. std::random_device rd;
  953. std::mt19937 gen(rd());
  954. uint32_t seed = 3;
  955. gen.seed(seed);
  956. const size_t iter_count = 2000;
  957. SpiFlashEmulator emu(10);
  958. const uint32_t NVS_FLASH_SECTOR = 2;
  959. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 8;
  960. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  961. size_t totalOps = 0;
  962. int lastPercent = -1;
  963. for (uint32_t errDelay = 0; ; ++errDelay) {
  964. INFO(errDelay);
  965. emu.randomize(seed);
  966. emu.clearStats();
  967. emu.failAfter(errDelay);
  968. RandomTest test;
  969. if (totalOps != 0) {
  970. int percent = errDelay * 100 / totalOps;
  971. if (percent > lastPercent) {
  972. printf("%d/%d (%d%%)\r\n", errDelay, static_cast<int>(totalOps), percent);
  973. lastPercent = percent;
  974. }
  975. }
  976. nvs_handle handle;
  977. size_t count = iter_count;
  978. if (nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK) {
  979. if (nvs_open("namespace1", NVS_READWRITE, &handle) == ESP_OK) {
  980. if(test.doRandomThings(handle, gen, count) != ESP_ERR_FLASH_OP_FAIL) {
  981. nvs_close(handle);
  982. break;
  983. }
  984. nvs_close(handle);
  985. }
  986. }
  987. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  988. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  989. auto res = test.doRandomThings(handle, gen, count);
  990. if (res != ESP_OK) {
  991. nvs_dump(NVS_DEFAULT_PART_NAME);
  992. CHECK(0);
  993. }
  994. nvs_close(handle);
  995. totalOps = emu.getEraseOps() + emu.getWriteBytes() / 4;
  996. }
  997. }
  998. TEST_CASE("test for memory leaks in open/set", "[leaks]")
  999. {
  1000. SpiFlashEmulator emu(10);
  1001. const uint32_t NVS_FLASH_SECTOR = 6;
  1002. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  1003. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  1004. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  1005. for (int i = 0; i < 100000; ++i) {
  1006. nvs_handle light_handle = 0;
  1007. char lightbulb[1024] = {12, 13, 14, 15, 16};
  1008. TEST_ESP_OK(nvs_open("light", NVS_READWRITE, &light_handle));
  1009. TEST_ESP_OK(nvs_set_blob(light_handle, "key", lightbulb, sizeof(lightbulb)));
  1010. TEST_ESP_OK(nvs_commit(light_handle));
  1011. nvs_close(light_handle);
  1012. }
  1013. }
  1014. TEST_CASE("duplicate items are removed", "[nvs][dupes]")
  1015. {
  1016. SpiFlashEmulator emu(3);
  1017. {
  1018. // create one item
  1019. nvs::Page p;
  1020. p.load(0);
  1021. p.writeItem<uint8_t>(1, "opmode", 3);
  1022. }
  1023. {
  1024. // add another two without deleting the first one
  1025. nvs::Item item(1, ItemType::U8, 1, "opmode");
  1026. item.data[0] = 2;
  1027. item.crc32 = item.calculateCrc32();
  1028. emu.write(3 * 32, reinterpret_cast<const uint32_t*>(&item), sizeof(item));
  1029. emu.write(4 * 32, reinterpret_cast<const uint32_t*>(&item), sizeof(item));
  1030. uint32_t mask = 0xFFFFFFEA;
  1031. emu.write(32, &mask, 4);
  1032. }
  1033. {
  1034. // load page and check that second item persists
  1035. nvs::Storage s;
  1036. s.init(0, 3);
  1037. uint8_t val;
  1038. ESP_ERROR_CHECK(s.readItem(1, "opmode", val));
  1039. CHECK(val == 2);
  1040. }
  1041. {
  1042. Page p;
  1043. p.load(0);
  1044. CHECK(p.getErasedEntryCount() == 2);
  1045. CHECK(p.getUsedEntryCount() == 1);
  1046. }
  1047. }
  1048. TEST_CASE("recovery after failure to write data", "[nvs]")
  1049. {
  1050. SpiFlashEmulator emu(3);
  1051. const char str[] = "value 0123456789abcdef012345678value 0123456789abcdef012345678";
  1052. // make flash write fail exactly in Page::writeEntryData
  1053. emu.failAfter(17);
  1054. {
  1055. Storage storage;
  1056. TEST_ESP_OK(storage.init(0, 3));
  1057. TEST_ESP_ERR(storage.writeItem(1, ItemType::SZ, "key", str, strlen(str)), ESP_ERR_FLASH_OP_FAIL);
  1058. // check that repeated operations cause an error
  1059. TEST_ESP_ERR(storage.writeItem(1, ItemType::SZ, "key", str, strlen(str)), ESP_ERR_NVS_INVALID_STATE);
  1060. uint8_t val;
  1061. TEST_ESP_ERR(storage.readItem(1, ItemType::U8, "key", &val, sizeof(val)), ESP_ERR_NVS_NOT_FOUND);
  1062. }
  1063. {
  1064. // load page and check that data was erased
  1065. Page p;
  1066. p.load(0);
  1067. CHECK(p.getErasedEntryCount() == 3);
  1068. CHECK(p.getUsedEntryCount() == 0);
  1069. // try to write again
  1070. TEST_ESP_OK(p.writeItem(1, ItemType::SZ, "key", str, strlen(str)));
  1071. }
  1072. }
  1073. TEST_CASE("crc errors in item header are handled", "[nvs]")
  1074. {
  1075. SpiFlashEmulator emu(3);
  1076. Storage storage;
  1077. // prepare some data
  1078. TEST_ESP_OK(storage.init(0, 3));
  1079. TEST_ESP_OK(storage.writeItem(0, "ns1", static_cast<uint8_t>(1)));
  1080. TEST_ESP_OK(storage.writeItem(1, "value1", static_cast<uint32_t>(1)));
  1081. TEST_ESP_OK(storage.writeItem(1, "value2", static_cast<uint32_t>(2)));
  1082. // corrupt item header
  1083. uint32_t val = 0;
  1084. emu.write(32 * 3, &val, 4);
  1085. // check that storage can recover
  1086. TEST_ESP_OK(storage.init(0, 3));
  1087. TEST_ESP_OK(storage.readItem(1, "value2", val));
  1088. CHECK(val == 2);
  1089. // check that the corrupted item is no longer present
  1090. TEST_ESP_ERR(ESP_ERR_NVS_NOT_FOUND, storage.readItem(1, "value1", val));
  1091. // add more items to make the page full
  1092. for (size_t i = 0; i < Page::ENTRY_COUNT; ++i) {
  1093. char item_name[Item::MAX_KEY_LENGTH + 1];
  1094. snprintf(item_name, sizeof(item_name), "item_%ld", (long int)i);
  1095. TEST_ESP_OK(storage.writeItem(1, item_name, static_cast<uint32_t>(i)));
  1096. }
  1097. // corrupt another item on the full page
  1098. val = 0;
  1099. emu.write(32 * 4, &val, 4);
  1100. // check that storage can recover
  1101. TEST_ESP_OK(storage.init(0, 3));
  1102. // check that the corrupted item is no longer present
  1103. TEST_ESP_ERR(ESP_ERR_NVS_NOT_FOUND, storage.readItem(1, "value2", val));
  1104. }
  1105. TEST_CASE("crc error in variable length item is handled", "[nvs]")
  1106. {
  1107. SpiFlashEmulator emu(3);
  1108. const uint64_t before_val = 0xbef04e;
  1109. const uint64_t after_val = 0xaf7e4;
  1110. // write some data
  1111. {
  1112. Page p;
  1113. p.load(0);
  1114. TEST_ESP_OK(p.writeItem<uint64_t>(0, "before", before_val));
  1115. const char* str = "foobar";
  1116. TEST_ESP_OK(p.writeItem(0, ItemType::SZ, "key", str, strlen(str)));
  1117. TEST_ESP_OK(p.writeItem<uint64_t>(0, "after", after_val));
  1118. }
  1119. // corrupt some data
  1120. uint32_t w;
  1121. CHECK(emu.read(&w, 32 * 3 + 8, sizeof(w)));
  1122. w &= 0xf000000f;
  1123. CHECK(emu.write(32 * 3 + 8, &w, sizeof(w)));
  1124. // load and check
  1125. {
  1126. Page p;
  1127. p.load(0);
  1128. CHECK(p.getUsedEntryCount() == 2);
  1129. CHECK(p.getErasedEntryCount() == 2);
  1130. uint64_t val;
  1131. TEST_ESP_OK(p.readItem<uint64_t>(0, "before", val));
  1132. CHECK(val == before_val);
  1133. TEST_ESP_ERR(p.findItem(0, ItemType::SZ, "key"), ESP_ERR_NVS_NOT_FOUND);
  1134. TEST_ESP_OK(p.readItem<uint64_t>(0, "after", val));
  1135. CHECK(val == after_val);
  1136. }
  1137. }
  1138. TEST_CASE("read/write failure (TW8406)", "[nvs]")
  1139. {
  1140. SpiFlashEmulator emu(3);
  1141. nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3);
  1142. for (int attempts = 0; attempts < 3; ++attempts) {
  1143. int i = 0;
  1144. nvs_handle light_handle = 0;
  1145. char key[15] = {0};
  1146. char data[76] = {12, 13, 14, 15, 16};
  1147. uint8_t number = 20;
  1148. size_t data_len = sizeof(data);
  1149. ESP_ERROR_CHECK(nvs_open("LIGHT", NVS_READWRITE, &light_handle));
  1150. ESP_ERROR_CHECK(nvs_set_u8(light_handle, "RecordNum", number));
  1151. for (i = 0; i < number; ++i) {
  1152. sprintf(key, "light%d", i);
  1153. ESP_ERROR_CHECK(nvs_set_blob(light_handle, key, data, sizeof(data)));
  1154. }
  1155. nvs_commit(light_handle);
  1156. uint8_t get_number = 0;
  1157. ESP_ERROR_CHECK(nvs_get_u8(light_handle, "RecordNum", &get_number));
  1158. REQUIRE(number == get_number);
  1159. for (i = 0; i < number; ++i) {
  1160. char data[76] = {0};
  1161. sprintf(key, "light%d", i);
  1162. ESP_ERROR_CHECK(nvs_get_blob(light_handle, key, data, &data_len));
  1163. }
  1164. nvs_close(light_handle);
  1165. }
  1166. }
  1167. TEST_CASE("nvs_flash_init checks for an empty page", "[nvs]")
  1168. {
  1169. const size_t blob_size = Page::CHUNK_MAX_SIZE;
  1170. uint8_t blob[blob_size] = {0};
  1171. SpiFlashEmulator emu(5);
  1172. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5) );
  1173. nvs_handle handle;
  1174. TEST_ESP_OK( nvs_open("test", NVS_READWRITE, &handle) );
  1175. // Fill first page
  1176. TEST_ESP_OK( nvs_set_blob(handle, "1a", blob, blob_size) );
  1177. // Fill second page
  1178. TEST_ESP_OK( nvs_set_blob(handle, "2a", blob, blob_size) );
  1179. // Fill third page
  1180. TEST_ESP_OK( nvs_set_blob(handle, "3a", blob, blob_size) );
  1181. TEST_ESP_OK( nvs_commit(handle) );
  1182. nvs_close(handle);
  1183. // first two pages are now full, third one is writable, last two are empty
  1184. // init should fail
  1185. TEST_ESP_ERR( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3), ESP_ERR_NVS_NO_FREE_PAGES );
  1186. }
  1187. TEST_CASE("multiple partitions access check", "[nvs]")
  1188. {
  1189. SpiFlashEmulator emu(10);
  1190. TEST_ESP_OK( nvs_flash_init_custom("nvs1", 0, 5) );
  1191. TEST_ESP_OK( nvs_flash_init_custom("nvs2", 5, 5) );
  1192. nvs_handle handle1, handle2;
  1193. TEST_ESP_OK( nvs_open_from_partition("nvs1", "test", NVS_READWRITE, &handle1) );
  1194. TEST_ESP_OK( nvs_open_from_partition("nvs2", "test", NVS_READWRITE, &handle2) );
  1195. TEST_ESP_OK( nvs_set_i32(handle1, "foo", 0xdeadbeef));
  1196. TEST_ESP_OK( nvs_set_i32(handle2, "foo", 0xcafebabe));
  1197. int32_t v1, v2;
  1198. TEST_ESP_OK( nvs_get_i32(handle1, "foo", &v1));
  1199. TEST_ESP_OK( nvs_get_i32(handle2, "foo", &v2));
  1200. CHECK(v1 == 0xdeadbeef);
  1201. CHECK(v2 == 0xcafebabe);
  1202. }
  1203. TEST_CASE("nvs page selection takes into account free entries also not just erased entries", "[nvs]")
  1204. {
  1205. const size_t blob_size = Page::CHUNK_MAX_SIZE/2;
  1206. uint8_t blob[blob_size] = {0};
  1207. SpiFlashEmulator emu(3);
  1208. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3) );
  1209. nvs_handle handle;
  1210. TEST_ESP_OK( nvs_open("test", NVS_READWRITE, &handle) );
  1211. // Fill first page
  1212. TEST_ESP_OK( nvs_set_blob(handle, "1a", blob, blob_size/3) );
  1213. TEST_ESP_OK( nvs_set_blob(handle, "1b", blob, blob_size) );
  1214. // Fill second page
  1215. TEST_ESP_OK( nvs_set_blob(handle, "2a", blob, blob_size) );
  1216. TEST_ESP_OK( nvs_set_blob(handle, "2b", blob, blob_size) );
  1217. // The item below should be able to fit the first page.
  1218. TEST_ESP_OK( nvs_set_blob(handle, "3a", blob, 4) );
  1219. TEST_ESP_OK( nvs_commit(handle) );
  1220. nvs_close(handle);
  1221. }
  1222. TEST_CASE("calculate used and free space", "[nvs]")
  1223. {
  1224. SpiFlashEmulator emu(6);
  1225. nvs_flash_deinit();
  1226. TEST_ESP_ERR(nvs_get_stats(NULL, NULL), ESP_ERR_INVALID_ARG);
  1227. nvs_stats_t stat1;
  1228. nvs_stats_t stat2;
  1229. TEST_ESP_ERR(nvs_get_stats(NULL, &stat1), ESP_ERR_NVS_NOT_INITIALIZED);
  1230. CHECK(stat1.free_entries == 0);
  1231. CHECK(stat1.namespace_count == 0);
  1232. CHECK(stat1.total_entries == 0);
  1233. CHECK(stat1.used_entries == 0);
  1234. nvs_handle handle = 0;
  1235. size_t h_count_entries;
  1236. TEST_ESP_ERR(nvs_get_used_entry_count(handle, &h_count_entries), ESP_ERR_NVS_INVALID_HANDLE);
  1237. CHECK(h_count_entries == 0);
  1238. // init nvs
  1239. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 6));
  1240. TEST_ESP_ERR(nvs_get_used_entry_count(handle, &h_count_entries), ESP_ERR_NVS_INVALID_HANDLE);
  1241. CHECK(h_count_entries == 0);
  1242. Page p;
  1243. // after erase. empty partition
  1244. TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
  1245. CHECK(stat1.free_entries != 0);
  1246. CHECK(stat1.namespace_count == 0);
  1247. CHECK(stat1.total_entries == 6 * p.ENTRY_COUNT);
  1248. CHECK(stat1.used_entries == 0);
  1249. // create namespace test_k1
  1250. nvs_handle handle_1;
  1251. TEST_ESP_OK(nvs_open("test_k1", NVS_READWRITE, &handle_1));
  1252. TEST_ESP_OK(nvs_get_stats(NULL, &stat2));
  1253. CHECK(stat2.free_entries + 1 == stat1.free_entries);
  1254. CHECK(stat2.namespace_count == 1);
  1255. CHECK(stat2.total_entries == stat1.total_entries);
  1256. CHECK(stat2.used_entries == 1);
  1257. // create pair key-value com
  1258. TEST_ESP_OK(nvs_set_i32(handle_1, "com", 0x12345678));
  1259. TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
  1260. CHECK(stat1.free_entries + 1 == stat2.free_entries);
  1261. CHECK(stat1.namespace_count == 1);
  1262. CHECK(stat1.total_entries == stat2.total_entries);
  1263. CHECK(stat1.used_entries == 2);
  1264. // change value in com
  1265. TEST_ESP_OK(nvs_set_i32(handle_1, "com", 0x01234567));
  1266. TEST_ESP_OK(nvs_get_stats(NULL, &stat2));
  1267. CHECK(stat2.free_entries == stat1.free_entries);
  1268. CHECK(stat2.namespace_count == 1);
  1269. CHECK(stat2.total_entries != 0);
  1270. CHECK(stat2.used_entries == 2);
  1271. // create pair key-value ru
  1272. TEST_ESP_OK(nvs_set_i32(handle_1, "ru", 0x00FF00FF));
  1273. TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
  1274. CHECK(stat1.free_entries + 1 == stat2.free_entries);
  1275. CHECK(stat1.namespace_count == 1);
  1276. CHECK(stat1.total_entries != 0);
  1277. CHECK(stat1.used_entries == 3);
  1278. // amount valid pair in namespace 1
  1279. size_t h1_count_entries;
  1280. TEST_ESP_OK(nvs_get_used_entry_count(handle_1, &h1_count_entries));
  1281. CHECK(h1_count_entries == 2);
  1282. nvs_handle handle_2;
  1283. // create namespace test_k2
  1284. TEST_ESP_OK(nvs_open("test_k2", NVS_READWRITE, &handle_2));
  1285. TEST_ESP_OK(nvs_get_stats(NULL, &stat2));
  1286. CHECK(stat2.free_entries + 1 == stat1.free_entries);
  1287. CHECK(stat2.namespace_count == 2);
  1288. CHECK(stat2.total_entries == stat1.total_entries);
  1289. CHECK(stat2.used_entries == 4);
  1290. // create pair key-value
  1291. TEST_ESP_OK(nvs_set_i32(handle_2, "su1", 0x00000001));
  1292. TEST_ESP_OK(nvs_set_i32(handle_2, "su2", 0x00000002));
  1293. TEST_ESP_OK(nvs_set_i32(handle_2, "sus", 0x00000003));
  1294. TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
  1295. CHECK(stat1.free_entries + 3 == stat2.free_entries);
  1296. CHECK(stat1.namespace_count == 2);
  1297. CHECK(stat1.total_entries == stat2.total_entries);
  1298. CHECK(stat1.used_entries == 7);
  1299. CHECK(stat1.total_entries == (stat1.used_entries + stat1.free_entries));
  1300. // amount valid pair in namespace 2
  1301. size_t h2_count_entries;
  1302. TEST_ESP_OK(nvs_get_used_entry_count(handle_2, &h2_count_entries));
  1303. CHECK(h2_count_entries == 3);
  1304. CHECK(stat1.used_entries == (h1_count_entries + h2_count_entries + stat1.namespace_count));
  1305. nvs_close(handle_1);
  1306. nvs_close(handle_2);
  1307. size_t temp = h2_count_entries;
  1308. TEST_ESP_ERR(nvs_get_used_entry_count(handle_1, &h2_count_entries), ESP_ERR_NVS_INVALID_HANDLE);
  1309. CHECK(h2_count_entries == 0);
  1310. h2_count_entries = temp;
  1311. TEST_ESP_ERR(nvs_get_used_entry_count(handle_1, NULL), ESP_ERR_INVALID_ARG);
  1312. nvs_handle handle_3;
  1313. // create namespace test_k3
  1314. TEST_ESP_OK(nvs_open("test_k3", NVS_READWRITE, &handle_3));
  1315. TEST_ESP_OK(nvs_get_stats(NULL, &stat2));
  1316. CHECK(stat2.free_entries + 1 == stat1.free_entries);
  1317. CHECK(stat2.namespace_count == 3);
  1318. CHECK(stat2.total_entries == stat1.total_entries);
  1319. CHECK(stat2.used_entries == 8);
  1320. // create pair blobs
  1321. uint32_t blob[12];
  1322. TEST_ESP_OK(nvs_set_blob(handle_3, "bl1", &blob, sizeof(blob)));
  1323. TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
  1324. CHECK(stat1.free_entries + 4 == stat2.free_entries);
  1325. CHECK(stat1.namespace_count == 3);
  1326. CHECK(stat1.total_entries == stat2.total_entries);
  1327. CHECK(stat1.used_entries == 12);
  1328. // amount valid pair in namespace 2
  1329. size_t h3_count_entries;
  1330. TEST_ESP_OK(nvs_get_used_entry_count(handle_3, &h3_count_entries));
  1331. CHECK(h3_count_entries == 4);
  1332. CHECK(stat1.used_entries == (h1_count_entries + h2_count_entries + h3_count_entries + stat1.namespace_count));
  1333. nvs_close(handle_3);
  1334. }
  1335. TEST_CASE("Recovery from power-off when the entry being erased is not on active page", "[nvs]")
  1336. {
  1337. const size_t blob_size = Page::CHUNK_MAX_SIZE/2 ;
  1338. size_t read_size = blob_size;
  1339. uint8_t blob[blob_size] = {0x11};
  1340. SpiFlashEmulator emu(3);
  1341. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3) );
  1342. nvs_handle handle;
  1343. TEST_ESP_OK( nvs_open("test", NVS_READWRITE, &handle) );
  1344. emu.clearStats();
  1345. emu.failAfter(Page::CHUNK_MAX_SIZE/4 + 75);
  1346. TEST_ESP_OK( nvs_set_blob(handle, "1a", blob, blob_size) );
  1347. TEST_ESP_OK( nvs_set_blob(handle, "1b", blob, blob_size) );
  1348. TEST_ESP_ERR( nvs_erase_key(handle, "1a"), ESP_ERR_FLASH_OP_FAIL );
  1349. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3) );
  1350. /* Check 1a is erased fully*/
  1351. TEST_ESP_ERR( nvs_get_blob(handle, "1a", blob, &read_size), ESP_ERR_NVS_NOT_FOUND);
  1352. /* Check 2b is still accessible*/
  1353. TEST_ESP_OK( nvs_get_blob(handle, "1b", blob, &read_size));
  1354. nvs_close(handle);
  1355. }
  1356. TEST_CASE("Recovery from power-off when page is being freed.", "[nvs]")
  1357. {
  1358. const size_t blob_size = (Page::ENTRY_COUNT-3) * Page::ENTRY_SIZE;
  1359. size_t read_size = blob_size/2;
  1360. uint8_t blob[blob_size] = {0};
  1361. SpiFlashEmulator emu(3);
  1362. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1363. nvs_handle handle;
  1364. TEST_ESP_OK(nvs_open("test", NVS_READWRITE, &handle));
  1365. // Fill first page
  1366. TEST_ESP_OK(nvs_set_blob(handle, "1a", blob, blob_size/3));
  1367. TEST_ESP_OK(nvs_set_blob(handle, "1b", blob, blob_size/3));
  1368. TEST_ESP_OK(nvs_set_blob(handle, "1c", blob, blob_size/4));
  1369. // Fill second page
  1370. TEST_ESP_OK(nvs_set_blob(handle, "2a", blob, blob_size/2));
  1371. TEST_ESP_OK(nvs_set_blob(handle, "2b", blob, blob_size/2));
  1372. TEST_ESP_OK(nvs_erase_key(handle, "1c"));
  1373. emu.clearStats();
  1374. emu.failAfter(6 * Page::ENTRY_COUNT);
  1375. TEST_ESP_ERR(nvs_set_blob(handle, "1d", blob, blob_size/4), ESP_ERR_FLASH_OP_FAIL);
  1376. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1377. read_size = blob_size/3;
  1378. TEST_ESP_OK( nvs_get_blob(handle, "1a", blob, &read_size));
  1379. TEST_ESP_OK( nvs_get_blob(handle, "1b", blob, &read_size));
  1380. read_size = blob_size /4;
  1381. TEST_ESP_ERR( nvs_get_blob(handle, "1c", blob, &read_size), ESP_ERR_NVS_NOT_FOUND);
  1382. TEST_ESP_ERR( nvs_get_blob(handle, "1d", blob, &read_size), ESP_ERR_NVS_NOT_FOUND);
  1383. read_size = blob_size /2;
  1384. TEST_ESP_OK( nvs_get_blob(handle, "2a", blob, &read_size));
  1385. TEST_ESP_OK( nvs_get_blob(handle, "2b", blob, &read_size));
  1386. TEST_ESP_OK(nvs_commit(handle));
  1387. nvs_close(handle);
  1388. }
  1389. TEST_CASE("Multi-page blobs are supported", "[nvs]")
  1390. {
  1391. const size_t blob_size = Page::CHUNK_MAX_SIZE *2;
  1392. uint8_t blob[blob_size] = {0};
  1393. SpiFlashEmulator emu(5);
  1394. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5));
  1395. nvs_handle handle;
  1396. TEST_ESP_OK(nvs_open("test", NVS_READWRITE, &handle));
  1397. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
  1398. TEST_ESP_OK(nvs_commit(handle));
  1399. nvs_close(handle);
  1400. }
  1401. TEST_CASE("Failures are handled while storing multi-page blobs", "[nvs]")
  1402. {
  1403. const size_t blob_size = Page::CHUNK_MAX_SIZE *7;
  1404. uint8_t blob[blob_size] = {0};
  1405. SpiFlashEmulator emu(5);
  1406. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5));
  1407. nvs_handle handle;
  1408. TEST_ESP_OK(nvs_open("test", NVS_READWRITE, &handle));
  1409. TEST_ESP_ERR(nvs_set_blob(handle, "abc", blob, blob_size), ESP_ERR_NVS_VALUE_TOO_LONG);
  1410. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, Page::CHUNK_MAX_SIZE*2));
  1411. TEST_ESP_OK(nvs_commit(handle));
  1412. nvs_close(handle);
  1413. }
  1414. TEST_CASE("Reading multi-page blobs", "[nvs]")
  1415. {
  1416. const size_t blob_size = Page::CHUNK_MAX_SIZE *3;
  1417. uint8_t blob[blob_size];
  1418. uint8_t blob_read[blob_size];
  1419. size_t read_size = blob_size;
  1420. SpiFlashEmulator emu(5);
  1421. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5));
  1422. nvs_handle handle;
  1423. memset(blob, 0x11, blob_size);
  1424. memset(blob_read, 0xee, blob_size);
  1425. TEST_ESP_OK(nvs_open("readTest", NVS_READWRITE, &handle));
  1426. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
  1427. TEST_ESP_OK(nvs_get_blob(handle, "abc", blob_read, &read_size));
  1428. CHECK(memcmp(blob, blob_read, blob_size) == 0);
  1429. TEST_ESP_OK(nvs_commit(handle));
  1430. nvs_close(handle);
  1431. }
  1432. TEST_CASE("Modification of values for Multi-page blobs are supported", "[nvs]")
  1433. {
  1434. const size_t blob_size = Page::CHUNK_MAX_SIZE *2;
  1435. uint8_t blob[blob_size] = {0};
  1436. uint8_t blob_read[blob_size] = {0xfe};;
  1437. uint8_t blob2[blob_size] = {0x11};
  1438. uint8_t blob3[blob_size] = {0x22};
  1439. uint8_t blob4[blob_size] ={ 0x33};
  1440. size_t read_size = blob_size;
  1441. SpiFlashEmulator emu(6);
  1442. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 6) );
  1443. nvs_handle handle;
  1444. memset(blob, 0x11, blob_size);
  1445. memset(blob2, 0x22, blob_size);
  1446. memset(blob3, 0x33, blob_size);
  1447. memset(blob4, 0x44, blob_size);
  1448. memset(blob_read, 0xff, blob_size);
  1449. TEST_ESP_OK( nvs_open("test", NVS_READWRITE, &handle) );
  1450. TEST_ESP_OK( nvs_set_blob(handle, "abc", blob, blob_size) );
  1451. TEST_ESP_OK( nvs_set_blob(handle, "abc", blob2, blob_size) );
  1452. TEST_ESP_OK( nvs_set_blob(handle, "abc", blob3, blob_size) );
  1453. TEST_ESP_OK( nvs_set_blob(handle, "abc", blob4, blob_size) );
  1454. TEST_ESP_OK( nvs_get_blob(handle, "abc", blob_read, &read_size));
  1455. CHECK(memcmp(blob4, blob_read, blob_size) == 0);
  1456. TEST_ESP_OK( nvs_commit(handle) );
  1457. nvs_close(handle);
  1458. }
  1459. TEST_CASE("Modification from single page blob to multi-page", "[nvs]")
  1460. {
  1461. const size_t blob_size = Page::CHUNK_MAX_SIZE *3;
  1462. uint8_t blob[blob_size] = {0};
  1463. uint8_t blob_read[blob_size] = {0xff};
  1464. size_t read_size = blob_size;
  1465. SpiFlashEmulator emu(5);
  1466. TEST_ESP_OK( nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5) );
  1467. nvs_handle handle;
  1468. TEST_ESP_OK(nvs_open("Test", NVS_READWRITE, &handle) );
  1469. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, Page::CHUNK_MAX_SIZE/2));
  1470. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
  1471. TEST_ESP_OK(nvs_get_blob(handle, "abc", blob_read, &read_size));
  1472. CHECK(memcmp(blob, blob_read, blob_size) == 0);
  1473. TEST_ESP_OK(nvs_commit(handle) );
  1474. nvs_close(handle);
  1475. }
  1476. TEST_CASE("Modification from multi-page to single page", "[nvs]")
  1477. {
  1478. const size_t blob_size = Page::CHUNK_MAX_SIZE *3;
  1479. uint8_t blob[blob_size] = {0};
  1480. uint8_t blob_read[blob_size] = {0xff};
  1481. size_t read_size = blob_size;
  1482. SpiFlashEmulator emu(5);
  1483. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5) );
  1484. nvs_handle handle;
  1485. TEST_ESP_OK(nvs_open("Test", NVS_READWRITE, &handle) );
  1486. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
  1487. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, Page::CHUNK_MAX_SIZE/2));
  1488. TEST_ESP_OK(nvs_set_blob(handle, "abc2", blob, blob_size));
  1489. TEST_ESP_OK(nvs_get_blob(handle, "abc", blob_read, &read_size));
  1490. CHECK(memcmp(blob, blob_read, Page::CHUNK_MAX_SIZE) == 0);
  1491. TEST_ESP_OK(nvs_commit(handle) );
  1492. nvs_close(handle);
  1493. }
  1494. TEST_CASE("Multi-page blob erased using nvs_erase_key should not be found when probed for just length", "[nvs]")
  1495. {
  1496. const size_t blob_size = Page::CHUNK_MAX_SIZE *3;
  1497. uint8_t blob[blob_size] = {0};
  1498. size_t read_size = blob_size;
  1499. SpiFlashEmulator emu(5);
  1500. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5));
  1501. nvs_handle handle;
  1502. TEST_ESP_OK(nvs_open("Test", NVS_READWRITE, &handle));
  1503. TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
  1504. TEST_ESP_OK(nvs_erase_key(handle, "abc"));
  1505. TEST_ESP_ERR(nvs_get_blob(handle, "abc", NULL, &read_size), ESP_ERR_NVS_NOT_FOUND);
  1506. TEST_ESP_OK(nvs_commit(handle));
  1507. nvs_close(handle);
  1508. }
  1509. TEST_CASE("Check that orphaned blobs are erased during init", "[nvs]")
  1510. {
  1511. const size_t blob_size = Page::CHUNK_MAX_SIZE *3 ;
  1512. uint8_t blob[blob_size] = {0x11};
  1513. uint8_t blob2[blob_size] = {0x22};
  1514. uint8_t blob3[blob_size] = {0x33};
  1515. SpiFlashEmulator emu(5);
  1516. Storage storage;
  1517. TEST_ESP_OK(storage.init(0, 5));
  1518. TEST_ESP_OK(storage.writeItem(1, ItemType::BLOB, "key", blob, sizeof(blob)));
  1519. TEST_ESP_OK(storage.init(0, 5));
  1520. /* Check that multi-page item is still available.**/
  1521. TEST_ESP_OK(storage.readItem(1, ItemType::BLOB, "key", blob, sizeof(blob)));
  1522. TEST_ESP_ERR(storage.writeItem(1, ItemType::BLOB, "key2", blob, sizeof(blob)), ESP_ERR_NVS_NOT_ENOUGH_SPACE);
  1523. Page p;
  1524. p.load(3); // This is where index will be placed.
  1525. p.erase();
  1526. TEST_ESP_OK(storage.init(0, 5));
  1527. TEST_ESP_ERR(storage.readItem(1, ItemType::BLOB, "key", blob, sizeof(blob)), ESP_ERR_NVS_NOT_FOUND);
  1528. TEST_ESP_OK(storage.writeItem(1, ItemType::BLOB, "key3", blob, sizeof(blob)));
  1529. }
  1530. TEST_CASE("nvs blob fragmentation test", "[nvs]")
  1531. {
  1532. SpiFlashEmulator emu(4);
  1533. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 4) );
  1534. const size_t BLOB_SIZE = 3500;
  1535. uint8_t *blob = (uint8_t*) malloc(BLOB_SIZE);
  1536. CHECK(blob != NULL);
  1537. memset(blob, 0xEE, BLOB_SIZE);
  1538. const uint32_t magic = 0xff33eaeb;
  1539. nvs_handle h;
  1540. TEST_ESP_OK( nvs_open("blob_tests", NVS_READWRITE, &h) );
  1541. for (int i = 0; i < 128; i++) {
  1542. INFO("Iteration " << i << "...\n");
  1543. TEST_ESP_OK( nvs_set_u32(h, "magic", magic) );
  1544. TEST_ESP_OK( nvs_set_blob(h, "blob", blob, BLOB_SIZE) );
  1545. char seq_buf[16];
  1546. sprintf(seq_buf, "seq%d", i);
  1547. TEST_ESP_OK( nvs_set_u32(h, seq_buf, i) );
  1548. }
  1549. free(blob);
  1550. }
  1551. TEST_CASE("nvs code handles errors properly when partition is near to full", "[nvs]")
  1552. {
  1553. const size_t blob_size = Page::CHUNK_MAX_SIZE * 0.3 ;
  1554. uint8_t blob[blob_size] = {0x11};
  1555. SpiFlashEmulator emu(5);
  1556. Storage storage;
  1557. char nvs_key[16] = "";
  1558. TEST_ESP_OK(storage.init(0, 5));
  1559. /* Four pages should fit roughly 12 blobs*/
  1560. for(uint8_t count = 1; count <= 12; count++) {
  1561. sprintf(nvs_key, "key:%u", count);
  1562. TEST_ESP_OK(storage.writeItem(1, ItemType::BLOB, nvs_key, blob, sizeof(blob)));
  1563. }
  1564. for(uint8_t count = 13; count <= 20; count++) {
  1565. sprintf(nvs_key, "key:%u", count);
  1566. TEST_ESP_ERR(storage.writeItem(1, ItemType::BLOB, nvs_key, blob, sizeof(blob)), ESP_ERR_NVS_NOT_ENOUGH_SPACE);
  1567. }
  1568. }
  1569. TEST_CASE("Check for nvs version incompatibility", "[nvs]")
  1570. {
  1571. SpiFlashEmulator emu(3);
  1572. int32_t val1 = 0x12345678;
  1573. Page p;
  1574. p.load(0);
  1575. TEST_ESP_OK(p.setVersion(Page::NVS_VERSION - 1));
  1576. TEST_ESP_OK(p.writeItem(1, ItemType::I32, "foo", &val1, sizeof(val1)));
  1577. TEST_ESP_ERR(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3), ESP_ERR_NVS_NEW_VERSION_FOUND);
  1578. }
  1579. TEST_CASE("Check that NVS supports old blob format without blob index", "[nvs]")
  1580. {
  1581. SpiFlashEmulator emu("../nvs_partition_generator/part_old_blob_format.bin");
  1582. nvs_handle handle;
  1583. TEST_ESP_OK( nvs_flash_init_custom("test", 0, 2) );
  1584. TEST_ESP_OK( nvs_open_from_partition("test", "dummyNamespace", NVS_READONLY, &handle));
  1585. char buf[64] = {0};
  1586. size_t buflen = 64;
  1587. uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef};
  1588. TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen));
  1589. CHECK(memcmp(buf, hexdata, buflen) == 0);
  1590. buflen = 64;
  1591. uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'};
  1592. TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
  1593. CHECK(memcmp(buf, base64data, buflen) == 0);
  1594. Page p;
  1595. p.load(0);
  1596. /* Check that item is stored in old format without blob index*/
  1597. TEST_ESP_OK(p.findItem(1, ItemType::BLOB, "dummyHex2BinKey"));
  1598. /* Modify the blob so that it is stored in the new format*/
  1599. hexdata[0] = hexdata[1] = hexdata[2] = 0x99;
  1600. TEST_ESP_OK(nvs_set_blob(handle, "dummyHex2BinKey", hexdata, sizeof(hexdata)));
  1601. Page p2;
  1602. p2.load(0);
  1603. /* Check the type of the blob. Expect type mismatch since the blob is stored in new format*/
  1604. TEST_ESP_ERR(p2.findItem(1, ItemType::BLOB, "dummyHex2BinKey"), ESP_ERR_NVS_TYPE_MISMATCH);
  1605. /* Check that index is present for the modified blob according to new format*/
  1606. TEST_ESP_OK(p2.findItem(1, ItemType::BLOB_IDX, "dummyHex2BinKey"));
  1607. /* Read the blob in new format and check the contents*/
  1608. buflen = 64;
  1609. TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
  1610. CHECK(memcmp(buf, base64data, buflen) == 0);
  1611. }
  1612. TEST_CASE("monkey test with old-format blob present", "[nvs][monkey]")
  1613. {
  1614. std::random_device rd;
  1615. std::mt19937 gen(rd());
  1616. uint32_t seed = 3;
  1617. gen.seed(seed);
  1618. SpiFlashEmulator emu(10);
  1619. emu.randomize(seed);
  1620. emu.clearStats();
  1621. const uint32_t NVS_FLASH_SECTOR = 2;
  1622. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 8;
  1623. static const size_t smallBlobLen = Page::CHUNK_MAX_SIZE / 3;
  1624. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  1625. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  1626. nvs_handle handle;
  1627. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1628. RandomTest test;
  1629. for ( uint8_t it = 0; it < 10; it++) {
  1630. size_t count = 200;
  1631. /* Erase index and chunks for the blob with "singlepage" key */
  1632. for (uint8_t num = NVS_FLASH_SECTOR; num < NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN; num++) {
  1633. Page p;
  1634. p.load(num);
  1635. p.eraseItem(1, ItemType::BLOB, "singlepage", Item::CHUNK_ANY, VerOffset::VER_ANY);
  1636. p.eraseItem(1, ItemType::BLOB_IDX, "singlepage", Item::CHUNK_ANY, VerOffset::VER_ANY);
  1637. p.eraseItem(1, ItemType::BLOB_DATA, "singlepage", Item::CHUNK_ANY, VerOffset::VER_ANY);
  1638. }
  1639. /* Now write "singlepage" blob in old format*/
  1640. for (uint8_t num = NVS_FLASH_SECTOR; num < NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN; num++) {
  1641. Page p;
  1642. p.load(num);
  1643. if (p.state() == Page::PageState::ACTIVE) {
  1644. uint8_t buf[smallBlobLen];
  1645. size_t blobLen = gen() % smallBlobLen;
  1646. if(blobLen > p.getVarDataTailroom()) {
  1647. blobLen = p.getVarDataTailroom();
  1648. }
  1649. std::generate_n(buf, blobLen, [&]() -> uint8_t {
  1650. return static_cast<uint8_t>(gen() % 256);
  1651. });
  1652. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB, "singlepage", buf, blobLen, Item::CHUNK_ANY));
  1653. TEST_ESP_OK(p.findItem(1, ItemType::BLOB, "singlepage"));
  1654. test.handleExternalWriteAtIndex(9, buf, blobLen); // This assumes "singlepage" is always at index 9
  1655. break;
  1656. }
  1657. }
  1658. /* Initialize again */
  1659. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
  1660. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1661. /* Perform random things */
  1662. auto res = test.doRandomThings(handle, gen, count);
  1663. if (res != ESP_OK) {
  1664. nvs_dump(NVS_DEFAULT_PART_NAME);
  1665. CHECK(0);
  1666. }
  1667. /* Check that only one version is present for "singlepage". Its possible that last iteration did not write
  1668. * anything for "singlepage". So either old version or new version should be present.*/
  1669. bool oldVerPresent = false, newVerPresent = false;
  1670. for (uint8_t num = NVS_FLASH_SECTOR; num < NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN; num++) {
  1671. Page p;
  1672. p.load(num);
  1673. if(!oldVerPresent && p.findItem(1, ItemType::BLOB, "singlepage", Item::CHUNK_ANY, VerOffset::VER_ANY) == ESP_OK) {
  1674. oldVerPresent = true;
  1675. }
  1676. if(!newVerPresent && p.findItem(1, ItemType::BLOB_IDX, "singlepage", Item::CHUNK_ANY, VerOffset::VER_ANY) == ESP_OK) {
  1677. newVerPresent = true;
  1678. }
  1679. }
  1680. CHECK(oldVerPresent != newVerPresent);
  1681. }
  1682. s_perf << "Monkey test: nErase=" << emu.getEraseOps() << " nWrite=" << emu.getWriteOps() << std::endl;
  1683. }
  1684. TEST_CASE("Recovery from power-off during modification of blob present in old-format (same page)", "[nvs]")
  1685. {
  1686. std::random_device rd;
  1687. std::mt19937 gen(rd());
  1688. uint32_t seed = 3;
  1689. gen.seed(seed);
  1690. SpiFlashEmulator emu(3);
  1691. emu.clearStats();
  1692. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1693. nvs_handle handle;
  1694. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1695. uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef};
  1696. uint8_t hexdata_old[] = {0x11, 0x12, 0x13, 0xbb, 0xcc, 0xee};
  1697. size_t buflen = sizeof(hexdata);
  1698. uint8_t buf[Page::CHUNK_MAX_SIZE];
  1699. /* Power-off when blob was being written on the same page where its old version in old format
  1700. * was present*/
  1701. Page p;
  1702. p.load(0);
  1703. /* Write blob in old-format*/
  1704. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB, "singlepage", hexdata_old, sizeof(hexdata_old)));
  1705. /* Write blob in new format*/
  1706. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB_DATA, "singlepage", hexdata, sizeof(hexdata), 0));
  1707. /* All pages are stored. Now store the index.*/
  1708. Item item;
  1709. item.blobIndex.dataSize = sizeof(hexdata);
  1710. item.blobIndex.chunkCount = 1;
  1711. item.blobIndex.chunkStart = VerOffset::VER_0_OFFSET;
  1712. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB_IDX, "singlepage", item.data, sizeof(item.data)));
  1713. TEST_ESP_OK(p.findItem(1, ItemType::BLOB, "singlepage"));
  1714. /* Initialize again */
  1715. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1716. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1717. TEST_ESP_OK( nvs_get_blob(handle, "singlepage", buf, &buflen));
  1718. CHECK(memcmp(buf, hexdata, buflen) == 0);
  1719. Page p2;
  1720. p2.load(0);
  1721. TEST_ESP_ERR(p2.findItem(1, ItemType::BLOB, "singlepage"), ESP_ERR_NVS_TYPE_MISMATCH);
  1722. }
  1723. TEST_CASE("Recovery from power-off during modification of blob present in old-format (different page)", "[nvs]")
  1724. {
  1725. std::random_device rd;
  1726. std::mt19937 gen(rd());
  1727. uint32_t seed = 3;
  1728. gen.seed(seed);
  1729. SpiFlashEmulator emu(3);
  1730. emu.clearStats();
  1731. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1732. nvs_handle handle;
  1733. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1734. uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef};
  1735. uint8_t hexdata_old[] = {0x11, 0x12, 0x13, 0xbb, 0xcc, 0xee};
  1736. size_t buflen = sizeof(hexdata);
  1737. uint8_t buf[Page::CHUNK_MAX_SIZE];
  1738. /* Power-off when blob was being written on the different page where its old version in old format
  1739. * was present*/
  1740. Page p;
  1741. p.load(0);
  1742. /* Write blob in old-format*/
  1743. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB, "singlepage", hexdata_old, sizeof(hexdata_old)));
  1744. /* Write blob in new format*/
  1745. TEST_ESP_OK(p.writeItem(1, ItemType::BLOB_DATA, "singlepage", hexdata, sizeof(hexdata), 0));
  1746. /* All pages are stored. Now store the index.*/
  1747. Item item;
  1748. item.blobIndex.dataSize = sizeof(hexdata);
  1749. item.blobIndex.chunkCount = 1;
  1750. item.blobIndex.chunkStart = VerOffset::VER_0_OFFSET;
  1751. p.markFull();
  1752. Page p2;
  1753. p2.load(1);
  1754. p2.setSeqNumber(1);
  1755. TEST_ESP_OK(p2.writeItem(1, ItemType::BLOB_IDX, "singlepage", item.data, sizeof(item.data)));
  1756. TEST_ESP_OK(p.findItem(1, ItemType::BLOB, "singlepage"));
  1757. /* Initialize again */
  1758. TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 3));
  1759. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle));
  1760. TEST_ESP_OK( nvs_get_blob(handle, "singlepage", buf, &buflen));
  1761. CHECK(memcmp(buf, hexdata, buflen) == 0);
  1762. Page p3;
  1763. p3.load(0);
  1764. TEST_ESP_ERR(p3.findItem(1, ItemType::BLOB, "singlepage"), ESP_ERR_NVS_NOT_FOUND);
  1765. }
  1766. static void check_nvs_part_gen_args(char const *part_name, int size, char const *filename, bool is_encr, nvs_sec_cfg_t* xts_cfg)
  1767. {
  1768. nvs_handle handle;
  1769. if (is_encr)
  1770. TEST_ESP_OK(nvs_flash_secure_init_custom(part_name, 0, size, xts_cfg));
  1771. else
  1772. TEST_ESP_OK( nvs_flash_init_custom(part_name, 0, size) );
  1773. TEST_ESP_OK( nvs_open_from_partition(part_name, "dummyNamespace", NVS_READONLY, &handle));
  1774. uint8_t u8v;
  1775. TEST_ESP_OK( nvs_get_u8(handle, "dummyU8Key", &u8v));
  1776. CHECK(u8v == 127);
  1777. int8_t i8v;
  1778. TEST_ESP_OK( nvs_get_i8(handle, "dummyI8Key", &i8v));
  1779. CHECK(i8v == -128);
  1780. uint16_t u16v;
  1781. TEST_ESP_OK( nvs_get_u16(handle, "dummyU16Key", &u16v));
  1782. CHECK(u16v == 32768);
  1783. uint32_t u32v;
  1784. TEST_ESP_OK( nvs_get_u32(handle, "dummyU32Key", &u32v));
  1785. CHECK(u32v == 4294967295);
  1786. int32_t i32v;
  1787. TEST_ESP_OK( nvs_get_i32(handle, "dummyI32Key", &i32v));
  1788. CHECK(i32v == -2147483648);
  1789. char buf[64] = {0};
  1790. size_t buflen = 64;
  1791. TEST_ESP_OK( nvs_get_str(handle, "dummyStringKey", buf, &buflen));
  1792. CHECK(strncmp(buf, "0A:0B:0C:0D:0E:0F", buflen) == 0);
  1793. uint8_t hexdata[] = {0x01, 0x02, 0x03, 0xab, 0xcd, 0xef};
  1794. buflen = 64;
  1795. int j;
  1796. TEST_ESP_OK( nvs_get_blob(handle, "dummyHex2BinKey", buf, &buflen));
  1797. CHECK(memcmp(buf, hexdata, buflen) == 0);
  1798. uint8_t base64data[] = {'1', '2', '3', 'a', 'b', 'c'};
  1799. TEST_ESP_OK( nvs_get_blob(handle, "dummyBase64Key", buf, &buflen));
  1800. CHECK(memcmp(buf, base64data, buflen) == 0);
  1801. buflen = 64;
  1802. uint8_t hexfiledata[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
  1803. TEST_ESP_OK( nvs_get_blob(handle, "hexFileKey", buf, &buflen));
  1804. CHECK(memcmp(buf, hexfiledata, buflen) == 0);
  1805. buflen = 64;
  1806. uint8_t strfiledata[64] = "abcdefghijklmnopqrstuvwxyz\0";
  1807. TEST_ESP_OK( nvs_get_str(handle, "stringFileKey", buf, &buflen));
  1808. CHECK(memcmp(buf, strfiledata, buflen) == 0);
  1809. char bin_data[5200];
  1810. size_t bin_len = sizeof(bin_data);
  1811. char binfiledata[5200];
  1812. ifstream file;
  1813. file.open(filename);
  1814. file.read(binfiledata,5200);
  1815. TEST_ESP_OK( nvs_get_blob(handle, "binFileKey", bin_data, &bin_len));
  1816. CHECK(memcmp(bin_data, binfiledata, bin_len) == 0);
  1817. file.close();
  1818. nvs_close(handle);
  1819. }
  1820. TEST_CASE("check and read data from partition generated via partition generation utility with multipage blob support disabled", "[nvs_part_gen]")
  1821. {
  1822. int status;
  1823. int childpid = fork();
  1824. if (childpid == 0) {
  1825. exit(execlp("cp", " cp",
  1826. "-rf",
  1827. "../nvs_partition_generator/testdata",
  1828. ".",NULL));
  1829. } else {
  1830. CHECK(childpid > 0);
  1831. waitpid(childpid, &status, 0);
  1832. CHECK(WEXITSTATUS(status) != -1);
  1833. childpid = fork();
  1834. if (childpid == 0) {
  1835. exit(execlp("python", "python",
  1836. "../nvs_partition_generator/nvs_partition_gen.py",
  1837. "--input",
  1838. "../nvs_partition_generator/sample_singlepage_blob.csv",
  1839. "--output",
  1840. "../nvs_partition_generator/partition_single_page.bin",
  1841. "--size",
  1842. "0x3000",
  1843. "--version",
  1844. "v1",NULL));
  1845. } else {
  1846. CHECK(childpid > 0);
  1847. int status;
  1848. waitpid(childpid, &status, 0);
  1849. CHECK(WEXITSTATUS(status) != -1);
  1850. }
  1851. }
  1852. SpiFlashEmulator emu("../nvs_partition_generator/partition_single_page.bin");
  1853. TEST_ESP_OK(nvs_flash_deinit());
  1854. check_nvs_part_gen_args("test", 3, "../nvs_partition_generator/testdata/sample_singlepage_blob.bin", false, NULL);
  1855. childpid = fork();
  1856. if (childpid == 0) {
  1857. exit(execlp("rm", " rm",
  1858. "-rf",
  1859. "testdata",NULL));
  1860. } else {
  1861. CHECK(childpid > 0);
  1862. waitpid(childpid, &status, 0);
  1863. CHECK(WEXITSTATUS(status) != -1);
  1864. }
  1865. }
  1866. TEST_CASE("check and read data from partition generated via partition generation utility with multipage blob support enabled", "[nvs_part_gen]")
  1867. {
  1868. int status;
  1869. int childpid = fork();
  1870. if (childpid == 0) {
  1871. exit(execlp("cp", " cp",
  1872. "-rf",
  1873. "../nvs_partition_generator/testdata",
  1874. ".",NULL));
  1875. } else {
  1876. CHECK(childpid > 0);
  1877. waitpid(childpid, &status, 0);
  1878. CHECK(WEXITSTATUS(status) != -1);
  1879. childpid = fork();
  1880. if (childpid == 0) {
  1881. exit(execlp("python", "python",
  1882. "../nvs_partition_generator/nvs_partition_gen.py",
  1883. "--input",
  1884. "../nvs_partition_generator/sample_multipage_blob.csv",
  1885. "--output",
  1886. "../nvs_partition_generator/partition_multipage_blob.bin",
  1887. "--size",
  1888. "0x4000",
  1889. "--version",
  1890. "v2",NULL));
  1891. } else {
  1892. CHECK(childpid > 0);
  1893. waitpid(childpid, &status, 0);
  1894. CHECK(WEXITSTATUS(status) != -1);
  1895. }
  1896. }
  1897. SpiFlashEmulator emu("../nvs_partition_generator/partition_multipage_blob.bin");
  1898. check_nvs_part_gen_args("test", 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin",false,NULL);
  1899. childpid = fork();
  1900. if (childpid == 0) {
  1901. exit(execlp("rm", " rm",
  1902. "-rf",
  1903. "testdata",NULL));
  1904. } else {
  1905. CHECK(childpid > 0);
  1906. waitpid(childpid, &status, 0);
  1907. CHECK(WEXITSTATUS(status) != -1);
  1908. }
  1909. }
  1910. TEST_CASE("check and read data from partition generated via manufacturing utility with multipage blob support disabled", "[mfg_gen]")
  1911. {
  1912. int childpid = fork();
  1913. int status;
  1914. if (childpid == 0) {
  1915. exit(execlp("bash", "bash",
  1916. "-c",
  1917. "rm -rf ../../../tools/mass_mfg/host_test | \
  1918. cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \
  1919. cp -rf ../nvs_partition_generator/testdata . | \
  1920. mkdir -p ../../../tools/mass_mfg/host_test",NULL));
  1921. } else {
  1922. CHECK(childpid > 0);
  1923. waitpid(childpid, &status, 0);
  1924. CHECK(WEXITSTATUS(status) != -1);
  1925. childpid = fork();
  1926. if (childpid == 0) {
  1927. exit(execlp("python", "python",
  1928. "../../../tools/mass_mfg/mfg_gen.py",
  1929. "--conf",
  1930. "../../../tools/mass_mfg/samples/sample_config.csv",
  1931. "--values",
  1932. "../../../tools/mass_mfg/samples/sample_values_singlepage_blob.csv",
  1933. "--prefix",
  1934. "Test",
  1935. "--size",
  1936. "0x3000",
  1937. "--outdir",
  1938. "../../../tools/mass_mfg/host_test",
  1939. "--version",
  1940. "v1",NULL));
  1941. } else {
  1942. CHECK(childpid > 0);
  1943. waitpid(childpid, &status, 0);
  1944. CHECK(WEXITSTATUS(status) != -1);
  1945. childpid = fork();
  1946. if (childpid == 0) {
  1947. exit(execlp("python", "python",
  1948. "../nvs_partition_generator/nvs_partition_gen.py",
  1949. "--input",
  1950. "../../../tools/mass_mfg/host_test/csv/Test-1.csv",
  1951. "--output",
  1952. "../nvs_partition_generator/Test-1-partition.bin",
  1953. "--size",
  1954. "0x3000",
  1955. "--version",
  1956. "v1",NULL));
  1957. } else {
  1958. CHECK(childpid > 0);
  1959. waitpid(childpid, &status, 0);
  1960. CHECK(WEXITSTATUS(status) != -1);
  1961. }
  1962. }
  1963. }
  1964. SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin");
  1965. check_nvs_part_gen_args("test", 3, "mfg_testdata/sample_singlepage_blob.bin", false, NULL);
  1966. SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition.bin");
  1967. check_nvs_part_gen_args("test", 3, "testdata/sample_singlepage_blob.bin", false, NULL);
  1968. childpid = fork();
  1969. if (childpid == 0) {
  1970. exit(execlp("bash", " bash",
  1971. "-c",
  1972. "rm -rf ../../../tools/mass_mfg/host_test | \
  1973. rm -rf mfg_testdata | \
  1974. rm -rf testdata",NULL));
  1975. } else {
  1976. CHECK(childpid > 0);
  1977. waitpid(childpid, &status, 0);
  1978. CHECK(WEXITSTATUS(status) != -1);
  1979. }
  1980. }
  1981. TEST_CASE("check and read data from partition generated via manufacturing utility with multipage blob support enabled", "[mfg_gen]")
  1982. {
  1983. int childpid = fork();
  1984. int status;
  1985. if (childpid == 0) {
  1986. exit(execlp("bash", " bash",
  1987. "-c",
  1988. "rm -rf ../../../tools/mass_mfg/host_test | \
  1989. cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \
  1990. cp -rf ../nvs_partition_generator/testdata . | \
  1991. mkdir -p ../../../tools/mass_mfg/host_test",NULL));
  1992. } else {
  1993. CHECK(childpid > 0);
  1994. waitpid(childpid, &status, 0);
  1995. CHECK(WEXITSTATUS(status) != -1);
  1996. childpid = fork();
  1997. if (childpid == 0) {
  1998. exit(execlp("python", "python",
  1999. "../../../tools/mass_mfg/mfg_gen.py",
  2000. "--conf",
  2001. "../../../tools/mass_mfg/samples/sample_config.csv",
  2002. "--values",
  2003. "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv",
  2004. "--prefix",
  2005. "Test",
  2006. "--size",
  2007. "0x4000",
  2008. "--outdir",
  2009. "../../../tools/mass_mfg/host_test",
  2010. "--version",
  2011. "v2",NULL));
  2012. } else {
  2013. CHECK(childpid > 0);
  2014. waitpid(childpid, &status, 0);
  2015. CHECK(WEXITSTATUS(status) != -1);
  2016. childpid = fork();
  2017. if (childpid == 0) {
  2018. exit(execlp("python", "python",
  2019. "../nvs_partition_generator/nvs_partition_gen.py",
  2020. "--input",
  2021. "../../../tools/mass_mfg/host_test/csv/Test-1.csv",
  2022. "--output",
  2023. "../nvs_partition_generator/Test-1-partition.bin",
  2024. "--size",
  2025. "0x4000",
  2026. "--version",
  2027. "v2",NULL));
  2028. } else {
  2029. CHECK(childpid > 0);
  2030. waitpid(childpid, &status, 0);
  2031. CHECK(WEXITSTATUS(status) != -1);
  2032. }
  2033. }
  2034. }
  2035. SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin");
  2036. check_nvs_part_gen_args("test", 4, "mfg_testdata/sample_multipage_blob.bin", false, NULL);
  2037. SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition.bin");
  2038. check_nvs_part_gen_args("test", 4, "testdata/sample_multipage_blob.bin", false, NULL);
  2039. childpid = fork();
  2040. if (childpid == 0) {
  2041. exit(execlp("bash", " bash",
  2042. "-c",
  2043. "rm -rf ../../../tools/mass_mfg/host_test | \
  2044. rm -rf mfg_testdata | \
  2045. rm -rf testdata",NULL));
  2046. } else {
  2047. CHECK(childpid > 0);
  2048. waitpid(childpid, &status, 0);
  2049. CHECK(WEXITSTATUS(status) != -1);
  2050. }
  2051. }
  2052. #if CONFIG_NVS_ENCRYPTION
  2053. TEST_CASE("check underlying xts code for 32-byte size sector encryption", "[nvs]")
  2054. {
  2055. auto toHex = [](char ch) {
  2056. if(ch >= '0' && ch <= '9')
  2057. return ch - '0';
  2058. else if(ch >= 'a' && ch <= 'f')
  2059. return ch - 'a' + 10;
  2060. else if(ch >= 'A' && ch <= 'F')
  2061. return ch - 'A' + 10;
  2062. else
  2063. return 0;
  2064. };
  2065. auto toHexByte = [toHex](char* c) {
  2066. return 16 * toHex(c[0]) + toHex(c[1]);
  2067. };
  2068. auto toHexStream = [toHexByte](char* src, uint8_t* dest) {
  2069. uint32_t cnt =0;
  2070. char* p = src;
  2071. while(*p != '\0' && *(p + 1) != '\0')
  2072. {
  2073. dest[cnt++] = toHexByte(p); p += 2;
  2074. }
  2075. };
  2076. uint8_t eky_hex[2 * NVS_KEY_SIZE];
  2077. uint8_t ptxt_hex[Page::ENTRY_SIZE], ctxt_hex[Page::ENTRY_SIZE], ba_hex[16];
  2078. mbedtls_aes_xts_context ectx[1];
  2079. mbedtls_aes_xts_context dctx[1];
  2080. char eky[][2 * NVS_KEY_SIZE + 1] = {
  2081. "0000000000000000000000000000000000000000000000000000000000000000",
  2082. "1111111111111111111111111111111111111111111111111111111111111111"
  2083. };
  2084. char tky[][2 * NVS_KEY_SIZE + 1] = {
  2085. "0000000000000000000000000000000000000000000000000000000000000000",
  2086. "2222222222222222222222222222222222222222222222222222222222222222"
  2087. };
  2088. char blk_addr[][2*16 + 1] = {
  2089. "00000000000000000000000000000000",
  2090. "33333333330000000000000000000000"
  2091. };
  2092. char ptxt[][2 * Page::ENTRY_SIZE + 1] = {
  2093. "0000000000000000000000000000000000000000000000000000000000000000",
  2094. "4444444444444444444444444444444444444444444444444444444444444444"
  2095. };
  2096. char ctxt[][2 * Page::ENTRY_SIZE + 1] = {
  2097. "d456b4fc2e620bba6ffbed27b956c9543454dd49ebd8d8ee6f94b65cbe158f73",
  2098. "e622334f184bbce129a25b2ac76b3d92abf98e22df5bdd15af471f3db8946a85"
  2099. };
  2100. mbedtls_aes_xts_init(ectx);
  2101. mbedtls_aes_xts_init(dctx);
  2102. for(uint8_t cnt = 0; cnt < sizeof(eky)/sizeof(eky[0]); cnt++) {
  2103. toHexStream(eky[cnt], eky_hex);
  2104. toHexStream(tky[cnt], &eky_hex[NVS_KEY_SIZE]);
  2105. toHexStream(ptxt[cnt], ptxt_hex);
  2106. toHexStream(ctxt[cnt], ctxt_hex);
  2107. toHexStream(blk_addr[cnt], ba_hex);
  2108. CHECK(!mbedtls_aes_xts_setkey_enc(ectx, eky_hex, 2 * NVS_KEY_SIZE * 8));
  2109. CHECK(!mbedtls_aes_xts_setkey_enc(dctx, eky_hex, 2 * NVS_KEY_SIZE * 8));
  2110. CHECK(!mbedtls_aes_crypt_xts(ectx, MBEDTLS_AES_ENCRYPT, Page::ENTRY_SIZE, ba_hex, ptxt_hex, ptxt_hex));
  2111. CHECK(!memcmp(ptxt_hex, ctxt_hex, Page::ENTRY_SIZE));
  2112. }
  2113. }
  2114. TEST_CASE("test nvs apis with encryption enabled", "[nvs]")
  2115. {
  2116. SpiFlashEmulator emu(10);
  2117. emu.randomize(100);
  2118. nvs_handle handle_1;
  2119. const uint32_t NVS_FLASH_SECTOR = 6;
  2120. const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
  2121. emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
  2122. nvs_sec_cfg_t xts_cfg;
  2123. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2124. xts_cfg.eky[count] = 0x11;
  2125. xts_cfg.tky[count] = 0x22;
  2126. }
  2127. for (uint16_t i = NVS_FLASH_SECTOR; i <NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN; ++i) {
  2128. spi_flash_erase_sector(i);
  2129. }
  2130. TEST_ESP_OK(nvs_flash_secure_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN, &xts_cfg));
  2131. TEST_ESP_ERR(nvs_open("namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND);
  2132. TEST_ESP_OK(nvs_open("namespace1", NVS_READWRITE, &handle_1));
  2133. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x12345678));
  2134. TEST_ESP_OK(nvs_set_i32(handle_1, "foo", 0x23456789));
  2135. nvs_handle handle_2;
  2136. TEST_ESP_OK(nvs_open("namespace2", NVS_READWRITE, &handle_2));
  2137. TEST_ESP_OK(nvs_set_i32(handle_2, "foo", 0x3456789a));
  2138. const char* str = "value 0123456789abcdef0123456789abcdef";
  2139. TEST_ESP_OK(nvs_set_str(handle_2, "key", str));
  2140. int32_t v1;
  2141. TEST_ESP_OK(nvs_get_i32(handle_1, "foo", &v1));
  2142. CHECK(0x23456789 == v1);
  2143. int32_t v2;
  2144. TEST_ESP_OK(nvs_get_i32(handle_2, "foo", &v2));
  2145. CHECK(0x3456789a == v2);
  2146. char buf[strlen(str) + 1];
  2147. size_t buf_len = sizeof(buf);
  2148. size_t buf_len_needed;
  2149. TEST_ESP_OK(nvs_get_str(handle_2, "key", NULL, &buf_len_needed));
  2150. CHECK(buf_len_needed == buf_len);
  2151. size_t buf_len_short = buf_len - 1;
  2152. TEST_ESP_ERR(ESP_ERR_NVS_INVALID_LENGTH, nvs_get_str(handle_2, "key", buf, &buf_len_short));
  2153. CHECK(buf_len_short == buf_len);
  2154. size_t buf_len_long = buf_len + 1;
  2155. TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len_long));
  2156. CHECK(buf_len_long == buf_len);
  2157. TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len));
  2158. CHECK(0 == strcmp(buf, str));
  2159. nvs_close(handle_1);
  2160. nvs_close(handle_2);
  2161. TEST_ESP_OK(nvs_flash_deinit());
  2162. }
  2163. TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled", "[nvs_part_gen]")
  2164. {
  2165. int status;
  2166. int childpid = fork();
  2167. if (childpid == 0) {
  2168. exit(execlp("cp", " cp",
  2169. "-rf",
  2170. "../nvs_partition_generator/testdata",
  2171. ".",NULL));
  2172. } else {
  2173. CHECK(childpid > 0);
  2174. waitpid(childpid, &status, 0);
  2175. CHECK(WEXITSTATUS(status) != -1);
  2176. childpid = fork();
  2177. if (childpid == 0) {
  2178. exit(execlp("python", "python",
  2179. "../nvs_partition_generator/nvs_partition_gen.py",
  2180. "--input",
  2181. "../nvs_partition_generator/sample_multipage_blob.csv",
  2182. "--output",
  2183. "../nvs_partition_generator/partition_encrypted.bin",
  2184. "--size",
  2185. "0x4000",
  2186. "--encrypt",
  2187. "True",
  2188. "--keyfile",
  2189. "../nvs_partition_generator/testdata/sample_encryption_keys.bin",NULL));
  2190. } else {
  2191. CHECK(childpid > 0);
  2192. waitpid(childpid, &status, 0);
  2193. CHECK(WEXITSTATUS(status) != -1);
  2194. }
  2195. }
  2196. SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted.bin");
  2197. nvs_sec_cfg_t cfg;
  2198. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2199. cfg.eky[count] = 0x11;
  2200. cfg.tky[count] = 0x22;
  2201. }
  2202. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg);
  2203. childpid = fork();
  2204. if (childpid == 0) {
  2205. exit(execlp("rm", " rm",
  2206. "-rf",
  2207. "testdata",NULL));
  2208. } else {
  2209. CHECK(childpid > 0);
  2210. waitpid(childpid, &status, 0);
  2211. CHECK(WEXITSTATUS(status) != -1);
  2212. }
  2213. }
  2214. TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using keygen", "[nvs_part_gen]")
  2215. {
  2216. int childpid = fork();
  2217. int status;
  2218. if (childpid == 0) {
  2219. exit(execlp("cp", " cp",
  2220. "-rf",
  2221. "../nvs_partition_generator/testdata",
  2222. ".",NULL));
  2223. } else {
  2224. CHECK(childpid > 0);
  2225. waitpid(childpid, &status, 0);
  2226. CHECK(WEXITSTATUS(status) != -1);
  2227. childpid = fork();
  2228. if (childpid == 0) {
  2229. exit(execlp("python", "python",
  2230. "../nvs_partition_generator/nvs_partition_gen.py",
  2231. "--input",
  2232. "../nvs_partition_generator/sample_multipage_blob.csv",
  2233. "--output",
  2234. "../nvs_partition_generator/partition_encrypted_using_keygen.bin",
  2235. "--size",
  2236. "0x4000",
  2237. "--encrypt",
  2238. "True",
  2239. "--keygen",
  2240. "true",NULL));
  2241. } else {
  2242. CHECK(childpid > 0);
  2243. waitpid(childpid, &status, 0);
  2244. CHECK(WEXITSTATUS(status) != -1);
  2245. }
  2246. }
  2247. DIR *dir;
  2248. struct dirent *file;
  2249. char *filename;
  2250. char *files;
  2251. char *file_ext;
  2252. dir = opendir("keys");
  2253. while ((file = readdir(dir)) != NULL)
  2254. {
  2255. filename = file->d_name;
  2256. files = strrchr(filename, '.');
  2257. if (files != NULL)
  2258. {
  2259. file_ext = files+1;
  2260. if (strncmp(file_ext,"bin",3) == 0)
  2261. {
  2262. break;
  2263. }
  2264. }
  2265. }
  2266. std::string encr_file = std::string("keys/") + std::string(filename);
  2267. SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keygen.bin");
  2268. char buffer[64];
  2269. FILE *fp;
  2270. fp = fopen(encr_file.c_str(),"rb");
  2271. fread(buffer,sizeof(buffer),1,fp);
  2272. fclose(fp);
  2273. TEST_ESP_OK(nvs_flash_deinit());
  2274. nvs_sec_cfg_t cfg;
  2275. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2276. cfg.eky[count] = buffer[count] & 255;
  2277. cfg.tky[count] = buffer[count+32] & 255;
  2278. }
  2279. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg);
  2280. }
  2281. TEST_CASE("test nvs apis for nvs partition generator utility with encryption enabled using keyfile", "[nvs_part_gen]")
  2282. {
  2283. int childpid = fork();
  2284. int status;
  2285. DIR *dir;
  2286. struct dirent *file;
  2287. char *filename;
  2288. char *files;
  2289. char *file_ext;
  2290. dir = opendir("keys");
  2291. while ((file = readdir(dir)) != NULL)
  2292. {
  2293. filename = file->d_name;
  2294. files = strrchr(filename, '.');
  2295. if (files != NULL)
  2296. {
  2297. file_ext = files+1;
  2298. if (strncmp(file_ext,"bin",3) == 0)
  2299. {
  2300. break;
  2301. }
  2302. }
  2303. }
  2304. std::string encr_file = std::string("keys/") + std::string(filename);
  2305. if (childpid == 0) {
  2306. exit(execlp("python", "python",
  2307. "../nvs_partition_generator/nvs_partition_gen.py",
  2308. "--input",
  2309. "../nvs_partition_generator/sample_multipage_blob.csv",
  2310. "--output",
  2311. "../nvs_partition_generator/partition_encrypted_using_keyfile.bin",
  2312. "--size",
  2313. "0x4000",
  2314. "--encrypt",
  2315. "True",
  2316. "--keyfile",
  2317. encr_file.c_str(),NULL));
  2318. } else {
  2319. CHECK(childpid > 0);
  2320. waitpid(childpid, &status, 0);
  2321. CHECK(WEXITSTATUS(status) != -1);
  2322. }
  2323. SpiFlashEmulator emu("../nvs_partition_generator/partition_encrypted_using_keyfile.bin");
  2324. char buffer[64];
  2325. FILE *fp;
  2326. fp = fopen(encr_file.c_str(),"rb");
  2327. fread(buffer,sizeof(buffer),1,fp);
  2328. fclose(fp);
  2329. TEST_ESP_OK(nvs_flash_deinit());
  2330. nvs_sec_cfg_t cfg;
  2331. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2332. cfg.eky[count] = buffer[count] & 255;
  2333. cfg.tky[count] = buffer[count+32] & 255;
  2334. }
  2335. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "../nvs_partition_generator/testdata/sample_multipage_blob.bin", true, &cfg);
  2336. childpid = fork();
  2337. if (childpid == 0) {
  2338. exit(execlp("rm", " rm",
  2339. "-rf",
  2340. "keys",NULL));
  2341. } else {
  2342. CHECK(childpid > 0);
  2343. waitpid(childpid, &status, 0);
  2344. CHECK(WEXITSTATUS(status) != -1);
  2345. childpid = fork();
  2346. if (childpid == 0) {
  2347. exit(execlp("rm", " rm",
  2348. "-rf",
  2349. "testdata",NULL));
  2350. } else {
  2351. CHECK(childpid > 0);
  2352. waitpid(childpid, &status, 0);
  2353. CHECK(WEXITSTATUS(status) != -1);
  2354. }
  2355. }
  2356. }
  2357. TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using sample keyfile", "[mfg_gen]")
  2358. {
  2359. int childpid = fork();
  2360. int status;
  2361. if (childpid == 0) {
  2362. exit(execlp("bash", " bash",
  2363. "-c",
  2364. "rm -rf ../../../tools/mass_mfg/host_test | \
  2365. cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \
  2366. cp -rf ../nvs_partition_generator/testdata . | \
  2367. mkdir -p ../../../tools/mass_mfg/host_test",NULL));
  2368. } else {
  2369. CHECK(childpid > 0);
  2370. waitpid(childpid, &status, 0);
  2371. CHECK(WEXITSTATUS(status) != -1);
  2372. childpid = fork();
  2373. if (childpid == 0) {
  2374. exit(execlp("python", "python",
  2375. "../../../tools/mass_mfg/mfg_gen.py",
  2376. "--conf",
  2377. "../../../tools/mass_mfg/samples/sample_config.csv",
  2378. "--values",
  2379. "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv",
  2380. "--prefix",
  2381. "Test",
  2382. "--size",
  2383. "0x4000",
  2384. "--outdir",
  2385. "../../../tools/mass_mfg/host_test",
  2386. "--version",
  2387. "v2",
  2388. "--encrypt",
  2389. "true",
  2390. "--keyfile",
  2391. "mfg_testdata/sample_encryption_keys.bin",NULL));
  2392. } else {
  2393. CHECK(childpid > 0);
  2394. waitpid(childpid, &status, 0);
  2395. CHECK(WEXITSTATUS(status) != -1);
  2396. childpid = fork();
  2397. if (childpid == 0) {
  2398. exit(execlp("python", "python",
  2399. "../nvs_partition_generator/nvs_partition_gen.py",
  2400. "--input",
  2401. "../../../tools/mass_mfg/host_test/csv/Test-1.csv",
  2402. "--output",
  2403. "../nvs_partition_generator/Test-1-partition-encrypted.bin",
  2404. "--size",
  2405. "0x4000",
  2406. "--version",
  2407. "v2",
  2408. "--encrypt",
  2409. "true",
  2410. "--keyfile",
  2411. "testdata/sample_encryption_keys.bin",NULL));
  2412. } else {
  2413. CHECK(childpid > 0);
  2414. waitpid(childpid, &status, 0);
  2415. CHECK(WEXITSTATUS(status) != -1);
  2416. }
  2417. }
  2418. }
  2419. SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin");
  2420. TEST_ESP_OK(nvs_flash_deinit());
  2421. nvs_sec_cfg_t cfg;
  2422. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2423. cfg.eky[count] = 0x11;
  2424. cfg.tky[count] = 0x22;
  2425. }
  2426. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg);
  2427. SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted.bin");
  2428. TEST_ESP_OK(nvs_flash_deinit());
  2429. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg);
  2430. childpid = fork();
  2431. if (childpid == 0) {
  2432. exit(execlp("bash", " bash",
  2433. "-c",
  2434. "rm -rf ../../../tools/mass_mfg/host_test | \
  2435. rm -rf mfg_testdata | \
  2436. rm -rf testdata",NULL));
  2437. } else {
  2438. CHECK(childpid > 0);
  2439. waitpid(childpid, &status, 0);
  2440. CHECK(WEXITSTATUS(status) != -1);
  2441. }
  2442. }
  2443. TEST_CASE("check and read data from partition generated via manufacturing utility with encryption enabled using new generated key", "[mfg_gen]")
  2444. {
  2445. int childpid = fork();
  2446. int status;
  2447. if (childpid == 0) {
  2448. exit(execlp("bash", " bash",
  2449. "-c",
  2450. "rm -rf ../../../tools/mass_mfg/host_test | \
  2451. cp -rf ../../../tools/mass_mfg/testdata mfg_testdata | \
  2452. cp -rf ../nvs_partition_generator/testdata . | \
  2453. mkdir -p ../../../tools/mass_mfg/host_test",NULL));
  2454. } else {
  2455. CHECK(childpid > 0);
  2456. waitpid(childpid, &status, 0);
  2457. CHECK(WEXITSTATUS(status) != -1);
  2458. childpid = fork();
  2459. if (childpid == 0) {
  2460. exit(execlp("python", "python",
  2461. "../../../tools/mass_mfg/mfg_gen.py",
  2462. "--keygen",
  2463. "true",
  2464. "--outdir",
  2465. "../../../tools/mass_mfg/host_test",
  2466. "--keyfile",
  2467. "encr_keys_host_test.bin",NULL));
  2468. } else {
  2469. CHECK(childpid > 0);
  2470. waitpid(childpid, &status, 0);
  2471. CHECK(WEXITSTATUS(status) != -1);
  2472. childpid = fork();
  2473. if (childpid == 0) {
  2474. exit(execlp("python", "python",
  2475. "../../../tools/mass_mfg/mfg_gen.py",
  2476. "--conf",
  2477. "../../../tools/mass_mfg/samples/sample_config.csv",
  2478. "--values",
  2479. "../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv",
  2480. "--prefix",
  2481. "Test",
  2482. "--size",
  2483. "0x4000",
  2484. "--outdir",
  2485. "../../../tools/mass_mfg/host_test",
  2486. "--version",
  2487. "v2",
  2488. "--encrypt",
  2489. "true",
  2490. "--keyfile",
  2491. "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin",NULL));
  2492. } else {
  2493. CHECK(childpid > 0);
  2494. waitpid(childpid, &status, 0);
  2495. CHECK(WEXITSTATUS(status) != -1);
  2496. childpid = fork();
  2497. if (childpid == 0) {
  2498. exit(execlp("python", "python",
  2499. "../nvs_partition_generator/nvs_partition_gen.py",
  2500. "--input",
  2501. "../../../tools/mass_mfg/host_test/csv/Test-1.csv",
  2502. "--output",
  2503. "../nvs_partition_generator/Test-1-partition-encrypted.bin",
  2504. "--size",
  2505. "0x4000",
  2506. "--version",
  2507. "v2",
  2508. "--encrypt",
  2509. "true",
  2510. "--keyfile",
  2511. "../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin",NULL));
  2512. } else {
  2513. CHECK(childpid > 0);
  2514. waitpid(childpid, &status, 0);
  2515. CHECK(WEXITSTATUS(status) != -1);
  2516. }
  2517. }
  2518. }
  2519. }
  2520. SpiFlashEmulator emu1("../../../tools/mass_mfg/host_test/bin/Test-1.bin");
  2521. char buffer[64];
  2522. FILE *fp;
  2523. fp = fopen("../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin","rb");
  2524. fread(buffer,sizeof(buffer),1,fp);
  2525. fclose(fp);
  2526. TEST_ESP_OK(nvs_flash_deinit());
  2527. nvs_sec_cfg_t cfg;
  2528. for(int count = 0; count < NVS_KEY_SIZE; count++) {
  2529. cfg.eky[count] = buffer[count] & 255;
  2530. cfg.tky[count] = buffer[count+32] & 255;
  2531. }
  2532. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "mfg_testdata/sample_multipage_blob.bin", true, &cfg);
  2533. SpiFlashEmulator emu2("../nvs_partition_generator/Test-1-partition-encrypted.bin");
  2534. TEST_ESP_OK(nvs_flash_deinit());
  2535. check_nvs_part_gen_args(NVS_DEFAULT_PART_NAME, 4, "testdata/sample_multipage_blob.bin", true, &cfg);
  2536. childpid = fork();
  2537. if (childpid == 0) {
  2538. exit(execlp("bash", " bash",
  2539. "-c",
  2540. "rm -rf keys | \
  2541. rm -rf mfg_testdata | \
  2542. rm -rf testdata | \
  2543. rm -rf ../../../tools/mass_mfg/host_test",NULL));
  2544. } else {
  2545. CHECK(childpid > 0);
  2546. waitpid(childpid, &status, 0);
  2547. CHECK(WEXITSTATUS(status) != -1);
  2548. }
  2549. }
  2550. #endif
  2551. /* Add new tests above */
  2552. /* This test has to be the final one */
  2553. TEST_CASE("dump all performance data", "[nvs]")
  2554. {
  2555. std::cout << "====================" << std::endl << "Dumping benchmarks" << std::endl;
  2556. std::cout << s_perf.str() << std::endl;
  2557. std::cout << "====================" << std::endl;
  2558. }