Added basic fuzz testing for input functions

This commit is contained in:
Andrew Kane
2024-04-27 10:49:45 -07:00
parent 1cc66543be
commit b52beefbc6

62
test/t/037_inputs.pl Normal file
View File

@@ -0,0 +1,62 @@
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More;
# Initialize node
my $node = get_new_node('node');
$node->init;
$node->start;
# Create extension
$node->safe_psql("postgres", "CREATE EXTENSION vector;");
my @types = ("vector", "halfvec", "sparsevec");
my @inputs = ("[1.23,4.56,7.89]", "[1.23,4.56,7.89]", "{1:1.23,2:4.56,3:7.89}/3");
my @subs = (" ", " ", ",", ":", "-", "1", "9", "\0", "2147483648", "-2147483649");
for my $i (0 .. $#types)
{
my $type = $types[$i];
for (1 .. 100)
{
my $input = $inputs[$i] . "";
for (1 .. 1 + int(rand() * 2))
{
my $r = int(rand() * length($input));
my $sub = $subs[int(rand() * scalar(@subs))];
if ($sub eq "\0")
{
# Truncate
$input = substr($input, 0, $r);
}
elsif (rand() > 0.5)
{
# Insert
substr($input, $r, 0) = $sub;
}
else
{
# Replace
substr($input, $r, length($sub), $sub);
}
}
my ($ret, $stdout, $stderr) = $node->psql("postgres", "SELECT '$input'::$type;");
if ($ret != 0)
{
# Test for type in error message
like($stderr, qr/$type/);
}
else
{
# Count test
is($ret, 0);
}
}
}
done_testing();