Browse code

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

Mikko Lehto authored on 29/02/2016 14:21:25
Showing 11 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test0 - invalid SDP
9
+
10
+v=0
11
+oo=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=rtpmap:100 X-NSE/8000
18
+
0 19
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test1 - no matching line
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=rtpmap:100 X-NSE/8000
18
+
0 19
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test2 - single matching line
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=X-cap: 1 audio RTP/AVP 100
18
+a=rtpmap:100 X-NSE/8000
19
+
0 20
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test3 - single matching line at the end of buffer
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=rtpmap:100 X-NSE/8000
18
+a=X-cap: 1 audio RTP/AVP 100
19
+
0 20
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test4 - dual matching lines
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=rtpmap:100 X-NSE/8000
18
+a=X-cap1: 1 audio RTP/AVP 100
19
+a=X-cap2: 1 audio RTP/AVP 100
20
+
0 21
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test5 - dual matching lines with gap
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=X-cap1: 1 audio RTP/AVP 100
18
+a=rtpmap:100 X-NSE/8000
19
+a=X-cap2: 1 audio RTP/AVP 100
20
+
0 21
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Info: sdpops remove_line_by_prefix() test6 - matching line + end of buffer
9
+
10
+v=0
11
+o=- 370010 0 IN IP4 192.168.13.31
12
+s=MGW
13
+c=IN IP4 192.168.13.31
14
+t=0 0
15
+m=audio 22616 RTP/AVP 0 96 100
16
+a=rtpmap:96 telephone-event/8000
17
+a=rtpmap:100 X-NSE/8000
18
+a=X-cap1: 1 audio RTP/AVP 100
19
+a=X
20
+
0 21
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+MESSAGE sip:bob@example.invalid SIP/2.0
2
+From: sip:alice@example.invalid;tag=45dfdf49
3
+To: sip:bob@example.invalid
4
+Call-ID: 1172299593a
5
+CSeq: 1 MESSAGE
6
+Content-Type: application/sdp
7
+Max-Forwards: 2
8
+X-Case: 60-test7
9
+X-Info: sdpops remove_line_by_prefix() test7 - change s -line
10
+
11
+v=0
12
+o=- 370010 0 IN IP4 192.168.13.31
13
+s=MGW
14
+c=IN IP4 192.168.13.31
15
+t=0 0
16
+m=audio 22616 RTP/AVP 0 96 100
17
+a=rtpmap:96 telephone-event/8000
18
+a=rtpmap:100 X-NSE/8000
19
+
0 20
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+# ----------- global configuration parameters ------------------------
2
+debug=2
3
+fork=yes
4
+log_stderror=no
5
+children=1
6
+disable_tcp=yes
7
+listen=udp:127.0.0.1:5060
8
+auto_aliases=no
9
+alias=example.invalid
10
+# ------------------ module loading ----------------------------------
11
+loadpath "../../modules/"
12
+
13
+loadmodule "tm.so"
14
+loadmodule "sl.so"
15
+loadmodule "pv.so"
16
+loadmodule "textops.so"
17
+loadmodule "textopsx.so"
18
+loadmodule "sdpops.so"
19
+
20
+route {
21
+	if ( is_present_hf("X-Case") ) {
22
+		if ($hdr(X-Case) == '60-test7') {
23
+			sdp_remove_line_by_prefix("s=");
24
+			msg_apply_changes();
25
+			set_reply_body($rb,"application/sdp");
26
+			sl_send_reply(200,"OK");
27
+			exit;
28
+		}
29
+	} else {
30
+		$var(rc) = sdp_remove_line_by_prefix("a=X-cap");
31
+		if ( $var(rc) < 0 ) {
32
+			sl_send_reply(500,"Some error");
33
+			exit;
34
+		}
35
+		msg_apply_changes();
36
+		set_reply_body($rb,"application/sdp");
37
+		sl_send_reply(200,"OK");
38
+		exit;
39
+	}
40
+}
41
+
0 42
new file mode 100755
... ...
@@ -0,0 +1,75 @@
1
+#!/bin/sh
2
+# checks sdpops module function remove_line_by_prefix()
3
+#
4
+# Copyright (C) 2016 mslehto@iki.fi
5
+#
6
+# This file is part of Kamailio, a free SIP server.
7
+#
8
+# Permission to use, copy, modify, and distribute this software for any
9
+# purpose with or without fee is hereby granted, provided that the above
10
+# copyright notice and this permission notice appear in all copies.
11
+#
12
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
+
20
+. include/common
21
+. include/require.sh
22
+
23
+CFG=60.cfg
24
+TMPFILE=$(mktemp -t kamailio-test.XXXXXXXXXX)
25
+
26
+if ! (check_sipsak && check_kamailio && check_module "sdpops"); then
27
+	exit 0
28
+fi
29
+
30
+${BIN} -w . -f ${CFG} > /dev/null
31
+ret=$?
32
+
33
+sleep 1
34
+if [ "${ret}" -ne 0 ] ; then
35
+	echo "start fail"
36
+	${KILL}
37
+	exit ${ret}
38
+fi
39
+
40
+# Borken SDP should give 500 response
41
+sipsak -f 60-message-sdp0.sip -L -s sip:127.0.0.1 -v > ${TMPFILE}
42
+ret=$?
43
+if [ "${ret}" -eq 1 ] ; then
44
+	ret=0
45
+else
46
+	echo "invalid SDP not rejected"
47
+	ret=1
48
+	exit ${ret}
49
+fi
50
+
51
+# Kamailio replies back with modified SDP
52
+for i in 1 2 3 4 5 6 7; do
53
+	FILE="60-message-sdp${i}.sip"
54
+	TOTALBEFORE=$(awk '/^v=0/,/^$/ {total++; if ($0 ~ /^a=X-cap/ ) { prefix++;} else { other++} } END {if (prefix) {print other " + " prefix} else { print other " + 0"} }' ${FILE})
55
+	OTHERBEFORE=$(echo ${TOTALBEFORE}|cut -d+ -f1)
56
+
57
+	sipsak -f ${FILE} -L -s sip:127.0.0.1 -v > ${TMPFILE}
58
+	ret=$?
59
+	if [ "${ret}" -eq 0 ] ; then
60
+		TOTALAFTER=$(awk '/^v=0/,/^$/ {total++; if ($0 ~ /^a=X-cap/ ) { prefix++;} else { other++} } END {if (prefix) {print other " + " prefix} else { print other " + 0"} }' ${TMPFILE})
61
+		OTHERAFTER=$(echo ${TOTALBEFORE}|cut -d+ -f1)
62
+		PREFIXAFTER=$(echo ${TOTALAFTER}|cut -d+ -f2)
63
+		if [ ${PREFIXAFTER} -eq 0 ] && [ ${OTHERBEFORE} -eq ${OTHERAFTER} ]; then
64
+			ret=0
65
+		else
66
+			ret=1
67
+			echo "found ${PREFIXAFTER} lines that should be deleted (${FILE})"
68
+		fi
69
+	fi
70
+done
71
+
72
+${KILL}
73
+
74
+exit ${ret}
75
+
0 76
new file mode 100644
... ...
@@ -0,0 +1,65 @@
1
+# Copyright (C) 2008 1&1 Internet AG
2
+# Copyright (C) 2016 Mikko Lehto
3
+#
4
+# This file is part of kamailio, a free SIP server.
5
+#
6
+# kamailio is free software; you can redistribute it and/or modify
7
+# it under the terms of the GNU General Public License as published by
8
+# the Free Software Foundation; either version 2 of the License, or
9
+# (at your option) any later version
10
+#
11
+# kamailio is distributed in the hope that it will be useful,
12
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+# GNU General Public License for more details.
15
+#
16
+# You should have received a copy of the GNU General Public License
17
+# along with this program; if not, write to the Free Software
18
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
+
20
+. include/common
21
+
22
+check_kamailio() {
23
+	if ! (test -e $BIN) ; then
24
+		echo "kamailio not found, not run"
25
+		return 1
26
+	fi;
27
+	return 0
28
+}
29
+
30
+check_module() {
31
+	if [ $# -ne 1 ]; then
32
+		echo "wrong number of params in check_module()"
33
+		return 1
34
+	fi
35
+
36
+	if ! (test -e $SRC_DIR/modules/$1/$1.so) ; then
37
+		echo "$SRC_DIR/modules/$1/$1.so not found, not run"
38
+		return 1
39
+	fi;
40
+	return 0
41
+}
42
+
43
+check_netcat() {
44
+	if ! ( which nc > /dev/null ); then
45
+		echo "netcat not found, not run"
46
+		return 1
47
+	fi;
48
+	return 0
49
+}
50
+
51
+check_sipp() {
52
+	if ! ( which sipp > /dev/null ); then
53
+		echo "sipp not found, not run"
54
+		return 1
55
+	fi;
56
+	return 0
57
+}
58
+
59
+check_sipsak() {
60
+	if ! ( which sipsak > /dev/null ); then
61
+		echo "sipsak not found, not run"
62
+		return 1
63
+	fi;
64
+	return 0
65
+}