Peter Marheine has uploaded this change for review.

View Change

tests: add init_shutdown test for realtek_mst_i2c_spi

This can catch regressions like the earlier one in this programmer that
caused initialization to always fail. Requires support for mocking POSIX
file I/O functions because the programmer does ioctls on the opened
file.

TEST=ninja test

Signed-off-by: Peter Marheine <pmarheine@chromium.org>
Change-Id: I5a5c617d1ec35d2a3bbe622e5add82a65eb396f0
---
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, 117 insertions(+), 1 deletion(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/11/56911/1
diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index 2698ea1..7078f79 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -296,3 +296,78 @@
skip();
#endif
}
+
+#if IS_LINUX == 1 && CONFIG_REALTEK_MST_I2C_SPI == 1
+
+#include <fcntl.h>
+#include <linux/i2c-dev.h>
+
+static int realtek_mst_open64(void *state, const char *pathname, int flags)
+{
+ if (strcmp(pathname, "/dev/i2c-254") != 0)
+ return -1;
+ if ((flags & O_RDWR) != O_RDWR)
+ return -1;
+
+ return 0x10ec;
+}
+
+static int realtek_mst_ioctl(void *state, int fd, unsigned long request, va_list args)
+{
+ unsigned long addr;
+
+ if (fd != 0x10ec)
+ return -1;
+ if (request != I2C_SLAVE)
+ return -1;
+ addr = va_arg(args, unsigned long);
+ printf("ioctl(0x10ec, I2C_SLAVE, %#lx)\n", addr);
+ if (addr != 0x4a)
+ return -1;
+
+ return 0;
+}
+
+static int realtek_mst_read(void *state, int fd, void *buf, size_t sz)
+{
+ if (fd != 0x10ec || sz != 1)
+ return -1;
+
+ printf("i2c register read\n");
+ return 1;
+}
+
+static int realtek_mst_write(void *state, int fd, const void *buf, size_t sz)
+{
+ const uint8_t *bytes_buf = buf;
+ if (fd != 0x10ec) {
+ return -1;
+ } else if (sz == 1) {
+ printf("i2c set register address %#x\n", bytes_buf[0]);
+ return 1;
+ } else if (sz == 2) {
+ printf("i2c write %#x -> %#x\n", bytes_buf[1], bytes_buf[0]);
+ return 2;
+ } else {
+ return -1;
+ }
+}
+
+void realtek_mst_init_and_shutdown_test_success(void **state)
+{
+ const struct io_mock realtek_mst_io = {
+ .open = realtek_mst_open64,
+ .ioctl = realtek_mst_ioctl,
+ .read = realtek_mst_read,
+ .write = realtek_mst_write,
+ };
+ io_mock_register(&realtek_mst_io);
+ run_lifecycle(state, &programmer_realtek_mst_i2c_spi, "bus=254,enter-isp=0");
+}
+
+#else
+void realtek_mst_init_and_shutdown_test_success(void **state)
+{
+ skip();
+}
+#endif /* IS_LINUX && CONFIG_REALTEK_MST_I2C_SPI */
diff --git a/tests/io_mock.h b/tests/io_mock.h
index bad13ba..732d8d5 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -68,10 +68,17 @@
unsigned char *data,
uint16_t wLength,
unsigned int timeout);
- /* File I/O */
+ /* Standard C File I/O */
FILE* (*fopen)(void *state, const char *pathname, const char *mode);
char* (*fgets)(void *state, char *buf, int len, FILE *fp);
size_t (*fread)(void *state, void *buf, size_t size, size_t len, FILE *fp);
+
+ /* POSIX File I/O */
+ int (*open)(void *state, const char *pathname, int flags);
+ int (*open64)(void *state, const char *pathname, int flags);
+ int (*ioctl)(void *state, int fd, unsigned long request, va_list args);
+ int (*read)(void *state, int fd, void *buf, size_t sz);
+ int (*write)(void *state, int fd, const void *buf, size_t sz);
};

void io_mock_register(const struct io_mock *io);
diff --git a/tests/meson.build b/tests/meson.build
index 014ff02..2fb601e 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -34,6 +34,8 @@
'-Wl,--wrap=open',
'-Wl,--wrap=open64',
'-Wl,--wrap=ioctl',
+ '-Wl,--wrap=read',
+ '-Wl,--wrap=write',
'-Wl,--wrap=fopen',
'-Wl,--wrap=fopen64',
'-Wl,--wrap=stat',
diff --git a/tests/tests.c b/tests/tests.c
index aab7d06..5ddb87c 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -89,15 +89,45 @@
int __wrap_open64(const char *pathname, int flags)
{
LOG_ME;
+ if (current_io) {
+ if (current_io->open64)
+ return current_io->open64(current_io->state, pathname, flags);
+ if (current_io->open)
+ return current_io->open(current_io->state, pathname, flags);
+ }
return MOCK_HANDLE;
}

int __wrap_ioctl(int fd, unsigned long int request, ...)
{
LOG_ME;
+ if (current_io && current_io->ioctl) {
+ va_list args;
+ int out;
+ va_start(args, request);
+ out = current_io->ioctl(current_io->state, fd, request, args);
+ va_end(args);
+ return out;
+ }
return MOCK_HANDLE;
}

+int __wrap_write(int fd, const void *buf, size_t sz)
+{
+ LOG_ME;
+ if (current_io && current_io->write)
+ return current_io->write(current_io->state, fd, buf, sz);
+ return -1;
+}
+
+int __wrap_read(int fd, void *buf, size_t sz)
+{
+ LOG_ME;
+ if (current_io && current_io->read)
+ return current_io->read(current_io->state, fd, buf, sz);
+ return -1;
+}
+
FILE *__wrap_fopen(const char *pathname, const char *mode)
{
LOG_ME;
@@ -312,6 +342,7 @@
cmocka_unit_test(ene_lpc_init_and_shutdown_test_success),
cmocka_unit_test(linux_mtd_init_and_shutdown_test_success),
cmocka_unit_test(linux_spi_init_and_shutdown_test_success),
+ cmocka_unit_test(realtek_mst_init_and_shutdown_test_success),
};
ret |= cmocka_run_group_tests_name("init_shutdown.c tests", init_shutdown_tests, NULL, NULL);

diff --git a/tests/tests.h b/tests/tests.h
index 989f83f..7088f05 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -48,6 +48,7 @@
void ene_lpc_init_and_shutdown_test_success(void **state);
void linux_mtd_init_and_shutdown_test_success(void **state);
void linux_spi_init_and_shutdown_test_success(void **state);
+void realtek_mst_init_and_shutdown_test_success(void **state);

/* layout.c */
void included_regions_dont_overlap_test_success(void **state);

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

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I5a5c617d1ec35d2a3bbe622e5add82a65eb396f0
Gerrit-Change-Number: 56911
Gerrit-PatchSet: 1
Gerrit-Owner: Peter Marheine <pmarheine@chromium.org>
Gerrit-MessageType: newchange