package discovery

import (
	"git.loafle.net/overflow/overflow_probe/discovery/core/scan/service"
	"git.loafle.net/overflow/overflow_probe/discovery/core/scan/zone"
	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types"
	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types/timestamp"
	"github.com/stretchr/testify/assert"
	"sync"
	"testing"
)

func TestServiceScan(t *testing.T) {

	_, p := getDummyPort()

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		service.Scan(p)
		wg.Done()
	}()
	wg.Wait()

	if len(p.Services) <= 0 {
		t.Error("err")
		return
	}

	for _, v := range p.Services {
		t.Log("service: ", v.ServiceName)
	}

	assert.Equal(t, "SMB", p.Services[0].ServiceName)

}

func TestServiceCheck(t *testing.T) {

	_, p := getDummyPort()

	var services []*types.DiscoveryService = make([]*types.DiscoveryService, 0)
	services = append(services, types.NewService(p.PortType, "SMB", p))

	err := service.Check(p, services)
	if err == nil {
		t.Logf("%s is okay.", services[0].ServiceName)
	}

	assert.Nil(t, err)
}

func getDummyPort() (*types.DiscoveryHost, *types.DiscoveryPort) {
	zone := zone.NewZone()

	h := &types.DiscoveryHost{
		Ip_:            "192.168.1.104",
		Ports_:         make(map[string]*types.DiscoveryPort, 139),
		Ip:             111,
		Mac:            222,
		CreateDate:     timestamp.Now(),
		UpdateDate:     timestamp.Now(),
		Zone:           zone,
		FirstScanRange: 1,
		LastScanRange:  100,
	}

	p := &types.DiscoveryPort{
		CreateDate: timestamp.Now(),
		UpdateDate: timestamp.Now(),
		Host:       h,
		Number:     139,
		Port_:      "139",
		PortType:   types.TYPE_TCP,
	}

	return h, p
}
