pb_io_ut.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "pb_io.h"
  2. #include "is_equal.h"
  3. #include <library/cpp/protobuf/util/ut/common_ut.pb.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. #include <util/folder/path.h>
  6. #include <util/folder/tempdir.h>
  7. #include <util/stream/file.h>
  8. #include <util/stream/str.h>
  9. static NProtobufUtilUt::TTextTest GetCorrectMessage() {
  10. NProtobufUtilUt::TTextTest m;
  11. m.SetFoo(42);
  12. return m;
  13. }
  14. static NProtobufUtilUt::TTextEnumTest GetCorrectEnumMessage() {
  15. NProtobufUtilUt::TTextEnumTest m;
  16. m.SetSlot(NProtobufUtilUt::TTextEnumTest::EET_SLOT_1);
  17. return m;
  18. }
  19. static const TString CORRECT_MESSAGE =
  20. R"(Foo: 42
  21. )";
  22. static const TString CORRECT_ENUM_NAME_MESSAGE =
  23. R"(Slot: EET_SLOT_1
  24. )";
  25. static const TString CORRECT_ENUM_ID_MESSAGE =
  26. R"(Slot: 1
  27. )";
  28. static const TString INCORRECT_MESSAGE =
  29. R"(Bar: 1
  30. )";
  31. static const TString INCORRECT_ENUM_NAME_MESSAGE =
  32. R"(Slot: EET_SLOT_3
  33. )";
  34. static const TString INCORRECT_ENUM_ID_MESSAGE =
  35. R"(Slot: 3
  36. )";
  37. static const TString CORRECT_BASE64_MESSAGE = "CCo,";
  38. static const TString CORRECT_UNEVEN_BASE64_MESSAGE = "CCo";
  39. static const TString INCORRECT_BASE64_MESSAGE = "CC";
  40. Y_UNIT_TEST_SUITE(TTestProtoBufIO) {
  41. Y_UNIT_TEST(TestBase64) {
  42. {
  43. NProtobufUtilUt::TTextTest message;
  44. UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_BASE64_MESSAGE, message));
  45. }
  46. {
  47. NProtobufUtilUt::TTextTest message;
  48. UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(INCORRECT_BASE64_MESSAGE, message));
  49. }
  50. {
  51. NProtobufUtilUt::TTextTest message;
  52. UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, true));
  53. }
  54. {
  55. NProtobufUtilUt::TTextTest message;
  56. UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, false));
  57. }
  58. {
  59. UNIT_ASSERT_VALUES_EQUAL(CORRECT_BASE64_MESSAGE, NProtoBuf::SerializeToBase64String(GetCorrectMessage()));
  60. }
  61. {
  62. const auto m = NProtoBuf::ParseFromBase64String<NProtobufUtilUt::TTextTest>(CORRECT_BASE64_MESSAGE);
  63. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  64. }
  65. }
  66. Y_UNIT_TEST(TestParseFromTextFormat) {
  67. TTempDir tempDir;
  68. const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt";
  69. const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt";
  70. TFileOutput{correctFileName}.Write(CORRECT_MESSAGE);
  71. TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE);
  72. {
  73. NProtobufUtilUt::TTextTest message;
  74. UNIT_ASSERT(TryParseFromTextFormat(correctFileName, message));
  75. }
  76. {
  77. NProtobufUtilUt::TTextTest message;
  78. UNIT_ASSERT(!TryParseFromTextFormat(incorrectFileName, message));
  79. }
  80. {
  81. NProtobufUtilUt::TTextTest message;
  82. TStringInput in{CORRECT_MESSAGE};
  83. UNIT_ASSERT(TryParseFromTextFormat(in, message));
  84. }
  85. {
  86. NProtobufUtilUt::TTextTest message;
  87. TStringInput in{INCORRECT_MESSAGE};
  88. UNIT_ASSERT(!TryParseFromTextFormat(in, message));
  89. }
  90. {
  91. NProtobufUtilUt::TTextTest message;
  92. UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat(incorrectFileName, message));
  93. }
  94. {
  95. NProtobufUtilUt::TTextTest message;
  96. UNIT_ASSERT(!TryParseFromTextFormat("this_file_doesnt_exists", message));
  97. }
  98. {
  99. NProtobufUtilUt::TTextTest message;
  100. UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat("this_file_doesnt_exists", message));
  101. }
  102. {
  103. NProtobufUtilUt::TTextTest message;
  104. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat("this_file_doesnt_exists", message), TFileError);
  105. }
  106. {
  107. NProtobufUtilUt::TTextTest message;
  108. UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(correctFileName, message));
  109. }
  110. {
  111. NProtobufUtilUt::TTextTest message;
  112. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(incorrectFileName, message), yexception);
  113. }
  114. {
  115. NProtobufUtilUt::TTextTest message;
  116. TStringInput in{CORRECT_MESSAGE};
  117. UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message));
  118. }
  119. {
  120. NProtobufUtilUt::TTextTest message;
  121. TStringInput in{INCORRECT_MESSAGE};
  122. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception);
  123. }
  124. {
  125. NProtobufUtilUt::TTextTest m;
  126. const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
  127. mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName);
  128. };
  129. UNIT_ASSERT_NO_EXCEPTION(f(m));
  130. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  131. }
  132. {
  133. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception);
  134. }
  135. {
  136. NProtobufUtilUt::TTextTest m;
  137. TStringInput in{CORRECT_MESSAGE};
  138. const auto f = [&in](NProtobufUtilUt::TTextTest& mm) {
  139. mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in);
  140. };
  141. UNIT_ASSERT_NO_EXCEPTION(f(m));
  142. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  143. }
  144. {
  145. TStringInput in{INCORRECT_MESSAGE};
  146. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception);
  147. }
  148. {
  149. const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt";
  150. const auto original = GetCorrectMessage();
  151. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2));
  152. const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll();
  153. UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE);
  154. }
  155. {
  156. const auto original = GetCorrectMessage();
  157. TStringStream out;
  158. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
  159. UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE);
  160. }
  161. {
  162. NProtobufUtilUt::TTextTest m;
  163. const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
  164. mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(
  165. correctFileName,
  166. EParseFromTextFormatOption::AllowUnknownField);
  167. };
  168. UNIT_ASSERT_NO_EXCEPTION(f(m));
  169. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  170. }
  171. {
  172. const NProtobufUtilUt::TTextTest empty;
  173. NProtobufUtilUt::TTextTest m;
  174. const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) {
  175. mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(
  176. incorrectFileName,
  177. EParseFromTextFormatOption::AllowUnknownField);
  178. };
  179. UNIT_ASSERT_NO_EXCEPTION(f(m));
  180. UNIT_ASSERT(NProtoBuf::IsEqual(empty, m));
  181. }
  182. }
  183. Y_UNIT_TEST(TestSerializeToTextFormatWithEnumId) {
  184. TTempDir tempDir;
  185. const TFsPath correctNameFileName = TFsPath{tempDir()} / "correct_name.pb.txt";
  186. const TFsPath incorrectNameFileName = TFsPath{tempDir()} / "incorrect_name.pb.txt";
  187. const TFsPath correctIdFileName = TFsPath{tempDir()} / "correct_id.pb.txt";
  188. const TFsPath incorrectIdFileName = TFsPath{tempDir()} / "incorrect_id.pb.txt";
  189. TFileOutput{correctNameFileName}.Write(CORRECT_ENUM_NAME_MESSAGE);
  190. TFileOutput{incorrectNameFileName}.Write(INCORRECT_ENUM_NAME_MESSAGE);
  191. TFileOutput{correctIdFileName}.Write(CORRECT_ENUM_ID_MESSAGE);
  192. TFileOutput{incorrectIdFileName}.Write(INCORRECT_ENUM_ID_MESSAGE);
  193. {
  194. NProtobufUtilUt::TTextEnumTest message;
  195. for (auto correct_message: {CORRECT_ENUM_ID_MESSAGE, CORRECT_ENUM_NAME_MESSAGE}) {
  196. TStringInput in{correct_message};
  197. UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message));
  198. }
  199. }
  200. {
  201. NProtobufUtilUt::TTextEnumTest message;
  202. for (auto incorrect_message: {INCORRECT_ENUM_ID_MESSAGE, INCORRECT_ENUM_NAME_MESSAGE}) {
  203. TStringInput in{incorrect_message};
  204. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception);
  205. }
  206. }
  207. {
  208. const auto f = [](NProtobufUtilUt::TTextEnumTest& mm, const TString fileName) {
  209. mm = ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(fileName);
  210. };
  211. for (auto fileName: {correctIdFileName, correctNameFileName}) {
  212. NProtobufUtilUt::TTextEnumTest m;
  213. UNIT_ASSERT_NO_EXCEPTION(f(m, fileName));
  214. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectEnumMessage(), m));
  215. }
  216. }
  217. {
  218. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectIdFileName), yexception);
  219. UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectNameFileName), yexception);
  220. }
  221. {
  222. const auto original = GetCorrectEnumMessage();
  223. TStringStream out;
  224. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
  225. UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_NAME_MESSAGE);
  226. }
  227. {
  228. const auto original = GetCorrectEnumMessage();
  229. TStringStream out;
  230. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormatWithEnumId(original, out));
  231. UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_ID_MESSAGE);
  232. }
  233. }
  234. Y_UNIT_TEST(TestMergeFromTextFormat) {
  235. //
  236. // Tests cases below are identical to `Parse` tests
  237. //
  238. TTempDir tempDir;
  239. const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt";
  240. const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt";
  241. TFileOutput{correctFileName}.Write(CORRECT_MESSAGE);
  242. TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE);
  243. {
  244. NProtobufUtilUt::TTextTest message;
  245. UNIT_ASSERT(TryMergeFromTextFormat(correctFileName, message));
  246. }
  247. {
  248. NProtobufUtilUt::TTextTest message;
  249. UNIT_ASSERT(!TryMergeFromTextFormat(incorrectFileName, message));
  250. }
  251. {
  252. NProtobufUtilUt::TTextTest message;
  253. TStringInput in{CORRECT_MESSAGE};
  254. UNIT_ASSERT(TryMergeFromTextFormat(in, message));
  255. }
  256. {
  257. NProtobufUtilUt::TTextTest message;
  258. TStringInput in{INCORRECT_MESSAGE};
  259. UNIT_ASSERT(!TryMergeFromTextFormat(in, message));
  260. }
  261. {
  262. NProtobufUtilUt::TTextTest message;
  263. UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat(incorrectFileName, message));
  264. }
  265. {
  266. NProtobufUtilUt::TTextTest message;
  267. UNIT_ASSERT(!TryMergeFromTextFormat("this_file_doesnt_exists", message));
  268. }
  269. {
  270. NProtobufUtilUt::TTextTest message;
  271. UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat("this_file_doesnt_exists", message));
  272. }
  273. {
  274. NProtobufUtilUt::TTextTest message;
  275. UNIT_ASSERT_EXCEPTION(MergeFromTextFormat("this_file_doesnt_exists", message), TFileError);
  276. }
  277. {
  278. NProtobufUtilUt::TTextTest message;
  279. UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(correctFileName, message));
  280. }
  281. {
  282. NProtobufUtilUt::TTextTest message;
  283. UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(incorrectFileName, message), yexception);
  284. }
  285. {
  286. NProtobufUtilUt::TTextTest message;
  287. TStringInput in{CORRECT_MESSAGE};
  288. UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(in, message));
  289. }
  290. {
  291. NProtobufUtilUt::TTextTest message;
  292. TStringInput in{INCORRECT_MESSAGE};
  293. UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(in, message), yexception);
  294. }
  295. {
  296. NProtobufUtilUt::TTextTest m;
  297. const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
  298. mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName);
  299. };
  300. UNIT_ASSERT_NO_EXCEPTION(f(m));
  301. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  302. }
  303. {
  304. UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception);
  305. }
  306. {
  307. NProtobufUtilUt::TTextTest m;
  308. TStringInput in{CORRECT_MESSAGE};
  309. const auto f = [&in](NProtobufUtilUt::TTextTest& mm) {
  310. mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in);
  311. };
  312. UNIT_ASSERT_NO_EXCEPTION(f(m));
  313. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  314. }
  315. {
  316. TStringInput in{INCORRECT_MESSAGE};
  317. UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception);
  318. }
  319. {
  320. const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt";
  321. const auto original = GetCorrectMessage();
  322. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2));
  323. const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll();
  324. UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE);
  325. }
  326. {
  327. const auto original = GetCorrectMessage();
  328. TStringStream out;
  329. UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out));
  330. UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE);
  331. }
  332. {
  333. NProtobufUtilUt::TTextTest m;
  334. const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) {
  335. mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(
  336. correctFileName,
  337. EParseFromTextFormatOption::AllowUnknownField);
  338. };
  339. UNIT_ASSERT_NO_EXCEPTION(f(m));
  340. UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m));
  341. }
  342. {
  343. const NProtobufUtilUt::TTextTest empty;
  344. NProtobufUtilUt::TTextTest m;
  345. const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) {
  346. mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(
  347. incorrectFileName,
  348. EParseFromTextFormatOption::AllowUnknownField);
  349. };
  350. UNIT_ASSERT_NO_EXCEPTION(f(m));
  351. UNIT_ASSERT(NProtoBuf::IsEqual(empty, m));
  352. }
  353. //
  354. // Test cases for `Merge`
  355. //
  356. {
  357. NProtobufUtilUt::TTextTest message;
  358. message.SetFoo(100500);
  359. TStringInput in{CORRECT_MESSAGE};
  360. UNIT_ASSERT(TryMergeFromTextFormat(in, message));
  361. UNIT_ASSERT(NProtoBuf::IsEqual(message, GetCorrectMessage()));
  362. }
  363. }
  364. Y_UNIT_TEST(TestMergeFromString) {
  365. NProtobufUtilUt::TMergeTest message;
  366. NProtobufUtilUt::TMergeTest messageFirstHalf;
  367. NProtobufUtilUt::TMergeTest messageSecondHalf;
  368. for (ui32 v = ~0; v != 0; v >>= 1) {
  369. message.AddMergeInt(v);
  370. (v > 0xffff ? messageFirstHalf : messageSecondHalf).AddMergeInt(v);
  371. }
  372. const TString full = message.SerializeAsString();
  373. {
  374. NProtobufUtilUt::TMergeTest m1;
  375. UNIT_ASSERT(NProtoBuf::MergeFromString(m1, full));
  376. UNIT_ASSERT(NProtoBuf::IsEqual(message, m1));
  377. }
  378. {
  379. NProtobufUtilUt::TMergeTest m2;
  380. TStringBuf s0 = TStringBuf(full).SubStr(0, 3);
  381. TStringBuf s1 = TStringBuf(full).SubStr(3);
  382. // объединение результатов двух MergePartialFromString не эквивалентно вызову MergePartialFromString от объединения строк
  383. UNIT_ASSERT(!(NProtoBuf::MergePartialFromString(m2, s0) && NProtoBuf::MergePartialFromString(m2, s1)));
  384. }
  385. {
  386. NProtobufUtilUt::TMergeTest m3;
  387. UNIT_ASSERT(NProtoBuf::MergePartialFromString(m3, messageFirstHalf.SerializeAsString()));
  388. UNIT_ASSERT(NProtoBuf::MergeFromString(m3, messageSecondHalf.SerializeAsString()));
  389. UNIT_ASSERT(NProtoBuf::IsEqual(message, m3));
  390. }
  391. }
  392. }