test_nvs.cpp 119 KB

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