base64_tests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <fstream>
  2. #include <sstream>
  3. #include <catch2/catch_test_macros.hpp>
  4. #include <catch2/benchmark/catch_benchmark.hpp>
  5. #include <fastgltf/base64.hpp>
  6. #include <fastgltf/types.hpp>
  7. #include <fastgltf/parser.hpp>
  8. #include "gltf_path.hpp"
  9. constexpr std::string_view testBase64 = "SGVsbG8gV29ybGQuIEhlbGxvIFdvcmxkLiBIZWxsbyBXb3JsZC4=";
  10. TEST_CASE("Check base64 utility functions", "[base64]") {
  11. REQUIRE(fastgltf::base64::getPadding("Li==") == 2);
  12. REQUIRE(fastgltf::base64::getPadding("Li4=") == 1);
  13. REQUIRE(fastgltf::base64::getPadding("Li4u") == 0);
  14. REQUIRE(fastgltf::base64::getOutputSize(4, 0) == 3); // Li4u
  15. REQUIRE(fastgltf::base64::getOutputSize(4, 1) == 2); // Li4=
  16. REQUIRE(fastgltf::base64::getOutputSize(4, 2) == 1); // Li==
  17. }
  18. TEST_CASE("Check base64 decoding", "[base64]") {
  19. // This is "Hello World. Hello World.". The decode function
  20. // uses the best possible SIMD version of the algorithm.
  21. auto bytes = fastgltf::base64::decode(testBase64);
  22. std::string strings(bytes.begin(), bytes.end());
  23. REQUIRE(strings == "Hello World. Hello World. Hello World.");
  24. }
  25. TEST_CASE("Check all base64 decoders", "[base64]") {
  26. // Checks that the base64 decoders return the same.
  27. auto bytes = fastgltf::base64::fallback_decode(testBase64);
  28. std::string strings(bytes.begin(), bytes.end());
  29. REQUIRE(strings == "Hello World. Hello World. Hello World.");
  30. #if defined(__x86_64__) || defined(_M_AMD64) || defined(_M_IX86)
  31. REQUIRE(bytes == fastgltf::base64::avx2_decode(testBase64));
  32. REQUIRE(bytes == fastgltf::base64::sse4_decode(testBase64));
  33. #endif
  34. #if defined(__aarch64__)
  35. REQUIRE(bytes == fastgltf::base64::neon_decode(testBase64));
  36. #endif
  37. }
  38. TEST_CASE("Check big base64 data decoding", "[base64]") {
  39. std::ifstream file(path / "base64.txt");
  40. REQUIRE(file.is_open());
  41. std::stringstream buffer;
  42. buffer << file.rdbuf();
  43. auto encodedBytes = buffer.str();
  44. auto bytes = fastgltf::base64::decode(encodedBytes);
  45. REQUIRE(!bytes.empty());
  46. std::ifstream output(path / "base64.txt.out", std::ios::binary | std::ios::ate);
  47. REQUIRE(output.is_open());
  48. std::vector<uint8_t> decodedBytes(output.tellg());
  49. output.seekg(0);
  50. output.read(reinterpret_cast<char*>(decodedBytes.data()), static_cast<std::streamsize>(decodedBytes.size()));
  51. REQUIRE(bytes == decodedBytes);
  52. }
  53. TEST_CASE("Test base64 buffer decoding", "[base64]") {
  54. fastgltf::Parser parser;
  55. fastgltf::Image texture;
  56. std::string bufferData;
  57. auto cylinderEngine = sampleModels / "2.0" / "2CylinderEngine" / "glTF-Embedded";
  58. auto boxTextured = sampleModels / "2.0" / "BoxTextured" / "glTF-Embedded";
  59. auto tceJsonData = std::make_unique<fastgltf::GltfDataBuffer>();
  60. REQUIRE(tceJsonData->loadFromFile(cylinderEngine / "2CylinderEngine.gltf"));
  61. auto btJsonData = std::make_unique<fastgltf::GltfDataBuffer>();
  62. REQUIRE(btJsonData->loadFromFile(boxTextured / "BoxTextured.gltf"));
  63. SECTION("Validate large buffer load from glTF") {
  64. auto asset = parser.loadGLTF(tceJsonData.get(), cylinderEngine, fastgltf::Options::None, fastgltf::Category::Buffers);
  65. REQUIRE(asset.error() == fastgltf::Error::None);
  66. REQUIRE(asset->buffers.size() == 1);
  67. // Load the buffer from the parsed glTF file.
  68. auto& buffer = asset->buffers.front();
  69. REQUIRE(buffer.byteLength == 1794612);
  70. auto bufferVector = std::get_if<fastgltf::sources::Vector>(&buffer.data);
  71. REQUIRE(bufferVector != nullptr);
  72. REQUIRE(bufferVector->mimeType == fastgltf::MimeType::OctetStream);
  73. REQUIRE(!bufferVector->bytes.empty());
  74. }
  75. SECTION("Validate base64 buffer and image load from glTF") {
  76. auto asset = parser.loadGLTF(btJsonData.get(), boxTextured, fastgltf::Options::None, fastgltf::Category::Images | fastgltf::Category::Buffers);
  77. REQUIRE(asset.error() == fastgltf::Error::None);
  78. REQUIRE(asset->buffers.size() == 1);
  79. REQUIRE(asset->images.size() == 1);
  80. auto& buffer = asset->buffers.front();
  81. REQUIRE(buffer.byteLength == 840);
  82. auto bufferVector = std::get_if<fastgltf::sources::Vector>(&buffer.data);
  83. REQUIRE(bufferVector != nullptr);
  84. REQUIRE(bufferVector->mimeType == fastgltf::MimeType::OctetStream);
  85. REQUIRE(!bufferVector->bytes.empty());
  86. auto& image = asset->images.front();
  87. auto imageVector = std::get_if<fastgltf::sources::Vector>(&image.data);
  88. REQUIRE(imageVector != nullptr);
  89. REQUIRE(imageVector->mimeType == fastgltf::MimeType::PNG);
  90. REQUIRE(!imageVector->bytes.empty());
  91. }
  92. }