#!/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 \ set debug.meshms on set_extra_config create_identities 4 export SERVALD_RHIZOME_DB_RETRY_LIMIT_MS=60000 start_servald_server } teardown() { stop_all_servald_servers kill_all_servald_processes assert_no_servald_processes report_all_servald_servers } set_extra_config() { : } doc_MeshmsDisabled="Java API fails when rhizome is disabled" setup_MeshmsDisabled() { setup executeOk_servald config set rhizome.enable off sync } test_MeshmsDisabled() { executeJava org.servalproject.test.Meshms meshms-list-conversations $SIDA1 tfw_cat --stdout --stderr assertExitStatus != 0 assertStderrGrep ServalDFailureException assertStderrGrep --ignore-case 'missing meshms_status_code' } 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=24, 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=24, read_offset=24" } doc_MeshmsListMessages="Java API list MeshMS messages in one conversation" setup_MeshmsListMessages() { setup meshms_add_messages $SIDA1 $SIDA2 '><>>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]\+:[0-9]\+:ACK:delivered$/{n;s/^[0-9]\+:\([0-9]\+\):[0-9]\+:>:.*/\1/p;q}' "$TFWSTDOUT") [ -z "$delivered_offset" ] && delivered_offset=0 read_offset=$(sed -n -e 's/^[0-9]\+:\([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/.*\'|'<') 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_MeshmsListMessagesNewSince="Java API list MeshMS messages in one conversation since token" setup_MeshmsListMessagesNewSince() { set_extra_config() { executeOk_servald config set api.restful.newsince_timeout 1s } setup meshms_add_messages $SIDA1 $SIDA2 '><>>A>A<>><><><>>>A>A><<<><>>A<<>' let NROWS=NSENT+NRECV+(NACK?1:0) executeJavaOk org.servalproject.test.Meshms meshms-list-messages "$SIDA1" "$SIDA2" tfw_cat --stdout --stderr assertStdoutLineCount == $(($NROWS + 2)) replayStdout | sed -n -e '3,$p' >messages tfw_preserve messages for ((i = 0; i < NROWS; i += 3)); do let lnum=i+1 token[$i]=$(sed -n -e $lnum's/.*\messages$i tfw_preserve messages$i } for ((i = 0; i < NROWS; i += 3)); do # At most fifteen requests going at once [ $i -ge 15 ] && fork_wait %list$((i-15)) fork %list$i list done fork_wait_all for ((i = 0; i < NROWS; i += 3)); do >tmp [ $i -gt 0 ] && sed -n -e "1,${i}p" messages >tmp tfw_preserve tmp # If tmp contains an ACK_RECEIVED, then so should messages$i. Otherwise, # ignore any ACK_RECEIVED in messages$i. grep -q 'type=ACK_RECEIVED' tmp || sed -i -e '/type=ACK_RECEIVED/d' messages$i assert cmp tmp messages$i done } grepall() { local pattern="$1" shift for file; do grep "$pattern" "$file" || return $? done return 0 } doc_MeshmsListMessagesNewSinceArrival="Java API list newly arriving MeshMS messages in one conversation" setup_MeshmsListMessagesNewSinceArrival() { set_extra_config() { executeOk_servald config set api.restful.newsince_timeout 360s \ set api.restful.users.harry.password potter } setup # Use REST interface to send messages, not CLI, in order to avoid a database # locking storm meshms_use_restful harry potter meshms_add_messages $SIDA1 $SIDA2 '><>A>' let NROWS=NSENT+NRECV+(NACK?1:0) executeJavaOk org.servalproject.test.Meshms meshms-list-messages "$SIDA1" "$SIDA2" tfw_cat --stdout --stderr assertStdoutLineCount == $(($NROWS + 2)) token=$(sed -n -e '3s/.*\messages$i } for i in 1 2 3; do fork %list$i list done wait_until [ -e messages1 -a -e messages2 -a -e messages3 ] for message in '>Rumplestiltskin' 'A' 'Eulenspiegel'; do meshms_add_messages $SIDA1 $SIDA2 "${message:0:1}" "${message:1}" case ${message:0:1} in '<'|'>') waitfor="${message:1}";; 'A') waitfor="ACK";; *) error "message=${message}";; esac wait_until grepall "$waitfor" messages{1,2,3} done fork_terminate_all fork_wait_all } teardown_MeshmsListMessagesNewSinceArrival() { tfw_preserve messages{1,2,3} teardown } 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:20: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::20:20\$" } 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:45: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::45:45\$" } 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:45: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:45: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:45:22\$" } runTests "$@"