usb_pe_drp: new unit test with mocks

Add a new unit test for usb_pe_drp with mocks for the various
subsystems that it will use.
The fake_prl module is already essentially a mock for the PRL layer,
so just move it into the mocks directory and make it available as
mock to other tests.

BUG=b:163421994
BRANCH=None
TEST=`make runhosttests` succeeds

Signed-off-by: Paul Fagerburg <pfagerburg@google.com>
Change-Id: I2aea3fa0694e8d9e6bae1f47516cb4d5d2a1e714
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2364050
Tested-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Jett Rink <jettrink@chromium.org>
Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
This commit is contained in:
Paul Fagerburg 2020-08-14 17:20:04 -06:00 committed by Commit Bot
parent bd018841f6
commit 33db716022
21 changed files with 456 additions and 11 deletions

View File

@ -16,4 +16,7 @@ mock-$(HAS_MOCK_TIMER) += timer_mock.o
mock-$(HAS_MOCK_USB_MUX) += usb_mux_mock.o
mock-$(HAS_MOCK_USB_PD) += usb_pd_mock.o
mock-$(HAS_MOCK_USB_PE_SM) += usb_pe_sm_mock.o
mock-$(HAS_MOCK_USB_TC_SM) += usb_tc_sm_mock.o
mock-$(HAS_MOCK_USB_TC_SM) += usb_tc_sm_mock.o
mock-$(HAS_MOCK_USB_PD_DPM) += usb_pd_dpm_mock.o
mock-$(HAS_MOCK_DP_ALT_MODE) += dp_alt_mode_mock.o
mock-$(HAS_MOCK_USB_PRL) += usb_prl_mock.o

View File

@ -0,0 +1,37 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Mock for DisplayPort alternate mode support
* Refer to VESA DisplayPort Alt Mode on USB Type-C Standard, version 2.0,
* section 5.2
*/
#include "usb_dp_alt_mode.h"
#include "mock/dp_alt_mode_mock.h"
#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)
#else
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif
void mock_dp_alt_mode_reset(void)
{
/* Nothing to do right now, but in the future ... */
}
void dp_init(int port)
{
CPRINTS("C%d: DP init", port);
}
void dp_teardown(int port)
{
CPRINTS("C%d: DP teardown", port);
}

View File

@ -0,0 +1,41 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Mock of Device Policy Manager implementation
* Refer to USB PD 3.0 spec, version 2.0, sections 8.2 and 8.3
*/
#include "usb_pd.h"
#include "mock/usb_pd_dpm_mock.h"
#include "memory.h"
struct mock_dpm_port_t dpm[CONFIG_USB_PD_PORT_MAX_COUNT];
void mock_dpm_reset(void)
{
/* Reset all values to 0. */
memset(dpm, 0, sizeof(dpm));
}
void dpm_init(int port)
{
dpm[port].mode_entry_done = false;
dpm[port].mode_exit_request = false;
}
void dpm_vdm_acked(int port, enum tcpm_transmit_type type, int vdo_count,
uint32_t *vdm)
{
}
void dpm_vdm_naked(int port, enum tcpm_transmit_type type, uint16_t svid,
uint8_t vdm_cmd)
{
}
void dpm_run(int port)
{
}

View File

@ -13,6 +13,12 @@
struct mock_pd_port_t mock_pd_port[CONFIG_USB_PD_PORT_MAX_COUNT];
void mock_pd_reset(void)
{
/* Reset all values to 0. */
memset(mock_pd_port, 0, sizeof(mock_pd_port));
}
enum pd_dual_role_states pd_get_dual_role(int port)
{
return PD_DRP_TOGGLE_ON;
@ -33,9 +39,6 @@ enum pd_cc_states pd_get_task_cc_state(int port)
return PD_CC_NONE;
}
/* TODO remove when usbc_fake is cleaned up */
#if !defined(CONFIG_USB_DRP_ACC_TRYSRC) && \
!defined(CONFIG_USB_CTVPD)
int pd_is_connected(int port)
{
return 1;
@ -45,19 +48,18 @@ bool pd_is_disconnected(int port)
{
return false;
}
#endif /* !CONFIG_USB_DRP_ACC_TRYSRC && !CONFIG_USB_CTVPD */
const uint32_t * const pd_get_src_caps(int port)
__overridable const uint32_t * const pd_get_src_caps(int port)
{
return NULL;
}
uint8_t pd_get_src_cap_cnt(int port)
__overridable uint8_t pd_get_src_cap_cnt(int port)
{
return 0;
}
void pd_set_src_caps(int port, int cnt, uint32_t *src_caps)
__overridable void pd_set_src_caps(int port, int cnt, uint32_t *src_caps)
{
}
@ -75,3 +77,21 @@ void pd_set_suspend(int port, int suspend)
{
}
enum tcpc_cc_polarity pd_get_polarity(int port)
{
return POLARITY_CC1;
}
void pd_request_data_swap(int port)
{}
void pd_request_vconn_swap_off(int port)
{}
void pd_request_vconn_swap_on(int port)
{}
bool pd_alt_mode_capable(int port)
{
return false;
}

View File

@ -4,13 +4,23 @@
*
* Fake Protocol Layer module.
*/
#include <string.h>
#include "common.h"
#include "usb_emsg.h"
#include "usb_prl_sm.h"
#include "mock/usb_prl_mock.h"
/* Defaults should all be 0 values. */
struct extended_msg rx_emsg[CONFIG_USB_PD_PORT_MAX_COUNT];
struct extended_msg tx_emsg[CONFIG_USB_PD_PORT_MAX_COUNT];
void mock_prl_reset(void)
{
/* Reset all values to 0. */
memset(rx_emsg, 0, sizeof(rx_emsg));
memset(tx_emsg, 0, sizeof(tx_emsg));
}
void prl_end_ams(int port)
{}
@ -30,6 +40,11 @@ int prl_is_running(int port)
return 1;
}
__overridable bool prl_is_busy(int port)
{
return false;
}
void prl_reset(int port)
{}

View File

@ -30,6 +30,8 @@ void mock_tc_port_reset(void)
mock_tc_port[port].msg_rx_id = 0;
mock_tc_port[port].sop = TCPC_TX_INVALID;
mock_tc_port[port].lcl_rp = TYPEC_RP_RESERVED;
mock_tc_port[port].attached_snk = 0;
mock_tc_port[port].attached_src = 0;
}
}
@ -48,7 +50,88 @@ void typec_select_src_collision_rp(int port, enum tcpc_rp_value rp)
mock_tc_port[port].lcl_rp = rp;
}
int tc_is_attached_src(int port)
{
return mock_tc_port[port].attached_src;
}
int tc_is_attached_snk(int port)
{
return mock_tc_port[port].attached_snk;
}
void tc_prs_snk_src_assert_rp(int port)
{
mock_tc_port[port].attached_snk = 0;
mock_tc_port[port].attached_src = 1;
}
void tc_prs_src_snk_assert_rd(int port)
{
mock_tc_port[port].attached_snk = 1;
mock_tc_port[port].attached_src = 0;
}
int typec_update_cc(int port)
{
return EC_SUCCESS;
}
void tc_set_data_role(int port, enum pd_data_role role)
{
mock_tc_port[port].data_role = role;
}
void tc_set_power_role(int port, enum pd_power_role role)
{
mock_tc_port[port].power_role = role;
}
int tc_check_vconn_swap(int port)
{
return 0;
}
void tc_ctvpd_detected(int port)
{}
int tc_is_vconn_src(int port)
{
return 0;
}
void tc_hard_reset_request(int port)
{
mock_tc_port_reset();
}
void tc_partner_dr_data(int port, int en)
{}
void tc_partner_dr_power(int port, int en)
{}
void tc_partner_unconstrainedpower(int port, int en)
{}
void tc_partner_usb_comm(int port, int en)
{}
void tc_pd_connection(int port, int en)
{}
void tc_pr_swap_complete(int port, bool success)
{}
void tc_src_power_off(int port)
{}
void tc_start_error_recovery(int port)
{}
void tc_snk_power_off(int port)
{}
void tc_request_power_swap(int port)
{
}

View File

@ -0,0 +1,14 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Mock for DisplayPort alternate mode support */
#ifndef __MOCK_DP_ALT_MODE_MOCK_H
#define __MOCK_DP_ALT_MODE_MOCK_H
#include "common.h"
void mock_dp_alt_mode_reset(void);
#endif /* __MOCK_DP_ALT_MODE_MOCK_H */

View File

@ -0,0 +1,23 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Mock of Device Policy Manager implementation */
#ifndef __MOCK_USB_PD_DPM_MOCK_H
#define __MOCK_USB_PD_DPM_MOCK_H
#include "common.h"
#include "usb_pd_dpm.h"
/* Defaults should all be 0 values. */
struct mock_dpm_port_t {
bool mode_entry_done;
bool mode_exit_request;
};
extern struct mock_dpm_port_t dpm[CONFIG_USB_PD_PORT_MAX_COUNT];
void mock_dpm_reset(void);
#endif /* __MOCK_USB_PD_DPM_MOCK_H */

View File

@ -10,6 +10,7 @@
#include "common.h"
#include "usb_pd.h"
/* Defaults should all be 0 values. */
struct mock_pd_port_t {
enum pd_data_role data_role;
enum pd_power_role power_role;
@ -17,4 +18,6 @@ struct mock_pd_port_t {
extern struct mock_pd_port_t mock_pd_port[CONFIG_USB_PD_PORT_MAX_COUNT];
void mock_pd_reset(void);
#endif /* __MOCK_USB_PD_MOCK_H */

View File

@ -0,0 +1,15 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Mock for USB protocol layer */
#ifndef __MOCK_USB_PRL_MOCK_H
#define __MOCK_USB_PRL_MOCK_H
#include "common.h"
#include "usb_emsg.h"
void mock_prl_reset(void);
#endif /* __MOCK_DP_ALT_MODE_MOCK_H */

View File

@ -19,6 +19,8 @@ struct mock_tc_port_t {
int msg_rx_id;
enum tcpm_transmit_type sop;
enum tcpc_rp_value lcl_rp;
int attached_snk;
int attached_src;
};
extern struct mock_tc_port_t mock_tc_port[CONFIG_USB_PD_PORT_MAX_COUNT];

View File

@ -94,6 +94,8 @@ test-list-host += usb_prl
test-list-host += usb_prl_noextended
test-list-host += usb_pe_drp_old
test-list-host += usb_pe_drp_old_noextended
test-list-host += usb_pe_drp
test-list-host += usb_pe_drp_noextended
test-list-host += utils
test-list-host += utils_str
test-list-host += vboot
@ -204,9 +206,10 @@ usb_typec_drp_acc_trysrc-y=usb_typec_drp_acc_trysrc.o vpd_api.o \
usb_prl_old-y=usb_prl_old.o usb_sm_checks.o fake_usbc.o
usb_prl-y=usb_prl.o usb_sm_checks.o
usb_prl_noextended-y=usb_prl_noextended.o usb_sm_checks.o fake_usbc.o
usb_pe_drp_old-y=usb_pe_drp_old.o usb_sm_checks.o fake_prl.o fake_usbc.o
usb_pe_drp_old_noextended-y=usb_pe_drp_old.o usb_sm_checks.o fake_prl.o \
fake_usbc.o
usb_pe_drp_old-y=usb_pe_drp_old.o usb_sm_checks.o fake_usbc.o
usb_pe_drp_old_noextended-y=usb_pe_drp_old.o usb_sm_checks.o fake_usbc.o
usb_pe_drp-y=usb_pe_drp.o usb_sm_checks.o
usb_pe_drp_noextended-y=usb_pe_drp_noextended.o usb_sm_checks.o
usb_tcpmv2_tcpci-y=usb_tcpmv2_tcpci.o vpd_api.o usb_sm_checks.o
utils-y=utils.o
utils_str-y=utils_str.o

View File

@ -378,6 +378,30 @@ int ncp15wb_calculate_temp(uint16_t adc);
#define CONFIG_USBC_SS_MUX
#endif
#if defined(TEST_USB_PE_DRP) || defined(TEST_USB_PE_DRP_NOEXTENDED)
#define CONFIG_TEST_USB_PE_SM
#define CONFIG_USB_PD_PORT_MAX_COUNT 1
#define CONFIG_USB_PE_SM
#define CONFIG_USB_PID 0x5036
#define CONFIG_USB_POWER_DELIVERY
#undef CONFIG_USB_PRL_SM
#define CONFIG_USB_PD_REV30
#if defined(TEST_USB_PE_DRP)
#define CONFIG_USB_PD_EXTENDED_MESSAGES
#endif
#define CONFIG_USB_PD_TCPMV2
#define CONFIG_USB_PD_DECODE_SOP
#undef CONFIG_USB_TYPEC_SM
#define CONFIG_USBC_VCONN
#define PD_VCONN_SWAP_DELAY 5000 /* us */
#define CONFIG_USB_PD_DISCHARGE_GPIO
#undef CONFIG_USB_PD_HOST_CMD
#define CONFIG_USB_PD_ALT_MODE_DFP
#define CONFIG_USBC_SS_MUX
#endif
/* Common TypeC tests defines */
#if defined(TEST_USB_TYPEC_VPD) || \
defined(TEST_USB_TYPEC_CTVPD)

56
test/usb_pe_drp.c Normal file
View File

@ -0,0 +1,56 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Test USB PE module.
*/
#include "common.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "usb_emsg.h"
#include "usb_mux.h"
#include "usb_pe.h"
#include "usb_pe_sm.h"
#include "usb_sm_checks.h"
#include "mock/usb_tc_sm_mock.h"
#include "mock/tcpc_mock.h"
#include "mock/usb_mux_mock.h"
#include "mock/usb_pd_mock.h"
#include "mock/usb_pd_dpm_mock.h"
#include "mock/dp_alt_mode_mock.h"
#include "mock/usb_prl_mock.h"
/* Install Mock TCPC and MUX drivers */
const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.drv = &mock_tcpc_driver,
},
};
const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.driver = &mock_usb_mux_driver,
}
};
void before_test(void)
{
mock_tc_port_reset();
mock_tcpc_reset();
mock_usb_mux_reset();
mock_pd_reset();
mock_dpm_reset();
mock_dp_alt_mode_reset();
mock_prl_reset();
}
void run_test(int argc, char **argv)
{
test_reset();
/* Do basic state machine validity checks last. */
RUN_TEST(test_pe_no_parent_cycles);
test_print_result();
}

13
test/usb_pe_drp.mocklist Normal file
View File

@ -0,0 +1,13 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#define CONFIG_TEST_MOCK_LIST \
MOCK(USB_TC_SM) \
MOCK(USB_PD) \
MOCK(TCPC) \
MOCK(USB_MUX) \
MOCK(USB_PD_DPM) \
MOCK(DP_ALT_MODE) \
MOCK(USB_PRL)

10
test/usb_pe_drp.tasklist Normal file
View File

@ -0,0 +1,10 @@
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* See CONFIG_TEST_TASK_LIST in config.h for details.
*/
#define CONFIG_TEST_TASK_LIST \
TASK_TEST(PD_C0, pd_task, NULL, LARGER_TASK_STACK_SIZE)

View File

@ -0,0 +1,46 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Test USB PE module.
*/
#include "common.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "usb_emsg.h"
#include "usb_mux.h"
#include "usb_pe.h"
#include "usb_pe_sm.h"
#include "usb_sm_checks.h"
#include "mock/usb_tc_sm_mock.h"
#include "mock/tcpc_mock.h"
#include "mock/usb_mux_mock.h"
/* Install Mock TCPC and MUX drivers */
const struct tcpc_config_t tcpc_config[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.drv = &mock_tcpc_driver,
},
};
const struct usb_mux usb_muxes[CONFIG_USB_PD_PORT_MAX_COUNT] = {
{
.driver = &mock_usb_mux_driver,
}
};
void before_test(void)
{
mock_tc_port_reset();
}
void run_test(int argc, char **argv)
{
test_reset();
/* Do basic state machine validity checks last. */
RUN_TEST(test_pe_no_parent_cycles);
test_print_result();
}

View File

@ -0,0 +1,13 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#define CONFIG_TEST_MOCK_LIST \
MOCK(USB_TC_SM) \
MOCK(USB_PD) \
MOCK(TCPC) \
MOCK(USB_MUX) \
MOCK(USB_PD_DPM) \
MOCK(DP_ALT_MODE) \
MOCK(USB_PRL)

View File

@ -0,0 +1,10 @@
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* See CONFIG_TEST_TASK_LIST in config.h for details.
*/
#define CONFIG_TEST_TASK_LIST \
TASK_TEST(PD_C0, pd_task, NULL, LARGER_TASK_STACK_SIZE)

View File

@ -0,0 +1,7 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#define CONFIG_TEST_MOCK_LIST \
MOCK(USB_PRL)

View File

@ -0,0 +1,7 @@
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#define CONFIG_TEST_MOCK_LIST \
MOCK(USB_PRL)