From: nwf Date: Wed, 27 Aug 2008 07:15:22 +0000 (-0400) Subject: Make Protoutils decoder frame correctly, make test suite more paranoid X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=52ef540048e95e76fa178b605f77eb8a73743a20;p=instirc Make Protoutils decoder frame correctly, make test suite more paranoid darcs-hash:20080827071522-4d648-3af2a6541cee43d27a03d0de019c6a80c6b9b892.gz --- diff --git a/Protoutils.pm b/Protoutils.pm index 12c5624..9364121 100644 --- a/Protoutils.pm +++ b/Protoutils.pm @@ -43,8 +43,21 @@ sub tlvs_to_message($){ sub run_callbacks($$$) { my ($coder, $cbs, $msg) = @_; - return if (0 ne index $msg, $MESSAGE_START); - $msg = substr($msg, length $MESSAGE_START); + my $regex = "^$MESSAGE_START([" + . (join ("",@{$$coder{'code_chars'}})) + ."]+)$MESSAGE_END(.*)\$"; + + my $rest; + # Note how this regex works: it will greedily consume into + # what we think is the message, and may have to backtrack out + # to find MESSAGE_END. It will never prematurely terminate + # the encoded message if it sees MESSAGE_END inside the message. + if ( $msg =~ /$regex/ ) { + $msg = $1; + $rest = $2; + } else { + return (0, $msg); + } while( $msg ne "" ) { @@ -68,7 +81,7 @@ sub run_callbacks($$$) { } } - return $msg eq ""; + return ($msg eq "", $rest); } sub dump_message($$) { diff --git a/Protoutils_test.pl b/Protoutils_test.pl index 08913fa..73c53ae 100644 --- a/Protoutils_test.pl +++ b/Protoutils_test.pl @@ -23,7 +23,8 @@ foreach my $ts (@test_strings) { print "ENCODED '", $ts, "' into hc ", $enc, " and tlv ", $tlv, "\n"; } -my $mesg = tlvs_to_message(\@tlvs); +my $mesg_suffix = "This is some normal text."; +my $mesg = tlvs_to_message(\@tlvs) . $mesg_suffix; dump_message($mc, $mesg); @@ -44,8 +45,11 @@ sub def_cb ($$) { die "Unanticipated message of type $t: $v"; } -run_callbacks($mc, +my ($res, $rest) = run_callbacks($mc, { 'default' => \&def_cb, $known_types{'InstanceLabelHuffman1'} => \&ilf_cb, }, $mesg ); + +die unless $res eq 1; +die unless $rest eq $mesg_suffix;