Yanbiao Li | e00f7f0 | 2015-05-20 20:20:10 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | defaultIdentity= |
| 3 | repoDir="test-repo" |
| 4 | repoConf="$repoDir/test.conf" |
| 5 | repoDataPrefix='ndn:/Z' |
| 6 | repoHost='localhost' |
| 7 | repoPort='9527' |
| 8 | nfdConf='host.nfd.conf' |
| 9 | systemNfdConf='/usr/local/etc/ndn/nfd.conf' |
| 10 | testLog='host.test.log' |
| 11 | nfdLog='host.nfd.log' |
| 12 | |
| 13 | LOG() { |
| 14 | echo "[A] $1" |
| 15 | echo "[A] $1" >>$testLog |
| 16 | } |
| 17 | |
| 18 | kill_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 | |
| 30 | delete_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 | |
| 41 | clean_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 | |
| 72 | create_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 | |
| 88 | publish_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 | |
| 103 | start_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 | |
| 123 | start_nfd() { |
| 124 | LOG "create nfd conf file with remote_register section specified for test" |
| 125 | [[ ! -f $systemNfdConf ]] && LOG "can not find nfd config file" && exit 1 |
| 126 | |
| 127 | cp $systemNfdConf $nfdConf |
| 128 | infoedit -f $nfdConf -s rib.remote_register.cost -v 15 |
| 129 | infoedit -f $nfdConf -s rib.remote_register.timeout -v 10000 |
| 130 | infoedit -f $nfdConf -s rib.remote_register.retry -v 0 |
| 131 | infoedit -f $nfdConf -s rib.remote_register.refresh_interval -v 15 |
| 132 | |
| 133 | kill_process nfd |
| 134 | LOG "start NFD" |
| 135 | sudo nohup nfd --config $nfdConf >>$nfdLog 2>&1 & |
| 136 | } |
| 137 | |
| 138 | prepare_for_test() { |
| 139 | echo "Start test log on end host" >$testLog |
| 140 | echo "Start nfd log on end host" >$nfdLog |
| 141 | |
| 142 | start_nfd && sleep 2 |
| 143 | |
| 144 | start_repo && sleep 2 |
| 145 | |
| 146 | defaultIdentity=$(ndnsec-get-default) |
| 147 | |
| 148 | # create identities and publish their certificates |
| 149 | LOG "create identity /Z/A and publish its cert" |
| 150 | create_identity ndn:/Z/A |
| 151 | publish_default_cert ndn:/Z/A |
| 152 | |
| 153 | LOG "create identity /Z/A/nrd and publish its cert" |
| 154 | create_identity ndn:/Z/A/rib ndn:/Z/A |
| 155 | publish_default_cert ndn:/Z/A/rib |
| 156 | |
| 157 | LOG "create identity /Z/A/B/C and publish its cert" |
| 158 | create_identity ndn:/Z/A/B/C ndn:/Z/A |
| 159 | publish_default_cert ndn:/Z/A/B/C |
| 160 | |
| 161 | LOG "create identity /Z/A/B/C/D/E/nrd and publish its cert" |
| 162 | create_identity ndn:/Z/A/B/C/D/E/rib ndn:/Z/A/B/C |
| 163 | publish_default_cert ndn:/Z/A/B/C/D/E/rib |
| 164 | |
| 165 | LOG "create identity /Z/F and publish its cert" |
| 166 | create_identity ndn:/Z/F ndn:/Z |
| 167 | publish_default_cert ndn:/Z/F |
| 168 | } |
| 169 | |
| 170 | generate_anchor() { |
| 171 | certFile='remote-register.hub.anchor.cert' |
| 172 | ndnsec-export -o $certFile $1 >>$testLog 2>&1 |
| 173 | cert=$(cat $certFile) && rm $certFile |
| 174 | echo $cert |
| 175 | } |
| 176 | |
| 177 | while getopts "a:cp" arg |
| 178 | do |
| 179 | case $arg in |
| 180 | a) |
| 181 | generate_anchor $OPTARG |
| 182 | ;; |
| 183 | c) |
| 184 | clean_up |
| 185 | ;; |
| 186 | p) |
| 187 | prepare_for_test |
| 188 | ;; |
| 189 | ?) |
| 190 | echo "unknown argument" |
| 191 | exit 1 |
| 192 | ;; |
| 193 | esac |
| 194 | done |