add a test to cover the fix to find_key's infinite loop/crash

Adds a test with a schema example containing a super cycle and a .cfg file containing an unknown key to this schema. Previously, this would cause an infinite loop or crash. Now an exception for invalid key should be thrown.
This commit is contained in:
Rafael Fillipe Silva 2023-12-20 00:10:20 -03:00 committed by Pentarctagon
parent c64416bbb6
commit c7e643bffd
7 changed files with 100 additions and 0 deletions

View file

@ -263,3 +263,4 @@ feature_test_WML_macro_define/macro_define_noArgument_ParseAsExpected
feature_test_WML_macro_define/macro_define_1Argument_ParseAsExpected
test_schema_self_validator/test_schema_super_self_reference
test_schema_self_validator/test_schema_super_cycle
test_schema_validator/test_super_cycle_crashes_on_unknown_key

View file

@ -1227,6 +1227,7 @@
<Unit filename="../../src/tests/utils/wml_equivalence.cpp" />
<Unit filename="../../src/tests/utils/wml_equivalence.hpp" />
<Unit filename="../../src/tests/wml/test_macro_define.cpp" />
<Unit filename="../../src/tests/wml/schema/test_schema_validator.cpp" />
<Unit filename="../../src/tests/wml/schema/test_schema_self_validator.cpp" />
<Unit filename="../../src/theme.cpp" />
<Unit filename="../../src/theme.hpp" />

View file

@ -661,6 +661,7 @@
62D24F321519987400350848 /* context_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 62D24F311519987400350848 /* context_manager.cpp */; };
62D24F351519995200350848 /* palette_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 62D24F341519995200350848 /* palette_manager.cpp */; };
6D574EACA3483ABEE72819F0 /* statistics_record.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27764FB68F02032F1C0B6748 /* statistics_record.cpp */; };
7A7146D7893AA09891352019 /* test_schema_validator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CF14AB694764953E2CB3AF7 /* test_schema_validator.cpp */; };
77D94146A5FA29849D1A9BD8 /* multiline_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0B0F48CE9CF65D9813BE6CDC /* multiline_text.cpp */; };
7BFC4DF5BFF8CF75855BA662 /* prompt.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 67044415B63F5888193BD7A6 /* prompt.cpp */; };
7FDF4E8D9C94E7DA8F41F7BB /* tod_new_schedule.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 5D46466DBCD81B13621C7342 /* tod_new_schedule.hpp */; };
@ -2195,6 +2196,7 @@
67044415B63F5888193BD7A6 /* prompt.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = prompt.cpp; sourceTree = "<group>"; };
6FA542D78393E8FF067775DA /* edit_pbl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = edit_pbl.cpp; sourceTree = "<group>"; };
755D4555A1DEA29125E7F338 /* scroll_text.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = scroll_text.cpp; path = scroll_text.cpp; sourceTree = "<group>"; };
7CF14AB694764953E2CB3AF7 /* test_schema_validator.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = test_schema_validator.cpp; path = test_schema_validator.cpp; sourceTree = "<group>"; };
84234C54BB84519421FD4136 /* general.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = general.cpp; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* The Battle for Wesnoth.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "The Battle for Wesnoth.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@ -3162,6 +3164,7 @@
isa = PBXGroup;
children = (
26A04033A9545CFE8A226FBD /* test_schema_self_validator.cpp */,
7CF14AB694764953E2CB3AF7 /* test_schema_validator.cpp */,
);
path = schema;
sourceTree = "<group>";
@ -6580,6 +6583,7 @@
E6CF415F9FD04C35A55FB24D /* scroll_text.cpp in Sources */,
62714C2FBE84B66CF14E3722 /* test_sdl.cpp in Sources */,
529242A8856DE8D00988B9BD /* test_schema_self_validator.cpp in Sources */,
7A7146D7893AA09891352019 /* test_schema_validator.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View file

@ -31,4 +31,5 @@ tests/utils/fake_display.cpp
tests/utils/game_config_manager_tests.cpp
tests/utils/wml_equivalence.cpp
tests/wml/test_macro_define.cpp
tests/wml/schema/test_schema_validator.cpp
tests/wml/schema/test_schema_self_validator.cpp

View file

@ -0,0 +1,59 @@
/*
Copyright (C) 2003 - 2023
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-test"
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "game_version.hpp"
#include "serialization/parser.hpp"
#include "serialization/preprocessor.hpp"
#include "serialization/schema_validator.hpp"
#include "wml_exception.hpp"
BOOST_AUTO_TEST_SUITE( test_schema_validator )
BOOST_AUTO_TEST_CASE( test_super_cycle_crashes_on_unknown_key )
{
constexpr auto schema_path = "src/tests/wml/schema/test_schema_validator/test_schema_super_cycle.cfg";
constexpr auto config_path = "src/tests/wml/schema/test_schema_validator/test_super_cycle_crashes_on_unknown_key.cfg";
auto validator = schema_validation::schema_validator(schema_path, false);
validator.set_create_exceptions(true);
preproc_map defines_map;
defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
defines_map["SCHEMA_VALIDATION"] = preproc_define();
auto stream = preprocess_file(config_path, &defines_map);
config result;
BOOST_CHECK_EXCEPTION(
read(result, *stream, &validator),
wml_exception,
[] (const wml_exception& e) {
return boost::algorithm::contains(
e.dev_message,
"Invalid key 'unknown=' in tag [first]"
);
}
);
}
BOOST_AUTO_TEST_SUITE_END()

View file

@ -0,0 +1,23 @@
[wml_schema]
[tag]
name="root"
[tag]
name="main"
[tag]
name="first"
super="second"
[/tag]
[/tag]
[tag]
name="other"
[tag]
name="second"
super="main/first"
[/tag]
[/tag]
[tag]
name="second"
super="other/second"
[/tag]
[/tag]
[/wml_schema]

View file

@ -0,0 +1,11 @@
[main]
[first]
unknown="1"
[/first]
[/main]
[other]
[second]
[/second]
[/other]
[second]
[/second]