local_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. kyoketsu, a Client-To-Client Network Enumeration System
  5. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  6. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  7. Everyone is permitted to copy and distribute verbatim copies
  8. of this license document, but changing it is not allowed.
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. See the GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package kyoketsu
  21. import (
  22. "encoding/json"
  23. "fmt"
  24. "log"
  25. "net"
  26. "os"
  27. "testing"
  28. )
  29. type IpAddresses struct {
  30. Addrs []string `json:"addresses"`
  31. }
  32. func LoadTestAddresses(loc string) map[string]struct{} {
  33. b, err := os.ReadFile(loc)
  34. if err != nil {
  35. log.Fatal("Test setup failed.\n", err)
  36. }
  37. var addr IpAddresses
  38. addrmap := map[string]struct{}{}
  39. err = json.Unmarshal(b, &addr)
  40. if err != nil {
  41. log.Fatal("test setup failed.\n", err)
  42. }
  43. for i := range addr.Addrs {
  44. addrmap[addr.Addrs[i]] = struct{}{}
  45. }
  46. return addrmap
  47. }
  48. // Testing the addres recursion function to return all IPs in the target address subnet
  49. // All test cases use a select sort to assert that all addresses in the test data are in the return
  50. func TestAddressRecurse(t *testing.T) {
  51. type TestCase struct {
  52. Name string
  53. TestData string
  54. InputAddr string
  55. InputMask int
  56. ShouldFail bool
  57. }
  58. tc := []TestCase{
  59. TestCase{
  60. Name: "Passing testcase with valid IP address, returns all addresses.",
  61. TestData: "../test/local_ips.json",
  62. InputAddr: "192.168.50.50",
  63. InputMask: 24,
  64. },
  65. TestCase{
  66. Name: "Passing testcase with valid IP address that belongs to a /16 subnet",
  67. TestData: "../test/slash16_ips.json",
  68. InputAddr: "10.252.1.1",
  69. InputMask: 16,
  70. },
  71. }
  72. for i := range tc {
  73. addr, network, err := net.ParseCIDR(fmt.Sprintf("%s/%v", tc[i].InputAddr, tc[i].InputMask))
  74. if err != nil {
  75. t.Errorf("Test case: '%s' failed! Reason: %s", tc[i].Name, err)
  76. }
  77. got := &IpSubnetMapper{}
  78. got.Mask = tc[i].InputMask
  79. got.NetworkAddr = addr.Mask(network.Mask)
  80. got.Current = addr.Mask(network.Mask)
  81. addressRecurse(got)
  82. want := LoadTestAddresses(tc[i].TestData)
  83. for x := range got.Ipv4s {
  84. gotip := got.Ipv4s[x]
  85. _, ok := want[gotip.String()]
  86. if !ok {
  87. t.Errorf("Test '%s' failed! Address: %s was not found in the test data: %s\n", tc[i].Name, gotip.String(), tc[i].TestData)
  88. }
  89. }
  90. t.Logf("Nice! Test: '%s' passed!\n", tc[i].Name)
  91. }
  92. }
  93. // Testing the function to retrieve the next network address
  94. func TestGetNextAddr(t *testing.T) {
  95. type TestCase struct {
  96. Name string
  97. Input string
  98. Wants string
  99. ShouldFail bool
  100. }
  101. tc := []TestCase{
  102. TestCase{
  103. Name: "Passing test case, function returns the next address",
  104. Input: "10.252.1.1",
  105. Wants: "10.252.1.2",
  106. ShouldFail: false,
  107. },
  108. TestCase{
  109. Name: "Failing test case, function returns the wrong address",
  110. Input: "10.252.1.1",
  111. Wants: "10.252.1.4",
  112. ShouldFail: true,
  113. },
  114. }
  115. for i := range tc {
  116. got := getNextAddr(tc[i].Input)
  117. if got != tc[i].Wants {
  118. if !tc[i].ShouldFail {
  119. 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)
  120. }
  121. }
  122. t.Logf("Test: '%s' passed!\n", tc[i].Name)
  123. }
  124. }
  125. func TestGetNetwork(t *testing.T) {
  126. type TestCase struct {
  127. Name string
  128. InputAddr string
  129. InputMask int
  130. Expects string
  131. ShouldFail bool
  132. }
  133. tc := []TestCase{
  134. TestCase{
  135. Name: "Passing test, function returns the correct network given the CIDR mask",
  136. InputAddr: "192.168.50.35",
  137. InputMask: 24,
  138. Expects: "192.168.50.0",
  139. ShouldFail: false,
  140. },
  141. TestCase{
  142. Name: "Passing test, function returns the correct network given the CIDR mask (Larger network, /16 CIDR)",
  143. InputAddr: "10.252.47.200",
  144. InputMask: 16,
  145. Expects: "10.252.0.0",
  146. ShouldFail: false,
  147. },
  148. }
  149. for i := range tc {
  150. got := getNetwork(tc[i].InputAddr, tc[i].InputMask)
  151. if got != tc[i].Expects {
  152. if !tc[i].ShouldFail {
  153. t.Errorf("Test: '%s' failed! Returned: %s\nExpected: %s\nShould fail: %v", tc[i].Name, got, tc[i].Expects, tc[i].ShouldFail)
  154. }
  155. }
  156. t.Logf("Test: '%s' passed!\n", tc[i].Name)
  157. }
  158. }