From 8cbaee1c341b97d81fc597a4571b459baaac5c11 Mon Sep 17 00:00:00 2001 From: Daniel Playfair Cal Date: Mon, 7 Jan 2019 22:33:15 +1100 Subject: Tests: replace usage of dlsym with separate modules containing functions that need to be mocked out --- shm/dconf-shm-mockable.c | 40 ++++++++++++++++++++++++++++++++++++++++ shm/dconf-shm-mockable.h | 31 +++++++++++++++++++++++++++++++ shm/dconf-shm.c | 5 +++-- shm/meson.build | 22 +++++++++++++++++++++- 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 shm/dconf-shm-mockable.c create mode 100644 shm/dconf-shm-mockable.h (limited to 'shm') diff --git a/shm/dconf-shm-mockable.c b/shm/dconf-shm-mockable.c new file mode 100644 index 0000000..6adf7d5 --- /dev/null +++ b/shm/dconf-shm-mockable.c @@ -0,0 +1,40 @@ +/* + * Copyright © 2019 Daniel Playfair Cal + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the licence, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + * Author: Daniel Playfair Cal + */ + +/** + * This module contains the production implementations of methods used in + * dconf_shm that need to be mocked out for tests. + * + * In some cases, external methods are wrapped with a different name. This is + * done so that it is not necessary to redefine the external functions in + * unit tests in order to mock them out, and therefore easy to also call the + * non mocked versions in tests if necessary. + */ + +#include "config.h" + +#include "dconf-shm-mockable.h" + +#include + +ssize_t +dconf_shm_pwrite (int fd, const void *buf, size_t count, off_t offset) +{ + return pwrite (fd, buf, count, offset); +} diff --git a/shm/dconf-shm-mockable.h b/shm/dconf-shm-mockable.h new file mode 100644 index 0000000..98ff33f --- /dev/null +++ b/shm/dconf-shm-mockable.h @@ -0,0 +1,31 @@ +/* + * Copyright © 2019 Daniel Playfair Cal + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the licence, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + * Author: Daniel Playfair Cal + */ + +#ifndef __dconf_shm_mockable_h__ +#define __dconf_shm_mockable_h__ + +#include + +G_GNUC_INTERNAL +ssize_t dconf_shm_pwrite (int fd, + const void *buf, + size_t count, + off_t offset); + +#endif /* __dconf_shm_mockable_h__ */ diff --git a/shm/dconf-shm.c b/shm/dconf-shm.c index ae8c1c7..dbde759 100644 --- a/shm/dconf-shm.c +++ b/shm/dconf-shm.c @@ -21,6 +21,7 @@ #include "config.h" #include "dconf-shm.h" +#include "dconf-shm-mockable.h" #include #include @@ -82,7 +83,7 @@ dconf_shm_open (const gchar *name) * By writing to the second byte in the file we ensure we don't * overwrite the first byte (which is the one we care about). */ - if (pwrite (fd, "", 1, 1) != 1) + if (dconf_shm_pwrite (fd, "", 1, 1) != 1) { g_critical ("failed to allocate file '%s': %s. dconf will not work properly.", filename, g_strerror (errno)); goto out; @@ -124,7 +125,7 @@ dconf_shm_flag (const gchar *name) * If this fails then it will probably fail for the client too. * If it doesn't then there's not really much we can do... */ - if (pwrite (fd, "", 1, 1) == 1) + if (dconf_shm_pwrite (fd, "", 1, 1) == 1) { guint8 *shm; diff --git a/shm/meson.build b/shm/meson.build index 5fb9fe2..07f77d0 100644 --- a/shm/meson.build +++ b/shm/meson.build @@ -1,6 +1,11 @@ +sources = files( + 'dconf-shm.c', + 'dconf-shm-mockable.c', +) + libdconf_shm = static_library( 'dconf-shm', - sources: 'dconf-shm.c', + sources: sources, include_directories: top_inc, dependencies: glib_dep, c_args: dconf_c_args, @@ -11,3 +16,18 @@ libdconf_shm_dep = declare_dependency( dependencies: glib_dep, link_with: libdconf_shm, ) + + +libdconf_shm_test = static_library( + 'dconf-shm-test', + sources: 'dconf-shm.c', + include_directories: top_inc, + dependencies: glib_dep, + c_args: dconf_c_args, + pic: true, +) + +libdconf_shm_test_dep = declare_dependency( + dependencies: glib_dep, + link_with: libdconf_shm, +) -- cgit v1.2.1