Skip to content
Snippets Groups Projects
Commit f343ca98 authored by Jakob Olsson's avatar Jakob Olsson
Browse files

add debug prints and debug flag JSON_VALIDATOR_DEBUG

parent f120a612
No related branches found
No related tags found
No related merge requests found
...@@ -64,6 +64,7 @@ INTERFACE ...@@ -64,6 +64,7 @@ INTERFACE
# and one for the validator # and one for the validator
add_library(json-validator add_library(json-validator
SHARED SHARED
src/debug.cpp
src/json-validator.cpp src/json-validator.cpp
src/schema.cpp src/schema.cpp
) )
...@@ -108,6 +109,10 @@ target_link_libraries(json-validator ...@@ -108,6 +109,10 @@ target_link_libraries(json-validator
PUBLIC PUBLIC
libubox) libubox)
IF(JSON_VALIDATOR_DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "-DJSON_VALIDATOR_DEBUG")
ENDIF()
#testing #testing
IF(CMAKE_BUILD_TYPE STREQUAL Debug) IF(CMAKE_BUILD_TYPE STREQUAL Debug)
OPTION(ENABLE_BUILD_TESTS "Build tests" ON) OPTION(ENABLE_BUILD_TESTS "Build tests" ON)
......
...@@ -17,9 +17,11 @@ extern "C" ...@@ -17,9 +17,11 @@ extern "C"
#include <stdbool.h> #include <stdbool.h>
#include <json-c/json.h> #include <json-c/json.h>
}
#include "debug.h"
#include "schema.h" #include "schema.h"
#include "json-validator.h" #include "json-validator.h"
}
using nlohmann::json; using nlohmann::json;
using nlohmann::json_uri; using nlohmann::json_uri;
...@@ -135,7 +137,6 @@ out: ...@@ -135,7 +137,6 @@ out:
return rv; return rv;
} }
bool schema_validator_validate_jobj(struct json_object *j_object, const char *object, const char *method, enum schema_call_t type) 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; struct schema_object *s_object;
...@@ -180,6 +181,8 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob ...@@ -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)); 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); rv = json_object_validate_schema(obj, sch);
out_definitions: out_definitions:
......
...@@ -11,6 +11,7 @@ extern "C" ...@@ -11,6 +11,7 @@ extern "C"
#include <stdlib.h> #include <stdlib.h>
#include <regex.h> #include <regex.h>
} }
#include "debug.h"
#include "schema.h" #include "schema.h"
#define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas" #define JSON_SCHEMA_DIR "/usr/share/rpcd/schemas"
...@@ -19,7 +20,7 @@ struct schema_context s_ctx; ...@@ -19,7 +20,7 @@ struct schema_context s_ctx;
static void schema_flush_method(struct schema_method *s_method) 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) if (s_method->b_input)
free(s_method->b_input); free(s_method->b_input);
...@@ -37,7 +38,7 @@ static void schema_flush_methods(struct schema_object *s_object) ...@@ -37,7 +38,7 @@ static void schema_flush_methods(struct schema_object *s_object)
{ {
struct schema_method *s_method, *tmp; 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_for_each_element_safe(&s_object->schema_methods, s_method, avl, tmp) {
avl_delete(&s_object->schema_methods, &s_method->avl); avl_delete(&s_object->schema_methods, &s_method->avl);
...@@ -49,7 +50,7 @@ static void schema_flush_methods(struct schema_object *s_object) ...@@ -49,7 +50,7 @@ static void schema_flush_methods(struct schema_object *s_object)
static void schema_flush_object(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); schema_flush_methods(s_object);
free((void *)s_object->avl.key); free((void *)s_object->avl.key);
...@@ -68,12 +69,13 @@ void schema_flush_objects(void) ...@@ -68,12 +69,13 @@ void schema_flush_objects(void)
{ {
struct schema_object *s_object, *tmp; 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_for_each_element_safe(&s_ctx.schema_objects, s_object, avl, tmp) {
avl_delete(&s_ctx.schema_objects, &s_object->avl); avl_delete(&s_ctx.schema_objects, &s_object->avl);
schema_flush_object(s_object); schema_flush_object(s_object);
} }
return; return;
} }
...@@ -87,7 +89,7 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -87,7 +89,7 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
if (strncmp(blobmsg_name(properties), "properties", 11)) if (strncmp(blobmsg_name(properties), "properties", 11))
continue; 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)); s_method = (struct schema_method *)calloc(1, sizeof(*s_method));
if (!s_method) if (!s_method)
continue; continue;
...@@ -96,22 +98,18 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -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); avl_insert(&schema_object->schema_methods, &s_method->avl);
blobmsg_for_each_attr(method, properties, rem2) { 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)) { if (!strncmp(blobmsg_name(method), "input", 6)) {
int len = blob_raw_len(method); int len = blob_raw_len(method);
s_method->b_input = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); s_method->b_input = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
memcpy(s_method->b_input, method, 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)) { else if (!strncmp(blobmsg_name(method), "output", 7)) {
int len = blob_raw_len(method); int len = blob_raw_len(method);
s_method->b_output = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len); s_method->b_output = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
memcpy(s_method->b_output, method, 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) ...@@ -129,7 +127,7 @@ schema_setup_file(const char *path)
blob_buf_init(&acl, 0); blob_buf_init(&acl, 0);
if (!blobmsg_add_json_from_file(&acl, path)) { 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; goto out;
} }
...@@ -143,8 +141,7 @@ schema_setup_file(const char *path) ...@@ -143,8 +141,7 @@ schema_setup_file(const char *path)
blob_for_each_attr(root, acl.head, rem) { blob_for_each_attr(root, acl.head, rem) {
if (!strncmp(blobmsg_name(root), "regex", 6)) { if (!strncmp(blobmsg_name(root), "regex", 6)) {
schema_object->regex = blobmsg_get_bool(root); 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)) { if (!strncmp(blobmsg_name(root), "object", 7)) {
...@@ -169,16 +166,17 @@ schema_setup_file(const char *path) ...@@ -169,16 +166,17 @@ schema_setup_file(const char *path)
continue; continue;
schema_parse_object(schema_object, object); 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) { if (schema_object->regex) {
int rv; 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); rv = regcomp(&schema_object->regex_exp, (char *) schema_object->avl.key, 0);
if (rv) { 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; goto out_flush;
} }
} }
...@@ -201,14 +199,14 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m ...@@ -201,14 +199,14 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m
struct schema_object *s_object; struct schema_object *s_object;
struct schema_method *s_method; 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); s_object = schema_get_object_schema(object);
if (!s_object) if (!s_object)
return NULL; return NULL;
avl_for_each_element(&s_object->schema_methods, s_method, avl) { 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)) if (!strncmp(method, (char *) s_method->avl.key, METHOD_NAME_MAX_LEN))
return s_method; return s_method;
} }
...@@ -230,10 +228,10 @@ struct schema_object *schema_get_object_schema(const char *object) ...@@ -230,10 +228,10 @@ struct schema_object *schema_get_object_schema(const char *object)
{ {
struct schema_object *s_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) { 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 (s_object->regex) {
if (!regexec(&s_object->regex_exp, object, 0, NULL, 0)) if (!regexec(&s_object->regex_exp, object, 0, NULL, 0))
return s_object; return s_object;
...@@ -258,7 +256,7 @@ schema_setup_json(void) ...@@ -258,7 +256,7 @@ schema_setup_json(void)
return; return;
for (i = 0; i < gl.gl_pathc; i++) { 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]); schema_setup_file(gl.gl_pathv[i]);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment