scanner_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "fmt"
  23. "log"
  24. "net"
  25. "sync"
  26. "testing"
  27. )
  28. func Equal(a, b []int) bool {
  29. if len(a) != len(b) {
  30. return false
  31. }
  32. for i, v := range a {
  33. if v != b[i] {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. func startTestSever(port int, t *testing.T) {
  40. listen, err := net.Listen("tcp", fmt.Sprintf("localhost:%v", port))
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. defer listen.Close()
  45. for {
  46. conn, err := listen.Accept()
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. t.Logf("Recieved dial from PortWalk of port: %v from host: %s\n", port, conn.RemoteAddr().String())
  51. conn.Close()
  52. }
  53. }
  54. func TestPortWalk(t *testing.T) {
  55. type TestCase struct {
  56. Name string
  57. ScanPort []int
  58. ListenPort []int
  59. ShouldFail bool
  60. }
  61. tc := []TestCase{
  62. TestCase{
  63. Name: "Passing test, listening on 8080/27017 and scanned on 8080/27017.",
  64. ScanPort: []int{8080, 27017},
  65. ListenPort: []int{8080, 27017},
  66. ShouldFail: false,
  67. },
  68. TestCase{
  69. Name: "Failing test, listening on 8081 scanned on 6439",
  70. ScanPort: []int{6439},
  71. ListenPort: []int{8081},
  72. ShouldFail: true,
  73. },
  74. }
  75. wg := &sync.WaitGroup{}
  76. for i := range tc {
  77. for x := range tc[i].ListenPort {
  78. wg.Add(1)
  79. go startTestSever(tc[i].ListenPort[x], t)
  80. }
  81. got := PortWalk("localhost", tc[i].ScanPort)
  82. for k, _ := range tc[i].ListenPort {
  83. wg.Done()
  84. if !Equal(got, tc[i].ListenPort) {
  85. if !tc[i].ShouldFail {
  86. t.Errorf("Test '%s' failed! PortWalk didnt detect the test server was listening on: %+v\n", tc[i].Name, tc[i].ListenPort)
  87. }
  88. }
  89. t.Logf("Test '%s' passed! Scanned port: '%v'", tc[i].Name, k)
  90. }
  91. }
  92. }