Newer
Older
* ubus_api_validator.c -
*
* Copyright (C) 2020 iopsys Software Solutions AB. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "ubus_api_validator.h"
#include <json-validator.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
json_object *jobj;
int result = -1;
jobj = json_object_from_file(path);
if (jobj == NULL) {
perror("could not read json file");
return result;
}
free(jobj);
return result;
}
struct json_object *ubus_methods;
// 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, "methods", &ubus_methods);
for (i = 0; i < json_object_array_length(ubus_methods); i++) {
struct json_object *ubus_method;
struct json_object *ubus_rc;
struct ubus_result *res = NULL;
int rc = 0;
const char *defstr = "no result";
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, "method", &ubus_method);
json_object_object_get_ex(tmp, "args", &ubus_args);
json_object_object_get_ex(tmp, "rc", &ubus_rc);
rc = json_object_get_int(ubus_rc);
res = ubus_call(object, method, args);
rv = asprintf(&format, "object: %s method:%s args:%s rc: %d result:%s",
object, method, args, rc,
json_object_to_json_string(res->result));
if (rv == -1) {
fprintf(stderr, "asprintf() failed: insufficient memory!\n");
return EXIT_FAILURE;
}
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 == -1) {
fprintf(stderr, "asprintf() failed: insufficient memory!\n");
return EXIT_FAILURE;
}
}
if (rv_validation && rc == res->rc)
ok(true, format);
else {
ok(false, format);
failed = 1;
done_testing();
if (failed)
return -1;
else
return 0;