From 1d7f733aaf5f06ae604299376e1629b7ef2da2ab Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Sat, 16 Sep 2023 22:51:22 -0400 Subject: [PATCH] tests for protocol version negotiation --- test/Utils.pm | 21 +++++++++++++++ test/fuzz.pl | 21 ++++----------- test/protoversion.pl | 62 ++++++++++++++++++++++++++++++++++++++++++++ test/test.pl | 7 +++++ 4 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 test/Utils.pm create mode 100755 test/protoversion.pl diff --git a/test/Utils.pm b/test/Utils.pm new file mode 100644 index 0000000..967426d --- /dev/null +++ b/test/Utils.pm @@ -0,0 +1,21 @@ +package Utils; + +use strict; + + +sub harnessTypeToCmd { + my $harnessType = shift; + + if ($harnessType eq 'cpp') { + return './cpp/harness'; + } elsif ($harnessType eq 'js') { + return 'node js/harness.js'; + } elsif ($harnessType eq 'rust') { + return '../../rust-negentropy/target/debug/harness'; + } + + die "unknown harness type: $harnessType"; +} + + +1; diff --git a/test/fuzz.pl b/test/fuzz.pl index 2213ecc..f737632 100755 --- a/test/fuzz.pl +++ b/test/fuzz.pl @@ -5,26 +5,15 @@ $|++; use IPC::Open2; use Session::Token; +use FindBin; +use lib "$FindBin::Bin"; +use Utils; die "usage: $0 " if @ARGV < 2; -my $harnessCmd1 = harnessTypeToCmd(shift) || die "please provide harness type (cpp, js, etc)"; -my $harnessCmd2 = harnessTypeToCmd(shift) || die "please provide harness type (cpp, js, etc)"; +my $harnessCmd1 = Utils::harnessTypeToCmd(shift) || die "please provide harness type (cpp, js, etc)"; +my $harnessCmd2 = Utils::harnessTypeToCmd(shift) || die "please provide harness type (cpp, js, etc)"; my $idSize = shift || 16; -sub harnessTypeToCmd { - my $harnessType = shift; - - if ($harnessType eq 'cpp') { - return './cpp/harness'; - } elsif ($harnessType eq 'js') { - return 'node js/harness.js'; - } elsif ($harnessType eq 'rust') { - return '../../rust-negentropy/target/debug/harness'; - } - - die "unknown harness type: $harnessType"; -} - srand($ENV{SEED} || 0); my $stgen = Session::Token->new(seed => "\x00" x 1024, alphabet => '0123456789abcdef', length => $idSize * 2); diff --git a/test/protoversion.pl b/test/protoversion.pl new file mode 100755 index 0000000..fecba34 --- /dev/null +++ b/test/protoversion.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +use strict; +$|++; + +use IPC::Open2; +use Session::Token; +use FindBin; +use lib "$FindBin::Bin"; +use Utils; + +die "usage: $0 " if @ARGV < 1; +my $harnessCmd = Utils::harnessTypeToCmd(shift) || die "please provide harness type (cpp, js, etc)"; +my $idSize = shift || 16; + + +my $expectedResp; + +## Get expected response using protocol version 0 + +{ + my ($infile, $outfile); + my $pid = open2($outfile, $infile, $harnessCmd); + + print $infile "item,12345,eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n"; + print $infile "seal\n"; + print $infile "msg,6000000200\n"; ## full range bound, empty IdList + + my $resp = <$outfile>; + chomp $resp; + + $expectedResp = $resp; +} + +## Client tries to use some hypothetical newer version, but falls back to version 0 + +{ + my ($infile, $outfile); + my $pid = open2($outfile, $infile, $harnessCmd); + + print $infile "item,12345,eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\n"; + print $infile "seal\n"; + + print $infile "msg,61aabbccddeeff\n"; ## some new protocol message + + my $resp = <$outfile>; + chomp $resp; + + ## 61: The bound timestamp, as varint. The value is 0x60 (preferred protocol version), but 1 is added as per timestamp protocol + ## 00: The following ID has length 0 + ## 04: UnsupportedProtocolVersion mode + die "bad upgrade response: $resp" unless $resp eq "msg,610004"; + + ## Try again with protocol version 0 + print $infile "msg,6000000200\n"; ## full range bound, empty IdList + + $resp = <$outfile>; + chomp $resp; + die "didn't fall back to protocol version 0: $resp" unless $resp eq $expectedResp; +} + +print "OK\n"; diff --git a/test/test.pl b/test/test.pl index 65bdf3e..edb8e3d 100755 --- a/test/test.pl +++ b/test/test.pl @@ -63,6 +63,13 @@ if (@langs >= 2) { +## Protocol upgrade tests + +foreach my $lang (@langs) { + note("------- PROTO UPGRADE $lang -------"); + run("perl protoversion.pl $lang"); +} + ########