fix single label get
[oweals/gnunet.git] / src / namestore / test_namestore_put_multiple.sh
1 #!/bin/bash
2
3 # Check for required packages
4 if ! [ -x "$(command -v gnunet-namestore)" ]; then
5     echo 'bind/named is not installed' >&2
6     exit 1
7 fi
8
9 # Check if gnunet is running
10 gnunet-arm -I 2&>1 /dev/null
11 ret=$?
12 if [ 0 -ne $ret ]; then
13     echo 'gnunet services are not running'
14     exit 1
15 fi
16
17 ## GNUNET part
18 # Check if identity exists and delets and readds it to get rid of entries in zone
19 gnunet-identity -d | grep randomtestingid 2>&1 /dev/null
20 ret=$?
21
22 if [ 0 -ne $ret ]; then
23     gnunet-identity -D randomtestingid
24     gnunet-identity -C randomtestingid
25 fi
26
27 function minimize_ttl {
28     ttl=10000000
29     arr=$1
30     # parse each element and update ttl to smallest one
31     for i in "${arr[@]}"
32     do
33         currttl=$(echo -n "$i" | cut -d' ' -f1)
34         if [ "$currttl"  -lt "$ttl" ]
35         then
36             ttl=$currttl
37         fi
38
39     done
40     echo "$ttl"
41 }
42
43 function get_record_type {
44     arr=$1
45     typ=$(echo -n "${arr[0]}" | cut -d' ' -f2)
46     echo "$typ"
47 }
48
49 function get_value {
50     arr=$1
51     val=$(echo -n "${arr[0]}" | cut -d' ' -f4-)
52     echo "$val"
53 }
54
55 function testing {
56     label=$1
57     records=$2
58     recordstring=""
59     typ=$(get_record_type "${records[@]}")
60     for i in "${records[@]}"
61     do
62         recordstring+="-R $i"
63     done
64     #echo "$recordstring"
65     gnunet-namestore -z randomtestingid -n "$label" "$recordstring" 2>&1  /dev/null
66     if [ 0 -ne $ret ]; then
67         echo "failed to add record $label: $recordstring"
68     fi
69     gnunet-gns -t "$typ" -u foo2.randomtestingid 2>&1 /dev/null
70     if [ 0 -ne $ret ]; then
71         echo "record $label could not be found"
72     fi
73 }
74
75 # TEST CASES
76 # 1
77 echo "Testing adding of single A record with -R"
78 testing test1 "${arr[@]}"
79 # 2
80 echo "Testing adding of multiple A records with -R"
81 declare -a arr=('1200 A n 127.0.0.1' '2400 A n 127.0.0.2')
82 testing test2 "${arr[@]}"
83 # 3
84 echo "Testing adding of multiple different records with -R"
85 declare -a arr=('1200 A n 127.0.0.1' '2400 AAAA n 2002::')
86 testing test3 "${arr[@]}"
87 # 4
88 echo "Testing adding of single GNS2DNS record with -R"
89 declare -a arr=('86400 GNS2DNS n gnu.org@127.0.0.1')
90 testing test4 "${arr[@]}"
91 # 5
92 echo "Testing adding of single GNS2DNS shadow record with -R"
93 declare -a arr=('86409 GNS2DNS s gnu.org@127.0.0.250')
94 testing test5 "${arr[@]}"
95 # 6
96 echo "Testing adding of multiple GNS2DNS record with -R"
97 declare -a arr=('1 GNS2DNS n gnunet.org@127.0.0.1' '3600 GNS2DNS s gnunet.org@127.0.0.2')
98 testing test6 "${arr[@]}"
99 val=$(gnunet-gns -t GNS2DNS -u test6.randomtestingid)
100 if [[ $val == *"127.0.0.1"* ]]; then
101     echo "shadow!"
102 fi
103 echo "Sleeping to let record expire"
104 sleep 5
105 val=$(gnunet-gns -t GNS2DNS -u test6.randomtestingid)
106 if [[ $val == *"127.0.0.2"* ]]; then
107     echo "no shadow!"
108 fi
109 # 7
110 echo "Testing adding MX record with -R"
111 declare -a arr=('3600 MX n 10,mail')
112 testing test7 "${arr[@]}"
113 # 8
114 echo "Testing adding TXT record with -R"
115 declare -a arr=('3600 TXT n Pretty_Unicorns')
116 testing test8 "${arr[@]}"
117 # 8
118 echo "Testing adding TXT record with -R"
119 declare -a arr=('3600 SRV n _autodiscover_old._tcp.bfh.ch.')
120 testing test8 "${arr[@]}"
121
122 # CLEANUP
123 gnunet-identity -D randomtestingid