diff --git a/CMakeLists.txt b/CMakeLists.txt index d4b42f9dba591f85fd379d0a86a1661621807488..3181f9ccc9564e478ed640c81424a373e4f7b1f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ INTERFACE # and one for the validator add_library(json-validator SHARED + src/debug.cpp src/json-validator.cpp src/schema.cpp ) @@ -108,6 +109,10 @@ target_link_libraries(json-validator PUBLIC libubox) +IF(JSON_VALIDATOR_DEBUG) + set(CMAKE_CXX_FLAGS_DEBUG "-DJSON_VALIDATOR_DEBUG") +ENDIF() + #testing IF(CMAKE_BUILD_TYPE STREQUAL Debug) OPTION(ENABLE_BUILD_TESTS "Build tests" ON) diff --git a/src/json-validator.cpp b/src/json-validator.cpp index fa288fceaad6fb37d223e2ac1433e2bc2358a635..d7533e40335f6d393960785f1d6e080d2d74f1be 100644 --- a/src/json-validator.cpp +++ b/src/json-validator.cpp @@ -17,9 +17,11 @@ extern "C" #include <stdbool.h> #include <json-c/json.h> +} + +#include "debug.h" #include "schema.h" #include "json-validator.h" -} using nlohmann::json; using nlohmann::json_uri; @@ -135,7 +137,6 @@ out: return rv; } - bool schema_validator_validate_jobj(struct json_object *j_object, const char *object, const char *method, enum schema_call_t type) { struct schema_object *s_object; @@ -180,6 +181,8 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob obj = json::parse(json_object_get_string(j_object)); + dbg("validation object -- %s\n", obj.dump().c_str()); + dbg("validation schema -- %s\n", sch.dump().c_str()); rv = json_object_validate_schema(obj, sch); out_definitions: diff --git a/src/schema.cpp b/src/schema.cpp index a9b31db9dd2ba569847f8628a6b9ff022aa8a64c..23e7b6d5207d8aa5da9050f99195cc8fde8c225b 100644 --- a/src/schema.cpp +++ b/src/schema.cpp @@ -11,6 +11,7 @@ extern "C" #include <stdlib.h> #include <regex.h> } +#include "debug.h" #include "schema.h" #define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas" @@ -19,7 +20,7 @@ struct schema_context s_ctx; static void schema_flush_method(struct schema_method *s_method) { - //fprintf(stderr, "cleaning method %s\n", (char *)s_method->avl.key); + dbg("cleaning method %s\n", (char *)s_method->avl.key); if (s_method->b_input) free(s_method->b_input); @@ -37,7 +38,7 @@ static void schema_flush_methods(struct schema_object *s_object) { struct schema_method *s_method, *tmp; - //fprintf(stderr, "flushing all methods of object %s\n", (char *)s_object->avl.key); + dbg("flushing all methods of object %s\n", (char *)s_object->avl.key); avl_for_each_element_safe(&s_object->schema_methods, s_method, avl, tmp) { avl_delete(&s_object->schema_methods, &s_method->avl); @@ -49,7 +50,7 @@ static void schema_flush_methods(struct schema_object *s_object) static void schema_flush_object(struct schema_object *s_object) { - //fprintf(stderr, "cleaning object %s\n", (char *)s_object->avl.key); + dbg("cleaning object %s\n", (char *)s_object->avl.key); schema_flush_methods(s_object); free((void *)s_object->avl.key); @@ -68,12 +69,13 @@ void schema_flush_objects(void) { struct schema_object *s_object, *tmp; - //fprintf(stderr, "cleaning all schema objects\n"); + dbg("cleaning all schema objects\n"); avl_for_each_element_safe(&s_ctx.schema_objects, s_object, avl, tmp) { avl_delete(&s_ctx.schema_objects, &s_object->avl); schema_flush_object(s_object); } + return; } @@ -87,7 +89,7 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob if (strncmp(blobmsg_name(properties), "properties", 11)) continue; - ////fprintf(stderr, "%s %d method name = %s\n", __func__, __LINE__, blobmsg_name(object)); + dbg("%s %d method name = %s\n", __func__, __LINE__, blobmsg_name(object)); s_method = (struct schema_method *)calloc(1, sizeof(*s_method)); if (!s_method) continue; @@ -96,22 +98,18 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob avl_insert(&schema_object->schema_methods, &s_method->avl); blobmsg_for_each_attr(method, properties, rem2) { - ////fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method)); + dbg("%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method)); if (!strncmp(blobmsg_name(method), "input", 6)) { int len = blob_raw_len(method); s_method->b_input = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); memcpy(s_method->b_input, method, len); - - //fprintf(stderr, "b_input = %s\n", blobmsg_format_json(s_method->b_input, true)); } else if (!strncmp(blobmsg_name(method), "output", 7)) { int len = blob_raw_len(method); s_method->b_output = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); memcpy(s_method->b_output, method, len); - - //fprintf(stderr, "b_output = %s\n", blobmsg_format_json(s_method->b_output, true)); } } } @@ -129,7 +127,7 @@ schema_setup_file(const char *path) blob_buf_init(&acl, 0); if (!blobmsg_add_json_from_file(&acl, path)) { - //fprintf(stderr, "Failed to parse %s\n", path); + dbg("Failed to parse %s\n", path); goto out; } @@ -143,8 +141,7 @@ schema_setup_file(const char *path) blob_for_each_attr(root, acl.head, rem) { if (!strncmp(blobmsg_name(root), "regex", 6)) { schema_object->regex = blobmsg_get_bool(root); - //fprintf(stderr, "%s: regex enabled %d\n", __func__, schema_object->regex); - + dbg("%s: regex enabled %d\n", __func__, schema_object->regex); } if (!strncmp(blobmsg_name(root), "object", 7)) { @@ -169,16 +166,17 @@ schema_setup_file(const char *path) continue; schema_parse_object(schema_object, object); - //fprintf(stderr, "added schema %s\n", schema_object->object_name); + dbg("added schema %s\n", schema_object->object_name); } } if (schema_object->regex) { int rv; - //fprintf(stderr, "parsing regex for %s!\n", (char *)schema_object->avl.key ); + + dbg("parsing regex for %s!\n", (char *)schema_object->avl.key ); rv = regcomp(&schema_object->regex_exp, (char *) schema_object->avl.key, 0); if (rv) { - //fprintf(stderr, "%s: invalid regex: %s, flushing validation schema!\n", __func__, (char *) schema_object->avl.key); + dbg("%s: invalid regex: %s, flushing validation schema!\n", __func__, (char *) schema_object->avl.key); goto out_flush; } } @@ -201,14 +199,14 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m struct schema_object *s_object; struct schema_method *s_method; - //fprintf(stderr, "%s: object = %s method = %s\n", __func__, object, method); + dbg("%s: object = %s method = %s\n", __func__, object, method); s_object = schema_get_object_schema(object); if (!s_object) return NULL; avl_for_each_element(&s_object->schema_methods, s_method, avl) { - //fprintf(stderr, "%s: method = %s, avl.key = %s\n", __func__, method, (char *)s_method->avl.key); + dbg("%s: method = %s, avl.key = %s\n", __func__, method, (char *)s_method->avl.key); if (!strncmp(method, (char *) s_method->avl.key, METHOD_NAME_MAX_LEN)) return s_method; } @@ -230,10 +228,10 @@ struct schema_object *schema_get_object_schema(const char *object) { struct schema_object *s_object; - //fprintf(stderr, "%s: object = %s\n", __func__, object); + dbg("%s: object = %s\n", __func__, object); avl_for_each_element(&s_ctx.schema_objects, s_object, avl) { - //fprintf(stderr, "%s: object = %s, avl.key = %s, regex = %d\n", __func__, object, (char *)s_object->avl.key, s_object->regex); + dbg("%s: object = %s, avl.key = %s, regex = %d\n", __func__, object, (char *)s_object->avl.key, s_object->regex); if (s_object->regex) { if (!regexec(&s_object->regex_exp, object, 0, NULL, 0)) return s_object; @@ -258,7 +256,7 @@ schema_setup_json(void) return; for (i = 0; i < gl.gl_pathc; i++) { - //fprintf(stderr, "path = %s\n", gl.gl_pathv[i]); + dbg("path = %s\n", gl.gl_pathv[i]); schema_setup_file(gl.gl_pathv[i]); }