From 67c8b70c7c18f59f148dca384c7a7d096201238c Mon Sep 17 00:00:00 2001 From: Roddie Kieley Date: Thu, 3 Nov 2016 23:28:21 -0230 Subject: [PATCH] DISPATCH-352 initial implementation. --- include/qpid/dispatch/dispatch.h | 6 ++++ router/src/main.c | 2 ++ src/dispatch.c | 38 +++++++++++++++++++++++ tests/config-0-empty/qdrouterd-empty.conf | 0 tests/run_unit_tests.c | 7 +++++ 5 files changed, 53 insertions(+) create mode 100644 tests/config-0-empty/qdrouterd-empty.conf diff --git a/include/qpid/dispatch/dispatch.h b/include/qpid/dispatch/dispatch.h index afc78b074a..1e8b360027 100644 --- a/include/qpid/dispatch/dispatch.h +++ b/include/qpid/dispatch/dispatch.h @@ -54,6 +54,12 @@ void qd_dispatch_free(qd_dispatch_t *qd); */ qd_error_t qd_dispatch_load_config(qd_dispatch_t *qd, const char *config_path); +/** + * Validate the configuration file. + * + * @param config_path The path to the configuration file. + */ +qd_error_t qd_dispatch_validate_config(const char *config_path); /** * @} diff --git a/router/src/main.c b/router/src/main.c index 969fc94456..b8fc04b97a 100644 --- a/router/src/main.c +++ b/router/src/main.c @@ -120,6 +120,8 @@ static void main_process(const char *config_path, const char *python_pkgdir, int dispatch = qd_dispatch(python_pkgdir); check(fd); log_source = qd_log_source("MAIN"); /* Logging is initialized by qd_dispatch. */ + qd_dispatch_validate_config(config_path); + check(fd); qd_dispatch_load_config(dispatch, config_path); check(fd); diff --git a/src/dispatch.c b/src/dispatch.c index 902f097b22..16b2a408e7 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -95,6 +95,44 @@ qd_error_t qd_dispatch_load_config(qd_dispatch_t *qd, const char *config_path) return qd_error_code(); } +qd_error_t qd_dispatch_validate_config(const char *config_path) +{ + FILE* config_file = NULL; + char config_data = '\0'; + qd_error_t validation_error = QD_ERROR_CONFIG; + + do { + if (!config_path) { + validation_error = qd_error(QD_ERROR_VALUE, "Configuration path value was empty"); + break; + } + + config_file = fopen(config_path, "r"); + if (!config_file) { + validation_error = qd_error(QD_ERROR_NOT_FOUND, "Configuration file could not be opened"); + break; + } + + // TODO Check the actual minimum number of bytes required for the smallest valid configuration file + if (!fread((void*)&config_data, 1, 1, config_file)) { + validation_error = qd_error(QD_ERROR_CONFIG, "Configuration file was empty"); + break; + } + + // TODO Add real validation code + + validation_error = QD_ERROR_NONE; + } while (false); // do once + + if (config_file) + { + fclose(config_file); + } + + return validation_error; +} + + /** * The Container Entity has been deprecated and will be removed in the future. Use the RouterEntity instead. */ diff --git a/tests/config-0-empty/qdrouterd-empty.conf b/tests/config-0-empty/qdrouterd-empty.conf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/run_unit_tests.c b/tests/run_unit_tests.c index df4fe56139..7a8b1d4347 100644 --- a/tests/run_unit_tests.c +++ b/tests/run_unit_tests.c @@ -41,6 +41,13 @@ int main(int argc, char** argv) // Call qd_dispatch() first initialize allocator used by other tests. qd_dispatch_t *qd = qd_dispatch(0); + + qd_dispatch_validate_config(argv[1]); + if (qd_error_code()) { + printf("Config failed: %s\n", qd_error_message()); + return 1; + } + qd_dispatch_load_config(qd, argv[1]); if (qd_error_code()) { printf("Config failed: %s\n", qd_error_message());