[libvirt PATCH 0/2] tests: Improve macOS stat() mocking logic

See 2/2 for more information. Andrea Bolognani (2): tests: Split macOS stat() mocking logic tests: Improve macOS stat() mocking logic tests/virmockstathelpers.c | 75 ++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 32 deletions(-) -- 2.26.2

On macOS, most of the symbols and declarations that we look at to determine which versions of stat() we need to mock are not present; on the other hand, there are some specific wrinkles that are introduced with Apple Silicon which we will need to take care of. To avoid making the logic even more of an opaque mess than it currently is, move the macOS part to a separate branch. This commit is better viewed with 'git show -w'. Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virmockstathelpers.c | 73 ++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/tests/virmockstathelpers.c b/tests/virmockstathelpers.c index 3bd2437ffe..97f628368d 100644 --- a/tests/virmockstathelpers.c +++ b/tests/virmockstathelpers.c @@ -74,42 +74,47 @@ * The same all applies to lstat() */ - -#if !defined(WITH___XSTAT_DECL) -# if defined(WITH_STAT) -# if !defined(WITH___XSTAT) && !defined(WITH_STAT64) || defined(__APPLE__) -# define MOCK_STAT +#if !defined(__APPLE__) +# if !defined(WITH___XSTAT_DECL) +# if defined(WITH_STAT) +# if !defined(WITH___XSTAT) && !defined(WITH_STAT64) +# define MOCK_STAT +# endif # endif -# endif -# if defined(WITH_STAT64) -# define MOCK_STAT64 -# endif -#else /* WITH___XSTAT_DECL */ -# if defined(WITH___XSTAT) && !defined(WITH___XSTAT64) -# define MOCK___XSTAT -# endif -# if defined(WITH___XSTAT64) -# define MOCK___XSTAT64 -# endif -#endif /* WITH___XSTAT_DECL */ - -#if !defined(WITH___LXSTAT_DECL) -# if defined(WITH_LSTAT) -# if !defined(WITH___LXSTAT) && !defined(WITH_LSTAT64) || defined(__APPLE__) -# define MOCK_LSTAT +# if defined(WITH_STAT64) +# define MOCK_STAT64 # endif -# endif -# if defined(WITH_LSTAT64) -# define MOCK_LSTAT64 -# endif -#else /* WITH___LXSTAT_DECL */ -# if defined(WITH___LXSTAT) && !defined(WITH___LXSTAT64) -# define MOCK___LXSTAT -# endif -# if defined(WITH___LXSTAT64) -# define MOCK___LXSTAT64 -# endif -#endif /* WITH___LXSTAT_DECL */ +# else /* WITH___XSTAT_DECL */ +# if defined(WITH___XSTAT) && !defined(WITH___XSTAT64) +# define MOCK___XSTAT +# endif +# if defined(WITH___XSTAT64) +# define MOCK___XSTAT64 +# endif +# endif /* WITH___XSTAT_DECL */ +# if !defined(WITH___LXSTAT_DECL) +# if defined(WITH_LSTAT) +# if !defined(WITH___LXSTAT) && !defined(WITH_LSTAT64) +# define MOCK_LSTAT +# endif +# endif +# if defined(WITH_LSTAT64) +# define MOCK_LSTAT64 +# endif +# else /* WITH___LXSTAT_DECL */ +# if defined(WITH___LXSTAT) && !defined(WITH___LXSTAT64) +# define MOCK___LXSTAT +# endif +# if defined(WITH___LXSTAT64) +# define MOCK___LXSTAT64 +# endif +# endif /* WITH___LXSTAT_DECL */ +#else /* __APPLE__ */ +# define MOCK_STAT +# define MOCK_STAT64 +# define MOCK_LSTAT +# define MOCK_LSTAT64 +#endif #ifdef MOCK_STAT static int (*real_stat)(const char *path, struct stat *sb); -- 2.26.2

We should not mock stat64() when building on Apple Silicon, because the declaration is not present in the header file. Detect this situation and handle it gracefully. https://gitlab.com/libvirt/libvirt/-/issues/121 Signed-off-by: Andrea Bolognani <abologna@redhat.com> --- tests/virmockstathelpers.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/virmockstathelpers.c b/tests/virmockstathelpers.c index 97f628368d..830dfe1085 100644 --- a/tests/virmockstathelpers.c +++ b/tests/virmockstathelpers.c @@ -57,7 +57,9 @@ * * On macOS stat() and lstat() are resolved to _stat$INODE64 and * _lstat$INODE64, respectively. stat(2) man page also declares that - * stat64(), lstat64() and fstat64() are deprecated. + * stat64(), lstat64() and fstat64() are deprecated, and when + * building on Apple Silicon (aarch64) those functions are missing + * from the header altogether and should not be mocked. * * With all this in mind the list of functions we have to mock will depend * on several factors @@ -111,9 +113,13 @@ # endif /* WITH___LXSTAT_DECL */ #else /* __APPLE__ */ # define MOCK_STAT -# define MOCK_STAT64 +# if defined(WITH_STAT64_DECL) +# define MOCK_STAT64 +# endif # define MOCK_LSTAT -# define MOCK_LSTAT64 +# if defined(WITH_LSTAT64_DECL) +# define MOCK_LSTAT64 +# endif #endif #ifdef MOCK_STAT -- 2.26.2

On 2/4/21 10:53 AM, Andrea Bolognani wrote:
See 2/2 for more information.
Andrea Bolognani (2): tests: Split macOS stat() mocking logic tests: Improve macOS stat() mocking logic
tests/virmockstathelpers.c | 75 ++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 32 deletions(-)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Michal
participants (2)
-
Andrea Bolognani
-
Michal Privoznik