summaryrefslogtreecommitdiff
path: root/test/src/gtest_fieldstat3.cpp
blob: 4cf36cdf5f76d27b68ca1f70246f15402b0fd91b (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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <MESA/fieldstat.h>

#include "tsg_entry.h"
#include "gtest_common.h"
#include "tsg_variable.h"
#include <MESA/MESA_prof_load.h>
#include <MESA/MESA_handle_logger.h>

#include <gtest/gtest.h>

const char *tsg_gtest_conffile = "tsgconf/main.conf";
extern struct tsg_statistic g_tsg_statis_para;
pthread_t g_pid[8];

TEST(FIELDSATA3, InterceptIllegalParameter)
{
    struct traffic_info _info;
    struct maat_rule p_result;

    int ret = tsg_set_intercept_flow(NULL, &_info, 0);
    EXPECT_EQ(ret, -1);

    ret = tsg_set_intercept_flow(&p_result, NULL, 0);
    EXPECT_EQ(ret, -1);

    ret = tsg_set_intercept_flow(&p_result, &_info, -1);
    EXPECT_EQ(ret, -1);
}

TEST(FIELDSATA3, PolicyIllegalParameter)
{
    struct maat_rule p_result;
    struct streaminfo a_stream;

    int ret = tsg_set_policy_flow(NULL, &p_result, 0);
    EXPECT_EQ(ret, -1);

    ret = tsg_set_policy_flow(&a_stream, NULL, 0);
    EXPECT_EQ(ret, -1);

    ret = tsg_set_policy_flow(&a_stream, &p_result, -1);
    EXPECT_EQ(ret, -1);
}

TEST(FIELDSATA3, Intercept)
{
    struct traffic_info _info;
    struct maat_rule p_result;

    _info.con_num = 10;
    _info.in_bytes = 1000;
    _info.in_packets = 25;
    _info.out_bytes = 1001;
    _info.out_packets = 24;

    p_result.action = TSG_ACTION_INTERCEPT;
    p_result.rule_id = 95536;
    p_result.do_log = 1;
    p_result.service_id = TSG_SERVICE_INTERCEPT;

    for (int i = 0; i < 50000; i++)
    {
        // p_result.rule_id += i;
        // _info.in_bytes += i;
        int ret = tsg_set_intercept_flow(&p_result, &_info, 0);
        EXPECT_EQ(ret, 0);
        // usleep(500);
    }
}

TEST(FIELDSATA3, PolicyFlow)
{
    struct maat_rule  p_result;
    struct streaminfo a_stream;

    p_result.action = TSG_ACTION_MONITOR;
    p_result.rule_id = 95500;
	p_result.service_id=TSG_SERVICE_SECURITY;

    for (int i = 0; i < 50000; i++)
    {
        // p_result.rule_id += i;
        // _info.in_bytes += i;
        int ret = tsg_set_policy_flow(&a_stream, &p_result, 0);
        EXPECT_EQ(ret, 0);
        // usleep(500);
    }
}

#if 0
void *run_time_funtion(void *arg)
{
    uint8_t pid = *(uint8_t *)arg;

    struct maat_rule p_result;
    struct streaminfo a_stream;
    struct traffic_info _info;

    while (1)
    {
        srand(time(NULL));
        p_result.action = TSG_ACTION_MONITOR;
        p_result.rule_id = rand() % 95500;

        usleep(rand() % 500);
        int ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
        EXPECT_EQ(ret, 0);

        srand(time(NULL));
        usleep(rand() % 500);

        p_result.action = TSG_ACTION_DENY;
        p_result.rule_id = rand() % 95500;
        ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
        EXPECT_EQ(ret, 0);

        srand(time(NULL));
        usleep(rand() % 500);

        p_result.action = TSG_ACTION_BYPASS;
        p_result.rule_id = rand() % 95500;
        ret = tsg_set_policy_flow(&a_stream, &p_result, pid);
        EXPECT_EQ(ret, 0);

        _info.con_num = rand() % 10;
        _info.in_bytes = rand() % 1000;
        _info.in_packets = rand() % 25;
        _info.out_bytes = rand() % 1001;
        _info.out_packets = rand() % 24;

        p_result.action = TSG_ACTION_INTERCEPT;
        p_result.rule_id = rand() % 95536;

        ret = tsg_set_intercept_flow(&p_result, &_info, pid);
        EXPECT_EQ(ret, 0);
    }

    return NULL;
}

TEST(FIELDSATA3, MultiThreading)
{
    for (uint8_t i = 0; i < 8; i++)
    {
        uint8_t i_pid = i;
        pthread_create(g_pid + i, NULL, run_time_funtion, (void *)&i_pid);
        EXPECT_NE(g_pid[i], 0);
    }
}
#endif

int main(int argc, char *argv[])
{
    void *logger = MESA_create_runtime_log_handle("log/gtest_fieldstat3.log", RLOG_LV_FATAL);
    tsg_metric_init(tsg_gtest_conffile, logger);
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
//    sleep(30);
//    for (int i = 0; i < 8; i++)
//    {
//        pthread_cancel(g_pid[i]);
//    }

    tsg_metric_destroy();

    return ret;
}