summaryrefslogtreecommitdiff
path: root/test/test_utils.cpp
blob: e639b752fbdacbab9faa09a15ca97ea91d864fee (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "test_utils.h"
#include "maat_redis_monitor.h"
#include "maat_utils.h"
#include "maat_table.h"
#include "maat_rule.h"
#include "maat_config_monitor.h"
#include "json2iris.h"

#include <assert.h>
#include <unistd.h>

int line_idx = 0;
long long absolute_expire_time = 0;

static int
count_line_num_cb(const char *table_name, const char *line, void *u_para)
{
	(*((unsigned int *)u_para))++;
	return 0;
}

static int
make_serial_rule(const char *table_name, const char *line, void *u_para)
{
	struct serial_rule *s_rule=(struct serial_rule *)u_para;
    redisContext *ctx = s_rule->ref_ctx;
	char *buff = ALLOC(char, strlen(line) + 1);

    memcpy(buff, line, strlen(line) + 1);

	while (buff[strlen(line) - 1] == '\n' ||
           buff[strlen(line) - 1] == '\t') {
		buff[strlen(line) - 1] = '\0';
	}

    const char *redis_rule_key = "TEST_RULE_KEY";
    redisReply *reply =
        maat_wrap_redis_command(ctx, NULL, "INCRBY %s %d", redis_rule_key, 1);

	if (reply->type == REDIS_REPLY_NIL) {
		printf("incrby redis_rule_key:%s failed.", redis_rule_key);
        return -1;
	} else {
		s_rule->rule_id = maat_read_redis_integer(reply);
		freeReplyObject(reply);
		reply = NULL;
	}

	maat_set_serial_rule(s_rule + line_idx, MAAT_OP_ADD, s_rule->rule_id,
                         table_name, buff, absolute_expire_time);
    (s_rule + line_idx)->ref_ctx = ctx;
	line_idx++;

    FREE(buff);

	return 0;
}

int write_json_to_iris(const char* json_fn, char *iris_path, size_t path_sz,
                       struct log_handle *logger)
{

    char *json_buff = NULL;
    size_t json_buff_sz = 0;
    
    int ret = load_file_to_memory(json_fn, (unsigned char **)&json_buff,
                                  &json_buff_sz);
    if (ret < 0) {
        return -1;
    }

    ret = json2iris(json_buff, json_fn, NULL, iris_path, path_sz, NULL, NULL, logger);
    FREE(json_buff);

    if (ret < 0) {
        return -1;
    }

    return 0;
}

int write_iris_to_redis(const char *iris_path, char *redis_ip, int redis_port,
                        int redis_db, struct log_handle *logger)
{
   redisContext *c = maat_connect_redis(redis_ip, redis_port, redis_db, logger);
    if (NULL == c) {
        return -1;
    }

    redisReply *reply = maat_wrap_redis_command(c, logger, "flushdb");
    if (NULL == reply) {
        return -1;
    } else {
        freeReplyObject(reply);
	    reply = NULL;
    }

    size_t total_line_cnt = 0;
    config_monitor_traverse(0, iris_path, NULL, count_line_num_cb,
                            NULL, &total_line_cnt, NULL, logger);

    struct serial_rule *s_rule = ALLOC(struct serial_rule, total_line_cnt);
    s_rule->ref_ctx = c;
    long long server_time = maat_redis_server_time_s(c);
    if (server_time < 0) {
        return -1;
    }

    absolute_expire_time = server_time + 300;
    config_monitor_traverse(0, iris_path, NULL, make_serial_rule,
                            NULL, s_rule, NULL, logger);
    s_rule->ref_ctx = NULL;
    line_idx = 0;
    absolute_expire_time = 0;

    int success_cnt = 0;
    do {
        success_cnt = maat_cmd_write_rule(c, s_rule, total_line_cnt,
                                          server_time, logger);
    } while (success_cnt < 0);

    assert(success_cnt == (int)total_line_cnt);

    for (size_t i = 0; i < total_line_cnt; i++) {
        maat_clear_rule_cache(s_rule + i);
    }
    FREE(s_rule);
    redisFree(c);
    return 0;
}

int write_json_to_redis(const char *json_filename, char *redis_ip, int redis_port,
                        int redis_db, struct log_handle *logger)
{
    char iris_path[512] = {0};

    int ret = write_json_to_iris(json_filename, iris_path, sizeof(iris_path), logger);
    if (ret < 0) {
        return -1;
    }

    ret = write_iris_to_redis(iris_path, redis_ip, redis_port, redis_db, logger);
    if (ret < 0) {
        return -1;
    }

    return 0;
}

int compile_table_set_line(struct maat *maat_inst, const char *table_name,
                           enum maat_operation op, long long compile_id,
                           const char *user_region, int clause_num,
                           int expire_after)
{
    char table_line[1024 * 16] = {0};
    sprintf(table_line, "%lld\t0\t0\t0\t0\t0\t%s\t%d\t%d\t0.0", 
            compile_id, user_region, clause_num, op);

    struct maat_cmd_line line_rule;
    line_rule.rule_id = compile_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

#define TO_GROUP2X_KEY(group_id, parent_id, clause_index) \
        (((unsigned long)group_id<<32|parent_id) + clause_index)

int group2compile_table_set_line(struct maat *maat_inst, const char *table_name,
                                 enum maat_operation op, long long group_id,
                                 long long compile_id, int not_flag,
                                 const char *vtable_name, int clause_index,
                                 int expire_after)
{
    char table_line[128] = {0};
    sprintf(table_line, "%lld\t%lld\t%d\t%s\t%d\t%d", 
            group_id, compile_id, not_flag, vtable_name, clause_index, op);

    struct maat_cmd_line line_rule;
    line_rule.rule_id = TO_GROUP2X_KEY(group_id, compile_id, clause_index);
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

int group2group_table_set_line(struct maat *maat_inst, const char *table_name,
                               enum maat_operation op, long long group_id,
                               long long sub_group_id, int expire_after)
{
    char table_line[128] = {0};
    sprintf(table_line, "%lld\t%lld\t%s\t%d", group_id, sub_group_id,
            "null", op);

    struct maat_cmd_line line_rule;
    line_rule.rule_id = TO_GROUP2X_KEY(group_id, sub_group_id, 0);
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

int expr_table_set_line(struct maat *maat_inst, const char *table_name,
                        enum maat_operation op, long long item_id,
                        long long group_id, const char *keywords,
                        const char *district, int expr_type,
                        int match_method, int is_hexbin, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_inst, table_name);
    if (table_id < 0) {
        return 0;
    }
    
    enum table_type table_type =
        table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
    assert(table_type == TABLE_TYPE_EXPR ||
           table_type == TABLE_TYPE_EXPR_PLUS);

    if (table_type == TABLE_TYPE_EXPR_PLUS) {
        sprintf(table_line, "%lld\t%lld\t%s\t%s\t%d\t%d\t%d\t%d",
                item_id, group_id, district, keywords, expr_type,
                match_method, is_hexbin, op);
    } else {
        sprintf(table_line, "%lld\t%lld\t%s\t%d\t%d\t%d\t%d",
                item_id, group_id, keywords, expr_type,
                match_method, is_hexbin, op);
    }

    struct maat_cmd_line line_rule;
    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

int interval_table_set_line(struct maat *maat_inst, const char *table_name,
    enum maat_operation op, long long item_id, long long group_id,
    unsigned int low_boundary, unsigned int up_boundary,
    const char *district, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_inst, table_name);
    if (table_id < 0) {
        return 0;
    }

    enum table_type table_type =
        table_manager_get_table_type(maat_inst->tbl_mgr, table_id);
    assert(table_type == TABLE_TYPE_INTERVAL ||
           table_type == TABLE_TYPE_INTERVAL_PLUS);

    if (table_type == TABLE_TYPE_INTERVAL_PLUS) {
        sprintf(table_line, "%lld\t%lld\t%s\t%u\t%u\t%d",
                item_id, group_id, district, low_boundary, up_boundary, op);
    } else {
        sprintf(table_line, "%lld\t%lld\t%u\t%u\t%d",
                item_id, group_id, low_boundary, up_boundary, op);
    }

    struct maat_cmd_line line_rule;
    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

int ip_table_set_line(struct maat *maat_inst, const char *table_name,
                      enum maat_operation op, long long item_id,
                      long long group_id, enum IP_TYPE type,
                      const char *ip1, const char *ip2, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_inst, table_name);
    if (table_id < 0) {
        return 0;
    }

    int ip_type = IPV4;
    if (type == IPv6) {
        ip_type = IPV6;
    }

    sprintf(table_line, "%lld\t%lld\t%d\trange\t%s\t%s\t0-65535\t%d",
            item_id, group_id, ip_type, ip1, ip2, op);
    struct maat_cmd_line line_rule;
    
    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}
int flag_table_set_line(struct maat *maat_inst, const char *table_name,
                    enum maat_operation op, long long item_id,
                    long long group_id, long long flag,
                    long long flag_mask, int expire_after)
{
    char table_line[1024] = {0};
    int table_id = maat_get_table_id(maat_inst, table_name);
    if (table_id < 0) {
        return 0;
    }

    sprintf(table_line, "%lld\t%lld\t%lld\t%lld\t%d",
            item_id, group_id, flag, flag_mask, op);
    struct maat_cmd_line line_rule;

    line_rule.rule_id = item_id;
    line_rule.table_line = table_line;
    line_rule.table_name = table_name;
    line_rule.expire_after = expire_after;

    return maat_cmd_set_line(maat_inst, &line_rule);
}

void random_keyword_generate(char *keyword_buf, size_t sz)
{
#define MIN_KEYWORD_LEN	4
	size_t i = 0, len = 0;
	len = random() % (sz - 1 - MIN_KEYWORD_LEN) + MIN_KEYWORD_LEN;

	for (i = 0; i < len; i++) {
		keyword_buf[i] = 'a' + random() % ('z' - 'a');
	}
	keyword_buf[i] = '\0';
}