12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package kyoketsu
- import (
- "encoding/json"
- "log"
- "net/netip"
- "os"
- "testing"
- "github.com/google/go-cmp/cmp"
- )
- func LoadTestAddresses(loc string) *AllAddress {
- b, err := os.ReadFile(loc)
- if err != nil {
- log.Fatal("Test setup failed.\n", err)
- }
- var alladdr AllAddress
- err = json.Unmarshal(b, &alladdr)
- if err != nil {
- log.Fatal("test setup failed.\n", err)
- }
- return &alladdr
- }
- // Testing the addres recursion function to return all IPs in the target address subnet
- func TestAddressRecurse(t *testing.T) {
- type TestCase struct {
- Name string
- Wants *AllAddress
- Input string
- ShouldFail bool
- }
- tc := []TestCase{
- TestCase{
- Name: "Passing testcase with valid IP address, returns all addresses.",
- Wants: LoadTestAddresses("../test/local_ips.json"),
- Input: "192.168.50.50",
- },
- TestCase{
- Name: "Passing testcase with valid IP address that belongs to a /16 subnet",
- Wants: LoadTestAddresses("../test/slash16_ips.json"),
- Input: "10.252.1.0",
- },
- }
- for i := range tc {
- addr, err := netip.ParseAddr(tc[i].Input)
- if err != nil {
- t.Errorf("Test case: '%s' failed! Reason: %s", tc[i].Name, err)
- }
- got := &AllAddress{}
- addressRecurse(addr, got, 65535)
- if !cmp.Equal(got, tc[i].Wants) {
- t.Errorf("Test case: '%s' failed! Got: %+v\nWant: %+v\n", tc[i].Name, got, tc[i].Wants)
- }
- }
- }
|