Skip to content

Commit

Permalink
test: OpenFileGDB: verify that we can generate an output that is byte…
Browse files Browse the repository at this point in the history
…-identical to the expected golden file.
  • Loading branch information
rouault committed Oct 31, 2024
1 parent a85853f commit 303e334
Show file tree
Hide file tree
Showing 24 changed files with 62 additions and 9 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
30 changes: 30 additions & 0 deletions autotest/ogr/ogr_openfilegdb_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# SPDX-License-Identifier: MIT
###############################################################################

import os
import struct
import sys

Expand Down Expand Up @@ -4571,3 +4572,32 @@ def test_ogr_openfilegdb_write_OGRUnsetMarker(tmp_vsimem):
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f["i32"] == -21121


###############################################################################
# Verify that we can generate an output that is byte-identical to the expected golden file.


@pytest.mark.parametrize(
"src_directory",
[
# Generated with:
# ogr2ogr autotest/ogr/data/openfilegdb/polygon_golden.gdb '{"type":"Feature","properties":{"foo":"bar"},"geometry":{"type":"Polygon","coordinates":[[[0,0],[0,1],[1,0],[0,0]]]}}' --config OPENFILEGDB_CREATOR GDAL --config OPENFILEGDB_REPRODUCIBLE_UUID YES -f openfilegdb
"data/openfilegdb/polygon_golden.gdb",
],
)
def test_ogr_openfilegdb_write_check_golden_file(tmp_path, src_directory):

out_directory = str(tmp_path / "test.gdb")
with gdaltest.config_options(
{"OPENFILEGDB_CREATOR": "GDAL", "OPENFILEGDB_REPRODUCIBLE_UUID": "YES"}
):
gdal.VectorTranslate(out_directory, src_directory, format="OpenFileGDB")
for filename in os.listdir(src_directory):
src_filename = os.path.join(src_directory, filename)
out_filename = os.path.join(out_directory, filename)

assert os.stat(src_filename).st_size == os.stat(out_filename).st_size, filename
assert (
open(src_filename, "rb").read() == open(out_filename, "rb").read()
), filename
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/openfilegdb/ogr_openfilegdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

using namespace OpenFileGDB;

std::string OFGDBGenerateUUID();
std::string OFGDBGenerateUUID(bool bInit = false);

int OGROpenFileGDBIsComparisonOp(int op);

Expand Down
36 changes: 28 additions & 8 deletions ogr/ogrsf_frmts/openfilegdb/ogropenfilegdb_generate_uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,30 @@ static int CPLGettimeofday(struct CPLTimeVal *tp, void * /* timezonep*/)
// Probably not the best UUID generator ever. One issue is that mt19937
// uses only a 32-bit seed.
CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
std::string OFGDBGenerateUUID()
std::string OFGDBGenerateUUID(bool bInit)
{
struct CPLTimeVal tv;
memset(&tv, 0, sizeof(tv));
static uint32_t nCounter = 0;
const bool bReproducibleUUID =
CPLTestBool(CPLGetConfigOption("OPENFILEGDB_REPRODUCIBLE_UUID", "NO"));

if (bInit)
{
if (bReproducibleUUID)
nCounter = 0;
return std::string();
}

uint32_t nCounterLocal = nCounter;
// From POSIX.1-2001 as an example of an implementation of rand()
// for reproducible output
const auto reproducibleRand = [&nCounterLocal]()
{
nCounterLocal = nCounterLocal * 1103515245U + 12345U;
return (nCounterLocal / 65536U) % 32768U;
};

std::stringstream ss;

{
Expand All @@ -77,17 +93,17 @@ std::string OFGDBGenerateUUID()
ss << std::hex;
for (int i = 0; i < 8; i++)
{
ss << dis(gen);
ss << (bReproducibleUUID ? (reproducibleRand() % 16) : dis(gen));
}
ss << "-";
for (int i = 0; i < 4; i++)
{
ss << dis(gen);
ss << (bReproducibleUUID ? (reproducibleRand() % 16) : dis(gen));
}
ss << "-4";
for (int i = 0; i < 3; i++)
{
ss << dis(gen);
ss << (bReproducibleUUID ? (reproducibleRand() % 16) : dis(gen));
}
}

Expand All @@ -102,17 +118,21 @@ std::string OFGDBGenerateUUID()
std::uniform_int_distribution<> dis2(8, 11);

ss << "-";
ss << dis2(gen);
ss << (bReproducibleUUID ? 8 : dis2(gen));
for (int i = 0; i < 3; i++)
{
ss << dis(gen);
ss << (bReproducibleUUID ? (reproducibleRand() % 16) : dis(gen));
}
ss << "-";
for (int i = 0; i < 12; i++)
{
ss << dis(gen);
ss << (bReproducibleUUID ? (reproducibleRand() % 16) : dis(gen));
};
ss << "}";
return ss.str();
}

if (!bReproducibleUUID)
nCounter = nCounterLocal;

return ss.str();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,8 @@ bool OGROpenFileGDBDataSource::Create(const char *pszName)
return false;
}

CPL_IGNORE_RET_VAL(OFGDBGenerateUUID(/* bInit = */ true));

m_osDirName = pszName;
eAccess = GA_Update;

Expand Down

0 comments on commit 303e334

Please sign in to comment.