apiary/honeypot/ports/server_test.go
2021-10-28 16:09:28 +02:00

46 lines
920 B
Go

package ports_test
import (
"net"
"testing"
"time"
"context"
"github.uio.no/torjus/apiary/honeypot/ports"
)
func TestServer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store := &ports.MemoryStore{}
server := ports.New(store)
server.IP = "127.0.0.1"
server.AddTCPPort("25")
go server.Start(ctx)
rAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(server.IP, "25"))
if err != nil {
t.Fatalf("Error resolving remote address: %s", err)
}
conn, err := net.DialTCP("tcp", nil, rAddr)
if err != nil {
t.Fatalf("Dialing server returned error: %s", err)
}
conn.Write([]byte("LOL"))
conn.Close()
time.Sleep(1 * time.Second)
cancel()
attempts, err := store.List()
if err != nil {
t.Fatalf("Getting attempts from store returned error: %s", err)
}
if len(attempts) != 1 {
t.Fatalf("Wrong amount of attempts in store: %d", len(attempts))
}
}