2
0

base64_test.cc 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "opentelemetry/sdk/common/base64.h"
  4. #include <string>
  5. #include <gtest/gtest.h>
  6. static const unsigned char base64_test_dec[64] = {
  7. 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD, 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
  8. 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09, 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
  9. 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31, 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
  10. 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B, 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97};
  11. static const unsigned char base64_test_enc_rfc_2045[] =
  12. "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
  13. "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
  14. TEST(Base64Test, EscapeRfc2045)
  15. {
  16. std::string encoded = opentelemetry::sdk::common::Base64Escape(opentelemetry::nostd::string_view{
  17. reinterpret_cast<const char *>(base64_test_dec), sizeof(base64_test_dec)});
  18. opentelemetry::nostd::string_view expected{
  19. reinterpret_cast<const char *>(base64_test_enc_rfc_2045), 88};
  20. EXPECT_EQ(encoded, expected);
  21. }
  22. TEST(Base64Test, UnescapeRfc2045)
  23. {
  24. std::string decoded;
  25. EXPECT_TRUE(opentelemetry::sdk::common::Base64Unescape(
  26. opentelemetry::nostd::string_view{reinterpret_cast<const char *>(base64_test_enc_rfc_2045),
  27. 88},
  28. &decoded));
  29. opentelemetry::nostd::string_view expected{reinterpret_cast<const char *>(base64_test_dec),
  30. sizeof(base64_test_dec)};
  31. EXPECT_EQ(decoded, expected);
  32. }
  33. TEST(Base64Test, UnescapeRfc2045InvalidInput)
  34. {
  35. std::string std_str_out;
  36. std::string std_str_in = "=";
  37. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  38. std_str_in = "J=";
  39. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  40. std_str_in = "JEhu=";
  41. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  42. std_str_in = "JEhuV=";
  43. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  44. std_str_in = "JEhuVodi=";
  45. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  46. std_str_in = "JEhuVodiW=";
  47. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  48. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  49. std_str_in = " J=";
  50. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  51. std_str_in = " JEhu=";
  52. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  53. std_str_in = " JEhuV=";
  54. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  55. std_str_in = " JEhuVodi=";
  56. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  57. std_str_in = " JEhuVodiW=";
  58. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  59. std_str_in = "JE==";
  60. EXPECT_TRUE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  61. std_str_in = "JE==huVo";
  62. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  63. std_str_in = "JE=huVo=";
  64. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  65. std_str_in = " JE==";
  66. EXPECT_TRUE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  67. std_str_in = " JE==huVo";
  68. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  69. std_str_in = " JE=huVo=";
  70. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  71. std_str_in = "JE==\r\n";
  72. EXPECT_TRUE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  73. std_str_in = "J E=";
  74. EXPECT_FALSE(opentelemetry::sdk::common::Base64Unescape(std_str_in, &std_str_out));
  75. }