-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.sh
executable file
·185 lines (162 loc) · 4.94 KB
/
test.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
#!/bin/bash
#
testSkipEmptyLines() {
result=$(echo ""|./portqry.sh)
assertEquals \
x"${result}" "x"
}
testSkipCommentLines() {
result=$(echo "#"|./portqry.sh)
assertEquals \
x"${result}" "x"
}
testLocalhostPort2NotListening() {
result=$( (echo "127.0.0.1:2")|./portqry.sh )
# assertEquals \
# "x127.0.0.1:2 NOT LISTENING" x"${result}"
assertContains "${result}" "127.0.0.1:2"
assertContains "${result}" "NOT LISTENING"
}
testValidateInteger() {
while read -r desc arg want; do
got=$(validate_integer "${arg}")
got=$?
#assertTrue "${desc}: validate_integer() unexpected error; return ${rtrn}" ${rtrn}
assertEquals "${desc}: validate_integer(): " "${want}" "${got}"
done <<EOF
int=0 0 0
int=1 1 0
int=2 2 0
maxInt 65535 0
greaterMaxInt 65536 1
intAsString abc 1
negativeInt -1 1
empty "" 1
EOF
}
testTimeoutAcceptInteger() {
result=$( (echo "127.0.0.1:2")|./portqry.sh -t 3 )
assertContains "${result}" "127.0.0.1:2"
assertContains "${result}" "NOT LISTENING"
}
testTimeoutRejectNonInteger() {
(echo "127.0.0.1:2")|./portqry.sh -t d 2>/dev/null
result=$?
assertNotEquals \
"0" "${result}"
}
testUseDefaultPort80() {
result=$( (echo "127.0.0.1")|./portqry.sh )
assertContains "${result}" "127.0.0.1:80"
}
testUsePortFlag() {
result=$( (echo "127.0.0.1")|./portqry.sh -p 3 )
assertContains "${result}" "127.0.0.1:3"
}
testValidatePortTable() {
while read -r desc arg want; do
got=$(validate_port "${arg}")
got=$?
#assertTrue "${desc}: validate_port() unexpected error; return ${rtrn}" ${rtrn}
assertEquals "${desc}: validate_port(): " "${want}" "${got}"
done <<EOF
port=1 1 0
port=2 2 0
maxPort 65535 0
greaterMaxPort 65536 1
port=0 0 1
portAsString abc 1
negativePort -1 1
portRange 2-3 0
inversPortRange 4-3 1
portList 2,3,6 0
invalidPortList 2,xy,6 1
portListWithEmptyPort 2,,3,6 1
empty "" 1
EOF
}
testUsePortFlagTable() {
while read -r desc arg want; do
got=$( (echo "127.0.0.1")|./portqry.sh -p "${arg}" )
assertContains "${desc}: validate_port(): " "${got}" "${want}"
done <<EOF
port=1 1 127.0.0.1:1
port=2 2 127.0.0.1:2
maxPort 65535 127.0.0.1:65535
greaterMaxPort 65536 Invalid port
port=0 0 Invalid port
portAsString abc Invalid port
negativePort -1 Invalid port
inversPortRange 4-3 Invalid port range
invalidPortList 2,xy,6 Invalid port
portListWithEmptyPort 2,,3,6 Invalid port
empty "" Invalid port
EOF
}
testUsePortFlagWithRange() {
result=$( (echo "127.0.0.1")|./portqry.sh -p 2-4 )
assertContains "${result}" "127.0.0.1:2"
assertContains "${result}" "127.0.0.1:3"
assertContains "${result}" "127.0.0.1:4"
}
testUsePortFlagWithList() {
result=$( (echo "127.0.0.1")|./portqry.sh -p 2,3,6 )
assertContains "${result}" "127.0.0.1:2"
assertContains "${result}" "127.0.0.1:3"
assertContains "${result}" "127.0.0.1:6"
}
testLocalhostListeningPorts() {
if [[ "$OSTYPE" == "linux-gnu" ]]; then
for p in $localListeningPorts ; do
result=$( (echo "127.0.0.1:$p")|./portqry.sh )
assertContains "${result}" "127.0.0.1:$p"
assertNotContains "${result}" "NOT LISTENING"
done
fi
}
testLocalhostNotListeningPorts() {
if [[ "$OSTYPE" == "linux-gnu" ]]; then
for p in $localNotListeningPorts ; do
result=$( (echo "127.0.0.1:$p")|./portqry.sh )
assertContains "${result}" "127.0.0.1:$p"
assertContains "${result}" "NOT LISTENING"
done
fi
}
testUsePortFlagWithLocalhostListeningPortList() {
if [[ "$OSTYPE" == "linux-gnu" ]]; then
portlist=${localListeningPorts// /,} # Replace ' ' with ',' cf. https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
portlist=${portlist%,}
result=$( (echo "127.0.0.1")|./portqry.sh -p "$portlist" )
for p in $localListeningPorts ; do
assertContains "${result}" "127.0.0.1:$p"
assertNotContains "${result}" "NOT LISTENING"
done
fi
}
testUsePortFlagWithLocalhostListeningPortRange() {
if [[ "$OSTYPE" == "linux-gnu" ]]; then
set -- ${localListeningPorts}
lo=$(( $1 - 1 ))
hi=$(( $1 + 1 ))
result=$( (echo "127.0.0.1")|./portqry.sh -p "${lo}-${hi}" )
assertContains "${result}" "127.0.0.1:$lo"
assertContains "${result}" "127.0.0.1:$1"
assertContains "${result}" "127.0.0.1:$hi"
assertNotContains "$(echo "${result}"|grep "127.0.0.1:$1" )" "NOT LISTENING"
fi
}
oneTimeSetUp() {
# Load portqry to test.
. ./portqry.sh
if [[ "$OSTYPE" == "linux-gnu" ]]; then
localListeningPorts=$(netstat -tln4 |awk '/^tcp/ {split($4,a,/:/); if (a[1] ~ /(0\.0\.0\.0|127\.0\.0\.1)/) print a[2]}'|sort -n |tr '\n' ' ')
localNotListeningPorts=$(for p in {78..85}; do [[ $localListeningPorts =~ $p ]] || echo $p; done|head -n5)
#elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
fi
}
localListeningPorts=""
localNotListeningPorts=""
# Load and run shUnit2.
. ./test/libs/shunit2/shunit2