Skip to content

Commit

Permalink
feat: unit-tests and it pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
docent-net committed Jan 5, 2022
1 parent afa9fb3 commit 9b9cad6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
7 changes: 7 additions & 0 deletions git-hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/sh

go test -v ./...
if [ $? -ne 0 ]; then
echo "unit tests failed"
exit 1
fi
5 changes: 2 additions & 3 deletions subnet/subnetContains.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package subnet
import (
"fmt"
"net"
"os"
)

func SubnetContains(args []string) bool {
Expand All @@ -13,7 +12,7 @@ func SubnetContains(args []string) bool {
_, ipv4Net, err := net.ParseCIDR(args[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
return false
}

ipv4Addr := net.ParseIP(args[1])
Expand All @@ -22,7 +21,7 @@ func SubnetContains(args []string) bool {
_, ipv4Net2, err := net.ParseCIDR(args[1])
if err != nil {
fmt.Println(string(args[1]) + " is not recognized as IPv4 address nor a network")
os.Exit(1)
return false
}
// checking if subnet contains another subnet:
if subnetContainsSubnet(ipv4Net, ipv4Net2) {
Expand Down
54 changes: 54 additions & 0 deletions tests/subnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tests

import (
"testing"
"github.com/docent-net/ipcalc-contains/subnet"
)

func TestSubnetContainsShouldFailUncrecognizedIPv4Network(t *testing.T) {
args := []string{"aaa", "192.168.123.0/30"}

if subnet.SubnetContains(args) != false {
t.Errorf("Expected %s to fail CIDR verification, but it did succeed", args[0])
}
}

func TestSubnetContainsShouldFailUncrecognizedIPv4(t *testing.T) {
args := []string{"192.168.1.0/20", "abc"}

if subnet.SubnetContains(args) != false {
t.Errorf("Expected %s to fail CIDR verification, but it did succeed", args[1])
}
}

func TestSubnetContainsNetworkShouldContainNetwork(t *testing.T) {
args := []string{"192.168.0.0/20", "192.168.12.0/30"}

if subnet.SubnetContains(args) != true {
t.Errorf("Expected %s to contain %s, but it failed", args[0], args[1])
}
}

func TestSubnetContainsNetworkShouldNotContainNetwork(t *testing.T) {
args := []string{"192.168.0.0/20", "192.168.128.0/30"}

if subnet.SubnetContains(args) != false {
t.Errorf("Expected %s to NOT contain %s, but it did", args[0], args[1])
}
}

func TestSubnetContainsNetworkShouldContainIPv4(t *testing.T) {
args := []string{"192.168.0.0/20", "192.168.12.123"}

if subnet.SubnetContains(args) != true {
t.Errorf("Expected %s to contain %s, but it failed", args[0], args[1])
}
}

func TestSubnetContainsNetworkShouldNotContainIPv4(t *testing.T) {
args := []string{"192.168.0.0/20", "10.0.0.12"}

if subnet.SubnetContains(args) != false {
t.Errorf("Expected %s to NOT contain %s, but it did", args[0], args[1])
}
}

0 comments on commit 9b9cad6

Please sign in to comment.