summaryrefslogtreecommitdiff
path: root/shm
diff options
context:
space:
mode:
authorDaniel Playfair Cal <daniel.playfair.cal@gmail.com>2019-01-07 22:33:15 +1100
committerDaniel Playfair Cal <daniel.playfair.cal@gmail.com>2019-02-12 19:47:56 +1100
commit8cbaee1c341b97d81fc597a4571b459baaac5c11 (patch)
tree1a1d58e7110fb231f0ea04d9b8626ec283e4a3c2 /shm
parent38e625da7f47e457d150efedc598437e2e867ef7 (diff)
downloaddconf-8cbaee1c341b97d81fc597a4571b459baaac5c11.tar.gz
Tests: replace usage of dlsym with separate modules containing functions that need to be mocked out
Diffstat (limited to 'shm')
-rw-r--r--shm/dconf-shm-mockable.c40
-rw-r--r--shm/dconf-shm-mockable.h31
-rw-r--r--shm/dconf-shm.c5
-rw-r--r--shm/meson.build22
4 files changed, 95 insertions, 3 deletions
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 <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daniel Playfair Cal <daniel.playfair.cal@gmail.com>
+ */
+
+/**
+ * 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 <unistd.h>
+
+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 <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daniel Playfair Cal <daniel.playfair.cal@gmail.com>
+ */
+
+#ifndef __dconf_shm_mockable_h__
+#define __dconf_shm_mockable_h__
+
+#include <glib.h>
+
+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 <sys/mman.h>
#include <unistd.h>
@@ -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,
+)