@@ -91,8 +91,8 @@ static int next_chunk_len(struct mctp_serial *dev)
91
91
* will be those non-escaped bytes, and does not include the escaped
92
92
* byte.
93
93
*/
94
- for (i = 1 ; i + dev -> txpos + 1 < dev -> txlen ; i ++ ) {
95
- if (needs_escape (dev -> txbuf [dev -> txpos + i + 1 ]))
94
+ for (i = 1 ; i + dev -> txpos < dev -> txlen ; i ++ ) {
95
+ if (needs_escape (dev -> txbuf [dev -> txpos + i ]))
96
96
break ;
97
97
}
98
98
@@ -521,3 +521,112 @@ module_exit(mctp_serial_exit);
521
521
MODULE_LICENSE ("GPL v2" );
522
522
MODULE_AUTHOR (
"Jeremy Kerr <[email protected] >" );
523
523
MODULE_DESCRIPTION ("MCTP Serial transport" );
524
+
525
+ #if IS_ENABLED (CONFIG_MCTP_SERIAL_TEST )
526
+ #include <kunit/test.h>
527
+
528
+ #define MAX_CHUNKS 6
529
+ struct test_chunk_tx {
530
+ u8 input_len ;
531
+ u8 input [MCTP_SERIAL_MTU ];
532
+ u8 chunks [MAX_CHUNKS ];
533
+ };
534
+
535
+ static void test_next_chunk_len (struct kunit * test )
536
+ {
537
+ struct mctp_serial devx ;
538
+ struct mctp_serial * dev = & devx ;
539
+ int next ;
540
+
541
+ const struct test_chunk_tx * params = test -> param_value ;
542
+
543
+ memset (dev , 0x0 , sizeof (* dev ));
544
+ memcpy (dev -> txbuf , params -> input , params -> input_len );
545
+ dev -> txlen = params -> input_len ;
546
+
547
+ for (size_t i = 0 ; i < MAX_CHUNKS ; i ++ ) {
548
+ next = next_chunk_len (dev );
549
+ dev -> txpos += next ;
550
+ KUNIT_EXPECT_EQ (test , next , params -> chunks [i ]);
551
+
552
+ if (next == 0 ) {
553
+ KUNIT_EXPECT_EQ (test , dev -> txpos , dev -> txlen );
554
+ return ;
555
+ }
556
+ }
557
+
558
+ KUNIT_FAIL_AND_ABORT (test , "Ran out of chunks" );
559
+ }
560
+
561
+ static struct test_chunk_tx chunk_tx_tests [] = {
562
+ {
563
+ .input_len = 5 ,
564
+ .input = { 0x00 , 0x11 , 0x22 , 0x7e , 0x80 },
565
+ .chunks = { 3 , 1 , 1 , 0 },
566
+ },
567
+ {
568
+ .input_len = 5 ,
569
+ .input = { 0x00 , 0x11 , 0x22 , 0x7e , 0x7d },
570
+ .chunks = { 3 , 1 , 1 , 0 },
571
+ },
572
+ {
573
+ .input_len = 3 ,
574
+ .input = { 0x7e , 0x11 , 0x22 , },
575
+ .chunks = { 1 , 2 , 0 },
576
+ },
577
+ {
578
+ .input_len = 3 ,
579
+ .input = { 0x7e , 0x7e , 0x7d , },
580
+ .chunks = { 1 , 1 , 1 , 0 },
581
+ },
582
+ {
583
+ .input_len = 4 ,
584
+ .input = { 0x7e , 0x7e , 0x00 , 0x7d , },
585
+ .chunks = { 1 , 1 , 1 , 1 , 0 },
586
+ },
587
+ {
588
+ .input_len = 6 ,
589
+ .input = { 0x7e , 0x7e , 0x00 , 0x7d , 0x10 , 0x10 },
590
+ .chunks = { 1 , 1 , 1 , 1 , 2 , 0 },
591
+ },
592
+ {
593
+ .input_len = 1 ,
594
+ .input = { 0x7e },
595
+ .chunks = { 1 , 0 },
596
+ },
597
+ {
598
+ .input_len = 1 ,
599
+ .input = { 0x80 },
600
+ .chunks = { 1 , 0 },
601
+ },
602
+ {
603
+ .input_len = 3 ,
604
+ .input = { 0x80 , 0x80 , 0x00 },
605
+ .chunks = { 3 , 0 },
606
+ },
607
+ {
608
+ .input_len = 7 ,
609
+ .input = { 0x01 , 0x00 , 0x08 , 0xc8 , 0x00 , 0x80 , 0x02 },
610
+ .chunks = { 7 , 0 },
611
+ },
612
+ {
613
+ .input_len = 7 ,
614
+ .input = { 0x01 , 0x00 , 0x08 , 0xc8 , 0x7e , 0x80 , 0x02 },
615
+ .chunks = { 4 , 1 , 2 , 0 },
616
+ },
617
+ };
618
+
619
+ KUNIT_ARRAY_PARAM (chunk_tx , chunk_tx_tests , NULL );
620
+
621
+ static struct kunit_case mctp_serial_test_cases [] = {
622
+ KUNIT_CASE_PARAM (test_next_chunk_len , chunk_tx_gen_params ),
623
+ };
624
+
625
+ static struct kunit_suite mctp_serial_test_suite = {
626
+ .name = "mctp_serial" ,
627
+ .test_cases = mctp_serial_test_cases ,
628
+ };
629
+
630
+ kunit_test_suite (mctp_serial_test_suite );
631
+
632
+ #endif /* CONFIG_MCTP_SERIAL_TEST */
0 commit comments