apdu-dispatch 0.4.0

Dispatch layer after nfc-device and usbd-ccid
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
use apdu_dispatch::app::{App, CommandView, Result as AppResult};
use apdu_dispatch::dispatch;
use apdu_dispatch::{interchanges, response};
use heapless::VecView;
use hex_literal::hex;
use interchange::Channel;
use iso7816::Status;

#[macro_use]
extern crate serial_test;

#[macro_use]
extern crate delog;
generate_macros!();

#[derive(Debug)]
pub struct StdoutFlusher {}
impl delog::Flusher for StdoutFlusher {
    fn flush(&self, logs: &str) {
        print!("{logs}");
    }
}

delog!(Delogger, 25 * 1024, 25 * 1024, StdoutFlusher);

static STDOUT_FLUSHER: StdoutFlusher = StdoutFlusher {};

#[allow(dead_code)]
enum TestInstruction {
    Echo = 0x10,
    Add = 0x11,
    GetData = 0x12,
}

fn dump_hex(data: &[u8]) {
    for b in data {
        print!("{b:02X} ");
    }
    println!();
}

pub struct TestApp1 {}

impl iso7816::App for TestApp1 {
    fn aid(&self) -> iso7816::Aid {
        iso7816::Aid::new(&hex!("0A01000001"))
    }
}

// This app echos to Ins code 0x10
impl App for TestApp1 {
    fn select(
        &mut self,
        _interface: dispatch::Interface,
        _apdu: CommandView<'_>,
        _reply: &mut VecView<u8>,
    ) -> AppResult {
        Ok(())
    }

    fn deselect(&mut self) {}

    fn call(
        &mut self,
        _: dispatch::Interface,
        apdu: CommandView<'_>,
        reply: &mut VecView<u8>,
    ) -> AppResult {
        println!("TestApp1::call");
        match apdu.instruction().into() {
            0x10 => {
                // Just echo 5x 0's for the request apdu header
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.extend_from_slice(apdu.data()).unwrap();
                Ok(())
            }
            // For measuring the stack burden of dispatch
            0x15 => {
                let buf = heapless::Vec::<u8, { response::SIZE }>::new();
                let addr = (&buf as *const VecView<u8>).addr() as u32;
                reply.extend_from_slice(&addr.to_be_bytes()).unwrap();
                Ok(())
            }

            // Testing a response larger than the interchange's size
            0x21 => {
                reply
                    .extend_from_slice(&[10; interchanges::SIZE + 1])
                    .unwrap();
                Ok(())
            }
            0x22 => {
                reply
                    .extend_from_slice(&[10; interchanges::SIZE - 2])
                    .unwrap();
                Ok(())
            }
            0x23 => {
                reply
                    .extend_from_slice(&[10; interchanges::SIZE - 1])
                    .unwrap();
                Ok(())
            }
            0x24 => {
                reply.extend_from_slice(&[10; interchanges::SIZE]).unwrap();
                Ok(())
            }
            _ => Err(Status::InstructionNotSupportedOrInvalid),
        }
    }
}

pub struct TestApp2 {}

impl iso7816::App for TestApp2 {
    fn aid(&self) -> iso7816::Aid {
        iso7816::Aid::new(&hex!("0A01000002"))
    }
}

// This app echos to Ins code 0x20
impl App for TestApp2 {
    fn select(
        &mut self,
        _interface: dispatch::Interface,
        _apdu: CommandView<'_>,
        _reply: &mut VecView<u8>,
    ) -> AppResult {
        Ok(())
    }

    fn deselect(&mut self) {}

    fn call(
        &mut self,
        _: dispatch::Interface,
        apdu: CommandView<'_>,
        reply: &mut VecView<u8>,
    ) -> AppResult {
        println!("TestApp2::call");
        match apdu.instruction().into() {
            0x20 => {
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.push(0).unwrap();
                reply.extend_from_slice(apdu.data()).unwrap();
                Ok(())
            }
            0x30 => {
                // Return 2KB bytes of byte-truncated fibonacci
                reply.extend_from_slice(&[0, 1, 1]).unwrap();
                for i in 3..2048 {
                    let next = ((reply[i - 1] as u32 + reply[i - 2] as u32) & 0xff) as u8;
                    reply.push(next).unwrap();
                }
                Ok(())
            }
            _ => Err(Status::InstructionNotSupportedOrInvalid),
        }
    }
}

pub struct PanicApp {}

impl iso7816::App for PanicApp {
    fn aid(&self) -> iso7816::Aid {
        iso7816::Aid::new(&hex!("0A01000003"))
    }
}

// This app echos to Ins code 0x20
impl App for PanicApp {
    fn select(
        &mut self,
        _interface: dispatch::Interface,
        _apdu: CommandView<'_>,
        _reply: &mut VecView<u8>,
    ) -> AppResult {
        panic!("Dont call the panic app");
    }

    fn deselect(&mut self) {
        panic!("Dont call the panic app");
    }

    fn call(
        &mut self,
        _: dispatch::Interface,
        _apdu: CommandView<'_>,
        _reply: &mut VecView<u8>,
    ) -> AppResult {
        panic!("Dont call the panic app");
    }
}

fn run_apdus(apdu_response_pairs: &[&[u8]]) {
    assert!(!apdu_response_pairs.is_empty());
    assert!((apdu_response_pairs.len() & 1) == 0);

    Delogger::init_default(delog::LevelFilter::Info, &STDOUT_FLUSHER).ok();

    let contact = Channel::new();
    let (mut contact_requester, contact_responder) = contact
        .split()
        .expect("could not setup ccid ApduInterchange");

    let contactless = Channel::new();
    let (_contactless_requester, contactless_responder) = contactless
        .split()
        .expect("could not setup iso14443 ApduInterchange");

    let mut apdu_dispatch =
        apdu_dispatch::dispatch::ApduDispatch::new(contact_responder, contactless_responder);
    Delogger::flush();

    let mut app0 = PanicApp {};
    let mut app1 = TestApp1 {};
    let mut app2 = PanicApp {};
    let mut app3 = TestApp2 {};
    let mut app4 = PanicApp {};

    // for i in 0..apdu_response_pairs.len() {
    // print!("- ");
    // dump_hex(apdu_response_pairs[i]);
    // }
    for i in (0..apdu_response_pairs.len()).step_by(2) {
        let raw_req = apdu_response_pairs[i];
        let raw_expected_res = apdu_response_pairs[i + 1];

        // let command = Command::try_from(raw_req).unwrap();
        // let expected_response = Response::Data::from_slice(&raw_res);

        print!("<< ");
        dump_hex(raw_req);

        contact_requester
            .request(interchanges::Data::from_slice(raw_req).unwrap())
            .expect("could not deposit command");

        apdu_dispatch.poll(&mut [&mut app0, &mut app1, &mut app2, &mut app3, &mut app4]);
        Delogger::flush();

        let response = contact_requester.take_response().unwrap();

        print!(">> ");
        dump_hex(&response);

        if raw_expected_res != response.as_slice() {
            print!("expected: ");
            dump_hex(raw_expected_res);
            print!("got: ");
            dump_hex(&response);
            panic!("Expected responses do not match");
        }
    }
}

#[test]
#[serial]
fn malformed_apdus() {
    run_apdus(&[
        // Too short
        &hex!("00"),
        &hex!("6F00"),
        // Too short
        &hex!("0000"),
        &hex!("6F00"),
        // Too short
        &hex!("000000"),
        &hex!("6F00"),
        // Wrong length
        &hex!("0000000010010101"),
        &hex!("6F00"),
        // Extra data
        &hex!("000000000501010101010101010101010101"),
        &hex!("6F00"),
        // Invalid CLA
        &hex!("FF000000"),
        &hex!("6F00"),
        // Invalid extended length
        &hex!("00000000ff00050101010101"),
        &hex!("6F00"),
        // sanity check with Valid APDU with extended length
        &hex!("000000000000050101010101"),
        &hex!("6A82"),
    ])
}

#[test]
#[serial]
fn select_1() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000001"),
        // Ok
        &hex!("9000"),
    ])
}

#[test]
#[serial]
fn select_2() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000002"),
        // Ok
        &hex!("9000"),
    ])
}

#[test]
#[serial]
fn select_not_found() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000100"),
        // Not found
        &hex!("6A82"),
    ])
}

#[test]
#[serial]
fn select_not_found_still_selected() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000001"),
        // Ok
        &hex!("9000"),
        // Select
        &hex!("00A40400 05 0A01000100"),
        // Not found
        &hex!("6A82"),
        // Echo app is still selected
        &hex!("80100000 05 0102030405 00"),
        // Echo + Ok
        &hex!("0000000000 0102030405 9000"),
    ])
}

#[test]
#[serial]
fn select_bad_aid() {
    // Select with an incorrect AID shouldn't crash the application
    run_apdus(&[
        // Select 1
        &hex!("00A40400"),
        &hex!("6A80"),
    ])
}

#[test]
#[serial]
fn echo_1() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000001"),
        // Ok
        &hex!("9000"),
        // Echo
        &hex!("00100000 05 0102030405 00"),
        // Echo + Ok
        &hex!("0000000000 01020304059000"),
    ])
}

#[test]
#[serial]
fn echo_with_cla_bits_set() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000001"),
        // Ok
        &hex!("9000"),
        // Echo
        &hex!("80100000 05 0102030405 00"),
        // Echo + Ok
        &hex!("0000000000 0102030405 9000"),
    ])
}

#[test]
#[serial]
fn echo_wrong_instruction() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000001"),
        // Ok
        &hex!("9000"),
        // Echo
        &hex!("00200000 05 0102030405 00"),
        // Wrong Ins
        &hex!("6d00"),
    ])
}

#[test]
#[serial]
fn echo_2() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000002"),
        // Ok
        &hex!("9000"),
        // Echo
        &hex!("00200000 05 0102030405 00"),
        // Echo + Ok
        &hex!("0000000000 0102030405 9000"),
    ])
}

#[test]
#[serial]
fn echo_wrong_instruction_2() {
    run_apdus(&[
        // Select
        &hex!("00A40400 05 0A01000002"),
        // Ok
        &hex!("9000"),
        // Echo
        &hex!("00100000 05 0102030405 00"),
        // Wrong Ins
        &hex!("6d00"),
    ])
}

#[test]
#[serial]
fn unsolicited_instruction() {
    run_apdus(&[
        // Echo
        &hex!("00100000 05 0102030405"),
        // Not found
        &hex!("6a82"),
    ])
}

#[test]
#[serial]
fn deselect() {
    run_apdus(&[
        // Select 1
        &hex!("00A40400 05 0A01000001"),
        &hex!("9000"),
        // Echo 1
        &hex!("00100000 05 0102030405 00"),
        &hex!("0000000000 0102030405 9000"),
        // Select 2
        &hex!("00A40400 05 0A01000002"),
        &hex!("9000"),
        // Echo 1
        &hex!("00100000 05 0102030405 00"),
        &hex!("6d00"),
    ])
}

#[test]
#[serial]
fn extended_length_echo() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400 05 0A01000001"),
            &hex!("9000"),

            // To be echo'd
            &hex!("00100000 000123
                /*        1                    8                       16                      24                      32 */
                /* 1  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 2  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 3  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 4  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 5  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 6  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 7  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 8  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 9  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 10 */  01 01 01
                0000
            "),
            // echo  Success
            &hex!("0000000000
                /*        1                    8                       16                      24                      32 */
                /* 1  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 2  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 3  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 4  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 5  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 6  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 7  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 8  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 9  */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                /* 10 */  01 01 01
                9000
            ")
        ]
    )
}

#[test]
#[serial]
fn chained_apdu_1() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400 05 0A01000001"),
            &hex!("9000"),

            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),

            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),

            // Send last command
            &hex!("0010000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                00
            "),
            // Expect 0xff + 0xff + 0x20 + 5 == 547 bytes back
            // Echo chunk + remaining
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6100
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + remaining
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6123
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + success
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01
                9000
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Error
            &hex!("6F00"),
        ]
    )
}

#[test]
#[serial]
fn chained_response() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400 05 0A01000001"),
            &hex!("9000"),

            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),

            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),

            // Send last command
            &hex!("0010000040
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                23 // Ask for less than 256
            "),
            // Echo chunk + remaining
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01
                6100
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + remaining
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6100
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + success
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6120
            "),

            // Get Response
            &hex!("00C00000 00"),
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                9000
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Error
            &hex!("6F00"),
        ]
    )
}

#[test]
#[serial]
fn multiple_chained_apdu_1() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400 05 0A01000001"),
            &hex!("9000"),

            // Set chaining bit
            &hex!(" 10200000ff
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),


            // Send last command
            &hex!(" 0010000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                00
            "),
            // Expect 0xff + 0xff + 0x20 + 5 == 292 bytes back
            // Data + remaining bytes
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6124
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + success
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01
                9000
            "),

            // Check short commands still work
            // Echo 1
            &hex!("00100000 05 0102030405 00"),
            &hex!("0000000000 01020304059000"),

            // Echo 2
            &hex!("00200000 05 0102030405 00"),
            &hex!("6d00"),

            // Check chaining command still works
            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),


            // Send last command
            &hex!("0010000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                00
            "),
            // Expect 0xff + 0xff + 0x20 + 5 == 292 bytes back
            // Data + remaining bytes
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
                6124
            "),

            // Get Response
            &hex!("00C00000 00"),
            // Echo chunk + success
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01
                9000
            "),
        ]
    )
}

#[test]
#[serial]
fn test_chained_fibonacci_response() {
    let mut expected = response::Data::new();
    expected.extend_from_slice(&[0, 1, 1]).unwrap();
    for i in 3..2048 {
        let next = ((expected[i - 1] as u32 + expected[i - 2] as u32) & 0xff) as u8;
        expected.push(next).unwrap();
    }
    // expected_reply.extend_from_slice(&[0x90, 0x00]).unwrap();
    fn apdu_res_chunk(data: &response::Data, start: &mut usize, size: usize) -> response::Data {
        let mut chunk = response::Data::new();
        let end = *start + size;
        chunk.extend_from_slice(&data[*start..end]).unwrap();
        if data[*start..].len() > 256 {
            chunk.push(0x61).unwrap();

            if data[end..].len() > 255 {
                chunk.push(0).unwrap();
            } else {
                chunk.push(data[end..].len() as u8).unwrap();
            }
        } else {
            chunk.push(0x90).unwrap();
            chunk.push(0x00).unwrap();
        }
        *start += size;
        chunk
    }

    let mut start = 0;
    let mut start2 = 0;

    run_apdus(
        &[
            // Select 2
            &hex!("00A40400 05 0A01000002"),
            &hex!("9000"),

            // Set chaining bit, command to get long fibonacci back
            &hex!("10300000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),

            // Send last command
            &hex!("0030000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                00
            "),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),
            &hex!("00C00000 00"),

            apdu_res_chunk(&expected, &mut start, 256).as_slice(),
            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),
            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),
            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start, 256).as_slice(),

            // chaining bit, command to get long fibonacci back
            &hex!("10300000 05 0102030405"),
            &hex!("9000 "),

            &hex!("00300000 05 0102030405 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

            &hex!("00C00000 00"),
            apdu_res_chunk(&expected, &mut start2, 256).as_slice(),

        ]
    )
}

#[test]
#[serial]
fn multiple_chained_apdu_interruption() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400050A01000001"),
            &hex!("9000"),

            // Set chaining bit
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),


            // Send last command
            &hex!("0010000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                00
            "),
            // Expect 0xff + 0xff + 0x20 + 5 == 292 bytes back
            // Data + remaining bytes
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                6124
            "),
            // Just ignore those 36 bytes and do something different
            // Echo 1
            &hex!("00100000 05 0102030405 00"),
            &hex!("0000000000 0102030405 9000"),

            // GetResponse no longer works
            &hex!("00C00000"),
            &hex!("6F00 "),

            // Check that new chaining transaction works
            &hex!("10200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),


            // Send last command
            &hex!("0010000020
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                00
            "),
            // Expect 0xff + 0xff + 0x20 + 5 == 292 bytes back
            // Data + remaining bytes
            &hex!("
                /*       1                    8                       16                      24                      32 */
                /* 1 */  00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                6124
            "),

        ]
    )
}

#[test]
#[serial]
fn chaining_with_unknown_class_range() {
    run_apdus(
        &[
            // Select 1
            &hex!("00A40400 05 0A01000001"),
            &hex!("9000"),

            // Set chaining bit + upper range bit
            &hex!("90200000FF
                /*       1                    8                       16                      24                      32 */
                /* 1 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 2 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 3 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 4 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 5 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 6 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 7 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
                /* 8 */  01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
            "),
            &hex!("9000"),
        ]
    )
}

#[test]
#[serial]
fn send_select_preceded_with_zero_chained_data() {
    // Sending a select after chaining 0 bytes should result in successful select operation
    run_apdus(&[
        // Chaining zero data
        &hex!("9060000000"),
        &hex!("9000"),
        // Select 1
        &hex!("00A40400 05 0A01000001"),
        &hex!("9000"),
    ])
}

#[test]
#[serial]
fn response_larger_than_interchange() {
    // Sending a select after chaining 0 bytes should result in successful select operation
    let mut response1 = vec![0x0A; interchanges::SIZE - 2];
    response1.extend_from_slice(&hex!("6103"));
    let mut response2 = vec![0x0A; interchanges::SIZE - 2];
    response2.extend_from_slice(&hex!("9000"));
    let mut response3 = vec![0x0A; interchanges::SIZE - 2];
    response3.extend_from_slice(&hex!("6101"));
    let mut response4 = vec![0x0A; interchanges::SIZE - 2];
    response4.extend_from_slice(&hex!("6102"));
    run_apdus(&[
        // Select 1
        &hex!("00A40400 05 0A01000001"),
        &hex!("9000"),
        &hex!("00210000  00ffff"),
        &response1,
        // Get Response
        &hex!("00C00000 00"),
        &hex!("0A0A0A 9000"),
        &hex!("00220000  00ffff"),
        &response2,
        &hex!("00230000  00ffff"),
        &response3,
        // Get Response
        &hex!("00C00000 00"),
        &hex!("0A 9000"),
        &hex!("00240000  00ffff"),
        &response4,
        // Get Response
        &hex!("00C00000 00"),
        &hex!("0A0A 9000"),
    ])
}

#[test]
#[serial]
fn check_stack_burden() {
    let contact = Channel::new();
    let (mut contact_requester, contact_responder) = contact
        .split()
        .expect("could not setup ccid ApduInterchange");

    let contactless = Channel::new();
    let (_contactless_requester, contactless_responder) = contactless
        .split()
        .expect("could not setup iso14443 ApduInterchange");

    let mut apdu_dispatch =
        apdu_dispatch::dispatch::ApduDispatch::new(contact_responder, contactless_responder);

    let mut app1 = TestApp1 {};

    contact_requester
        .request(interchanges::Data::from_slice(&hex!("00A40400050A01000001")).unwrap())
        .expect("could not deposit command");

    apdu_dispatch.poll(&mut [&mut app1]);

    let response = contact_requester.take_response().unwrap();

    print!(">> ");
    dump_hex(&response);

    contact_requester
        .request(interchanges::Data::from_slice(&hex!("0015000000")).unwrap())
        .expect("could not deposit command");

    apdu_dispatch.poll(&mut [&mut app1]);

    let response = contact_requester.take_response().unwrap();

    print!(">> ");
    dump_hex(&response);

    let payload: [u8; 4] = [response[0], response[1], response[2], response[3]];
    let min_stack = u32::from_be_bytes(payload);
    let max_stack = (&response as *const interchanges::Data) as u32;

    // Last checked:
    // Burden: 43744 bytes with Large apdu for Command & Response, not returning Data.
    println!("Burden: {} bytes", max_stack - min_stack);

    // Uncomment to see stack burden printed out
    // assert!(false);
}