Skip to content
Snippets Groups Projects
Commit 0e82b6ed authored by Suru Dissanaike's avatar Suru Dissanaike
Browse files

added arg validation, changed json-format

parent 7f5655fe
Branches
No related tags found
No related merge requests found
{
"object": "demo",
"calls": [
{
"call": "unixtime",
"args": {
"format": "%d--%d--%d %d::%d::%d"
}
},
{
"call": "unixtimer"
},
{
"call": "unixtime"
}
]
}
{
"object": "demo",
"calls": [
{
"call": "unixtime",
"args": {
"format": "%d--%d--%d %d::%d::%d"
}
},
{
"call": "unixtimer"
},
{
"call": "unixtime"
}
]
}
{
"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
}
]
}
......@@ -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;
......
{
"object": "demo",
"calls": [
{
"call": "unixtime",
"args": {
"format": "%d--%d--%d %d::%d::%d"
}
},
{
"call": "unixtime"
},
{
"call": "unixtime"
}
]
}
......@@ -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();
......
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment