DBIO-PostgreSQL-PostGIS

 view release on metacpan or  search on metacpan

t/codec-wkb-decoder.t  view on Meta::CPAN

  $got = $dec->($hex_of->(
    $head->(7), pack('V', 2),
    $head->(2 | $SRID), pack('V', 4326), $line->([0,0],[1,1]),
    $head->(1), $point->(5, 6),
  ));
  is $got->{srid}, undef, 'no outer SRID';
  is_deeply $got->{coords}, [
    { type => 'linestring', coords => [[0,0],[1,1]], is_empty => 0 },
    { type => 'point', coords => [5,6], is_empty => 0 },
  ], 'in a collection, too';
};

# A member must have the dimensions of the whole: ST_GeomFromEWKB says
# "Dimensions mismatch in lwcollection", for MULTI* and collections, in
# every combination of Z and M tried. The decoder read a member with the
# flags of the outer header, so a member of other dimensions was read at
# the wrong width.
subtest 'a member of other dimensions than the whole is refused' => sub {
  my $Z = 0x80000000; my $M = 0x40000000;
  my %bad = (
    'MULTIPOINT Z, 2D member' => [ 4 | $Z,      1,      [1, 2] ],
    'MULTIPOINT, Z member'    => [ 4,           1 | $Z, [1, 2, 3] ],
    'COLLECTION, M member'    => [ 7,           1 | $M, [1, 2, 3] ],
    'COLLECTION Z, 2D member' => [ 7 | $Z,      1,      [1, 2] ],
    'COLLECTION ZM, Z member' => [ 7 | $Z | $M, 1 | $Z, [1, 2, 3] ],
  );
  for my $name (sort keys %bad) {
    my ($outer, $inner, $pt) = @{ $bad{$name} };
    my $got = eval { $d->decode_hex($hex_of->(
      $head->($outer), pack('V', 1), $head->($inner), $point->(@$pt),
    )) };
    is $got, undef, "$name: no answer" or diag explain $got;
    like $@, qr/decode_hex: member .*dimensions/, "$name: the error names the dimensions"
      or diag $@;
  }
  my $ok = $dec->($hex_of->(
    $head->(4 | $Z), pack('V', 1), $head->(1 | $Z), $point->(1, 2, 3),
  ));
  is_deeply $ok->{coords}, [[1,2,3]], 'MULTIPOINT Z with a Z member: read';
};

# ISO WKB writes the dimension into the type code (1001 = POINT Z, 2001 =
# POINT M, 3001 = POINT ZM). PostGIS reads those codes but never sends
# them, and this decoder does not read them: the type word is masked with
# 0xFFFF, so 1001 is a type id without a name. It is decoded like any such
# id -- header only, no type, no coordinates -- and does not throw.
# (Maintainer decision, karr k25.)
subtest 'ISO WKB type codes are not read' => sub {
  for my $iso (1001, 2001, 3001, 1002, 1007) {
    my $got = eval { $d->decode_hex($hex_of->($head->($iso), $point->(1, 2, 3))) };
    ok $got, "$iso: decode_hex answers" or diag $@ or next;
    is $got->{type},   undef, "$iso: no type";
    is $got->{coords}, undef, "$iso: no coordinates";
    is $got->{has_z},  0,     "$iso: no Z flag";
  }
};

# Emptiness (karr k19) and the parts of a collection (karr k24). The hex
# strings are what a PostGIS 3.6 server sends for ST_GeomFromText(<wkt>,
# 4326); t/36-empty-collection-live.t holds them against the server it
# runs on. PostGIS writes POINT EMPTY as a point of NaN ordinates, every
# other empty geometry as a count of 0. The decoder reports is_empty as
# ST_IsEmpty does: a geometry is empty if it has no parts or only empty
# ones (MULTIPOINT(EMPTY) is empty). An empty point has coords [], not
# [NaN, NaN].
my $NaN = 'NaN' + 0;
my $pt = sub { { type => 'point', coords => $_[0], is_empty => $_[1] // 0 } };
my @empty_cases = (
  [ 'POINT EMPTY', '0101000020e6100000000000000000f87f000000000000f87f',
    'point', [], 1, 0, 0 ],
  [ 'POINT Z EMPTY', '01010000a0e6100000000000000000f87f000000000000f87f000000000000f87f',
    'point', [], 1, 1, 0 ],
  [ 'POINT M EMPTY', '0101000060e6100000000000000000f87f000000000000f87f000000000000f87f',
    'point', [], 1, 0, 1 ],
  [ 'LINESTRING EMPTY', '0102000020e610000000000000', 'linestring', [], 1, 0, 0 ],
  [ 'LINESTRING ZM EMPTY', '01020000e0e610000000000000', 'linestring', [], 1, 1, 1 ],
  [ 'POLYGON EMPTY', '0103000020e610000000000000', 'polygon', [], 1, 0, 0 ],
  [ 'MULTIPOINT EMPTY', '0104000020e610000000000000', 'multipoint', [], 1, 0, 0 ],
  [ 'MULTIPOINT Z EMPTY', '01040000a0e610000000000000', 'multipoint', [], 1, 1, 0 ],
  [ 'GEOMETRYCOLLECTION EMPTY', '0107000020e610000000000000',
    'geometrycollection', [], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION Z EMPTY', '01070000a0e610000000000000',
    'geometrycollection', [], 1, 1, 0 ],
  # Empty parts: the part stays in its place as an empty list.
  [ 'MULTIPOINT(EMPTY,(1 2))',
    '0104000020e6100000020000000101000000000000000000f87f000000000000f87f0101000000000000000000f03f0000000000000040',
    'multipoint', [ [], [1,2] ], 0, 0, 0 ],
  [ 'MULTIPOINT((1 2),EMPTY)',
    '0104000020e6100000020000000101000000000000000000f03f00000000000000400101000000000000000000f87f000000000000f87f',
    'multipoint', [ [1,2], [] ], 0, 0, 0 ],
  [ 'MULTIPOLYGON(EMPTY,((0 0,1 0,1 1,0 0)))',
    '0106000020e6100000020000000103000000000000000103000000010000000400000000000000000000000000000000000000000000000000f03f0000000000000000000000000000f03f000000000000f03f00000000000000000000000000000000',
    'multipolygon', [ [], [[[0,0],[1,0],[1,1],[0,0]]] ], 0, 0, 0 ],
  # A geometry of nothing but empty parts is empty (ST_IsEmpty says so).
  [ 'MULTIPOINT(EMPTY)',
    '0104000020e6100000' . unpack('H*', pack('V C V d< d<', 1, 1, 1, $NaN, $NaN)),
    'multipoint', [ [] ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION(POLYGON EMPTY)', '0107000020e610000001000000010300000000000000',
    'geometrycollection', [ { type => 'polygon', coords => [], is_empty => 1 } ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION(MULTIPOINT EMPTY)', '0107000020e610000001000000010400000000000000',
    'geometrycollection', [ { type => 'multipoint', coords => [], is_empty => 1 } ], 1, 0, 0 ],
  [ 'GEOMETRYCOLLECTION Z (POINT Z EMPTY)',
    '01070000a0e6100000010000000101000080000000000000f87f000000000000f87f000000000000f87f',
    'geometrycollection', [ $pt->([], 1) ], 1, 1, 0 ],
  [ 'GEOMETRYCOLLECTION(POINT EMPTY,LINESTRING(0 0,1 1))',
    '0107000020e6100000020000000101000000000000000000f87f000000000000f87f01020000000200000000000000000000000000000000000000000000000000f03f000000000000f03f',
    'geometrycollection',
    [ $pt->([], 1), { type => 'linestring', coords => [[0,0],[1,1]], is_empty => 0 } ], 0, 0, 0 ],
  # Parts of every kind, and a collection in a collection.
  [ 'GEOMETRYCOLLECTION(MULTIPOINT((1 2),(3 4)),POLYGON((0 0,1 0,1 1,0 0)))',
    '0107000020e6100000020000000104000000020000000101000000000000000000f03f00000000000000400101000000000000000000084000000000000010400103000000010000000400000000000000000000000000000000000000000000000000f03f0000000000000000000000000000f03f00000000000...
    'geometrycollection',
    [ { type => 'multipoint', coords => [[1,2],[3,4]], is_empty => 0 },
      { type => 'polygon', coords => [[[0,0],[1,0],[1,1],[0,0]]], is_empty => 0 } ], 0, 0, 0 ],
  [ 'GEOMETRYCOLLECTION(GEOMETRYCOLLECTION(POINT(1 2)),POINT(3 4))',
    '0107000020e610000002000000' . '0107000000' . '01000000' . '0101000000' . unpack('H*', pack('d< d<', 1, 2))
      . '0101000000' . unpack('H*', pack('d< d<', 3, 4)),
    'geometrycollection',
    [ { type => 'geometrycollection', coords => [ $pt->([1,2]) ], is_empty => 0 }, $pt->([3,4]) ], 0, 0, 0 ],
  [ 'GEOMETRYCOLLECTION M (POINT M (1 2 3))',
    '0107000060e6100000010000000101000040000000000000f03f00000000000000400000000000000840',
    'geometrycollection', [ $pt->([1,2,3]) ], 0, 0, 1 ],
);
for my $c (@empty_cases) {
  my ($wkt, $hex, $type, $coords, $empty, $z, $m) = @$c;
  subtest "empty and parts: $wkt" => sub {
    my $got = eval { $d->decode_hex($hex) };
    ok $got, 'decodes' or diag $@ or return;
    is $got->{type},     $type,   'type';
    is $got->{is_empty}, $empty,  'is_empty';
    is $got->{has_z},    $z,      'Z flag';
    is $got->{has_m},    $m,      'M flag';
    is_deeply $got->{coords}, $coords, 'coords' or diag explain $got->{coords};
  };
}

# A value that is not decoded says nothing about emptiness: is_empty is
# undef, not 0.
subtest 'a value that is not decoded has no is_empty' => sub {
  my $got = $d->decode_hex(
    '0108000020E61000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000'
  );
  ok exists $got->{is_empty}, 'is_empty is in the answer ...';
  is $got->{is_empty}, undef, '... and is undef';
};

# A point with one NaN ordinate is a point, not an empty one.
subtest 'a point with a single NaN ordinate is not empty' => sub {
  my $got = $dec->($hex_of->($head->(1), $point->($NaN, 2)));
  is $got->{is_empty}, 0, 'not empty';
  is scalar @{ $got->{coords} }, 2, 'coords keep both ordinates';
};

# A NaN pair inside a line is a coordinate like any other, not an empty
# point: only a POINT (or a member of a MULTIPOINT or collection) is.
subtest 'NaN in a line is not an empty point' => sub {
  my $got = $dec->($hex_of->($head->(2), $line->([$NaN, $NaN], [1, 1])));
  is $got->{is_empty}, 0, 'not empty';
  is scalar @{ $got->{coords} }, 2, 'both points kept';
};

# Collections nest at most 32 levels deep, the outermost being level 1;
# deeper input is refused like any other that is no EWKB.
subtest 'nesting of collections is limited to 32 levels' => sub {
  my $nested = sub {
    my $depth = shift;
    my $hex = '';
    $hex .= unpack('H*', pack('C V V', 1, 7, 1)) for 1 .. $depth - 1;
    $hex .= unpack('H*', pack('C V V', 1, 7, 0));
    return $hex;
  };

  my $got = eval { $d->decode_hex($nested->(32)) };
  ok $got, 'depth 32 is decoded' or diag $@;
  is $got->{type}, 'geometrycollection', '... as a collection';

  $got = eval { $d->decode_hex($nested->(33)) };
  is $got, undef, 'depth 33: no answer';
  like $@, qr/decode_hex: nesting too deep/, '... naming the limit';
  unlike $@, qr/[0-9a-f]{10}/, '... without the input in the message';

  # The depth is refused early, so the cost does not depend on the input size.
  # The input is built before the measurement; the measure is CPU time of the
  # process, not wall clock, so a loaded machine does not tip it. The limit is
  # generous: a refusal costs close to nothing, a decoder that reads or copies
  # the whole input first is what it catches.
  my $deep = $nested->(100_000);
  my $started = cpu_time();
  eval { $d->decode_hex($deep) };
  my $took = cpu_time() - $started;
  like $@, qr/nesting too deep/, 'depth 100000 is refused';
  cmp_ok $took, '<', 1, '... fast';

  require DBIO::PostgreSQL::PostGIS::Geometry;
  my $g = DBIO::PostgreSQL::PostGIS::Geometry->from_ewkb_hex($nested->(33));
  isa_ok $g, 'DBIO::PostgreSQL::PostGIS::Geometry', 'from_ewkb_hex of depth 33';
  is $g->ewkb_hex, $nested->(33), '... keeps the hex';
  is $g->geometry_type, 'GEOMETRYCOLLECTION', '... and the type of its header';
  is $g->coordinates, undef, '... without coordinates';
};

# A coordinate read from EWKB is the shortest decimal whose double is the
# bytes read (what PostGIS writes as text), also on a perl whose NV is
# wider than a double: there 13.4 would read as 13.4000000000000004.
for my $x (0.1, 13.4, 52.5, -0.25, 1e300, 5e-324, 0.30000000000000004) {
  my $bytes = pack 'd<', $x;
  my $hex = unpack 'H*', pack('C V', 1, 1) . $bytes . pack('d<', 0);
  my $got = $d->decode_hex($hex)->{coords}[0];
  is pack('d<', $got), $bytes, "x=$x: read number packs to the input bytes";
}
for my $lit ('0.1', '13.4', '52.5', '-0.25', '1e+300') {
  my $hex = unpack 'H*', pack('C V d< d<', 1, 1, $lit, 0);
  is $d->decode_hex($hex)->{coords}[0], $lit, "$lit reads as the literal";
}
{
  my $hex = unpack 'H*', pack('C V', 1, 1) . pack('d<', 9**9**9 / 9**9**9) x 2;
  is_deeply $d->decode_hex($hex)->{coords}, [], 'NaN point stays an empty point';
}

done_testing;



( run in 0.691 second using v1.01-cache-2.11-cpan-60884a8df76 )