22
22
#include "sn_coap_builder_stub.h"
23
23
#include "sn_coap_parser_stub.h"
24
24
#include "socket_api.h"
25
+ #include "coap_message_handler.c"
25
26
26
27
int retCounter = 0 ;
27
28
int retValue = 0 ;
29
+ int transaction_cb = 0 ;
28
30
29
- static void * own_alloc (uint16_t size )
31
+ static void * test_own_alloc (uint16_t size )
30
32
{
31
33
if ( retCounter > 0 ){
32
34
retCounter -- ;
@@ -35,7 +37,7 @@ static void *own_alloc(uint16_t size)
35
37
return NULL ;
36
38
}
37
39
38
- static void own_free (void * ptr )
40
+ static void test_own_free (void * ptr )
39
41
{
40
42
if (ptr ) {
41
43
free (ptr );
@@ -56,24 +58,30 @@ int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c)
56
58
return retValue ;
57
59
}
58
60
61
+ static int transaction_recv_cb (int8_t service_id , uint8_t source_address [static 16 ], uint16_t source_port , sn_coap_hdr_s * response_ptr )
62
+ {
63
+ transaction_cb = 1 ;
64
+ return 1 ;
65
+ }
66
+
59
67
bool test_coap_message_handler_init ()
60
68
{
61
69
if ( NULL != coap_message_handler_init (NULL , NULL , NULL ) )
62
70
return false;
63
- if ( NULL != coap_message_handler_init (& own_alloc , NULL , NULL ) )
71
+ if ( NULL != coap_message_handler_init (& test_own_alloc , NULL , NULL ) )
64
72
return false;
65
- if ( NULL != coap_message_handler_init (& own_alloc , & own_free , NULL ) )
73
+ if ( NULL != coap_message_handler_init (& test_own_alloc , & test_own_free , NULL ) )
66
74
return false;
67
- if ( NULL != coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function ) )
75
+ if ( NULL != coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function ) )
68
76
return false;
69
77
retCounter = 1 ;
70
78
sn_coap_protocol_stub .expectedCoap = NULL ;
71
- if ( NULL != coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function ) )
79
+ if ( NULL != coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function ) )
72
80
return false;
73
81
retCounter = 1 ;
74
82
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
75
83
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
76
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
84
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
77
85
if ( NULL == handle )
78
86
return false;
79
87
free (sn_coap_protocol_stub .expectedCoap );
@@ -89,7 +97,7 @@ bool test_coap_message_handler_destroy()
89
97
retCounter = 1 ;
90
98
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
91
99
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
92
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
100
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
93
101
94
102
if ( 0 != coap_message_handler_destroy (handle ) )
95
103
return false;
@@ -105,7 +113,7 @@ bool test_coap_message_handler_find_transaction()
105
113
retCounter = 1 ;
106
114
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
107
115
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
108
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
116
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
109
117
110
118
uint8_t buf [16 ];
111
119
memset (& buf , 1 , 16 );
@@ -139,7 +147,7 @@ bool test_coap_message_handler_coap_msg_process()
139
147
retCounter = 1 ;
140
148
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
141
149
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
142
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
150
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
143
151
144
152
sn_coap_protocol_stub .expectedHeader = NULL ;
145
153
/* Coap parse returns null */
@@ -213,7 +221,7 @@ bool test_coap_message_handler_request_send()
213
221
retCounter = 1 ;
214
222
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
215
223
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
216
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
224
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
217
225
218
226
uint8_t buf [16 ];
219
227
memset (& buf , 1 , 16 );
@@ -255,7 +263,7 @@ bool test_coap_message_handler_request_delete()
255
263
retCounter = 1 ;
256
264
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
257
265
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
258
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
266
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
259
267
260
268
uint8_t buf [16 ];
261
269
memset (& buf , 1 , 16 );
@@ -291,7 +299,7 @@ bool test_coap_message_handler_response_send()
291
299
retCounter = 1 ;
292
300
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
293
301
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
294
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
302
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
295
303
sn_coap_hdr_s * header = (sn_coap_hdr_s * )malloc (sizeof (sn_coap_hdr_s ));
296
304
memset (header , 0 , sizeof (sn_coap_hdr_s ));
297
305
@@ -340,15 +348,54 @@ bool test_coap_message_handler_response_send()
340
348
341
349
bool test_coap_message_handler_exec ()
342
350
{
351
+ /* Null as a parameter */
343
352
if ( -1 != coap_message_handler_exec (NULL , 0 ))
344
353
return false;
354
+
345
355
retCounter = 1 ;
346
356
sn_coap_protocol_stub .expectedCoap = (struct coap_s * )malloc (sizeof (struct coap_s ));
347
357
memset (sn_coap_protocol_stub .expectedCoap , 0 , sizeof (struct coap_s ));
348
- coap_msg_handler_t * handle = coap_message_handler_init (& own_alloc , & own_free , & coap_tx_function );
358
+ coap_msg_handler_t * handle = coap_message_handler_init (& test_own_alloc , & test_own_free , & coap_tx_function );
359
+
360
+ if ( 0 != coap_message_handler_exec (handle , 0 ))
361
+ return false;
362
+
363
+ nsdynmemlib_stub .returnCounter = 1 ;
364
+ coap_transaction_t * transact_ptr = transaction_create ();
365
+
366
+ /* Transaction not timed out*/
349
367
if ( 0 != coap_message_handler_exec (handle , 0 ))
350
368
return false;
351
369
370
+ if (transaction_cb != 0 )
371
+ return false;
372
+
373
+ /* Timed out, no CB */
374
+ if ( 0 != coap_message_handler_exec (handle , 300 ))
375
+ return false;
376
+
377
+ if (transaction_cb != 0 )
378
+ return false;
379
+
380
+ nsdynmemlib_stub .returnCounter = 1 ;
381
+ transact_ptr = transaction_create ();
382
+ transact_ptr -> resp_cb = transaction_recv_cb ;
383
+
384
+ /* Transaction not timed out */
385
+ if ( 0 != coap_message_handler_exec (handle , 0 ))
386
+ return false;
387
+
388
+ if (transaction_cb != 0 )
389
+ return false;
390
+
391
+ /* Transaction timed out */
392
+ if ( 0 != coap_message_handler_exec (handle , 300 ))
393
+ return false;
394
+
395
+ if (transaction_cb == 0 )
396
+ return false;
397
+
398
+ /* Teardown */
352
399
free (sn_coap_protocol_stub .expectedCoap );
353
400
sn_coap_protocol_stub .expectedCoap = NULL ;
354
401
coap_message_handler_destroy (handle );
0 commit comments