serval-dna/tests/meshmsjava
2014-06-24 12:12:48 +09:30

233 lines
9.9 KiB
Bash
Executable File

#!/bin/bash
# Tests for MeshMS Java API.
#
# Copyright 2014 Serval Project Inc.
#
# 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"
source "${0%/*}/../testdefs_java.sh"
source "${0%/*}/../testdefs_meshms.sh"
setup() {
setup_servald
setup_servald_so
compile_java_classes
set_instance +A
executeOk_servald config \
set log.console.level debug \
set debug.httpd on
create_identities 4
start_servald_server
}
teardown() {
stop_all_servald_servers
kill_all_servald_processes
assert_no_servald_processes
report_all_servald_servers
}
doc_MeshmsListConversations="Java API list MeshMS conversations"
setup_MeshmsListConversations() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message One"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message Two"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message Three"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message Four"
}
test_MeshmsListConversations() {
executeJavaOk org.servalproject.test.Meshms meshms-list-conversations $SIDA1
tfw_cat --stderr
assertStdoutLineCount '==' 3
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA2, read=true, last_message=0, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA3, read=false, last_message=14, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA4, read=false, last_message=18, read_offset=0"
executeOk_servald meshms read messages $SIDA1
executeJavaOk org.servalproject.test.Meshms meshms-list-conversations $SIDA1
assertStdoutLineCount '==' 3
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA2, read=true, last_message=0, read_offset=0"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA3, read=true, last_message=14, read_offset=14"
assertStdoutGrep "my_sid=$SIDA1, their_sid=$SIDA4, read=true, last_message=18, read_offset=18"
}
doc_MeshmsListMessages="Java API list MeshMS messages in one conversation"
setup_MeshmsListMessages() {
setup
meshms_add_messages $SIDA1 $SIDA2 '><>>A>A<>><><><>>>A>A><<<<A<>><>>A<<>'
let NROWS=NSENT+NRECV+(NACK?1:0)
executeOk_servald meshms list messages $SIDA1 $SIDA2
delivered_offset=$(sed -n -e '/^[0-9]\+:[0-9]\+:ACK:delivered$/{n;s/^[0-9]\+:\([0-9]\+\):>:.*/\1/p;q}' "$TFWSTDOUT")
[ -z "$delivered_offset" ] && delivered_offset=0
read_offset=$(sed -n -e 's/^[0-9]\+:\([0-9]\+\):MARK:read$/\1/p' "$TFWSTDOUT")
[ -z "$read_offset" ] && read_offset=0
tfw_log delivered_offset="$delivered_offset" read_offset="$read_offset"
}
test_MeshmsListMessages() {
executeJavaOk org.servalproject.test.Meshms meshms-list-messages $SIDA1 $SIDA2
assertStdoutLineCount '==' $(($NROWS + 2))
assertStdoutIs --line=1 -e "read_offset=$read_offset\n"
assertStdoutIs --line=2 -e "latest_ack_offset=$delivered_offset\n"
seen_ack=false
let lnum=3
for ((j = NMESSAGE-1; j >= 0; --j)); do
case ${MESSAGE[$j]} in
'ACK') $seen_ack && continue
esac
assertStdoutGrep --line=$lnum 'token=[-_A-Za-z0-9=]\+,'
assertStdoutGrep --line=$lnum "my_sid=$SIDA1,"
assertStdoutGrep --line=$lnum "their_sid=$SIDA2,"
text="$(sed -n -e $lnum's/.*\<text=\([^ ]*\), .*/\1/p' "$TFWSTDOUT")"
offset="$(sed -n -e $lnum's/.*\<offset=\([0-9]\+\).*/\1/p' "$TFWSTDOUT")"
is_delivered="$(sed -n -e $lnum's/.*\<delivered=\(true\|false\).*/\1/p' "$TFWSTDOUT")"
is_read="$(sed -n -e $lnum's/.*\<read=\(true\|false\).*/\1/p' "$TFWSTDOUT")"
ack_offset="$(sed -n -e $lnum's/.*\<ack_offset=\(null\|[0-9]\+\).*/\1/p' "$TFWSTDOUT")"
tfw_log text="$text" offset="$offset" is_delivered="$is_delivered" is_read="$is_read" ack_offset="$ack_offset"
case ${MESSAGE[$j]} in
'>'|'<')
echo -n "${TEXT[$j]}" | tr ' \n' . >text_fixture
echo -n "$text" >text_list
assert --dump-on-fail=text_fixture --dump-on-fail=text_list cmp text_fixture text_list
assert [ "$ack_offset" = null ]
;;
esac
case ${MESSAGE[$j]} in
'>')
assertStdoutGrep --line=$lnum 'type=MESSAGE_SENT,'
if [ "$offset" -le "$delivered_offset" ]; then
assert [ "$is_delivered" = true ]
else
assert [ "$is_delivered" = false ]
fi
let ++lnum
;;
'<')
assertStdoutGrep --line=$lnum 'type=MESSAGE_RECEIVED,'
if [ "$offset" -le "$read_offset" ]; then
assert [ "$is_read" = true ]
else
assert [ "$is_read" = false ]
fi
let ++lnum
;;
'ACK')
assertStdoutGrep --line=$lnum 'type=ACK_RECEIVED,'
assert [ "$text" = null ]
assert [ "$ack_offset" = "$delivered_offset" ]
seen_ack=true
let ++lnum
;;
esac
done
}
doc_MeshmsListMessagesNoIdentity="Java API list MeshMS messages from unknown identity"
test_MeshmsListMessagesNoIdentity() {
SIDX=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
executeJavaOk org.servalproject.test.Meshms meshms-list-messages $SIDX $SIDA2
assertStdoutGrep 'MeshMSUnknownIdentityException'
tfw_cat --stdout --stderr
}
doc_MeshmsSend="Java API send MeshMS message"
test_MeshmsSend() {
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDA1 $SIDA2 "Hello World"
executeOk_servald meshms list messages $SIDA1 $SIDA2
assertStdoutGrep --matches=1 ':>:Hello World'
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDA2 $SIDA1 "Hello Back!"
executeOk_servald meshms list messages $SIDA1 $SIDA2
assertStdoutGrep --matches=1 ':>:Hello World$'
assertStdoutGrep --matches=1 ':<:Hello Back!$'
}
doc_MeshmsSendNoIdentity="Java API send MeshMS message from unknown identity"
test_MeshmsSendNoIdentity() {
SIDX=0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
executeJavaOk org.servalproject.test.Meshms meshms-send-message $SIDX $SIDA2 "Hello World"
assertStdoutGrep 'MeshMSUnknownIdentityException'
tfw_cat --stdout --stderr
}
doc_MeshmsReadAllConversations="Java API MeshMS mark all conversations read"
setup_MeshmsReadAllConversations() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message3"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message4"
executeOk_servald meshms list conversations $SIDA1
assertStdoutGrep --stderr --matches=1 ":$SIDA2::0:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA3:unread:11:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA4:unread:14:0\$"
}
test_MeshmsReadAllConversations() {
executeJavaOk org.servalproject.test.Meshms meshms-mark-all-conversations-read $SIDA1
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA1
assertStdoutGrep --stderr --matches=1 ":$SIDA2::0:0\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA3::11:11\$"
assertStdoutGrep --stderr --matches=1 ":$SIDA4::14:14\$"
}
doc_MeshmsReadAllMessages="Java API MeshMS mark all conversations read"
setup_MeshmsReadAllMessages() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message3"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message4"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message5"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message6"
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:33:0\$"
}
test_MeshmsReadAllMessages() {
executeJavaOk org.servalproject.test.Meshms meshms-mark-all-messages-read $SIDA2 $SIDA1
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1::33:33\$"
}
doc_MeshmsReadMessage="Java API MeshMS mark a message as read"
setup_MeshmsReadMessage() {
setup
# create 3 threads, with all permutations of incoming and outgoing messages
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message1"
executeOk_servald meshms send message $SIDA3 $SIDA1 "Message2"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message3"
executeOk_servald meshms send message $SIDA1 $SIDA4 "Message4"
executeOk_servald meshms send message $SIDA4 $SIDA1 "Message5"
executeOk_servald meshms send message $SIDA1 $SIDA2 "Message6"
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:33:0\$"
}
test_MeshmsReadMessage() {
executeJavaOk org.servalproject.test.Meshms meshms-advance-read-offset $SIDA2 $SIDA1 22
assertStdoutIs -e 'UPDATED\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:33:22\$"
executeJavaOk org.servalproject.test.Meshms meshms-advance-read-offset $SIDA2 $SIDA1 11
assertStdoutIs -e 'OK\n'
executeOk_servald meshms list conversations $SIDA2
assertStdoutGrep --stderr --matches=1 ":$SIDA1:unread:33:22\$"
}
runTests "$@"