From ea346125e8ef0c14e1566070380ec777d4a1a56a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:28:42 +0200 Subject: [PATCH] Add some validation for invalid router destinations --- serial_link/protocol/frame_router.c | 16 ++++++++---- serial_link/tests/frame_router_tests.c | 36 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index c61c50ebd6..890ebbe9ea 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -33,7 +33,9 @@ void router_set_master(bool master) { void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ if (is_master) { - transport_recv_frame(data[size-1], data, size - 1); + if (link == DOWN_LINK) { + transport_recv_frame(data[size-1], data, size - 1); + } } else { if (link == UP_LINK) { @@ -52,11 +54,15 @@ void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { if (destination == 0) { - data[size] = 1; - validator_send_frame(UP_LINK, data, size + 1); + if (!is_master) { + data[size] = 1; + validator_send_frame(UP_LINK, data, size + 1); + } } else { - data[size] = destination; - validator_send_frame(DOWN_LINK, data, size + 1); + if (is_master) { + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); + } } } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 6b00aca677..0b0ea6e7f4 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -174,7 +174,6 @@ Ensure(FrameRouter, first_link_sends_to_master) { } Ensure(FrameRouter, second_link_sends_to_master) { - printf("Second to master start\n"); frame_buffer_t data; data.data = 0xAB7055BB; activate_router(2); @@ -195,3 +194,38 @@ Ensure(FrameRouter, second_link_sends_to_master) { assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, master_sends_to_master_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(0); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, link_sends_to_other_link_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(2, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, master_receives_on_uplink_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + never_expect(transport_recv_frame); + activate_router(0); + receive_data(UP_LINK, + router_buffers[1].send_buffers[UP_LINK].sent_data, + router_buffers[1].send_buffers[UP_LINK].sent_data_size); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +}