From a1293fbd7987b521a93c08691ecd14e79979c768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 10 Jan 2017 11:45:12 +0200 Subject: [PATCH] Add unique constraint to enumeration parameters Enum parameters can now be declared unique. This prevents multiple values for enumerations which only accept one value. --- include/maxscale/modinfo.h | 3 ++- server/core/config.c | 9 +++++++++ server/core/test/testconfig.c | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/maxscale/modinfo.h b/include/maxscale/modinfo.h index bd6395822..7e607d143 100644 --- a/include/maxscale/modinfo.h +++ b/include/maxscale/modinfo.h @@ -98,7 +98,8 @@ enum mxs_module_param_options MXS_MODULE_OPT_PATH_X_OK = (1 << 1), /**< PATH: Execute permission to path required */ MXS_MODULE_OPT_PATH_R_OK = (1 << 2), /**< PATH: Read permission to path required */ MXS_MODULE_OPT_PATH_W_OK = (1 << 3), /**< PATH: Write permission to path required */ - MXS_MODULE_OPT_PATH_F_OK = (1 << 4) /**< PATH: Path must exist */ + MXS_MODULE_OPT_PATH_F_OK = (1 << 4), /**< PATH: Path must exist */ + MXS_MODULE_OPT_ENUM_UNIQUE = (1 << 5) /**< ENUM: Only one value can be defined */ }; /** String to enum value mappings */ diff --git a/server/core/config.c b/server/core/config.c index cabd933ef..627fa2c82 100644 --- a/server/core/config.c +++ b/server/core/config.c @@ -3262,7 +3262,16 @@ bool config_param_is_valid(const MXS_MODULE_PARAM *params, const char *key, break; } } + tok = strtok_r(NULL, delim, &endptr); + + if ((params[i].options & MXS_MODULE_OPT_ENUM_UNIQUE) && (tok || !valid)) + { + /** Either the only defined enum value is not valid + * or multiple values were defined */ + valid = false; + break; + } } } break; diff --git a/server/core/test/testconfig.c b/server/core/test/testconfig.c index 3521e3c2f..a8d317922 100644 --- a/server/core/test/testconfig.c +++ b/server/core/test/testconfig.c @@ -39,6 +39,7 @@ int test_validity() {"p5", MXS_MODULE_PARAM_ENUM, "a", MXS_MODULE_OPT_NONE, enum_values}, {"p6", MXS_MODULE_PARAM_PATH, "/tmp", MXS_MODULE_OPT_PATH_F_OK}, {"p7", MXS_MODULE_PARAM_SERVICE, "my-service"}, + {"p8", MXS_MODULE_PARAM_ENUM, "a", MXS_MODULE_OPT_ENUM_UNIQUE, enum_values}, {MXS_END_MODULE_PARAMS} }; @@ -76,7 +77,13 @@ int test_validity() TEST(config_param_is_valid(params, "p5", "a", &ctx)); TEST(config_param_is_valid(params, "p5", "b", &ctx)); TEST(config_param_is_valid(params, "p5", "c", &ctx)); + TEST(config_param_is_valid(params, "p5", "a,b", &ctx)); + TEST(config_param_is_valid(params, "p5", "b,a", &ctx)); + TEST(config_param_is_valid(params, "p5", "a, b, c", &ctx)); + TEST(config_param_is_valid(params, "p5", "c,a,b", &ctx)); TEST(!config_param_is_valid(params, "p5", "d", &ctx)); + TEST(!config_param_is_valid(params, "p5", "a,d", &ctx)); + TEST(!config_param_is_valid(params, "p5", "a,b,c,d", &ctx)); /** Path parameter */ TEST(config_param_is_valid(params, "p6", "/tmp", &ctx)); @@ -90,6 +97,18 @@ int test_validity() TEST(!config_param_is_valid(params, "p7", "test-service", NULL)); TEST(!config_param_is_valid(params, "p7", "no-such-service", &ctx)); + /** Unique enum parameter */ + TEST(config_param_is_valid(params, "p8", "a", &ctx)); + TEST(config_param_is_valid(params, "p8", "b", &ctx)); + TEST(config_param_is_valid(params, "p8", "c", &ctx)); + TEST(!config_param_is_valid(params, "p8", "a,b", &ctx)); + TEST(!config_param_is_valid(params, "p8", "b,a", &ctx)); + TEST(!config_param_is_valid(params, "p8", "a, b, c", &ctx)); + TEST(!config_param_is_valid(params, "p8", "c,a,b", &ctx)); + TEST(!config_param_is_valid(params, "p8", "d", &ctx)); + TEST(!config_param_is_valid(params, "p8", "a,d", &ctx)); + TEST(!config_param_is_valid(params, "p8", "a,b,c,d", &ctx)); + return 0; }