-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathutils_for_testing.h
313 lines (271 loc) · 8.41 KB
/
utils_for_testing.h
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
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2018 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
#ifndef UTILS_FOR_TESTING_H
#define UTILS_FOR_TESTING_H
#include <gtest/gtest.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "pf_includes.h"
#include "mocks.h"
#if defined(TEST_DEBUG)
#define TEST_TRACE(...) printf (__VA_ARGS__)
#else
#define TEST_TRACE(...)
#endif
#define TEST_UDP_DELAY (500 * 1000) /* us */
#define TEST_DATA_DELAY (2 * 1000) /* us */
#define TEST_TIMEOUT_DELAY (3 * 1000 * 1000) /* us */
#define TICK_INTERVAL_US 1000 /* us */
#define TEST_API_IDENT 0
#define TEST_API_NONEXIST_IDENT 2
#define TEST_SLOT_IDENT 1
#define TEST_SLOT_NONEXIST_IDENT 2
#define TEST_SUBSLOT_IDENT 1
#define TEST_SUBSLOT_NONEXIST_IDENT 2
#define TEST_CHANNEL_IDENT 0
#define TEST_CHANNEL_NONEXIST_IDENT 1
#define TEST_CHANNEL_ILLEGAL 0x8001
#define TEST_CHANNEL_DIRECTION PNET_DIAG_CH_PROP_DIR_OUTPUT
#define TEST_CHANNEL_NUMBER_OF_BITS PNET_DIAG_CH_PROP_TYPE_8_BIT
#define TEST_CHANNEL_ERRORTYPE 0x0001
#define TEST_CHANNEL_ERRORTYPE_B 0x0002
#define TEST_CHANNEL_ERRORTYPE_C 0x0003
#define TEST_CHANNEL_ERRORTYPE_D 0x0004
#define TEST_CHANNEL_ERRORTYPE_NONEXIST 0x0007
#define TEST_DIAG_USI_CUSTOM 0x1234
#define TEST_DIAG_USI_NONEXIST 0x1235
#define TEST_DIAG_USI_INVALID 0x8888
#define TEST_DIAG_EXT_ERRTYPE 0x0002
#define TEST_DIAG_EXT_ERRTYPE_NONEXIST 0x0009
#define TEST_DIAG_EXT_ADDVALUE 0x00030004
#define TEST_DIAG_EXT_ADDVALUE_B 0x00030005
#define TEST_DIAG_QUALIFIER 0x00010000
#define TEST_DIAG_QUALIFIER_NOTSET 0x00000000
#define TEST_INTERFACE_NAME "en1"
#define TEST_MAX_NUMBER_AVAILABLE_MODULE_TYPES 20
#define TEST_MAX_NUMBER_AVAILABLE_SUBMODULE_TYPES 20
/*
* I/O Modules. These modules and their sub-modules must be plugged by the
* application after the call to pnet_init.
*
* Assume that all modules only have a single submodule, with same number.
*/
#define TEST_MOD_8_0_IDENT 0x00000030 /* 8 bit input */
#define TEST_MOD_0_8_IDENT 0x00000031 /* 8 bit output */
#define TEST_MOD_8_8_IDENT 0x00000032 /* 8 bit input, 8 bit output */
#define TEST_SUBMOD_CUSTOM_IDENT 0x00000001
#define TEST_DATASIZE_INPUT 1 /* bytes, for digital inputs data */
#define TEST_DATASIZE_OUTPUT 1 /* bytes, for digital outputs data */
typedef struct cfg_submodules
{
uint32_t module_ident_number;
uint32_t submodule_ident_number;
pnet_submodule_dir_t direction;
uint16_t input_length;
uint16_t output_length;
} cfg_submodules_t;
typedef struct call_counters_obj
{
uint16_t state_calls;
uint16_t connect_calls;
uint16_t release_calls;
uint16_t dcontrol_calls;
uint16_t ccontrol_calls;
uint16_t read_calls;
uint16_t write_calls;
uint16_t led_on_calls;
uint16_t led_off_calls;
} call_counters_t;
typedef struct app_data_for_testing_obj
{
uint32_t tick_ctr;
os_timer_t * periodic_timer;
pnet_event_values_t cmdev_state;
uint16_t data_cycle_ctr;
uint32_t data_ctr;
os_event_t * main_events;
uint32_t main_arep;
bool alarm_allowed;
uint32_t app_param_1;
uint32_t app_param_2;
uint8_t inputdata[TEST_DATASIZE_INPUT];
uint8_t custom_input_slots[PNET_MAX_SLOTS];
uint8_t custom_output_slots[PNET_MAX_SLOTS];
uint32_t available_module_types[TEST_MAX_NUMBER_AVAILABLE_MODULE_TYPES];
cfg_submodules_t
available_submodule_types[TEST_MAX_NUMBER_AVAILABLE_SUBMODULE_TYPES];
bool init_done;
uint16_t read_fails;
call_counters_t call_counters;
} app_data_for_testing_t;
/******************** Callbacks defined by p-net *****************************/
int my_connect_ind (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_result_t * p_result);
int my_release_ind (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_result_t * p_result);
int my_dcontrol_ind (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_control_command_t control_command,
pnet_result_t * p_result);
int my_ccontrol_cnf (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_result_t * p_result);
int my_read_ind (
pnet_t * net,
void * arg,
uint32_t arep,
uint16_t api,
uint16_t slot,
uint16_t subslot,
uint16_t idx,
uint16_t sequence_number,
uint8_t ** pp_read_data, /* Out: A pointer to the data */
uint16_t * p_read_length, /* Out: Size of data */
pnet_result_t * p_result); /* Error status if returning != 0 */
int my_write_ind (
pnet_t * net,
void * arg,
uint32_t arep,
uint16_t api,
uint16_t slot,
uint16_t subslot,
uint16_t idx,
uint16_t sequence_number,
uint16_t write_length,
uint8_t * p_write_data,
pnet_result_t * p_result);
int my_new_data_status_ind (
pnet_t * net,
void * arg,
uint32_t arep,
uint32_t crep,
uint8_t changes,
uint8_t data_status);
int my_alarm_ind (
pnet_t * net,
void * arg,
uint32_t arep,
const pnet_alarm_argument_t * p_alarm_arg,
uint16_t data_len,
uint16_t data_usi,
uint8_t * p_data);
int my_alarm_cnf (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_pnio_status_t * p_pnio_status);
int my_state_ind (
pnet_t * net,
void * arg,
uint32_t arep,
pnet_event_values_t state);
int my_exp_module_ind (
pnet_t * net,
void * arg,
uint16_t api,
uint16_t slot,
uint32_t module_ident);
int my_exp_submodule_ind (
pnet_t * net,
void * arg,
uint16_t api,
uint16_t slot,
uint16_t subslot,
uint32_t module_ident,
uint32_t submodule_ident);
#ifdef __cplusplus
}
#endif
/*********************** Base classes for tests *****************************/
class PnetUnitTest : public ::testing::Test
{
protected:
virtual void SetUp(){};
};
class PnetIntegrationTestBase : public ::testing::Test
{
protected:
pnet_cfg_t pnet_default_cfg;
app_data_for_testing_t appdata;
pnet_t the_net;
pnet_t * net = &the_net;
/** Initialize appdata, including clearing available modules etc. */
virtual void appdata_init();
virtual void callcounter_reset();
virtual void available_modules_and_submodules_init();
virtual void cfg_init();
/** Simulate sleep
*
* This function updates the app state and calls the periodic
* maintenance functions while simulating sleeping.
*
* @param us In: time to sleep (in us)
*/
virtual void run_stack (int us);
/** Send raw Ethernet test data
*
* @param data_packet In: Data packet
* @param len In: Length of data packet
*/
virtual void send_data (uint8_t * data_packet, uint16_t len);
};
class PnetIntegrationTest : public PnetIntegrationTestBase
{
protected:
virtual void SetUp() override
{
mock_init();
cfg_init();
appdata_init();
available_modules_and_submodules_init();
callcounter_reset();
pnet_init_only (net, &pnet_default_cfg);
mock_clear(); /* lldp sends a frame at init */
};
virtual void TearDown() override
{
pnet_free_members (net);
};
};
/*************************** Assertion helpers ******************************/
template <typename T, size_t size>
::testing::AssertionResult ArraysMatch (
const T (&expected)[size],
const T (&actual)[size])
{
for (size_t i (0); i < size; ++i)
{
if (expected[i] != actual[i])
{
return ::testing::AssertionFailure()
<< std::hex << std::showbase << "array[" << i << "] ("
<< static_cast<int> (actual[i]) << ") != expected[" << i
<< "] (" << static_cast<int> (expected[i]) << ")";
}
}
return ::testing::AssertionSuccess();
}
#endif /* UTILS_FOR_TESTING_H */