test_nvs.cpp 120 KB

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