Anastasia Klimchuk has uploaded this change for review.

View Change

dediprog: Init-shutdown test for dediprog

This patch adds mocks for libusb functions. To avoid dependency on
libusb.h all libusb-specific pointers are replaced with void*. Real
libusb handles/ctx/etc are not needed for tests, and 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>
---
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, 120 insertions(+), 6 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/34/55934/1
diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index 48448f8..894a517 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -85,6 +85,41 @@
#endif
}

+int dediprog_libusb_control_transfer(void *state,
+ void *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
+ /* Manual voltage setting */
+ 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..01d0ad2 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -42,6 +42,23 @@

void (*outl)(void *state, unsigned int value, unsigned short port);
unsigned int (*inl)(void *state, unsigned short port);
+
+ /* USB I/O */
+ /*
+ * libusb_device_handle* replaced with void* to avoid dependency on libusb
+ * in tests (libusb may not be supported, but tests should not break).
+ * For mocking purposes it is all fine, and real libusb functions are never
+ * called in tests anyway.
+ */
+ int (*libusb_control_transfer)(void *state,
+ void *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 44bbbdb..eef6374 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -19,6 +19,7 @@

#include <stdio.h>
#include <stdint.h>
+#include <string.h>

/* redefinitions/wrapping */
#define LOG_ME printf("%s is called\n", __func__)
@@ -88,45 +89,97 @@
return 0;
}

-void __wrap_test_outb(unsigned char value, unsigned short port) {
+void __wrap_test_outb(unsigned char value, unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->outb)
current_io->outb(current_io->state, value, port);
}

-unsigned char __wrap_test_inb(unsigned short port) {
+unsigned char __wrap_test_inb(unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->inb)
return current_io->inb(current_io->state, port);
return 0;
}

-void __wrap_test_outw(unsigned short value, unsigned short port) {
+void __wrap_test_outw(unsigned short value, unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->outw)
current_io->outw(current_io->state, value, port);
}

-unsigned short __wrap_test_inw(unsigned short port) {
+unsigned short __wrap_test_inw(unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->inw)
return current_io->inw(current_io->state, port);
return 0;
}

-void __wrap_test_outl(unsigned int value, unsigned short port) {
+void __wrap_test_outl(unsigned int value, unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->outl)
current_io->outl(current_io->state, value, port);
}

-unsigned int __wrap_test_inl(unsigned short port) {
+unsigned int __wrap_test_inl(unsigned short port)
+{
/* LOG_ME; */
if (current_io && current_io->inl)
return current_io->inl(current_io->state, port);
return 0;
}

+void *__wrap_usb_dev_get_by_vid_pid_number(
+ void *usb_ctx, uint16_t vid, uint16_t pid, unsigned int num)
+{
+ LOG_ME;
+ return (void *)2021;
+}
+
+int __wrap_libusb_set_configuration(void *devh, int config)
+{
+ LOG_ME;
+ return 0;
+}
+
+int __wrap_libusb_claim_interface(void *devh, int interface_number)
+{
+ LOG_ME;
+ return 0;
+}
+
+int __wrap_libusb_control_transfer(void *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(void *devh, int interface_number)
+{
+ LOG_ME;
+ return 0;
+}
+
+void __wrap_libusb_close(void *devh)
+{
+ LOG_ME;
+}
+
+void __wrap_libusb_exit(void *ctx)
+{
+ LOG_ME;
+}
+
int main(void)
{
int ret = 0;
@@ -166,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: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange