serval-dna/tests/dnahelper
2012-07-23 12:23:17 +09:30

168 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# Tests for Serval DNA server operations.
#
# Copyright 2012 Paul Gardner-Stephen
#
# 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
}
teardown() {
stop_all_servald_servers
kill_all_servald_processes
assert_no_servald_processes
}
# Called by start_servald_instances immediately before starting the server
# process in each instance.
configure_servald_server() {
executeOk_servald config set log.show_pid on
executeOk_servald config set log.show_time on
executeOk_servald config set debug.dnahelper on
executeOk_servald config set dna.helper.executable "$dnahelper"
}
setup_dnahelper() {
dnahelper="$TFWTMP/dnahelper"
cat >"$dnahelper" <<'EOF'
#!/bin/sh
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|')
echo "$token|A|$did|B|"
;;
*'|00001|')
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
;;
*'|00002|')
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
sleep 0.1
echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
sleep 0.1
;;
*'|00003|')
echo "$token|sip://$token@10.1.0.1|$did|Joe A. Bloggs|"
sleep 0.1
echo "$token|sip://$token@10.1.0.2|$did|Joe B. Bloggs|"
sleep 0.1
echo "$token|sip://$token@10.1.0.3|$did|Joe C. Bloggs|"
sleep 0.1
;;
*'|00004|')
# Empty URI
echo "$token||$did|Eccles|"
;;
# Test malformed URI
# Test mismatched token
# Test empty token
# Test long token
# Test empty DID
# Test mismatched DID
# Test long DID
# Test malformed line
# Test long reply
# Test DONE timeout
# Test die
# Test fork and die
*'|'*'|')
;;
*)
echo "garbage line" >&2
;;
esac
echo DONE
done
EOF
chmod 0755 "$dnahelper"
executeOk "$dnahelper" <<EOF
ToKeN|00000|
EOF
assertStdoutIs -e "STARTED\nToKeN|A|00000|B|\nDONE\n"
}
setup_single_instance() {
setup
setup_dnahelper
start_servald_instances +A
}
doc_ExecError="Non-existent DNA helper executable"
setup_ExecError() {
setup
dnahelper=/non/existent
assert [ ! -e "$dnahelper" ]
start_servald_instances +A
}
test_ExecError() {
executeOk_servald dna lookup 12345
}
doc_OneReply="DNA helper returns one line"
setup_OneReply() {
setup_single_instance
}
test_OneReply() {
executeOk_servald dna lookup 00001
assertStdoutIs -e "sip://$SIDA@10.1.0.1:00001:Joe A. Bloggs\n"
}
doc_TwoReplies="DNA helper returns two lines"
setup_TwoReplies() {
setup_single_instance
}
test_TwoReplies() {
executeOk_servald dna lookup 00002
assertStdoutIs -e "sip://$SIDA@10.1.0.1:00002:Joe A. Bloggs\nsip://$SIDA@10.1.0.2:00002:Joe B. Bloggs\n"
}
doc_ThreeReplies="DNA helper returns three lines"
setup_ThreeReplies() {
setup_single_instance
}
test_ThreeReplies() {
executeOk_servald dna lookup 00003
assertStdoutIs -e "sip://$SIDA@10.1.0.1:00003:Joe A. Bloggs\nsip://$SIDA@10.1.0.2:00003:Joe B. Bloggs\nsip://$SIDA@10.1.0.3:00003:Joe C. Bloggs\n"
}
doc_EmptyURI="DNA helper returns empry URI"
setup_EmptyURI() {
setup_single_instance
}
test_EmptyURI() {
executeOk_servald dna lookup 00004
assertStdoutIs ""
}
runTests "$@"