blob: a1dffb1828a843c4d0c8b90f892a7d091bb71be0 [file] [log] [blame]
Yanbiao Lie00f7f02015-05-20 20:20:10 -07001#!/usr/bin/env bash
2defaultIdentity=
3repoDir="test-repo"
4repoConf="$repoDir/test.conf"
5repoDataPrefix='ndn:/Z'
6repoHost='localhost'
7repoPort='9527'
8nfdConf='host.nfd.conf'
Yanbiao Li526b5642016-11-17 01:42:27 +08009systemNfdConf='/usr/local/etc/ndn/nfd.conf.sample'
Yanbiao Lie00f7f02015-05-20 20:20:10 -070010testLog='host.test.log'
11nfdLog='host.nfd.log'
12
13LOG() {
14 echo "[A] $1"
15 echo "[A] $1" >>$testLog
16}
17
18kill_process() {
19 # $1: name of process
20 if [[ $# -lt 1 ]]; then
21 echo "require name of process"
22 exit 1
23 fi
24
25 if pgrep -x $1 > /dev/null; then
26 sudo -b killall -w "$1" >>$testLog 2>&1
27 fi
28}
29
30delete_identity() {
31 # $1: name of identity
32 identity=$1 && identity=${identity#ndn:}
33 if [[ $(ndnsec-list | grep -c "$identity$") -gt 0 ]]; then
34 LOG " deleting identity $1"
35 ndnsec-delete $identity >>$testLog 2>&1
36 else
37 LOG " identity $identity does not exist"
38 fi
39}
40
41clean_up() {
42 LOG "Killing processeses"
43 kill_process "ndn-repo-ng"
44 kill_process "nfd"
45 kill_process "ndnpingserver"
46
47 LOG "Deleting repo data"
48 [[ -d $repoDir ]] && rm -rf $repoDir
49
50 LOG "Deleting identities"
51 delete_identity ndn:/Z
52 delete_identity ndn:/Z/A
53 delete_identity ndn:/Z/A/rib
54 delete_identity ndn:/Z/A/B/C
55 delete_identity ndn:/Z/A/B/C/D/E/rib
56 delete_identity ndn:/Z/F
57
58 if [[ -n "$defaultIdentity" ]]; then
59 if [[ $(ndnsec-list | grep -c "$defaultIdentity$") -gt 0 ]]; then
60 ndnsec-set-default $defaultIdentity
61 fi
62 else
63 r=$(ndnsec-list)
64 if [[ -n "$r" ]]; then
65 r=`echo "$r" | tr "\n" "-"`
66 r=${r%%-*} && r=${r#*/} && r="/$r"
67 ndnsec-set-default $r
68 fi
69 fi
70}
71
72create_identity() {
73 # $1: creating identity
74 # $2: signing identity
75 if [[ $# -gt 1 ]]; then
76 ndnsec-key-gen -n $1 > tmp.req
77 ndnsec-cert-gen -N "tmp" -s $2 -r tmp.req > tmp.cert; rm tmp.req
78 ndnsec-cert-install tmp.cert >>$testLog 2>&1
79 [[ -f tmp.cert ]] && rm tmp.cert
80 elif [[ $# -gt 0 ]]; then
81 ndnsec-key-gen -n $1 | ndnsec-cert-install - >>$testLog 2>&1
82 else
83 echo "require input identity"
84 exit 1
85 fi
86}
87
88publish_default_cert() {
89 # $1: name of identity
90 if [[ $# -lt 1 ]]; then
91 echo "require identity to publish cert"
92 exit 1
93 fi
94
95 if [[ "$1"x = "${1#${repoDataPrefix}}"x ]]; then
96 echo "can not publish $1 under $repoDataPrefix"
97 exit 1
98 fi
99
100 ndnsec-cert-dump -r -H $repoHost -P $repoPort -i $1
101}
102
103start_repo() {
104 LOG "create repo dir config file for repo"
105 [[ -d $repoDir ]] || mkdir -p $repoDir
106
107 echo "" > $repoConf # create repo conf file
108
109 infoedit -f $repoConf -s repo.data.prefix -v $repoDataPrefix
110 infoedit -f $repoConf -s repo.command.prefix -v ""
111 infoedit -f $repoConf -s repo.storage.method -v sqlite
112 infoedit -f $repoConf -s repo.storage.path -v $repoDir
113 infoedit -f $repoConf -s repo.storage.max-packets -v 100
114 infoedit -f $repoConf -s repo.tcp_bulk_insert.host -v $repoHost
115 infoedit -f $repoConf -s repo.tcp_bulk_insert.port -v $repoPort
116 infoedit -f $repoConf -s repo.validator.trust-anchor.type -v any
117
118 kill_process ndn-repo-ng
119 LOG "start repo-ng"
120 nohup ndn-repo-ng -c $repoConf >>$testLog 2>&1 &
121}
122
123start_nfd() {
Yanbiao Li7112c892016-01-24 15:48:10 -0800124 LOG "create nfd conf file with auto_prefix_propagate section specified for test"
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700125 [[ ! -f $systemNfdConf ]] && LOG "can not find nfd config file" && exit 1
126
127 cp $systemNfdConf $nfdConf
Yanbiao Li7112c892016-01-24 15:48:10 -0800128 infoedit -f $nfdConf -s rib.auto_prefix_propagate.cost -v 15
129 infoedit -f $nfdConf -s rib.auto_prefix_propagate.timeout -v 10000
130 infoedit -f $nfdConf -s rib.auto_prefix_propagate.base_retry_wait -v 50
131 infoedit -f $nfdConf -s rib.auto_prefix_propagate.max_retry_wait -v 3600
132 infoedit -f $nfdConf -s rib.auto_prefix_propagate.refresh_interval -v 15
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700133
134 kill_process nfd
135 LOG "start NFD"
136 sudo nohup nfd --config $nfdConf >>$nfdLog 2>&1 &
137}
138
139prepare_for_test() {
140 echo "Start test log on end host" >$testLog
141 echo "Start nfd log on end host" >$nfdLog
142
143 start_nfd && sleep 2
144
145 start_repo && sleep 2
146
147 defaultIdentity=$(ndnsec-get-default)
148
149 # create identities and publish their certificates
150 LOG "create identity /Z/A and publish its cert"
151 create_identity ndn:/Z/A
152 publish_default_cert ndn:/Z/A
153
154 LOG "create identity /Z/A/nrd and publish its cert"
155 create_identity ndn:/Z/A/rib ndn:/Z/A
156 publish_default_cert ndn:/Z/A/rib
157
158 LOG "create identity /Z/A/B/C and publish its cert"
159 create_identity ndn:/Z/A/B/C ndn:/Z/A
160 publish_default_cert ndn:/Z/A/B/C
161
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700162 LOG "create identity /Z/F and publish its cert"
Yanbiao Li7112c892016-01-24 15:48:10 -0800163 create_identity ndn:/Z/F
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700164 publish_default_cert ndn:/Z/F
165}
166
167generate_anchor() {
Yanbiao Li526b5642016-11-17 01:42:27 +0800168 certFile='anchor.cert'
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700169 ndnsec-export -o $certFile $1 >>$testLog 2>&1
Yanbiao Li526b5642016-11-17 01:42:27 +0800170 echo $certFile
Yanbiao Lie00f7f02015-05-20 20:20:10 -0700171}
172
173while getopts "a:cp" arg
174 do
175 case $arg in
176 a)
177 generate_anchor $OPTARG
178 ;;
179 c)
180 clean_up
181 ;;
182 p)
183 prepare_for_test
184 ;;
185 ?)
186 echo "unknown argument"
187 exit 1
188 ;;
189 esac
190done