Skip to content

Commit ef939df

Browse files
committed
Don't export vars to global scope in is_ip_rdns_valid and make the function testable
1 parent 37a15bb commit ef939df

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

func/ip.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ is_ip_key_empty() {
2626
}
2727

2828
is_ip_rdns_valid() {
29-
ip="$1"
30-
network_ip=$(echo $ip | cut -d"." -f1-3)
31-
awk_ip=$(echo $network_ip | sed 's|\.|/\&\&/|g')
32-
rev_awk_ip=$(echo $awk_ip | rev)
33-
rdns=$(dig +short -x $ip | sed 's/.$//')
29+
local ip="$1"
30+
local network_ip=$(echo $ip | cut -d"." -f1-3)
31+
local awk_ip=$(echo $network_ip | sed 's|\.|/\&\&/|g')
32+
local rev_awk_ip=$(echo $awk_ip | rev)
3433

35-
if [ ! -z $rdns ] && [ ! $(echo $rdns | awk "/$awk_ip/ || /$rev_awk_ip/") ]; then
34+
[ -z "$rdns" ] && local rdns=$(dig +short -x $ip | head -n 1 | sed 's/.$//')
35+
36+
if [ ! -z "$rdns" ] && [ ! $(echo $rdns | awk "/$awk_ip/ || /$rev_awk_ip/") ]; then
3637
echo $rdns
38+
return 0 # True
3739
fi
40+
41+
return 1 # False
3842
}
3943

4044
# Update ip address value

test/test.bats

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,50 +82,54 @@ function validate_web_domain() {
8282
refute_output
8383
}
8484

85+
#----------------------------------------------------------#
86+
# IP #
87+
#----------------------------------------------------------#
88+
8589
@test "Check reverse Dns validation" {
86-
skip
8790

8891
# 1. PTR record for a IP should return a hostname(reverse) which in turn must resolve to the same IP addr(forward). (Full circle)
92+
# `-> not implemented in `is_ip_rdns_valid` yet and also not tested here
8993
# 2. Reject rPTR records that match generic dynamic IP pool patterns
9094

9195
local ip="54.200.1.22"
9296
local rdns="ec2-54-200-1-22.us-west-2.compute.amazonaws.com"
93-
run is_ip_rdns_valid "$ip" "$rdns"
97+
run is_ip_rdns_valid "$ip"
9498
assert_failure
9599
refute_output
96100

97101
local rdns="ec2.54.200.1.22.us-west-2.compute.amazonaws.com"
98-
run is_ip_rdns_valid "$ip" "$rdns"
102+
run is_ip_rdns_valid "$ip"
99103
assert_failure
100104
refute_output
101105

102106
local rdns="ec2-22-1-200-54.us-west-2.compute.amazonaws.com"
103-
run is_ip_rdns_valid "$ip" "$rdns"
107+
run is_ip_rdns_valid "$ip"
104108
assert_failure
105109
refute_output
106110

107111
local rdns="ec2.22.1.200.54.us-west-2.compute.amazonaws.com"
108-
run is_ip_rdns_valid "$ip" "$rdns"
112+
run is_ip_rdns_valid "$ip"
109113
assert_failure
110114
refute_output
111115

112116
local rdns="ec2-200-54-1-22.us-west-2.compute.amazonaws.com"
113-
run is_ip_rdns_valid "$ip" "$rdns"
117+
run is_ip_rdns_valid "$ip"
114118
assert_failure
115119
refute_output
116120

117121
local rdns="panel-22.mydomain.tld"
118-
run is_ip_rdns_valid "$ip" "$rdns"
122+
run is_ip_rdns_valid "$ip"
119123
assert_success
120124
assert_output "$rdns"
121125

122126
local rdns="mail.mydomain.tld"
123-
run is_ip_rdns_valid "$ip" "$rdns"
127+
run is_ip_rdns_valid "$ip"
124128
assert_success
125129
assert_output "$rdns"
126130

127131
local rdns="mydomain.tld"
128-
run is_ip_rdns_valid "$ip" "$rdns"
132+
run is_ip_rdns_valid "$ip"
129133
assert_success
130134
assert_output "$rdns"
131135

0 commit comments

Comments
 (0)