瀏覽代碼

test/unit: add unit test for sdpops script function sdp_remove_line_by_prefix()

Mikko Lehto 9 年之前
父節點
當前提交
83d44c6472

+ 18 - 0
test/unit/60-message-sdp0.sip

@@ -0,0 +1,18 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test0 - invalid SDP
+
+v=0
+oo=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+

+ 18 - 0
test/unit/60-message-sdp1.sip

@@ -0,0 +1,18 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test1 - no matching line
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+

+ 19 - 0
test/unit/60-message-sdp2.sip

@@ -0,0 +1,19 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test2 - single matching line
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=X-cap: 1 audio RTP/AVP 100
+a=rtpmap:100 X-NSE/8000
+

+ 19 - 0
test/unit/60-message-sdp3.sip

@@ -0,0 +1,19 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test3 - single matching line at the end of buffer
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+a=X-cap: 1 audio RTP/AVP 100
+

+ 20 - 0
test/unit/60-message-sdp4.sip

@@ -0,0 +1,20 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test4 - dual matching lines
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+a=X-cap1: 1 audio RTP/AVP 100
+a=X-cap2: 1 audio RTP/AVP 100
+

+ 20 - 0
test/unit/60-message-sdp5.sip

@@ -0,0 +1,20 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test5 - dual matching lines with gap
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=X-cap1: 1 audio RTP/AVP 100
+a=rtpmap:100 X-NSE/8000
+a=X-cap2: 1 audio RTP/AVP 100
+

+ 20 - 0
test/unit/60-message-sdp6.sip

@@ -0,0 +1,20 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Info: sdpops remove_line_by_prefix() test6 - matching line + end of buffer
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+a=X-cap1: 1 audio RTP/AVP 100
+a=X
+

+ 19 - 0
test/unit/60-message-sdp7.sip

@@ -0,0 +1,19 @@
+MESSAGE sip:[email protected] SIP/2.0
+From: sip:[email protected];tag=45dfdf49
+To: sip:[email protected]
+Call-ID: 1172299593a
+CSeq: 1 MESSAGE
+Content-Type: application/sdp
+Max-Forwards: 2
+X-Case: 60-test7
+X-Info: sdpops remove_line_by_prefix() test7 - change s -line
+
+v=0
+o=- 370010 0 IN IP4 192.168.13.31
+s=MGW
+c=IN IP4 192.168.13.31
+t=0 0
+m=audio 22616 RTP/AVP 0 96 100
+a=rtpmap:96 telephone-event/8000
+a=rtpmap:100 X-NSE/8000
+

+ 41 - 0
test/unit/60.cfg

@@ -0,0 +1,41 @@
+# ----------- global configuration parameters ------------------------
+debug=2
+fork=yes
+log_stderror=no
+children=1
+disable_tcp=yes
+listen=udp:127.0.0.1:5060
+auto_aliases=no
+alias=example.invalid
+# ------------------ module loading ----------------------------------
+loadpath "../../modules/"
+
+loadmodule "tm.so"
+loadmodule "sl.so"
+loadmodule "pv.so"
+loadmodule "textops.so"
+loadmodule "textopsx.so"
+loadmodule "sdpops.so"
+
+route {
+	if ( is_present_hf("X-Case") ) {
+		if ($hdr(X-Case) == '60-test7') {
+			sdp_remove_line_by_prefix("s=");
+			msg_apply_changes();
+			set_reply_body($rb,"application/sdp");
+			sl_send_reply(200,"OK");
+			exit;
+		}
+	} else {
+		$var(rc) = sdp_remove_line_by_prefix("a=X-cap");
+		if ( $var(rc) < 0 ) {
+			sl_send_reply(500,"Some error");
+			exit;
+		}
+		msg_apply_changes();
+		set_reply_body($rb,"application/sdp");
+		sl_send_reply(200,"OK");
+		exit;
+	}
+}
+

+ 75 - 0
test/unit/60.sh

@@ -0,0 +1,75 @@
+#!/bin/sh
+# checks sdpops module function remove_line_by_prefix()
+#
+# Copyright (C) 2016 [email protected]
+#
+# This file is part of Kamailio, a free SIP server.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. include/common
+. include/require.sh
+
+CFG=60.cfg
+TMPFILE=$(mktemp -t kamailio-test.XXXXXXXXXX)
+
+if ! (check_sipsak && check_kamailio && check_module "sdpops"); then
+	exit 0
+fi
+
+${BIN} -w . -f ${CFG} > /dev/null
+ret=$?
+
+sleep 1
+if [ "${ret}" -ne 0 ] ; then
+	echo "start fail"
+	${KILL}
+	exit ${ret}
+fi
+
+# Borken SDP should give 500 response
+sipsak -f 60-message-sdp0.sip -L -s sip:127.0.0.1 -v > ${TMPFILE}
+ret=$?
+if [ "${ret}" -eq 1 ] ; then
+	ret=0
+else
+	echo "invalid SDP not rejected"
+	ret=1
+	exit ${ret}
+fi
+
+# Kamailio replies back with modified SDP
+for i in 1 2 3 4 5 6 7; do
+	FILE="60-message-sdp${i}.sip"
+	TOTALBEFORE=$(awk '/^v=0/,/^$/ {total++; if ($0 ~ /^a=X-cap/ ) { prefix++;} else { other++} } END {if (prefix) {print other " + " prefix} else { print other " + 0"} }' ${FILE})
+	OTHERBEFORE=$(echo ${TOTALBEFORE}|cut -d+ -f1)
+
+	sipsak -f ${FILE} -L -s sip:127.0.0.1 -v > ${TMPFILE}
+	ret=$?
+	if [ "${ret}" -eq 0 ] ; then
+		TOTALAFTER=$(awk '/^v=0/,/^$/ {total++; if ($0 ~ /^a=X-cap/ ) { prefix++;} else { other++} } END {if (prefix) {print other " + " prefix} else { print other " + 0"} }' ${TMPFILE})
+		OTHERAFTER=$(echo ${TOTALBEFORE}|cut -d+ -f1)
+		PREFIXAFTER=$(echo ${TOTALAFTER}|cut -d+ -f2)
+		if [ ${PREFIXAFTER} -eq 0 ] && [ ${OTHERBEFORE} -eq ${OTHERAFTER} ]; then
+			ret=0
+		else
+			ret=1
+			echo "found ${PREFIXAFTER} lines that should be deleted (${FILE})"
+		fi
+	fi
+done
+
+${KILL}
+
+exit ${ret}
+

+ 65 - 0
test/unit/include/require.sh

@@ -0,0 +1,65 @@
+# Copyright (C) 2008 1&1 Internet AG
+# Copyright (C) 2016 Mikko Lehto
+#
+# This file is part of kamailio, a free SIP server.
+#
+# kamailio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version
+#
+# kamailio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+. include/common
+
+check_kamailio() {
+	if ! (test -e $BIN) ; then
+		echo "kamailio not found, not run"
+		return 1
+	fi;
+	return 0
+}
+
+check_module() {
+	if [ $# -ne 1 ]; then
+		echo "wrong number of params in check_module()"
+		return 1
+	fi
+
+	if ! (test -e $SRC_DIR/modules/$1/$1.so) ; then
+		echo "$SRC_DIR/modules/$1/$1.so not found, not run"
+		return 1
+	fi;
+	return 0
+}
+
+check_netcat() {
+	if ! ( which nc > /dev/null ); then
+		echo "netcat not found, not run"
+		return 1
+	fi;
+	return 0
+}
+
+check_sipp() {
+	if ! ( which sipp > /dev/null ); then
+		echo "sipp not found, not run"
+		return 1
+	fi;
+	return 0
+}
+
+check_sipsak() {
+	if ! ( which sipsak > /dev/null ); then
+		echo "sipsak not found, not run"
+		return 1
+	fi;
+	return 0
+}