scanner_test.go 2.5 KB

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