test_nvs.cpp 105 KB

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