local_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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{Mask: tc[i].InputMask, NetworkAddr: addr.Mask(network.Mask), Ipv4s: []net.IP{addr}}
  78. addressRecurse(got)
  79. want := LoadTestAddresses(tc[i].TestData)
  80. for x := range got.Ipv4s {
  81. t.Logf("%s\n", got.Ipv4s[x])
  82. gotip := got.Ipv4s[x]
  83. _, ok := want[gotip.String()]
  84. if !ok {
  85. t.Errorf("Test '%s' failed! Address: %s was not found in the test data: %s\n", tc[i].Name, gotip.String(), tc[i].TestData)
  86. }
  87. }
  88. t.Logf("Nice! Test: '%s' passed!\n", tc[i].Name)
  89. }
  90. }
  91. // Testing the function to retrieve the next network address
  92. func TestGetNextAddr(t *testing.T) {
  93. type TestCase struct {
  94. Name string
  95. Input string
  96. Wants string
  97. ShouldFail bool
  98. }
  99. tc := []TestCase{
  100. TestCase{
  101. Name: "Passing test case, function returns the next address",
  102. Input: "10.252.1.1",
  103. Wants: "10.252.1.2",
  104. ShouldFail: false,
  105. },
  106. TestCase{
  107. Name: "Failing test case, function returns the wrong address",
  108. Input: "10.252.1.1",
  109. Wants: "10.252.1.4",
  110. ShouldFail: true,
  111. },
  112. }
  113. for i := range tc {
  114. got := getNextAddr(tc[i].Input)
  115. if got != tc[i].Wants {
  116. if !tc[i].ShouldFail {
  117. 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)
  118. }
  119. }
  120. t.Logf("Test: '%s' passed!\n", tc[i].Name)
  121. }
  122. }
  123. func TestGetNetwork(t *testing.T) {
  124. type TestCase struct {
  125. Name string
  126. InputAddr string
  127. InputMask int
  128. Expects string
  129. ShouldFail bool
  130. }
  131. tc := []TestCase{
  132. TestCase{
  133. Name: "Passing test, function returns the correct network given the CIDR mask",
  134. InputAddr: "192.168.50.35",
  135. InputMask: 24,
  136. Expects: "192.168.50.0",
  137. ShouldFail: false,
  138. },
  139. TestCase{
  140. Name: "Passing test, function returns the correct network given the CIDR mask (Larger network, /16 CIDR)",
  141. InputAddr: "10.252.47.200",
  142. InputMask: 16,
  143. Expects: "10.252.0.0",
  144. ShouldFail: false,
  145. },
  146. }
  147. for i := range tc {
  148. got := getNetwork(tc[i].InputAddr, tc[i].InputMask)
  149. if got != tc[i].Expects {
  150. if !tc[i].ShouldFail {
  151. t.Errorf("Test: '%s' failed! Returned: %s\nExpected: %s\nShould fail: %v", tc[i].Name, got, tc[i].Expects, tc[i].ShouldFail)
  152. }
  153. }
  154. t.Logf("Test: '%s' passed!\n", tc[i].Name)
  155. }
  156. }
  157. func TestGetNetworkAddresses(t *testing.T) {
  158. type TestCase struct {
  159. Name string
  160. TestData string
  161. InputAddr string
  162. ShouldFail bool
  163. }
  164. tc := []TestCase{
  165. TestCase{
  166. Name: "Passing testcase with valid IP address, returns all addresses.",
  167. TestData: "../test/local_ips.json",
  168. InputAddr: "192.168.50.50/24",
  169. ShouldFail: false,
  170. },
  171. TestCase{
  172. Name: "Passing testcase with valid IP address that belongs to a /16 subnet",
  173. TestData: "../test/slash16_ips.json",
  174. InputAddr: "10.252.1.1/16",
  175. ShouldFail: false,
  176. },
  177. TestCase{
  178. Name: "Failing testcase with invalid IP address.",
  179. TestData: "../test/slash16_ips.json",
  180. InputAddr: "abcdefgh1234455667",
  181. ShouldFail: true,
  182. },
  183. TestCase{
  184. Name: "Failing testcase with valid IP address, but bad CIDR mask",
  185. TestData: "../test/slash16_ips.json",
  186. InputAddr: "192.168.50.1/deez",
  187. ShouldFail: true,
  188. },
  189. }
  190. for i := range tc {
  191. got, err := GetNetworkAddresses(tc[i].InputAddr)
  192. if err != nil {
  193. if !tc[i].ShouldFail {
  194. t.Errorf("Test: '%s' failed! Error: %s", tc[i].Name, err)
  195. }
  196. continue
  197. }
  198. want := LoadTestAddresses(tc[i].TestData)
  199. for x := range got.Ipv4s {
  200. gotip := got.Ipv4s[x]
  201. _, ok := want[gotip.String()]
  202. if !ok {
  203. if !tc[i].ShouldFail {
  204. t.Errorf("Test '%s' failed! Address: %s was not found in the test data: %s\n", tc[i].Name, gotip.String(), tc[i].TestData)
  205. }
  206. }
  207. }
  208. t.Logf("Test: '%s' passed!", tc[i].Name)
  209. }
  210. }
  211. func TestBitsToMask(t *testing.T) {
  212. type TestCase struct {
  213. Name string
  214. Gets string
  215. Wants string
  216. }
  217. tc := []TestCase{
  218. TestCase{
  219. Name: "Function gets valid IP with cidr",
  220. Gets: "192.168.50.1/28",
  221. Wants: "255.255.255.240",
  222. },
  223. }
  224. for i := range tc {
  225. _, net, err := net.ParseCIDR(tc[i].Gets)
  226. if err != nil {
  227. log.Fatal(err)
  228. }
  229. want := tc[i].Wants
  230. got := bitsToMask(net.Mask.Size())
  231. if got != want {
  232. t.Errorf("test '%s' failed! got: %s\nwanted: %s\n", tc[i].Name, got, want)
  233. }
  234. }
  235. }