Skip to content

Commit 0088554

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

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

random/random.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ func RandIPv4Private() net.IP {
184184
func RandIPv4Loopback() net.IP {
185185
return net.IPv4(
186186
127,
187-
0,
188-
0,
189-
1,
187+
byte(RandIntRange(1, 256)),
188+
byte(RandIntRange(1, 256)),
189+
byte(RandIntRange(1, 256)),
190190
)
191191
}
192192

random/random_test.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,8 @@ func TestRandIPv4Private(t *testing.T) {
204204
t.Error("Generated IP is not a valid IPv4 address: " + r.String())
205205
}
206206

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-
}
207+
// Check if the IP is private
208+
isPrivate := r.IsPrivate()
224209

225210
if !isPrivate {
226211
t.Error("Generated IP is not in a private range: " + r.String())
@@ -229,7 +214,7 @@ func TestRandIPv4Private(t *testing.T) {
229214
}
230215

231216
func TestRandIPv4Loopback(t *testing.T) {
232-
for range 100 {
217+
for range 5 {
233218
r := RandIPv4Loopback()
234219
parsed := net.ParseIP(r.String())
235220
if parsed == nil {
@@ -239,10 +224,11 @@ func TestRandIPv4Loopback(t *testing.T) {
239224
t.Error("Generated IP is not a valid IPv4 address: " + r.String())
240225
}
241226

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())
227+
// Check if the IP is loopback
228+
isLoopback := r.IsLoopback()
229+
230+
if !isLoopback {
231+
t.Error("Generated IP is not the expected loopback address:" + r.String())
246232
}
247233
}
248234
}

0 commit comments

Comments
 (0)