test_nvs.cpp 125 KB

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