Edward O'Callaghan submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved Edward O'Callaghan: Looks good to me, approved
dediprog: Init-shutdown test for dediprog

This patch adds mocks for libusb functions. To avoid dependency on
libusb.h, libusb symbols for context and device handle are redefined.
Real libusb functions are never called in tests anyway, cmocka wraps
work with this without complaints.

BUG=b:181803212
TEST=builds and ninja test

Change-Id: I38508dfb6d7c24d42522f22fcae0c5e410c5f7ea
Signed-off-by: Anastasia Klimchuk <aklm@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/55934
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
---
M tests/init_shutdown.c
M tests/io_mock.h
M tests/meson.build
M tests/tests.c
M tests/tests.h
5 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index 48448f8..8469902 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -85,6 +85,40 @@
#endif
}

+int dediprog_libusb_control_transfer(void *state,
+ libusb_device_handle *devh,
+ uint8_t bmRequestType,
+ uint8_t bRequest,
+ uint16_t wValue,
+ uint16_t wIndex,
+ unsigned char *data,
+ uint16_t wLength,
+ unsigned int timeout)
+{
+ if (bRequest == 0x08 /* dediprog_cmds CMD_READ_PROG_INFO */) {
+ /* Provide dediprog Device String into data buffer */
+ memcpy(data, "SF600 V:7.2.2 ", wLength);
+ }
+ return wLength;
+}
+
+void dediprog_init_and_shutdown_test_success(void **state)
+{
+#if CONFIG_DEDIPROG == 1
+ const struct io_mock dediprog_io = {
+ .libusb_control_transfer = dediprog_libusb_control_transfer,
+ };
+
+ io_mock_register(&dediprog_io);
+
+ run_lifecycle(state, &programmer_dediprog, "voltage=3.5V");
+
+ io_mock_register(NULL);
+#else
+ skip();
+#endif
+}
+
struct ene_lpc_io_state {
unsigned char outb_val;
int pause_cmd;
diff --git a/tests/io_mock.h b/tests/io_mock.h
index 69045a2..72d89c5 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -31,9 +31,16 @@
#ifndef _IO_MOCK_H_
#define _IO_MOCK_H_

+/* Define libusb symbols to avoid dependency on libusb.h */
+struct libusb_device_handle;
+typedef struct libusb_device_handle libusb_device_handle;
+struct libusb_context;
+typedef struct libusb_context libusb_context;
+
struct io_mock {
void *state;

+ /* Port I/O */
void (*outb)(void *state, unsigned char value, unsigned short port);
unsigned char (*inb)(void *state, unsigned short port);

@@ -42,6 +49,17 @@

void (*outl)(void *state, unsigned int value, unsigned short port);
unsigned int (*inl)(void *state, unsigned short port);
+
+ /* USB I/O */
+ int (*libusb_control_transfer)(void *state,
+ libusb_device_handle *devh,
+ uint8_t bmRequestType,
+ uint8_t bRequest,
+ uint16_t wValue,
+ uint16_t wIndex,
+ unsigned char *data,
+ uint16_t wLength,
+ unsigned int timeout);
};

void io_mock_register(const struct io_mock *io);
diff --git a/tests/meson.build b/tests/meson.build
index 852fd4c..a18bc45 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -39,6 +39,13 @@
'-Wl,--wrap=test_inw',
'-Wl,--wrap=test_outl',
'-Wl,--wrap=test_inl',
+ '-Wl,--wrap=usb_dev_get_by_vid_pid_number',
+ '-Wl,--wrap=libusb_set_configuration',
+ '-Wl,--wrap=libusb_claim_interface',
+ '-Wl,--wrap=libusb_control_transfer',
+ '-Wl,--wrap=libusb_release_interface',
+ '-Wl,--wrap=libusb_close',
+ '-Wl,--wrap=libusb_exit',
'-Wl,--gc-sections',
]

diff --git a/tests/tests.c b/tests/tests.c
index dcbd5af..cde696f 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -22,6 +22,7 @@

/* redefinitions/wrapping */
#define LOG_ME printf("%s is called\n", __func__)
+#define MOCK_HANDLE 2021

static const struct io_mock *current_io = NULL;

@@ -55,19 +56,19 @@
int __wrap_open(const char *pathname, int flags)
{
LOG_ME;
- return 2021;
+ return MOCK_HANDLE;
}

int __wrap_open64(const char *pathname, int flags)
{
LOG_ME;
- return 2021;
+ return MOCK_HANDLE;
}

int __wrap_ioctl(int fd, unsigned long int request, ...)
{
LOG_ME;
- return 2021;
+ return MOCK_HANDLE;
}

FILE *__wrap_fopen(const char *pathname, const char *mode)
@@ -133,6 +134,52 @@
return 0;
}

+void *__wrap_usb_dev_get_by_vid_pid_number(
+ libusb_context *usb_ctx, uint16_t vid, uint16_t pid, unsigned int num)
+{
+ LOG_ME;
+ return (void *)MOCK_HANDLE;
+}
+
+int __wrap_libusb_set_configuration(libusb_device_handle *devh, int config)
+{
+ LOG_ME;
+ return 0;
+}
+
+int __wrap_libusb_claim_interface(libusb_device_handle *devh, int interface_number)
+{
+ LOG_ME;
+ return 0;
+}
+
+int __wrap_libusb_control_transfer(libusb_device_handle *devh, uint8_t bmRequestType,
+ uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data,
+ uint16_t wLength, unsigned int timeout)
+{
+ LOG_ME;
+ if (current_io && current_io->libusb_control_transfer)
+ return current_io->libusb_control_transfer(current_io->state,
+ devh, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
+ return 0;
+}
+
+int __wrap_libusb_release_interface(libusb_device_handle *devh, int interface_number)
+{
+ LOG_ME;
+ return 0;
+}
+
+void __wrap_libusb_close(libusb_device_handle *devh)
+{
+ LOG_ME;
+}
+
+void __wrap_libusb_exit(libusb_context *ctx)
+{
+ LOG_ME;
+}
+
int main(void)
{
int ret = 0;
@@ -172,6 +219,7 @@
const struct CMUnitTest init_shutdown_tests[] = {
cmocka_unit_test(dummy_init_and_shutdown_test_success),
cmocka_unit_test(mec1308_init_and_shutdown_test_success),
+ cmocka_unit_test(dediprog_init_and_shutdown_test_success),
cmocka_unit_test(ene_lpc_init_and_shutdown_test_success),
cmocka_unit_test(linux_spi_init_and_shutdown_test_success),
};
diff --git a/tests/tests.h b/tests/tests.h
index da4f4a9..2007695 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -43,6 +43,7 @@
/* init_shutdown.c */
void dummy_init_and_shutdown_test_success(void **state);
void mec1308_init_and_shutdown_test_success(void **state);
+void dediprog_init_and_shutdown_test_success(void **state);
void ene_lpc_init_and_shutdown_test_success(void **state);
void linux_spi_init_and_shutdown_test_success(void **state);


To view, visit change 55934. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I38508dfb6d7c24d42522f22fcae0c5e410c5f7ea
Gerrit-Change-Number: 55934
Gerrit-PatchSet: 5
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Edward O'Callaghan <quasisec@chromium.org>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@mailbox.org>
Gerrit-MessageType: merged