-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmd_abnormal_hooks_unittest.c
209 lines (183 loc) · 9 KB
/
md_abnormal_hooks_unittest.c
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
/*
* Excelfore gptp - Implementation of gPTP(IEEE 802.1AS)
* Copyright (C) 2019 Excelfore Corporation (https://excelfore.com)
*
* This file is part of Excelfore-gptp.
*
* Excelfore-gptp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Excelfore-gptp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Excelfore-gptp. If not, see
* <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
*/
#include <stdio.h>
#include <xl4unibase/unibase_binding.h>
#include <setjmp.h>
#include <cmocka.h>
#include "mdeth.h"
#include "md_abnormal_hooks.h"
#include "gptpnet.h"
static gptpnet_data_t *gpnet;
static int gptpnet_cb(void *cb_data, int portIndex, gptpnet_event_t event,
int64_t *event_ts, void *event_data)
{
return 0;
}
static void test_abnormal_events1(void **state)
{
md_abn_event_t event1={domainNumber:0, ndevIndex:0, msgtype:SYNC,
eventtype:MD_ABN_EVENTP_SKIP,
eventrate:1.0, repeat:0, interval:0, eventpara:0};
int length=sizeof(MDPTPMsgSync);
ClockIdentity clockid={0,};
md_header_compose(gpnet, 1, SYNC, length, clockid, 1, 100, -3);
// without md_abnormal_init, no event happens
assert_int_equal(md_abnormal_register_event(&event1),-1);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
md_abnormal_init();
assert_int_equal(md_abnormal_register_event(&event1),0);
// repeat every time forever
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=1, interval=0 : repeat one time
event1.repeat=1;
event1.interval=0;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=1, interval=1 : repeat one time
event1.repeat=1;
event1.interval=1;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=2, interval=0 : repeat 2 times
event1.repeat=2;
event1.interval=0;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=2, interval=1 : repeat 2 times with interval 1
event1.repeat=2;
event1.interval=1;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=2, interval=2 : repeat 2 times with interval 2
event1.repeat=2;
event1.interval=2;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// repeat=3, interval=2 : repeat 3 times with interval 2
event1.repeat=3;
event1.interval=2;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_SKIP);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
md_abnormal_close();
}
static void test_abnormal_events2(void **state)
{
md_abn_event_t event1={domainNumber:0, ndevIndex:0, msgtype:PDELAY_REQ,
eventtype:MD_ABN_EVENTP_DUPLICATE,
eventrate:1.0, repeat:0, interval:0, eventpara:0};
int length=sizeof(MDPTPMsgPdelayReq);
ClockIdentity clockid={0,};
int i, count;
md_header_compose(gpnet, 1, SYNC, length, clockid, 1, 100, -3);
md_abnormal_init();
// PDELAY_REQ != SYNC, the event doesn't happen
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
md_header_compose(gpnet, 1, PDELAY_REQ, length, clockid, 1, 100, -3);
// repeat every time forever
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_DUPLICATE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_DUPLICATE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_DUPLICATE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// possibility=0.0, the event never happens
event1.eventrate=0.0;
assert_int_equal(md_abnormal_register_event(&event1),0);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_gptpnet_send_hook(gpnet, 0, length), MD_ABN_EVENTP_NONE);
assert_int_equal(md_abnormal_deregister_all_events(),0);
// possibility=0.5, the event happens 50%
event1.eventrate=0.5;
assert_int_equal(md_abnormal_register_event(&event1),0);
for(i=0,count=0;i<1000;i++){
if(md_abnormal_gptpnet_send_hook(gpnet, 0, length)==MD_ABN_EVENTP_DUPLICATE)
count++;
}
assert_in_range(count, 400, 600);
assert_int_equal(md_abnormal_deregister_all_events(),0);
md_abnormal_close();
}
static int setup(void **state)
{
unibase_init_para_t init_para;
char *netdevs[2]={"cbeth0",NULL};
int np;
ubb_default_initpara(&init_para);
init_para.ub_log_initstr=UBL_OVERRIDE_ISTR("4,ubase:45,cbase:45,gptp:46", "UBL_GPTP");
unibase_init(&init_para);
gpnet=gptpnet_init(gptpnet_cb, NULL, NULL, netdevs, &np, NULL);
return 0;
}
static int teardown(void **state)
{
gptpnet_close(gpnet);
unibase_close();
return 0;
}
int main(int argc, char *argv[])
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_abnormal_events1),
cmocka_unit_test(test_abnormal_events2),
};
return cmocka_run_group_tests(tests, setup, teardown);
}