diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2017-07-31 16:28:57 -0400 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2017-08-02 10:53:39 -0400 |
commit | 782672ef01d5903076b13e765cecedaea4981ecf (patch) | |
tree | 1df2928f9ea28c012edb89771327ed3de1429680 /src/mongo/util | |
parent | ecb31a3631780bfa1cd04664aa319374317c374d (diff) | |
download | mongo-782672ef01d5903076b13e765cecedaea4981ecf.tar.gz |
SERVER-30361 Do not accept map/sequence for scalar config value
Diffstat (limited to 'src/mongo/util')
-rw-r--r-- | src/mongo/util/options_parser/options_parser.cpp | 5 | ||||
-rw-r--r-- | src/mongo/util/options_parser/options_parser_test.cpp | 76 |
2 files changed, 81 insertions, 0 deletions
diff --git a/src/mongo/util/options_parser/options_parser.cpp b/src/mongo/util/options_parser/options_parser.cpp index 191d9ad8b80..6d3cb66514e 100644 --- a/src/mongo/util/options_parser/options_parser.cpp +++ b/src/mongo/util/options_parser/options_parser.cpp @@ -341,6 +341,11 @@ Status YAMLNodeToValue(const YAML::Node& YAMLNode, return Status::OK(); } + if (!YAMLNode.IsScalar() && !YAMLNode.IsNull()) { + return Status(ErrorCodes::BadValue, + str::stream() << "Scalar option '" << key << "' must be a single value"); + } + // Our YAML parser reads everything as a string, so we need to parse it ourselves. std::string stringVal = YAMLNode.Scalar(); return stringToValue(stringVal, type, key, value); diff --git a/src/mongo/util/options_parser/options_parser_test.cpp b/src/mongo/util/options_parser/options_parser_test.cpp index 7abb4540209..c6cdebdd291 100644 --- a/src/mongo/util/options_parser/options_parser_test.cpp +++ b/src/mongo/util/options_parser/options_parser_test.cpp @@ -1834,6 +1834,82 @@ TEST(Parsing, BadConfigFileOption) { ASSERT_NOT_OK(parser.run(testOpts, argv, env_map, &environment)); } +TEST(Parsing, MapForScalarMismatch) { + OptionsParserTester parser; + moe::Environment environment; + moe::OptionSection testOpts; + + testOpts.addOptionChaining("config", "config", moe::Int, "Config file to parse"); + testOpts.addOptionChaining("str", "str", moe::String, ""); + + std::vector<std::string> argv; + argv.push_back("binaryname"); + argv.push_back("--config"); + argv.push_back("config.json"); + std::map<std::string, std::string> env_map; + + parser.setConfig("config.json", R"cfg({ str: { elem: "val" } })cfg"); + + ASSERT_NOT_OK(parser.run(testOpts, argv, env_map, &environment)); +} + +TEST(Parsing, ScalarForMapMismatch) { + OptionsParserTester parser; + moe::Environment environment; + moe::OptionSection testOpts; + + testOpts.addOptionChaining("config", "config", moe::Int, "Config file to parse"); + testOpts.addOptionChaining("strmap", "strmap", moe::StringMap, ""); + + std::vector<std::string> argv; + argv.push_back("binaryname"); + argv.push_back("--config"); + argv.push_back("config.json"); + std::map<std::string, std::string> env_map; + + parser.setConfig("config.json", R"cfg({ str: "val" })cfg"); + + ASSERT_NOT_OK(parser.run(testOpts, argv, env_map, &environment)); +} + +TEST(Parsing, ListForScalarMismatch) { + OptionsParserTester parser; + moe::Environment environment; + moe::OptionSection testOpts; + + testOpts.addOptionChaining("config", "config", moe::Int, "Config file to parse"); + testOpts.addOptionChaining("str", "str", moe::String, ""); + + std::vector<std::string> argv; + argv.push_back("binaryname"); + argv.push_back("--config"); + argv.push_back("config.json"); + std::map<std::string, std::string> env_map; + + parser.setConfig("config.json", R"cfg({ str: ["val"] })cfg"); + + ASSERT_NOT_OK(parser.run(testOpts, argv, env_map, &environment)); +} + +TEST(Parsing, ScalarForListMismatch) { + OptionsParserTester parser; + moe::Environment environment; + moe::OptionSection testOpts; + + testOpts.addOptionChaining("config", "config", moe::Int, "Config file to parse"); + testOpts.addOptionChaining("strlist", "strlist", moe::StringVector, ""); + + std::vector<std::string> argv; + argv.push_back("binaryname"); + argv.push_back("--config"); + argv.push_back("config.json"); + std::map<std::string, std::string> env_map; + + parser.setConfig("config.json", R"cfg({ str: "val" })cfg"); + + ASSERT_NOT_OK(parser.run(testOpts, argv, env_map, &environment)); +} + TEST(ConfigFromFilesystem, JSONGood) { moe::OptionsParser parser; moe::Environment environment; |