From 11549cc3a2a3a3692d1c39c1bc04f1fe48619cc3 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 10 Dec 2021 15:25:00 -0500 Subject: [PATCH] TT#144701 support JSON in tests and CLI tool Change-Id: Idd1b413d720b7be8f36849660c547332a344a698 --- debian/control | 1 + perl/NGCP/Rtpengine.pm | 5 +++-- perl/NGCP/Rtpengine/AutoTest.pm | 7 ++++++- t/auto-daemon-tests-pubsub.pl | 1 + utils/rtpengine-ng-client | 11 ++++++++++- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/debian/control b/debian/control index 9609f152a..76586e68c 100644 --- a/debian/control +++ b/debian/control @@ -27,6 +27,7 @@ Build-Depends: libio-socket-ip-perl, libiptc-dev, libjson-glib-dev, + libjson-perl, libmosquitto-dev, libnet-interface-perl, libpcap0.8-dev, diff --git a/perl/NGCP/Rtpengine.pm b/perl/NGCP/Rtpengine.pm index 804c4607e..c6ce33c28 100644 --- a/perl/NGCP/Rtpengine.pm +++ b/perl/NGCP/Rtpengine.pm @@ -8,6 +8,7 @@ use IO::Socket; use IO::Socket::IP; use Bencode; use Data::Dumper; +use JSON; sub new { my ($class, $addr, $port) = @_; @@ -30,12 +31,12 @@ sub req { my ($self, $packet) = @_; my $cookie = rand() . ' '; - my $p = $cookie . Bencode::bencode($packet); + my $p = $cookie . ($self->{json} ? encode_json($packet) : Bencode::bencode($packet)); $self->{socket}->send($p, 0) or die $!; my $ret; $self->{socket}->recv($ret, 65535) or die $!; $ret =~ s/^\Q$cookie\E//s or die $ret; - my $resp = Bencode::bdecode($ret, 1); + my $resp = $self->{json} ? decode_json($ret) : Bencode::bdecode($ret, 1); $resp->{result} or die Dumper $resp; diff --git a/perl/NGCP/Rtpengine/AutoTest.pm b/perl/NGCP/Rtpengine/AutoTest.pm index cc08b1ce1..3bafd2f87 100644 --- a/perl/NGCP/Rtpengine/AutoTest.pm +++ b/perl/NGCP/Rtpengine/AutoTest.pm @@ -21,7 +21,7 @@ BEGIN { @ISA = qw(Exporter); our @EXPORT = qw(autotest_start new_call offer answer ft tt snd srtp_snd rtp rcv srtp_rcv srtp_dec escape rtpm rtpmre reverse_tags new_ft new_tt crlf sdp_split rtpe_req offer_answer - autotest_init subscribe_request subscribe_answer publish); + autotest_init subscribe_request subscribe_answer publish use_json); }; @@ -290,6 +290,11 @@ sub terminate { die "error: $msg\n"; } +sub use_json { + my $bool = shift; + $c->{json} = $bool; +} + END { if ($rtpe_pid) { diff --git a/t/auto-daemon-tests-pubsub.pl b/t/auto-daemon-tests-pubsub.pl index 687626844..ea66e8fcc 100755 --- a/t/auto-daemon-tests-pubsub.pl +++ b/t/auto-daemon-tests-pubsub.pl @@ -24,6 +24,7 @@ my ($sock_a, $sock_b, $sock_c, $sock_d, $port_a, $port_b, $port_c, $ssrc_a, $ssr +use_json(1); new_call; diff --git a/utils/rtpengine-ng-client b/utils/rtpengine-ng-client index e6369d706..7c93e95ef 100755 --- a/utils/rtpengine-ng-client +++ b/utils/rtpengine-ng-client @@ -12,6 +12,7 @@ use NGCP::Rtpengine; my %options = ('proxy-address' => 'localhost', 'proxy-port' => 2223); GetOptions( + 'json' => \$options{'json'}, 'proxy-address=s' => \$options{'proxy-address'}, 'proxy-port=s' => \$options{'proxy-port'}, 'from-tag=s' => \$options{'from-tag'}, @@ -109,7 +110,14 @@ my $cmd = shift(@ARGV) or die; my %packet = (command => $cmd); for my $x (split(/,/, 'from-tag,to-tag,call-id,transport protocol,media address,ICE,address family,DTLS,via-branch,media address,ptime,xmlrpc-callback,metadata,address,file,db-id,code,DTLS-fingerprint,ICE-lite,media echo,label,set-label,from-label,to-label')) { - defined($options{$x}) and $packet{$x} = \$options{$x}; + if (defined($options{$x})) { + if (!$options{json}) { + $packet{$x} = \$options{$x}; + } + else { + $packet{$x} = $options{$x}; + } + } } for my $x (split(/,/, 'TOS,delete-delay')) { defined($options{$x}) and $packet{$x} = $options{$x}; @@ -190,6 +198,7 @@ if (defined($packet{sdp})) { } my $engine = NGCP::Rtpengine->new($options{'proxy-address'}, $options{'proxy-port'}); +$engine->{json} = $options{json}; my $resp = $engine->req(\%packet); #print Dumper $resp;