From ad115ee5bf4a1fb2e9f0c762ff75f3b51a52dddd Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sun, 28 Apr 2024 11:30:50 -0400 Subject: [PATCH] TEST: Add more unit tests to the STL file Reader to test error handling Signed-off-by: Michael Jackson --- .../Filters/Algorithms/ReadStlFile.cpp | 5 +- src/Plugins/SimplnxCore/test/CMakeLists.txt | 1 + .../SimplnxCore/test/ReadStlFileTest.cpp | 90 ++++++++++++++++++- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp index 57bbdd4cde..ca8e168ff5 100644 --- a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp +++ b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp @@ -220,8 +220,9 @@ Result<> ReadStlFile::operator()() fgetpos(f, &pos); if(pos >= stlFileSize) { - std::string msg = - fmt::format("Trying to read at file position {} >= file size {}. The STL File is probably corrupt or not written properly.\n The file header was '{}'", pos, stlFileSize, stlHeaderStr); + std::string msg = fmt::format( + "Trying to read at file position {} >= file size {}.\n File Header: '{}'\n Header Triangle Count: {} Current Triangle: {}\n The STL File does not conform to the STL file specification.", + pos, stlFileSize, stlHeaderStr, triCount, t); return MakeErrorResult(nx::core::StlConstants::k_StlFileLengthError, msg); } diff --git a/src/Plugins/SimplnxCore/test/CMakeLists.txt b/src/Plugins/SimplnxCore/test/CMakeLists.txt index f9d887112e..75b7e7050d 100644 --- a/src/Plugins/SimplnxCore/test/CMakeLists.txt +++ b/src/Plugins/SimplnxCore/test/CMakeLists.txt @@ -230,6 +230,7 @@ if(EXISTS "${DREAM3D_DATA_DIR}" AND SIMPLNX_DOWNLOAD_TEST_FILES) download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME remove_flagged_triangles_test.tar.gz SHA512 cd5c6f3ea16a6d09e00e0c0bd0f941b27dca8a0beaeabb7262a2a6adaad83c829187c5d1aa433718123b628eaa839f016604c1134ced9f870723594b2df4be99) download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME generate_color_table_test.tar.gz SHA512 b5683c758964eb723267400b14047f8adb0d5365ee9ca93d1a6940e9b6ad198cd4739c1ca799eb787b7706e668dbc16ab8243642034cdba5b71d64c27e682d3f) download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME read_vtk_structured_points_test.tar.gz SHA512 e7a07a4e3901204c2562754cd71e0fdba1a46de2a5135bad2b6d66b40eefd0e63bed4dbe0ccd6ccadafb708ef63e20635d080aa3a35c172c4ced6986e0f75d5c) + download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME ReadSTLFileTest.tar.gz SHA512 975587206625ffa183160308934e767347de55a34a16272cf5c121114efa286b3c6939e3c6a397e8728fdefe1771bc024bd4c9b409afdff0b76f2f56fcb9eb69) endif() diff --git a/src/Plugins/SimplnxCore/test/ReadStlFileTest.cpp b/src/Plugins/SimplnxCore/test/ReadStlFileTest.cpp index e6a5d4227b..c7af3236e8 100644 --- a/src/Plugins/SimplnxCore/test/ReadStlFileTest.cpp +++ b/src/Plugins/SimplnxCore/test/ReadStlFileTest.cpp @@ -16,8 +16,10 @@ namespace fs = std::filesystem; using namespace nx::core; using namespace nx::core::Constants; -TEST_CASE("SimplnxCore::ReadStlFileFilter", "[SimplnxCore][ReadStlFileFilter]") +TEST_CASE("SimplnxCore::ReadStlFileFilter:Valid_File", "[SimplnxCore][ReadStlFileFilter]") { + const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest"); + // Instantiate the filter, a DataStructure object and an Arguments Object DataStructure dataStructure; Arguments args; @@ -25,7 +27,7 @@ TEST_CASE("SimplnxCore::ReadStlFileFilter", "[SimplnxCore][ReadStlFileFilter]") DataPath triangleGeomDataPath({"[Triangle Geometry]"}); - std::string inputFile = fmt::format("{}/ASTMD638_specimen.stl", unit_test::k_ComplexTestDataSourceDir.view()); + std::string inputFile = fmt::format("{}/ReadSTLFileTest/ASTMD638_specimen.stl", unit_test::k_TestFilesDir); // Create default Parameters for the filter. args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any(fs::path(inputFile))); @@ -48,3 +50,87 @@ TEST_CASE("SimplnxCore::ReadStlFileFilter", "[SimplnxCore][ReadStlFileFilter]") WriteTestDataStructure(dataStructure, fs::path(fmt::format("{}/StlFileReaderTest.dream3d", unit_test::k_BinaryTestOutputDir))); #endif } + +TEST_CASE("SimplnxCore::ReadStlFileFilter:STLParseError", "[SimplnxCore][ReadStlFileFilter]") +{ + const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest"); + + // Instantiate the filter, a DataStructure object and an Arguments Object + DataStructure dataStructure; + Arguments args; + ReadStlFileFilter filter; + + DataPath triangleGeomDataPath({"[Triangle Geometry]"}); + + std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_wrong_num_triangles.stl", unit_test::k_TestFilesDir); + + // Create default Parameters for the filter. + args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any(fs::path(inputFile))); + args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any(triangleGeomDataPath)); + + // Preflight the filter and check result + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + + // Execute the filter and check the result + auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + + REQUIRE(executeResult.result.errors().front().code == -1108); +} + +TEST_CASE("SimplnxCore::ReadStlFileFilter:TriangleParseError", "[SimplnxCore][ReadStlFileFilter]") +{ + const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest"); + + // Instantiate the filter, a DataStructure object and an Arguments Object + DataStructure dataStructure; + Arguments args; + ReadStlFileFilter filter; + + DataPath triangleGeomDataPath({"[Triangle Geometry]"}); + + std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_2_TriangleParseError.stl", unit_test::k_TestFilesDir); + + // Create default Parameters for the filter. + args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any(fs::path(inputFile))); + args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any(triangleGeomDataPath)); + + // Preflight the filter and check result + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + + // Execute the filter and check the result + auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + + REQUIRE(executeResult.result.errors().front().code == -1106); +} + +TEST_CASE("SimplnxCore::ReadStlFileFilter:AttributeParseError", "[SimplnxCore][ReadStlFileFilter]") +{ + const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest"); + + // Instantiate the filter, a DataStructure object and an Arguments Object + DataStructure dataStructure; + Arguments args; + ReadStlFileFilter filter; + + DataPath triangleGeomDataPath({"[Triangle Geometry]"}); + + std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_2_AttributeParseError.stl", unit_test::k_TestFilesDir); + + // Create default Parameters for the filter. + args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any(fs::path(inputFile))); + args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any(triangleGeomDataPath)); + + // Preflight the filter and check result + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + + // Execute the filter and check the result + auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result); + + REQUIRE(executeResult.result.errors().front().code == -1107); +}