pgvector.git / summary / log / commit / refs
commit b52beefbc632921162ee357c861b28018d7fd39d
Author: Andrew Kane <andrew@ankane.org>
Date: Sat Apr 27 17:49:45 2024 +0000
Added basic fuzz testing for input functions
test/t/037_inputs.pl | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
diff --git a/test/t/037_inputs.pl b/test/t/037_inputs.pl
new file mode 100644
index 00000000..121e9bf9
--- /dev/null
+++ b/test/t/037_inputs.pl
@@ -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();
[parent: 1cc66543bec9]