-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·383 lines (296 loc) · 8.19 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#! /bin/sh
#e.g: ./run -s, which run commands one by one. Pressing any key to continue
#e.g: ./run -b 5, which run from test 5
ROOT="./build"
CMDOUT="/dev/null"
ZONEINFO="zone-details.txt"
DLGTINFO="delegation-details.txt"
TMP=""
ENABLEFILTER=1
BEGINSTEP=4
CURRENTSTEP=1
ERRORN=0
#############
if [ "$1"x == "-s"x ] ; then
STEPBYSTEP=$1
elif [ "$1"x == "-b"x ] ; then
ENABLEFILTER=1
if [ $# -gt 1 ] ; then
BEGINSTEP=$2
fi
fi
#whether to pause and wait for input when execute command
#0 parameters, use global variable $STEPBYSTEP
function do_pause() {
if [ "${STEPBYSTEP}"x = "-s"x ] ;
then
echo "Press Any Key to run: $1"
read
else
echo $1
fi
}
#run functionality related commands
#3 parameters at most, 1 optional. $1 is command to be executed
#$2 (optional) is whether to redirect the output to $CMDOUT (0 is no, else is yes-default.)
#$3 (optional) is whether add ROOT as the prefix of command (0 is no, else is yes-default.)
#$4 (optional) is the expected return value (none is default)
function do_run() {
if [ "$2"x != "0"x ] ;
then
cmd="$1 > $CMDOUT"
fi
#echo "p1=$1, p2(out)=$2, p3(path)=$3, p4(ret)=$4"
if [ "$3"x != "0"x ] ;
then
cmd="${ROOT}/$cmd"
fi
do_pause "$cmd"
eval "$cmd"
ret=$?
#echo $ret
if [ $# -eq 4 ] && [ $4 -lt 0 ] ; then
expected=$[256+$4]
else
expected=$4
fi
if [ $# -eq 4 ] && [ $ret != $expected ] ; then
ERRORN=$[$ERRORN+1]
echo "!!!!!!! $cmd (returned $ret != expected $4) !!!!!!!!!!"
fi
echo
}
#run functionality related commands
#2 parameters at most, 1 optional. $1 is command to be executed
#$2 (optional) is whether to redirect the output to $CMDOUT (0 is yes, else is no-default.)
function do_run_daemon() {
cmd="nohup ${ROOT}/$1"
do_run "$cmd" "$2" 0
}
#will create the keys and register the zone on the local ndns
#1 parameter at most, $1 is the zone name
function func_zone_build() {
cmd="zone-create $1 -s"
do_run "$cmd"
LINE=$(tail -n 1 $ZONEINFO)
OLD_IFS="$IFS"
IFS=" "
arr=($LINE)
IFS="$OLD_IFS"
CERT=${arr[4]}
cmd="zone-register $1 $CERT"
do_run "$cmd"
}
#1 parameter at most, $1 is the site's name
function func_site_create() {
cmd="site-create $1 -s"
do_run "$cmd"
}
# parameters at most, $1 is the zone's name
# $2 is the column index (starting from 0)
# return value is stored in global variable TMP
function do_extract_zone_info() {
fin=$ZONEINFO
index=$2
if [ "$2"x == "KSK"x ] ; then
index=1
elif [ "$2"x == "KSK-CERT"x ] ; then
index=2
elif [ "$2"x == "DSK"x ] ; then
index=3
elif [ "$2"x == "DSK-CERT"x ] ; then
index=4
elif [ "$2"x == "KSK-PKS"x ] ; then #KSK's parent signed cert
fin=$DLGTINFO
index=2
fi
line=$(cat $fin | grep "^$1 " | tail -n 1)
OLD_IFS="$IFS"
IFS=" "
arr=($line)
IFS="$OLD_IFS"
#echo $fin, $1, $2, $index
TMP=${arr[$index]}
}
#delegate a subzone on the parent(current working) zone
#4 parameters at most, 2 optional
#$1 is current working zone, $2 is subzone
#$3 is "--rrtype=..", $4 is "--rrdata=..."
function func_zone_delegate() {
LINE=$(cat $ZONEINFO | grep "^$1 " | tail -n 1)
OLD_IFS="$IFS"
IFS=" "
arr=($LINE)
IFS="$OLD_IFS"
p1=${arr[0]}
p3=${arr[3]}
LINE=$(cat $ZONEINFO | grep "^$2 " | tail -n 1)
OLD_IFS="$IFS"
IFS=" "
arr=($LINE)
IFS="$OLD_IFS"
p2=${arr[0]}
p4=${arr[2]}
if [ $# -gt 2 ] ;
then
p5=$3 #--rrtype
p0=site-delegate
else
p5=""
p0=zone-delegate
fi
if [ $# -gt 3 ] ;
then
p6=$4 #--rrdata
else
p6=""
fi
cmd="$p0 $p1 $p2 $p3 $p4 $p5 $p6 -s"
if [ "$1"x = "$p1"x -a "$2"x = "$p2"x ];
then
do_run "$cmd"
else
echo "CMD: $cmd"
echo "ERROR: p1=$p1 p2=$p2 while intented p1=$1 p2=$2"
fi
}
#delegate a site on the parent(current working) zone
#4 parameters at most, 2 optional
#$1 is current working zone, $2 is subzone
#$3 is "--rrtype=..", $4 is "--rrdata=..."
function func_site_delegate() {
if [ $# -lt 3 ] ; then
rrtype="--rrtype=TXT"
else
rrtype="$3"
fi
func_zone_delegate "$1" "$2" "$rrtype" "$4"
}
function func_reset_settings() {
sqlite3 src/db/ndns-local.db < src/db/ndns-db-tables.sql
echo "reset DB"
ndnsec delete -n /
ndnsec delete -n /net
ndnsec delete -n /net/ndnsim
ndnsec delete -n /net/ndnsim/www
ndnsec delete -n /net/ndnsim
ndnsec delete -n /net/ndnsim/doc/www
ndnsec delete -n /net/rtc
ndnsec delete -n /net/rtc/shock
echo "reset local KeyChain"
rm $ZONEINFO
rm $DLGTINFO
rm *.cert
}
function test_add_zone_and_site() {
func_zone_build "/"
func_zone_build "/net"
func_zone_delegate "/" "/net"
func_site_create "/net/ndnsim/doc/www"
func_zone_build "/net/ndnsim"
func_zone_delegate "/net" "/net/ndnsim"
#if not specified the rrtype (by$3), will create a TXT
func_site_delegate "/net/ndnsim" "/net/ndnsim/doc/www"
func_site_create "/net/ndnsim/www"
func_site_delegate "/net/ndnsim" "/net/ndnsim/www" "--rrtype=TXT"
func_zone_build "/net/rtc"
func_zone_delegate "/net" "/net/rtc"
func_site_create "/net/rtc/shock"
func_site_delegate "/net/rtc" "/net/rtc/shock" "--rrtype=FH" "--rrdata=/att"
}
function func_start_daemon() {
pkill server-daemon
pkill resolver-daemon
cmd="server-daemon / / > root.log &"
do_run_daemon "$cmd" 0
cmd="server-daemon /net /net > net.log &"
do_run_daemon "$cmd" 0
cmd="server-daemon /net/ndnsim /net/ndnsim > net-ndnsim.log &"
do_run_daemon "$cmd" 0
cmd="server-daemon /net/rtc /net/rtc > net-rtc.log &"
do_run_daemon "$cmd" 0
cmd="resolver-daemon /lo > resolver.log &"
do_run_daemon "$cmd" 0
sleep 5
}
function test_dig() {
cmd="dig /net/ndnsim/www /lo"
do_run "$cmd" 1 1 1
# $1=cmd $2=out-redirect $3=root-path-append $4=expected-ret
cmd="dig /net/ndnsim/www /lo --rrtype=TXT"
do_run "$cmd" 1 1 1
cmd="dig /net/ndnsim /lo --rrtype=NS"
do_run "$cmd" 1 1 1
cmd="dig /net/ndnsim2 /lo --rrtype=NS"
do_run "$cmd" 1 1 0
cmd="dig /net/ndnsim/doc/www /lo --rrtype=TXT"
do_run "$cmd" 1 1 1
cmd="dig /net/ndnsim/doc/www /lo --rrtype=FH"
do_run "$cmd" 1 1 0
#note: get none Data back, if the rrtype is illegal
cmd="dig /net/rtc/shock /lo --rrtype=FH2"
do_run "$cmd" 1 1 -1
#warning: returned rrst's rrset and zone info is not right
cmd="dig /net/rtc/shock /lo --rrtype=FH"
do_run "$cmd" 0 1 1
do_extract_zone_info "/net" "DSK"
key=$TMP
cmd="dig $key /lo --rrtype=ID-CERT"
do_run "$cmd" 1 1 1
do_extract_zone_info "/net" "KSK-PKS"
key=$TMP
cmd="dig $key /lo --rrtype=ID-CERT"
do_run "$cmd" 1 1 1
}
function test_update() {
cmd="update /net/rtc /shock FH /ucla ADD"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/shock /lo --rrtype=FH"
do_run "$cmd" 1 1 2
cmd="update /net/rtc /shock FH /ucla REMOVE"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/shock /lo --rrtype=FH"
do_run "$cmd" 0 1 1
cmd="update /net/rtc /shock FH /ucla ADD"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/shock /lo --rrtype=FH"
do_run "$cmd" 0 1 2
cmd="update /net/rtc /shock FH /t-mobile REPLACE-ALL"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/shock /lo --rrtype=FH"
do_run "$cmd" 0 1 1
#add non-existing rrset
cmd="update /net/rtc /alex FH /sprint ADD"
do_run "$cmd" 1 1 1
#duplicate ADD ($cmd is changed in do_run)
cmd="update /net/rtc /alex FH /sprint ADD"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/alex /lo --rrtype=FH"
do_run "$cmd" 0 1 1
#remove non-existing rr
cmd="update /net/rtc /alex FH /sprint2 REMOVE"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/alex /lo --rrtype=FH"
do_run "$cmd" 1 1 1
#remove non-existing rrset
cmd="update /net/rtc /yingdi FH /sprint2 REMOVE"
do_run "$cmd" 1 1 1
cmd="dig /net/rtc/yingdi /lo --rrtype=FH"
do_run "$cmd" 1 1 0
}
function filter() {
if [ $ENABLEFILTER -gt 0 ] && [ $CURRENTSTEP -lt $BEGINSTEP ] ; then
echo "pass function $1"
else
echo "CURRENTSTEP=$CURRENTSTEP BEGINSTEP=$BEGINSTEP"
$1
fi
CURRENTSTEP=$[$CURRENTSTEP+1]
}
filter func_reset_settings
filter test_add_zone_and_site
#daemon needs to get the id from database
filter func_start_daemon
filter test_dig
filter test_update
echo "---------------- Errors: ${ERRORN} --------------------"