diff --git a/bin/check.py b/bin/check.py index b0ec7b44..8a12196b 100755 --- a/bin/check.py +++ b/bin/check.py @@ -26,10 +26,9 @@ import json import logging from yaml import load -from pprint import pprint try: from yaml import CLoader as Loader -except: +except ImportError: from yaml import Loader @@ -38,10 +37,10 @@ class XAvp: """ Class to simulate the xavp """ def __init__(self, name, data): - result = re.match('\$xavp\((\w+)\)', name) + result = re.match(r'\$xavp\((\w+)\)', name) try: self._name = result.group(1) - except: + except Exception: raise Exception('not a xavp') self._data = data @@ -74,9 +73,9 @@ class XAvp: @classmethod def parse(cls, str): - pattern_nindx = '(\[(?P<%s>\d+)\])?' % 'nindx' - pattern_kindx = '(\[(?P<%s>\d+|\*+)\])?' % 'kindx' - pattern = '\$xavp\((?P\w+)%s(=>(?P\w+)%s)?\)' % ( + pattern_nindx = r'(\[(?P<%s>\d+)\])?' % 'nindx' + pattern_kindx = r'(\[(?P<%s>\d+|\*+)\])?' % 'kindx' + pattern = r'\$xavp\((?P\w+)%s(=>(?P\w+)%s)?\)' % ( pattern_nindx, pattern_kindx) result = re.match(pattern, str) if result is not None: @@ -86,7 +85,7 @@ class XAvp: nindx = 0 try: kindx = int(result.group('kindx')) - except: + except Exception: if result.group('kindx') == '*': kindx = '*' else: @@ -134,7 +133,7 @@ class Test: elif isinstance(val0, int): try: result = (val0 == int(val1)) - except: + except Exception: result = False elif isinstance(val0, list) and isinstance(val1, list): size = len(val0) @@ -230,7 +229,7 @@ def check_flow(scen, check, test): (sk, sv) = scen[i].popitem() try: (ck, cv) = check[i].popitem() - except: + except Exception: test.error('wrong flow. Expected: %s but is nothing there' % sk) continue if(sk != ck): @@ -243,11 +242,11 @@ def check_flow(scen, check, test): test.ok('flow[%s]' % sk) check_flow_vars(sk, sv, cv, test) if(len(check) > len(scen)): - l = [] + line = [] for i in check: for k in i.keys(): - l.append(k) - test.error('Expected to end but there are more flows %s' % l) + line.append(k) + test.error('Expected to end but there are more flows %s' % line) def check_sip(scen, msg, test): @@ -346,7 +345,7 @@ def main(): try: check = load_check(args[1]) - except: + except Exception: check = {'flow': [], 'sip_in': '', 'sip_out': []} test.error("Error loading file:%s" % args[1]) @@ -360,5 +359,6 @@ def main(): if test.isError(): sys.exit(1) + if __name__ == "__main__": main() diff --git a/bin/generate_test_tt2.pl b/bin/generate_test_tt2.pl index 1555a0aa..1deb454a 100755 --- a/bin/generate_test_tt2.pl +++ b/bin/generate_test_tt2.pl @@ -41,6 +41,7 @@ sub usage $output .= "\t-F --filter-common: filter common headers:\n"; $output .= "\t\t@common_hdrs\n"; $output .= "\t-i --ids: subst ids present in scenarios_ids.yml file\n"; + $output .= "\t-n --network: subst sipp ports using networks defined at scenarios.yml file\n"; return $output } @@ -48,23 +49,46 @@ sub load_ids { my $yml = LoadFile($_[0]); my $res = {}; - get_uuid($yml, "", $res); + get_id($yml, "", $res, 'id'); return $res; } -sub get_uuid +sub load_uuids +{ + my $yml = LoadFile($_[0]); + my $res = {}; + get_id($yml, "", $res, 'uuid'); + return $res; +} + +sub load_network +{ + my $yml = LoadFile($_[0]); + my @res = (); + foreach (@{$yml->{scenarios}}) { + push @res, $_->{ip}; + foreach (@{$_->{responders}}) { + push @res, $_->{ip}; + } + } + @res = uniq(@res); + return \@res; +} + +sub get_id { my $data = shift; my $str_key = shift; my $res = shift; + my $id = shift; foreach my $key (sort keys %{$data}) { my $new_key = ${str_key} ? "${str_key}.${key}" : "${key}"; - if($key eq 'uuid') { + if($key eq $id) { $res->{$data->{$key}} = $new_key; } else { my $_type = defined(reftype($data->{$key})) ? reftype($data->{$key}) : ""; if( $_type eq 'HASH') { - get_uuid($data->{$key}, $new_key, $res); + get_id($data->{$key}, $new_key, $res, $id); } } } @@ -72,6 +96,7 @@ sub get_uuid } my $uuids; +my $ids; sub subst_uuids { my $line = shift; @@ -84,6 +109,17 @@ sub subst_uuids return $line; } +sub subst_ids +{ + my $line = shift; + foreach my $id (sort keys %{$ids}) { + if($line =~ s/(=|: )\Q${id}\E([^\d]?)/${1}[% $ids->{$id} %]${2}/g) { + return $line; + } + } + return $line; +} + sub subst_common { my $line = shift; @@ -91,14 +127,43 @@ sub subst_common $line =~ s/;tag=(.+)/;tag=[\\w-]+/; } elsif($line =~ /^CSeq: /i) { $line =~ s/:[ ]+\d+[ ]+/: \\d+ /; - } elsif($line =~ /^WWW-Authenticate: /i) { - $line =~ s/nonce=".+"/nonce=".+"/; - } elsif($line =~ /^Server: Sipwise/i) { - $line =~ s/^: Sipwise .+/: Sipwise NGCP Proxy/; + } elsif($line =~ /^(WWW|Proxy)-Authenticate: /i) { + $line =~ s/nonce="[^"]+"/nonce=".+"/; + } elsif($line =~ /^(WWW|Proxy)-Authorization: /i) { + $line =~ s/response="[^"]+"/response=".+"/; + $line =~ s/nonce="[^"]+"/nonce=".+"/; + } elsif($line =~ /^(Server|User-Agent): Sipwise/i) { + $line =~ s/: Sipwise NGCP (Proxy|PBX) Server.+/: Sipwise NGCP ${1} Server/; } elsif($line =~ /^Content-Length:[ ]+[1-9]/i) { - $line =~ s/:[ ]+\d+/: \\d+/; + $line =~ s/:[ ]+\d+/:\\s+\\d+/; } elsif($line =~ /^P-LB-Uptime: /i) { $line =~ s/: \d+/: \\d+/; + } elsif($line =~ /^P-NGCP-Src-Port: /i) { + $line =~ s/: \d+/: \\d+/; + } elsif($line =~ /^P-NGCP-Acc-(Src|Dst)-Leg: /i) { + $line =~ s/: .+/: .+/; + } elsif($line =~ /^SIP-If-Match: /i) { + $line =~ s/: .+/: .+/; + } elsif($line =~ /127\.0\.0\.1(:|;port=)508[58]/) { + $line =~ s/127\.0\.0\.1(:|;port=)\d+/127.0.0.1${1}508[58]/g; + } elsif($line =~ /^Content-Type: application\/dialog\-info\+xml/i) { + $line =~ s/: application\/dialog\-info\+xml/: application\/dialog\\-info\\+xml/; + } + return $line; +} + +my $network; +sub subst_network +{ + my $line = shift; + if ($line =~ /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/) { + foreach my $ip (@{$network}) { + if($line =~ s/\Q${ip}\E:\d+/${ip}:\\d+/g) { + return $line; + } elsif($line =~ s/ip=\Q${ip}\E;port=\d+/ip=${ip};port=\\d+/g) { + return $line; + } + } } return $line; } @@ -112,7 +177,13 @@ sub print_header if($uuids) { $line = subst_uuids($_l); } + if($ids) { + $line = subst_ids($line); + } $line = subst_common($line); + if($network) { + $line = subst_network($line); + } if($_type eq 'sip_in') { print " - '$line'\n"; } else { @@ -135,11 +206,13 @@ sub filter_header my $help = 0; my $f_common = 0; my $f_ids; +my $f_net; GetOptions ( "h|help" => \$help, "f|filter=s" => \@headers, "F|filter-common" => \$f_common, "i|ids=s" => \$f_ids, + "n|network=s" => \$f_net, ) or die("Error in command line arguments\n".usage()); if($#ARGV!=0 || $help) @@ -162,7 +235,12 @@ my $json; my $inlog = decode_json($json); if($f_ids) { - $uuids = load_ids(abs_path($f_ids)); + $uuids = load_uuids(abs_path($f_ids)); + $ids = load_ids(abs_path($f_ids)); +} + +if($f_net) { + $network = load_network(abs_path($f_net)); } print "flow:\n"; @@ -189,21 +267,25 @@ foreach my $i (@{$inlog->{'sip_in'}}) } } } -print "sip_out:\n"; -foreach my $i (@{$inlog->{'sip_out'}}) -{ - my @line = split(/\r\n/, $i); - print " - [\n"; - foreach my $l (@line) +if(@{$inlog->{'sip_out'}}) { + print "sip_out:\n"; + foreach my $i (@{$inlog->{'sip_out'}}) { - if($l) { - if(!filter_header($l)) { - print_header('sip_out', $l); + my @line = split(/\r\n/, $i); + print " - [\n"; + foreach my $l (@line) + { + if($l) { + if(!filter_header($l)) { + print_header('sip_out', $l); + } + } else { + # we don't care about SDP + last; } - } else { - # we don't care about SDP - last; } + print " ]\n"; } - print " ]\n"; -} +} else { + print "sip_out: []\n"; +} \ No newline at end of file