test_nvs.cpp 121 KB

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