Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL Core
4 : * Purpose: Test block cache & writing behaviour under multi-threading
5 : * Author: Even Rouault, <even dot rouault at spatialys dot com>
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2011, Even Rouault <even dot rouault at spatialys dot com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_conv.h"
14 : #include "cpl_string.h"
15 : #include "gdal_alg.h"
16 : #include "gdal_priv.h"
17 : #include "gdal.h"
18 :
19 : #include "test_data.h"
20 :
21 : #include "gtest_include.h"
22 :
23 : // Excluded from Coverity Scan since it intentionally leaks datasets
24 : #ifndef __COVERITY__
25 :
26 : namespace
27 : {
28 :
29 : // ---------------------------------------------------------------------------
30 :
31 1 : static void OpenJPEG2000(const char *pszFilename)
32 : {
33 1 : const int N_DRIVERS = 6;
34 1 : const char *const apszDrivers[] = {"JP2ECW", "JP2OpenJPEG", "JPEG2000",
35 : "JP2MrSID", "JP2KAK", "JP2Lura"};
36 : GDALDriverH aphDrivers[N_DRIVERS];
37 : int i, j;
38 :
39 7 : for (i = 0; i < N_DRIVERS; i++)
40 6 : aphDrivers[i] = GDALGetDriverByName(apszDrivers[i]);
41 :
42 7 : for (i = 0; i < N_DRIVERS; i++)
43 : {
44 6 : if (aphDrivers[i] == nullptr)
45 4 : continue;
46 14 : for (j = 0; j < N_DRIVERS; j++)
47 : {
48 12 : if (i == j || aphDrivers[j] == nullptr)
49 10 : continue;
50 2 : GDALDeregisterDriver(aphDrivers[j]);
51 : }
52 :
53 2 : GDALDatasetH hDS = GDALOpen(pszFilename, GA_ReadOnly);
54 2 : if (!EQUAL(apszDrivers[i], "JP2Lura") &&
55 2 : !EQUAL(apszDrivers[i], "JPEG2000"))
56 : {
57 2 : ASSERT_TRUE(hDS != nullptr);
58 : }
59 2 : if (hDS)
60 2 : GDALGetRasterCount(hDS);
61 14 : for (j = 0; j < N_DRIVERS; j++)
62 : {
63 12 : if (i == j || aphDrivers[j] == nullptr)
64 10 : continue;
65 2 : GDALRegisterDriver(aphDrivers[j]);
66 : }
67 : }
68 : }
69 :
70 : // ---------------------------------------------------------------------------
71 :
72 4 : TEST(testclosedondestroydm, test)
73 : {
74 1 : const char *pszGDAL_SKIP = CPLGetConfigOption("GDAL_SKIP", nullptr);
75 1 : if (pszGDAL_SKIP == nullptr)
76 1 : CPLSetConfigOption("GDAL_SKIP", "GIF");
77 : else
78 0 : CPLSetConfigOption("GDAL_SKIP", CPLSPrintf("%s GIF", pszGDAL_SKIP));
79 :
80 1 : GDALAllRegister();
81 :
82 7 : const auto DoSomethingWithDataset = [](GDALDatasetH hDS)
83 : {
84 7 : if (hDS)
85 4 : GDALGetRasterCount(hDS);
86 7 : };
87 :
88 : {
89 1 : auto hDS = GDALOpen(GCORE_DATA_DIR "byte.tif", GA_ReadOnly);
90 1 : if (hDS)
91 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
92 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
93 : }
94 :
95 : {
96 1 : auto hDS = GDALOpen(GCORE_DATA_DIR "byte.vrt", GA_ReadOnly);
97 1 : if (hDS)
98 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
99 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
100 : }
101 :
102 : {
103 1 : auto hDS = GDALOpen(GDRIVERS_DIR "data/vrt/rgb_warp.vrt", GA_ReadOnly);
104 1 : if (hDS)
105 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
106 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
107 : }
108 :
109 1 : DoSomethingWithDataset(
110 : GDALOpen(GDRIVERS_DIR "data/nitf/A.TOC", GA_ReadOnly));
111 :
112 : {
113 : auto hDS =
114 1 : GDALOpen("NITF_TOC_ENTRY:CADRG_ONC_1,000,000_2_0:" GDRIVERS_DIR
115 : "data/nitf/A.TOC",
116 : GA_ReadOnly);
117 1 : if (hDS)
118 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
119 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
120 : }
121 :
122 : {
123 1 : auto hDS = GDALOpen(GDRIVERS_DIR "data/til/testtil.til", GA_ReadOnly);
124 1 : if (hDS)
125 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
126 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
127 : }
128 :
129 : {
130 1 : auto hDS = GDALOpen(GDRIVERS_DIR "data/rs2/product.xml", GA_ReadOnly);
131 1 : if (hDS)
132 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
133 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
134 : }
135 :
136 : {
137 : auto hDS =
138 1 : GDALOpen(GDRIVERS_DIR "data/dimap/METADATA.DIM", GA_ReadOnly);
139 1 : if (hDS)
140 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
141 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
142 : }
143 :
144 : {
145 : auto hDS =
146 1 : GDALOpen(GDRIVERS_DIR "tmp/cache/file9_j2c.ntf", GA_ReadOnly);
147 1 : if (hDS)
148 0 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
149 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
150 : }
151 :
152 : {
153 1 : auto hDS = GDALOpen(GDRIVERS_DIR "data/gif/bug407.gif", GA_ReadOnly);
154 1 : if (hDS)
155 : {
156 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
157 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
158 1 : GDALSetCacheMax(0);
159 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
160 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
161 : }
162 : }
163 :
164 : {
165 : /* Create external overviews */
166 1 : auto hSrcDS = GDALOpen(GCORE_DATA_DIR "byte.tif", GA_ReadOnly);
167 1 : CPL_IGNORE_RET_VAL(GDALCreateCopy(GDALGetDriverByName("GTiff"),
168 : "byte.tif", hSrcDS, 0, nullptr,
169 : nullptr, nullptr));
170 1 : GDALClose(hSrcDS);
171 : }
172 :
173 : {
174 1 : auto hDS = GDALOpen("byte.tif", GA_ReadOnly);
175 1 : int nOvrLevel = 2;
176 1 : int nBandNum = 1;
177 1 : CPL_IGNORE_RET_VAL(GDALBuildOverviews(hDS, "NEAR", 1, &nOvrLevel, 1,
178 : &nBandNum, nullptr, nullptr));
179 1 : GDALClose(hDS);
180 : }
181 :
182 : {
183 1 : auto hDS = GDALOpen("byte.tif", GA_ReadOnly);
184 1 : GDALGetOverviewCount(GDALGetRasterBand(hDS, 1));
185 : }
186 :
187 : {
188 : /* Create internal overviews */
189 1 : auto hSrcDS = GDALOpen(GCORE_DATA_DIR "byte.tif", GA_ReadOnly);
190 1 : CPL_IGNORE_RET_VAL(GDALCreateCopy(GDALGetDriverByName("GTiff"),
191 : "byte2.tif", hSrcDS, 0, nullptr,
192 : nullptr, nullptr));
193 1 : GDALClose(hSrcDS);
194 : }
195 :
196 : {
197 1 : auto hDS = GDALOpen("byte2.tif", GA_Update);
198 1 : int nOvrLevel = 2;
199 1 : int nBandNum = 1;
200 1 : CPL_IGNORE_RET_VAL(GDALBuildOverviews(hDS, "NEAR", 1, &nOvrLevel, 1,
201 : &nBandNum, nullptr, nullptr));
202 1 : GDALClose(hDS);
203 : }
204 :
205 : {
206 1 : auto hDS = GDALOpen("byte2.tif", GA_ReadOnly);
207 1 : GDALGetOverviewCount(GDALGetRasterBand(hDS, 1));
208 : }
209 :
210 : {
211 : /* Create external mask */
212 1 : auto hSrcDS = GDALOpen(GCORE_DATA_DIR "byte.tif", GA_ReadOnly);
213 1 : CPL_IGNORE_RET_VAL(GDALCreateCopy(GDALGetDriverByName("GTiff"),
214 : "byte3.tif", hSrcDS, 0, nullptr,
215 : nullptr, nullptr));
216 1 : GDALClose(hSrcDS);
217 : }
218 :
219 : {
220 1 : auto hDS = GDALOpen("byte3.tif", GA_ReadOnly);
221 1 : GDALCreateDatasetMaskBand(hDS, GMF_PER_DATASET);
222 1 : GDALClose(hDS);
223 : }
224 :
225 : {
226 1 : auto hDS = GDALOpen("byte3.tif", GA_ReadOnly);
227 1 : GDALGetMaskFlags(GDALGetRasterBand(hDS, 1));
228 : }
229 :
230 : {
231 1 : FILE *f = fopen("byte.vrt", "wb");
232 1 : fprintf(f, "%s",
233 : "<VRTDataset rasterXSize=\"20\" rasterYSize=\"20\">"
234 : "<VRTRasterBand dataType=\"Byte\" band=\"1\">"
235 : "<SimpleSource>"
236 : "<SourceFilename relativeToVRT=\"1\">" GCORE_DATA_DIR
237 : "byte.tif</SourceFilename>"
238 : "<SourceBand>1</SourceBand>"
239 : "<SourceProperties RasterXSize=\"20\" RasterYSize=\"20\" "
240 : "DataType=\"Byte\" BlockXSize=\"20\" BlockYSize=\"20\" />"
241 : "<SrcRect xOff=\"0\" yOff=\"0\" xSize=\"20\" ySize=\"20\"/>"
242 : "<DstRect xOff=\"0\" yOff=\"0\" xSize=\"20\" ySize=\"20\"/>"
243 : "</SimpleSource>"
244 : "</VRTRasterBand>"
245 : "</VRTDataset>");
246 1 : fclose(f);
247 : }
248 :
249 : {
250 1 : auto hDS = GDALOpen("byte.vrt", GA_ReadOnly);
251 1 : int nOvrLevel = 2;
252 1 : int nBandNum = 1;
253 1 : CPL_IGNORE_RET_VAL(GDALBuildOverviews(hDS, "NEAR", 1, &nOvrLevel, 1,
254 : &nBandNum, nullptr, nullptr));
255 1 : GDALClose(hDS);
256 : }
257 :
258 : {
259 1 : auto hDS = GDALOpen("byte.vrt", GA_ReadOnly);
260 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
261 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
262 1 : GDALGetOverviewCount(GDALGetRasterBand(hDS, 1));
263 : }
264 :
265 : {
266 1 : auto hDS = GDALOpen(
267 : "<VRTDataset rasterXSize=\"20\" rasterYSize=\"20\">"
268 : "<VRTRasterBand dataType=\"Byte\" band=\"1\">"
269 : "<SimpleSource>"
270 : "<SourceFilename relativeToVRT=\"1\">byte.vrt</SourceFilename>"
271 : "<SourceBand>1</SourceBand>"
272 : "<SourceProperties RasterXSize=\"20\" RasterYSize=\"20\" "
273 : "DataType=\"Byte\" BlockXSize=\"20\" BlockYSize=\"20\" />"
274 : "<SrcRect xOff=\"0\" yOff=\"0\" xSize=\"20\" ySize=\"20\"/>"
275 : "<DstRect xOff=\"0\" yOff=\"0\" xSize=\"20\" ySize=\"20\"/>"
276 : "</SimpleSource>"
277 : "</VRTRasterBand>"
278 : "</VRTDataset>",
279 : GA_ReadOnly);
280 1 : GDALChecksumImage(GDALGetRasterBand(hDS, 1), 0, 0,
281 : GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
282 : }
283 :
284 1 : DoSomethingWithDataset(
285 : GDALOpenShared(GCORE_DATA_DIR "byte.tif", GA_ReadOnly));
286 :
287 1 : DoSomethingWithDataset(
288 : GDALOpenShared(GCORE_DATA_DIR "byte.tif", GA_ReadOnly));
289 :
290 1 : DoSomethingWithDataset(
291 : GDALOpenShared(GDRIVERS_DIR "data/sid/mercator.sid", GA_ReadOnly));
292 :
293 1 : DoSomethingWithDataset(
294 : GDALOpen("RASTERLITE:" GDRIVERS_DIR
295 : "data/rasterlite/rasterlite_pyramids.sqlite,table=test",
296 : GA_ReadOnly));
297 :
298 1 : DoSomethingWithDataset(GDALOpen(
299 : "RASTERLITE:" GDRIVERS_DIR
300 : "data/rasterlite/rasterlite_pyramids.sqlite,table=test,level=1",
301 : GA_ReadOnly));
302 :
303 1 : OpenJPEG2000(GDRIVERS_DIR "data/jpeg2000/rgbwcmyk01_YeGeo_kakadu.jp2");
304 :
305 1 : DoSomethingWithDataset(
306 : GDALOpen(GDRIVERS_DIR "tmp/cache/Europe 2001_OZF.map", GA_ReadOnly));
307 :
308 1 : CPLDebug("TEST", "Call GDALDestroyDriverManager()");
309 1 : GDALDestroyDriverManager();
310 :
311 1 : unlink("byte.tif");
312 1 : unlink("byte.tif.ovr");
313 1 : unlink("byte2.tif");
314 1 : unlink("byte3.tif");
315 1 : unlink("byte3.tif.msk");
316 1 : unlink("byte.vrt");
317 1 : unlink("byte.vrt.ovr");
318 1 : }
319 :
320 : } // namespace
321 :
322 : #endif // __COVERITY__
|