local_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package kyoketsu
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/netip"
  6. "os"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. )
  10. func LoadTestAddresses(loc string) *AllAddress {
  11. b, err := os.ReadFile(loc)
  12. if err != nil {
  13. log.Fatal("Test setup failed.\n", err)
  14. }
  15. var alladdr AllAddress
  16. err = json.Unmarshal(b, &alladdr)
  17. if err != nil {
  18. log.Fatal("test setup failed.\n", err)
  19. }
  20. return &alladdr
  21. }
  22. // Testing the addres recursion function to return all IPs in the target address subnet
  23. func TestAddressRecurse(t *testing.T) {
  24. type TestCase struct {
  25. Name string
  26. Wants *AllAddress
  27. Input string
  28. ShouldFail bool
  29. }
  30. tc := []TestCase{
  31. TestCase{
  32. Name: "Passing testcase with valid IP address, returns all addresses.",
  33. Wants: LoadTestAddresses("../test/local_ips.json"),
  34. Input: "192.168.50.50",
  35. },
  36. TestCase{
  37. Name: "Passing testcase with valid IP address that belongs to a /16 subnet",
  38. Wants: LoadTestAddresses("../test/slash16_ips.json"),
  39. Input: "10.252.1.0",
  40. },
  41. }
  42. for i := range tc {
  43. addr, err := netip.ParseAddr(tc[i].Input)
  44. if err != nil {
  45. t.Errorf("Test case: '%s' failed! Reason: %s", tc[i].Name, err)
  46. }
  47. got := &AllAddress{}
  48. addressRecurse(addr, got, 65535)
  49. if !cmp.Equal(got, tc[i].Wants) {
  50. t.Errorf("Test case: '%s' failed! Got: %+v\nWant: %+v\n", tc[i].Name, got, tc[i].Wants)
  51. }
  52. }
  53. }