#!/bin/bash

# Tests for voice calls
#
# Copyright 2012 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"

setup() {
   setup_servald
   assert_no_servald_processes
   start_servald_instances +A +B
}
teardown() {
   stop_all_servald_servers
   kill_all_servald_processes
   assert_no_servald_processes
   report_all_servald_servers
}

configure_servald_server() {
   create_single_identity
   add_servald_interface
   executeOk_servald config \
      set debug.mdprequests on \
      set debug.monitor on \
      set debug.vomp on \
      set vomp.dial_timeout_ms 1000 \
      set vomp.ring_timeout_ms 5000 \
      set vomp.network_timeout_ms 5000 \
      set log.console.level DEBUG \
      set log.console.show_time on
}

vomp_console() {
# Note, this might block and stall the test, but I need to predict where stdout is going to be
# perhaps add an argument to execute?
   $servald console <&8 >"$1"
}

send_cmd() {
   tfw_log "sending $1 to $2"
# Note, this might block and stall the test...
   echo "$1" >> "$2"
}

init_console() {
  eval ${instance_name}_IN="$PWD/${instance_name}_IN"
  eval ${instance_name}_OUT="$PWD/${instance_name}_OUT"
  mkfifo "$PWD/${instance_name}_IN"
  exec 8<>"$PWD/${instance_name}_IN"
  fork vomp_console "$PWD/${instance_name}_OUT"
  exec 8>&-
}

doc_nophone="Attempt to dial a node with no phone"
test_nophone() {
   set_instance +A
   init_console
   send_cmd "call $SIDB $DIDA $DIDB" "$A_IN"
   wait_until --timeout=10 grep "^Call ended$" "$A_OUT"
   send_cmd "quit" "$A_IN"
   fork_wait_all
   tfw_cat "$A_OUT"
}

doc_hangup="Hangup instead of answering"
test_hangup() {
   foreach_instance +A +B init_console
   send_cmd "call $SIDA $DIDB $DIDA" "$B_IN"
   wait_until --timeout=10 grep "^Dialling$" "$B_OUT"
   wait_until --timeout=10 grep "^Incoming call" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$B_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$B_OUT"
   send_cmd "hangup" "$A_IN"
   wait_until --timeout=10 grep "^Call ended$" "$A_OUT"
   wait_until --timeout=10 grep "^Call ended$" "$B_OUT"
   send_cmd "quit" "$A_IN"
   send_cmd "quit" "$B_IN"
   fork_wait_all
   tfw_cat "$A_OUT" "$B_OUT"
}

doc_timeout="Call timeout"
test_timeout() {
   foreach_instance +A +B init_console
   send_cmd "call $SIDA $DIDB $DIDA" "$B_IN"
   wait_until --timeout=10 grep "^Dialling$" "$B_OUT"
   wait_until --timeout=10 grep "^Incoming call" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$B_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$B_OUT"
   send_cmd "answer" "$A_IN"
   wait_until --timeout=10 grep "^Picked up$" "$B_OUT"
   send_cmd "say hello" "$A_IN"
   send_cmd "say hi there" "$B_IN"
   wait_until --timeout=10 grep "^hello$" "$B_OUT"
   wait_until --timeout=10 grep "^hi there$" "$A_OUT"
   stop_servald_server +B
# B's console should just quit
   wait_until --timeout=10 --timeout=20 grep "^Call ended$" "$A_OUT"
   send_cmd "quit" "$A_IN"
   fork_wait_all
   tfw_cat "$A_OUT" "$B_OUT"
}

doc_call_lifecycle="Successful call lifecycle"
test_call_lifecycle() {
   foreach_instance +A +B init_console
   send_cmd "call $SIDA $DIDB $DIDA" "$B_IN"
   wait_until --timeout=10 grep "^Dialling$" "$B_OUT"
   wait_until --timeout=10 grep "^Incoming call" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$B_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$B_OUT"
   send_cmd "answer" "$A_IN"
   wait_until --timeout=10 grep "^Picked up$" "$B_OUT"
   send_cmd "say hello" "$A_IN"
   send_cmd "say hi there" "$B_IN"
   wait_until --timeout=10 grep "^hello$" "$B_OUT"
   wait_until --timeout=10 grep "^hi there$" "$A_OUT"
   send_cmd "hangup" "$B_IN"
   wait_until --timeout=10 grep "^Call ended$" "$A_OUT"
   wait_until --timeout=10 grep "^Call ended$" "$B_OUT"
   send_cmd "quit" "$A_IN"
   send_cmd "quit" "$B_IN"
   fork_wait_all
   tfw_cat "$A_OUT" "$B_OUT"
}

doc_concurrent="Multiple calls at once"
setup_concurrent() {
   setup_servald
   assert_no_servald_processes
   start_servald_instances +A +B +C +D +E
}
test_concurrent() {
   foreach_instance +A +B +C +D +E init_console
   send_cmd "call $SIDB $DIDA $DIDB" "$A_IN"
   send_cmd "call $SIDC $DIDA $DIDC" "$A_IN"
   send_cmd "call $SIDA $DIDD $DIDA" "$D_IN"
   send_cmd "call $SIDA $DIDE $DIDA" "$E_IN"
   wait_until --timeout=10 grep "^Incoming call" "$A_OUT"
   wait_until --timeout=10 grep "^Incoming call" "$B_OUT"
   wait_until --timeout=10 grep "^Incoming call" "$C_OUT"
   wait_until --timeout=10 grep "^Codec list" "$A_OUT"
   wait_until --timeout=10 grep "^Codec list" "$B_OUT"
   wait_until --timeout=10 grep "^Codec list" "$C_OUT"
   wait_until --timeout=10 grep "^Codec list" "$D_OUT"
   wait_until --timeout=10 grep "^Codec list" "$E_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$A_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$D_OUT"
   wait_until --timeout=10 grep "^Ringing$" "$E_OUT"
   send_cmd "answer" "$A_IN"
   send_cmd "answer" "$A_IN"
   send_cmd "answer" "$B_IN"
   send_cmd "hangup" "$C_IN"
   wait_until --timeout=10 grep "^Picked up$" "$A_OUT"
   wait_until --timeout=10 grep "^Picked up$" "$D_OUT"
   wait_until --timeout=10 grep "^Picked up$" "$E_OUT"
   wait_until --timeout=10 grep "^Call ended$" "$C_OUT"
   send_cmd "hangup" "$B_IN"
   send_cmd "hangup" "$D_IN"
   send_cmd "hangup" "$E_IN"
   wait_until --timeout=10 grep "^Call ended$" "$B_OUT"
   wait_until --timeout=10 grep "^Call ended$" "$D_OUT"
   wait_until --timeout=10 grep "^Call ended$" "$E_OUT"
   send_cmd "quit" "$A_IN"
   send_cmd "quit" "$B_IN"
   send_cmd "quit" "$C_IN"
   send_cmd "quit" "$D_IN"
   send_cmd "quit" "$E_IN"
   fork_wait_all
   tfw_cat "$A_OUT" "$B_OUT" "$C_OUT" "$D_OUT" "$E_OUT"
}

runTests "$@"