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
|
#include <MESA/stream.h>
#include <cjson/cJSON.h>
#include <gtest/gtest.h>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include "stub.h"
#define SHAPING_STAT_FILE_NAME "/tmp/shaping_metrics.json"
#define SHAPING_SESSION_QUEUE_LEN 100
#define SHAPING_SESSIONS_LIMIT_PER_AVL 1000
char profile_type_primary[] = "primary";
char profile_type_borrow[] = "borrow";
using namespace std;
static int judge_packet_eq(struct stub_pkt_queue *expec_queue, struct stub_pkt_queue *actual_queue, int num)
{
struct stub_packet_node *expec_pkt_node;
struct stub_packet_node *actual_pkt_node;
for (int i = 0; i < num; i++) {
if(TAILQ_EMPTY(actual_queue)) {
return -1;
}
expec_pkt_node = TAILQ_FIRST(expec_queue);
actual_pkt_node = TAILQ_FIRST(actual_queue);
if (expec_pkt_node->raw_packet != actual_pkt_node->raw_packet) {
return -2;
}
TAILQ_REMOVE(expec_queue, expec_pkt_node, node);
TAILQ_REMOVE(actual_queue, actual_pkt_node, node);
free(expec_pkt_node->raw_packet);
free(expec_pkt_node);
free(actual_pkt_node);
}
return 0;
}
static struct stub_packet* packet_new(unsigned long long income_time, unsigned int length, unsigned char dir)
{
struct stub_packet *packet;
packet = (struct stub_packet*)calloc(1, sizeof(struct stub_packet));
packet->income_time = income_time;
packet->length = length;
packet->direction = dir;
return packet;
}
static struct stub_packet_node* packet_node_new(stub_packet *packet)
{
struct stub_packet_node *pkt_node;
pkt_node = (struct stub_packet_node*)calloc(1, sizeof(struct stub_packet_node));
pkt_node->raw_packet = packet;
return pkt_node;
}
static void send_packets(struct streaminfo *stream, void **pme, int pkt_num, int pkt_len,
unsigned char dir, struct stub_pkt_queue *expec_tx_queue, int polling_times,
int is_tcp, int is_pure_control)
{
struct stub_packet_node *pkt_node;
struct stub_packet *packet;
unsigned long long time;
char ret;
stream->threadnum = 0;//just 1 thread for test!!!
for (int i = 0; i < pkt_num; i++) {
time = stub_curr_time_get();
packet = packet_new(time, pkt_len, dir);
if (expec_tx_queue) {
pkt_node = packet_node_new(packet);
TAILQ_INSERT_TAIL(expec_tx_queue, pkt_node, node);
}
time++;
stream->routedir = dir;
stream->hash_index = pkt_len;//just for stub test!!!!! use hash_index to store pkt_len
if (is_tcp) {
stream->ptcpdetail->clientpktnum++;
if (is_pure_control) {
stream->ptcpdetail->pdata = NULL;
} else {
stream->ptcpdetail->pdata = (void*)0x1234;//just for stub test, will not access this pointer
}
} else {
stream->pudpdetail->clientpktnum++;
}
if (is_tcp) {
ret = tcp_allpkt_raw_process(stream, NULL, packet, pme);
} else {
ret = udp_raw_process(stream, NULL, packet, pme);
}
if (ret == APP_STATE_GIVEME) {
if (!packet->detained_flag) {
stub_send_packet(packet);
}
}
for (int j = 0; j < polling_times; j++) {
polling_entry(NULL, NULL, 0, NULL);
}
stub_curr_time_inc();
}
}
static void stream_state_change_udp(struct streaminfo *stream, void **pme, unsigned char state)
{
stream->opstate = state;
//udp_process(stream, pme, 0, NULL);//just change state, no packet
if (state == OP_STATE_PENDING) {
stub_stream_bridge_sync_cb_invoke(stream);
} else if(state == OP_STATE_CLOSE) {
stub_stream_bridge_free_cb_invoke(stream);
}
udp_raw_process(stream, NULL, NULL, pme);//just change state, no packet
return;
}
static void stream_state_change_tcp(struct streaminfo *stream, void **pme, unsigned char state)
{
stream->pktstate = state;
//tcp_allpkt_process(stream, pme, 0, NULL);//just change state, no packet
if (state == OP_STATE_PENDING) {
stub_stream_bridge_sync_cb_invoke(stream);
} else if(state == OP_STATE_CLOSE) {
stub_stream_bridge_free_cb_invoke(stream);
}
tcp_allpkt_raw_process(stream, NULL, NULL, pme);//just change state, no packet
return;
}
static void shaping_stat_judge(char *file_line, int rule_id, int profile_id, int priority,
unsigned long long rx_pkts, unsigned long long rx_bytes,
unsigned long long tx_pkts, unsigned long long tx_bytes,
unsigned long long drop_pkts, long long queue_len, long long max_latency,
int queueing_sessions, unsigned char direction, char profile_type[])
{
cJSON *json = NULL;
cJSON *tmp_obj = NULL;
char attr_name[32] = {0};
json = cJSON_Parse(file_line);
ASSERT_TRUE(json != NULL);
tmp_obj = cJSON_GetObjectItem(json, "rule_id");
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(rule_id, atoi(tmp_obj->valuestring));
tmp_obj = cJSON_GetObjectItem(json, "profile_id");
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(profile_id, atoi(tmp_obj->valuestring));
tmp_obj = cJSON_GetObjectItem(json, "priority");
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(priority, atoi(tmp_obj->valuestring));
tmp_obj = cJSON_GetObjectItem(json, "profile_type");
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_STREQ(tmp_obj->valuestring, profile_type);
tmp_obj = cJSON_GetObjectItem(json, "queueing_sessions");
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(queueing_sessions, tmp_obj->valueint);
snprintf(attr_name, sizeof(attr_name), "%s_rx_pkts", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(rx_pkts, tmp_obj->valueint);
snprintf(attr_name, sizeof(attr_name), "%s_rx_bytes", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(rx_bytes, tmp_obj->valueint);
snprintf(attr_name, sizeof(attr_name), "%s_tx_pkts", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(tx_pkts, tmp_obj->valueint);
snprintf(attr_name, sizeof(attr_name), "%s_tx_bytes", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(tx_bytes, tmp_obj->valueint);
snprintf(attr_name, sizeof(attr_name), "%s_drop_pkts", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(drop_pkts, tmp_obj->valueint);
if (max_latency != -1) {
snprintf(attr_name, sizeof(attr_name), "%s_max_latency_us", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(max_latency, tmp_obj->valueint);
}
snprintf(attr_name, sizeof(attr_name), "%s_queue_len", direction == DIR_ROUTE_UP ? "out" : "in");
tmp_obj = cJSON_GetObjectItem(json, attr_name);
ASSERT_TRUE(tmp_obj != NULL);
EXPECT_EQ(queue_len, tmp_obj->valueint);
cJSON_Delete(json);
return;
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
//testing::GTEST_FLAG(filter) = "single_session.udp_diff_direction";
return RUN_ALL_TESTS();
}
|