Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial C++ fuzz tests #1605

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/MaterialXCore/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const std::tuple<int, int, int> LIBRARY_VERSION_TUPLE(MATERIALX_MAJOR_VERSION,

bool invalidNameChar(char c)
{
return !isalnum(c) && c != '_' && c != ':';
return !isalnum((unsigned char) c) && c != '_' && c != ':';
}

} // anonymous namespace
Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXFormat/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const string MATERIALX_SEARCH_PATH_ENV_VAR = "MATERIALX_SEARCH_PATH";

inline bool hasWindowsDriveSpecifier(const string& val)
{
return (val.length() > 1 && std::isalpha(val[0]) && (val[1] == ':'));
return (val.length() > 1 && std::isalpha((unsigned char) val[0]) && (val[1] == ':'));
}

//
Expand Down
46 changes: 45 additions & 1 deletion source/MaterialXTest/MaterialXFormat/XmlIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <MaterialXTest/External/Catch/catch.hpp>

#include <MaterialXFormat/Environ.h>
#include <MaterialXFormat/File.h>
#include <MaterialXFormat/Util.h>
#include <MaterialXFormat/XmlIo.h>

Expand Down Expand Up @@ -262,6 +261,51 @@ TEST_CASE("Comments and newlines", "[xmlio]")
REQUIRE(origXml == newXml);
}

TEST_CASE("Fuzz testing", "[xmlio]")
{
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
mx::FilePath examplesPath = searchPath.find("resources/Materials/Examples/StandardSurface");

std::mt19937 rng(0);
std::uniform_int_distribution<size_t> randChar(0, 255);

for (const mx::FilePath& filename : examplesPath.getFilesInDirectory(mx::MTLX_EXTENSION))
{
// Read the example file into an XML string buffer.
const std::string origString = mx::readFile(examplesPath / filename);
REQUIRE(origString.size() > 0);
std::uniform_int_distribution<size_t> randPos(0, origString.size() - 1);

// Iterate over test runs.
for (size_t testRun = 0; testRun < 256; testRun++)
{
std::string editString = origString;

// Iterate over string edits.
for (size_t editIndex = 0; editIndex < 32; editIndex++)
{
// Randomly alter one character in the document.
size_t charIndex = randPos(rng);
size_t newChar = randChar(rng);
editString[charIndex] = (char) newChar;

// Attempt to interpret the edited string as a document, allowing only MaterialX exceptions.
mx::DocumentPtr doc = mx::createDocument();
try
{
mx::readFromXmlString(doc, editString, searchPath);
doc->validate();
}
catch (const mx::Exception&)
{
// On a MaterialX exception, proceed to the next test run.
break;
}
}
}
}
}

TEST_CASE("Locale region testing", "[xmlio]")
{
// In the United States, the thousands separator is a comma, while in Germany it is a period.
Expand Down
5 changes: 2 additions & 3 deletions source/MaterialXTest/MaterialXGenShader/GenShaderUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ void shaderGenPerformanceTest(mx::GenContext& context)
REQUIRE(loadedDocuments.size() == documentsPaths.size());

// Shuffle the order of documents and perform document library import validatation and shadergen
std::random_device random_dev;
std::mt19937 generator(random_dev());
std::shuffle(loadedDocuments.begin(), loadedDocuments.end(), generator);
std::mt19937 rng(0);
std::shuffle(loadedDocuments.begin(), loadedDocuments.end(), rng);
for (const auto& doc : loadedDocuments)
{
doc->importLibrary(nodeLibrary);
Expand Down