Database-BI

 view release on metacpan or  search on metacpan

t/domain.t  view on Meta::CPAN

	# -- DOMAIN 13 ------------------------------------------------------
	# The password: any text but empty, a reference, or ending in NUL.  The
	# edge at 64 bytes is where HMAC starts hashing the key first: both sides
	# must verify, and a changed last character must not.
	subtest 'DOMAIN 13 password: lengths 1, 63, 64, 65, 66 bytes and every kind of text' => sub {
		my $salt = 'S' x $LIMIT{salt_bytes};
		for my $length (1, $LIMIT{hmac_block} - 1, $LIMIT{hmac_block}, $LIMIT{hmac_block} + 1, $LIMIT{hmac_block} + 2) {
			my $password = 'a' x ($length - 1) . 'b';
			my $hash = Database::BI::Password::hash_password($password, rounds => $LIMIT{rounds_min}, salt => $salt);
			ok Database::BI::Password::verify_password($password, $hash), "$length bytes verifies";
			ok !Database::BI::Password::verify_password('a' x $length, $hash), "$length bytes: the last character matters";
			ok !Database::BI::Password::verify_password('a' x ($length - 1), $hash) || $length == 1, "$length bytes: one fewer does not verify" if $length > 1;
		}
		my %hash_of;
		for my $kind (@TEXT_KINDS) {
			# A character string (UTF-8 flag on, as a decoded form field is).
			my $password = $TEXT{$kind};
			utf8::upgrade($password);
			my $hash = Database::BI::Password::hash_password($password, rounds => $LIMIT{rounds_min}, salt => $salt);
			ok Database::BI::Password::verify_password($password, $hash), "$kind text: made and verified";
			ok Database::BI::Password::verify_password(Encode::encode('UTF-8', $password), $hash), "$kind text: as characters or as its UTF-8 bytes, the same password";
			$hash_of{$hash}++;
		}
		is scalar(keys %hash_of), scalar(@TEXT_KINDS), 'different texts give different hashes';
		is $why->(sub { Database::BI::Password::hash_password('') }), 'hash_password: a password is required', 'empty: refused';
		is $why->(sub { Database::BI::Password::hash_password("x\0") }), 'hash_password: a password may not end in a NUL byte', 'a trailing NUL: refused';
		ok eval { Database::BI::Password::hash_password("x\0y", rounds => $LIMIT{rounds_min}); 1 }, 'a NUL inside is a password';
		ok eval { Database::BI::Password::hash_password('0', rounds => $LIMIT{rounds_min}); 1 }, '"0" is a password, not an empty one';
		ok eval { Database::BI::Password::hash_password(' ', rounds => $LIMIT{rounds_min}); 1 }, 'a space is a password';
	};

	# -- DOMAIN 14 ------------------------------------------------------
	# point_key: lat in -90..90 and lon in -180..180, written as a plain decimal.
	subtest 'DOMAIN 14 point_key range: the edges and one either side' => sub {
		my $key = sub { Database::BI::Model::Boundary->point_key(@_) };
		my %valid = (
			'-90,-180' => '-90.000,-180.000', '90,180' => '90.000,180.000', '0,0' => '0.000,0.000',
			'89.9999,179.9999' => '90.000,180.000', '-89.9999,-179.9999' => '-90.000,-180.000',
			'90,-180' => '90.000,-180.000', '-90,180' => '-90.000,180.000',
		);
		is $key->(split /,/), $valid{$_}, "$_ -> $valid{$_}" for sort keys %valid;
		for my $point ('90.0001,0', '-90.0001,0', '0,180.0001', '0,-180.0001', '91,0', '0,181', '90.0001,180.0001', '90,180.0001', '90.0001,180',
			'1000,0', '0,1000') {
			is $key->(split /,/, $point), undef, "out of range: $point";
		}
		# Combinatorial: each coordinate at its limit, the other at both edges of its own.
		is $key->(90, -180), '90.000,-180.000', 'latitude at its maximum, longitude at its minimum';
		is $key->(-90, 180), '-90.000,180.000', 'latitude at its minimum, longitude at its maximum';
		is $key->(90, 180.001), undef, 'latitude at its maximum, longitude just above';
		is $key->(-90.001, -180), undef, 'longitude at its minimum, latitude just below';
		# Rounding is to three places.
		is $key->(0.0004, 0.0004), '0.000,0.000', '0.0004 rounds down';
		is $key->(0.0006, 0.0006), '0.001,0.001', '0.0006 rounds up';
	};

	subtest 'DOMAIN 15 point_key format: plain decimals against everything else' => sub {
		my $key = sub { Database::BI::Model::Boundary->point_key(@_) };
		for my $good ('1', '1.5', '-1.5', '0', '-0', '0.0', '12.345678', '007') {
			ok defined $key->($good, $good), 'a plain decimal: ' . $good;
		}
		for my $bad ('+1', '.5', '5.', '1e2', '1E2', ' 1', '1 ', "1\n", '1,5', '0x1', 'NaN', 'Inf', '', '--1', '1-', "\x{661}", "1\x{FF11}", "\x{1F600}", "1\x{202E}", "\x{0301}1", "\x{FC}") {
			is $key->($bad, 0), undef, 'not a plain decimal: ' . $shown->($bad);
		}
		is $key->(undef, 0), undef, 'undef latitude';
		is $key->(0, undef), undef, 'undef longitude';
	};

	# -- DOMAIN 16 ------------------------------------------------------
	# Boundary settings: ranks 8, 10 or 12; country codes of two letters;
	# online_max a whole number from 0.
	subtest 'DOMAIN 16 ranks, codes and online_max' => sub {
		my $class = 'Database::BI::Model::Boundary';
		for my $rank (8, 10, 12) {
			is_deeply $class->check_ranks({ lk => $rank }), { lk => $rank }, "rank $rank accepted";
		}
		for my $rank (7, 9, 11, 13, 0, -8, 80, 100, '08', '8 ') {
			throws_ok { $class->check_ranks({ lk => $rank }) } qr/\Athe rank for 'lk' must be one of 8, 10, 12/, "rank refused: '$rank'";
		}
		is_deeply $class->check_ranks({ aa => 8, ZZ => 12 }), { aa => 8, zz => 12 }, 'the first and last two-letter codes, in either case';
		for my $code ('', 'a', 'abc', 'a1', '1a', 'a-', "\x{FC}\x{FC}", "\x{1F600}\x{1F600}", "a\x{301}", "a\n") {
			throws_ok { $class->check_ranks({ $code => 10 }) } qr/\A'.*' is not a two-letter country code/s, 'code refused: ' . $shown->($code);
		}
		for my $max (0, 1, $LIMIT{online_max}, 1_000_000) {
			ok eval { $class->new(online_max => $max); 1 }, "online_max $max accepted";
		}
		for my $max (-1, 1.5, 'x', '') {
			ok !eval { $class->new(online_max => $max); 1 }, 'online_max refused: ' . $shown->($max);
		}
	};

	# -- DOMAIN 17 ------------------------------------------------------
	# parse_number: a plain ASCII decimal of up to 300 integer digits takes
	# the fast path; longer ones the full path; anything past DBL_MAX is no
	# number.  Non-ASCII symbols are removed, so currency signs do not matter.
	subtest 'DOMAIN 17 parse_number: digit count, magnitude and symbols' => sub {
		my $num = \&Database::BI::Util::parse_number;
		is $num->('1' x $LIMIT{plain_digits}), '1' x $LIMIT{plain_digits} + 0, 'the longest fast-path integer';
		is $num->('1' . '0' x ($LIMIT{plain_digits})), 1e300, 'one digit longer takes the full path to the same kind of answer';
		is $num->('1' . '0' x 307), 1e307, '308 digits';
		ok defined $num->('1' . '0' x 308), '309 digits starting 1 (1e308) is under the largest double';
		is $num->('9' . '0' x 308), undef, '309 digits starting 9 is past the largest double: no number';
		is $num->('-' . '9' . '0' x 308), undef, '... and so is its negative';
		ok defined $num->('-1' . '0' x 308), 'the negative edge, inside';
		ok defined $num->('1.7976931348623157e308'), 'the largest double, spelt out';
		is $num->('1.7976931348623159e308'), undef, 'one step past it';
		is $num->('1e309'), undef, '1e309';
		is $num->('0'), 0, 'zero';
		is $num->('-0.0'), 0, 'minus zero';
		is $num->('1e-400'), 0, 'underflow is zero, not an error';

		is $num->("\x{20AC}1,234.50"), 1234.5, 'a euro sign is removed';
		is $num->("1,234.50\x{20AC}"), 1234.5, '... after the number too';
		is $num->("\x{A3}5"), 5, 'a pound sign';
		is $num->("\x{202E}5"), 5, 'a right-to-left override is not a letter';
		is $num->('5%'), 5, 'a percent sign';
		for my $text ($TEXT{umlaut}, 'Gr' . "\x{F6}" . "\x{DF}e", $TEXT{zalgo}, $TEXT{cjk}, "\x{661}\x{662}\x{663}", "\x{FF11}\x{FF12}", 'abc', '1a', 'a1', '1.2.3', '--1') {
			is $num->($text), undef, 'not a number: ' . $shown->($text);
		}
		is $num->("\x{1F600}"), undef, 'an emoji alone is not a number';
		is $num->("(1,234.50)"), -1234.5, 'accounting brackets mean negative';
		is $num->('USD 7'), 7, 'a currency code';



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