Skip to content

Commit

Permalink
Add: Validate UTF-8 for exported JSON strings
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Mar 6, 2024
1 parent 3352fd5 commit 90c4637
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
10 changes: 5 additions & 5 deletions tests/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,17 @@ TEST_CASE("Test accessors min/max", "[gltf-loader]") {

TEST_CASE("Test unicode characters", "[gltf-loader]") {
#if FASTGLTF_CPP_20
auto lightsLamp = sampleModels / "2.0" / std::filesystem::path(u8"Unicode❤♻Test") / "glTF";
auto unicodePath = sampleModels / "2.0" / std::filesystem::path(u8"Unicode❤♻Test") / "glTF";
fastgltf::GltfDataBuffer jsonData;
REQUIRE(jsonData.loadFromFile(lightsLamp / std::filesystem::path(u8"Unicode❤♻Test.gltf")));
REQUIRE(jsonData.loadFromFile(unicodePath / std::filesystem::path(u8"Unicode❤♻Test.gltf")));
#else
auto lightsLamp = sampleModels / "2.0" / std::filesystem::u8path(u8"Unicode❤♻Test") / "glTF";
auto unicodePath = sampleModels / "2.0" / std::filesystem::u8path(u8"Unicode❤♻Test") / "glTF";
fastgltf::GltfDataBuffer jsonData;
REQUIRE(jsonData.loadFromFile(lightsLamp / std::filesystem::u8path(u8"Unicode❤♻Test.gltf")));
REQUIRE(jsonData.loadFromFile(unicodePath / std::filesystem::u8path(u8"Unicode❤♻Test.gltf")));
#endif

fastgltf::Parser parser;
auto asset = parser.loadGltfJson(&jsonData, lightsLamp);
auto asset = parser.loadGltfJson(&jsonData, unicodePath);
REQUIRE(asset.error() == fastgltf::Error::None);
REQUIRE(fastgltf::validate(asset.get()) == fastgltf::Error::None);

Expand Down
43 changes: 42 additions & 1 deletion tests/write_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <catch2/catch_test_macros.hpp>

#include <simdjson.h>

#include <fastgltf/core.hpp>
#include "gltf_path.hpp"

Expand Down Expand Up @@ -153,8 +155,11 @@ TEST_CASE("Test all local models and re-export them", "[write-tests]") {
auto exported = exporter.writeGltfJson(model.get());
REQUIRE(exported.error() == fastgltf::Error::None);

// Parse the re-generated glTF and validate
// UTF-8 validation on the exported JSON string
auto& exportedJson = exported.get().output;
REQUIRE(simdjson::validate_utf8(exportedJson));

// Parse the re-generated glTF and validate
fastgltf::GltfDataBuffer regeneratedJson;
regeneratedJson.copyBytes(reinterpret_cast<const uint8_t*>(exportedJson.data()), exportedJson.size());
auto regeneratedModel = parser.loadGltf(&regeneratedJson, epath.parent_path());
Expand All @@ -166,3 +171,39 @@ TEST_CASE("Test all local models and re-export them", "[write-tests]") {
}
printf("Successfully tested fastgltf exporter on %" PRIu64 " assets.", testedAssets);
}

TEST_CASE("Test Unicode exporting", "[write-tests]") {
#if FASTGLTF_CPP_20
auto unicodePath = sampleModels / "2.0" / std::filesystem::path(u8"Unicode❤♻Test") / "glTF";
fastgltf::GltfDataBuffer jsonData;
REQUIRE(jsonData.loadFromFile(unicodePath / std::filesystem::path(u8"Unicode❤♻Test.gltf")));
#else
auto unicodePath = sampleModels / "2.0" / std::filesystem::u8path(u8"Unicode❤♻Test") / "glTF";
fastgltf::GltfDataBuffer jsonData;
REQUIRE(jsonData.loadFromFile(unicodePath / std::filesystem::u8path(u8"Unicode❤♻Test.gltf")));
#endif

fastgltf::Parser parser;
auto asset = parser.loadGltfJson(&jsonData, unicodePath);
REQUIRE(asset.error() == fastgltf::Error::None);

fastgltf::Exporter exporter;
auto exported = exporter.writeGltfJson(asset.get());
REQUIRE(exported.error() == fastgltf::Error::None);

// UTF-8 validation on the exported JSON string
auto& exportedJson = exported.get().output;
REQUIRE(simdjson::validate_utf8(exportedJson));

fastgltf::GltfDataBuffer regeneratedJson;
regeneratedJson.copyBytes(reinterpret_cast<const uint8_t*>(exportedJson.data()), exportedJson.size());
auto reparsed = parser.loadGltfJson(&regeneratedJson, unicodePath);
REQUIRE(reparsed.error() == fastgltf::Error::None);

REQUIRE(!asset->materials.empty());
REQUIRE(asset->materials[0].name == "Unicode❤♻Material");

REQUIRE(!asset->buffers.empty());
auto bufferUri = std::get<fastgltf::sources::URI>(asset->buffers[0].data);
REQUIRE(bufferUri.uri.path() == "Unicode❤♻Binary.bin");
}

0 comments on commit 90c4637

Please sign in to comment.