forked from driftregion/iso14229
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_iso14229.c
1244 lines (1049 loc) · 43.8 KB
/
test_iso14229.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
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
#include "test_iso14229.h"
#include "iso14229.h"
#include "iso14229client.h"
#include "iso14229server.h"
#include "isotp-c/isotp.h"
#include "isotp-c/isotp_defines.h"
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
uint32_t mockUserGetms();
int mockClientSendCAN(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size);
int mockServerCANTransmit(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size);
enum Iso14229CANRxStatus mockServerCANRxPoll(uint32_t *arbitration_id, uint8_t *data,
uint8_t *size);
void mockSrvFuncLinkDbg(const char *message, ...);
void mockSrvPhysLinkDbg(const char *message, ...);
void mockClientLinkDbg(const char *message, ...);
// ================================================
// Global Variables
// ================================================
#define CAN_MESSAGE_QUEUE_SIZE 10
// global state: memset() to zero in TEST_SETUP();
static struct {
int ms; // simulated absolute time
int t0; // marks a time point
struct CANMessage serverRecvQueue[CAN_MESSAGE_QUEUE_SIZE];
int serverRecvQueueIdx;
struct CANMessage clientRecvQueue[CAN_MESSAGE_QUEUE_SIZE];
int clientRecvQueueIdx;
int g_serverSvcCallCount[ISO14229_NUM_SERVICES];
Iso14229Service g_serverServices[ISO14229_NUM_SERVICES];
IsoTpLink clientLink, srvPhysLink, srvFuncLink;
uint8_t srvPhysLinkRxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t srvPhysLinkTxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t srvFuncLinkRxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t srvFuncLinkTxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t clientLinkRxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t clientLinkTxBuf[DEFAULT_ISOTP_BUFSIZE];
uint8_t scratch[DEFAULT_ISOTP_BUFSIZE];
uint16_t size;
int ret;
enum Iso14229ResponseCode userResponse;
} g;
static const struct IsoTpLinkConfig SRV_PHYS_LINK_DEFAULT_CONFIG = {
.send_id = SERVER_SEND_ID,
.send_buffer = g.srvPhysLinkTxBuf,
.send_buf_size = sizeof(g.srvPhysLinkTxBuf),
.recv_buffer = g.srvPhysLinkRxBuf,
.recv_buf_size = sizeof(g.srvPhysLinkRxBuf),
.user_get_ms = mockUserGetms,
.user_send_can = mockServerCANTransmit,
.user_debug = mockSrvPhysLinkDbg};
static const struct IsoTpLinkConfig CLIENT_LINK_DEFAULT_CONFIG = {
.send_id = SERVER_PHYS_RECV_ID,
.send_buffer = g.clientLinkTxBuf,
.send_buf_size = sizeof(g.clientLinkTxBuf),
.recv_buffer = g.clientLinkRxBuf,
.recv_buf_size = sizeof(g.clientLinkRxBuf),
.user_get_ms = mockUserGetms,
.user_send_can = mockClientSendCAN,
.user_debug = mockClientLinkDbg};
// ================================================
// common mock functions
// ---
// these are used in all tests
// ================================================
uint32_t mockUserGetms() { return g.ms; }
// ================================================
// isotp-c mock functions
// ================================================
void mockClientLinkDbg(const char *message, ...) {
printf("CLIENT ISO-TP: ");
va_list ap;
va_start(ap, message);
vprintf(message, ap);
va_end(ap);
}
void mockSrvPhysLinkDbg(const char *message, ...) {
printf("SRV PHYS ISO-TP: ");
va_list ap;
va_start(ap, message);
vprintf(message, ap);
va_end(ap);
}
void mockSrvFuncLinkDbg(const char *message, ...) {
printf("SRV FUNC ISO-TP: ");
va_list ap;
va_start(ap, message);
vprintf(message, ap);
va_end(ap);
}
// ================================================
// Iso14229Server Mock Functions
// ------------------------------------------------
// These are used in tests that require server callbacks
// ================================================
static enum Iso14229ResponseCode
mockDiagnosticSessionControlHandler(const struct Iso14229ServerStatus *status,
enum Iso14229DiagnosticSessionType type) {
(void)status;
(void)type;
return kPositiveResponse;
}
static enum Iso14229ResponseCode mockUserRdbiHandler(const struct Iso14229ServerStatus *status,
uint16_t data_id,
uint8_t const **data_location, uint16_t *len) {
(void)status;
const uint8_t vin[] = {0x57, 0x30, 0x4C, 0x30, 0x30, 0x30, 0x30, 0x34, 0x33,
0x4D, 0x42, 0x35, 0x34, 0x31, 0x33, 0x32, 0x36};
const uint8_t data_0x010A[] = {0xA6, 0x66, 0x07, 0x50, 0x20, 0x1A,
0x00, 0x63, 0x4A, 0x82, 0x7E};
const uint8_t data_0x0110[] = {0x8C};
switch (data_id) {
case 0xF190:
*data_location = vin;
*len = sizeof(vin);
break;
case 0x010A:
*data_location = data_0x010A;
*len = sizeof(data_0x010A);
break;
case 0x0110:
*data_location = data_0x0110;
*len = sizeof(data_0x0110);
break;
default:
return kRequestOutOfRange;
}
return kPositiveResponse;
}
static int g_mockECUResetHandlerCallCount = 0;
static enum Iso14229ResponseCode mockECUResetHandler(const struct Iso14229ServerStatus *status,
uint8_t resetType, uint8_t *powerDownTime) {
(void)status;
(void)resetType;
(void)powerDownTime;
g_mockECUResetHandlerCallCount++;
return kPositiveResponse;
}
static int g_mockSessionTimeoutHandlerCallCount = 0;
static void mockSessionTimeoutHandler() { g_mockSessionTimeoutHandlerCallCount++; }
// ================================================
// Iso14229Client Mock Functions
// ================================================
static enum Iso14229CANRxStatus mockClientCANRxPoll(uint32_t *arb_id, uint8_t *data, uint8_t *dlc) {
if (g.clientRecvQueueIdx > 0) {
struct CANMessage *msg = &g.clientRecvQueue[0];
*arb_id = msg->arbId;
memmove(data, msg->data, msg->size);
*dlc = msg->size;
ISO14229USERDEBUG("%06d c<0x%03x [%02d]: ", g.ms, msg->arbId, g.clientRecvQueueIdx);
PRINTHEX(msg->data, msg->size);
g.clientRecvQueueIdx--;
memmove(g.clientRecvQueue, &g.clientRecvQueue[1], sizeof(struct CANMessage));
return kCANRxSome;
}
return kCANRxNone;
}
// ================================================
// Fixtures
// ================================================
/**
* @brief Sends CAN messages from client to an in-memory FIFO queue
*/
int mockClientSendCAN(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size) {
assert(size <= 8);
assert(g.serverRecvQueueIdx < CAN_MESSAGE_QUEUE_SIZE);
struct CANMessage *msg = &g.serverRecvQueue[g.serverRecvQueueIdx++];
memmove(msg->data, data, size);
msg->arbId = arbitration_id;
msg->size = size;
ISO14229USERDEBUG("%06d " ANSI_BRIGHT_GREEN "c>"
"0x%03x [%02d]: ",
g.ms, arbitration_id, g.serverRecvQueueIdx);
PRINTHEX(data, size);
ISO14229USERDEBUG(ANSI_RESET);
return ISOTP_RET_OK;
}
/**
* @brief Sends CAN messages from server to an in-memory FIFO queue
*/
int mockServerCANTransmit(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size) {
assert(size <= 8);
assert(g.clientRecvQueueIdx < CAN_MESSAGE_QUEUE_SIZE);
struct CANMessage *msg = &g.clientRecvQueue[g.clientRecvQueueIdx++];
memmove(msg->data, data, size);
msg->arbId = arbitration_id;
msg->size = size;
ISO14229USERDEBUG("%06d " ANSI_BRIGHT_MAGENTA "s>0x%03x [%02d]: ", g.ms, arbitration_id,
g.clientRecvQueueIdx);
PRINTHEX(data, size);
ISO14229USERDEBUG(ANSI_RESET);
return ISOTP_RET_OK;
}
enum Iso14229CANRxStatus mockServerCANRxPoll(uint32_t *arbitration_id, uint8_t *data,
uint8_t *size) {
if (g.serverRecvQueueIdx > 0) {
struct CANMessage *msg = &g.serverRecvQueue[0];
*arbitration_id = msg->arbId;
memmove(data, msg->data, msg->size);
*size = msg->size;
// ISO14229USERDEBUG("%06d s<0x%03x [%02d]: ", g.ms, msg->arbId, g.serverRecvQueueIdx);
// PRINTHEX(msg->data, msg->size);
g.serverRecvQueueIdx--;
memmove(g.serverRecvQueue, &g.serverRecvQueue[1], sizeof(struct CANMessage));
return kCANRxSome;
}
return kCANRxNone;
}
void fixtureClientLinkProcess() {
uint32_t arb_id;
uint8_t data[8], dlc;
switch (mockClientCANRxPoll(&arb_id, data, &dlc)) {
case kCANRxSome:
isotp_on_can_message(&g.clientLink, data, dlc);
break;
case kCANRxNone:
break;
default:
assert(0);
}
isotp_poll(&g.clientLink);
}
void fixtureSrvLinksProcess() {
uint32_t arb_id;
uint8_t data[8], dlc;
switch (mockServerCANRxPoll(&arb_id, data, &dlc)) {
case kCANRxSome:
switch (arb_id) {
case SERVER_PHYS_RECV_ID:
isotp_on_can_message(&g.srvPhysLink, data, dlc);
break;
case SERVER_FUNC_RECV_ID:
isotp_on_can_message(&g.srvFuncLink, data, dlc);
break;
default:
assert(0);
}
break;
case kCANRxNone:
break;
default:
assert(0);
}
isotp_poll(&g.srvPhysLink);
isotp_poll(&g.srvFuncLink);
}
// ================================================
// Server tests
// ================================================
void testServerInit() {
TEST_SETUP();
Iso14229Server srv;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
Iso14229ServerInit(&srv, &cfg);
TEST_TEARDOWN();
}
void testServer0x10DiagnosticSessionControlIsDisabledByDefault() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t MOCK_DATA[] = {0x10, 0x02};
const uint8_t CORRECT_RESPONSE[] = {0x7f, 0x10, 0x11};
// sending a diagnostic session control request
isotp_send(&g.clientLink, MOCK_DATA, sizeof(MOCK_DATA));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < 2); // (in a reasonable time)
}
// that is negative, indicating that diagnostic session control is disabled by default
ASSERT_MEMORY_EQUAL(CORRECT_RESPONSE, g.scratch, sizeof(CORRECT_RESPONSE));
TEST_TEARDOWN();
}
// Special-case of ECU reset service
// ISO-14229-1 2013 9.3.1:
// on the behaviour of the ECU from the time following the positive response message to the ECU
// reset request: It is recommended that during this time the ECU does not accept any request
// messages and send any response messages.
void testServer0x11DoesNotSendOrReceiveMessagesAfterECUReset() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userECUResetHandler = mockECUResetHandler;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t MOCK_DATA[] = {0x11, 0x01};
const uint8_t EXPECTED_RESPONSE[] = {0x51, 0x01};
// Sending an ECU reset
isotp_send(&g.clientLink, MOCK_DATA, sizeof(MOCK_DATA));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < 2); // (in a reasonable time)
}
// that matches the expected response.
ASSERT_INT_EQUAL(g.size, sizeof(EXPECTED_RESPONSE));
ASSERT_MEMORY_EQUAL(EXPECTED_RESPONSE, g.scratch, sizeof(EXPECTED_RESPONSE));
// The ECU reset handler should have been called.
ASSERT_INT_EQUAL(g_mockECUResetHandlerCallCount, 1);
// Sending a second ECU reset
isotp_send(&g.clientLink, MOCK_DATA, sizeof(MOCK_DATA));
// should not receive a response until the server is reset
while (g.ms++ < 100) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
g.ret = isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size);
ASSERT_INT_EQUAL(g.ret, ISOTP_RET_NO_DATA);
}
TEST_TEARDOWN();
}
void testServer0x22RDBI1() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userRDBIHandler = mockUserRdbiHandler;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t MOCK_DATA[] = {0x22, 0xF1, 0x90};
const uint8_t CORRECT_RESPONSE[] = {0x62, 0xF1, 0x90, 0x57, 0x30, 0x4C, 0x30, 0x30, 0x30, 0x30,
0x34, 0x33, 0x4D, 0x42, 0x35, 0x34, 0x31, 0x33, 0x32, 0x36};
// sending an RDBI request
isotp_send(&g.clientLink, MOCK_DATA, sizeof(MOCK_DATA));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < 10); // (in a reasonable time)
}
// that matches the correct response
ASSERT_INT_EQUAL(sizeof(CORRECT_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(CORRECT_RESPONSE, g.scratch, sizeof(CORRECT_RESPONSE));
TEST_TEARDOWN();
}
enum Iso14229ResponseCode mockSecurityAccessGenerateSeed(const struct Iso14229ServerStatus *status,
uint8_t level, const uint8_t *in_data,
uint16_t in_size, uint8_t *out_data,
uint16_t out_bufsize, uint16_t *out_size) {
const uint8_t seed[] = {0x36, 0x57};
(void)status;
(void)level;
(void)in_data;
(void)in_size;
if (status->securityLevel == level) {
assert(out_bufsize >= 2);
out_data[0] = 0;
out_data[1] = 0;
*out_size = 2;
} else {
assert(out_bufsize >= sizeof(seed));
memmove(out_data, seed, sizeof(seed));
*out_size = sizeof(seed);
}
return kPositiveResponse;
}
enum Iso14229ResponseCode mockSecurityAccessValidateKey(const struct Iso14229ServerStatus *status,
uint8_t level, const uint8_t *key,
uint16_t size) {
(void)status;
(void)level;
(void)key;
(void)size;
return kPositiveResponse;
}
// ISO14229-1 2013 9.4.5.2
void testServer0x27SecurityAccess() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userSecurityAccessGenerateSeed = mockSecurityAccessGenerateSeed;
cfg.userSecurityAccessValidateKey = mockSecurityAccessValidateKey;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t SEED_REQUEST[] = {0x27, 0x01};
const uint8_t SEED_RESPONSE[] = {0x67, 0x01, 0x36, 0x57};
const uint8_t UNLOCK_REQUEST[] = {0x27, 0x02, 0xC9, 0xA9};
const uint8_t UNLOCK_RESPONSE[] = {0x67, 0x02};
// the server security level after initialization should be 0
ASSERT_INT_EQUAL(server.status.securityLevel, 0);
// sending a seed request
isotp_send(&g.clientLink, SEED_REQUEST, sizeof(SEED_REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < SERVER_DEFAULT_P2_MS); // (in a reasonable time)
}
// that matches the correct response
ASSERT_INT_EQUAL(sizeof(SEED_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(SEED_RESPONSE, g.scratch, sizeof(SEED_RESPONSE));
// subsequently sending an unlock request
isotp_send(&g.clientLink, UNLOCK_REQUEST, sizeof(UNLOCK_REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < 2 * SERVER_DEFAULT_P2_MS); // (in a reasonable time)
}
// that matches the correct response
ASSERT_INT_EQUAL(sizeof(UNLOCK_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(UNLOCK_RESPONSE, g.scratch, sizeof(UNLOCK_RESPONSE));
// Additionally, the security level should now be 1
ASSERT_INT_EQUAL(server.status.securityLevel, 1);
TEST_TEARDOWN();
}
// ISO14229-1 2013 9.4.5.3
void testServer0x27SecurityAccessAlreadyUnlocked() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userSecurityAccessGenerateSeed = mockSecurityAccessGenerateSeed;
cfg.userSecurityAccessValidateKey = mockSecurityAccessValidateKey;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t SEED_REQUEST[] = {0x27, 0x01};
const uint8_t ALREADY_UNLOCKED_RESPONSE[] = {0x67, 0x01, 0x00, 0x00};
// when the security level is already set to 1
server.status.securityLevel = 1;
// sending a seed request
isotp_send(&g.clientLink, SEED_REQUEST, sizeof(SEED_REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < SERVER_DEFAULT_P2_MS); // (in a reasonable time)
}
// that matches the correct response
ASSERT_INT_EQUAL(sizeof(ALREADY_UNLOCKED_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(ALREADY_UNLOCKED_RESPONSE, g.scratch, sizeof(ALREADY_UNLOCKED_RESPONSE));
TEST_TEARDOWN();
}
static enum Iso14229ResponseCode testServer0x31RCRRPMockRoutineControl(
const struct Iso14229ServerStatus *status, enum RoutineControlType routineControlType,
uint16_t routineIdentifier, Iso14229RoutineControlArgs *args) {
(void)status;
(void)routineControlType;
(void)routineIdentifier;
(void)args;
return g.userResponse;
}
// ISO-14229-1 2013 Table A.1 Byte Value 0x78: requestCorrectlyReceived-ResponsePending
// "This NRC is in general supported by each diagnostic service".
void testServer0x31RCRRP() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userRoutineControlHandler = testServer0x31RCRRPMockRoutineControl;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t REQUEST[] = {0x31, 0x01, 0x12, 0x34};
const uint8_t RCRRP[] = {0x7F, 0x31, 0x78}; // Request Correctly Received - Response Pending
const uint8_t POSITIVE_RESPONSE[] = {0x71, 0x01, 0x12, 0x34};
// When a user handler initially returns RRCRP
g.userResponse = kRequestCorrectlyReceived_ResponsePending;
// sending a request to the server
isotp_send(&g.clientLink, REQUEST, sizeof(REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < cfg.p2_ms); // in p2 ms
}
// a RequestCorrectlyReceived-ResponsePending response.
ASSERT_INT_EQUAL(sizeof(RCRRP), g.size);
ASSERT_MEMORY_EQUAL(RCRRP, g.scratch, sizeof(RCRRP));
// The server should again respond
g.t0 = g.ms;
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ - g.t0 < SERVER_DEFAULT_P2_STAR_MS); // in p2_star ms
}
// with another RequestCorrectlyReceived-ResponsePending response.
ASSERT_INT_EQUAL(sizeof(RCRRP), g.size);
ASSERT_MEMORY_EQUAL(RCRRP, g.scratch, sizeof(RCRRP));
// When the user handler now returns a positive response
g.userResponse = kPositiveResponse;
g.t0 = g.ms;
// the server should respond
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ - g.t0 < SERVER_DEFAULT_P2_MS); // in p2_ms
}
// with a positive response
ASSERT_INT_EQUAL(sizeof(POSITIVE_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(POSITIVE_RESPONSE, g.scratch, sizeof(POSITIVE_RESPONSE));
TEST_TEARDOWN();
}
void testServer0x34NotEnabled() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t REQUEST_DOWNLOAD_REQUEST[] = {0x34, 0x11, 0x33, 0x60, 0x20,
0x00, 0x00, 0xFF, 0xFF};
const uint8_t NEGATIVE_RESPONSE[] = {0x7F, 0x34, 0x11};
// when no requestDownloadHandler is installed,
// sending a request to the server
isotp_send(&g.clientLink, REQUEST_DOWNLOAD_REQUEST, sizeof(REQUEST_DOWNLOAD_REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < cfg.p2_ms); // in p2 ms
}
// a kServiceNotSupported response
ASSERT_INT_EQUAL(sizeof(NEGATIVE_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(NEGATIVE_RESPONSE, g.scratch, sizeof(NEGATIVE_RESPONSE));
TEST_TEARDOWN();
}
static enum Iso14229ResponseCode testServer0x34DownloadDataMockHandlerOnExit(
const struct Iso14229ServerStatus *status, void *userCtx, uint16_t buffer_size,
uint8_t *transferResponseParameterRecord, uint16_t *transferResponseParameterRecordSize) {
(void)status;
(void)userCtx;
(void)buffer_size;
(void)transferResponseParameterRecord;
(void)transferResponseParameterRecordSize;
return kPositiveResponse;
}
static enum Iso14229ResponseCode
testServer0x34DownloadDataMockHandlerOnTransfer(const struct Iso14229ServerStatus *status,
void *userCtx, const uint8_t *data, uint32_t len) {
(void)status;
(void)userCtx;
(void)data;
(void)len;
return kPositiveResponse;
}
static enum Iso14229ResponseCode testServer0x34DownloadDataMockuserRequestDownloadHandler(
const struct Iso14229ServerStatus *status, void *memoryAddress, size_t memorySize,
uint8_t dataFormatIdentifier, Iso14229DownloadHandler **handler,
uint16_t *maxNumberOfBlockLength) {
(void)status;
static Iso14229DownloadHandler mockHandler = {
.onExit = testServer0x34DownloadDataMockHandlerOnExit,
.onTransfer = testServer0x34DownloadDataMockHandlerOnTransfer,
.userCtx = NULL,
};
ASSERT_INT_EQUAL(0x11, dataFormatIdentifier);
ASSERT_PTR_EQUAL((void *)0x602000, memoryAddress);
ASSERT_INT_EQUAL(0x00FFFF, memorySize);
*handler = &mockHandler;
*maxNumberOfBlockLength = 0x0081;
return kPositiveResponse;
}
void testServer0x34DownloadData() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
// when a handler is installed that implements ISO14229-1:2013 Table 415
cfg.userRequestDownloadHandler = testServer0x34DownloadDataMockuserRequestDownloadHandler;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
const uint8_t REQUEST_DOWNLOAD_REQUEST[] = {0x34, 0x11, 0x33, 0x60, 0x20,
0x00, 0x00, 0xFF, 0xFF};
const uint8_t POSITIVE_RESPONSE[] = {0x74, 0x20, 0x00, 0x81};
// sending this request to the server
isotp_send(&g.clientLink, REQUEST_DOWNLOAD_REQUEST, sizeof(REQUEST_DOWNLOAD_REQUEST));
// should result in a response
while (ISOTP_RET_OK != isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size)) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
assert(g.ms++ < cfg.p2_ms); // in less than p2 ms
}
// a positive response matching ISO14229-1:2013 Table 415
ASSERT_INT_EQUAL(sizeof(POSITIVE_RESPONSE), g.size);
ASSERT_MEMORY_EQUAL(POSITIVE_RESPONSE, g.scratch, sizeof(POSITIVE_RESPONSE));
TEST_TEARDOWN();
}
// #define TEST_0x36_MOCK_DATA 0xF0, 0x00, 0xBA, 0xBA
// static enum Iso14229ResponseCode
// testServer0x36TransferDataMockHandlerOnTransfer(const struct Iso14229ServerStatus *status,
// void *userCtx, const uint8_t *data, uint32_t len)
// {
// (void)status;
// (void)userCtx;
// const uint8_t MOCK_DATA[] = {TEST_0x36_MOCK_DATA};
// ASSERT_INT_EQUAL(sizeof(MOCK_DATA), len);
// ASSERT_MEMORY_EQUAL(MOCK_DATA, data, len);
// return kPositiveResponse;
// }
// Iso14229DownloadHandler testServer0x36TransferDataMockHandler = {
// .onTransfer = testServer0x36TransferDataMockHandlerOnTransfer,
// };
// void testServer0x36TransferData() {
// Iso14229Server server;
// Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
// // when a handler is installed that implements ISO14229-1:2013 Table 415
// cfg.userRequestDownloadHandler = testServer0x34DownloadDataMockuserRequestDownloadHandler;
// Iso14229ServerInit(&server, &cfg);
// IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
// SERVER_TEST_SETUP();
// server.downloadHandler = &testServer0x36TransferDataMockHandler;
// Iso14229DownloadHandlerInit(server.downloadHandler, 0xFFFF);
// const uint8_t TRANSFER_DATA_REQUEST[] = {0x36, 0x01, TEST_0x36_MOCK_DATA};
// #undef TEST_0x36_MOCK_DATA
// const uint8_t TRANSFER_DATA_RESPONSE[] = {0x76, 0x01};
// SERVER_TEST_SEQUENCE_BEGIN();
// case 0:
// SERVER_TEST_CLIENT_SEND(TRANSFER_DATA_REQUEST);
// break;
// case 1:
// SERVER_TEST_AWAIT_RESPONSE(TRANSFER_DATA_RESPONSE);
// break;
// case 2:
// done = true;
// break;
// SERVER_TEST_SEQUENCE_END(server.cfg->p2_ms * 3);
// ASSERT_INT_EQUAL(server.downloadHandler->numBytesTransferred, 4);
// ASSERT_INT_EQUAL(server.downloadHandler->blockSequenceCounter, 2);
// TEST_TEARDOWN();
// }
/* ISO14229-1 2013 Table 72 */
void testServer0x3ESuppressPositiveResponse() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
// when the suppressPositiveResponse bit is set
const uint8_t REQUEST[] = {0x3E, 0x80};
// sending this request
isotp_send(&g.clientLink, REQUEST, sizeof(REQUEST));
// should result in no response.
while (g.ms++ < cfg.p2_ms) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
g.ret = isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size);
ASSERT_INT_EQUAL(ISOTP_RET_NO_DATA, g.ret);
ASSERT_INT_EQUAL(g.size, 0);
}
TEST_TEARDOWN();
}
void testServer0x83DiagnosticSessionControl() {
TEST_SETUP();
Iso14229Server server;
Iso14229ServerConfig cfg = DEFAULT_SERVER_CONFIG();
cfg.userDiagnosticSessionControlHandler = mockDiagnosticSessionControlHandler;
Iso14229ServerInit(&server, &cfg);
IsoTpInitLink(&g.clientLink, &CLIENT_LINK_DEFAULT_CONFIG);
// the server sessionType after initialization should be kDefaultSession.
ASSERT_INT_EQUAL(server.status.sessionType, kDefaultSession);
// When the suppressPositiveResponse bit is set
const uint8_t REQUEST[] = {0x10, 0x83};
// sending this request
isotp_send(&g.clientLink, REQUEST, sizeof(REQUEST));
// should result in no response.
while (g.ms++ < cfg.p2_ms) {
Iso14229ServerPoll(&server);
fixtureClientLinkProcess();
g.ret = isotp_receive(&g.clientLink, g.scratch, sizeof(g.scratch), &g.size);
ASSERT_INT_EQUAL(ISOTP_RET_NO_DATA, g.ret);
ASSERT_INT_EQUAL(g.size, 0);
}
// and the server sessionType should have changed
ASSERT_INT_EQUAL(server.status.sessionType, kExtendedDiagnostic);
TEST_TEARDOWN();
}
// ================================================
// Client tests
// ================================================
void testClientInit() {
TEST_SETUP();
Iso14229Client client;
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
TEST_TEARDOWN();
}
void testClientP2TimeoutExceeded() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// Sending an ECU reset
ECUReset(&client, kHardReset);
// and not receiving a response after approximately p2 ms
while (g.ms++ < cfg.p2_ms + 3) {
Iso14229ClientPoll(&client);
}
// should result in kRequestTimedOut
ASSERT_INT_EQUAL(kRequestTimedOut, client.err);
ASSERT_INT_EQUAL(kRequestStateIdle, client.state);
TEST_TEARDOWN();
}
void testClientP2TimeoutNotExceeded() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// a client that sends an ECU reset
ECUReset(&client, kHardReset);
// which receives a positive response
const uint8_t POSITIVE_RESPONSE[] = {0x51, 0x01};
isotp_send(&g.srvPhysLink, POSITIVE_RESPONSE, sizeof(POSITIVE_RESPONSE));
// should return to the idle state
while (kRequestStateIdle != client.state) {
Iso14229ClientPoll(&client);
assert(g.ms++ < cfg.p2_ms); // before p2 ms has elapsed
}
// and should have no error.
ASSERT_INT_EQUAL(kRequestNoError, client.err);
TEST_TEARDOWN();
}
void testClientSuppressPositiveResponse() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// Setting the suppressPositiveResponse flag before sending a request
client.suppressPositiveResponse = true;
ECUReset(&client, kHardReset);
// and not receiving a response after approximately p2 ms
while (g.ms++ < cfg.p2_ms + 3) {
Iso14229ClientPoll(&client);
}
// should not result in an error.
ASSERT_INT_EQUAL(kRequestNoError, client.err);
ASSERT_INT_EQUAL(kRequestStateIdle, client.state);
TEST_TEARDOWN();
}
void testClientBusy() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// Sending a request should not return an error
ASSERT_INT_EQUAL(kRequestNoError, ECUReset(&client, kHardReset));
// unless there is an existing unresolved request
ASSERT_INT_EQUAL(kRequestNotSentBusy, ECUReset(&client, kHardReset));
TEST_TEARDOWN();
}
void testClientUnexpectedResponse() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// sending an ECU reset
ECUReset(&client, kHardReset);
// The correct response SID to EcuReset (0x11) is 0x51.
const uint8_t WEIRD_RESPONSE[] = {0x50, 0x01}; // incorrect, unexpected
isotp_send(&g.srvPhysLink, WEIRD_RESPONSE, sizeof(WEIRD_RESPONSE));
// should return to the idle state
while (kRequestStateIdle != client.state) {
Iso14229ClientPoll(&client);
assert(g.ms++ < cfg.p2_ms); // before p2 ms has elapsed
}
// with a kRequestErrorResponseSIDMismatch error.
ASSERT_INT_EQUAL(kRequestErrorResponseSIDMismatch, client.err);
TEST_TEARDOWN();
}
void testClient0x11ECUReset() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// sending an ECUReset of type kHardReset
ECUReset(&client, kHardReset);
// should send these bytes to the ISO-TP layer
const uint8_t HARD_RESET_REQUEST[] = {0x11, 0x01};
ASSERT_MEMORY_EQUAL(g.clientLinkTxBuf, HARD_RESET_REQUEST, sizeof(HARD_RESET_REQUEST));
ASSERT_INT_EQUAL(sizeof(HARD_RESET_REQUEST), g.clientLink.send_size);
TEST_TEARDOWN();
}
void testClient0x11ECUResetNegativeResponse() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// A client that sends an ECU reset
ECUReset(&client, kHardReset);
// and receives a negative response
const uint8_t NEG_RESPONSE[] = {0x7F, 0x11, 0x10};
isotp_send(&g.srvPhysLink, NEG_RESPONSE, sizeof(NEG_RESPONSE));
// should return to the idle state
while (kRequestStateIdle != client.state) {
Iso14229ClientPoll(&client);
assert(g.ms++ < cfg.p2_ms); // before p2 ms has elapsed
}
// with a kRequestErrorNegativeResponse error
ASSERT_INT_EQUAL(kRequestErrorNegativeResponse, client.err);
TEST_TEARDOWN();
}
void testClient0x11ECUResetNegativeResponseNoError() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// A client that sets the negativeResponseIsError flag to false
client.negativeResponseIsError = false;
// before sending an ECU reset
ECUReset(&client, kHardReset);
// and receiving a negative response
const uint8_t NEG_RESPONSE[] = {0x7F, 0x11, 0x10};
isotp_send(&g.srvPhysLink, NEG_RESPONSE, sizeof(NEG_RESPONSE));
// should return to the idle state
while (kRequestStateIdle != client.state) {
Iso14229ClientPoll(&client);
assert(g.ms++ < cfg.p2_ms); // before p2 ms has elapsed
}
// with no error
ASSERT_INT_EQUAL(kRequestNoError, client.err);
TEST_TEARDOWN();
}
void testClient0x22RDBITxBufferTooSmall() {
TEST_SETUP();
Iso14229Client client;
IsoTpInitLink(&g.srvPhysLink, &SRV_PHYS_LINK_DEFAULT_CONFIG);
struct Iso14229ClientConfig cfg = DEFAULT_CLIENT_CONFIG();
iso14229ClientInit(&client, &cfg);
// attempting to send a request payload of 6 bytes
uint16_t didList[] = {0x0001, 0x0002, 0x0003};
// which is larger than the underlying buffer
client.link->send_buf_size = 4;
// should return an error
ASSERT_INT_EQUAL(kRequestNotSentInvalidArgs,
ReadDataByIdentifier(&client, didList, ARRAY_SZ(didList)))
// and no data should be sent
ASSERT_INT_EQUAL(client.link->send_size, 0);
TEST_TEARDOWN();
}
void testClient0x22RDBIUnpackResponse() {
TEST_SETUP();
uint8_t RESPONSE[] = {0x72, 0x12, 0x34, 0x00, 0x00, 0xAA, 0x00, 0x56, 0x78, 0xAA, 0xBB};
struct Iso14229Response resp = {
.buf = RESPONSE, .buffer_size = sizeof(RESPONSE), .len = sizeof(RESPONSE)};
uint8_t buf[4];
uint16_t offset = 0;
int err = 0;
err = RDBIReadDID(&resp, 0x1234, buf, 4, &offset);