Skip to content

Commit

Permalink
added few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saschamonteiro committed Jul 27, 2024
1 parent 4a070e8 commit b598904
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"testing"
)

func TestHostsFromCIDR(t *testing.T) {
tests := []struct {
name string
cidr string
length int
first string
last string
shouldError string
}{
{
name: "subnet/32-singlehost",
cidr: "192.168.10.110/32",
length: 1,
first: "192.168.10.110",
}, {
name: "subnet/24",
cidr: "192.168.10.0/24",
length: 254,
first: "192.168.10.1",
last: "192.168.10.254",
},
{
name: "subnet/16",
cidr: "192.168.0.0/16",
length: 65534,
first: "192.168.0.1",
last: "192.168.255.254",
},
{
name: "no subnet",
cidr: "192.168.10.0",
length: 0,
shouldError: "invalid CIDR address: 192.168.10.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hosts, err := hostsFromCIDR(tt.cidr)
if err != nil {
if tt.shouldError != "" {
if err.Error() != tt.shouldError {
t.Errorf("hostsFromCIDR() error = %v, want %v", err, tt.shouldError)
}
} else {
t.Errorf("hostsFromCIDR() error = %v", err)
}
}
if len(hosts) != tt.length {
t.Errorf("hostsFromCIDR() = %v, want %v", len(hosts), tt.length)
}
if tt.length > 1 {
if hosts[0] != tt.first {
t.Errorf("hostsFromCIDR() = %v, want %v", hosts[0], tt.first)
}
if hosts[len(hosts)-1] != tt.last {
t.Errorf("hostsFromCIDR() = %v, want %v", hosts[len(hosts)-1], tt.last)
}
}
if tt.length == 1 {
if hosts[0] != tt.first {
t.Errorf("hostsFromCIDR() = %v, want %v", hosts[0], tt.first)
}
}
})
}
}

0 comments on commit b598904

Please sign in to comment.