automatic prefix propagation test scenario

Change-Id: I515981054466f656602cc47ae02355ee264330fa
refs: #3218
diff --git a/test_auto_prefix_propagate/A.sh b/test_auto_prefix_propagate/A.sh
new file mode 100755
index 0000000..ab82d7f
--- /dev/null
+++ b/test_auto_prefix_propagate/A.sh
@@ -0,0 +1,191 @@
+#!/usr/bin/env bash
+defaultIdentity=
+repoDir="test-repo"
+repoConf="$repoDir/test.conf"
+repoDataPrefix='ndn:/Z'
+repoHost='localhost'
+repoPort='9527'
+nfdConf='host.nfd.conf'
+systemNfdConf='/usr/local/etc/ndn/nfd.conf'
+testLog='host.test.log'
+nfdLog='host.nfd.log'
+
+LOG() {
+    echo "[A] $1"
+    echo "[A] $1" >>$testLog
+}
+
+kill_process() {
+    # $1: name of process
+    if [[ $# -lt 1 ]]; then
+        echo "require name of process"
+        exit 1
+    fi
+
+    if pgrep -x $1 > /dev/null; then
+        sudo -b killall -w "$1" >>$testLog 2>&1
+    fi
+}
+
+delete_identity() {
+    # $1: name of identity
+    identity=$1 && identity=${identity#ndn:}
+    if [[ $(ndnsec-list | grep -c "$identity$") -gt 0 ]]; then
+        LOG " deleting identity $1"
+        ndnsec-delete $identity >>$testLog 2>&1
+    else
+        LOG " identity $identity does not exist"
+    fi
+}
+
+clean_up() {
+    LOG "Killing processeses"
+    kill_process "ndn-repo-ng"
+    kill_process "nfd"
+    kill_process "ndnpingserver"
+
+    LOG "Deleting repo data"
+    [[ -d $repoDir ]] && rm -rf $repoDir
+
+    LOG "Deleting identities"
+    delete_identity ndn:/Z
+    delete_identity ndn:/Z/A
+    delete_identity ndn:/Z/A/rib
+    delete_identity ndn:/Z/A/B/C
+    delete_identity ndn:/Z/A/B/C/D/E/rib
+    delete_identity ndn:/Z/F
+
+    if [[ -n "$defaultIdentity" ]]; then
+        if [[ $(ndnsec-list | grep -c "$defaultIdentity$") -gt 0 ]]; then
+            ndnsec-set-default $defaultIdentity
+        fi
+    else
+        r=$(ndnsec-list)
+        if [[ -n "$r" ]]; then
+            r=`echo "$r" | tr "\n" "-"`
+            r=${r%%-*} && r=${r#*/} && r="/$r"
+            ndnsec-set-default $r
+        fi
+    fi
+}
+
+create_identity() {
+    # $1: creating identity
+    # $2: signing identity
+    if [[ $# -gt 1 ]]; then
+        ndnsec-key-gen -n $1 > tmp.req
+        ndnsec-cert-gen -N "tmp" -s $2 -r tmp.req > tmp.cert; rm tmp.req
+        ndnsec-cert-install tmp.cert >>$testLog 2>&1
+        [[ -f tmp.cert ]] && rm tmp.cert
+    elif [[ $# -gt 0 ]]; then
+        ndnsec-key-gen -n $1 | ndnsec-cert-install - >>$testLog 2>&1
+    else
+        echo "require input identity"
+        exit 1
+    fi
+}
+
+publish_default_cert() {
+    # $1: name of identity
+    if [[ $# -lt 1 ]]; then
+        echo "require identity to publish cert"
+        exit 1
+    fi
+
+    if [[ "$1"x = "${1#${repoDataPrefix}}"x ]]; then
+        echo "can not publish $1 under $repoDataPrefix"
+        exit 1
+    fi
+
+    ndnsec-cert-dump -r -H $repoHost -P $repoPort -i $1
+}
+
+start_repo() {
+    LOG "create repo dir config file for repo"
+    [[ -d $repoDir ]] || mkdir -p $repoDir
+
+    echo "" > $repoConf # create repo conf file
+
+    infoedit -f $repoConf -s repo.data.prefix -v $repoDataPrefix
+    infoedit -f $repoConf -s repo.command.prefix -v ""
+    infoedit -f $repoConf -s repo.storage.method -v sqlite
+    infoedit -f $repoConf -s repo.storage.path -v $repoDir
+    infoedit -f $repoConf -s repo.storage.max-packets -v 100
+    infoedit -f $repoConf -s repo.tcp_bulk_insert.host -v $repoHost
+    infoedit -f $repoConf -s repo.tcp_bulk_insert.port -v $repoPort
+    infoedit -f $repoConf -s repo.validator.trust-anchor.type -v any
+
+    kill_process ndn-repo-ng
+    LOG "start repo-ng"
+    nohup ndn-repo-ng -c $repoConf >>$testLog 2>&1 &
+}
+
+start_nfd() {
+    LOG "create nfd conf file with auto_prefix_propagate section specified for test"
+    [[ ! -f $systemNfdConf ]] && LOG "can not find nfd config file" && exit 1
+
+    cp $systemNfdConf $nfdConf
+    infoedit -f $nfdConf -s rib.auto_prefix_propagate.cost -v 15
+    infoedit -f $nfdConf -s rib.auto_prefix_propagate.timeout -v 10000
+    infoedit -f $nfdConf -s rib.auto_prefix_propagate.base_retry_wait -v 50
+    infoedit -f $nfdConf -s rib.auto_prefix_propagate.max_retry_wait -v 3600
+    infoedit -f $nfdConf -s rib.auto_prefix_propagate.refresh_interval -v 15
+
+    kill_process nfd
+    LOG "start NFD"
+    sudo nohup nfd --config $nfdConf >>$nfdLog 2>&1 &
+}
+
+prepare_for_test() {
+    echo "Start test log on end host" >$testLog
+    echo "Start nfd log on end host" >$nfdLog
+
+    start_nfd && sleep 2
+
+    start_repo && sleep 2
+
+    defaultIdentity=$(ndnsec-get-default)
+
+    # create identities and publish their certificates
+    LOG "create identity /Z/A and publish its cert"
+    create_identity ndn:/Z/A
+    publish_default_cert ndn:/Z/A
+
+    LOG "create identity /Z/A/nrd and publish its cert"
+    create_identity ndn:/Z/A/rib ndn:/Z/A
+    publish_default_cert ndn:/Z/A/rib
+
+    LOG "create identity /Z/A/B/C and publish its cert"
+    create_identity ndn:/Z/A/B/C ndn:/Z/A
+    publish_default_cert ndn:/Z/A/B/C
+
+    LOG "create identity /Z/F and publish its cert"
+    create_identity ndn:/Z/F
+    publish_default_cert ndn:/Z/F
+}
+
+generate_anchor() {
+    certFile='auto-propagate.anchor.cert'
+    ndnsec-export -o $certFile $1 >>$testLog 2>&1
+    cert=$(cat $certFile) && rm $certFile
+    echo $cert
+}
+
+while getopts "a:cp" arg
+    do
+        case $arg in
+             a)
+                 generate_anchor $OPTARG
+                 ;;
+             c)
+                 clean_up
+                 ;;
+             p)
+                 prepare_for_test
+                 ;;
+             ?)
+                 echo "unknown argument"
+        exit 1
+        ;;
+        esac
+done