From fcf4a9730c69edb50ea393fef34d312ff5f7cb57 Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Sun, 18 Aug 2024 18:11:26 +0200 Subject: [PATCH] Add test with bad JSON --- g2o/core/io/io_json.cpp | 2 +- unit_test/general/graph_io.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/g2o/core/io/io_json.cpp b/g2o/core/io/io_json.cpp index 7406e4862..34fb1e9ae 100644 --- a/g2o/core/io/io_json.cpp +++ b/g2o/core/io/io_json.cpp @@ -49,7 +49,7 @@ std::optional IoJson::load(std::istream& input) { input >> json; return json::fromJson(json); } catch (const std::exception& e) { - G2O_ERROR("Exception while saving: {}", e.what()); + G2O_ERROR("Exception while loading: {}", e.what()); } return std::nullopt; } diff --git a/unit_test/general/graph_io.cpp b/unit_test/general/graph_io.cpp index 690b1ce05..c66316ff1 100644 --- a/unit_test/general/graph_io.cpp +++ b/unit_test/general/graph_io.cpp @@ -44,6 +44,10 @@ #include "gtest/gtest.h" #include "unit_test/test_helper/allocate_optimizer.h" +#ifdef G2O_HAVE_JSON +#include "g2o/core/io/io_json.h" +#endif + using namespace testing; // NOLINT MATCHER(ParamEqual, "") { @@ -317,6 +321,20 @@ INSTANTIATE_TEST_SUITE_P(AbstractGraph, AbstractGraphIO, kFileformatsToTest); INSTANTIATE_TEST_SUITE_P(OptimizableGraphGraph, OptimizableGraphIO, kFileformatsToTest); +TEST(OptimizableGraphIO, IllegalJson) { +#ifdef G2O_HAVE_JSON + std::istringstream input(R"({"name": "Joe", "age": 42})"); + g2o::IoJson json; +#ifdef NDEBUG + EXPECT_FALSE(json.load(input).has_value()); +#else + EXPECT_DEBUG_DEATH(json.load(input), "file json.hpp"); +#endif +#else + SUCCEED(); +#endif +} + TEST(OptimizableGraphIO, FileFilter) { EXPECT_THAT(g2o::io::getFileFilter(false), Not(IsEmpty())); EXPECT_THAT(g2o::io::getFileFilter(true), Not(IsEmpty()));