summaryrefslogtreecommitdiff
path: root/test/verify_policy_tool.cpp
blob: b70ed6724b51aab93deb8644b60f24f5b4e4a181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*************************************************************************
	> File Name:
	> Author:
	> Mail:
	> Created Time: 2020��05��28�� ������ 19ʱ21��37��
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <cjson/cJSON.h>
#include <sys/stat.h>
#include "utils.h"

enum curl_post_type
{
    CURL_POST_HTTP,
    CURL_POST_SSL,
    __CURL_POST_MAX
};

struct curl_post_data
{
	char *input_result_data;
	char *curl_post_file;
	enum curl_post_type protocol_type;
};

char *curl_read_file(char *result_json_file)
{
	FILE* fp=NULL;
	struct stat file_info;
	stat(result_json_file, &file_info);
	size_t input_sz=file_info.st_size;

	fp=fopen(result_json_file,"r");
	if(fp==NULL)
	{
		return NULL;
	}
	char* input=(char*)malloc(input_sz);
	fread(input,1,input_sz,fp);
	fclose(fp);

	return input;
}

int curl_post_system_cmd(char *result, size_t result_len, char *curl_post_json)
{
	int line_num=0;
    char command[1024] = {0};
	char *cmd_str=NULL;

	snprintf(command, sizeof(command), "curl -X POST -H 'Content-Type: application/json' http://127.0.0.1:9994/v1/policy/verify -d@%s", curl_post_json);

	char *p=result;
	char line[2048] = {0};
    FILE *fp = NULL;

    if((fp = popen(command, "r")) == NULL)
	{
        printf("popen error!\n");
        return 0;
    }
	memset(result, 0, result_len);
    while (fgets(line, sizeof(line), fp))
	{
		if((p - result) < (int)result_len)
		{
			if(line_num)
			{
				p += snprintf(p, result_len - (p - result), ",");
			}

			p += snprintf(p, result_len - (p - result), "%s", line);
		}
		line_num++;
    }
    pclose(fp);

	free(cmd_str);
	return 1;
}

int curl_exec_expect_result(char *result, char *input_json_file)
{
	int ret=-1;
	cJSON *result_json=NULL, *input_json=NULL;
	cJSON_bool successful = false;

	result_json = cJSON_Parse(result);
	if(!result_json)
	{
		goto finish;
	}
	input_json = cJSON_Parse(input_json_file);
	if(!input_json)
	{
		goto finish;
	}
	successful = cJSON_Compare(result_json, result_json, true);
	if(successful)
	{
		ret=0;
	}
finish:
	cJSON_Delete(result_json);
	cJSON_Delete(input_json);
	return ret;
}

int call_curl_post(struct curl_post_data *post_data)
{
	int ret=-1;
	char result[81920]={0};

	curl_post_system_cmd(result, sizeof(result), post_data->curl_post_file);

	switch(post_data->protocol_type)
	{
		case CURL_POST_HTTP:
			ret = curl_exec_expect_result(result, post_data->input_result_data);
			break;
		case CURL_POST_SSL:
			break;
		default:
			break;
	}
	return ret;
}

static void help()
{
	fprintf(stderr,
		"verify_policy_test <-j| -t | -c > arg\n"
		"Usage:\n"
		"  -j <post json data>\n"
		"  -t <protocol type>\n"
		"  -c <input json>\n");
	    exit(1);
}

int main(int argc, char ** argv)
{
	int i=0, ret = 0;
	int curl_post_type=0;
	char result_json_file[256]={0};
	char curl_post_file[256]={0};

	if(argc < 3) {help();}
	for (i = 1; i < argc; i++)
	{
        int lastarg = i==argc-1;
		if (!strcmp(argv[i], "-j") && !lastarg)
		{
			 strncpy(curl_post_file, argv[++i], sizeof(curl_post_file));
		}
		else if (!strcmp(argv[i], "-t") && !lastarg)
		{
			sscanf(argv[++i], "%u", &curl_post_type);
		}
		else if (!strcmp(argv[i], "-c") && !lastarg)
		{
			strncpy(result_json_file, argv[++i], sizeof(result_json_file));
		}
		else
		{
			help();
		}
    }
	struct curl_post_data *post_data=NULL;
	post_data = ALLOC(struct curl_post_data, 1);
	post_data->protocol_type=(enum curl_post_type)curl_post_type;
	post_data->input_result_data=curl_read_file(result_json_file);
	post_data->curl_post_file=curl_post_file;

	ret = call_curl_post(post_data);

	return ret;
}