local_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package kyoketsu
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net"
  7. "os"
  8. "testing"
  9. )
  10. type IpAddresses struct {
  11. Addrs []string `json:"addresses"`
  12. }
  13. func LoadTestAddresses(loc string) map[string]struct{} {
  14. b, err := os.ReadFile(loc)
  15. if err != nil {
  16. log.Fatal("Test setup failed.\n", err)
  17. }
  18. var addr IpAddresses
  19. addrmap := map[string]struct{}{}
  20. err = json.Unmarshal(b, &addr)
  21. if err != nil {
  22. log.Fatal("test setup failed.\n", err)
  23. }
  24. for i := range addr.Addrs {
  25. addrmap[addr.Addrs[i]] = struct{}{}
  26. }
  27. return addrmap
  28. }
  29. // Testing the addres recursion function to return all IPs in the target address subnet
  30. // All test cases use a select sort to assert that all addresses in the test data are in the return
  31. func TestAddressRecurse(t *testing.T) {
  32. type TestCase struct {
  33. Name string
  34. TestData string
  35. InputAddr string
  36. InputMask int
  37. ShouldFail bool
  38. }
  39. tc := []TestCase{
  40. TestCase{
  41. Name: "Passing testcase with valid IP address, returns all addresses.",
  42. TestData: "../test/local_ips.json",
  43. InputAddr: "192.168.50.50",
  44. InputMask: 24,
  45. },
  46. TestCase{
  47. Name: "Passing testcase with valid IP address that belongs to a /16 subnet",
  48. TestData: "../test/slash16_ips.json",
  49. InputAddr: "10.252.1.1",
  50. InputMask: 16,
  51. },
  52. }
  53. for i := range tc {
  54. addr, network, err := net.ParseCIDR(fmt.Sprintf("%s/%v", tc[i].InputAddr, tc[i].InputMask))
  55. if err != nil {
  56. t.Errorf("Test case: '%s' failed! Reason: %s", tc[i].Name, err)
  57. }
  58. got := &IpSubnetMapper{}
  59. got.Mask = tc[i].InputMask
  60. got.NetworkAddr = addr.Mask(network.Mask)
  61. got.Current = addr.Mask(network.Mask)
  62. addressRecurse(got, 65535)
  63. want := LoadTestAddresses(tc[i].TestData)
  64. for x := range got.Ipv4s {
  65. gotip := got.Ipv4s[x]
  66. _, ok := want[gotip.String()]
  67. if !ok {
  68. t.Errorf("Test '%s' failed! Address: %s was not found in the test data: %s\n", tc[i].Name, gotip.String(), tc[i].TestData)
  69. }
  70. }
  71. log.Printf("Nice! Test: '%s' passed!\n", tc[i].Name)
  72. }
  73. }
  74. // Testing the function to retrieve the next network address
  75. func TestGetNextAddr(t *testing.T) {
  76. type TestCase struct {
  77. Name string
  78. Input string
  79. Wants string
  80. ShouldFail bool
  81. }
  82. tc := []TestCase{
  83. TestCase{
  84. Name: "Passing test case, function returns the next address",
  85. Input: "10.252.1.1",
  86. Wants: "10.252.1.2",
  87. ShouldFail: false,
  88. },
  89. TestCase{
  90. Name: "Failing test case, function returns the wrong address",
  91. Input: "10.252.1.1",
  92. Wants: "10.252.1.4",
  93. ShouldFail: true,
  94. },
  95. }
  96. for i := range tc {
  97. got := getNextAddr(tc[i].Input)
  98. if got != tc[i].Wants {
  99. if !tc[i].ShouldFail {
  100. t.Errorf("Test: '%s' failed! Return: %s\nTest expected: %s\nTest Should fail: %v\n", tc[i].Name, got, tc[i].Wants, tc[i].ShouldFail)
  101. }
  102. }
  103. }
  104. }
  105. func TestGetNetwork(t *testing.T) {
  106. type TestCase struct {
  107. Name string
  108. InputAddr string
  109. InputMask int
  110. Expects string
  111. ShouldFail bool
  112. }
  113. tc := []TestCase{
  114. TestCase{
  115. Name: "Passing test, function returns the correct network given the CIDR mask",
  116. InputAddr: "192.168.50.35",
  117. InputMask: 24,
  118. Expects: "192.168.50.0",
  119. ShouldFail: false,
  120. },
  121. TestCase{
  122. Name: "Passing test, function returns the correct network given the CIDR mask (Larger network, /16 CIDR)",
  123. InputAddr: "10.252.47.200",
  124. InputMask: 16,
  125. Expects: "10.252.0.0",
  126. ShouldFail: false,
  127. },
  128. }
  129. for i := range tc {
  130. got := getNetwork(tc[i].InputAddr, tc[i].InputMask)
  131. if got != tc[i].Expects {
  132. if !tc[i].ShouldFail {
  133. t.Errorf("Test: '%s' failed! Returned: %s\nExpected: %s\nShould fail: %v", tc[i].Name, got, tc[i].Expects, tc[i].ShouldFail)
  134. }
  135. }
  136. }
  137. }