diff --git a/json/ubus copy 2.json b/json/ubus copy 2.json deleted file mode 100644 index 9d12f24c56e732d298a84755be9d347b87d38adf..0000000000000000000000000000000000000000 --- a/json/ubus copy 2.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "object": "demo", - "calls": [ - { - "call": "unixtime", - "args": { - "format": "%d--%d--%d %d::%d::%d" - } - }, - { - "call": "unixtimer" - }, - { - "call": "unixtime" - } - ] -} - diff --git a/json/ubus copy.json b/json/ubus copy.json deleted file mode 100644 index 9d12f24c56e732d298a84755be9d347b87d38adf..0000000000000000000000000000000000000000 --- a/json/ubus copy.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "object": "demo", - "calls": [ - { - "call": "unixtime", - "args": { - "format": "%d--%d--%d %d::%d::%d" - } - }, - { - "call": "unixtimer" - }, - { - "call": "unixtime" - } - ] -} - diff --git a/json/ubus.json b/json/ubus.json index 9d12f24c56e732d298a84755be9d347b87d38adf..12881c3879c28f68dd7ee86eb548c9e8e01c2e17 100644 --- a/json/ubus.json +++ b/json/ubus.json @@ -1,18 +1,31 @@ { "object": "demo", - "calls": [ + "methods": [ { - "call": "unixtime", + "method": "unixtime", + "rc":0 + }, + { + "method": "unixtime", "args": { "format": "%d--%d--%d %d::%d::%d" - } + }, + "rc":0 }, { - "call": "unixtimer" + "method": "unixtime", + "args": { + "format": 0 + }, + "rc":0 }, { - "call": "unixtime" + "method": "unixtime-format", + "args": { + "format": 0 + }, + "rc":2 + } ] } - diff --git a/main.c b/main.c index 26f2db3d62c7d01aabe75a6a057bb7729fb5aaa6..e1968d40ad481c8ef5d2a2178b9af8091db75b0a 100644 --- a/main.c +++ b/main.c @@ -136,7 +136,7 @@ int main(int argc, char *argv[]) { } } else if (global_args.path == NULL && global_args.dir_path == NULL) { printf("trying default file... \n"); - validate_path("./ubus.json"); + validate_path("./json/ubus.json"); } return 0; diff --git a/ubus.json b/ubus.json deleted file mode 100644 index 36f29f690674fb0ed71d5e0cf76f9ca4861b7a50..0000000000000000000000000000000000000000 --- a/ubus.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "object": "demo", - "calls": [ - { - "call": "unixtime", - "args": { - "format": "%d--%d--%d %d::%d::%d" - } - }, - { - "call": "unixtime" - }, - { - "call": "unixtime" - } - ] -} - diff --git a/ubus_api_validator.c b/ubus_api_validator.c index e98cf225745a4ebd370e977830616faad01d4fe6..6557b4d2678c53e69ea907d32b1e3db6b3a44140 100644 --- a/ubus_api_validator.c +++ b/ubus_api_validator.c @@ -49,16 +49,16 @@ int validate_path(char *path) { static int validate_jobj(struct json_object *jobj) { - struct json_object *result = NULL; struct json_object *ubus_object; - struct json_object *ubus_calls; + struct json_object *ubus_methods; + const char *object; int failed = 0; /*Now printing the json object*/ // printf("\n json object: %s \n", json_object_to_json_string(jobj)); json_object_object_get_ex(jobj, "object", &ubus_object); - json_object_object_get_ex(jobj, "calls", &ubus_calls); + json_object_object_get_ex(jobj, "methods", &ubus_methods); // init TAP plan(NO_PLAN); @@ -66,51 +66,68 @@ static int validate_jobj(struct json_object *jobj) { int i = 0; object = json_object_get_string(ubus_object); - for (i = 0; i < json_object_array_length(ubus_calls); i++) { + for (i = 0; i < json_object_array_length(ubus_methods); i++) { struct json_object *tmp; struct json_object *ubus_method; struct json_object *ubus_args; + struct json_object *ubus_rc; + struct ubus_result *res = NULL; + const char *method; const char *args; + int rc = 0; + const char *defstr = "no result"; - int rv; + int rv = 0; + int rv_validation = 0; + char *format = NULL; - tmp = json_object_array_get_idx(ubus_calls, i); + tmp = json_object_array_get_idx(ubus_methods, i); // printf("json-array[%d] = %s\n", i, json_object_to_json_string(tmp)); - json_object_object_get_ex(tmp, "call", &ubus_method); + json_object_object_get_ex(tmp, "method", &ubus_method); json_object_object_get_ex(tmp, "args", &ubus_args); + json_object_object_get_ex(tmp, "rc", &ubus_rc); method = json_object_get_string(ubus_method); args = json_object_get_string(ubus_args); + rc = json_object_get_int(ubus_rc); - // printf("object: %s\n", object); - // printf("call: %s\n", method); - // printf("args: %s\n", args); + res = ubus_call(object, method, args); - result = ubus_call(object, method, args); + if (res->rc == 0) { - // printf("\n json_object_to_json_string of result: %s \n", - // json_object_to_json_string(result)); + rv = asprintf(&format, "object: %s method:%s args:%s rc: %d result:%s", + object, method, args, rc, + json_object_to_json_string(res->result)); - char *res; - rv = asprintf(&res, "object: %s method:%s args:%s result:%s", object, - method, args, json_object_to_json_string(result)); - if (rv == -1) { - fprintf(stderr, "asprintf() failed: insufficient memory!\n"); - return EXIT_FAILURE; - } + if (rv == -1) { + fprintf(stderr, "asprintf() failed: insufficient memory!\n"); + return EXIT_FAILURE; + } - rv = schema_validator_validate_jobj(result, object, method, - SCHEMA_OUTPUT_CALL); + rv_validation = schema_validator_validate_jobj( + res->result, object, method, SCHEMA_OUTPUT_CALL); + } else { + rv = asprintf(&format, "object: %s method:%s args:%s rc: %d result:%s", + object, method, args, rc, defstr); - if (rv) { - ok(true, res); + if (rv == -1) { + fprintf(stderr, "asprintf() failed: insufficient memory!\n"); + return EXIT_FAILURE; + } + } + + // rc == res->rc handles negative testing + if (rv_validation || (rc == res->rc)) { + ok(true, format); } else { - ok(false, res); + ok(false, format); failed = 1; } + + free(res); } done_testing(); diff --git a/ubus_call.c b/ubus_call.c index 88a4cfa826ff901a55630243f29d8f6ef75a8738..354bc8a8b087cfc505f035d759573c5506fcf310 100644 --- a/ubus_call.c +++ b/ubus_call.c @@ -66,13 +66,14 @@ void parse_ubus_cb(struct ubus_request *req, int type, struct blob_attr *msg) { free(json_str); } -json_object *ubus_call(const char *object, const char *method, - const char *args) { +struct ubus_result *ubus_call(const char *object, const char *method, + const char *args) { struct blob_buf buff = {0}; int rv = -1; uint32_t obj_id; struct json_object *json_ubus_result; + struct ubus_result *res = NULL; ubus_lookup_id(ctx, object, &obj_id); @@ -100,13 +101,15 @@ json_object *ubus_call(const char *object, const char *method, (ubus_data_handler_t)parse_ubus_cb, &json_ubus_result, UBUS_DEFAULT_TIMEOUT); - if (rv) { - fprintf(stderr, "ubus_invoke error code %d \n", rv); - json_ubus_result = NULL; - } + res = calloc(1, sizeof(struct ubus_result)); + if (!res) + goto out; + + res->rc = rv; + res->result = json_ubus_result; out: blob_buf_free(&buff); - return json_ubus_result; + return res; } \ No newline at end of file diff --git a/ubus_call.h b/ubus_call.h index e1c18153a70c0d9e7ccb21ee8c36e36dcb5f5359..cefdc5716994ef1e79f6857c21ab5fe163304f86 100644 --- a/ubus_call.h +++ b/ubus_call.h @@ -6,11 +6,15 @@ #include <libubox/blobmsg_json.h> #include <libubus.h> #include <stdio.h> +struct ubus_result +{ + json_object *result; + int rc; +}; int ubus_init(void); -json_object *ubus_call(const char *object, const char *method, const char *args); - +struct ubus_result *ubus_call(const char *object, const char *method, const char *args); #endif /* UBUS_CALL_H */ \ No newline at end of file