aboutsummaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2021-09-07 14:47:50 +0200
committerEven Rouault <even.rouault@spatialys.com>2021-09-07 15:36:11 +0200
commitce080251225d16e11e139a5ebe07cf608fe022b2 (patch)
tree0b0435314410b304298b3f716bd6c5f518725391 /test/unit
parente75eebac667ef68222ba4ae697118761f667b57f (diff)
downloadPROJ-ce080251225d16e11e139a5ebe07cf608fe022b2.tar.gz
PROJ-ce080251225d16e11e139a5ebe07cf608fe022b2.zip
Fix database access across fork() when SQLite3 doesn't use pread[64]() (fixes #2843)
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/CMakeLists.txt12
-rw-r--r--test/unit/Makefile.am11
-rw-r--r--test/unit/test_fork.c122
3 files changed, 143 insertions, 2 deletions
diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt
index 1a080ac5..cc5d18b7 100644
--- a/test/unit/CMakeLists.txt
+++ b/test/unit/CMakeLists.txt
@@ -189,3 +189,15 @@ target_link_libraries(test_misc
add_test(NAME test_misc COMMAND test_misc)
set_property(TEST test_misc
PROPERTY ENVIRONMENT ${PROJ_TEST_ENVIRONMENT})
+
+if (USE_THREAD AND Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
+add_definitions(-DMUTEX_pthread)
+add_executable(test_fork
+ test_fork.c)
+target_link_libraries(test_fork
+ PRIVATE ${PROJ_LIBRARIES}
+ PRIVATE ${CMAKE_THREAD_LIBS_INIT})
+add_test(NAME test_fork COMMAND test_fork)
+set_property(TEST test_fork
+ PROPERTY ENVIRONMENT ${PROJ_TEST_ENVIRONMENT})
+endif()
diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am
index 1326332b..9ca3a723 100644
--- a/test/unit/Makefile.am
+++ b/test/unit/Makefile.am
@@ -4,7 +4,7 @@ EXTRA_DIST = CMakeLists.txt
noinst_HEADERS = gtest_include.h test_primitives.hpp
-AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include -I$(top_srcdir)/test @GTEST_CFLAGS@ @SQLITE3_CFLAGS@
+AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/include -I$(top_srcdir)/test @GTEST_CFLAGS@ @SQLITE3_CFLAGS@ -DMUTEX_@MUTEX_SETTING@
AM_CXXFLAGS = @CXX_WFLAGS@ @NO_ZERO_AS_NULL_POINTER_CONSTANT_FLAG@
PROJ_LIB ?= ../../data/for_tests
@@ -20,6 +20,7 @@ noinst_PROGRAMS += test_network
noinst_PROGRAMS += test_defmodel
noinst_PROGRAMS += test_tinshift
noinst_PROGRAMS += test_misc
+noinst_PROGRAMS += test_fork
pj_phi2_test_SOURCES = pj_phi2_test.cpp main.cpp
pj_phi2_test_LDADD = ../../src/libproj.la @GTEST_LIBS@
@@ -95,7 +96,13 @@ test_misc_LDADD = ../../src/libproj.la @GTEST_LIBS@
test_misc-check: test_misc
PROJ_LIB=$(PROJ_LIB) ./test_misc
+test_fork_SOURCES = test_fork.c
+test_fork_LDADD = ../../src/libproj.la @THREAD_LIB@
+
+test_fork-check: test_fork
+ PROJ_LIB=$(PROJ_LIB) ./test_fork
+
check-local: pj_phi2_test-check proj_errno_string_test-check \
proj_angular_io_test-check proj_context_test-check test_cpp_api-check \
gie_self_tests-check test_network-check test_defmodel-check test_tinshift-check \
- test_misc-check
+ test_misc-check test_fork-check
diff --git a/test/unit/test_fork.c b/test/unit/test_fork.c
new file mode 100644
index 00000000..181c1121
--- /dev/null
+++ b/test/unit/test_fork.c
@@ -0,0 +1,122 @@
+/******************************************************************************
+ *
+ * Project: PROJ
+ * Purpose: Test behavior of database access across fork()
+ * Author: Even Rouault <even dot rouault at spatialys dot com>
+ *
+ ******************************************************************************
+ * Copyright (c) 2021, Even Rouault <even dot rouault at spatialys dot com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ ****************************************************************************/
+
+#if defined(_WIN32) && defined(MUTEX_pthread)
+#undef MUTEX_pthread
+#endif
+
+#ifdef MUTEX_pthread
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "proj.h"
+
+int main()
+{
+ PJ_CONTEXT* ctxt = proj_context_create();
+ PJ_CONTEXT* ctxt2 = proj_context_create();
+
+ /* Cause database initialization */
+ {
+ PJ* P = proj_create(ctxt, "EPSG:4326");
+ if( P == NULL )
+ {
+ proj_context_destroy(ctxt);
+ return 1;
+ }
+ proj_destroy(P);
+ }
+ {
+ PJ* P = proj_create(ctxt2, "EPSG:4326");
+ if( P == NULL )
+ {
+ proj_context_destroy(ctxt);
+ proj_context_destroy(ctxt2);
+ return 1;
+ }
+ proj_destroy(P);
+ }
+
+ for(int iters = 0; iters < 100; ++iters )
+ {
+ pid_t children[4];
+ for( int i = 0; i< 4; i++ )
+ {
+ children[i] = fork();
+ if( children[i] < 0 )
+ {
+ fprintf(stderr, "fork() failed\n");
+ return 1;
+ }
+ if( children[i] == 0 )
+ {
+ {
+ PJ* P = proj_create(ctxt, "EPSG:3067");
+ if( P == NULL )
+ _exit(1);
+ proj_destroy(P);
+ }
+ {
+ PJ* P = proj_create(ctxt2, "EPSG:32631");
+ if( P == NULL )
+ _exit(1);
+ proj_destroy(P);
+ }
+ _exit(0);
+ }
+ }
+ for( int i = 0; i< 4; i++ )
+ {
+ int status = 0;
+ waitpid(children[i], &status, 0);
+ if( status != 0 )
+ {
+ fprintf(stderr, "Error in child\n");
+ return 1;
+ }
+ }
+ }
+
+ proj_context_destroy(ctxt);
+ proj_context_destroy(ctxt2);
+
+ return 0;
+}
+
+#else
+
+int main()
+{
+ return 0;
+}
+
+#endif