Geo-Coder-Free

 view release on metacpan or  search on metacpan

t/edge_cases.t  view on Meta::CPAN

#!/usr/bin/env perl

# edge_cases.t — Destructive, boundary-condition, and security tests for
# Geo::Coder::Free and its backends.  The strategy is to actively try to
# break or subvert the module rather than verifying the happy path.

use strict;
use warnings;

use File::Temp qw(tempdir tempfile);
use Scalar::Util qw(looks_like_number refaddr);
use POSIX qw(HUGE_VAL);
use Test::Most;
use Test::Mockingbird qw(mock restore_all);

# Suppress OA/WOF auto-detection so no real database is required.
local $ENV{OPENADDR_HOME}    = '';
local $ENV{WHOSONFIRST_HOME} = '';

use_ok 'Geo::Coder::Free';
use_ok 'Geo::Coder::Free::Local';
use_ok 'Geo::Coder::Free::OpenAddresses';
use_ok 'Geo::Coder::Free::Utils';

# -----------------------------------------------------------------------
# Shared fixtures — created once to avoid exhausting <DATA> filehandle.
# -----------------------------------------------------------------------

# Single Local instance — Geo::Coder::Free::Local reads __DATA__ only once.
my $LOCAL_OBJ = Geo::Coder::Free::Local->new();

# MaxMind mock — prevents any real DB access for GCF facade tests.
# Call _remock_maxmind() after any restore_all() to reinstate these stubs.
sub _remock_maxmind {
	mock('Geo::Coder::Free::MaxMind', 'geocode',         sub { return undef });
	mock('Geo::Coder::Free::MaxMind', 'reverse_geocode', sub { return undef });
}
_remock_maxmind();

# Tempdir for OA — creates a valid (but empty) openaddr directory.
my $TMPDIR = tempdir(CLEANUP => 1);

# Named constants replace magic numbers in assertions.
use constant {
	LAT_MAX   =>  90,
	LAT_MIN   => -90,
	LON_MAX   =>  180,
	LON_MIN   => -180,
	# Alarm timeout for ReDoS regression tests (seconds).
	REDOS_TIMEOUT => 5,
};

# -----------------------------------------------------------------------
# 1. Utils::distance — undef / missing coordinates
# Strategy: each null argument slot should trigger a distinct croak
# rather than silently producing 0 or NaN.
# -----------------------------------------------------------------------
subtest 'distance() — null/undef coordinates croak' => sub {
	throws_ok { Geo::Coder::Free::Utils::distance(undef, 0,     0,     0    ) } qr/lat1 must be defined/,  'undef lat1';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     undef, 0,     0    ) } qr/lon1 must be defined/,  'undef lon1';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     0,     undef, 0    ) } qr/lat2 must be defined/,  'undef lat2';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     0,     0,     undef) } qr/lon2 must be defined/,  'undef lon2';
};

# -----------------------------------------------------------------------
# 2. Utils::distance — non-numeric coordinates
# Strategy: ensure the numeric guard is enforced for every parameter
# position, including empty string (looks_like_number returns false for '').
# -----------------------------------------------------------------------
subtest 'distance() — non-numeric coordinates croak' => sub {
	throws_ok { Geo::Coder::Free::Utils::distance('abc', 0,     0,     0    ) } qr/lat1 must be numeric/, 'alpha lat1';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     'xyz', 0,     0    ) } qr/lon1 must be numeric/, 'alpha lon1';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     0,     'bad', 0    ) } qr/lat2 must be numeric/, 'alpha lat2';
	throws_ok { Geo::Coder::Free::Utils::distance(0,     0,     0,     'no' ) } qr/lon2 must be numeric/, 'alpha lon2';

	# Empty string is not numeric.
	throws_ok { Geo::Coder::Free::Utils::distance('', 0, 0, 0) } qr/lat1 must be numeric/, 'empty string lat1';
};

# -----------------------------------------------------------------------
# 3. Utils::distance — out-of-range coordinates
# Strategy: the range guards must reject impossible geographic coordinates
# even when the values are otherwise numeric.
# -----------------------------------------------------------------------
subtest 'distance() — out-of-range coordinates croak' => sub {
	throws_ok { Geo::Coder::Free::Utils::distance(91,   0,  0,   0   ) } qr/Latitude must be between/, 'lat1 > 90';
	throws_ok { Geo::Coder::Free::Utils::distance(-91,  0,  0,   0   ) } qr/Latitude must be between/, 'lat1 < -90';
	throws_ok { Geo::Coder::Free::Utils::distance(0,    0,  91,  0   ) } qr/Latitude must be between/, 'lat2 > 90';
	throws_ok { Geo::Coder::Free::Utils::distance(0,    0, -91,  0   ) } qr/Latitude must be between/, 'lat2 < -90';

	throws_ok { Geo::Coder::Free::Utils::distance(0, 181,   0,  0   ) } qr/Longitude must be between/, 'lon1 > 180';
	throws_ok { Geo::Coder::Free::Utils::distance(0, -181,  0,  0   ) } qr/Longitude must be between/, 'lon1 < -180';
	throws_ok { Geo::Coder::Free::Utils::distance(0,  0,    0, 181  ) } qr/Longitude must be between/, 'lon2 > 180';
	throws_ok { Geo::Coder::Free::Utils::distance(0,  0,    0, -181 ) } qr/Longitude must be between/, 'lon2 < -180';

	# Large values that are still numeric.
	throws_ok { Geo::Coder::Free::Utils::distance(9999, 0, 0, 0) } qr/Latitude must be between/, 'large lat';
	throws_ok { Geo::Coder::Free::Utils::distance(0, 9999, 0, 0) } qr/Longitude must be between/, 'large lon';
};

# -----------------------------------------------------------------------
# 4. Utils::distance — exact boundary coordinates (valid; must not croak)
# Strategy: lat=±90 and lon=±180 are the inclusive extremes of the WGS-84
# coordinate system.  The range check uses strict > / < so the boundary
# values themselves are valid.
# -----------------------------------------------------------------------
subtest 'distance() — valid boundary coordinates' => sub {
	my $d;
	lives_ok { $d = Geo::Coder::Free::Utils::distance(LAT_MAX,  LON_MAX,  LAT_MIN,  LON_MIN,  'K') } 'poles + antimeridian accepted';
	cmp_ok($d, '>', 0, 'distance between diagonally opposite extreme points > 0');

	# Exact poles: both at North Pole should be distance 0.
	lives_ok { $d = Geo::Coder::Free::Utils::distance(90, 0, 90, 0, 'M') } 'North Pole to itself accepted';
	is($d, 0, 'North Pole to itself is 0 miles');

	# Antimeridian: same lat, lon ±180.



( run in 3.095 seconds using v1.01-cache-2.11-cpan-60884a8df76 )