Anastasia Klimchuk submitted this change.
9 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted one.
tests: Mock the mode_t variant of open
open has a second form with a mode_t argument. When mocking without this
argument a caller trying to O_CREAT would have their mode_t argument
discarded and a random stack variable would be used instead.
BUG=b:187647884
BRANCH=None
TEST=meson test
Change-Id: I8c134e6d36a248d0f51985e389085a9e585fb83d
Signed-off-by: Evan Benn <evanbenn@chromium.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/69263
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
M tests/io_mock.h
M tests/tests.c
M tests/tests.h
M tests/wraps.h
4 files changed, 57 insertions(+), 17 deletions(-)
diff --git a/tests/io_mock.h b/tests/io_mock.h
index 935e84b..020794a 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -49,11 +49,6 @@
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
@@ -107,7 +102,7 @@
int (*libusb_handle_events_timeout)(void *state, libusb_context *ctx, struct timeval *tv);
/* POSIX File I/O */
- int (*iom_open)(void *state, const char *pathname, int flags);
+ int (*iom_open)(void *state, const char *pathname, int flags, mode_t mode);
int (*iom_ioctl)(void *state, int fd, unsigned long request, va_list args);
int (*iom_read)(void *state, int fd, void *buf, size_t sz);
int (*iom_write)(void *state, int fd, const void *buf, size_t sz);
diff --git a/tests/tests.c b/tests/tests.c
index abbac64..b517898 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -75,10 +75,10 @@
return (uint8_t)mock();
}
-static int mock_open(const char *pathname, int flags)
+static int mock_open(const char *pathname, int flags, mode_t mode)
{
if (get_io() && get_io()->iom_open)
- return get_io()->iom_open(get_io()->state, pathname, flags);
+ return get_io()->iom_open(get_io()->state, pathname, flags, mode);
if (get_io() && get_io()->fallback_open_state) {
struct io_mock_fallback_open_state *io_state;
@@ -96,22 +96,43 @@
return MOCK_FD;
}
-int __wrap_open(const char *pathname, int flags)
+int __wrap_open(const char *pathname, int flags, ...)
{
LOG_ME;
- return mock_open(pathname, flags);
+ mode_t mode = 0;
+ if (flags & O_CREAT) {
+ va_list ap;
+ va_start(ap, flags);
+ mode = va_arg(ap, mode_t);
+ va_end(ap);
+ }
+ return mock_open(pathname, flags, mode);
}
-int __wrap_open64(const char *pathname, int flags)
+int __wrap_open64(const char *pathname, int flags, ...)
{
LOG_ME;
- return mock_open(pathname, flags);
+ mode_t mode = 0;
+ if (flags & O_CREAT) {
+ va_list ap;
+ va_start(ap, flags);
+ mode = va_arg(ap, mode_t);
+ va_end(ap);
+ }
+ return mock_open(pathname, flags, mode);
}
-int __wrap___open64_2(const char *pathname, int flags)
+int __wrap___open64_2(const char *pathname, int flags, ...)
{
LOG_ME;
- return mock_open(pathname, flags);
+ mode_t mode = 0;
+ if (flags & O_CREAT) {
+ va_list ap;
+ va_start(ap, flags);
+ mode = va_arg(ap, mode_t);
+ va_end(ap);
+ }
+ return mock_open(pathname, flags, mode);
}
int __wrap_ioctl(int fd, unsigned long int request, ...)
diff --git a/tests/tests.h b/tests/tests.h
index b36ac70..6a12fdb 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -16,6 +16,8 @@
#ifndef TESTS_H
#define TESTS_H
+#include <fcntl.h>
+
/* helpers.c */
void address_to_bits_test_success(void **state);
void bitcount_test_success(void **state);
diff --git a/tests/wraps.h b/tests/wraps.h
index f97eaba..e224d79 100644
--- a/tests/wraps.h
+++ b/tests/wraps.h
@@ -28,9 +28,9 @@
uintptr_t __wrap_pcidev_readbar(void *dev, int bar);
void __wrap_sio_write(uint16_t port, uint8_t reg, uint8_t data);
uint8_t __wrap_sio_read(uint16_t port, uint8_t reg);
-int __wrap_open(const char *pathname, int flags);
-int __wrap_open64(const char *pathname, int flags);
-int __wrap___open64_2(const char *pathname, int flags);
+int __wrap_open(const char *pathname, int flags, ...);
+int __wrap_open64(const char *pathname, int flags, ...);
+int __wrap___open64_2(const char *pathname, int flags, ...);
int __wrap_ioctl(int fd, unsigned long int request, ...);
int __wrap_write(int fd, const void *buf, size_t sz);
int __wrap_read(int fd, void *buf, size_t sz);
To view, visit change 69263. To unsubscribe, or for help writing mail filters, visit settings.