test_nvs.cpp 118 KB

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