mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 07:06:32 +08:00
del the global schema, each driver maintains its own schema
This commit is contained in:
+2
-1
@@ -183,7 +183,8 @@ target_link_libraries(neuron-base vector-static csptr jansson crypto ssl mqttc y
|
||||
|
||||
#copy file for run and unit test
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/neuron.yaml DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/modbus/modbus-tcp-plugin.json DESTINATION ${CMAKE_BINARY_DIR}/schema/)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/mqtt/mqtt-plugin.json DESTINATION ${CMAKE_BINARY_DIR}/schema/)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/private_test.key DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/public_test.pem DESTINATION ${CMAKE_BINARY_DIR})
|
||||
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#ifndef _NEU_SCHEMA_H_
|
||||
#define _NEU_SCHEMA_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum neu_schema_valid_type {
|
||||
NEU_SCHEMA_VALID_INT,
|
||||
NEU_SCHEMA_VALID_REAL,
|
||||
NEU_SCHEMA_VALID_STRING,
|
||||
NEU_SCHEMA_VALID_ENUM,
|
||||
} neu_schema_valid_type_e;
|
||||
|
||||
typedef struct neu_schema_valid neu_schema_valid_t;
|
||||
|
||||
neu_schema_valid_t *neu_schema_load(char *buf, char *plugin);
|
||||
void neu_schema_free(neu_schema_valid_t *v);
|
||||
|
||||
int neu_schema_valid_param_int(neu_schema_valid_t *v, int64_t value,
|
||||
char *name);
|
||||
int neu_schema_valid_param_real(neu_schema_valid_t *v, double value,
|
||||
char *name);
|
||||
int neu_schema_valid_param_string(neu_schema_valid_t *v, char *value,
|
||||
char *name);
|
||||
int neu_schema_valid_param_enum(neu_schema_valid_t *v, int64_t value,
|
||||
char *name);
|
||||
int neu_schema_valid_tag_type(neu_schema_valid_t *v, uint8_t t);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+2
-1
@@ -49,7 +49,8 @@ cp ${lib_path}/libyaml.so ${package_name}/
|
||||
cp build/lib*.so \
|
||||
build/neuron \
|
||||
build/neuron.yaml \
|
||||
build/plugin_param_schema.json \
|
||||
build/schema/mqtt-plugin.json \
|
||||
build/schema/modbus-tcp-plugin.json \
|
||||
${package_name}/
|
||||
|
||||
tar czf ${package_name}.tar.gz ${package_name}/
|
||||
|
||||
@@ -1,73 +1,81 @@
|
||||
{
|
||||
"plugins": [
|
||||
"test-plugin"
|
||||
"tag_type": [
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
14
|
||||
],
|
||||
"test-plugin": {
|
||||
"tag_type": [
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
14
|
||||
],
|
||||
"params": [
|
||||
"host",
|
||||
"port",
|
||||
"mode",
|
||||
"baud_rate",
|
||||
"real_param"
|
||||
],
|
||||
"host": {
|
||||
"name": "host",
|
||||
"description": "host",
|
||||
"type": "string",
|
||||
"default": "127.0.0.1",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
"params": [
|
||||
"host",
|
||||
"port",
|
||||
"mode",
|
||||
"baud_rate",
|
||||
"real_param"
|
||||
],
|
||||
"host": {
|
||||
"name": "host",
|
||||
"description": "host",
|
||||
"type": "string",
|
||||
"default": "127.0.0.1",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
},
|
||||
"port": {
|
||||
"name": "port",
|
||||
"description": "port",
|
||||
"type": "int",
|
||||
"default": 502,
|
||||
"valid": {
|
||||
"min": 1024,
|
||||
"max": 65535
|
||||
}
|
||||
},
|
||||
"mode": {
|
||||
"name": "mode",
|
||||
"description": "mode",
|
||||
"type": "bool",
|
||||
"default": false,
|
||||
"valid": {}
|
||||
},
|
||||
"baud_rate": {
|
||||
"name": "baud rate",
|
||||
"description": "port",
|
||||
"type": "int",
|
||||
"default": 9600,
|
||||
"valid": {
|
||||
"value": [
|
||||
9600,
|
||||
112800
|
||||
]
|
||||
}
|
||||
},
|
||||
"real_param": {
|
||||
"name": "real param",
|
||||
"description": "real",
|
||||
"type": "real",
|
||||
"default": 11.22,
|
||||
"valid": {
|
||||
"min": 1.1,
|
||||
"max": 20.2
|
||||
}
|
||||
},
|
||||
"ca": {
|
||||
"name": "ca",
|
||||
"description": "",
|
||||
"attribute": "optional",
|
||||
"type": "file",
|
||||
"condition": {
|
||||
"field": "mode",
|
||||
"value": true
|
||||
},
|
||||
"port": {
|
||||
"name": "port",
|
||||
"description": "port",
|
||||
"type": "int",
|
||||
"default": 502,
|
||||
"valid": {
|
||||
"min": 1024,
|
||||
"max": 65535
|
||||
}
|
||||
},
|
||||
"mode": {
|
||||
"name": "mode",
|
||||
"description": "mode",
|
||||
"type": "bool",
|
||||
"default": false,
|
||||
"valid": {}
|
||||
},
|
||||
"baud_rate": {
|
||||
"name": "baud rate",
|
||||
"description": "port",
|
||||
"type": "int",
|
||||
"default": 9600,
|
||||
"valid": {
|
||||
"value": [
|
||||
9600,
|
||||
112800
|
||||
]
|
||||
}
|
||||
},
|
||||
"real_param": {
|
||||
"name": "real param",
|
||||
"description": "real",
|
||||
"type": "real",
|
||||
"default": 11.22,
|
||||
"valid": {
|
||||
"min": 1.1,
|
||||
"max": 20.2
|
||||
}
|
||||
"valid": {
|
||||
"length": 1024
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"tag_type": [
|
||||
4,
|
||||
5,
|
||||
8,
|
||||
9,
|
||||
11,
|
||||
14
|
||||
],
|
||||
"host": {
|
||||
"name": "host",
|
||||
"description": "remote device ip",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
},
|
||||
"port": {
|
||||
"name": "port",
|
||||
"description": "remote device port",
|
||||
"attribute": "required",
|
||||
"type": "int",
|
||||
"default": 502,
|
||||
"valid": {
|
||||
"min": 1,
|
||||
"max": 65535
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
{
|
||||
"client-id": {
|
||||
"name": "client-id",
|
||||
"description": "Neuron MQTT client ID",
|
||||
"type": "string",
|
||||
"attribute": "required",
|
||||
"default": "",
|
||||
"valid": {
|
||||
"length": 100
|
||||
}
|
||||
},
|
||||
"ssl": {
|
||||
"name": "ssl",
|
||||
"description": "Enable SSL connection",
|
||||
"attribute": "required",
|
||||
"type": "bool",
|
||||
"default": false,
|
||||
"valid": {}
|
||||
},
|
||||
"host": {
|
||||
"name": "host",
|
||||
"description": "MQTT broker host",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"default": "broker.emqx.io",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
},
|
||||
"port": {
|
||||
"name": "port",
|
||||
"description": "MQTT broker port",
|
||||
"attribute": "required",
|
||||
"type": "int",
|
||||
"default": 1883,
|
||||
"valid": {
|
||||
"min": 1024,
|
||||
"max": 65535
|
||||
}
|
||||
},
|
||||
"username": {
|
||||
"name": "username",
|
||||
"description": "User name",
|
||||
"attribute": "optional",
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
},
|
||||
"password": {
|
||||
"name": "password",
|
||||
"description": "Password",
|
||||
"attribute": "optional",
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"valid": {
|
||||
"length": 30
|
||||
}
|
||||
},
|
||||
"ca-path": {
|
||||
"name": "ca-path",
|
||||
"description": "",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"condition": {
|
||||
"field": "ssl",
|
||||
"value": true
|
||||
},
|
||||
"valid": {
|
||||
"length": 100
|
||||
}
|
||||
},
|
||||
"ca-file": {
|
||||
"name": "ca-file",
|
||||
"description": "",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"condition": {
|
||||
"field": "ssl",
|
||||
"value": true
|
||||
},
|
||||
"default": "",
|
||||
"valid": {
|
||||
"length": 100
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "mqtt_util.h"
|
||||
#include "schema.h"
|
||||
#include "json/json.h"
|
||||
#include <config.h>
|
||||
#include <neuron.h>
|
||||
@@ -193,71 +192,6 @@ static int decode_node_setting(const char * json_str,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int valid_node_setting(const char *file, const char *plugin_name,
|
||||
struct node_setting *setting)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(file, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *valid = neu_schema_load(buf, (char *) plugin_name);
|
||||
if (NULL == valid) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int rc = 0;
|
||||
rc = neu_schema_valid_param_string(valid, setting->client_id,
|
||||
(char *) "client-id");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -2;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_string(valid, setting->host, (char *) "host");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -4;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_int(valid, setting->port, (char *) "port");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -5;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_string(valid, setting->username,
|
||||
(char *) "username");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -6;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_string(valid, setting->password,
|
||||
(char *) "password");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -7;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_string(valid, setting->ca_file,
|
||||
(char *) "ca-file");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -8;
|
||||
}
|
||||
|
||||
rc = neu_schema_valid_param_string(valid, setting->ca_path,
|
||||
(char *) "ca-path");
|
||||
if (0 != rc) {
|
||||
neu_schema_free(valid);
|
||||
return -9;
|
||||
}
|
||||
|
||||
neu_schema_free(valid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
|
||||
{
|
||||
if (NULL == config || NULL == option) {
|
||||
@@ -270,8 +204,6 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
|
||||
return -2;
|
||||
}
|
||||
|
||||
rc = valid_node_setting(SCHEMA_FILE, MQTT_PLUGIN_NAME, &setting);
|
||||
|
||||
// MQTT option
|
||||
option->clientid = NULL; // Use random id
|
||||
option->MQTT_version = 4; // Version 3.1.1
|
||||
|
||||
@@ -661,7 +661,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
|
||||
const neu_plugin_module_t neu_plugin_module = {
|
||||
.version = NEURON_PLUGIN_VER_1_0,
|
||||
.module_name = "neuron-mqtt-plugin",
|
||||
.module_name = "mqtt-plugin",
|
||||
.module_descr = "Neuron northbound MQTT communication plugin",
|
||||
.intf_funs = &plugin_intf_funs,
|
||||
.kind = PLUGIN_KIND_SYSTEM,
|
||||
|
||||
@@ -90,7 +90,7 @@ struct neu_rest_handler cors_handler[] = {
|
||||
.url = "/api/v2/subscribe",
|
||||
},
|
||||
{
|
||||
.url = "/api/v2/schema/plugin",
|
||||
.url = "/api/v2/schema",
|
||||
},
|
||||
{
|
||||
.url = "/api/v2/node/setting",
|
||||
@@ -253,7 +253,7 @@ struct neu_rest_handler api_handlers[] = {
|
||||
{
|
||||
.method = NEU_REST_METHOD_GET,
|
||||
.type = NEU_REST_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/schema/plugin",
|
||||
.url = "/api/v2/schema",
|
||||
.value.handler = handle_get_plugin_schema,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -123,13 +123,24 @@ void handle_logout(nng_aio *aio)
|
||||
|
||||
void handle_get_plugin_schema(nng_aio *aio)
|
||||
{
|
||||
size_t len = 0;
|
||||
char schema_path[256] = { 0 };
|
||||
|
||||
VALIDATE_JWT(aio);
|
||||
|
||||
size_t len = 0;
|
||||
char * buf = NULL;
|
||||
buf = file_string_read(&len, "./plugin_param_schema.json");
|
||||
const char *plugin_name = http_get_param(aio, "plugin_name", &len);
|
||||
if (plugin_name == NULL || len <= 0) {
|
||||
http_bad_request(aio, "{\"error\": 1002}");
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(schema_path, sizeof(schema_path) - 1, "./schema/%s.json",
|
||||
plugin_name);
|
||||
|
||||
char *buf = NULL;
|
||||
buf = file_string_read(&len, schema_path);
|
||||
if (NULL == buf) {
|
||||
log_info("open ./plugin_param_schema.json error: %d", errno);
|
||||
log_info("open %s error: %d", schema_path, errno);
|
||||
http_not_found(aio, "{\"status\": \"error\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "neu_log.h"
|
||||
#include "schema.h"
|
||||
#include "json/json.h"
|
||||
|
||||
struct neu_schema_valid {
|
||||
void * root;
|
||||
void * plugin_json;
|
||||
int n_type;
|
||||
uint32_t types[32];
|
||||
};
|
||||
|
||||
static bool valid_init(void *json, neu_json_elem_t *ele, char *name);
|
||||
|
||||
neu_schema_valid_t *neu_schema_load(char *buf, char *plugin)
|
||||
{
|
||||
void * json = neu_json_decode_new(buf);
|
||||
neu_json_elem_t elem = { .name = plugin, .t = NEU_JSON_OBJECT };
|
||||
int ret = 0;
|
||||
neu_schema_valid_t *st = NULL;
|
||||
int size = 0;
|
||||
|
||||
assert(json != NULL);
|
||||
ret = neu_json_decode_value(json, &elem);
|
||||
if (ret != 0) {
|
||||
neu_json_decode_free(json);
|
||||
return st;
|
||||
}
|
||||
size = neu_json_decode_array_size_by_json(elem.v.val_object, "tag_type");
|
||||
|
||||
if (size <= 0) {
|
||||
neu_json_decode_free(json);
|
||||
return NULL;
|
||||
}
|
||||
assert(size <= 32);
|
||||
|
||||
st = calloc(1, sizeof(neu_schema_valid_t));
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
neu_json_elem_t e = { .name = NULL, .t = NEU_JSON_INT };
|
||||
if (neu_json_decode_array_by_json(elem.v.val_object, "tag_type", i, 1,
|
||||
&e) == 0) {
|
||||
st->types[st->n_type] = e.v.val_int;
|
||||
st->n_type++;
|
||||
}
|
||||
}
|
||||
|
||||
st->root = json;
|
||||
st->plugin_json = elem.v.val_object;
|
||||
|
||||
return st;
|
||||
}
|
||||
|
||||
void neu_schema_free(neu_schema_valid_t *v)
|
||||
{
|
||||
neu_json_decode_free(v->root);
|
||||
free(v);
|
||||
}
|
||||
|
||||
int neu_schema_valid_tag_type(neu_schema_valid_t *v, uint8_t t)
|
||||
{
|
||||
for (int i = 0; i < v->n_type; i++) {
|
||||
if (v->types[i] == t)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool valid_init(void *json, neu_json_elem_t *vele, char *name)
|
||||
{
|
||||
neu_json_elem_t ele = { .name = name, .t = NEU_JSON_OBJECT };
|
||||
|
||||
vele->name = "valid";
|
||||
vele->t = NEU_JSON_OBJECT;
|
||||
|
||||
return neu_json_decode_value(json, &ele) == 0 &&
|
||||
neu_json_decode_value(ele.v.val_object, vele) == 0;
|
||||
}
|
||||
|
||||
int neu_schema_valid_param_int(neu_schema_valid_t *v, int64_t value, char *name)
|
||||
{
|
||||
neu_json_elem_t valid_ele = { 0 };
|
||||
neu_json_elem_t min_ele = { .name = "min", .t = NEU_JSON_INT };
|
||||
neu_json_elem_t max_ele = { .name = "max", .t = NEU_JSON_INT };
|
||||
|
||||
if (!valid_init(v->plugin_json, &valid_ele, name)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (neu_json_decode_value(valid_ele.v.val_object, &min_ele) != 0 ||
|
||||
neu_json_decode_value(valid_ele.v.val_object, &max_ele) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (value >= min_ele.v.val_int && value <= max_ele.v.val_int) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int neu_schema_valid_param_real(neu_schema_valid_t *v, double value, char *name)
|
||||
{
|
||||
neu_json_elem_t valid_ele = { 0 };
|
||||
neu_json_elem_t min_ele = { .name = "min", .t = NEU_JSON_DOUBLE };
|
||||
neu_json_elem_t max_ele = { .name = "max", .t = NEU_JSON_DOUBLE };
|
||||
|
||||
if (!valid_init(v->plugin_json, &valid_ele, name)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (neu_json_decode_value(valid_ele.v.val_object, &min_ele) != 0 ||
|
||||
neu_json_decode_value(valid_ele.v.val_object, &max_ele) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (value >= min_ele.v.val_double && value <= max_ele.v.val_double) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int neu_schema_valid_param_string(neu_schema_valid_t *v, char *value,
|
||||
char *name)
|
||||
{
|
||||
neu_json_elem_t valid_ele = { 0 };
|
||||
neu_json_elem_t len_ele = { .name = "length", .t = NEU_JSON_INT };
|
||||
|
||||
if (!valid_init(v->plugin_json, &valid_ele, name)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (neu_json_decode_value(valid_ele.v.val_object, &len_ele) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((uint64_t) len_ele.v.val_int >= strlen(value)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int neu_schema_valid_param_enum(neu_schema_valid_t *v, int64_t value,
|
||||
char *name)
|
||||
{
|
||||
neu_json_elem_t valid_ele = { 0 };
|
||||
int size = 0;
|
||||
|
||||
if (!valid_init(v->plugin_json, &valid_ele, name)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size = neu_json_decode_array_size_by_json(valid_ele.v.val_object, "value");
|
||||
if (size <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
neu_json_elem_t ve = { .name = NULL, .t = NEU_JSON_INT };
|
||||
if (neu_json_decode_array_by_json(valid_ele.v.val_object, "value", i, 1,
|
||||
&ve) == 0) {
|
||||
if (ve.v.val_int == value) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
+1
-12
@@ -110,15 +110,6 @@ target_include_directories(uuid_test PRIVATE
|
||||
)
|
||||
target_link_libraries(uuid_test neuron-base gtest_main gtest crypto)
|
||||
|
||||
#copy schema for test
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema_test.json DESTINATION ${CMAKE_BINARY_DIR}/tests)
|
||||
add_executable(schema_valid_test schema_valid_test.cc ${CMAKE_SOURCE_DIR}/src/schema/schema.c)
|
||||
target_include_directories(schema_valid_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
target_link_libraries(schema_valid_test neuron-base gtest_main gtest jansson)
|
||||
|
||||
add_executable(http_test http_test.cc
|
||||
${CMAKE_SOURCE_DIR}/plugins/restful/http.c)
|
||||
|
||||
@@ -128,10 +119,9 @@ target_include_directories(http_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/plugins/restful)
|
||||
target_link_libraries(http_test neuron-base gtest_main gtest yaml jansson nng)
|
||||
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}/tests)
|
||||
#file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}/tests)
|
||||
add_executable(mqtt_test mqtt_test.cc
|
||||
${CMAKE_SOURCE_DIR}/src/config.c
|
||||
${CMAKE_SOURCE_DIR}/src/schema/schema.c
|
||||
${CMAKE_SOURCE_DIR}/plugins/mqtt/mqtt_util.c
|
||||
${CMAKE_SOURCE_DIR}/plugins/mqtt/command/node.c)
|
||||
target_include_directories(mqtt_test PRIVATE
|
||||
@@ -187,7 +177,6 @@ gtest_discover_tests(json_plugin_test)
|
||||
gtest_discover_tests(json_group_config_test)
|
||||
gtest_discover_tests(json_tty_test)
|
||||
gtest_discover_tests(uuid_test)
|
||||
gtest_discover_tests(schema_valid_test)
|
||||
gtest_discover_tests(http_test)
|
||||
gtest_discover_tests(mqtt_test)
|
||||
gtest_discover_tests(jwt_test)
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "schema.h"
|
||||
#include "json/json.h"
|
||||
|
||||
#define SCHEMA_FILE "./plugin_param_schema_test.json"
|
||||
|
||||
TEST(JsonSchema, SchemaValidTagType)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(SCHEMA_FILE, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *sv = neu_schema_load(buf, (char *) "test-plugin");
|
||||
|
||||
EXPECT_EQ(-1, neu_schema_valid_tag_type(sv, 1));
|
||||
EXPECT_EQ(0, neu_schema_valid_tag_type(sv, 4));
|
||||
|
||||
neu_schema_free(sv);
|
||||
}
|
||||
|
||||
TEST(JsonSchema, SchemaValidInt)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(SCHEMA_FILE, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *sv = neu_schema_load(buf, (char *) "test-plugin");
|
||||
|
||||
EXPECT_EQ(0, neu_schema_valid_param_int(sv, 1234, (char *) "port"));
|
||||
|
||||
EXPECT_EQ(-1, neu_schema_valid_param_int(sv, 1, (char *) "port"));
|
||||
|
||||
neu_schema_free(sv);
|
||||
}
|
||||
|
||||
TEST(JsonSchema, SchemaValidString)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(SCHEMA_FILE, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *sv = neu_schema_load(buf, (char *) "test-plugin");
|
||||
|
||||
char *str1 = (char *) "1234";
|
||||
char *str2 = (char *) "1234567890123456789012345678901";
|
||||
|
||||
EXPECT_EQ(0, neu_schema_valid_param_string(sv, str1, (char *) "host"));
|
||||
|
||||
EXPECT_EQ(-1, neu_schema_valid_param_string(sv, str2, (char *) "host"));
|
||||
|
||||
neu_schema_free(sv);
|
||||
}
|
||||
|
||||
TEST(JsonSchema, SchemaValidReal)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(SCHEMA_FILE, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *sv = neu_schema_load(buf, (char *) "test-plugin");
|
||||
|
||||
EXPECT_EQ(0, neu_schema_valid_param_real(sv, 10.1, (char *) "real_param"));
|
||||
|
||||
EXPECT_EQ(-1, neu_schema_valid_param_real(sv, 30.5, (char *) "real_param"));
|
||||
|
||||
neu_schema_free(sv);
|
||||
}
|
||||
|
||||
TEST(JsonSchema, SchemaValidEnum)
|
||||
{
|
||||
char buf[40960] = { 0 };
|
||||
FILE *fp = fopen(SCHEMA_FILE, "r");
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
neu_schema_valid_t *sv = neu_schema_load(buf, (char *) "test-plugin");
|
||||
|
||||
EXPECT_EQ(0, neu_schema_valid_param_enum(sv, 9600, (char *) "baud_rate"));
|
||||
|
||||
EXPECT_EQ(-1, neu_schema_valid_param_enum(sv, 1234, (char *) "baud_rate"));
|
||||
|
||||
neu_schema_free(sv);
|
||||
}
|
||||
Reference in New Issue
Block a user