#!/bin/bash

# Tests for Serval DNA HLR 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"

# Default teardown function
teardown() {
   stop_dna_server
}

# Utility function
create_sid() {
   executeOk $dna "$@" -d 0427679796 -C
   assertStdoutGrep --matches=1 '^OK:'
   sid=`replayStdout | grep '^OK:' | cut -f2 -d:`
}

# Utility function
set_short_var() {
   executeOk $dna -s $sid -i 0 -W note="a short literal value"
}

# Utility function
echo_long_message() {
   local i=0
   while [ $i -lt 100 ]; do
      echo "${i}ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-${i}"
      let i=i+1
   done
}

# Test case, transcribed from ../testdna
doc_CreateSID='Create new SID'
setup_CreateSID() {
   setup_dna
   start_dna_server
}
test_CreateSID() {
   create_sid
   assertRealTime '<' 0.5
   assertExpr --stdout --message='valid SID was returned' "$sid" '~' '[0-9A-F]+'
}

# Test case, transcribed from ../testdna
doc_GetShortVar='Get short variable'
setup_GetShortVar() {
   setup_dna
   start_dna_server
   create_sid
}
test_GetShortVar() {
   executeOk $dna -d 0427679796 -R dids
   assertRealTime '>' 2.9 # Waited for all replies
   assertStdoutGrep --message='received an existing DID' "^DIDS:${sid}:[0-9]\+:0:0427679796"
   assertStdoutGrep --matches=1 --message='filtered out duplicate responses' "^DIDS:${sid}:[0-9]\+:0:0427679796"
}

# Test case, transcribed from ../testdna
doc_SetShortVar='Set short variable'
setup_SetShortVar() {
   setup_dna
   start_dna_server
   create_sid
}
test_SetShortVar() {
   set_short_var
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable write confirmed' "^WROTE:$sid$"
}

# Test case, transcribed from ../testdna
doc_SetGetShortVar='Set then get short variable'
setup_SetGetShortVar() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
}
test_SetGetShortVar() {
   executeOk $dna -s $sid -i 0 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='read variable correctly' "^NOTE:$sid:[0-9]\+:0:a short literal value"
}

# Test case, transcribed from ../testdna
doc_GetShortVarToFile='Get variable into a file'
setup_GetShortVarToFile() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
}
test_GetShortVarToFile() {
   echo "WARNING: Known issue: output to file does not work with DID lists"
   dnatest_dat=$DNATMP/dnatest.dat
   executeOk $dna -s $sid -O $dnatest_dat -i 0 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='read variable into file' "^NOTE:$sid:[0-9]\+:0"
   assertFileContents $dnatest_dat 'a short literal value'
}

# Test case, transcribed from ../testdna
doc_SetMultiShortVar='Set multiple instances of a short variable'
setup_SetMultiShortVar() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
}
test_SetMultiShortVar() {
   executeOk $dna -s $sid -i 1 -W note='$414243'
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable write confirmed' "^WROTE:$sid$"
   executeOk $dna -s $sid -i 1 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='new variable correctly set in hex' "^NOTE:$sid:[0-9]\+:1:ABC$"
}

# Test case, transcribed from ../testdna
doc_GetMultiShortVar='Get multiple instances of a short variable'
setup_GetMultiShortVar() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
   executeOk $dna -s $sid -i 1 -W note='$414243'
   executeOk $dna -s $sid -i 2 -W note
}
test_GetMultiShortVar() {
   executeOk $dna -s $sid -m -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='first variable read correctly' "^NOTE:$sid:[0-9]\+:0:a short literal value$"
   assertStdoutGrep --matches=1 --message='second variable read correctly' "^NOTE:$sid:[0-9]\+:1:ABC$"
   assertStdoutGrep --matches=1 --message='third variable read correctly' "^NOTE:$sid:[0-9]\+:2$"
}

# Test case, transcribed from ../testdna
doc_OverwriteVarNoUpdate='Set does not overwrite variable'
setup_OverwriteVarNoUpdate() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
}
test_OverwriteVarNoUpdate() {
   executeOk $dna -s $sid -i 0 -W note="replacement short literal value"
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=0 --message='variable write not confirmed' "^WROTE:$sid$"
   executeOk $dna -s $sid -i 0 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable retains original value' "^NOTE:$sid:[0-9]\+:0:a short literal value"
}

# Test case, transcribed from ../testdna
doc_UpdateVar='Update overwrites variable'
setup_UpdateVar() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
}
test_UpdateVar() {
   executeOk $dna -s $sid -i 0 -U note="replacement short literal value"
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable write confirmed' "^WROTE:$sid$"
   executeOk $dna -s $sid -i 0 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable has new value' "^NOTE:$sid:[0-9]\+:0:replacement short literal value"
}

# Test case, transcribed from ../testdna
doc_UpdateVarBigValue='Can set variable with multi-packet value'
setup_UpdateVarBigValue() {
   setup_dna
   start_dna_server
   create_sid
   set_short_var
   echo_long_message >$DNATMP/dnatest.in
}
test_UpdateVarBigValue() {
   executeOk $dna -s $sid -i 0 -U note="@dnatest.in"
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable write confirmed' "^WROTE:$sid$"
   executeOk $dna -v verbose -s $sid -O $DNATMP/dnatest.out -i 0 -R note
   assertRealTime '<' 0.5
   assertStdoutGrep --matches=1 --message='variable new value returned' "^NOTE:$sid:[0-9]\+:0"
   assert --message='long value read correctly' cmp --quiet $DNATMP/dnatest.in $DNATMP/dnatest.out
}

runTests "$@"