Skip to content

Commit 03b8a57

Browse files
committed
Fix: Update
Signed-off-by: vr-varad <varadgupta21@gmail.com>
1 parent 37b156b commit 03b8a57

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

random/random_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,57 @@ func TestRandIPv6(t *testing.T) {
192192
}
193193
}
194194
}
195+
196+
func TestRandIPv4Private(t *testing.T) {
197+
for range 100 {
198+
r := RandIPv4Private()
199+
parsed := net.ParseIP(r.String())
200+
if parsed == nil {
201+
t.Error("Generated IP is nil: " + r.String())
202+
}
203+
if parsed.To4() == nil {
204+
t.Error("Generated IP is not a valid IPv4 address: " + r.String())
205+
}
206+
207+
// Check if the IP is in one of the private ranges
208+
isPrivate := false
209+
210+
// 10.0.0.0/8 (10.0.0.0 - 10.255.255.255)
211+
if r[0] == 10 {
212+
isPrivate = true
213+
}
214+
215+
// 172.16.0.0/12 (172.16.0.0 - 172.31.255.255)
216+
if r[0] == 172 && r[1] >= 16 && r[1] <= 31 {
217+
isPrivate = true
218+
}
219+
220+
// 192.168.0.0/16 (192.168.0.0 - 192.168.255.255)
221+
if r[0] == 192 && r[1] == 168 {
222+
isPrivate = true
223+
}
224+
225+
if !isPrivate {
226+
t.Error("Generated IP is not in a private range: " + r.String())
227+
}
228+
}
229+
}
230+
231+
func TestRandIPv4Loopback(t *testing.T) {
232+
for range 100 {
233+
r := RandIPv4Loopback()
234+
parsed := net.ParseIP(r.String())
235+
if parsed == nil {
236+
t.Error("Generated IP is nil: " + r.String())
237+
}
238+
if parsed.To4() == nil {
239+
t.Error("Generated IP is not a valid IPv4 address: " + r.String())
240+
}
241+
242+
// Check if it's exactly 127.0.0.1
243+
expected := "127.0.0.1"
244+
if r.String() != expected {
245+
t.Error("Generated IP is not the expected loopback address. Expected: " + expected + ", Got: " + r.String())
246+
}
247+
}
248+
}

0 commit comments

Comments
 (0)