nack: update Nack counting mechanism in tests 3 and 4

use new nfdc syntax

refs #3933

Change-Id: Ifa4088ad59f0ad62758df22f7ba97c0dfe7bf302
diff --git a/test_nack/README.md b/test_nack/README.md
index 6577105..6a92f77 100644
--- a/test_nack/README.md
+++ b/test_nack/README.md
@@ -10,32 +10,33 @@
 
 ## Steps
 
-1. start NFD on A,B,C,D
-2. configure NFD on A,C,D to use best-route strategy
-3. configure NFD on B to use best-route v1 strategy, which will not transmit or process Nack
-4. on A, create a route for prefix ndn:/P toward B and C,
+1. Start NFD on A, B, C, and D
+2. Configure NFD on A, C, and D to use best-route strategy
+3. Configure NFD on B to use best-route v1 strategy, which will not transmit or process Nacks
+4. On A, create a route for prefix /P toward B and C:
    set the cost toward B to be 10.
    set the cost toward C to be 20.
-5. on D, create a route for prefix ndn:/P toward A
-6. on D, express an Interest for ndn:/P/1 with InterestLifetime=4000ms and no consumer
+5. On D, create a face to A and a route for prefix /P toward A.
+6. On D, express an Interest for /P/1 with InterestLifetime=4000ms and no consumer
    retransmission; wait 8000ms since expressing Interest, fail the scenario
    if Nack comes back;
-7. on D, express an Interest for ndn:/P/2 with InterestLifetime=4000ms, and retransmit
+7. On D, express an Interest for /P/2 with InterestLifetime=4000ms, and retransmit
    with a new Nonce after 200ms;
    wait 8000ms since expressing first Interest, fail the scenario if Nack comes back;
    reason: A shouldn't return a Nack when some upstream times out, even if some other
-   upstream returns Nack.
-8. configure NFD on B to use best-route strategy
-9. on B and C, configure unidirectional link delay toward A as 500ms
-10. on D, express an Interest for ndn:/P/3 with InterestLifetime=4000ms, and retransmit
+   upstream returns Nack;
+8. Configure NFD on B to use best-route strategy
+9. On B and C, configure unidirectional link delay toward A as 500ms
+10. On D, express an Interest for /P/3 with InterestLifetime=4000ms, and retransmit
     with nonce2  after 200ms;
     wait 3000ms since expressing first Interest, expect exactly one Nack comes back
     where Reason is NoRoute and Nonce equals nonce2, otherwise fail the scenario;
-    reason: A should return a Nack after both upstreams have returned Nack
-11. on B, configure unidirectional link delay toward A as 1000ms
-12. on D, express an Interest for ndn:/P/4 with InterestLifetime=4000ms, and
+    reason: A should return a Nack after both upstreams have returned Nack. Use ndndump on D to
+    record traffic for analysis
+11. On B, configure unidirectional link delay toward A as 1000ms
+12. On D, express an Interest for /P/4 with InterestLifetime=4000ms, and
     retransmit with nonce2 after 200ms;
     wait 3000ms since expressing first Interest, expect exactly one Nack comes back
     where Reason is NoRoute and Nonce equals nonce2, otherwise fail the scenario;
     reason: the Nack should carry the latest incoming Nonce from downstream,
-    not the Nonce from last incoming Nack.
\ No newline at end of file
+    not the Nonce from last incoming Nack. Use ndndump on D to record traffic for analysis
diff --git a/test_nack/nack-test.sh b/test_nack/nack-test.sh
index 1f3a313..0f53f50 100755
--- a/test_nack/nack-test.sh
+++ b/test_nack/nack-test.sh
@@ -9,14 +9,17 @@
 mkdir -p $workDir/logs
 
 clean_up() {
-
-  dataB=$1
-  dataC=$2
   echo "killing nfd on B, C and D..."
-  ssh $CTRL_B "sudo killall nfd >> $testLogB 2>&1 &\
-               sudo /sbin/tc qdisc del dev $dataB root"
-  ssh $CTRL_C "sudo killall nfd >> $testLogC 2>&1 &\
-               sudo /sbin/tc qdisc del dev $dataC root"
+  ssh $CTRL_B "sudo killall nfd >> $testLogB 2>&1"
+  if [[ $# -ge 1 ]]; then
+    local dataB=$1
+    ssh $CTRL_B "sudo tc qdisc del dev $dataB root"
+  fi
+  ssh $CTRL_C "sudo killall nfd >> $testLogC 2>&1"
+  if [[ $# -ge 2 ]]; then
+    local dataC=$2
+    ssh $CTRL_C "sudo tc qdisc del dev $dataC root"
+  fi
   ssh $CTRL_D "sudo killall nfd >> $testLogD 2>&1"
 
   sudo killall nfd 2>&1
@@ -28,70 +31,68 @@
 # start nfd on localhost (A) and set best-route strategy
 nfd-start &> $logDir/nfdA.log
 sleep 2
-nfdc set-strategy / /localhost/nfd/strategy/best-route >> $testLogA
+nfdc strategy set / /localhost/nfd/strategy/best-route >> $testLogA
 
 echo "Configure host B..."
 # start nfd on hostB and set best-route-v1
 ssh $CTRL_B "mkdir -p $logDir ; nfd-start &> $logDir/nfdB.log &\
   sleep 2 ; echo 'Starting nfd on host B' > $testLogB &\
-  nfdc set-strategy / /localhost/nfd/strategy/best-route/%FD%01 >> $testLogB"
+  nfdc strategy set / /localhost/nfd/strategy/best-route/%FD%01 >> $testLogB"
 
 echo "Configure host C..."
 # start nfd on hostC and set best-route strategy
 ssh $CTRL_C "mkdir -p $logDir ; nfd-start &> $logDir/nfdC.log &\
   sleep 2 ; echo 'Starting nfd on host C' > $testLogC &\
-  nfdc set-strategy / /localhost/nfd/strategy/best-route >> $testLogC"
+  nfdc strategy set / /localhost/nfd/strategy/best-route >> $testLogC"
 
 echo "Configure host D..."
 # start nfd on hostD and set best-route strategy
 ssh $CTRL_D "mkdir -p $logDir ; nfd-start &> $logDir/nfdD.log &\
   sleep 2 ; echo 'Starting nfd on host D' > $testLogD &\
-  nfdc set-strategy / /localhost/nfd/strategy/best-route >> $testLogD"
+  nfdc strategy set / /localhost/nfd/strategy/best-route >> $testLogD"
 
-# A: create a route for prefix ndn:/P toward B and C,
+# A: create faces and routes for prefix /P toward B and C,
 # where the cost toward B (10) is lower than the cost toward C(20)
-nfdc register -c 10 ndn:/P udp4://$IP4_B1:6363 >> $testLogA
-nfdc register -c 20 ndn:/P udp4://$IP4_C1:6363 >> $testLogA
+nfdc face create udp4://$IP4_B1:6363 >> $testLogA
+nfdc face create udp4://$IP4_C1:6363 >> $testLogA
+nfdc route add /P udp4://$IP4_B1:6363 cost 10 >> $testLogA
+nfdc route add /P udp4://$IP4_C1:6363 cost 20 >> $testLogA
 
-# D: create a route for prefix ndn:/P toward A
-ssh $CTRL_D "echo 'creating a route for namespace ndn:/P toward A' >> $testLogD &\
-  nfdc register -c 20 ndn:/P udp4://$IP4_A2:6363 >> $testLogD &"
+# D: create a face to A and a route for prefix /P toward A
+ssh $CTRL_D "echo 'creating a route toward A and a route for namespace /P toward A' >> $testLogD &\
+  nfdc face create udp4://$IP4_A2:6363 >> $testLogD &&\
+  nfdc route add /P udp4://$IP4_A2:6363 cost 20 >> $testLogD"
 
 echo "Start test 1..."
-# D: express an Interest for ndn:/P/1, no retransmissions, wait 8000ms
-ssh $CTRL_D "echo 'express an Interest for ndn:/P/1' >> $testLogD &\
-  test-nack-consumer -p ndn:/P/1 -w 8000 > $logDir/test1.log"
+# D: express an Interest for /P/1, no retransmissions, wait 8000ms
+ssh $CTRL_D "echo 'express an Interest for /P/1' >> $testLogD &\
+  test-nack-consumer -p /P/1 -w 8000 > $logDir/test1.log"
 
 scp $CTRL_D:$logDir/test1.log $logDir/test1.log
 
 echo "Analyzing result of test 1..."
 received=$(tail -n 1  $logDir/test1.log | awk '{print $1}')
-if [[ "$received" == "NACK" ]]
-then
-  echo "FAIL: A returned unexpected Nack while Nack is not expected if not all &\
-        upstreams return Nack. The test doesn't retransmit the Interest, and &\
-        therefore A didn't try all possible upstreams to return Nack from all. &\
-        Check test1 log for details."
+if [[ "$received" == "NACK" ]]; then
+  echo "FAIL: A returned an unexpected Nack. This test does not retransmit the Interest,"
+  echo "and therefore A did not try all possible nexthops. Check $logDir/test1.log for more details."
   clean_up
-exit 1
+  exit 1
 fi
 
 echo "Start test 2..."
-# D: express an Interest for ndn:/P/2, retransmit with new nonce, wait 8000ms
-ssh $CTRL_D "echo 'express an Interest for ndn:/P/2' >> $testLogD &\
-  test-nack-consumer -p ndn:/P/2 -r 2000 -w 8000 > $logDir/test2.log"
+# D: express an Interest for /P/2, retransmit with new nonce, wait 8000ms
+ssh $CTRL_D "echo 'express an Interest for /P/2' >> $testLogD &\
+  test-nack-consumer -p /P/2 -r 2000 -w 8000 > $logDir/test2.log"
 
 scp $CTRL_D:$logDir/test2.log $logDir/test2.log
 
 echo "Analyzing result of test 2..."
 received=$(tail -n 1 $logDir/test2.log | awk '{print $1}')
-if [[ "$received" == "NACK" ]]
-then
-echo "FAIL: A returned unexpected Nack. Nack is not expected when one or &\
-      upstreams time out, even if some upstreams return Nack. &\
-      Check test2 log for details."
-clean_up
-exit 1
+if [[ "$received" == "NACK" ]]; then
+  echo "FAIL: A returned an unexpected Nack. A Nack is not expected when one or more upstreams"
+  echo "time out, even if some upstreams return Nacks. Check $logDir/test2.log for more details."
+  clean_up
+  exit 1
 fi
 
 echo "Setup test 3..."
@@ -99,7 +100,7 @@
 dataB=$(ssh $CTRL_B "netstat -ie | grep -B1 $IP4_B1 | head -n1" | awk '{print $1}')
 
 # B: configure best-route strategy and get the set link delay to 500 ms
-ssh $CTRL_B "nfdc set-strategy / /localhost/nfd/strategy/best-route >> $testLogB &\
+ssh $CTRL_B "nfdc strategy set / /localhost/nfd/strategy/best-route >> $testLogB &\
   sudo /sbin/tc qdisc add dev $dataB root netem delay 500ms >> $testLogB "
 
 # get data interface of host C
@@ -108,22 +109,31 @@
 # B: configure link delay to 500 ms
 ssh $CTRL_C "sudo /sbin/tc qdisc add dev $dataC root netem delay 500ms >> $testLogC"
 
+# get data interface of host D
+dataD=$(ssh $CTRL_D "netstat -ie | grep -B1 $IP4_D1 | head -n1" | awk '{print $1}')
+
 echo "Start test 3..."
-# D: express an Interest for ndn:/P/3, retransmit with new nonce after 200ms, wait 3000ms
-ssh $CTRL_D "echo 'express an Interest for ndn:/P/3' >> $testLogD &\
-  test-nack-consumer -p ndn:/P/3 -r 200 -w 3000 > $logDir/test3.log"
+# D: start ndndump for interface to R, express an Interest for /P/3, retransmit with new nonce after
+# 200ms, wait 3000ms
+ssh $CTRL_D "sudo ndndump -i $dataD > $logDir/ndndump-3.out & sleep 1"
+ssh $CTRL_D "echo 'express an Interest for /P/3' >> $testLogD &\
+  test-nack-consumer -p /P/3 -r 200 -w 3000 > $logDir/test3.log"
+ssh $CTRL_D "sleep 1; sudo killall ndndump"
 
 scp $CTRL_D:$logDir/test3.log $logDir/test3.log
+scp $CTRL_D:$logDir/ndndump-3.out $logDir/ndndump-3.out
 
 echo "Analyzing result of test 3..."
-reason=$(grep NACK $logDir/test3.log | awk '{print $NF}')
-NACK_count=$(grep NACK $logDir/test3.log | wc -l)
+NACK_count=$(grep "NACK" $logDir/ndndump-3.out | wc -l)
+reason=$(grep -Po "NACK: \K[^,]*" $logDir/ndndump-3.out)
+nonce2=$(grep "Sending Interest 2:" $logDir/test3.log | awk '{print $NF}')
+NACK_nonce=$(grep -Po "NACK:.*?.*ndn\.Nonce=\K[0-9]*" $logDir/ndndump-3.out)
 
-if [ $NACK_count -ne 1 ] || [[ "$reason" != "NoRoute" ]]
-then
-  echo "FAIL: A should return exactly one Nack after both upstreams have returned Nack. &\
-        Reason should be 'NoRoute'. Test returned $NACK_count Nacks, with reason $reason. &\
-        Check test3 log for more details."
+if [[ $NACK_count -ne 1 || "$reason" != NoRoute || "$NACK_nonce" -ne "$nonce2" ]]; then
+  echo "FAIL: A should return exactly one Nack after both upstreams have returned Nack."
+  echo "Reason should be 'NoRoute' and Nonce should match the second Interest ($nonce2)."
+  echo "Test returned $NACK_count Nacks, with reason $reason for Nonce $NACK_nonce."
+  echo "Check $logDir/test3.log and $logDir/ndndump-3.out for more details."
   clean_up $dataB $dataC
   exit 1
 fi
@@ -131,32 +141,35 @@
 echo "Setup test 4..."
 
 # B: configure link delay to 1000 ms
-ssh $CTRL_B "nfdc set-strategy / /localhost/nfd/strategy/best-route >> $testLogB&\
+ssh $CTRL_B "nfdc strategy set / /localhost/nfd/strategy/best-route >> $testLogB&\
   sudo /sbin/tc qdisc change dev $dataB root netem delay 1000ms >> $testLogB"
 
-# D: express an Interest for ndn:/P/4, retransmit with new nonce after 200ms, wait 3000ms
-ssh $CTRL_D "echo 'express an Interest for ndn:/P/4' >> $testLogD &\
-  test-nack-consumer -p ndn:/P/4 -r 200 -w 3000 > $logDir/test4.log"
+# D: start ndndump for interface to R, express an Interest for /P/4, retransmit with new nonce after
+# 200ms, wait 3000ms
+ssh $CTRL_D "sudo ndndump -i $dataD > $logDir/ndndump-4.out & sleep 1"
+ssh $CTRL_D "echo 'express an Interest for /P/4' >> $testLogD &\
+  test-nack-consumer -p /P/4 -r 200 -w 3000 > $logDir/test4.log"
+ssh $CTRL_D "sleep 1; sudo killall ndndump"
 
 scp $CTRL_D:$logDir/test4.log $logDir/test4.log
+scp $CTRL_D:$logDir/ndndump-4.out $logDir/ndndump-4.out
 
 echo "Analyzing result of test 4"
-reason=$(grep NACK $logDir/test4.log | awk '{print $NF}')
-NACK_count=$(grep NACK $logDir/test4.log | wc -l)
+NACK_count=$(grep "NACK" $logDir/ndndump-4.out | wc -l)
+reason=$(grep -Po "NACK: \K[^,]*" $logDir/ndndump-4.out)
 nonce2=$(grep "Sending Interest 2:" $logDir/test4.log | awk '{print $NF}')
-NACK_nonce=$(grep NACK $logDir/test4.log | awk '{for(i=1;i<=NF;i++){if($i~/^Nonce/){print $(i+1)}}}'
-)
+NACK_nonce=$(grep -Po "NACK:.*?.*ndn\.Nonce=\K[0-9]*" $logDir/ndndump-4.out)
 
-if [ $NACK_count -ne 1 ] || [[ "$reason" != "NoRoute" ]] || [ "$NACK_nonce" -ne "$nonce2" ]
-then
-  echo "FAIL: A should return exactly one Nack that should carry the latest incoming Nonce \
-  from downstream. Reason should be 'NoRoute'. Test returned $NACK_count Nacks, with nonce &\
-  $NACK_nonce and reason $reason. Check test4 log for test details."
+if [[ $NACK_count -ne 1 || "$reason" != NoRoute || "$NACK_nonce" -ne "$nonce2" ]]; then
+  echo "FAIL: A should return exactly one Nack carrying the latest incoming Nonce from"
+  echo "downstream ($nonce2). The reason should be 'NoRoute'. The test returned $NACK_count Nacks,"
+  echo "with nonce $NACK_nonce and reason $reason. Check $logDir/test4.log and"
+  echo "$logDir/ndndump-4.out for more details."
   clean_up $dataB $dataC
-exit 1
+  exit 1
 fi
 
 # cleanup
 clean_up $dataB $dataC
-echo "Test passed successfully"
-exit 0
\ No newline at end of file
+echo "Test passed"
+exit 0