summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2022-11-10 17:27:08 +0100
committerEven Rouault <even.rouault@spatialys.com>2022-11-21 23:21:13 +0100
commit91e95ee9ae9cc91b5e657cb078827c361da9b1ae (patch)
tree91a4bf83b281ea92793504a6fdb4d0e532b032dc /test
parente7e567536baddd491e124f6fc5e11e80e740934e (diff)
downloadlibtiff-git-91e95ee9ae9cc91b5e657cb078827c361da9b1ae.tar.gz
Add TIFFOpenExt(), TIFFOpenWExt() and TIFFFdOpenExt() with re-entrant error handlers
Rename TIFFClientOpenEx() to TIFFClientOpenExt() Rework signature of the re-entrant error handlers and of TIFFSetWarningHandlerExt() and TIFFSetErrorHandlerExt() Use structures that can be extended as extra argument. Leverages and ammends https://gitlab.com/libtiff/libtiff/-/merge_requests/409
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt5
-rw-r--r--test/Makefile.am4
-rw-r--r--test/test_error_handlers.c151
3 files changed, 159 insertions, 1 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3554b214..d7c18584 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -175,6 +175,11 @@ target_sources(testtypes PRIVATE testtypes.c)
target_link_libraries(testtypes PRIVATE tiff tiff_port)
list(APPEND simple_tests testtypes)
+add_executable(test_error_handlers ../placeholder.h)
+target_sources(test_error_handlers PRIVATE test_error_handlers.c)
+target_link_libraries(test_error_handlers PRIVATE tiff tiff_port)
+list(APPEND simple_tests test_error_handlers)
+
if(WEBP_SUPPORT AND EMSCRIPTEN)
# Emscripten is pretty finnicky about linker flags.
# It needs --shared-memory if and only if atomics or bulk-memory is used.
diff --git a/test/Makefile.am b/test/Makefile.am
index 09a83065..bcb69a4f 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -75,7 +75,7 @@ endif
if TIFF_TESTS
check_PROGRAMS = \
ascii_tag long_tag short_tag strip_rw rewrite custom_dir custom_dir_EXIF_231 \
- defer_strile_loading defer_strile_writing test_directory \
+ defer_strile_loading defer_strile_writing test_directory test_error_handlers \
testtypes test_signed_tags $(JPEG_DEPENDENT_CHECK_PROG) $(STATIC_CHECK_PROGS)
endif
@@ -246,6 +246,8 @@ defer_strile_writing_SOURCES = defer_strile_writing.c
defer_strile_writing_LDADD = $(LIBTIFF)
test_directory_SOURCES = test_directory.c
test_directory_LDADD = $(LIBTIFF)
+test_error_handlers_SOURCES = test_error_handlers.c
+test_error_handlers_LDADD = $(LIBTIFF)
AM_CPPFLAGS = -I$(top_srcdir)/libtiff
diff --git a/test/test_error_handlers.c b/test/test_error_handlers.c
new file mode 100644
index 00000000..56faa29e
--- /dev/null
+++ b/test/test_error_handlers.c
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2022, Even Rouault <even.rouault at spatialys.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ *
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+/*
+ * TIFF Library
+ *
+ * Test error handlers
+ */
+
+#include "tif_config.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include "tiffio.h"
+
+#define ERROR_STRING_SIZE 1024
+
+typedef struct MyErrorHandlerUserDataStruct
+{
+ char* buffer;
+ size_t buffer_size;
+ TIFF* tif_got_from_callback;
+ char module[64];
+} MyErrorHandlerUserDataStruct;
+
+static int myErrorHandler(TIFF* tiff, void* user_data, const char* module, const char* fmt, va_list ap)
+{
+ MyErrorHandlerUserDataStruct* errorhandler_user_data = (MyErrorHandlerUserDataStruct*)user_data;
+ vsnprintf(errorhandler_user_data->buffer,
+ errorhandler_user_data->buffer_size,
+ fmt,
+ ap);
+ errorhandler_user_data->tif_got_from_callback = tiff;
+ snprintf(errorhandler_user_data->module, sizeof(errorhandler_user_data->module), "%s", module);
+ return 1;
+}
+
+int test_open_ext(int handlers_set_in_open)
+{
+ int ret = 0;
+ char error_buffer[ERROR_STRING_SIZE] = {0};
+ char warn_buffer[ERROR_STRING_SIZE] = {0};
+ MyErrorHandlerUserDataStruct errorhandler_user_data =
+ {
+ .buffer = error_buffer,
+ .buffer_size = ERROR_STRING_SIZE
+ };
+ MyErrorHandlerUserDataStruct warnhandler_user_data =
+ {
+ .buffer = warn_buffer,
+ .buffer_size = ERROR_STRING_SIZE
+ };
+ TIFF* tif;
+ if( handlers_set_in_open )
+ {
+ TIFFOpenExtStruct arguments = {
+ .version = 1,
+ .errorhandler = myErrorHandler,
+ .errorhandler_user_data = &errorhandler_user_data,
+ .warnhandler = myErrorHandler,
+ .warnhandler_user_data = &warnhandler_user_data
+ };
+ tif = TIFFOpenExt("test_error_handler.tif", "w", &arguments);
+ }
+ else
+ {
+ tif = TIFFOpen("test_error_handler.tif", "w");
+ TIFFSetErrorHandlerExtR(tif, myErrorHandler, &errorhandler_user_data);
+ TIFFSetWarningHandlerExtR(tif, myErrorHandler, &warnhandler_user_data);
+ }
+ if( tif == NULL )
+ {
+ fprintf(stderr, "Cannot create test_error_handler.tif");
+ exit(1);
+ }
+
+ // Simulate an error emitted by libtiff
+ TIFFErrorExtR(tif, "my_error_module", "%s", "some error message");
+ if( strcmp(error_buffer, "some error message") != 0 )
+ {
+ fprintf(stderr, "Did not get expected error message\n");
+ ret = 1;
+ }
+ if( strcmp(errorhandler_user_data.module, "my_error_module") != 0 )
+ {
+ fprintf(stderr, "Did not get expected error module\n");
+ ret = 1;
+ }
+ if( errorhandler_user_data.tif_got_from_callback != tif)
+ {
+ fprintf(stderr, "errorhandler_user_data.tif_got_from_callback != tif\n");
+ ret = 1;
+ }
+
+ // Simulate a warning emitted by libtiff
+ TIFFWarningExtR(tif, "my_warning_module", "%s", "some warning message");
+ if( strcmp(warn_buffer, "some warning message") != 0 )
+ {
+ fprintf(stderr, "Did not get expected warning message\n");
+ ret = 1;
+ }
+ if( strcmp(warnhandler_user_data.module, "my_warning_module") != 0 )
+ {
+ fprintf(stderr, "Did not get expected warning module\n");
+ ret = 1;
+ }
+ if( warnhandler_user_data.tif_got_from_callback != tif)
+ {
+ fprintf(stderr, "warnhandler_user_data.tif_got_from_callback != tif\n");
+ ret = 1;
+ }
+
+ TIFFClose(tif);
+ unlink("test_error_handler.tif");
+ return ret;
+}
+
+int main()
+{
+ int ret = 0;
+ ret += test_open_ext(1);
+ ret += test_open_ext(0);
+ return ret;
+}