Evan Benn has uploaded this change for review.

View Change

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>
---
M tests/io_mock.h
M tests/tests.c
M tests/wraps.h
3 files changed, 32 insertions(+), 17 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/63/69263/1
diff --git a/tests/io_mock.h b/tests/io_mock.h
index 9e92a9d..8e6ecb9 100644
--- a/tests/io_mock.h
+++ b/tests/io_mock.h
@@ -33,6 +33,8 @@

/* Required for `FILE *` */
#include <stdio.h>
+/* Required for `mode_t` */
+#include <fcntl.h>

#include <stdint.h>

@@ -47,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

@@ -101,7 +98,7 @@
void (*libusb_free_config_descriptor)(void *state, struct libusb_config_descriptor *);

/* POSIX File I/O */
- int (*open)(void *state, const char *pathname, int flags);
+ int (*open)(void *state, const char *pathname, int flags, mode_t mode);
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);
diff --git a/tests/tests.c b/tests/tests.c
index 53bad65..530d5a7 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()->open)
- return get_io()->open(get_io()->state, pathname, flags);
+ return get_io()->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,22 @@
return MOCK_FD;
}

-int __wrap_open(const char *pathname, int flags)
+int __wrap_open(const char *pathname, int flags, mode_t mode)
{
LOG_ME;
- return mock_open(pathname, flags);
+ return mock_open(pathname, flags, mode);
}

-int __wrap_open64(const char *pathname, int flags)
+int __wrap_open64(const char *pathname, int flags, mode_t mode)
{
LOG_ME;
- return mock_open(pathname, flags);
+ return mock_open(pathname, flags, mode);
}

-int __wrap___open64_2(const char *pathname, int flags)
+int __wrap___open64_2(const char *pathname, int flags, mode_t mode)
{
LOG_ME;
- return mock_open(pathname, flags);
+ return mock_open(pathname, flags, mode);
}

int __wrap_ioctl(int fd, unsigned long int request, ...)
diff --git a/tests/wraps.h b/tests/wraps.h
index f97eaba..e2134fd 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, mode_t mode);
+int __wrap_open64(const char *pathname, int flags, mode_t mode);
+int __wrap___open64_2(const char *pathname, int flags, mode_t mode);
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.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I8c134e6d36a248d0f51985e389085a9e585fb83d
Gerrit-Change-Number: 69263
Gerrit-PatchSet: 1
Gerrit-Owner: Evan Benn <evanbenn@google.com>
Gerrit-MessageType: newchange