aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/init_shutdown.c54
-rw-r--r--tests/io_mock.h14
-rw-r--r--tests/meson.build2
-rw-r--r--tests/tests.c29
-rw-r--r--tests/tests.h1
5 files changed, 100 insertions, 0 deletions
diff --git a/tests/init_shutdown.c b/tests/init_shutdown.c
index eeea3cc1..d1d0c7f3 100644
--- a/tests/init_shutdown.c
+++ b/tests/init_shutdown.c
@@ -209,3 +209,57 @@ void linux_spi_init_and_shutdown_test_success(void **state)
skip();
#endif
}
+
+#define REALTEK_MST_MOCK_FD 0x10ec
+
+static int realtek_mst_open(void *state, const char *pathname, int flags)
+{
+ assert_string_equal(pathname, "/dev/i2c-254");
+ assert_int_equal(flags & O_RDWR, O_RDWR);
+ return REALTEK_MST_MOCK_FD;
+}
+
+static int realtek_mst_ioctl(void *state, int fd, unsigned long request, va_list args)
+{
+ assert_int_equal(fd, REALTEK_MST_MOCK_FD);
+ assert_int_equal(request, I2C_SLAVE);
+ /* Only access to I2C address 0x4a is expected */
+ unsigned long addr = va_arg(args, unsigned long);
+ assert_int_equal(addr, 0x4a);
+
+ return 0;
+}
+
+static int realtek_mst_read(void *state, int fd, void *buf, size_t sz)
+{
+ assert_int_equal(fd, REALTEK_MST_MOCK_FD);
+ assert_int_equal(sz, 1);
+ return sz;
+}
+
+static int realtek_mst_write(void *state, int fd, const void *buf, size_t sz)
+{
+ assert_int_equal(fd, REALTEK_MST_MOCK_FD);
+ const LargestIntegralType accepted_sizes[] = {1, 2};
+ assert_in_set(sz, accepted_sizes, ARRAY_SIZE(accepted_sizes));
+ return sz;
+}
+
+void realtek_mst_init_and_shutdown_test_success(void **state)
+{
+#if CONFIG_REALTEK_MST_I2C_SPI == 1
+ const struct io_mock realtek_mst_io = {
+ .open = realtek_mst_open,
+ .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");
+
+ io_mock_register(NULL);
+#else
+ skip();
+#endif /* CONFIG_REALTEK_I2C_SPI */
+}
diff --git a/tests/io_mock.h b/tests/io_mock.h
index 5cddca0d..c69cd38d 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -42,6 +42,14 @@ struct pci_dev {
unsigned int device_id;
};
+/* POSIX open() flags, avoiding dependency on fcntl.h */
+#define O_RDONLY 0
+#define O_WRONLY 1
+#define O_RDWR 2
+
+/* Linux I2C interface constants, avoiding linux/i2c-dev.h */
+#define I2C_SLAVE 0x0703
+
struct io_mock {
void *state;
@@ -65,6 +73,12 @@ struct io_mock {
unsigned char *data,
uint16_t wLength,
unsigned int timeout);
+
+ /* POSIX File I/O */
+ int (*open)(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 9fb86c71..2bde3f01 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -34,6 +34,8 @@ mocks = [
'-Wl,--wrap=open',
'-Wl,--wrap=open64',
'-Wl,--wrap=ioctl',
+ '-Wl,--wrap=read',
+ '-Wl,--wrap=write',
'-Wl,--wrap=fopen',
'-Wl,--wrap=fopen64',
'-Wl,--wrap=rget_io_perms',
diff --git a/tests/tests.c b/tests/tests.c
index ece3e8a5..13686326 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -83,21 +83,49 @@ uint8_t __wrap_sio_read(uint16_t port, uint8_t reg)
int __wrap_open(const char *pathname, int flags)
{
LOG_ME;
+ if (current_io && current_io->open)
+ return current_io->open(current_io->state, pathname, flags);
return MOCK_HANDLE;
}
int __wrap_open64(const char *pathname, int flags)
{
LOG_ME;
+ if (current_io && 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 sz;
+}
+
+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 sz;
+}
+
FILE *__wrap_fopen(const char *pathname, const char *mode)
{
LOG_ME;
@@ -250,6 +278,7 @@ int main(void)
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),
+ 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 9eacf13d..2b03458c 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -47,6 +47,7 @@ void nicrealtek_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);
+void realtek_mst_init_and_shutdown_test_success(void **state);
/* layout.c */
void included_regions_dont_overlap_test_success(void **state);