probe/_discoverer/discoverer_test.go

169 lines
4.2 KiB
Go
Raw Normal View History

2018-08-13 07:19:59 +00:00
package discoverer_test
import (
"encoding/json"
"log"
"strconv"
"testing"
2018-08-23 09:21:48 +00:00
olog "git.loafle.net/overflow/log-go"
2018-08-13 07:19:59 +00:00
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/discoverer"
)
var (
dz = &omd.DiscoverZone{
ExcludePatterns: []string{},
}
z = &omd.Zone{
2018-08-28 15:32:03 +00:00
Network: "192.168.35.0/24",
2018-08-13 07:19:59 +00:00
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
2018-08-28 15:32:03 +00:00
// Address: "192.168.1.201",
// Iface: "\\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}",
// Mac: "30:9C:23:15:A3:09",
// Address: "192.168.1.101",
// Iface: "enp3s0",
// Mac: "44:8a:5b:f1:f1:f3",
Address: "192.168.35.234",
Iface: "wlp5s0",
Mac: "d0:7e:35:da:26:68",
2018-08-13 07:19:59 +00:00
}
dh = &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
2018-08-28 15:32:03 +00:00
FirstScanRange: "192.168.35.1",
LastScanRange: "192.168.35.254",
2018-08-13 07:19:59 +00:00
}
h = &omd.Host{
Zone: z,
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
2018-08-28 15:32:03 +00:00
Address: "192.168.35.80",
Mac: "6c:ad:f8:d3:f8:c6",
2018-08-13 07:19:59 +00:00
}
dp = &omd.DiscoverPort{
FirstScanRange: 1,
2018-08-15 06:18:40 +00:00
LastScanRange: 65535,
2018-08-13 07:19:59 +00:00
ExcludePorts: []int{
631,
},
IncludeTCP: true,
IncludeUDP: true,
}
p = &omd.Port{
Host: h,
MetaPortType: omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
PortNumber: json.Number(strconv.Itoa(21)),
2018-08-13 07:19:59 +00:00
DiscoveredDate: omu.NowPtr(),
}
dha = &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.0",
LastScanRange: "192.168.1.255",
DiscoverPort: &omd.DiscoverPort{
FirstScanRange: 1,
2018-08-15 06:18:40 +00:00
LastScanRange: 20000,
2018-08-13 07:19:59 +00:00
ExcludePorts: []int{
631,
},
IncludeTCP: true,
IncludeUDP: false,
2018-08-15 07:46:52 +00:00
DiscoverService: nil,
2018-08-13 07:19:59 +00:00
},
}
)
func init() {
// runtime.LockOSThread()
2018-08-23 09:21:48 +00:00
olog.InitializeLogger("")
2018-08-13 07:19:59 +00:00
}
func TestDiscoverZone(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverZone(dataChan, dz)
})
}
func TestDiscoverHost(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverHost(dataChan, z, dh)
})
}
func TestDiscoverPort(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverPort(dataChan, h, dp)
})
}
func TestDiscoverService(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
2018-08-15 07:46:52 +00:00
_discoverer.DiscoverSerice(dataChan, p, nil)
2018-08-13 07:19:59 +00:00
})
}
func TestDiscoverHostAll(t *testing.T) {
_discoverer := discoverer.GetDiscoverer()
handleDiscovery(_discoverer, func(dataChan chan *discoverer.DiscoveryData) {
_discoverer.DiscoverHost(dataChan, z, dha)
})
}
func handleDiscovery(_discoverer discoverer.Discoverer, discoveryFunc func(dataChan chan *discoverer.DiscoveryData)) error {
var dataChan chan *discoverer.DiscoveryData
retainChan := make(chan struct{})
go func() {
dataChan = _discoverer.Retain()
close(retainChan)
}()
select {
case <-retainChan:
}
defer func() {
_discoverer.Release(dataChan)
}()
go discoveryFunc(dataChan)
for {
select {
case data, ok := <-dataChan:
if !ok {
return nil
}
switch data.Type {
case discoverer.DiscoveryDataTypeStart:
log.Printf("DiscoveryService.DiscoveryStart")
case discoverer.DiscoveryDataTypeStop:
log.Printf("DiscoveryService.DiscoveryStop")
data.Release()
return nil
case discoverer.DiscoveryDataTypeError:
log.Printf("DiscoveryService.DiscoveryDataTypeError %v", data.Error)
case discoverer.DiscoveryDataTypeZone:
log.Printf("DiscoveryService.DiscoveryDataTypeZone %v", data.Result)
case discoverer.DiscoveryDataTypeHost:
log.Printf("DiscoveryService.DiscoveryDataTypeHost %v", data.Result)
case discoverer.DiscoveryDataTypePort:
log.Printf("DiscoveryService.DiscoveryDataTypePort %v", data.Result)
case discoverer.DiscoveryDataTypeService:
// log.Printf("DiscoveryService.DiscoveryDataTypeService %v", data.Result)
log.Println(data.Result)
}
data.Release()
}
}
}