-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathcoap_reliability.cpp
More file actions
1255 lines (1108 loc) · 35.5 KB
/
Copy pathcoap_reliability.cpp
File metadata and controls
1255 lines (1108 loc) · 35.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#include <climits>
#include "coap_channel.h"
#include "forward_message_channel.h"
#include "messages.h"
#include <catch2/catch.hpp>
#include "fakeit.hpp"
using namespace particle::protocol;
using namespace fakeit;
/**
* A CoAPChannel that uses a forwarding message channel so we can redirect
* where messages are delivered.
*/
class ForwardCoAPChannel: public CoAPChannel<ForwardMessageChannel>
{
using super = CoAPChannel<ForwardMessageChannel>;
public:
ForwardCoAPChannel(MessageChannel& ch)
{
setForward(&ch);
}
};
message_id_t decode_id(Message& msg)
{
uint8_t* buf = msg.buf();
return buf[2] << 8 | buf[3];
}
SCENARIO("message IDs monotinically increment from 1")
{
GIVEN("a CoAP message channel and a message with no assigned ID")
{
Mock<MessageChannel> mock;
ForwardCoAPChannel channel(mock.get());
uint8_t buf[10];
When(Method(mock,create)).AlwaysDo([&buf](Message& msg, size_t len)
{
msg.set_buffer(buf, 10); return NO_ERROR;
});
When(Method(mock,send)).AlwaysReturn(NO_ERROR);
WHEN("a message is sent")
{
Message msg;
CHECK(channel.create(msg, 5)==NO_ERROR);
msg.set_length(4);
REQUIRE(channel.send(msg)==NO_ERROR);
THEN("the encoded message ID is 1")
{
REQUIRE(decode_id(msg)==1);
REQUIRE(msg.has_id());
REQUIRE(msg.get_id()==1);
}
AND_WHEN("a second message is sent")
{
Message msg;
CHECK(channel.create(msg, 5)==NO_ERROR);
msg.set_length(4);
REQUIRE(channel.send(msg)==NO_ERROR);
THEN("the encoded message ID is 2")
{
REQUIRE(msg.has_id());
REQUIRE(msg.get_id()==2);
REQUIRE(decode_id(msg)==2);
}
AND_WHEN("300 more messages are sent")
{
for (int i=0; i<300; i++)
{
Message msg;
channel.create(msg, 5);
msg.set_length(4);
channel.send(msg);
}
THEN("the encoded message ID is 302")
{
REQUIRE(decode_id(msg)==302);
}
}
}
}
}
}
SCENARIO("there are no registered messages initially")
{
GIVEN("an empty message store")
{
CoAPMessageStore store;
THEN("no messages are present")
{
REQUIRE(store.from_id(0)==nullptr);
REQUIRE(store.from_id(1)==nullptr);
REQUIRE(store.from_id(2)==nullptr);
}
}
}
SCENARIO("a message can be added and removed by id", "[reliability]")
{
const message_id_t id = 456;
GIVEN("an empty message store")
{
Mock<MessageChannel> mock;
CoAPMessageStore store;
REQUIRE(store.from_id(id)==nullptr);
WHEN("a message is added with id 456")
{
CoAPMessage message(id);
REQUIRE(store.add(message)==NO_ERROR);
THEN("the message can be retrieved by id 456")
{
CoAPMessage* retrieve = store.from_id(id);
REQUIRE(retrieve==&message);
AND_WHEN("the message is removed")
{
CoAPMessage* removed = store.remove(id);
THEN("the removed message is the one added")
{
REQUIRE(removed==&message);
REQUIRE(message.get_next()==nullptr);
AND_WHEN("the same id is removed again") {
CoAPMessage* removed2 = store.remove(id);
THEN("no message is retrieved") {
REQUIRE(removed2==nullptr);
REQUIRE(store.remove(id)==nullptr);
}
}
}
}
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("a message can be added to a message store with the same id more than once", "[reliability]")
{
const message_id_t id = 456;
GIVEN("an empty message store")
{
Mock<MessageChannel> mock;
WHEN("a message is added twice")
{
CoAPMessageStore store;
REQUIRE(store.from_id(id)==nullptr);
CoAPMessage* m1 = new CoAPMessage(id);
CoAPMessage* m2 = new CoAPMessage(id);
CHECK(store.add(m1)==NO_ERROR);
REQUIRE(store.add(m2)==NO_ERROR);
THEN("retrieving a the message by id returns the last added message")
{
REQUIRE(store.from_id(id)==m2);
AND_WHEN("the message is removed")
{
CHECK(store.remove(id)==m2);
THEN("no message is retrieved")
{
REQUIRE(store.from_id(id)==nullptr);
}
}
}
// m1 already removed by the store
delete m2;
}
}
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("multiple messages are stored in a list in the order they are added, most recent first")
{
const message_id_t id1 = 456;
const message_id_t id2 = 345;
REQUIRE(CoAPMessage::messages()==0);
GIVEN("an empty message store")
{
CoAPMessageStore store;
CoAPMessage* m1 = new CoAPMessage(id1);
CoAPMessage* m2 = new CoAPMessage(id2);
REQUIRE(store.from_id(id1)==nullptr);
REQUIRE(store.from_id(id2)==nullptr);
WHEN("adding two messages with distinct IDs")
{
REQUIRE(m1->get_id()==id1);
REQUIRE(m2->get_id()==id2);
REQUIRE(store.add(m1)==NO_ERROR);
REQUIRE(store.add(m2)==NO_ERROR);
THEN("the second message points to the first")
{
REQUIRE(m2->get_next()==m1);
AND_THEN("both messages can be retrieved")
{
REQUIRE(store.from_id(id1)==m1);
REQUIRE(store.from_id(id2)==m2);
}
AND_WHEN("removing the most recently added message")
{
REQUIRE(store.remove(id2)==m2);
THEN("only that message is removed")
{
CHECK(m2->get_next()==nullptr);
CHECK(store.from_id(id2)==nullptr);
CHECK(store.from_id(id1)==m1);
}
delete m2; // m1 will be removed by the store
}
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("adding the same message twice")
{
const message_id_t id = 456;
GIVEN("an empty message store")
{
Mock<MessageChannel> mock;
CoAPMessageStore store;
REQUIRE(store.from_id(id)==nullptr);
CoAPMessage* m = new CoAPMessage(id);
WHEN("the message is added twice")
{
CHECK(store.add(m)==NO_ERROR);
THEN("the second add succeeds")
{
REQUIRE(store.add(m)==NO_ERROR);
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
void build_message_channel_mock(Mock<MessageChannel>& mock)
{
When(Method(mock,notify_established)).AlwaysReturn(NO_ERROR);
When(Method(mock,is_unreliable)).AlwaysReturn(false);
When(Method(mock,command)).AlwaysReturn(NO_ERROR);
When(Method(mock,notify_client_messages_processed)).AlwaysReturn();
}
SCENARIO("an unacknowledged message is resent up to MAX_TRANSMIT times, with exponentially increasing delays, after which it is removed")
{
REQUIRE(CoAPMessage::messages()==0);
GIVEN("a channel and a confirmable message")
{
Mock<MessageChannel> mock;
MessageChannel& channel = mock.get();
uint8_t buf[10];
When(Method(mock,create)).AlwaysDo([&buf](Message& msg, size_t len)
{
msg.set_buffer(buf, 10); return NO_ERROR;
});
build_message_channel_mock(mock);
Message m;
channel.create(m, 5);
uint8_t data[] = { 0x40, 0, 0x12, 0x34 };
memcpy(m.buf(), data, 4);
m.set_length(4);
m.decode_id();
message_id_t id = m.get_id();
CoAPMessageStore store;
REQUIRE(id==0x1234);
REQUIRE(store.is_confirmable(m.buf()));
WHEN("the message is sent")
{
REQUIRE(store.send(m, 0)==NO_ERROR);
THEN("the message is pending")
{
CoAPMessage* cm = store.from_id(id);
REQUIRE(cm!=nullptr);
REQUIRE(cm->get_timeout()<CoAPMessage::ACK_TIMEOUT*CoAPMessage::ACK_RANDOM_FACTOR/CoAPMessage::ACK_RANDOM_DIVISOR);
REQUIRE(cm->get_timeout()>=CoAPMessage::ACK_TIMEOUT*1);
// the send() function should be invoked with a message corresponding to the coap message
auto verify_message = [cm](Message& msg)->ProtocolError {
REQUIRE(msg.get_id()==cm->get_id());
REQUIRE(msg.length()==cm->get_data_length());
REQUIRE(!memcmp(msg.buf(), cm->get_data(), msg.length()));
return NO_ERROR;
};
When(Method(mock,send)).AlwaysDo(verify_message);
AND_WHEN("the message times out MAX_RETRANSMIT times")
{
system_tick_t last_timeout = 0;
for (int i=0; i<CoAPMessage::MAX_RETRANSMIT; i++)
{
CoAPMessage* m = store.from_id(id);
REQUIRE(m->get_timeout()>last_timeout);
last_timeout = m->get_timeout();
store.process(m->get_timeout(), channel);
Verify(Method(mock,send)).Exactly(i+1);
REQUIRE(store.from_id(id)==m);
}
THEN("the message has been sent MAX_RETRANSMIT times")
{
Verify(Method(mock,send)).Exactly(CoAPMessage::MAX_RETRANSMIT);
AND_WHEN("process is invoked once more")
{
// the final time removes it from the store
store.process(cm->get_timeout(), channel);
AND_THEN("send is not called again and the message the message is no longer pending")
{
Verify(Method(mock,send)).Exactly(CoAPMessage::MAX_RETRANSMIT);
REQUIRE(store.from_id(id)==nullptr);
}
}
}
}
AND_WHEN("the message times out 3 times")
{
system_tick_t last_timeout = 0;
for (int i=0; i<3; i++)
{
CoAPMessage* m = store.from_id(id);
REQUIRE(m->get_timeout()>=last_timeout);
last_timeout = m->get_timeout();
store.process(m->get_timeout(), channel);
Verify(Method(mock,send)).Exactly(i+1);
REQUIRE(store.from_id(id)==m);
}
THEN("the message has been sent 3 times")
{
Verify(Method(mock,send)).Exactly(3);
AND_WHEN("the message is acknowledged")
{
system_tick_t timeout = cm->get_timeout();
m.set_length(Messages::empty_ack(m.buf(), 0x12, 0x34));
store.receive(m, channel, 0);
THEN("the message is no longer pending")
{
REQUIRE(store.from_id(id)==nullptr);
REQUIRE(CoAPMessage::messages()==0);
// the final time removes it from the store
store.process(timeout, channel);
Verify(Method(mock,send)).Exactly(3);
REQUIRE(store.from_id(id)==nullptr);
}
}
}
}
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("a CoAPMessage can be created with the message buffer part of the allocation")
{
// todo - factor out the message tests to their own test suite
Message msg;
msg.set_id(1234);
uint8_t buf[234];
msg.set_buffer(buf, sizeof(buf));
REQUIRE(msg.has_id());
REQUIRE(msg.get_id()==1234);
REQUIRE(msg.length()==0);
REQUIRE(msg.capacity()==sizeof(buf));
for (int i=0; i<sizeof(buf); i++)
buf[i] = rand();
msg.set_length(sizeof(buf));
REQUIRE(msg.length()==sizeof(buf));
CoAPMessage* coapmsg = CoAPMessage::create(msg);
REQUIRE(coapmsg!=nullptr);
REQUIRE(coapmsg->get_id()==1234);
REQUIRE(coapmsg->get_next()==nullptr);
REQUIRE(coapmsg->matches(1234));
REQUIRE(coapmsg->get_data_length()==sizeof(buf));
const uint8_t* data = coapmsg->get_data();
REQUIRE(data!=nullptr);
for (int i=0; i<sizeof(buf); i++) {
INFO( "checking coapmsg buffer index " << i);
if (data[i]!=buf[i])
FAIL("buffer content is different");
}
delete coapmsg;
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("a CoAPMessage can be created with a smaller buffer")
{
GIVEN("a message")
{
uint8_t buf[] = { 0x40, 0x00, 0x12, 0x34, 0xFF, 1, 2, 3, 4, 0, 0, 0 }; // 12 bytes, 9 of them message content
Message m(buf, 12, 9);
m.decode_id();
WHEN("A CoAP message of length 5 is created")
{
CoAPMessage* cm = CoAPMessage::create(m, 5);
THEN("It has a distinct buffer of size 5")
{
REQUIRE(cm!=nullptr);
REQUIRE(cm->get_data()!=buf);
REQUIRE(cm->get_data_length()==5);
}
THEN("that equals the first 5 bytes of the orginal buffer")
{
REQUIRE(!memcmp(cm->get_data(), buf, 5));
}
delete cm;
}
}
}
SCENARIO("sending a non-confirmable message does not add a new coap message to the store")
{
GIVEN("a reliable channel")
{
Mock<MessageChannel> mock;
MessageChannel& channel = mock.get();
CoAPMessageStore store;
uint8_t buf[10];
When(Method(mock,create)).AlwaysDo([&buf](Message& msg, size_t len)
{
msg.set_buffer(buf, 10); return NO_ERROR;
});
When(Method(mock,send)).AlwaysReturn(NO_ERROR);
Message m;
channel.create(m, 5);
uint8_t data[] = { 0x50, 0, 0x12, 0x34 };
memcpy(m.buf(), data, 4);
m.set_length(4);
m.decode_id();
REQUIRE(m.get_type()==CoAPType::NON);
REQUIRE(m.get_id()==0x1234);
WHEN("the message is sent")
{
store.send(m, 0);
THEN("the message is sent without being stored")
{
REQUIRE(store.from_id(0x1234)==nullptr);
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
SCENARIO("a Confirmable message is pending until acknowledged, reset or timeout")
{
GIVEN("a channel and a confirmable message")
{
Mock<MessageChannel> mock;
MessageChannel& channel = mock.get();
uint8_t buf[10];
When(Method(mock,create)).AlwaysDo([&buf](Message& msg, size_t len)
{
msg.set_buffer(buf, 10); return NO_ERROR;
});
When(Method(mock,send)).AlwaysReturn(NO_ERROR);
build_message_channel_mock(mock);
Message m;
channel.create(m, 5);
uint8_t data[] = { 0x40, 0, 0x12, 0x34 };
memcpy(m.buf(), data, 4);
m.set_length(4);
m.decode_id();
message_id_t id = m.get_id();
REQUIRE(CoAPMessage::messages()==0);
WHEN("the message is sent")
{
CoAPMessageStore store;
REQUIRE(id==0x1234);
REQUIRE(store.is_confirmable(m.buf()));
REQUIRE(store.send(m, 0)==NO_ERROR);
THEN("the message is pending")
{
REQUIRE(store.from_id(id)!=nullptr);
AND_WHEN("the message is acknowledged")
{
m.set_length(Messages::empty_ack(m.buf(), 0x12, 0x34));
store.receive(m, channel, 0);
REQUIRE(m.length()>0); // ack is available to application
THEN("the message is no longer pending")
{
REQUIRE(store.from_id(id)==nullptr);
REQUIRE(CoAPMessage::messages()==0);
}
AND_WHEN("the message is acknowledged a second time")
{
m.set_length(Messages::empty_ack(m.buf(), 0x12, 0x34));
store.receive(m, channel, 0);
THEN("the message is no longer pending")
{
REQUIRE(store.from_id(id)==nullptr);
REQUIRE(CoAPMessage::messages()==0);
}
AND_THEN("the message is not made available to the application")
{
REQUIRE(m.length()==0);
}
}
}
AND_WHEN("the message is reset")
{
m.set_length(Messages::reset(m.buf(), 0x12, 0x34));
store.receive(m, channel, 0);
THEN("the message is no longer pending")
{
REQUIRE(store.from_id(id)==nullptr);
REQUIRE(CoAPMessage::messages()==0);
}
}
}
}
}
REQUIRE(CoAPMessage::messages()==0);
}
using particle::protocol::time_has_passed;
SCENARIO("Rollover timestamps can be used to determine a timeout")
{
REQUIRE(time_has_passed(2000,1000));
REQUIRE(time_has_passed(2000,2000));
REQUIRE(!time_has_passed(2000,3000));
REQUIRE(time_has_passed(0x80000000, 1));
REQUIRE(!time_has_passed(0x80000000, 0));
REQUIRE(time_has_passed(1000, 0xFFFFFFFF));
REQUIRE(!time_has_passed(0xFFFFFFFF, 1000));
REQUIRE(time_has_passed(0, 0x80000000));
REQUIRE(!time_has_passed(1, 0x80000000));
}
SCENARIO("retransmit delay is exponential with randomness")
{
for (int i=0; i<CoAPMessage::MAX_RETRANSMIT; i++)
{
system_tick_t gmin = INT_MAX;
system_tick_t gmax = 0;
for (unsigned u = 0; u<500; u++)
{
system_tick_t timeout = CoAPMessage::transmit_timeout(i);
system_tick_t min = 4000 << i;
system_tick_t max = min * 1.5;
if (timeout<min)
REQUIRE(timeout>=min);
if (timeout>max)
REQUIRE(timeout<=max);
if (timeout<gmin)
gmin = timeout;
if (timeout>gmax)
gmax = timeout;
}
REQUIRE(gmin<gmax);
}
}
SCENARIO("a repeated confirmable CoAP message is passed only once to the application and the acknowledgement is retained and returned until MAX_TRANSMIT_SPAN time has elapsed")
{
GIVEN("a Confirmable message is received multiple times")
{
uint8_t buf[] = { 0x40, 0, 0x12, 0x34, 0xFF, 1, 2, 3, 4 };
Message m(buf, sizeof(buf), sizeof(buf)); // confirmable message
Mock<MessageChannel> mock;
build_message_channel_mock(mock);
MessageChannel& channel = mock.get();
CoAPMessageStore store;
WHEN("the Confirmable message is received first time")
{
REQUIRE(store.receive(m, channel, 0)==NO_ERROR);
THEN("it is made available to the application")
{
REQUIRE(m.length()==9); // message is available to application
REQUIRE(store.from_id(0x1234)!=nullptr); // todo - could compare message content to original message
AND_WHEN("the Confirmable message is received again")
{
REQUIRE(store.receive(m, channel, 123)==NO_ERROR);
THEN("then no response is sent to the channel")
{
AND_THEN("the message is not returned to the application")
{
REQUIRE(m.length()==0);
AND_WHEN("MAX_TRANSMIT_SPAN time has elapsed the message is removed")
{
CoAPMessage* msg = store.from_id(0x1234);
REQUIRE(msg!=nullptr);
REQUIRE(msg->get_timeout()==45*1000);
store.process(msg->get_timeout(), channel);
REQUIRE(store.from_id(0x1234)==nullptr);
}
}
}
}
AND_WHEN("the message is acknowledged and the message is received again")
{
uint8_t buf2[9];
Message ack(buf2, sizeof(buf2), sizeof(buf2));
ack.set_length(Messages::empty_ack(buf2, 0x12, 0x34));
ack.decode_id();
store.send(ack, 0); // application response
auto validate_message = [ack](Message& msg){
REQUIRE(msg.get_id()==0x1234);
REQUIRE(msg.length()==ack.length());
REQUIRE(!memcmp(msg.buf(), ack.buf(), msg.length()));
return NO_ERROR;
};
When(Method(mock,send)).Do(validate_message);
REQUIRE(store.receive(m, channel,123)==NO_ERROR);
THEN("the previous response is sent to the channel")
{
Verify(Method(mock,send)).Exactly(Once);
}
AND_THEN("the message is not returned to the application")
{
REQUIRE(m.length()==0);
AND_WHEN("MAX_TRANSMIT_SPAN time has elapsed the message is removed")
{
CoAPMessage* msg = store.from_id(0x1234);
REQUIRE(msg!=nullptr);
REQUIRE(msg->get_timeout()==45*1000);
store.process(msg->get_timeout(), channel);
REQUIRE(store.from_id(0x1234)==nullptr);
}
}
}
}
}
}
}
/**
* A reliable CoAP Channel that uses a forwarding message channel.
* This allows the actual message channel to be set later (e.g. a mock.)
* The typename M is a callable that provides the current time.
*/
template <typename M>
class ForwardCoAPReliableChannel: public CoAPReliableChannel<ForwardMessageChannel, M>
{
using super = CoAPReliableChannel<ForwardMessageChannel, M>;
public:
ForwardCoAPReliableChannel(MessageChannel& ch, M m) : super(m)
{
this->setForward(&ch);
}
};
template<typename R>
struct Callme
{
virtual R operator()()=0;
};
SCENARIO("the send_synchronous method blocks until the message is sent or the send fails or the send times out")
{
GIVEN("A confirmable message marked as confirm received")
{
uint8_t buf[] = { 0x40, 0, 0x12, 0x34, 0xFF, 1, 2, 3, 4 };
Message m(buf, sizeof(buf), sizeof(buf));
m.set_confirm_received(true);
m.decode_id();
Mock<MessageChannel> mock;
build_message_channel_mock(mock);
MessageChannel& delegate = mock.get();
typedef Callme<system_tick_t> time_source;
Mock<time_source> mockTime;
time_source& time = mockTime.get();
system_tick_t time_count = 0;
auto time_increment = [&time_count]() { return time_count+=1000; };
When(Method(mockTime, operator())).AlwaysDo(time_increment);
ForwardCoAPReliableChannel<decltype(time)> channel(delegate, time);
const CoAPMessageStore& store = channel.client_messages();
WHEN("the message is sent")
{
REQUIRE(m.get_id()==0x1234);
AND_WHEN("the channel retrieves a response")
{
uint8_t buf2[5];
Message ack(buf2, sizeof(buf2));
ack.set_length(Messages::empty_ack(buf2, 0x12, 0x34));
auto receive_ack = [&ack](Message& msg) {
msg.set_buffer(ack.buf(), ack.capacity());
msg.set_length(ack.length());
return NO_ERROR;
};
When(Method(mock,receive)).Do(receive_ack);
auto verify_msg = [&m](Message& msg) {
REQUIRE(m.get_id()==msg.get_id());
REQUIRE(m.length()==msg.length());
REQUIRE(!memcmp(m.buf(), msg.buf(), msg.length()));
return NO_ERROR;
};
When(Method(mock,send)).AlwaysDo(verify_msg);
THEN("the send_synchronous method returns success")
{
ProtocolError error = channel.send_synchronous(m);
REQUIRE(error==NO_ERROR);
AND_THEN("the CoAP message is purged")
{
REQUIRE(store.from_id(0x1234)==nullptr);
}
AND_THEN("notify_client_messages_processed() is not invoked")
{
Verify(Method(mock, notify_client_messages_processed)).Never();
}
}
}
AND_WHEN("no response is received")
{
// receive endlessly receives no packets
auto receive_none = [](Message& msg) {
msg.set_length(0);
return NO_ERROR;
};
When(Method(mock,receive)).AlwaysDo(receive_none);
auto verify_msg = [buf](Message& msg) {
// NB: the passed in message instance is re-used to retrieve messages from the channel
// hence m will be equal to th eempty message. (Previous code tried comparing m==msg which consequently failed.)
REQUIRE(9==msg.length());
REQUIRE(!memcmp(buf, msg.buf(), msg.length()));
REQUIRE(0x1234==msg.get_id());
return NO_ERROR;
};
When(Method(mock,send)).AlwaysDo(verify_msg);
THEN("the send_synchronous method times out")
{
ProtocolError error = channel.send_synchronous(m);
REQUIRE(error==MESSAGE_TIMEOUT);
AND_THEN("the CoAP message is purged")
{
REQUIRE(store.from_id(0x1234)==nullptr);
}
AND_THEN("notify_client_messages_processed() is not invoked")
{
Verify(Method(mock, notify_client_messages_processed)).Never();
}
}
}
AND_WHEN("the error response IO_ERROR is received on the first send")
{
When(Method(mock,send)).Return(IO_ERROR);
THEN("the send_synchronous method fails with IO_ERROR")
{
ProtocolError error = channel.send_synchronous(m);
REQUIRE(error==IO_ERROR);
AND_THEN("the CoAP message is purged")
{
REQUIRE(store.from_id(0x1234)==nullptr);
}
AND_THEN("notify_client_messages_processed() is not invoked")
{
Verify(Method(mock, notify_client_messages_processed)).Never();
}
}
}
AND_WHEN("the error response IO_ERROR is received the second send")
{
When(Method(mock,send)).Return(NO_ERROR);
When(Method(mock,send)).Return(IO_ERROR);
THEN("the send_synchronous method fails with IO_ERROR")
{
ProtocolError error = channel.send_synchronous(m);
REQUIRE(error==IO_ERROR);
AND_THEN("the CoAP message is purged")
{
REQUIRE(store.from_id(0x1234)==nullptr);
}
AND_THEN("notify_client_messages_processed() is not invoked")
{
Verify(Method(mock, notify_client_messages_processed)).Never();
}
}
}
}
}
}
SCENARIO("sending a message and re-establishing the connection clears existing messages")
{
GIVEN("a CoAPReliableChannel")
{
Mock<MessageChannel> mock;
MessageChannel& delegate = mock.get();
auto time = []() { return system_tick_t(0); }; // time not needed here
ForwardCoAPReliableChannel<decltype(time)> channel(delegate, time);
WHEN("a CON message is sent")
{
uint8_t buf[] = { 0x40, 0, 0x12, 0x34, 0xFF, 1, 2, 3, 4 };
Message m(buf, sizeof(buf), sizeof(buf));
m.decode_id();
When(Method(mock, send)).Return(NO_ERROR); // send on the channel is called once
channel.send(m); // send and register with the store
THEN("the message is retained in the client store")
{
REQUIRE(channel.client_messages().from_id(0x1234)!=nullptr); // message has been sent and registered
REQUIRE(CoAPMessage::messages()==1);
Verify(Method(mock,send)).Exactly(Once);
}
AND_WHEN("the connection is re-established")
{
When(Method(mock,establish)).Return(NO_ERROR);
channel.establish();
THEN("the message store is cleared")
{
REQUIRE(channel.client_messages().from_id(0x1234)==nullptr); // message has been sent and registered
REQUIRE(CoAPMessage::messages()==0);
Verify(Method(mock,establish)).Exactly(Once);
}
}
}
}
}
SCENARIO("sending a reliable message first passes it to the store, and then on to the channel")
{
GIVEN("a CoAPReliableChannel")
{
Mock<MessageChannel> mock;
MessageChannel& delegate = mock.get();
auto time = []() { return system_tick_t(0); }; // time not needed here
ForwardCoAPReliableChannel<decltype(time)> channel(delegate, time);
uint8_t buf[] = { 0x40, 0, 0x12, 0x34, 0xFF, 1, 2, 3, 4 };
Message m(buf, sizeof(buf), sizeof(buf));
WHEN("an invalid message is sent")
{
ProtocolError result = channel.send(m);
THEN("the message is not sent to the channel")
{
Verify(Method(mock,send)).Exactly(0);
REQUIRE(result==MISSING_MESSAGE_ID);
REQUIRE(channel.client_messages().from_id(0x1234)==nullptr);
REQUIRE(channel.server_messages().from_id(0x1234)==nullptr);
}
}
AND_WHEN("a valid message is sent")
{
m.decode_id(); // set up the ID
When(Method(mock,send)).Return(NO_ERROR);
ProtocolError result = channel.send(m);
THEN("the message is sent to the channel")
{
Verify(Method(mock,send)).Exactly(1);
REQUIRE(result==NO_ERROR);
REQUIRE(channel.client_messages().from_id(0x1234)!=nullptr);
REQUIRE(channel.server_messages().from_id(0x1234)==nullptr);
}
}
AND_WHEN("a valid message is sent but fails sending")
{
m.decode_id(); // set up the ID
When(Method(mock,send)).Return(IO_ERROR);
ProtocolError result = channel.send(m);
THEN("the message is remains in the store and the send error is returned")
{
Verify(Method(mock,send)).Exactly(1);
REQUIRE(result==IO_ERROR);
// still maintained in the client store.
REQUIRE(channel.client_messages().from_id(0x1234)!=nullptr);
REQUIRE(channel.server_messages().from_id(0x1234)==nullptr);
}
}
}
}
SCENARIO("receiving a message first retrieves from the channel and then passes the message to the store")
{
GIVEN("a CoAPReliableChannel")
{
Mock<MessageChannel> mock;
MessageChannel& delegate = mock.get();
auto time = []() { return system_tick_t(234); };
ForwardCoAPReliableChannel<decltype(time)> channel(delegate, time);
uint8_t con[] = { 0x40, 0, 0x12, 0x34, 0xFF, 1, 2, 3, 4 };
uint8_t buf[9];
Message m(buf, sizeof(buf)); // empty message
m.decode_id();
WHEN("the message is received")
{
auto receive_con = [con](Message& msg) {
memcpy(msg.buf(), con, 9);
msg.set_length(9);
return NO_ERROR;
};
When(Method(mock,receive)).Do(receive_con);
ProtocolError result = channel.receive(m);
THEN("it is passed to the server store")
{
Verify(Method(mock,receive)).Exactly(1);
CoAPMessage* cm = channel.server_messages().from_id(0x1234);
REQUIRE(cm!=nullptr);
REQUIRE(cm->get_timeout()==CoAPMessage::MAX_TRANSMIT_SPAN + 234);
}
}
}
}
bool message_equals(CoAPMessage* cm, Message& msg, const char* name)
{
INFO("checking message " << name);
REQUIRE(cm!=nullptr);
REQUIRE(cm->get_id()==msg.get_id());
REQUIRE( cm->get_data_length()==msg.length());
REQUIRE(!memcmp(cm->get_data(), msg.buf(), msg.length()));
return true;
}
SCENARIO("client and server can send requests with the same message ID and they are not confused")
{
GIVEN("a reliable CoAP channel")