IOBufCursorTest.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/io/IOBuf.h>
  17. #include <folly/Format.h>
  18. #include <folly/Range.h>
  19. #include <folly/io/Cursor.h>
  20. #include <folly/portability/GTest.h>
  21. #include <numeric>
  22. #include <vector>
  23. using folly::ByteRange;
  24. using folly::format;
  25. using folly::IOBuf;
  26. using folly::StringPiece;
  27. using std::unique_ptr;
  28. using namespace folly::io;
  29. TEST(IOBuf, RWCursor) {
  30. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  31. iobuf1->append(20);
  32. unique_ptr<IOBuf> iobuf2(IOBuf::create(20));
  33. iobuf2->append(20);
  34. iobuf2.get();
  35. iobuf1->prependChain(std::move(iobuf2));
  36. EXPECT_TRUE(iobuf1->isChained());
  37. RWPrivateCursor wcursor(iobuf1.get());
  38. Cursor rcursor(iobuf1.get());
  39. wcursor.writeLE((uint64_t)1);
  40. wcursor.writeLE((uint64_t)1);
  41. wcursor.writeLE((uint64_t)1);
  42. wcursor.write((uint8_t)1);
  43. EXPECT_EQ(1u, rcursor.readLE<uint64_t>());
  44. rcursor.skip(8);
  45. EXPECT_EQ(1u, rcursor.readLE<uint32_t>());
  46. rcursor.skip(0);
  47. EXPECT_EQ(0u, rcursor.read<uint8_t>());
  48. EXPECT_EQ(0u, rcursor.read<uint8_t>());
  49. EXPECT_EQ(0u, rcursor.read<uint8_t>());
  50. EXPECT_EQ(0u, rcursor.read<uint8_t>());
  51. EXPECT_EQ(1u, rcursor.read<uint8_t>());
  52. }
  53. TEST(IOBuf, skip) {
  54. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  55. iobuf1->append(20);
  56. RWPrivateCursor wcursor(iobuf1.get());
  57. wcursor.write((uint8_t)1);
  58. wcursor.write((uint8_t)2);
  59. Cursor cursor(iobuf1.get());
  60. cursor.skip(1);
  61. EXPECT_EQ(2, cursor.read<uint8_t>());
  62. }
  63. TEST(IOBuf, reset) {
  64. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  65. iobuf1->append(20);
  66. RWPrivateCursor wcursor(iobuf1.get());
  67. wcursor.write((uint8_t)1);
  68. wcursor.write((uint8_t)2);
  69. wcursor.reset(iobuf1.get());
  70. EXPECT_EQ(1, wcursor.read<uint8_t>());
  71. }
  72. TEST(IOBuf, copy_assign_convert) {
  73. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  74. iobuf1->append(20);
  75. RWPrivateCursor wcursor(iobuf1.get());
  76. RWPrivateCursor cursor2(wcursor);
  77. RWPrivateCursor cursor3(iobuf1.get());
  78. wcursor.write((uint8_t)1);
  79. cursor3 = wcursor;
  80. wcursor.write((uint8_t)2);
  81. Cursor cursor4(wcursor);
  82. RWPrivateCursor cursor5(wcursor);
  83. wcursor.write((uint8_t)3);
  84. EXPECT_EQ(1, cursor2.read<uint8_t>());
  85. EXPECT_EQ(2, cursor3.read<uint8_t>());
  86. EXPECT_EQ(3, cursor4.read<uint8_t>());
  87. }
  88. TEST(IOBuf, arithmetic) {
  89. IOBuf iobuf1(IOBuf::CREATE, 20);
  90. iobuf1.append(20);
  91. RWPrivateCursor wcursor(&iobuf1);
  92. wcursor += 1;
  93. wcursor.write((uint8_t)1);
  94. Cursor cursor(&iobuf1);
  95. cursor += 1;
  96. EXPECT_EQ(1, cursor.read<uint8_t>());
  97. Cursor start(&iobuf1);
  98. Cursor cursor2 = start + 9;
  99. EXPECT_EQ(7, cursor2 - cursor);
  100. EXPECT_NE(cursor, cursor2);
  101. cursor += 8;
  102. cursor2 = cursor2 + 1;
  103. EXPECT_EQ(cursor, cursor2);
  104. }
  105. TEST(IOBuf, endian) {
  106. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  107. iobuf1->append(20);
  108. RWPrivateCursor wcursor(iobuf1.get());
  109. Cursor rcursor(iobuf1.get());
  110. uint16_t v = 1;
  111. int16_t vu = -1;
  112. wcursor.writeBE(v);
  113. wcursor.writeBE(vu);
  114. // Try a couple combinations to ensure they were generated correctly
  115. wcursor.writeBE(vu);
  116. wcursor.writeLE(vu);
  117. wcursor.writeLE(vu);
  118. wcursor.writeLE(v);
  119. EXPECT_EQ(v, rcursor.readBE<uint16_t>());
  120. }
  121. TEST(IOBuf, Cursor) {
  122. unique_ptr<IOBuf> iobuf1(IOBuf::create(1));
  123. iobuf1->append(1);
  124. RWPrivateCursor c(iobuf1.get());
  125. c.write((uint8_t)40); // OK
  126. try {
  127. c.write((uint8_t)10); // Bad write, checked should except.
  128. ADD_FAILURE();
  129. } catch (...) {
  130. }
  131. }
  132. TEST(IOBuf, UnshareCursor) {
  133. uint8_t buf = 0;
  134. unique_ptr<IOBuf> iobuf1(IOBuf::wrapBuffer(&buf, 1));
  135. unique_ptr<IOBuf> iobuf2(IOBuf::wrapBuffer(&buf, 1));
  136. RWUnshareCursor c1(iobuf1.get());
  137. RWUnshareCursor c2(iobuf2.get());
  138. c1.write((uint8_t)10); // This should duplicate the two buffers.
  139. uint8_t t = c2.read<uint8_t>();
  140. EXPECT_EQ(0, t);
  141. iobuf1 = IOBuf::wrapBuffer(&buf, 1);
  142. iobuf2 = IOBuf::wrapBuffer(&buf, 1);
  143. RWPrivateCursor c3(iobuf1.get());
  144. RWPrivateCursor c4(iobuf2.get());
  145. c3.write((uint8_t)10); // This should _not_ duplicate the two buffers.
  146. t = c4.read<uint8_t>();
  147. EXPECT_EQ(10, t);
  148. }
  149. namespace {
  150. void append(std::unique_ptr<IOBuf>& buf, folly::StringPiece data) {
  151. EXPECT_LE(data.size(), buf->tailroom());
  152. memcpy(buf->writableData(), data.data(), data.size());
  153. buf->append(data.size());
  154. }
  155. void append(Appender& appender, StringPiece data) {
  156. appender.push(ByteRange(data));
  157. }
  158. std::string toString(const IOBuf& buf) {
  159. std::string str;
  160. Cursor cursor(&buf);
  161. ByteRange b;
  162. while (!(b = cursor.peekBytes()).empty()) {
  163. str.append(reinterpret_cast<const char*>(b.data()), b.size());
  164. cursor.skip(b.size());
  165. }
  166. return str;
  167. }
  168. } // namespace
  169. TEST(IOBuf, PullAndPeek) {
  170. std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
  171. append(iobuf1, "he");
  172. std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
  173. append(iobuf2, "llo ");
  174. std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
  175. append(iobuf3, "world");
  176. iobuf1->prependChain(std::move(iobuf2));
  177. iobuf1->prependChain(std::move(iobuf3));
  178. EXPECT_EQ(3, iobuf1->countChainElements());
  179. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  180. char buf[12];
  181. memset(buf, 0, sizeof(buf));
  182. Cursor(iobuf1.get()).pull(buf, 11);
  183. EXPECT_EQ("hello world", std::string(buf));
  184. memset(buf, 0, sizeof(buf));
  185. EXPECT_EQ(11, Cursor(iobuf1.get()).pullAtMost(buf, 20));
  186. EXPECT_EQ("hello world", std::string(buf));
  187. EXPECT_THROW({ Cursor(iobuf1.get()).pull(buf, 20); }, std::out_of_range);
  188. {
  189. RWPrivateCursor cursor(iobuf1.get());
  190. auto b = cursor.peekBytes();
  191. EXPECT_EQ("he", StringPiece(b));
  192. cursor.skip(b.size());
  193. b = cursor.peekBytes();
  194. EXPECT_EQ("llo ", StringPiece(b));
  195. cursor.skip(b.size());
  196. b = cursor.peekBytes();
  197. EXPECT_EQ("world", StringPiece(b));
  198. cursor.skip(b.size());
  199. EXPECT_EQ(3, iobuf1->countChainElements());
  200. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  201. }
  202. {
  203. RWPrivateCursor cursor(iobuf1.get());
  204. cursor.gather(11);
  205. auto b = cursor.peekBytes();
  206. EXPECT_EQ("hello world", StringPiece(b));
  207. EXPECT_EQ(1, iobuf1->countChainElements());
  208. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  209. }
  210. }
  211. TEST(IOBuf, pushCursorData) {
  212. unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
  213. iobuf1->append(15);
  214. iobuf1->trimStart(5);
  215. unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
  216. unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
  217. iobuf3->append(10);
  218. iobuf1->prependChain(std::move(iobuf2));
  219. iobuf1->prependChain(std::move(iobuf3));
  220. EXPECT_TRUE(iobuf1->isChained());
  221. // write 20 bytes to the buffer chain
  222. RWPrivateCursor wcursor(iobuf1.get());
  223. EXPECT_FALSE(wcursor.isAtEnd());
  224. wcursor.writeBE<uint64_t>(1);
  225. wcursor.writeBE<uint64_t>(10);
  226. wcursor.writeBE<uint32_t>(20);
  227. EXPECT_TRUE(wcursor.isAtEnd());
  228. // create a read buffer for the buffer chain
  229. Cursor rcursor(iobuf1.get());
  230. EXPECT_EQ(1, rcursor.readBE<uint64_t>());
  231. EXPECT_EQ(10, rcursor.readBE<uint64_t>());
  232. EXPECT_EQ(20, rcursor.readBE<uint32_t>());
  233. EXPECT_EQ(0, rcursor.totalLength());
  234. rcursor.reset(iobuf1.get());
  235. EXPECT_EQ(20, rcursor.totalLength());
  236. // create another write buffer
  237. unique_ptr<IOBuf> iobuf4(IOBuf::create(30));
  238. iobuf4->append(30);
  239. RWPrivateCursor wcursor2(iobuf4.get());
  240. // write buffer chain data into it, now wcursor2 should only
  241. // have 10 bytes writable space
  242. wcursor2.push(rcursor, 20);
  243. EXPECT_EQ(wcursor2.totalLength(), 10);
  244. // write again with not enough space in rcursor
  245. EXPECT_THROW(wcursor2.push(rcursor, 20), std::out_of_range);
  246. // create a read cursor to check iobuf3 data back
  247. Cursor rcursor2(iobuf4.get());
  248. EXPECT_EQ(1, rcursor2.readBE<uint64_t>());
  249. EXPECT_EQ(10, rcursor2.readBE<uint64_t>());
  250. EXPECT_EQ(20, rcursor2.readBE<uint32_t>());
  251. }
  252. TEST(IOBuf, Gather) {
  253. std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
  254. append(iobuf1, "he");
  255. std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
  256. append(iobuf2, "llo ");
  257. std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
  258. append(iobuf3, "world");
  259. iobuf1->prependChain(std::move(iobuf2));
  260. iobuf1->prependChain(std::move(iobuf3));
  261. EXPECT_EQ(3, iobuf1->countChainElements());
  262. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  263. // Attempting to gather() more data than available in the chain should fail.
  264. // Try from the very beginning of the chain.
  265. RWPrivateCursor cursor(iobuf1.get());
  266. EXPECT_THROW(cursor.gather(15), std::overflow_error);
  267. // Now try from the middle of the chain
  268. cursor += 3;
  269. EXPECT_THROW(cursor.gather(10), std::overflow_error);
  270. // Calling gatherAtMost() should succeed, however, and just gather
  271. // as much as it can
  272. cursor.gatherAtMost(10);
  273. EXPECT_EQ(8, cursor.length());
  274. EXPECT_EQ(8, cursor.totalLength());
  275. EXPECT_FALSE(cursor.isAtEnd());
  276. EXPECT_EQ(
  277. "lo world",
  278. folly::StringPiece(
  279. reinterpret_cast<const char*>(cursor.data()), cursor.length()));
  280. EXPECT_EQ(2, iobuf1->countChainElements());
  281. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  282. // Now try gather again on the chain head
  283. cursor = RWPrivateCursor(iobuf1.get());
  284. cursor.gather(5);
  285. // Since gather() doesn't split buffers, everything should be collapsed into
  286. // a single buffer now.
  287. EXPECT_EQ(1, iobuf1->countChainElements());
  288. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  289. EXPECT_EQ(11, cursor.length());
  290. EXPECT_EQ(11, cursor.totalLength());
  291. }
  292. TEST(IOBuf, cloneAndInsert) {
  293. std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
  294. append(iobuf1, "he");
  295. std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
  296. append(iobuf2, "llo ");
  297. std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
  298. append(iobuf3, "world");
  299. iobuf1->prependChain(std::move(iobuf2));
  300. iobuf1->prependChain(std::move(iobuf3));
  301. EXPECT_EQ(3, iobuf1->countChainElements());
  302. EXPECT_EQ(11, iobuf1->computeChainDataLength());
  303. std::unique_ptr<IOBuf> cloned;
  304. Cursor(iobuf1.get()).clone(cloned, 3);
  305. EXPECT_EQ(2, cloned->countChainElements());
  306. EXPECT_EQ(3, cloned->computeChainDataLength());
  307. EXPECT_EQ(11, Cursor(iobuf1.get()).cloneAtMost(cloned, 20));
  308. EXPECT_EQ(3, cloned->countChainElements());
  309. EXPECT_EQ(11, cloned->computeChainDataLength());
  310. EXPECT_THROW({ Cursor(iobuf1.get()).clone(cloned, 20); }, std::out_of_range);
  311. {
  312. // Check that inserting in the middle of an iobuf splits
  313. RWPrivateCursor cursor(iobuf1.get());
  314. Cursor(iobuf1.get()).clone(cloned, 3);
  315. EXPECT_EQ(2, cloned->countChainElements());
  316. EXPECT_EQ(3, cloned->computeChainDataLength());
  317. cursor.skip(1);
  318. cursor.insert(std::move(cloned));
  319. cursor.insert(folly::IOBuf::create(0));
  320. EXPECT_EQ(4, cursor.getCurrentPosition());
  321. EXPECT_EQ(7, iobuf1->countChainElements());
  322. EXPECT_EQ(14, iobuf1->computeChainDataLength());
  323. // Check that nextBuf got set correctly to the buffer with 1 byte left
  324. EXPECT_EQ(1, cursor.peekBytes().size());
  325. cursor.read<uint8_t>();
  326. }
  327. {
  328. // Check that inserting at the end doesn't create empty buf
  329. RWPrivateCursor cursor(iobuf1.get());
  330. Cursor(iobuf1.get()).clone(cloned, 1);
  331. EXPECT_EQ(1, cloned->countChainElements());
  332. EXPECT_EQ(1, cloned->computeChainDataLength());
  333. cursor.skip(1);
  334. cursor.insert(std::move(cloned));
  335. EXPECT_EQ(2, cursor.getCurrentPosition());
  336. EXPECT_EQ(8, iobuf1->countChainElements());
  337. EXPECT_EQ(15, iobuf1->computeChainDataLength());
  338. // Check that nextBuf got set correctly
  339. cursor.read<uint8_t>();
  340. }
  341. {
  342. // Check that inserting at the beginning of a chunk (except first one)
  343. // doesn't create empty buf
  344. RWPrivateCursor cursor(iobuf1.get());
  345. Cursor(iobuf1.get()).clone(cloned, 1);
  346. EXPECT_EQ(1, cloned->countChainElements());
  347. EXPECT_EQ(1, cloned->computeChainDataLength());
  348. cursor.skip(1);
  349. cursor.insert(std::move(cloned));
  350. EXPECT_EQ(2, cursor.getCurrentPosition());
  351. EXPECT_EQ(14, cursor.totalLength());
  352. EXPECT_EQ(9, iobuf1->countChainElements());
  353. EXPECT_EQ(16, iobuf1->computeChainDataLength());
  354. // Check that nextBuf got set correctly
  355. cursor.read<uint8_t>();
  356. }
  357. {
  358. // Check that inserting at the beginning of a chain DOES keep an empty
  359. // buffer.
  360. RWPrivateCursor cursor(iobuf1.get());
  361. Cursor(iobuf1.get()).clone(cloned, 1);
  362. EXPECT_EQ(1, cloned->countChainElements());
  363. EXPECT_EQ(1, cloned->computeChainDataLength());
  364. cursor.insert(std::move(cloned));
  365. EXPECT_EQ(1, cursor.getCurrentPosition());
  366. EXPECT_EQ(16, cursor.totalLength());
  367. EXPECT_EQ(11, iobuf1->countChainElements());
  368. EXPECT_EQ(17, iobuf1->computeChainDataLength());
  369. // Check that nextBuf got set correctly
  370. cursor.read<uint8_t>();
  371. }
  372. {
  373. // Check that inserting at the end of the buffer keeps it at the end.
  374. RWPrivateCursor cursor(iobuf1.get());
  375. Cursor(iobuf1.get()).clone(cloned, 1);
  376. EXPECT_EQ(1, cloned->countChainElements());
  377. EXPECT_EQ(1, cloned->computeChainDataLength());
  378. cursor.advanceToEnd();
  379. EXPECT_EQ(17, cursor.getCurrentPosition());
  380. cursor.insert(std::move(cloned));
  381. EXPECT_EQ(18, cursor.getCurrentPosition());
  382. EXPECT_EQ(0, cursor.totalLength());
  383. EXPECT_EQ(12, iobuf1->countChainElements());
  384. EXPECT_EQ(18, iobuf1->computeChainDataLength());
  385. EXPECT_TRUE(cursor.isAtEnd());
  386. }
  387. }
  388. TEST(IOBuf, cloneWithEmptyBufAtStart) {
  389. folly::IOBufEqualTo eq;
  390. auto empty = IOBuf::create(0);
  391. auto hel = IOBuf::create(3);
  392. append(hel, "hel");
  393. auto lo = IOBuf::create(2);
  394. append(lo, "lo");
  395. auto iobuf = empty->clone();
  396. iobuf->prependChain(hel->clone());
  397. iobuf->prependChain(lo->clone());
  398. iobuf->prependChain(empty->clone());
  399. iobuf->prependChain(hel->clone());
  400. iobuf->prependChain(lo->clone());
  401. iobuf->prependChain(empty->clone());
  402. iobuf->prependChain(lo->clone());
  403. iobuf->prependChain(hel->clone());
  404. iobuf->prependChain(lo->clone());
  405. iobuf->prependChain(lo->clone());
  406. Cursor cursor(iobuf.get());
  407. std::unique_ptr<IOBuf> cloned;
  408. char data[3];
  409. cursor.pull(&data, 3);
  410. cursor.clone(cloned, 2);
  411. EXPECT_EQ(1, cloned->countChainElements());
  412. EXPECT_EQ(2, cloned->length());
  413. EXPECT_TRUE(eq(lo, cloned));
  414. cursor.pull(&data, 3);
  415. EXPECT_EQ("hel", std::string(data, sizeof(data)));
  416. cursor.skip(2);
  417. cursor.clone(cloned, 2);
  418. EXPECT_TRUE(eq(lo, cloned));
  419. std::string hello = cursor.readFixedString(5);
  420. cursor.clone(cloned, 2);
  421. EXPECT_TRUE(eq(lo, cloned));
  422. }
  423. TEST(IOBuf, Appender) {
  424. std::unique_ptr<IOBuf> head(IOBuf::create(10));
  425. append(head, "hello");
  426. Appender app(head.get(), 10);
  427. auto cap = head->capacity();
  428. auto len1 = app.length();
  429. EXPECT_EQ(cap - 5, len1);
  430. app.ensure(len1); // won't grow
  431. EXPECT_EQ(len1, app.length());
  432. app.ensure(len1 + 1); // will grow
  433. EXPECT_LE(len1 + 1, app.length());
  434. append(app, " world");
  435. EXPECT_EQ("hello world", toString(*head));
  436. }
  437. TEST(IOBuf, Printf) {
  438. IOBuf head(IOBuf::CREATE, 24);
  439. Appender app(&head, 32);
  440. app.printf("%s", "test");
  441. EXPECT_EQ(head.length(), 4);
  442. EXPECT_EQ(0, memcmp(head.data(), "test\0", 5));
  443. app.printf(
  444. "%d%s %s%s %#x",
  445. 32,
  446. "this string is",
  447. "longer than our original allocation size,",
  448. "and will therefore require a new allocation",
  449. 0x12345678);
  450. // The tailroom should start with a nul byte now.
  451. EXPECT_GE(head.prev()->tailroom(), 1u);
  452. EXPECT_EQ(0, *head.prev()->tail());
  453. EXPECT_EQ(
  454. "test32this string is longer than our original "
  455. "allocation size,and will therefore require a "
  456. "new allocation 0x12345678",
  457. head.moveToFbString().toStdString());
  458. }
  459. TEST(IOBuf, Format) {
  460. IOBuf head(IOBuf::CREATE, 24);
  461. Appender app(&head, 32);
  462. format("{}", "test")(app);
  463. EXPECT_EQ(head.length(), 4);
  464. EXPECT_EQ(0, memcmp(head.data(), "test", 4));
  465. auto fmt = format(
  466. "{}{} {}{} {:#x}",
  467. 32,
  468. "this string is",
  469. "longer than our original allocation size,",
  470. "and will therefore require a new allocation",
  471. 0x12345678);
  472. fmt(app);
  473. EXPECT_EQ(
  474. "test32this string is longer than our original "
  475. "allocation size,and will therefore require a "
  476. "new allocation 0x12345678",
  477. head.moveToFbString().toStdString());
  478. }
  479. TEST(IOBuf, QueueAppender) {
  480. folly::IOBufQueue queue;
  481. // Allocate 100 bytes at once, but don't grow past 1024
  482. QueueAppender app(&queue, 100);
  483. size_t n = 1024 / sizeof(uint32_t);
  484. for (uint32_t i = 0; i < n; ++i) {
  485. app.writeBE(i);
  486. }
  487. // There must be a goodMallocSize between 100 and 1024...
  488. EXPECT_LT(1u, queue.front()->countChainElements());
  489. const IOBuf* buf = queue.front();
  490. do {
  491. EXPECT_LE(100u, buf->capacity());
  492. buf = buf->next();
  493. } while (buf != queue.front());
  494. Cursor cursor(queue.front());
  495. for (uint32_t i = 0; i < n; ++i) {
  496. EXPECT_EQ(i, cursor.readBE<uint32_t>());
  497. }
  498. EXPECT_THROW({ cursor.readBE<uint32_t>(); }, std::out_of_range);
  499. }
  500. TEST(IOBuf, QueueAppenderPushAtMostFillBuffer) {
  501. folly::IOBufQueue queue;
  502. // There should be a goodMallocSize between 125 and 1000
  503. QueueAppender appender{&queue, 125};
  504. std::vector<uint8_t> data;
  505. data.resize(1000);
  506. std::iota(data.begin(), data.end(), uint8_t(0));
  507. // Add 100 byte
  508. appender.pushAtMost(data.data(), 100);
  509. // Add 900 bytes
  510. appender.pushAtMost(data.data() + 100, data.size() - 100);
  511. const auto buf = queue.front();
  512. // Should fill the current buffer before adding another
  513. EXPECT_LE(2, buf->countChainElements());
  514. EXPECT_EQ(0, buf->tailroom());
  515. EXPECT_LE(125, buf->length());
  516. EXPECT_EQ(1000, buf->computeChainDataLength());
  517. const StringPiece sp{(const char*)data.data(), data.size()};
  518. EXPECT_EQ(sp, toString(*buf));
  519. }
  520. TEST(IOBuf, QueueAppenderInsertOwn) {
  521. auto buf = IOBuf::create(10);
  522. folly::IOBufQueue queue;
  523. QueueAppender appender{&queue, 128};
  524. appender.insert(std::move(buf));
  525. std::vector<uint8_t> data;
  526. data.resize(256);
  527. std::iota(data.begin(), data.end(), 0);
  528. appender.pushAtMost(folly::range(data));
  529. // Buffer is owned, so we should write to it
  530. EXPECT_LE(2, queue.front()->countChainElements());
  531. EXPECT_EQ(0, queue.front()->tailroom());
  532. const StringPiece sp{(const char*)data.data(), data.size()};
  533. EXPECT_EQ(sp, toString(*queue.front()));
  534. }
  535. TEST(IOBuf, QueueAppenderInsertClone) {
  536. IOBuf buf{IOBuf::CREATE, 100};
  537. folly::IOBufQueue queue;
  538. QueueAppender appender{&queue, 100};
  539. // Buffer is shared, so we create a new buffer to write to
  540. appender.insert(buf);
  541. uint8_t x = 42;
  542. appender.pushAtMost(&x, 1);
  543. EXPECT_EQ(2, queue.front()->countChainElements());
  544. EXPECT_EQ(0, queue.front()->length());
  545. EXPECT_LT(0, queue.front()->tailroom());
  546. EXPECT_EQ(1, queue.front()->next()->length());
  547. EXPECT_EQ(x, queue.front()->next()->data()[0]);
  548. }
  549. TEST(IOBuf, CursorOperators) {
  550. // Test operators on a single-item chain
  551. {
  552. std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
  553. chain1->append(10);
  554. Cursor curs1(chain1.get());
  555. EXPECT_EQ(0, curs1 - chain1.get());
  556. EXPECT_FALSE(curs1.isAtEnd());
  557. curs1.skip(3);
  558. EXPECT_EQ(3, curs1 - chain1.get());
  559. EXPECT_FALSE(curs1.isAtEnd());
  560. curs1.skip(7);
  561. EXPECT_EQ(10, curs1 - chain1.get());
  562. EXPECT_TRUE(curs1.isAtEnd());
  563. Cursor curs2(chain1.get());
  564. EXPECT_EQ(0, curs2 - chain1.get());
  565. EXPECT_EQ(10, curs1 - curs2);
  566. EXPECT_THROW(curs2 - curs1, std::out_of_range);
  567. }
  568. // Test cross-chain operations
  569. {
  570. std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
  571. chain1->append(10);
  572. std::unique_ptr<IOBuf> chain2 = chain1->clone();
  573. Cursor curs1(chain1.get());
  574. Cursor curs2(chain2.get());
  575. EXPECT_THROW(curs1 - curs2, std::out_of_range);
  576. EXPECT_THROW(curs1 - chain2.get(), std::out_of_range);
  577. }
  578. // Test operations on multi-item chains
  579. {
  580. std::unique_ptr<IOBuf> chain(IOBuf::create(20));
  581. chain->append(10);
  582. chain->appendChain(chain->clone());
  583. EXPECT_EQ(20, chain->computeChainDataLength());
  584. Cursor curs1(chain.get());
  585. curs1.skip(5);
  586. Cursor curs2(chain.get());
  587. curs2.skip(3);
  588. EXPECT_EQ(2, curs1 - curs2);
  589. EXPECT_EQ(5, curs1 - chain.get());
  590. EXPECT_THROW(curs2 - curs1, std::out_of_range);
  591. curs1.skip(7);
  592. EXPECT_EQ(9, curs1 - curs2);
  593. EXPECT_EQ(12, curs1 - chain.get());
  594. EXPECT_THROW(curs2 - curs1, std::out_of_range);
  595. curs2.skip(7);
  596. EXPECT_EQ(2, curs1 - curs2);
  597. EXPECT_THROW(curs2 - curs1, std::out_of_range);
  598. }
  599. // Test isAtEnd() with empty buffers at the end of a chain
  600. {
  601. auto iobuf1 = IOBuf::create(20);
  602. iobuf1->append(15);
  603. iobuf1->trimStart(5);
  604. Cursor c(iobuf1.get());
  605. EXPECT_FALSE(c.isAtEnd());
  606. c.skip(10);
  607. EXPECT_TRUE(c.isAtEnd());
  608. iobuf1->prependChain(IOBuf::create(10));
  609. iobuf1->prependChain(IOBuf::create(10));
  610. EXPECT_TRUE(c.isAtEnd());
  611. iobuf1->prev()->append(5);
  612. EXPECT_FALSE(c.isAtEnd());
  613. c.skip(5);
  614. EXPECT_TRUE(c.isAtEnd());
  615. }
  616. // Test canAdvance with a chain of items
  617. {
  618. auto chain = IOBuf::create(10);
  619. chain->append(10);
  620. chain->appendChain(chain->clone());
  621. EXPECT_EQ(2, chain->countChainElements());
  622. EXPECT_EQ(20, chain->computeChainDataLength());
  623. Cursor c(chain.get());
  624. for (size_t i = 0; i <= 20; ++i) {
  625. EXPECT_TRUE(c.canAdvance(i));
  626. }
  627. EXPECT_FALSE(c.canAdvance(21));
  628. c.skip(10);
  629. EXPECT_TRUE(c.canAdvance(10));
  630. EXPECT_FALSE(c.canAdvance(11));
  631. }
  632. }
  633. TEST(IOBuf, StringOperations) {
  634. // Test a single buffer with two null-terminated strings and an extra uint8_t
  635. // at the end
  636. {
  637. std::unique_ptr<IOBuf> chain(IOBuf::create(16));
  638. Appender app(chain.get(), 0);
  639. app.push(reinterpret_cast<const uint8_t*>("hello\0world\0\x01"), 13);
  640. Cursor curs(chain.get());
  641. EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
  642. EXPECT_STREQ("world", curs.readTerminatedString().c_str());
  643. EXPECT_EQ(1, curs.read<uint8_t>());
  644. }
  645. // Test multiple buffers where the first is empty and the string starts in
  646. // the second buffer.
  647. {
  648. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  649. chain->prependChain(IOBuf::create(12));
  650. Appender app(chain.get(), 0);
  651. app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
  652. Cursor curs(chain.get());
  653. EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
  654. }
  655. // Test multiple buffers with a single null-terminated string spanning them
  656. {
  657. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  658. chain->prependChain(IOBuf::create(8));
  659. chain->append(8);
  660. chain->next()->append(4);
  661. RWPrivateCursor rwc(chain.get());
  662. rwc.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
  663. Cursor curs(chain.get());
  664. EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
  665. }
  666. // Test a reading a null-terminated string that's longer than the maximum
  667. // allowable length
  668. {
  669. std::unique_ptr<IOBuf> chain(IOBuf::create(16));
  670. Appender app(chain.get(), 0);
  671. app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
  672. Cursor curs(chain.get());
  673. EXPECT_THROW(curs.readTerminatedString('\0', 5), std::length_error);
  674. }
  675. // Test reading a null-terminated string from a chain with an empty buffer at
  676. // the front
  677. {
  678. std::unique_ptr<IOBuf> buf(IOBuf::create(8));
  679. Appender app(buf.get(), 0);
  680. app.push(reinterpret_cast<const uint8_t*>("hello\0"), 6);
  681. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  682. chain->prependChain(std::move(buf));
  683. Cursor curs(chain.get());
  684. EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
  685. }
  686. // Test reading a null-terminated string from a chain that doesn't contain the
  687. // terminator
  688. {
  689. std::unique_ptr<IOBuf> buf(IOBuf::create(8));
  690. Appender app(buf.get(), 0);
  691. app.push(reinterpret_cast<const uint8_t*>("hello"), 5);
  692. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  693. chain->prependChain(std::move(buf));
  694. Cursor curs(chain.get());
  695. EXPECT_THROW(curs.readTerminatedString(), std::out_of_range);
  696. }
  697. // Test reading a null-terminated string past the maximum length
  698. {
  699. std::unique_ptr<IOBuf> buf(IOBuf::create(8));
  700. Appender app(buf.get(), 0);
  701. app.push(reinterpret_cast<const uint8_t*>("hello\0"), 6);
  702. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  703. chain->prependChain(std::move(buf));
  704. Cursor curs(chain.get());
  705. EXPECT_THROW(curs.readTerminatedString('\0', 3), std::length_error);
  706. }
  707. // Test reading a two fixed-length strings from a single buffer with an extra
  708. // uint8_t at the end
  709. {
  710. std::unique_ptr<IOBuf> chain(IOBuf::create(16));
  711. Appender app(chain.get(), 0);
  712. app.push(reinterpret_cast<const uint8_t*>("helloworld\x01"), 11);
  713. Cursor curs(chain.get());
  714. EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
  715. EXPECT_STREQ("world", curs.readFixedString(5).c_str());
  716. EXPECT_EQ(1, curs.read<uint8_t>());
  717. }
  718. // Test multiple buffers where the first is empty and a fixed-length string
  719. // starts in the second buffer.
  720. {
  721. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  722. chain->prependChain(IOBuf::create(16));
  723. Appender app(chain.get(), 0);
  724. app.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
  725. Cursor curs(chain.get());
  726. EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
  727. }
  728. // Test multiple buffers with a single fixed-length string spanning them
  729. {
  730. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  731. chain->prependChain(IOBuf::create(8));
  732. chain->append(7);
  733. chain->next()->append(4);
  734. RWPrivateCursor rwc(chain.get());
  735. rwc.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
  736. Cursor curs(chain.get());
  737. EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
  738. }
  739. // Test reading a fixed-length string from a chain with an empty buffer at
  740. // the front
  741. {
  742. std::unique_ptr<IOBuf> buf(IOBuf::create(8));
  743. Appender app(buf.get(), 0);
  744. app.push(reinterpret_cast<const uint8_t*>("hello"), 5);
  745. std::unique_ptr<IOBuf> chain(IOBuf::create(8));
  746. chain->prependChain(std::move(buf));
  747. Cursor curs(chain.get());
  748. EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
  749. }
  750. }
  751. TEST(IOBuf, ReadWhileTrue) {
  752. auto isAlpha = [](uint8_t ch) {
  753. return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
  754. };
  755. auto isDigit = [](uint8_t ch) { return (ch >= '0' && ch <= '9'); };
  756. // Test reading alternating alphabetic and numeric strings
  757. {
  758. std::unique_ptr<IOBuf> chain(IOBuf::create(32));
  759. Appender app(chain.get(), 0);
  760. app.push(StringPiece("hello123world456"));
  761. Cursor curs(chain.get());
  762. EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
  763. EXPECT_STREQ("123", curs.readWhile(isDigit).c_str());
  764. EXPECT_STREQ("world", curs.readWhile(isAlpha).c_str());
  765. EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
  766. EXPECT_TRUE(curs.isAtEnd());
  767. }
  768. // The same, but also use skipWhile()
  769. {
  770. std::unique_ptr<IOBuf> chain(IOBuf::create(16));
  771. Appender app(chain.get(), 0);
  772. app.push(StringPiece("hello123world456"));
  773. Cursor curs(chain.get());
  774. EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
  775. curs.skipWhile(isDigit);
  776. curs.skipWhile(isAlpha);
  777. EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
  778. EXPECT_TRUE(curs.isAtEnd());
  779. }
  780. // Test readWhile() using data split across multiple buffers,
  781. // including some empty buffers in the middle of the chain.
  782. {
  783. std::unique_ptr<IOBuf> chain;
  784. // First element in the chain has "he"
  785. auto buf = IOBuf::create(40);
  786. Appender app(buf.get(), 0);
  787. app.push(StringPiece("he"));
  788. chain = std::move(buf);
  789. // The second element has "ll", after 10 bytes of headroom
  790. buf = IOBuf::create(40);
  791. buf->advance(10);
  792. app = Appender{buf.get(), 0};
  793. app.push(StringPiece("ll"));
  794. chain->prependChain(std::move(buf));
  795. // The third element is empty
  796. buf = IOBuf::create(40);
  797. buf->advance(15);
  798. chain->prependChain(std::move(buf));
  799. // The fourth element has "o12"
  800. buf = IOBuf::create(40);
  801. buf->advance(37);
  802. app = Appender{buf.get(), 0};
  803. app.push(StringPiece("o12"));
  804. chain->prependChain(std::move(buf));
  805. // The fifth element has "3"
  806. buf = IOBuf::create(40);
  807. app = Appender{buf.get(), 0};
  808. app.push(StringPiece("3"));
  809. chain->prependChain(std::move(buf));
  810. // The sixth element is empty
  811. buf = IOBuf::create(40);
  812. chain->prependChain(std::move(buf));
  813. // The seventh element has "world456"
  814. buf = IOBuf::create(40);
  815. app = Appender{buf.get(), 0};
  816. app.push(StringPiece("world456"));
  817. chain->prependChain(std::move(buf));
  818. // The eighth element is empty
  819. buf = IOBuf::create(40);
  820. chain->prependChain(std::move(buf));
  821. Cursor curs(chain.get());
  822. EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
  823. EXPECT_STREQ("123", curs.readWhile(isDigit).c_str());
  824. EXPECT_STREQ("world", curs.readWhile(isAlpha).c_str());
  825. EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
  826. EXPECT_TRUE(curs.isAtEnd());
  827. }
  828. }
  829. TEST(IOBuf, TestAdvanceToEndSingle) {
  830. std::unique_ptr<IOBuf> chain(IOBuf::create(10));
  831. chain->append(10);
  832. Cursor curs(chain.get());
  833. curs.advanceToEnd();
  834. EXPECT_TRUE(curs.isAtEnd());
  835. EXPECT_EQ(curs - chain.get(), 10);
  836. }
  837. TEST(IOBuf, TestAdvanceToEndMulti) {
  838. std::unique_ptr<IOBuf> chain(IOBuf::create(10));
  839. chain->append(10);
  840. std::unique_ptr<IOBuf> buf(IOBuf::create(5));
  841. buf->append(5);
  842. chain->prependChain(std::move(buf));
  843. buf = IOBuf::create(20);
  844. buf->append(20);
  845. chain->prependChain(std::move(buf));
  846. Cursor curs(chain.get());
  847. curs.advanceToEnd();
  848. EXPECT_TRUE(curs.isAtEnd());
  849. EXPECT_EQ(curs - chain.get(), 35);
  850. curs.reset(chain.get());
  851. curs.skip(12);
  852. curs.advanceToEnd();
  853. EXPECT_TRUE(curs.isAtEnd());
  854. }
  855. TEST(IOBuf, TestRetreatSingle) {
  856. std::unique_ptr<IOBuf> chain(IOBuf::create(20));
  857. chain->append(20);
  858. Cursor curs(chain.get());
  859. EXPECT_EQ(curs.retreatAtMost(0), 0);
  860. EXPECT_EQ(curs.totalLength(), 20);
  861. EXPECT_EQ(curs.retreatAtMost(5), 0);
  862. EXPECT_EQ(curs.totalLength(), 20);
  863. EXPECT_EQ(curs.retreatAtMost(25), 0);
  864. EXPECT_EQ(curs.totalLength(), 20);
  865. curs.retreat(0);
  866. EXPECT_THROW(curs.retreat(5), std::out_of_range);
  867. curs.reset(chain.get());
  868. EXPECT_THROW(curs.retreat(25), std::out_of_range);
  869. curs.reset(chain.get());
  870. curs.advanceToEnd();
  871. curs.retreat(5);
  872. EXPECT_EQ(curs.totalLength(), 5);
  873. curs.retreat(10);
  874. EXPECT_EQ(curs.totalLength(), 15);
  875. EXPECT_THROW(curs.retreat(10), std::out_of_range);
  876. curs.reset(chain.get());
  877. curs.advanceToEnd();
  878. EXPECT_EQ(curs.retreatAtMost(5), 5);
  879. EXPECT_EQ(curs.totalLength(), 5);
  880. EXPECT_EQ(curs.retreatAtMost(10), 10);
  881. EXPECT_EQ(curs.totalLength(), 15);
  882. EXPECT_EQ(curs.retreatAtMost(10), 5);
  883. EXPECT_EQ(curs.totalLength(), 20);
  884. }
  885. TEST(IOBuf, TestRetreatMulti) {
  886. std::unique_ptr<IOBuf> chain(IOBuf::create(10));
  887. chain->append(10);
  888. std::unique_ptr<IOBuf> buf(IOBuf::create(5));
  889. buf->append(5);
  890. chain->prependChain(std::move(buf));
  891. buf = IOBuf::create(20);
  892. buf->append(20);
  893. chain->prependChain(std::move(buf));
  894. Cursor curs(chain.get());
  895. EXPECT_EQ(curs.retreatAtMost(10), 0);
  896. EXPECT_THROW(curs.retreat(10), std::out_of_range);
  897. curs.reset(chain.get());
  898. curs.advanceToEnd();
  899. curs.retreat(20);
  900. EXPECT_EQ(curs.totalLength(), 20);
  901. EXPECT_EQ(curs.length(), 20);
  902. curs.retreat(1);
  903. EXPECT_EQ(curs.totalLength(), 21);
  904. EXPECT_EQ(curs.length(), 1);
  905. EXPECT_EQ(curs.retreatAtMost(50), 14);
  906. EXPECT_EQ(curs.totalLength(), 35);
  907. curs.advanceToEnd();
  908. curs.retreat(30);
  909. EXPECT_EQ(curs.totalLength(), 30);
  910. }
  911. TEST(IOBuf, TestRetreatOperators) {
  912. std::unique_ptr<IOBuf> chain(IOBuf::create(20));
  913. chain->append(20);
  914. Cursor curs(chain.get());
  915. curs.advanceToEnd();
  916. curs -= 5;
  917. EXPECT_EQ(curs.totalLength(), 5);
  918. curs.advanceToEnd();
  919. auto retreated = curs - 5;
  920. EXPECT_EQ(retreated.totalLength(), 5);
  921. EXPECT_EQ(curs.totalLength(), 0);
  922. }
  923. TEST(IOBuf, tryRead) {
  924. unique_ptr<IOBuf> iobuf1(IOBuf::create(6));
  925. iobuf1->append(6);
  926. unique_ptr<IOBuf> iobuf2(IOBuf::create(24));
  927. iobuf2->append(24);
  928. iobuf1->prependChain(std::move(iobuf2));
  929. EXPECT_TRUE(iobuf1->isChained());
  930. RWPrivateCursor wcursor(iobuf1.get());
  931. Cursor rcursor(iobuf1.get());
  932. wcursor.writeLE((uint32_t)1);
  933. wcursor.writeLE((uint64_t)1);
  934. wcursor.writeLE((uint64_t)1);
  935. wcursor.writeLE((uint64_t)1);
  936. wcursor.writeLE((uint16_t)1);
  937. EXPECT_EQ(0, wcursor.totalLength());
  938. EXPECT_EQ(1u, rcursor.readLE<uint32_t>());
  939. EXPECT_EQ(1u, rcursor.readLE<uint32_t>());
  940. EXPECT_EQ(0u, rcursor.readLE<uint32_t>());
  941. EXPECT_EQ(1u, rcursor.readLE<uint32_t>());
  942. rcursor.skip(4);
  943. uint32_t val;
  944. EXPECT_TRUE(rcursor.tryRead(val));
  945. EXPECT_EQ(1, val);
  946. EXPECT_TRUE(rcursor.tryRead(val));
  947. EXPECT_EQ(0, val);
  948. EXPECT_FALSE(rcursor.tryRead(val));
  949. }
  950. TEST(IOBuf, tryReadLE) {
  951. IOBuf buf{IOBuf::CREATE, 4};
  952. buf.append(4);
  953. RWPrivateCursor wcursor(&buf);
  954. Cursor rcursor(&buf);
  955. const uint32_t expected = 0x01020304;
  956. wcursor.writeLE(expected);
  957. uint32_t actual;
  958. EXPECT_TRUE(rcursor.tryReadLE(actual));
  959. EXPECT_EQ(expected, actual);
  960. }
  961. TEST(IOBuf, tryReadBE) {
  962. IOBuf buf{IOBuf::CREATE, 4};
  963. buf.append(4);
  964. RWPrivateCursor wcursor(&buf);
  965. Cursor rcursor(&buf);
  966. const uint32_t expected = 0x01020304;
  967. wcursor.writeBE(expected);
  968. uint32_t actual;
  969. EXPECT_TRUE(rcursor.tryReadBE(actual));
  970. EXPECT_EQ(expected, actual);
  971. }
  972. TEST(IOBuf, tryReadConsumesAllInputOnFailure) {
  973. IOBuf buf{IOBuf::CREATE, 2};
  974. buf.append(2);
  975. Cursor rcursor(&buf);
  976. uint32_t val;
  977. EXPECT_FALSE(rcursor.tryRead(val));
  978. EXPECT_EQ(0, rcursor.totalLength());
  979. }
  980. TEST(IOBuf, readConsumesAllInputOnFailure) {
  981. IOBuf buf{IOBuf::CREATE, 2};
  982. buf.append(2);
  983. Cursor rcursor(&buf);
  984. EXPECT_THROW(rcursor.read<uint32_t>(), std::out_of_range);
  985. EXPECT_EQ(0, rcursor.totalLength());
  986. }
  987. TEST(IOBuf, pushEmptyByteRange) {
  988. // Test pushing an empty ByteRange. This mainly tests that we do not
  989. // trigger UBSAN warnings by calling memcpy() with an null source pointer,
  990. // which is undefined behavior even if the length is 0.
  991. IOBuf buf{IOBuf::CREATE, 2};
  992. ByteRange emptyBytes;
  993. // Test calling Cursor::push()
  994. RWPrivateCursor wcursor(&buf);
  995. wcursor.push(emptyBytes);
  996. EXPECT_EQ(0, buf.computeChainDataLength());
  997. // Test calling Appender::push()
  998. Appender app(&buf, 16);
  999. app.push(emptyBytes);
  1000. EXPECT_EQ(0, buf.computeChainDataLength());
  1001. }
  1002. TEST(IOBuf, positionTracking) {
  1003. unique_ptr<IOBuf> iobuf1(IOBuf::create(6));
  1004. iobuf1->append(6);
  1005. unique_ptr<IOBuf> iobuf2(IOBuf::create(24));
  1006. iobuf2->append(24);
  1007. iobuf1->prependChain(std::move(iobuf2));
  1008. Cursor cursor(iobuf1.get());
  1009. EXPECT_EQ(0, cursor.getCurrentPosition());
  1010. EXPECT_EQ(6, cursor.length());
  1011. cursor.skip(3);
  1012. EXPECT_EQ(3, cursor.getCurrentPosition());
  1013. EXPECT_EQ(3, cursor.length());
  1014. // Test that we properly handle advancing to the next chunk.
  1015. cursor.skip(4);
  1016. EXPECT_EQ(7, cursor.getCurrentPosition());
  1017. EXPECT_EQ(23, cursor.length());
  1018. // Test that we properly handle doing to the previous chunk.
  1019. cursor.retreat(2);
  1020. EXPECT_EQ(5, cursor.getCurrentPosition());
  1021. EXPECT_EQ(1, cursor.length());
  1022. // Test that we properly handle advanceToEnd
  1023. cursor.advanceToEnd();
  1024. EXPECT_EQ(30, cursor.getCurrentPosition());
  1025. EXPECT_EQ(0, cursor.totalLength());
  1026. // Reset to 0.
  1027. cursor.reset(iobuf1.get());
  1028. EXPECT_EQ(0, cursor.getCurrentPosition());
  1029. EXPECT_EQ(30, cursor.totalLength());
  1030. }