filter.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <sstream>
  7. #include "Allocators.hpp"
  8. #include "Literals.hpp"
  9. using namespace ArduinoJson::detail;
  10. TEST_CASE("deserializeMsgPack() filter") {
  11. SpyingAllocator spy;
  12. JsonDocument doc(&spy);
  13. DeserializationError error;
  14. JsonDocument filter;
  15. DeserializationOption::Filter filterOpt(filter);
  16. SECTION("root is fixmap") {
  17. SECTION("filter = {include:true,ignore:false)") {
  18. filter["include"] = true;
  19. filter["ignore"] = false;
  20. SECTION("input truncated after ignored key") {
  21. error = deserializeMsgPack(doc, "\x82\xA6ignore", 8, filterOpt);
  22. CHECK(error == DeserializationError::IncompleteInput);
  23. CHECK(doc.as<std::string>() == "{}");
  24. CHECK(spy.log() == AllocatorLog{
  25. Allocate(sizeofString("ignore")),
  26. Deallocate(sizeofString("ignore")),
  27. });
  28. }
  29. SECTION("input truncated after inside skipped uint 8") {
  30. error = deserializeMsgPack(doc, "\x82\xA6ignore\xCC\x2A\xA7include\x2A",
  31. 9, filterOpt);
  32. CHECK(error == DeserializationError::IncompleteInput);
  33. CHECK(doc.as<std::string>() == "{}");
  34. CHECK(spy.log() == AllocatorLog{
  35. Allocate(sizeofString("ignore")),
  36. Deallocate(sizeofString("ignore")),
  37. });
  38. }
  39. SECTION("input truncated after before skipped string size") {
  40. error = deserializeMsgPack(doc, "\x82\xA6ignore\xd9", 9, filterOpt);
  41. CHECK(error == DeserializationError::IncompleteInput);
  42. CHECK(doc.as<std::string>() == "{}");
  43. CHECK(spy.log() == AllocatorLog{
  44. Allocate(sizeofString("ignore")),
  45. Deallocate(sizeofString("ignore")),
  46. });
  47. }
  48. SECTION("input truncated after before skipped ext size") {
  49. error = deserializeMsgPack(doc, "\x82\xA6ignore\xC7", 9, filterOpt);
  50. CHECK(error == DeserializationError::IncompleteInput);
  51. CHECK(doc.as<std::string>() == "{}");
  52. CHECK(spy.log() == AllocatorLog{
  53. Allocate(sizeofString("ignore")),
  54. Deallocate(sizeofString("ignore")),
  55. });
  56. }
  57. SECTION("skip nil") {
  58. error = deserializeMsgPack(doc, "\x82\xA6ignore\xC0\xA7include\x2A",
  59. filterOpt);
  60. CHECK(error == DeserializationError::Ok);
  61. CHECK(doc.as<std::string>() == "{\"include\":42}");
  62. CHECK(spy.log() == AllocatorLog{
  63. Allocate(sizeofString("ignore")),
  64. Deallocate(sizeofString("ignore")),
  65. Allocate(sizeofString("include")),
  66. Allocate(sizeofPool()),
  67. Reallocate(sizeofPool(), sizeofObject(1)),
  68. });
  69. }
  70. SECTION("reject 0xc1") {
  71. error = deserializeMsgPack(doc, "\x82\xA6ignore\xC1\xA7include\x2A",
  72. filterOpt);
  73. CHECK(error == DeserializationError::InvalidInput);
  74. CHECK(spy.log() == AllocatorLog{
  75. Allocate(sizeofString("ignore")),
  76. Deallocate(sizeofString("ignore")),
  77. });
  78. }
  79. SECTION("skip false") {
  80. error = deserializeMsgPack(doc, "\x82\xA6ignore\xC2\xA7include\x2A",
  81. filterOpt);
  82. CHECK(error == DeserializationError::Ok);
  83. CHECK(doc.as<std::string>() == "{\"include\":42}");
  84. CHECK(spy.log() == AllocatorLog{
  85. Allocate(sizeofString("ignore")),
  86. Deallocate(sizeofString("ignore")),
  87. Allocate(sizeofString("include")),
  88. Allocate(sizeofPool()),
  89. Reallocate(sizeofPool(), sizeofObject(1)),
  90. });
  91. }
  92. SECTION("skip true") {
  93. error = deserializeMsgPack(doc, "\x82\xA6ignore\xC3\xA7include\x2A",
  94. filterOpt);
  95. CHECK(error == DeserializationError::Ok);
  96. CHECK(doc.as<std::string>() == "{\"include\":42}");
  97. CHECK(spy.log() == AllocatorLog{
  98. Allocate(sizeofString("ignore")),
  99. Deallocate(sizeofString("ignore")),
  100. Allocate(sizeofString("include")),
  101. Allocate(sizeofPool()),
  102. Reallocate(sizeofPool(), sizeofObject(1)),
  103. });
  104. }
  105. SECTION("skip positive fixint") {
  106. error = deserializeMsgPack(doc, "\x82\xA6ignore\x2A\xA7include\x2A",
  107. filterOpt);
  108. CHECK(error == DeserializationError::Ok);
  109. CHECK(doc.as<std::string>() == "{\"include\":42}");
  110. CHECK(spy.log() == AllocatorLog{
  111. Allocate(sizeofString("ignore")),
  112. Deallocate(sizeofString("ignore")),
  113. Allocate(sizeofString("include")),
  114. Allocate(sizeofPool()),
  115. Reallocate(sizeofPool(), sizeofObject(1)),
  116. });
  117. }
  118. SECTION("skip negative fixint") {
  119. error = deserializeMsgPack(doc, "\x82\xA6ignore\xFF\xA7include\x2A",
  120. filterOpt);
  121. CHECK(error == DeserializationError::Ok);
  122. CHECK(doc.as<std::string>() == "{\"include\":42}");
  123. CHECK(spy.log() == AllocatorLog{
  124. Allocate(sizeofString("ignore")),
  125. Deallocate(sizeofString("ignore")),
  126. Allocate(sizeofString("include")),
  127. Allocate(sizeofPool()),
  128. Reallocate(sizeofPool(), sizeofObject(1)),
  129. });
  130. }
  131. SECTION("skip uint 8") {
  132. error = deserializeMsgPack(doc, "\x82\xA6ignore\xCC\x2A\xA7include\x2A",
  133. filterOpt);
  134. CHECK(error == DeserializationError::Ok);
  135. CHECK(doc.as<std::string>() == "{\"include\":42}");
  136. CHECK(spy.log() == AllocatorLog{
  137. Allocate(sizeofString("ignore")),
  138. Deallocate(sizeofString("ignore")),
  139. Allocate(sizeofString("include")),
  140. Allocate(sizeofPool()),
  141. Reallocate(sizeofPool(), sizeofObject(1)),
  142. });
  143. }
  144. SECTION("skip int 8") {
  145. error = deserializeMsgPack(doc, "\x82\xA6ignore\xD0\x2A\xA7include\x2A",
  146. filterOpt);
  147. CHECK(error == DeserializationError::Ok);
  148. CHECK(doc.as<std::string>() == "{\"include\":42}");
  149. CHECK(spy.log() == AllocatorLog{
  150. Allocate(sizeofString("ignore")),
  151. Deallocate(sizeofString("ignore")),
  152. Allocate(sizeofString("include")),
  153. Allocate(sizeofPool()),
  154. Reallocate(sizeofPool(), sizeofObject(1)),
  155. });
  156. }
  157. SECTION("skip uint 16") {
  158. error = deserializeMsgPack(
  159. doc, "\x82\xA6ignore\xcd\x30\x39\xA7include\x2A", filterOpt);
  160. CHECK(error == DeserializationError::Ok);
  161. CHECK(doc.as<std::string>() == "{\"include\":42}");
  162. CHECK(spy.log() == AllocatorLog{
  163. Allocate(sizeofString("ignore")),
  164. Deallocate(sizeofString("ignore")),
  165. Allocate(sizeofString("include")),
  166. Allocate(sizeofPool()),
  167. Reallocate(sizeofPool(), sizeofObject(1)),
  168. });
  169. }
  170. SECTION("skip int 16") {
  171. error = deserializeMsgPack(
  172. doc, "\x82\xA6ignore\xD1\xCF\xC7\xA7include\x2A", filterOpt);
  173. CHECK(error == DeserializationError::Ok);
  174. CHECK(doc.as<std::string>() == "{\"include\":42}");
  175. CHECK(spy.log() == AllocatorLog{
  176. Allocate(sizeofString("ignore")),
  177. Deallocate(sizeofString("ignore")),
  178. Allocate(sizeofString("include")),
  179. Allocate(sizeofPool()),
  180. Reallocate(sizeofPool(), sizeofObject(1)),
  181. });
  182. }
  183. SECTION("skip uint 32") {
  184. error = deserializeMsgPack(
  185. doc, "\x82\xA6ignore\xCE\x12\x34\x56\x78\xA7include\x2A",
  186. filterOpt);
  187. CHECK(error == DeserializationError::Ok);
  188. CHECK(doc.as<std::string>() == "{\"include\":42}");
  189. CHECK(spy.log() == AllocatorLog{
  190. Allocate(sizeofString("ignore")),
  191. Deallocate(sizeofString("ignore")),
  192. Allocate(sizeofString("include")),
  193. Allocate(sizeofPool()),
  194. Reallocate(sizeofPool(), sizeofObject(1)),
  195. });
  196. }
  197. SECTION("skip int 32") {
  198. error = deserializeMsgPack(
  199. doc, "\x82\xA6ignore\xD2\xB6\x69\xFD\x2E\xA7include\x2A",
  200. filterOpt);
  201. CHECK(error == DeserializationError::Ok);
  202. CHECK(doc.as<std::string>() == "{\"include\":42}");
  203. CHECK(spy.log() == AllocatorLog{
  204. Allocate(sizeofString("ignore")),
  205. Deallocate(sizeofString("ignore")),
  206. Allocate(sizeofString("include")),
  207. Allocate(sizeofPool()),
  208. Reallocate(sizeofPool(), sizeofObject(1)),
  209. });
  210. }
  211. SECTION("skip uint 64") {
  212. error = deserializeMsgPack(
  213. doc,
  214. "\x82\xA6ignore\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0\xA7include\x2A",
  215. filterOpt);
  216. CHECK(error == DeserializationError::Ok);
  217. CHECK(doc.as<std::string>() == "{\"include\":42}");
  218. CHECK(spy.log() == AllocatorLog{
  219. Allocate(sizeofString("ignore")),
  220. Deallocate(sizeofString("ignore")),
  221. Allocate(sizeofString("include")),
  222. Allocate(sizeofPool()),
  223. Reallocate(sizeofPool(), sizeofObject(1)),
  224. });
  225. }
  226. SECTION("skip int 64") {
  227. error = deserializeMsgPack(
  228. doc,
  229. "\x82\xA6ignore\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0\xA7include\x2A",
  230. filterOpt);
  231. CHECK(error == DeserializationError::Ok);
  232. CHECK(doc.as<std::string>() == "{\"include\":42}");
  233. CHECK(spy.log() == AllocatorLog{
  234. Allocate(sizeofString("ignore")),
  235. Deallocate(sizeofString("ignore")),
  236. Allocate(sizeofString("include")),
  237. Allocate(sizeofPool()),
  238. Reallocate(sizeofPool(), sizeofObject(1)),
  239. });
  240. }
  241. SECTION("skip float 32") {
  242. error = deserializeMsgPack(
  243. doc, "\x82\xA6ignore\xCA\x40\x48\xF5\xC3\xA7include\x2A",
  244. filterOpt);
  245. CHECK(error == DeserializationError::Ok);
  246. CHECK(doc.as<std::string>() == "{\"include\":42}");
  247. CHECK(spy.log() == AllocatorLog{
  248. Allocate(sizeofString("ignore")),
  249. Deallocate(sizeofString("ignore")),
  250. Allocate(sizeofString("include")),
  251. Allocate(sizeofPool()),
  252. Reallocate(sizeofPool(), sizeofObject(1)),
  253. });
  254. }
  255. SECTION("skip float 64") {
  256. error = deserializeMsgPack(
  257. doc,
  258. "\x82\xA6ignore\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F\xA7include\x2A",
  259. filterOpt);
  260. CHECK(error == DeserializationError::Ok);
  261. CHECK(doc.as<std::string>() == "{\"include\":42}");
  262. CHECK(spy.log() == AllocatorLog{
  263. Allocate(sizeofString("ignore")),
  264. Deallocate(sizeofString("ignore")),
  265. Allocate(sizeofString("include")),
  266. Allocate(sizeofPool()),
  267. Reallocate(sizeofPool(), sizeofObject(1)),
  268. });
  269. }
  270. SECTION("skip fixstr") {
  271. error = deserializeMsgPack(
  272. doc, "\x82\xA6ignore\xABhello world\xA7include\x2A", filterOpt);
  273. CHECK(error == DeserializationError::Ok);
  274. CHECK(doc.as<std::string>() == "{\"include\":42}");
  275. CHECK(spy.log() == AllocatorLog{
  276. Allocate(sizeofString("ignore")),
  277. Deallocate(sizeofString("ignore")),
  278. Allocate(sizeofString("include")),
  279. Allocate(sizeofPool()),
  280. Reallocate(sizeofPool(), sizeofObject(1)),
  281. });
  282. }
  283. SECTION("skip str 8") {
  284. error = deserializeMsgPack(
  285. doc, "\x82\xA6ignore\xd9\x05hello\xA7include\x2A", filterOpt);
  286. CHECK(error == DeserializationError::Ok);
  287. CHECK(doc.as<std::string>() == "{\"include\":42}");
  288. CHECK(spy.log() == AllocatorLog{
  289. Allocate(sizeofString("ignore")),
  290. Deallocate(sizeofString("ignore")),
  291. Allocate(sizeofString("include")),
  292. Allocate(sizeofPool()),
  293. Reallocate(sizeofPool(), sizeofObject(1)),
  294. });
  295. }
  296. SECTION("skip str 16") {
  297. error = deserializeMsgPack(
  298. doc, "\x82\xA6ignore\xda\x00\x05hello\xA7include\x2A", filterOpt);
  299. CHECK(error == DeserializationError::Ok);
  300. CHECK(doc.as<std::string>() == "{\"include\":42}");
  301. CHECK(spy.log() == AllocatorLog{
  302. Allocate(sizeofString("ignore")),
  303. Deallocate(sizeofString("ignore")),
  304. Allocate(sizeofString("include")),
  305. Allocate(sizeofPool()),
  306. Reallocate(sizeofPool(), sizeofObject(1)),
  307. });
  308. }
  309. SECTION("skip str 32") {
  310. error = deserializeMsgPack(
  311. doc, "\x82\xA6ignore\xdb\x00\x00\x00\x05hello\xA7include\x2A",
  312. filterOpt);
  313. CHECK(error == DeserializationError::Ok);
  314. CHECK(doc.as<std::string>() == "{\"include\":42}");
  315. CHECK(spy.log() == AllocatorLog{
  316. Allocate(sizeofString("ignore")),
  317. Deallocate(sizeofString("ignore")),
  318. Allocate(sizeofString("include")),
  319. Allocate(sizeofPool()),
  320. Reallocate(sizeofPool(), sizeofObject(1)),
  321. });
  322. }
  323. SECTION("skip bin 8") {
  324. error = deserializeMsgPack(
  325. doc, "\x82\xA6ignore\xC4\x05hello\xA7include\x2A", filterOpt);
  326. CHECK(error == DeserializationError::Ok);
  327. CHECK(doc.as<std::string>() == "{\"include\":42}");
  328. CHECK(spy.log() == AllocatorLog{
  329. Allocate(sizeofString("ignore")),
  330. Deallocate(sizeofString("ignore")),
  331. Allocate(sizeofString("include")),
  332. Allocate(sizeofPool()),
  333. Reallocate(sizeofPool(), sizeofObject(1)),
  334. });
  335. }
  336. SECTION("skip bin 16") {
  337. error = deserializeMsgPack(
  338. doc, "\x82\xA6ignore\xC5\x00\x05hello\xA7include\x2A", filterOpt);
  339. CHECK(error == DeserializationError::Ok);
  340. CHECK(doc.as<std::string>() == "{\"include\":42}");
  341. CHECK(spy.log() == AllocatorLog{
  342. Allocate(sizeofString("ignore")),
  343. Deallocate(sizeofString("ignore")),
  344. Allocate(sizeofString("include")),
  345. Allocate(sizeofPool()),
  346. Reallocate(sizeofPool(), sizeofObject(1)),
  347. });
  348. }
  349. SECTION("skip bin 32") {
  350. error = deserializeMsgPack(
  351. doc, "\x82\xA6ignore\xC6\x00\x00\x00\x05hello\xA7include\x2A",
  352. filterOpt);
  353. CHECK(error == DeserializationError::Ok);
  354. CHECK(doc.as<std::string>() == "{\"include\":42}");
  355. CHECK(spy.log() == AllocatorLog{
  356. Allocate(sizeofString("ignore")),
  357. Deallocate(sizeofString("ignore")),
  358. Allocate(sizeofString("include")),
  359. Allocate(sizeofPool()),
  360. Reallocate(sizeofPool(), sizeofObject(1)),
  361. });
  362. }
  363. SECTION("skip fixarray") {
  364. error = deserializeMsgPack(
  365. doc, "\x82\xA6ignore\x92\x01\x02\xA7include\x2A", filterOpt);
  366. CHECK(error == DeserializationError::Ok);
  367. CHECK(doc.as<std::string>() == "{\"include\":42}");
  368. CHECK(spy.log() == AllocatorLog{
  369. Allocate(sizeofString("ignore")),
  370. Deallocate(sizeofString("ignore")),
  371. Allocate(sizeofString("include")),
  372. Allocate(sizeofPool()),
  373. Reallocate(sizeofPool(), sizeofObject(1)),
  374. });
  375. }
  376. SECTION("skip array 16") {
  377. error = deserializeMsgPack(
  378. doc, "\x82\xA6ignore\xDC\x00\x02\xA5hello\xA5world\xA7include\x2A",
  379. filterOpt);
  380. CHECK(error == DeserializationError::Ok);
  381. CHECK(doc.as<std::string>() == "{\"include\":42}");
  382. CHECK(spy.log() == AllocatorLog{
  383. Allocate(sizeofString("ignore")),
  384. Deallocate(sizeofString("ignore")),
  385. Allocate(sizeofString("include")),
  386. Allocate(sizeofPool()),
  387. Reallocate(sizeofPool(), sizeofObject(1)),
  388. });
  389. }
  390. SECTION("skip array 32") {
  391. error = deserializeMsgPack(
  392. doc,
  393. "\x82\xA6ignore"
  394. "\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3"
  395. "\xA7include\x2A",
  396. filterOpt);
  397. CHECK(error == DeserializationError::Ok);
  398. CHECK(doc.as<std::string>() == "{\"include\":42}");
  399. CHECK(spy.log() == AllocatorLog{
  400. Allocate(sizeofString("ignore")),
  401. Deallocate(sizeofString("ignore")),
  402. Allocate(sizeofString("include")),
  403. Allocate(sizeofPool()),
  404. Reallocate(sizeofPool(), sizeofObject(1)),
  405. });
  406. }
  407. SECTION("skip fixmap") {
  408. error = deserializeMsgPack(
  409. doc, "\x82\xA6ignore\x82\xA3one\x01\xA3two\x02\xA7include\x2A",
  410. filterOpt);
  411. CHECK(error == DeserializationError::Ok);
  412. CHECK(doc.as<std::string>() == "{\"include\":42}");
  413. CHECK(spy.log() == AllocatorLog{
  414. Allocate(sizeofString("ignore")),
  415. Deallocate(sizeofString("ignore")),
  416. Allocate(sizeofString("include")),
  417. Allocate(sizeofPool()),
  418. Reallocate(sizeofPool(), sizeofObject(1)),
  419. });
  420. }
  421. SECTION("skip map 16") {
  422. error = deserializeMsgPack(doc,
  423. "\x82\xA6ignore"
  424. "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world"
  425. "\xA7include\x2A",
  426. filterOpt);
  427. CHECK(error == DeserializationError::Ok);
  428. CHECK(doc.as<std::string>() == "{\"include\":42}");
  429. CHECK(spy.log() == AllocatorLog{
  430. Allocate(sizeofString("ignore")),
  431. Deallocate(sizeofString("ignore")),
  432. Allocate(sizeofString("include")),
  433. Allocate(sizeofPool()),
  434. Reallocate(sizeofPool(), sizeofObject(1)),
  435. });
  436. }
  437. SECTION("skip map 32") {
  438. error = deserializeMsgPack(doc,
  439. "\x82\xA6ignore"
  440. "\xDF\x00\x00\x00\x02"
  441. "\xA4zero\xCA\x00\x00\x00\x00"
  442. "\xA2pi\xCA\x40\x48\xF5\xC3"
  443. "\xA7include\x2A",
  444. filterOpt);
  445. CHECK(error == DeserializationError::Ok);
  446. CHECK(doc.as<std::string>() == "{\"include\":42}");
  447. CHECK(spy.log() == AllocatorLog{
  448. Allocate(sizeofString("ignore")),
  449. Deallocate(sizeofString("ignore")),
  450. Allocate(sizeofString("include")),
  451. Allocate(sizeofPool()),
  452. Reallocate(sizeofPool(), sizeofObject(1)),
  453. });
  454. }
  455. SECTION("skip fixext 1") {
  456. error = deserializeMsgPack(doc,
  457. "\x82\xA6ignore"
  458. "\xd4\x01\x02"
  459. "\xA7include\x2A",
  460. filterOpt);
  461. CHECK(error == DeserializationError::Ok);
  462. CHECK(doc.as<std::string>() == "{\"include\":42}");
  463. CHECK(spy.log() == AllocatorLog{
  464. Allocate(sizeofString("ignore")),
  465. Deallocate(sizeofString("ignore")),
  466. Allocate(sizeofString("include")),
  467. Allocate(sizeofPool()),
  468. Reallocate(sizeofPool(), sizeofObject(1)),
  469. });
  470. }
  471. SECTION("skip fixext 2") {
  472. error = deserializeMsgPack(doc,
  473. "\x82\xA6ignore"
  474. "\xd5\x01\x02\x03"
  475. "\xA7include\x2A",
  476. filterOpt);
  477. CHECK(error == DeserializationError::Ok);
  478. CHECK(doc.as<std::string>() == "{\"include\":42}");
  479. CHECK(spy.log() == AllocatorLog{
  480. Allocate(sizeofString("ignore")),
  481. Deallocate(sizeofString("ignore")),
  482. Allocate(sizeofString("include")),
  483. Allocate(sizeofPool()),
  484. Reallocate(sizeofPool(), sizeofObject(1)),
  485. });
  486. }
  487. SECTION("skip fixext 4") {
  488. error = deserializeMsgPack(doc,
  489. "\x82\xA6ignore"
  490. "\xd6\x01\x02\x03\x04\x05"
  491. "\xA7include\x2A",
  492. filterOpt);
  493. CHECK(error == DeserializationError::Ok);
  494. CHECK(doc.as<std::string>() == "{\"include\":42}");
  495. CHECK(spy.log() == AllocatorLog{
  496. Allocate(sizeofString("ignore")),
  497. Deallocate(sizeofString("ignore")),
  498. Allocate(sizeofString("include")),
  499. Allocate(sizeofPool()),
  500. Reallocate(sizeofPool(), sizeofObject(1)),
  501. });
  502. }
  503. SECTION("skip fixext 8") {
  504. error = deserializeMsgPack(doc,
  505. "\x82\xA6ignore"
  506. "\xd7\x01\x02\x03\x04\x05\x06\x07\x08\x09"
  507. "\xA7include\x2A",
  508. filterOpt);
  509. CHECK(error == DeserializationError::Ok);
  510. CHECK(doc.as<std::string>() == "{\"include\":42}");
  511. CHECK(spy.log() == AllocatorLog{
  512. Allocate(sizeofString("ignore")),
  513. Deallocate(sizeofString("ignore")),
  514. Allocate(sizeofString("include")),
  515. Allocate(sizeofPool()),
  516. Reallocate(sizeofPool(), sizeofObject(1)),
  517. });
  518. }
  519. SECTION("skip fixext 16") {
  520. error =
  521. deserializeMsgPack(doc,
  522. "\x82\xA6ignore"
  523. "\xd8\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"
  524. "\x0B\x0C\x0D\x0E\x0F\x10\x11"
  525. "\xA7include\x2A",
  526. filterOpt);
  527. CHECK(error == DeserializationError::Ok);
  528. CHECK(doc.as<std::string>() == "{\"include\":42}");
  529. CHECK(spy.log() == AllocatorLog{
  530. Allocate(sizeofString("ignore")),
  531. Deallocate(sizeofString("ignore")),
  532. Allocate(sizeofString("include")),
  533. Allocate(sizeofPool()),
  534. Reallocate(sizeofPool(), sizeofObject(1)),
  535. });
  536. }
  537. SECTION("skip ext 8") {
  538. error = deserializeMsgPack(doc,
  539. "\x82\xA6ignore"
  540. "\xc7\x02\x00\x01\x02"
  541. "\xA7include\x2A",
  542. filterOpt);
  543. CHECK(error == DeserializationError::Ok);
  544. CHECK(doc.as<std::string>() == "{\"include\":42}");
  545. CHECK(spy.log() == AllocatorLog{
  546. Allocate(sizeofString("ignore")),
  547. Deallocate(sizeofString("ignore")),
  548. Allocate(sizeofString("include")),
  549. Allocate(sizeofPool()),
  550. Reallocate(sizeofPool(), sizeofObject(1)),
  551. });
  552. }
  553. SECTION("skip ext 16") {
  554. error = deserializeMsgPack(doc,
  555. "\x82\xA6ignore"
  556. "\xc8\x00\x02\x00\x01\x02"
  557. "\xA7include\x2A",
  558. filterOpt);
  559. CHECK(error == DeserializationError::Ok);
  560. CHECK(doc.as<std::string>() == "{\"include\":42}");
  561. CHECK(spy.log() == AllocatorLog{
  562. Allocate(sizeofString("ignore")),
  563. Deallocate(sizeofString("ignore")),
  564. Allocate(sizeofString("include")),
  565. Allocate(sizeofPool()),
  566. Reallocate(sizeofPool(), sizeofObject(1)),
  567. });
  568. }
  569. SECTION("skip ext 32") {
  570. error = deserializeMsgPack(doc,
  571. "\x82\xA6ignore"
  572. "\xc9\x00\x00\x00\x02\x00\x01\x02"
  573. "\xA7include\x2A",
  574. filterOpt);
  575. CHECK(error == DeserializationError::Ok);
  576. CHECK(doc.as<std::string>() == "{\"include\":42}");
  577. CHECK(spy.log() == AllocatorLog{
  578. Allocate(sizeofString("ignore")),
  579. Deallocate(sizeofString("ignore")),
  580. Allocate(sizeofString("include")),
  581. Allocate(sizeofPool()),
  582. Reallocate(sizeofPool(), sizeofObject(1)),
  583. });
  584. }
  585. }
  586. SECTION("Filter = {arronly:[{measure:true}],include:true}") {
  587. filter["onlyarr"][0]["measure"] = true;
  588. filter["include"] = true;
  589. CAPTURE(filter.as<std::string>());
  590. SECTION("include fixarray") {
  591. error = deserializeMsgPack(doc,
  592. "\x82\xA7onlyarr\x92"
  593. "\x82\xA8location\x01\xA7measure\x02"
  594. "\x82\xA8location\x02\xA7measure\x04"
  595. "\xA7include\x2A",
  596. filterOpt);
  597. CHECK(error == DeserializationError::Ok);
  598. CHECK(doc.as<std::string>() ==
  599. "{\"onlyarr\":[{\"measure\":2},{\"measure\":4}],\"include\":42}");
  600. CHECK(spy.log() ==
  601. AllocatorLog{
  602. Allocate(sizeofString("onlyarr")),
  603. Allocate(sizeofPool()),
  604. Allocate(sizeofString("location")),
  605. Reallocate(sizeofString("location"), sizeofString("measure")),
  606. Allocate(sizeofString("location")),
  607. Reallocate(sizeofString("location"), sizeofString("include")),
  608. Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(2) +
  609. 2 * sizeofObject(1)),
  610. });
  611. }
  612. SECTION("include array 16") {
  613. error = deserializeMsgPack(doc,
  614. "\x82\xA7onlyarr"
  615. "\xDC\x00\x02"
  616. "\x82\xA8location\x01\xA7measure\x02"
  617. "\x82\xA8location\x02\xA7measure\x04"
  618. "\xA7include\x2A",
  619. filterOpt);
  620. CHECK(error == DeserializationError::Ok);
  621. CHECK(doc.as<std::string>() ==
  622. "{\"onlyarr\":[{\"measure\":2},{\"measure\":4}],\"include\":42}");
  623. CHECK(spy.log() ==
  624. AllocatorLog{
  625. Allocate(sizeofString("onlyarr")),
  626. Allocate(sizeofPool()),
  627. Allocate(sizeofString("location")),
  628. Reallocate(sizeofString("location"), sizeofString("measure")),
  629. Allocate(sizeofString("location")),
  630. Reallocate(sizeofString("location"), sizeofString("include")),
  631. Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(2) +
  632. 2 * sizeofObject(1)),
  633. });
  634. }
  635. SECTION("include array 32") {
  636. error = deserializeMsgPack(doc,
  637. "\x82\xA7onlyarr"
  638. "\xDD\x00\x00\x00\x02"
  639. "\x82\xA8location\x01\xA7measure\x02"
  640. "\x82\xA8location\x02\xA7measure\x04"
  641. "\xA7include\x2A",
  642. filterOpt);
  643. CHECK(error == DeserializationError::Ok);
  644. CHECK(doc.as<std::string>() ==
  645. "{\"onlyarr\":[{\"measure\":2},{\"measure\":4}],\"include\":42}");
  646. CHECK(spy.log() ==
  647. AllocatorLog{
  648. Allocate(sizeofString("onlyarr")),
  649. Allocate(sizeofPool()),
  650. Allocate(sizeofString("location")),
  651. Reallocate(sizeofString("location"), sizeofString("measure")),
  652. Allocate(sizeofString("location")),
  653. Reallocate(sizeofString("location"), sizeofString("include")),
  654. Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(2) +
  655. 2 * sizeofObject(1)),
  656. });
  657. }
  658. SECTION("skip null") {
  659. error = deserializeMsgPack(doc, "\x82\xA7onlyarr\xC0\xA7include\x2A",
  660. filterOpt);
  661. CHECK(error == DeserializationError::Ok);
  662. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  663. CHECK(spy.log() == AllocatorLog{
  664. Allocate(sizeofString("onlyarr")),
  665. Allocate(sizeofPool()),
  666. Allocate(sizeofString("include")),
  667. Reallocate(sizeofPool(), sizeofObject(2)),
  668. });
  669. }
  670. SECTION("skip false") {
  671. error = deserializeMsgPack(doc, "\x82\xA7onlyarr\xC2\xA7include\x2A",
  672. filterOpt);
  673. CHECK(error == DeserializationError::Ok);
  674. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  675. CHECK(spy.log() == AllocatorLog{
  676. Allocate(sizeofString("onlyarr")),
  677. Allocate(sizeofPool()),
  678. Allocate(sizeofString("include")),
  679. Reallocate(sizeofPool(), sizeofObject(2)),
  680. });
  681. }
  682. SECTION("skip true") {
  683. error = deserializeMsgPack(doc, "\x82\xA7onlyarr\xC3\xA7include\x2A",
  684. filterOpt);
  685. CHECK(error == DeserializationError::Ok);
  686. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  687. CHECK(spy.log() == AllocatorLog{
  688. Allocate(sizeofString("onlyarr")),
  689. Allocate(sizeofPool()),
  690. Allocate(sizeofString("include")),
  691. Reallocate(sizeofPool(), sizeofObject(2)),
  692. });
  693. }
  694. SECTION("skip positive fixint") {
  695. error = deserializeMsgPack(doc, "\x82\xA7onlyarr\x2A\xA7include\x2A",
  696. filterOpt);
  697. CHECK(error == DeserializationError::Ok);
  698. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  699. CHECK(spy.log() == AllocatorLog{
  700. Allocate(sizeofString("onlyarr")),
  701. Allocate(sizeofPool()),
  702. Allocate(sizeofString("include")),
  703. Reallocate(sizeofPool(), sizeofObject(2)),
  704. });
  705. }
  706. SECTION("skip negative fixint") {
  707. error = deserializeMsgPack(doc, "\x82\xA7onlyarr\xFF\xA7include\x2A",
  708. filterOpt);
  709. CHECK(error == DeserializationError::Ok);
  710. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  711. CHECK(spy.log() == AllocatorLog{
  712. Allocate(sizeofString("onlyarr")),
  713. Allocate(sizeofPool()),
  714. Allocate(sizeofString("include")),
  715. Reallocate(sizeofPool(), sizeofObject(2)),
  716. });
  717. }
  718. SECTION("skip uint 8") {
  719. error = deserializeMsgPack(
  720. doc, "\x82\xA7onlyarr\xCC\x2A\xA7include\x2A", filterOpt);
  721. CHECK(error == DeserializationError::Ok);
  722. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  723. CHECK(spy.log() == AllocatorLog{
  724. Allocate(sizeofString("onlyarr")),
  725. Allocate(sizeofPool()),
  726. Allocate(sizeofString("include")),
  727. Reallocate(sizeofPool(), sizeofObject(2)),
  728. });
  729. }
  730. SECTION("skip uint 16") {
  731. error = deserializeMsgPack(
  732. doc, "\x82\xA7onlyarr\xcd\x30\x39\xA7include\x2A", filterOpt);
  733. CHECK(error == DeserializationError::Ok);
  734. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  735. CHECK(spy.log() == AllocatorLog{
  736. Allocate(sizeofString("onlyarr")),
  737. Allocate(sizeofPool()),
  738. Allocate(sizeofString("include")),
  739. Reallocate(sizeofPool(), sizeofObject(2)),
  740. });
  741. }
  742. SECTION("skip uint 32") {
  743. error = deserializeMsgPack(
  744. doc, "\x82\xA7onlyarr\xCE\x12\x34\x56\x78\xA7include\x2A",
  745. filterOpt);
  746. CHECK(error == DeserializationError::Ok);
  747. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  748. CHECK(spy.log() == AllocatorLog{
  749. Allocate(sizeofString("onlyarr")),
  750. Allocate(sizeofPool()),
  751. Allocate(sizeofString("include")),
  752. Reallocate(sizeofPool(), sizeofObject(2)),
  753. });
  754. }
  755. SECTION("skip uint 64") {
  756. error = deserializeMsgPack(doc,
  757. "\x82\xA7onlyarr\xCF\x12\x34\x56\x78\x9A\xBC"
  758. "\xDE\xF0\xA7include\x2A",
  759. filterOpt);
  760. CHECK(error == DeserializationError::Ok);
  761. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  762. CHECK(spy.log() == AllocatorLog{
  763. Allocate(sizeofString("onlyarr")),
  764. Allocate(sizeofPool()),
  765. Allocate(sizeofString("include")),
  766. Reallocate(sizeofPool(), sizeofObject(2)),
  767. });
  768. }
  769. SECTION("skip int 8") {
  770. error = deserializeMsgPack(
  771. doc, "\x82\xA7onlyarr\xD0\x2A\xA7include\x2A", filterOpt);
  772. CHECK(error == DeserializationError::Ok);
  773. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  774. CHECK(spy.log() == AllocatorLog{
  775. Allocate(sizeofString("onlyarr")),
  776. Allocate(sizeofPool()),
  777. Allocate(sizeofString("include")),
  778. Reallocate(sizeofPool(), sizeofObject(2)),
  779. });
  780. }
  781. SECTION("skip int 16") {
  782. error = deserializeMsgPack(
  783. doc, "\x82\xA7onlyarr\xD1\xCF\xC7\xA7include\x2A", filterOpt);
  784. CHECK(error == DeserializationError::Ok);
  785. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  786. CHECK(spy.log() == AllocatorLog{
  787. Allocate(sizeofString("onlyarr")),
  788. Allocate(sizeofPool()),
  789. Allocate(sizeofString("include")),
  790. Reallocate(sizeofPool(), sizeofObject(2)),
  791. });
  792. }
  793. SECTION("skip int 32") {
  794. error = deserializeMsgPack(
  795. doc, "\x82\xA7onlyarr\xD2\xB6\x69\xFD\x2E\xA7include\x2A",
  796. filterOpt);
  797. CHECK(error == DeserializationError::Ok);
  798. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  799. CHECK(spy.log() == AllocatorLog{
  800. Allocate(sizeofString("onlyarr")),
  801. Allocate(sizeofPool()),
  802. Allocate(sizeofString("include")),
  803. Reallocate(sizeofPool(), sizeofObject(2)),
  804. });
  805. }
  806. SECTION("skip int 64") {
  807. error = deserializeMsgPack(doc,
  808. "\x82\xA7onlyarr\xD3\x12\x34\x56\x78\x9A\xBC"
  809. "\xDE\xF0\xA7include\x2A",
  810. filterOpt);
  811. CHECK(error == DeserializationError::Ok);
  812. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  813. CHECK(spy.log() == AllocatorLog{
  814. Allocate(sizeofString("onlyarr")),
  815. Allocate(sizeofPool()),
  816. Allocate(sizeofString("include")),
  817. Reallocate(sizeofPool(), sizeofObject(2)),
  818. });
  819. }
  820. SECTION("skip float 32") {
  821. error = deserializeMsgPack(
  822. doc, "\x82\xA7onlyarr\xCA\x40\x48\xF5\xC3\xA7include\x2A",
  823. filterOpt);
  824. CHECK(error == DeserializationError::Ok);
  825. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  826. CHECK(spy.log() == AllocatorLog{
  827. Allocate(sizeofString("onlyarr")),
  828. Allocate(sizeofPool()),
  829. Allocate(sizeofString("include")),
  830. Reallocate(sizeofPool(), sizeofObject(2)),
  831. });
  832. }
  833. SECTION("skip float 64") {
  834. error = deserializeMsgPack(doc,
  835. "\x82\xA7onlyarr\xCB\x40\x09\x21\xCA\xC0\x83"
  836. "\x12\x6F\xA7include\x2A",
  837. filterOpt);
  838. CHECK(error == DeserializationError::Ok);
  839. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  840. CHECK(spy.log() == AllocatorLog{
  841. Allocate(sizeofString("onlyarr")),
  842. Allocate(sizeofPool()),
  843. Allocate(sizeofString("include")),
  844. Reallocate(sizeofPool(), sizeofObject(2)),
  845. });
  846. }
  847. SECTION("skip fixstr") {
  848. error = deserializeMsgPack(
  849. doc, "\x82\xA7onlyarr\xABhello world\xA7include\x2A", filterOpt);
  850. CHECK(error == DeserializationError::Ok);
  851. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  852. CHECK(spy.log() == AllocatorLog{
  853. Allocate(sizeofString("onlyarr")),
  854. Allocate(sizeofPool()),
  855. Allocate(sizeofString("include")),
  856. Reallocate(sizeofPool(), sizeofObject(2)),
  857. });
  858. }
  859. SECTION("skip str 8") {
  860. error = deserializeMsgPack(
  861. doc, "\x82\xA7onlyarr\xd9\x05hello\xA7include\x2A", filterOpt);
  862. CHECK(error == DeserializationError::Ok);
  863. }
  864. SECTION("skip str 16") {
  865. error = deserializeMsgPack(
  866. doc, "\x82\xA7onlyarr\xda\x00\x05hello\xA7include\x2A", filterOpt);
  867. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  868. }
  869. SECTION("skip str 32") {
  870. error = deserializeMsgPack(
  871. doc, "\x82\xA7onlyarr\xdb\x00\x00\x00\x05hello\xA7include\x2A",
  872. filterOpt);
  873. CHECK(spy.log() == AllocatorLog{
  874. Allocate(sizeofString("onlyarr")),
  875. Allocate(sizeofPool()),
  876. Allocate(sizeofString("include")),
  877. Reallocate(sizeofPool(), sizeofObject(2)),
  878. });
  879. }
  880. SECTION("skip fixmap") {
  881. error = deserializeMsgPack(
  882. doc, "\x82\xA7onlyarr\x82\xA3one\x01\xA3two\x02\xA7include\x2A",
  883. filterOpt);
  884. CHECK(error == DeserializationError::Ok);
  885. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  886. CHECK(spy.log() == AllocatorLog{
  887. Allocate(sizeofString("onlyarr")),
  888. Allocate(sizeofPool()),
  889. Allocate(sizeofString("one")),
  890. Deallocate(sizeofString("one")),
  891. Allocate(sizeofString("include")),
  892. Reallocate(sizeofPool(), sizeofObject(2)),
  893. });
  894. }
  895. SECTION("skip map 16") {
  896. error = deserializeMsgPack(doc,
  897. "\x82\xA7onlyarr"
  898. "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world"
  899. "\xA7include\x2A",
  900. filterOpt);
  901. CHECK(error == DeserializationError::Ok);
  902. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  903. CHECK(spy.log() == AllocatorLog{
  904. Allocate(sizeofString("onlyarr")),
  905. Allocate(sizeofPool()),
  906. Allocate(sizeofString("H")),
  907. Deallocate(sizeofString("H")),
  908. Allocate(sizeofString("include")),
  909. Reallocate(sizeofPool(), sizeofObject(2)),
  910. });
  911. }
  912. SECTION("skip map 32") {
  913. error = deserializeMsgPack(doc,
  914. "\x82\xA7onlyarr"
  915. "\xDF\x00\x00\x00\x02"
  916. "\xA4zero\xCA\x00\x00\x00\x00"
  917. "\xA2pi\xCA\x40\x48\xF5\xC3"
  918. "\xA7include\x2A",
  919. filterOpt);
  920. CHECK(error == DeserializationError::Ok);
  921. CHECK(doc.as<std::string>() == "{\"onlyarr\":null,\"include\":42}");
  922. CHECK(spy.log() == AllocatorLog{
  923. Allocate(sizeofString("onlyarr")),
  924. Allocate(sizeofPool()),
  925. Allocate(sizeofString("zero")),
  926. Deallocate(sizeofString("zero")),
  927. Allocate(sizeofString("include")),
  928. Reallocate(sizeofPool(), sizeofObject(2)),
  929. });
  930. }
  931. }
  932. }
  933. SECTION("root is fixarray") {
  934. SECTION("filter = [false, true]") {
  935. filter[0] = false; // only the first elment of the filter matters
  936. filter[1] = true; // so this one is ignored
  937. SECTION("input = [1,2,3]") {
  938. error = deserializeMsgPack(doc, "\x93\x01\x02\x03", filterOpt);
  939. CHECK(error == DeserializationError::Ok);
  940. CHECK(doc.as<std::string>() == "[]");
  941. CHECK(spy.log() == AllocatorLog());
  942. }
  943. }
  944. SECTION("filter = [true, false]") {
  945. filter[0] = true; // only the first elment of the filter matters
  946. filter[1] = false; // so this one is ignored
  947. SECTION("input = [1,2,3]") {
  948. error = deserializeMsgPack(doc, "\x93\x01\x02\x03", filterOpt);
  949. CHECK(error == DeserializationError::Ok);
  950. CHECK(doc.as<std::string>() == "[1,2,3]");
  951. CHECK(spy.log() == AllocatorLog{
  952. Allocate(sizeofPool()),
  953. Reallocate(sizeofPool(), sizeofArray(3)),
  954. });
  955. }
  956. }
  957. }
  958. SECTION("Filter = {onlyobj:{measure:true},include:true}") {
  959. filter["onlyobj"]["measure"] = true;
  960. filter["include"] = true;
  961. CAPTURE(filter.as<std::string>());
  962. SECTION("include fixmap") {
  963. error = deserializeMsgPack(doc,
  964. "\x82\xA7onlyobj"
  965. "\x82\xA8location\x01\xA7measure\x02"
  966. "\xA7include\x2A",
  967. filterOpt);
  968. CHECK(error == DeserializationError::Ok);
  969. CHECK(doc.as<std::string>() ==
  970. "{\"onlyobj\":{\"measure\":2},\"include\":42}");
  971. CHECK(spy.log() ==
  972. AllocatorLog{
  973. Allocate(sizeofString("onlyobj")),
  974. Allocate(sizeofPool()),
  975. Allocate(sizeofString("location")),
  976. Reallocate(sizeofString("location"), sizeofString("measure")),
  977. Allocate(sizeofString("include")),
  978. Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
  979. });
  980. }
  981. SECTION("include map 16") {
  982. error = deserializeMsgPack(doc,
  983. "\x82\xA7onlyobj"
  984. "\xDE\x00\x02\xA8location\x01\xA7measure\x02"
  985. "\xA7include\x2A",
  986. filterOpt);
  987. CHECK(error == DeserializationError::Ok);
  988. CHECK(doc.as<std::string>() ==
  989. "{\"onlyobj\":{\"measure\":2},\"include\":42}");
  990. CHECK(spy.log() ==
  991. AllocatorLog{
  992. Allocate(sizeofString("onlyobj")),
  993. Allocate(sizeofPool()),
  994. Allocate(sizeofString("location")),
  995. Reallocate(sizeofString("location"), sizeofString("measure")),
  996. Allocate(sizeofString("include")),
  997. Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
  998. });
  999. }
  1000. SECTION("include map 32") {
  1001. error = deserializeMsgPack(doc,
  1002. "\x82\xA7onlyobj"
  1003. "\xDF\x00\x00\x00\x02"
  1004. "\xA8location\x01\xA7measure\x02"
  1005. "\xA7include\x2A",
  1006. filterOpt);
  1007. CHECK(error == DeserializationError::Ok);
  1008. CHECK(doc.as<std::string>() ==
  1009. "{\"onlyobj\":{\"measure\":2},\"include\":42}");
  1010. CHECK(spy.log() ==
  1011. AllocatorLog{
  1012. Allocate(sizeofString("onlyobj")),
  1013. Allocate(sizeofPool()),
  1014. Allocate(sizeofString("location")),
  1015. Reallocate(sizeofString("location"), sizeofString("measure")),
  1016. Allocate(sizeofString("include")),
  1017. Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
  1018. });
  1019. }
  1020. SECTION("skip null") {
  1021. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xC0\xA7include\x2A",
  1022. filterOpt);
  1023. CHECK(error == DeserializationError::Ok);
  1024. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1025. CHECK(spy.log() == AllocatorLog{
  1026. Allocate(sizeofString("onlyarr")),
  1027. Allocate(sizeofPool()),
  1028. Allocate(sizeofString("include")),
  1029. Reallocate(sizeofPool(), sizeofObject(2)),
  1030. });
  1031. }
  1032. SECTION("skip false") {
  1033. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xC2\xA7include\x2A",
  1034. filterOpt);
  1035. CHECK(error == DeserializationError::Ok);
  1036. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1037. CHECK(spy.log() == AllocatorLog{
  1038. Allocate(sizeofString("onlyarr")),
  1039. Allocate(sizeofPool()),
  1040. Allocate(sizeofString("include")),
  1041. Reallocate(sizeofPool(), sizeofObject(2)),
  1042. });
  1043. }
  1044. SECTION("skip true") {
  1045. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xC3\xA7include\x2A",
  1046. filterOpt);
  1047. CHECK(error == DeserializationError::Ok);
  1048. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1049. CHECK(spy.log() == AllocatorLog{
  1050. Allocate(sizeofString("onlyarr")),
  1051. Allocate(sizeofPool()),
  1052. Allocate(sizeofString("include")),
  1053. Reallocate(sizeofPool(), sizeofObject(2)),
  1054. });
  1055. }
  1056. SECTION("skip positive fixint") {
  1057. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\x2A\xA7include\x2A",
  1058. filterOpt);
  1059. CHECK(error == DeserializationError::Ok);
  1060. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1061. CHECK(spy.log() == AllocatorLog{
  1062. Allocate(sizeofString("onlyarr")),
  1063. Allocate(sizeofPool()),
  1064. Allocate(sizeofString("include")),
  1065. Reallocate(sizeofPool(), sizeofObject(2)),
  1066. });
  1067. }
  1068. SECTION("skip negative fixint") {
  1069. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xFF\xA7include\x2A",
  1070. filterOpt);
  1071. CHECK(error == DeserializationError::Ok);
  1072. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1073. CHECK(spy.log() == AllocatorLog{
  1074. Allocate(sizeofString("onlyarr")),
  1075. Allocate(sizeofPool()),
  1076. Allocate(sizeofString("include")),
  1077. Reallocate(sizeofPool(), sizeofObject(2)),
  1078. });
  1079. }
  1080. SECTION("skip uint 8") {
  1081. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xCC\x2A\xA7include\x2A",
  1082. filterOpt);
  1083. CHECK(error == DeserializationError::Ok);
  1084. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1085. CHECK(spy.log() == AllocatorLog{
  1086. Allocate(sizeofString("onlyarr")),
  1087. Allocate(sizeofPool()),
  1088. Allocate(sizeofString("include")),
  1089. Reallocate(sizeofPool(), sizeofObject(2)),
  1090. });
  1091. }
  1092. SECTION("skip uint 16") {
  1093. error = deserializeMsgPack(
  1094. doc, "\x82\xA7onlyobj\xcd\x30\x39\xA7include\x2A", filterOpt);
  1095. CHECK(error == DeserializationError::Ok);
  1096. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1097. CHECK(spy.log() == AllocatorLog{
  1098. Allocate(sizeofString("onlyarr")),
  1099. Allocate(sizeofPool()),
  1100. Allocate(sizeofString("include")),
  1101. Reallocate(sizeofPool(), sizeofObject(2)),
  1102. });
  1103. }
  1104. SECTION("skip uint 32") {
  1105. error = deserializeMsgPack(
  1106. doc, "\x82\xA7onlyobj\xCE\x12\x34\x56\x78\xA7include\x2A", filterOpt);
  1107. CHECK(error == DeserializationError::Ok);
  1108. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1109. CHECK(spy.log() == AllocatorLog{
  1110. Allocate(sizeofString("onlyarr")),
  1111. Allocate(sizeofPool()),
  1112. Allocate(sizeofString("include")),
  1113. Reallocate(sizeofPool(), sizeofObject(2)),
  1114. });
  1115. }
  1116. SECTION("skip uint 64") {
  1117. error = deserializeMsgPack(doc,
  1118. "\x82\xA7onlyobj\xCF\x12\x34\x56\x78\x9A\xBC"
  1119. "\xDE\xF0\xA7include\x2A",
  1120. filterOpt);
  1121. CHECK(error == DeserializationError::Ok);
  1122. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1123. CHECK(spy.log() == AllocatorLog{
  1124. Allocate(sizeofString("onlyarr")),
  1125. Allocate(sizeofPool()),
  1126. Allocate(sizeofString("include")),
  1127. Reallocate(sizeofPool(), sizeofObject(2)),
  1128. });
  1129. }
  1130. SECTION("skip int 8") {
  1131. error = deserializeMsgPack(doc, "\x82\xA7onlyobj\xD0\x2A\xA7include\x2A",
  1132. filterOpt);
  1133. CHECK(error == DeserializationError::Ok);
  1134. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1135. CHECK(spy.log() == AllocatorLog{
  1136. Allocate(sizeofString("onlyarr")),
  1137. Allocate(sizeofPool()),
  1138. Allocate(sizeofString("include")),
  1139. Reallocate(sizeofPool(), sizeofObject(2)),
  1140. });
  1141. }
  1142. SECTION("skip int 16") {
  1143. error = deserializeMsgPack(
  1144. doc, "\x82\xA7onlyobj\xD1\xCF\xC7\xA7include\x2A", filterOpt);
  1145. CHECK(error == DeserializationError::Ok);
  1146. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1147. CHECK(spy.log() == AllocatorLog{
  1148. Allocate(sizeofString("onlyarr")),
  1149. Allocate(sizeofPool()),
  1150. Allocate(sizeofString("include")),
  1151. Reallocate(sizeofPool(), sizeofObject(2)),
  1152. });
  1153. }
  1154. SECTION("skip int 32") {
  1155. error = deserializeMsgPack(
  1156. doc, "\x82\xA7onlyobj\xD2\xB6\x69\xFD\x2E\xA7include\x2A", filterOpt);
  1157. CHECK(error == DeserializationError::Ok);
  1158. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1159. CHECK(spy.log() == AllocatorLog{
  1160. Allocate(sizeofString("onlyarr")),
  1161. Allocate(sizeofPool()),
  1162. Allocate(sizeofString("include")),
  1163. Reallocate(sizeofPool(), sizeofObject(2)),
  1164. });
  1165. }
  1166. SECTION("skip int 64") {
  1167. error = deserializeMsgPack(doc,
  1168. "\x82\xA7onlyobj\xD3\x12\x34\x56\x78\x9A\xBC"
  1169. "\xDE\xF0\xA7include\x2A",
  1170. filterOpt);
  1171. CHECK(error == DeserializationError::Ok);
  1172. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1173. CHECK(spy.log() == AllocatorLog{
  1174. Allocate(sizeofString("onlyarr")),
  1175. Allocate(sizeofPool()),
  1176. Allocate(sizeofString("include")),
  1177. Reallocate(sizeofPool(), sizeofObject(2)),
  1178. });
  1179. }
  1180. SECTION("skip float 32") {
  1181. error = deserializeMsgPack(
  1182. doc, "\x82\xA7onlyobj\xCA\x40\x48\xF5\xC3\xA7include\x2A", filterOpt);
  1183. CHECK(error == DeserializationError::Ok);
  1184. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1185. CHECK(spy.log() == AllocatorLog{
  1186. Allocate(sizeofString("onlyarr")),
  1187. Allocate(sizeofPool()),
  1188. Allocate(sizeofString("include")),
  1189. Reallocate(sizeofPool(), sizeofObject(2)),
  1190. });
  1191. }
  1192. SECTION("skip float 64") {
  1193. error = deserializeMsgPack(doc,
  1194. "\x82\xA7onlyobj\xCB\x40\x09\x21\xCA\xC0\x83"
  1195. "\x12\x6F\xA7include\x2A",
  1196. filterOpt);
  1197. CHECK(error == DeserializationError::Ok);
  1198. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1199. CHECK(spy.log() == AllocatorLog{
  1200. Allocate(sizeofString("onlyarr")),
  1201. Allocate(sizeofPool()),
  1202. Allocate(sizeofString("include")),
  1203. Reallocate(sizeofPool(), sizeofObject(2)),
  1204. });
  1205. }
  1206. SECTION("skip fixstr") {
  1207. error = deserializeMsgPack(
  1208. doc, "\x82\xA7onlyobj\xABhello world\xA7include\x2A", filterOpt);
  1209. CHECK(error == DeserializationError::Ok);
  1210. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1211. CHECK(spy.log() == AllocatorLog{
  1212. Allocate(sizeofString("onlyarr")),
  1213. Allocate(sizeofPool()),
  1214. Allocate(sizeofString("include")),
  1215. Reallocate(sizeofPool(), sizeofObject(2)),
  1216. });
  1217. }
  1218. SECTION("skip str 8") {
  1219. error = deserializeMsgPack(
  1220. doc, "\x82\xA7onlyobj\xd9\x05hello\xA7include\x2A", filterOpt);
  1221. CHECK(error == DeserializationError::Ok);
  1222. }
  1223. SECTION("skip str 16") {
  1224. error = deserializeMsgPack(
  1225. doc, "\x82\xA7onlyobj\xda\x00\x05hello\xA7include\x2A", filterOpt);
  1226. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1227. }
  1228. SECTION("skip str 32") {
  1229. error = deserializeMsgPack(
  1230. doc, "\x82\xA7onlyobj\xdb\x00\x00\x00\x05hello\xA7include\x2A",
  1231. filterOpt);
  1232. CHECK(spy.log() == AllocatorLog{
  1233. Allocate(sizeofString("onlyarr")),
  1234. Allocate(sizeofPool()),
  1235. Allocate(sizeofString("include")),
  1236. Reallocate(sizeofPool(), sizeofObject(2)),
  1237. });
  1238. }
  1239. SECTION("skip fixarray") {
  1240. error = deserializeMsgPack(
  1241. doc, "\x82\xA7onlyobj\x92\x01\x02\xA7include\x2A", filterOpt);
  1242. CHECK(error == DeserializationError::Ok);
  1243. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1244. CHECK(spy.log() == AllocatorLog{
  1245. Allocate(sizeofString("onlyarr")),
  1246. Allocate(sizeofPool()),
  1247. Allocate(sizeofString("include")),
  1248. Reallocate(sizeofPool(), sizeofObject(2)),
  1249. });
  1250. }
  1251. SECTION("skip array 16") {
  1252. error = deserializeMsgPack(doc,
  1253. "\x82\xA7onlyobj\xDC\x00\x01\xA7"
  1254. "example\xA7include\x2A",
  1255. filterOpt);
  1256. CHECK(error == DeserializationError::Ok);
  1257. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1258. CHECK(spy.log() == AllocatorLog{
  1259. Allocate(sizeofString("onlyarr")),
  1260. Allocate(sizeofPool()),
  1261. Allocate(sizeofString("include")),
  1262. Reallocate(sizeofPool(), sizeofObject(2)),
  1263. });
  1264. }
  1265. SECTION("skip array 32") {
  1266. error = deserializeMsgPack(doc,
  1267. "\x82\xA7onlyobj"
  1268. "\xDD\x00\x00\x00\x02\x01\x02"
  1269. "\xA7include\x2A",
  1270. filterOpt);
  1271. CHECK(error == DeserializationError::Ok);
  1272. CHECK(doc.as<std::string>() == "{\"onlyobj\":null,\"include\":42}");
  1273. CHECK(spy.log() == AllocatorLog{
  1274. Allocate(sizeofString("onlyarr")),
  1275. Allocate(sizeofPool()),
  1276. Allocate(sizeofString("include")),
  1277. Reallocate(sizeofPool(), sizeofObject(2)),
  1278. });
  1279. }
  1280. }
  1281. SECTION("filter = true") {
  1282. filter.set(true);
  1283. error = deserializeMsgPack(doc, "\x90", filterOpt);
  1284. CHECK(error == DeserializationError::Ok);
  1285. CHECK(doc.is<JsonArray>() == true);
  1286. CHECK(doc.size() == 0);
  1287. }
  1288. SECTION("filter = false") {
  1289. filter.set(false);
  1290. SECTION("input = fixarray") {
  1291. error = deserializeMsgPack(doc, "\x92\x01\x02", filterOpt);
  1292. CHECK(error == DeserializationError::Ok);
  1293. CHECK(doc.isNull() == true);
  1294. }
  1295. SECTION("input = array 16") {
  1296. error = deserializeMsgPack(doc, "\xDC\x00\x02\x01\x02", filterOpt);
  1297. CHECK(error == DeserializationError::Ok);
  1298. CHECK(doc.isNull() == true);
  1299. }
  1300. SECTION("array too deep") {
  1301. error = deserializeMsgPack(doc, "\x91\x91\x91\x91\x91", 5, filterOpt,
  1302. DeserializationOption::NestingLimit(4));
  1303. CHECK(error == DeserializationError::TooDeep);
  1304. }
  1305. SECTION("object too deep") {
  1306. error = deserializeMsgPack(
  1307. doc, "\x81\xA1z\x81\xA1z\x81\xA1z\x81\xA1z\x81\xA1z", 15, filterOpt,
  1308. DeserializationOption::NestingLimit(4));
  1309. CHECK(error == DeserializationError::TooDeep);
  1310. }
  1311. }
  1312. }
  1313. TEST_CASE("Zero-copy mode") { // issue #1697
  1314. char input[] = "\x82\xA7include\x01\xA6ignore\x02";
  1315. JsonDocument filter;
  1316. filter["include"] = true;
  1317. JsonDocument doc;
  1318. DeserializationError err =
  1319. deserializeMsgPack(doc, input, 18, DeserializationOption::Filter(filter));
  1320. CHECK(err == DeserializationError::Ok);
  1321. CHECK(doc.as<std::string>() == "{\"include\":1}");
  1322. }
  1323. TEST_CASE("Overloads") {
  1324. JsonDocument doc;
  1325. JsonDocument filter;
  1326. using namespace DeserializationOption;
  1327. // deserializeMsgPack(..., Filter)
  1328. SECTION("const char*, Filter") {
  1329. deserializeMsgPack(doc, "{}", Filter(filter));
  1330. }
  1331. SECTION("const char*, size_t, Filter") {
  1332. deserializeMsgPack(doc, "{}", 2, Filter(filter));
  1333. }
  1334. SECTION("const std::string&, Filter") {
  1335. deserializeMsgPack(doc, "{}"_s, Filter(filter));
  1336. }
  1337. SECTION("std::istream&, Filter") {
  1338. std::stringstream s("{}");
  1339. deserializeMsgPack(doc, s, Filter(filter));
  1340. }
  1341. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  1342. SECTION("char[n], Filter") {
  1343. size_t i = 4;
  1344. char vla[i];
  1345. strcpy(vla, "{}");
  1346. deserializeMsgPack(doc, vla, Filter(filter));
  1347. }
  1348. #endif
  1349. // deserializeMsgPack(..., Filter, NestingLimit)
  1350. SECTION("const char*, Filter, NestingLimit") {
  1351. deserializeMsgPack(doc, "{}", Filter(filter), NestingLimit(5));
  1352. }
  1353. SECTION("const char*, size_t, Filter, NestingLimit") {
  1354. deserializeMsgPack(doc, "{}", 2, Filter(filter), NestingLimit(5));
  1355. }
  1356. SECTION("const std::string&, Filter, NestingLimit") {
  1357. deserializeMsgPack(doc, "{}"_s, Filter(filter), NestingLimit(5));
  1358. }
  1359. SECTION("std::istream&, Filter, NestingLimit") {
  1360. std::stringstream s("{}");
  1361. deserializeMsgPack(doc, s, Filter(filter), NestingLimit(5));
  1362. }
  1363. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  1364. SECTION("char[n], Filter, NestingLimit") {
  1365. size_t i = 4;
  1366. char vla[i];
  1367. strcpy(vla, "{}");
  1368. deserializeMsgPack(doc, vla, Filter(filter), NestingLimit(5));
  1369. }
  1370. #endif
  1371. // deserializeMsgPack(..., NestingLimit, Filter)
  1372. SECTION("const char*, NestingLimit, Filter") {
  1373. deserializeMsgPack(doc, "{}", NestingLimit(5), Filter(filter));
  1374. }
  1375. SECTION("const char*, size_t, NestingLimit, Filter") {
  1376. deserializeMsgPack(doc, "{}", 2, NestingLimit(5), Filter(filter));
  1377. }
  1378. SECTION("const std::string&, NestingLimit, Filter") {
  1379. deserializeMsgPack(doc, "{}"_s, NestingLimit(5), Filter(filter));
  1380. }
  1381. SECTION("std::istream&, NestingLimit, Filter") {
  1382. std::stringstream s("{}");
  1383. deserializeMsgPack(doc, s, NestingLimit(5), Filter(filter));
  1384. }
  1385. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  1386. SECTION("char[n], NestingLimit, Filter") {
  1387. size_t i = 4;
  1388. char vla[i];
  1389. strcpy(vla, "{}");
  1390. deserializeMsgPack(doc, vla, NestingLimit(5), Filter(filter));
  1391. }
  1392. #endif
  1393. }