base32_ut.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <library/cpp/string_utils/base32/base32.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. #include <util/generic/yexception.h>
  4. #include <vector>
  5. namespace {
  6. static const std::vector<std::string> TEST_DATA = {
  7. {},
  8. "\x00",
  9. "\x01\x02",
  10. "\x01\x02\x03",
  11. "\x03\x02\x01",
  12. "\x10\x20\x30\x40",
  13. "\x10\x20\x30\x40\x50",
  14. "\xFF\xFF\xFF",
  15. "\xFF\xFE\xFD\xFC\xFB\xFA",
  16. };
  17. } // namespace
  18. TEST(base32, encode)
  19. {
  20. EXPECT_EQ(Base32Encode({}), "");
  21. EXPECT_EQ(Base32Encode({"\x00\x40", 2}), "ABAA====");
  22. EXPECT_EQ(Base32Encode("apple"), "MFYHA3DF");
  23. EXPECT_EQ(Base32Encode("TestTest"), "KRSXG5CUMVZXI===");
  24. EXPECT_EQ(Base32Encode("1234567890"), "GEZDGNBVGY3TQOJQ");
  25. }
  26. TEST(base32, decode_strict)
  27. {
  28. EXPECT_EQ(Base32StrictDecode(""), "");
  29. EXPECT_EQ(Base32StrictDecode("MFYHA3DF"), "apple");
  30. EXPECT_EQ(Base32StrictDecode("KRSXG5CUMVZXI"), "TestTest");
  31. EXPECT_EQ(Base32StrictDecode("KRSXG5CUMVZXI==="), "TestTest");
  32. EXPECT_THROW(Base32StrictDecode("KRSXG5CUMVZXI=======A"), yexception);
  33. EXPECT_EQ(Base32StrictDecode("AA======"), std::string(1, '\x00'));
  34. EXPECT_THROW(Base32StrictDecode("AAA="), yexception);
  35. EXPECT_EQ(Base32StrictDecode("AE======"), "\x01");
  36. EXPECT_THROW(Base32StrictDecode("AB======"), yexception); // "\x00\x40"
  37. EXPECT_THROW(Base32StrictDecode("invalid"), yexception);
  38. EXPECT_THROW(Base32StrictDecode("\xFF\xFF"), yexception);
  39. EXPECT_THROW(Base32StrictDecode(std::string_view{"A\0", 2}), yexception);
  40. }
  41. TEST(base32, decode)
  42. {
  43. EXPECT_EQ(Base32Decode(""), "");
  44. EXPECT_EQ(Base32Decode("MFYHA3DF"), "apple");
  45. EXPECT_EQ(Base32Decode("KRSXG5CUMVZXI"), "TestTest");
  46. EXPECT_EQ(Base32Decode("KRSXG5CUMVZXI==="), "TestTest");
  47. EXPECT_NO_THROW(Base32Decode("KRSXG5CUMVZXI=======A"));
  48. EXPECT_EQ(Base32Decode("AA======"), std::string(1, '\x00'));
  49. EXPECT_NO_THROW(Base32Decode("AAA="));
  50. EXPECT_EQ(Base32Decode("AE======"), "\x01");
  51. EXPECT_NO_THROW(Base32Decode("AB======")); // "\x00\x40"
  52. EXPECT_NO_THROW(Base32Decode("invalid"));
  53. EXPECT_NO_THROW(Base32Decode("\xFF\xFF"));
  54. EXPECT_NO_THROW(Base32Decode(std::string_view{"A\0", 2}));
  55. }
  56. TEST(base32, encode_decode)
  57. {
  58. for (const auto& data : TEST_DATA) {
  59. EXPECT_THAT(Base32StrictDecode(Base32Encode(data)), ::testing::ContainerEq(data));
  60. }
  61. }