Skip to content

Commit

Permalink
Add autotest/cpp/test_deferred_plugin.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 12, 2023
1 parent 3a181e5 commit 522c9d9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
9 changes: 8 additions & 1 deletion autotest/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ set(QUICKTEST_LIST
test-multi-threaded-writing
test-destroy
test-bug1488
test-log)
test-log
test-deferred-plugin
)

gdal_gtest_target(testcopywords test-copy-words testcopywords.cpp)
gdal_gtest_target(testclosedondestroydm test-closed-on-destroy-DM testclosedondestroydm.cpp)
Expand All @@ -308,6 +310,11 @@ gdal_autotest_target(test_include_from_c_file test-include-from-C-file test_incl
gdal_autotest_target(test_c_include_from_cpp_file test-C-include-from-CPP-file test_c_include_from_cpp_file.cpp "")
gdal_gtest_target(bug1488 test-bug1488 bug1488.cpp)
gdal_gtest_target(testlog test-log testlog.cpp)
gdal_gtest_target(test_deferred_plugin test-deferred-plugin test_deferred_plugin.cpp)

if (GDAL_ENABLE_DRIVER_JPEG_PLUGIN)
target_compile_definitions(test_deferred_plugin PRIVATE -DJPEG_PLUGIN)
endif()

if (UNIX)
list(APPEND QUICKTEST_LIST test-osr-set-proj-search-paths)
Expand Down
83 changes: 83 additions & 0 deletions autotest/cpp/test_deferred_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/******************************************************************************
* $Id$
*
* Project: GDAL
* Purpose: Test deferred plugin loading
* Author: Even Rouault, even.rouault at spatialys.com
*
******************************************************************************
* Copyright (c) 2023, Even Rouault <even.rouault at spatialys.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.
****************************************************************************/

#include "gdal_priv.h"

#include "gtest_include.h"

#include "test_data.h"

namespace
{

// ---------------------------------------------------------------------------

TEST(test_deferredplugin, test_missing)
{
#ifdef JPEG_PLUGIN
CPLSetConfigOption("GDAL_DRIVER_PATH", "/i/do_not_exist");
GDALAllRegister();
CPLSetConfigOption("GDAL_DRIVER_PATH", nullptr);
GDALDriverH hDrv = GDALGetDriverByName("JPEG");
EXPECT_EQ(hDrv, nullptr);
// May trigger error message `/path/to/autotest/gdrivers/data/jpeg/byte_with_xmp.jpg' not recognized as a supported file format. It could have been recognized by driver JPEG, but plugin gdal_JPEG.so is not available in your installation.
CPLPushErrorHandler(CPLQuietErrorHandler);
GDALClose(
GDALOpen(GDRIVERS_DIR "data/jpeg/byte_with_xmp.jpg", GA_ReadOnly));
CPLPopErrorHandler();
EXPECT_TRUE(
strstr(CPLGetLastErrorMsg(),
"It could have been recognized by driver JPEG, but plugin "
"gdal_JPEG.so is not available in your installation") !=
nullptr);
#else
GTEST_SKIP() << "JPEG driver not built or not built as a plugin";
#endif
}

TEST(test_deferredplugin, test_nominal)
{
#ifdef JPEG_PLUGIN
GDALAllRegister();
GDALDriverH hDrv = GDALGetDriverByName("JPEG");
ASSERT_NE(hDrv, nullptr);
EXPECT_NE(GDALDriver::FromHandle(hDrv)->pfnIdentify, nullptr);
EXPECT_STREQ(GDALGetMetadataItem(hDrv, GDAL_DCAP_OPEN, nullptr), "YES");
EXPECT_EQ(GDALDriver::FromHandle(hDrv)->pfnOpen, nullptr);
GDALDatasetH hDS =
GDALOpen(GDRIVERS_DIR "data/jpeg/byte_with_xmp.jpg", GA_ReadOnly);
EXPECT_NE(hDS, nullptr);
EXPECT_NE(GDALDriver::FromHandle(hDrv)->pfnOpen, nullptr);
GDALClose(hDS);
#else
GTEST_SKIP() << "JPEG driver not built or not built as a plugin";
#endif
}

} // namespace

0 comments on commit 522c9d9

Please sign in to comment.