2012-07-03 07:15:03 +00:00
#!/bin/bash
# Tests for Serval DNA server operations.
#
2013-02-06 08:25:31 +00:00
# Copyright 2012 Serval Project, Inc.
2012-07-03 07:15:03 +00:00
#
# This program 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.
#
# This program 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.
source "${0%/*}/../testframework.sh"
source "${0%/*}/../testdefs.sh"
setup() {
setup_servald
assert_no_servald_processes
2012-07-23 08:59:57 +00:00
setup_dnahelper
start_servald_instances +A
2012-09-14 05:34:05 +00:00
assert_all_instance_peers_complete +A
2012-07-03 07:15:03 +00:00
}
2012-10-17 07:40:39 +00:00
finally() {
2012-07-04 04:42:36 +00:00
stop_all_servald_servers
2012-10-17 07:40:39 +00:00
}
teardown() {
2012-07-03 07:15:03 +00:00
kill_all_servald_processes
assert_no_servald_processes
2012-10-17 07:40:39 +00:00
report_all_servald_servers
2012-07-03 07:15:03 +00:00
}
2012-07-04 04:42:36 +00:00
# Called by start_servald_instances immediately before starting the server
# process in each instance.
configure_servald_server() {
2012-12-06 02:01:19 +00:00
executeOk_servald config \
set log.show_pid on \
set log.show_time on \
set debug.dnahelper on \
set dna.helper.executable "$dnahelper" \
set dna.helper.argv.1 "Hello," \
set dna.helper.argv.2 "World!"
2012-07-04 04:42:36 +00:00
}
setup_dnahelper() {
2012-07-24 01:59:07 +00:00
export SID_JOE_A=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEA
export SID_JOE_B=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEB
export SID_JOE_C=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEC
export SID_JOE_D=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDED
export SID_JOE_E=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEE
export SID_JOE_F=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF
export SID_ECCLES=1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDE0
2012-07-18 09:46:30 +00:00
dnahelper="$TFWTMP/dnahelper"
2012-09-07 06:12:20 +00:00
echo "#!$BASH" >"$dnahelper"
2012-09-06 07:07:41 +00:00
cat >>"$dnahelper" <<'EOF'
2012-07-23 02:53:17 +00:00
echo STARTED
while read line
do
token="${line%%|*}"
line="${line#*|}"
did="${line%%|*}"
line="${line#*|}"
case "$token|$did|$line" in
'|'*'|')
echo "empty token" >&2
;;
*'||')
echo "empty DID" >&2
;;
*'|00000|')
2012-07-24 01:59:07 +00:00
# For verification during setup
2012-07-23 02:53:17 +00:00
echo "$token|A|$did|B|"
;;
*'|00001|')
2012-07-24 01:59:07 +00:00
# One valid reply
echo "$token|sip://$SID_JOE_A@10.1.1.1|$did|Joe A. Bloggs|"
2012-07-23 02:53:17 +00:00
;;
*'|00002|')
2012-07-24 01:59:07 +00:00
# Two valid replies
echo "$token|sip://$SID_JOE_A@10.1.1.1|$did|Joe A. Bloggs|"
2012-07-23 02:53:17 +00:00
sleep 0.1
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_JOE_B@10.1.1.1|$did|Joe B. Bloggs|"
2012-07-23 02:53:17 +00:00
sleep 0.1
;;
*'|00003|')
2012-07-24 01:59:07 +00:00
# Three valid replies
echo "$token|sip://$SID_JOE_A@10.1.1.1|$did|Joe A. Bloggs|"
2012-07-23 02:53:17 +00:00
sleep 0.1
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_JOE_B@10.1.1.1|$did|Joe B. Bloggs|"
2012-07-23 02:53:17 +00:00
sleep 0.1
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_JOE_C@10.1.1.1|$did|Joe C. Bloggs|"
2012-07-23 02:53:17 +00:00
sleep 0.1
;;
*'|00004|')
# Empty URI
echo "$token||$did|Eccles|"
;;
2012-07-23 08:59:57 +00:00
*'|000051|')
# Malformed URI
echo "$token|Bluebottle|$did|Eccles|"
;;
*'|000052|')
# Malformed URI
echo "$token|sip://Sea goon|$did|Eccles|"
;;
*'|000053|')
# Malformed URI
echo "$token|sip:|$did|Eccles|"
;;
*'|000061|')
# Mismatched token
2012-07-24 01:59:07 +00:00
echo "$SID_ECCLES|did://$SID_ECCLES/$did|$did|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000062|')
# Empty token
2012-07-24 01:59:07 +00:00
echo "|did://$SID_ECCLES/$did|$did|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000063|')
2012-07-24 01:59:07 +00:00
# Invalid token (not a SID)
echo "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEX|did://$SID_ECCLES/$did|$did|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000064|')
# Long token
2012-07-24 01:59:07 +00:00
echo "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF0|did://$SID_ECCLES/$did|$did|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000065|')
# Short token
2012-07-24 01:59:07 +00:00
echo "1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDE|did://$SID_ECCLES/$did|$did|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000071|')
# Mismatched DID
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|99999|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000072|')
# Empty DID
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did||Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000073|')
# Invalid DID
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|9999X|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000074|')
# Long DID
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|123456789012345678901234567890123|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000075|')
# Short DID
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|9999|Eccles|"
2012-07-23 08:59:57 +00:00
;;
*'|000081|')
# Malformed reply, missing final delimiter
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|9999|Eccles"
2012-07-23 08:59:57 +00:00
;;
*'|000082|')
# Malformed reply, long name
2012-07-24 01:59:07 +00:00
echo "$token|sip://$SID_ECCLES/$did|9999|Abcd efgh ijkl mnop qrst uvwx yzab cdef ghij klmn opqr stuv wxyz abcd efgh ijkl|"
2012-07-23 08:59:57 +00:00
;;
*'|000083|')
# Malformed reply, empty line
echo
;;
*'|000084|')
2012-07-24 01:59:07 +00:00
# Malformed reply, missing \n (which swallows the following DONE line)
echo -n "$token|sip://$SID_JOE_A@10.1.1.1|$did|Joe A. Bloggs|"
2012-07-23 08:59:57 +00:00
;;
*'|000085|')
# Malformed reply, line too long
for i in 1 2 3 4 5 6 7 8 9 0; do
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
echo -n 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
done
echo
;;
*'|00009|')
2012-07-24 01:59:07 +00:00
# Take too long to respond
sleep 2
echo "$token|sip://$SID_JOE_D@10.1.1.1|$did|Joe D. Bloggs|"
;;
*'|00010|')
# Spurious output after DONE
echo "$token|sip://$SID_JOE_E@10.1.1.1|$did|Joe E. Bloggs|"
echo DONE
echo "$token|sip://$SID_JOE_F@10.1.1.1|$did|Joe F. Bloggs|"
;;
*'|00011|')
2012-07-23 08:59:57 +00:00
# Die unexpectedly
echo "goodbye cruel world" >&2
exit 42
;;
2012-07-23 02:53:17 +00:00
*'|'*'|')
2012-07-23 08:59:57 +00:00
echo "token=$token did=$did line=$line" >&2
2012-07-23 02:53:17 +00:00
;;
*)
echo "garbage line" >&2
;;
esac
echo DONE
done
2012-07-03 07:15:03 +00:00
EOF
2012-07-18 09:46:30 +00:00
chmod 0755 "$dnahelper"
2012-07-19 08:29:45 +00:00
executeOk "$dnahelper" <<EOF
2012-07-23 02:53:17 +00:00
ToKeN|00000|
2012-07-19 08:29:45 +00:00
EOF
2012-07-23 02:53:17 +00:00
assertStdoutIs -e "STARTED\nToKeN|A|00000|B|\nDONE\n"
}
2012-07-19 08:29:45 +00:00
doc_ExecError="Non-existent DNA helper executable"
setup_ExecError() {
2012-07-23 08:59:57 +00:00
setup_servald
assert_no_servald_processes
2012-07-19 08:29:45 +00:00
dnahelper=/non/existent
assert [ ! -e "$dnahelper" ]
start_servald_instances +A
2012-09-14 05:34:05 +00:00
assert_all_instance_peers_complete +A
2012-07-19 08:29:45 +00:00
}
test_ExecError() {
executeOk_servald dna lookup 12345
2012-07-03 07:15:03 +00:00
}
2012-12-05 04:21:38 +00:00
doc_ExecArgs="DNA helper configured argument"
setup_ExecArgs() {
2012-08-02 07:41:36 +00:00
setup_servald
assert_no_servald_processes
dnahelper="$TFWTMP/dnahelper"
2012-09-07 06:12:20 +00:00
echo "#!$BASH" >"$dnahelper"
2012-09-06 07:49:51 +00:00
cat >>"$dnahelper" <<'EOF'
2012-08-02 07:41:36 +00:00
echo STARTED
while read line
do
token="${line%%|*}"
line="${line#*|}"
did="${line%%|*}"
line="${line#*|}"
2012-12-05 04:21:38 +00:00
echo "$token|uri:dumb|$did|$*|"
2012-08-02 07:41:36 +00:00
echo DONE
done
EOF
chmod 0755 "$dnahelper"
start_servald_instances +A
2012-09-14 05:34:05 +00:00
assert_all_instance_peers_complete +A
2012-08-02 07:41:36 +00:00
}
2012-12-05 04:21:38 +00:00
test_ExecArgs() {
2012-08-02 07:41:36 +00:00
executeOk_servald dna lookup 12345
assertStdoutIs -e "uri:dumb:12345:Hello, World!\n"
}
2012-07-23 08:59:57 +00:00
doc_ReplyOk1="DNA helper returns one valid reply"
test_ReplyOk1() {
2012-07-23 02:53:17 +00:00
executeOk_servald dna lookup 00001
2012-07-24 01:59:07 +00:00
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00001:Joe A. Bloggs\n"
2012-07-23 02:53:17 +00:00
}
2012-07-23 08:59:57 +00:00
doc_ReplyOk2="DNA helper returns two valid replies"
test_ReplyOk2() {
2012-07-23 02:53:17 +00:00
executeOk_servald dna lookup 00002
2012-07-24 01:59:07 +00:00
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00002:Joe A. Bloggs\nsip://$SID_JOE_B@10.1.1.1:00002:Joe B. Bloggs\n"
2012-07-23 02:53:17 +00:00
}
2012-07-23 08:59:57 +00:00
doc_ReplyOk3="DNA helper returns three valid replies"
test_ReplyOk3() {
2012-07-23 02:53:17 +00:00
executeOk_servald dna lookup 00003
2012-07-24 01:59:07 +00:00
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00003:Joe A. Bloggs\nsip://$SID_JOE_B@10.1.1.1:00003:Joe B. Bloggs\nsip://$SID_JOE_C@10.1.1.1:00003:Joe C. Bloggs\n"
2012-07-23 02:53:17 +00:00
}
2012-07-23 08:59:57 +00:00
doc_UriEmpty="DNA helper returns empty URI"
test_UriEmpty() {
2012-07-23 02:53:17 +00:00
executeOk_servald dna lookup 00004
assertStdoutIs ""
2012-07-23 08:59:57 +00:00
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*empty URI'
}
doc_UriInvalid1="DNA helper returns invalid URI, missing scheme"
test_UriInvalid1() {
executeOk_servald dna lookup 000051
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*Bluebottle.*invalid URI'
}
doc_UriInvalid2="DNA helper returns invalid URI, invalid char"
test_UriInvalid2() {
executeOk_servald dna lookup 000052
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*sip://Sea goon.*invalid URI'
}
doc_UriInvalid3="DNA helper returns invalid URI, empty hierarchical part"
test_UriInvalid3() {
executeOk_servald dna lookup 000053
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*sip:.*invalid URI'
}
doc_TokenMismatch="DNA helper returns mismatched token"
test_TokenMismatch() {
executeOk_servald dna lookup 000061
assertStdoutIs ""
2012-08-09 02:51:40 +00:00
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*mismatched token'
2012-07-23 08:59:57 +00:00
}
doc_TokenEmpty="DNA helper returns empty token"
test_TokenEmpty() {
executeOk_servald dna lookup 000062
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*empty token'
}
doc_TokenInvalid="DNA helper returns invalid token"
test_TokenInvalid() {
executeOk_servald dna lookup 000063
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*invalid token'
}
doc_TokenInvalidLong="DNA helper returns invalid token, too long"
test_TokenInvalidLong() {
executeOk_servald dna lookup 000064
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply.*invalid'
}
doc_TokenInvalidShort="DNA helper returns invalid token, too short"
test_TokenInvalidShort() {
executeOk_servald dna lookup 000065
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*invalid token'
}
doc_DidMismatch="DNA helper returns mismatched DID"
test_DidMismatch() {
executeOk_servald dna lookup 000071
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*mismatched DID'
}
doc_DidEmpty="DNA helper returns empty DID"
test_DidEmpty() {
executeOk_servald dna lookup 000072
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*empty DID'
}
doc_DidInvalid="DNA helper returns invalid DID"
test_DidInvalid() {
executeOk_servald dna lookup 000073
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*invalid DID'
}
doc_DidInvalidLong="DNA helper returns invalid DID, too long"
test_DidInvalidLong() {
executeOk_servald dna lookup 000074
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply.*invalid'
}
doc_DidInvalidShort="DNA helper returns invalid DID, too short"
test_DidInvalidShort() {
executeOk_servald dna lookup 000075
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*invalid DID'
}
doc_ReplyInvalidMissingDelim="DNA helper returns invalid reply, missing delimiter"
test_ReplyInvalidMissingDelim() {
executeOk_servald dna lookup 000081
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply.*invalid'
}
doc_ReplyInvalidLongName="DNA helper returns invalid reply, name too long"
test_ReplyInvalidLongName() {
executeOk_servald dna lookup 000082
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply.*invalid'
}
doc_ReplyInvalidEmpty="DNA helper returns invalid reply, empty line"
test_ReplyInvalidEmpty() {
executeOk_servald dna lookup 000083
assertStdoutIs ""
2012-10-06 05:12:22 +00:00
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply .\\n. invalid'
2012-07-23 08:59:57 +00:00
}
doc_ReplyInvalidMissingNewline="DNA helper returns invalid reply, missing newline"
test_ReplyInvalidMissingNewline() {
executeOk_servald dna lookup 000084
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply.*spurious'
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply timeout'
}
2012-07-24 01:59:07 +00:00
doc_HelperTimeout="DNA helper process takes too long to reply and is restarted"
test_HelperTimeout() {
executeOk_servald dna lookup 00009
assertStdoutIs ""
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*reply timeout'
assertGrep "$LOGA" 'INFO:.*DNAHELPER.*process.*terminated by signal 15'
executeOk_servald dna lookup 00001
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00001:Joe A. Bloggs\n"
}
doc_ReplySpurious="DNA helper spurious output after DONE is ignored"
test_ReplySpurious() {
executeOk_servald dna lookup 00010
assertStdoutIs -e "sip://$SID_JOE_E@10.1.1.1:00010:Joe E. Bloggs\n"
assertGrep "$LOGA" 'WARN:.*DNAHELPER.*spurious output'
executeOk_servald dna lookup 00001
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00001:Joe A. Bloggs\n"
}
2012-07-23 08:59:57 +00:00
doc_HelperDies="DNA helper process dies unexpectedly and is restarted"
test_HelperDies() {
2012-07-24 01:59:07 +00:00
executeOk_servald dna lookup 00011
2012-07-23 08:59:57 +00:00
assertStdoutIs ""
assertGrep "$LOGA" 'INFO:.*DNAHELPER.*process.*exited normally with status 42'
2012-10-06 05:12:22 +00:00
assertGrep "$LOGA" 'ERROR:.*DNAHELPER.*goodbye cruel world\\n'
2012-07-23 08:59:57 +00:00
executeOk_servald dna lookup 00001
2012-07-24 01:59:07 +00:00
assertStdoutIs -e "sip://$SID_JOE_A@10.1.1.1:00001:Joe A. Bloggs\n"
2012-07-03 07:15:03 +00:00
}
runTests "$@"