summaryrefslogtreecommitdiff
path: root/test/adapter_hs_gtest.cpp
blob: d1e8825e49668da31dc20d47d2e8b5e418272882 (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include <gtest/gtest.h>

#include "log/log.h"
#include "adapter_hs.h"
#include "maat_utils.h"
#include "cJSON/cJSON.h"

struct log_handle *g_logger = NULL;

enum hs_match_mode match_method_to_match_mode(const char *method)
{
    enum hs_match_mode mode = HS_MATCH_MODE_MAX;

    if (strcmp(method, "sub") == 0) {
        mode = HS_MATCH_MODE_SUB;
    } else if (strcmp(method, "exactly") == 0) {
        mode = HS_MATCH_MODE_EXACTLY;
    } else if (strcmp(method, "prefix") == 0) {
        mode = HS_MATCH_MODE_PREFIX;
    } else if (strcmp(method, "suffix") == 0) {
        mode = HS_MATCH_MODE_SUFFIX;
    } else {
        assert(0);
    }

    return mode;
}

enum hs_case_sensitive case_sensitive_str_to_enum(const char *str)
{
    enum hs_case_sensitive case_sensitive = HS_CASE_SENSITIVE;

    if (strcmp(str, "yes") == 0) {
        case_sensitive = HS_CASE_SENSITIVE;
    } else if (strcmp(str, "no") == 0) {
        case_sensitive = HS_CASE_INSENSITIVE;
    } else {
        assert(0);
    }

    return case_sensitive;
}

int is_hexbin_str_to_int(const char *str)
{
    int ret = 0;

    if (strcmp(str, "yes") == 0) {
        ret = 1;
    }

    return ret;
}

static int convertHextoint(char srctmp)
{
	if (isdigit(srctmp)) {
		return srctmp - '0';
	} else {
		char temp = toupper(srctmp);
		temp = temp - 'A' + 10;
		return temp;
	}
}

static size_t hex2bin(char *hex, int hex_len, char *binary, size_t size)
{
	size_t resultlen = 0;
	int high,low;
	for (int i = 0; i < hex_len && size > resultlen; i += 2, resultlen++) {
		high = convertHextoint(hex[i]);
		low = convertHextoint(hex[i+1]);
		binary[resultlen] = high * 16 + low;
	}

	size = resultlen;
	binary[resultlen] = '\0';

	return resultlen;
}

enum hs_pattern_type pattern_type_str_to_enum(const char *str)
{
    enum hs_pattern_type pattern_type = HS_PATTERN_TYPE_MAX;

    if (strcmp(str, "regex") == 0) {
        pattern_type = HS_PATTERN_TYPE_REG;
    } else if (strcmp(str, "literal") == 0) {
        pattern_type = HS_PATTERN_TYPE_STR;
    } else {
        assert(0);
    }

    return pattern_type;
}

int parse_config_file(const char *filename, struct expr_rule exprs[], size_t *n_expr)
{
    unsigned char *json_buff = NULL;
    size_t json_buff_size = 0;
    
    int ret = load_file_to_memory(filename, &json_buff, &json_buff_size);
    if (ret < 0) {
        printf("load file:%s to memory failed.\n", filename);
        return -1;
    }

    size_t rule_cnt = 0;
    cJSON *rules_obj = NULL;
    cJSON *root = cJSON_Parse((const char *)json_buff);
    if (NULL == root) {
        printf("Error before: %-200.200s\n", cJSON_GetErrorPtr());
        ret = -1;
        goto next;
    }

    rules_obj = cJSON_GetObjectItem(root, "expr_rules");
    if (NULL == rules_obj) {
        printf("Error before: %-200.200s\n", cJSON_GetErrorPtr());
        ret = -1;
        goto next;
    }

    rule_cnt = cJSON_GetArraySize(rules_obj);
    for (size_t i = 0; i < rule_cnt; i++) {
        cJSON *expr_obj = cJSON_GetArrayItem(rules_obj, i);
        cJSON *tmp_item = cJSON_GetObjectItem(expr_obj, "expr_id");
        if (tmp_item != NULL && tmp_item->type == cJSON_Number) {
            exprs[i].expr_id = tmp_item->valueint;
        }

        tmp_item = cJSON_GetObjectItem(expr_obj, "pattern_num");
        if (tmp_item != NULL && tmp_item->type == cJSON_Number) {
            exprs[i].n_patterns = tmp_item->valueint;
        }

        tmp_item = cJSON_GetObjectItem(expr_obj, "patterns");
        if (NULL == tmp_item || tmp_item->type != cJSON_Array) {
            printf("json has no patterns array.\n");
            ret = -1;
            goto next;
        }

        size_t pattern_cnt = cJSON_GetArraySize(tmp_item);
        for (size_t j = 0; j < pattern_cnt; j++) {
            cJSON *pat_item = cJSON_GetArrayItem(tmp_item, j);

            cJSON *item = cJSON_GetObjectItem(pat_item, "pattern_type");
            if (item != NULL && item->type == cJSON_String) {
                exprs[i].patterns[j].pattern_type = pattern_type_str_to_enum(item->valuestring);
            }

            item = cJSON_GetObjectItem(pat_item, "match_method");
            if (item != NULL && item->type == cJSON_String) {
                exprs[i].patterns[j].match_mode = match_method_to_match_mode(item->valuestring);
            }

            item = cJSON_GetObjectItem(pat_item, "case_sensitive");
            if (item != NULL && item->type == cJSON_String) {
                exprs[i].patterns[j].case_sensitive = case_sensitive_str_to_enum(item->valuestring);
            }

            item = cJSON_GetObjectItem(pat_item, "is_hexbin");
            if (item != NULL && item->type == cJSON_String) {
                exprs[i].patterns[j].is_hexbin = is_hexbin_str_to_int(item->valuestring);
            }

            item = cJSON_GetObjectItem(pat_item, "pattern");
            if (item != NULL && item->type == cJSON_String) {
                exprs[i].patterns[j].pat = ALLOC(char, strlen(item->valuestring) + 1);

                if (exprs[i].patterns[j].is_hexbin == 1) {
                    size_t pat_str_len = strlen(item->valuestring) + 1;
                    char *pat_str = ALLOC(char, pat_str_len);
                    pat_str_len = hex2bin(item->valuestring, strlen(item->valuestring), 
                                          pat_str, pat_str_len);
                    
                    memcpy(exprs[i].patterns[j].pat, pat_str, pat_str_len);
                    free(pat_str);
                    exprs[i].patterns[j].pat_len = pat_str_len;
                } else {
                    memcpy(exprs[i].patterns[j].pat, item->valuestring, 
                           strlen(item->valuestring));
                    exprs[i].patterns[j].pat_len = strlen(item->valuestring);
                }
            }

            if (exprs[i].patterns->match_mode == HS_MATCH_MODE_SUB) {
                item = cJSON_GetObjectItem(pat_item, "offset");
                if (item != NULL && item->type == cJSON_String) {
                    int key_left_offset = -1;
                    int key_right_offset = -1;
                    sscanf(item->valuestring, "%d~%d", &key_left_offset, &key_right_offset);
                    if (key_left_offset < -1 || key_right_offset < -1) {
                        printf("Error: offset should not less than -1, left_offset:%d, right_offset:%d\n",
                                key_left_offset, key_right_offset);
                    }
                    exprs[i].patterns[j].start_offset = key_left_offset;
                    exprs[i].patterns[j].end_offset = key_right_offset;
                } else {
                    exprs[i].patterns[j].start_offset = -1;
                    exprs[i].patterns[j].end_offset = -1;
                }
            }

            if (exprs[i].patterns->match_mode == HS_MATCH_MODE_EXACTLY) {
                exprs[i].patterns[j].start_offset = 0;
                exprs[i].patterns[j].end_offset = exprs[i].patterns[j].pat_len - 1;
            }
        }
        exprs[i].n_patterns = pattern_cnt;
    }

    *n_expr = rule_cnt;
next:
    cJSON_Delete(root);
    FREE(json_buff);
    return ret;
}

void expr_array_free(struct expr_rule rules[], size_t n_rule)
{
    for (size_t i = 0; i < n_rule; i++) {
        for (size_t j = 0; j < rules[i].n_patterns; j++) {
            if (rules[i].patterns[j].pat != NULL) {
                free(rules[i].patterns[j].pat);
                rules[i].patterns[j].pat = NULL;
            }
        }
    }
}

TEST(adapter_hs_init, invalid_input_parameter)
{
    struct expr_rule rules[64];
    size_t n_rule = 0;

    struct adapter_hs *hs_instance = adapter_hs_new(1, NULL, 0, g_logger);
    EXPECT_TRUE(hs_instance == NULL);

    hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance == NULL);

    n_rule = 1;
    rules[0].expr_id = 101;
    rules[0].n_patterns = 10;
    hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance == NULL);

    memset(rules, 0, sizeof(rules));
    n_rule = 1;
    rules[0].expr_id = 101;
    rules[0].n_patterns = 1;
    hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance == NULL);
}

TEST(adapter_hs_scan, literal_sub_has_normal_offset)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;

    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello aaa";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data2[64] = "Ahello aaa";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 101);
    
    char scan_data3[64] = "Aahello aaa";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 101);

    char scan_data4[64] = "Aaahello aaa";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_sub_has_left_unlimit_offset)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;

    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello bbb";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 102);

    char scan_data2[64] = "Ahello bbb";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 102);

    char scan_data3[64] = "Aahello bbb";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 102);

    char scan_data4[64] = "Aaahello bbb";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_sub_has_right_unlimit_offset)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;

    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello ccc";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data2[64] = "1234hello ccc";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data3[64] = "12345hello ccc";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 103);

    char scan_data4[64] = "12345hello cccAaBb";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 103);

    char scan_data5[64] = "123456hello cccAaBb";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data5, strlen(scan_data5), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 103);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_sub_with_no_offset)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello ddd";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 104);

    char scan_data2[64] = "123hello ddd";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 104);

    char scan_data3[64] = "123hello ddd456";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 104);

    char scan_data4[64] = "helloddd";
    memset(result, 0, sizeof(result));
    n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_exactly)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello eee";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 105);

    char scan_data2[64] = "Ahello eee";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data3[64] = "hello eeeB";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_prefix)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello fff";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 106);

    char scan_data2[64] = "Ahello fff";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data3[64] = "Ahello fffBCD";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data4[64] = "hello fffBCD";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 106);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_suffix)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "hello ggg";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 107);

    char scan_data2[64] = "ABChello ggg";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 107);

    char scan_data3[64] = "ABChello gggDEF";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data3, strlen(scan_data3), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    char scan_data4[64] = "hello gggDEF";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data4, strlen(scan_data4), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_sub_with_hexbin)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char scan_data1[64] = "Content-Type: /html";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data1, strlen(scan_data1), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 108);

    char scan_data2[64] = " html";
    memset(result, 0, sizeof(result));
    n_result = 0;

    ret = adapter_hs_scan(hs_instance, 0, scan_data2, strlen(scan_data2), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 0);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, literal_with_chinese)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char data0[64] = "#中国 你好";
    struct hs_scan_result result0[64] = {0};
    size_t n_result0 = 0;
    ret = adapter_hs_scan(hs_instance, 0, data0, strlen(data0), result0, 64, &n_result0);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result0, 1);
    EXPECT_EQ(result0[0].rule_id, 110);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;
}

TEST(adapter_hs_scan, same_pattern_different_offset)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    char data[64] = "onetoday,anothertoday";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, data, strlen(data), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 112);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;                                             
}

TEST(adapter_hs_scan, long_scan_data)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./literal_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    struct adapter_hs *hs_instance = adapter_hs_new(1, rules, n_rule, g_logger);
    EXPECT_TRUE(hs_instance != NULL);
    expr_array_free(rules, n_rule);

    const char* scan_data = "A directed path in a directed graph is a finite or infinite\
sequence of edges which joins a sequence of distinct vertices, but with the added restriction\
that the edges be all directed in the same direction.";
    struct hs_scan_result result[64] = {0};
    size_t n_result = 0;
    ret = adapter_hs_scan(hs_instance, 0, scan_data, strlen(scan_data), result, 64, &n_result);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(n_result, 1);
    EXPECT_EQ(result[0].rule_id, 113);

    adapter_hs_free(hs_instance);
    hs_instance = NULL;                          
}

TEST(adapter_hs_scan, regex_expression_check)
{
    struct expr_rule rules[64] = {0};
    size_t n_rule = 0;
    
    int ret = parse_config_file("./regex_expr.conf", rules, &n_rule);
    EXPECT_EQ(ret, 0);

    for (size_t i = 0; i < n_rule; i++) {
        for (size_t j = 0; j < rules[i].n_patterns; j++) {
            adapter_hs_verify_regex_expression(rules[i].patterns[j].pat, g_logger);
        }
    }

    expr_array_free(rules, n_rule);
}

int main(int argc, char **argv)
{
    int ret = 0;
    ::testing::InitGoogleTest(&argc, argv);
    g_logger = log_handle_create("./adapter_hs_gtest.log", 0);

    ret = RUN_ALL_TESTS();

    log_handle_destroy(g_logger);

    return ret;
}