net_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package network
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/evilsocket/islazy/data"
  6. )
  7. func TestIsZeroMac(t *testing.T) {
  8. exampleMAC, _ := net.ParseMAC("00:00:00:00:00:00")
  9. exp := true
  10. got := IsZeroMac(exampleMAC)
  11. if got != exp {
  12. t.Fatalf("expected '%t', got '%t'", exp, got)
  13. }
  14. }
  15. func TestIsBroadcastMac(t *testing.T) {
  16. exampleMAC, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
  17. exp := true
  18. got := IsBroadcastMac(exampleMAC)
  19. if got != exp {
  20. t.Fatalf("expected '%t', got '%t'", exp, got)
  21. }
  22. }
  23. func TestNormalizeMac(t *testing.T) {
  24. exp := "ff:ff:ff:ff:ff:ff"
  25. got := NormalizeMac("fF-fF-fF-fF-fF-fF")
  26. if got != exp {
  27. t.Fatalf("expected '%s', got '%s'", exp, got)
  28. }
  29. }
  30. // TODO: refactor to parse targets with an actual alias map
  31. func TestParseTargets(t *testing.T) {
  32. aliasMap, err := data.NewMemUnsortedKV()
  33. if err != nil {
  34. panic(err)
  35. }
  36. aliasMap.Set("5c:00:0b:90:a9:f0", "test_alias")
  37. aliasMap.Set("5c:00:0b:90:a9:f1", "Home_Laptop")
  38. cases := []struct {
  39. Name string
  40. InputTargets string
  41. InputAliases *data.UnsortedKV
  42. ExpectedIPCount int
  43. ExpectedMACCount int
  44. ExpectedError bool
  45. }{
  46. // Not sure how to trigger sad path where macParser.FindAllString()
  47. // finds a MAC but net.ParseMac() fails on the result.
  48. {
  49. "empty target string causes empty return",
  50. "",
  51. &data.UnsortedKV{},
  52. 0,
  53. 0,
  54. false,
  55. },
  56. {
  57. "MACs are parsed",
  58. "192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0, 6C:00:0B:90:A9:F0",
  59. &data.UnsortedKV{},
  60. 2,
  61. 3,
  62. false,
  63. },
  64. {
  65. "Aliases are parsed",
  66. "test_alias, Home_Laptop",
  67. aliasMap,
  68. 0,
  69. 2,
  70. false,
  71. },
  72. }
  73. for _, test := range cases {
  74. t.Run(test.Name, func(t *testing.T) {
  75. ips, macs, err := ParseTargets(test.InputTargets, test.InputAliases)
  76. if err != nil && !test.ExpectedError {
  77. t.Errorf("unexpected error: %s", err)
  78. }
  79. if err == nil && test.ExpectedError {
  80. t.Error("Expected error, but got none")
  81. }
  82. if test.ExpectedError {
  83. return
  84. }
  85. if len(ips) != test.ExpectedIPCount {
  86. t.Errorf("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets)
  87. }
  88. if len(macs) != test.ExpectedMACCount {
  89. t.Errorf("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets)
  90. }
  91. })
  92. }
  93. }
  94. func TestBuildEndpointFromInterface(t *testing.T) {
  95. ifaces, err := net.Interfaces()
  96. if err != nil {
  97. t.Error(err)
  98. }
  99. if len(ifaces) <= 0 {
  100. t.Error("Unable to find any network interfaces to run test with.")
  101. }
  102. _, err = buildEndpointFromInterface(ifaces[0])
  103. if err != nil {
  104. t.Error(err)
  105. }
  106. }
  107. func TestFindInterfaceByName(t *testing.T) {
  108. ifaces, err := net.Interfaces()
  109. if err != nil {
  110. t.Error(err)
  111. }
  112. if len(ifaces) <= 0 {
  113. t.Error("Unable to find any network interfaces to run test with.")
  114. }
  115. var exampleIface net.Interface
  116. // emulate libpcap's pcap_lookupdev function to find
  117. // default interface to test with ( maybe could use loopback ? )
  118. for _, iface := range ifaces {
  119. if iface.HardwareAddr != nil {
  120. exampleIface = iface
  121. break
  122. }
  123. }
  124. foundEndpoint, err := findInterfaceByName(exampleIface.Name, ifaces)
  125. if err != nil {
  126. t.Error("unable to find a given interface by name to build endpoint", err)
  127. }
  128. if foundEndpoint.Name() != exampleIface.Name {
  129. t.Error("unable to find a given interface by name to build endpoint")
  130. }
  131. }
  132. func TestFindInterface(t *testing.T) {
  133. ifaces, err := net.Interfaces()
  134. if err != nil {
  135. t.Error(err)
  136. }
  137. if len(ifaces) <= 0 {
  138. t.Error("Unable to find any network interfaces to run test with.")
  139. }
  140. var exampleIface net.Interface
  141. // emulate libpcap's pcap_lookupdev function to find
  142. // default interface to test with ( maybe could use loopback ? )
  143. for _, iface := range ifaces {
  144. if iface.HardwareAddr != nil {
  145. exampleIface = iface
  146. break
  147. }
  148. }
  149. foundEndpoint, err := FindInterface(exampleIface.Name)
  150. if err != nil {
  151. t.Error("unable to find a given interface by name to build endpoint", err)
  152. }
  153. if foundEndpoint.Name() != exampleIface.Name {
  154. t.Error("unable to find a given interface by name to build endpoint")
  155. }
  156. }