Skip to content

Commit 9b08742

Browse files
committed
Merge branch 'mctp-serial-tx-escapes'
Matt Johnston says: ==================== net: mctp-serial: Fix for missing tx escapes The mctp-serial code to add escape characters was incorrect due to an off-by-one error. This series adds a test for the chunking which splits by escape characters, and fixes the bug. v2: Fix kunit param const pointer ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents fe1910f + f962e83 commit 9b08742

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

drivers/net/mctp/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ config MCTP_SERIAL
2121
Say y here if you need to connect to MCTP endpoints over serial. To
2222
compile as a module, use m; the module will be called mctp-serial.
2323

24+
config MCTP_SERIAL_TEST
25+
bool "MCTP serial tests" if !KUNIT_ALL_TESTS
26+
depends on MCTP_SERIAL=y && KUNIT=y
27+
default KUNIT_ALL_TESTS
28+
2429
config MCTP_TRANSPORT_I2C
2530
tristate "MCTP SMBus/I2C transport"
2631
# i2c-mux is optional, but we must build as a module if i2c-mux is a module

drivers/net/mctp/mctp-serial.c

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ static int next_chunk_len(struct mctp_serial *dev)
9191
* will be those non-escaped bytes, and does not include the escaped
9292
* byte.
9393
*/
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]))
9696
break;
9797
}
9898

@@ -521,3 +521,112 @@ module_exit(mctp_serial_exit);
521521
MODULE_LICENSE("GPL v2");
522522
MODULE_AUTHOR("Jeremy Kerr <[email protected]>");
523523
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

Comments
 (0)