moving test that somehow ended up in wrong package

This commit is contained in:
херетик
2023-05-29 16:40:44 +01:00
parent 1454dafcc2
commit 69c1d48c3b
2 changed files with 95 additions and 101 deletions

View File

@@ -2,15 +2,7 @@ package dispatcher
import (
"context"
"git-indra.lan/indra-labs/indra/pkg/engine"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
"git-indra.lan/indra-labs/indra/pkg/engine/sessions"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/qu"
"go.uber.org/atomic"
"os"
"reflect"
"sync"
"testing"
"time"
@@ -254,96 +246,3 @@ func TestDispatcher_Rekey(t *testing.T) {
}
}
func TestEngine_SendHiddenService(t *testing.T) {
log2.SetLogLevel(log2.Debug)
log2.App.Store("")
var clients []*engine.Engine
var e error
const nCircuits = 10
ctx, cancel := context.WithCancel(context.Background())
if clients, e = engine.CreateNMockCircuits(nCircuits, nCircuits,
ctx); fails(e) {
t.Error(e)
t.FailNow()
}
// Start up the clients.
for _, v := range clients {
go v.Start()
}
var wg sync.WaitGroup
var counter atomic.Int32
quit := qu.T()
go func() {
select {
case <-time.After(time.Second * 6):
quit.Q()
t.Error("MakeHiddenService test failed")
case <-quit:
for _, v := range clients {
v.Shutdown()
}
for i := 0; i < int(counter.Load()); i++ {
wg.Done()
}
return
}
}()
for i := 0; i < nCircuits*nCircuits/2; i++ {
wg.Add(1)
counter.Inc()
e = clients[0].BuyNewSessions(1000000, func() {
wg.Done()
counter.Dec()
})
if fails(e) {
wg.Done()
counter.Dec()
}
wg.Wait()
}
log2.SetLogLevel(log2.Debug)
var idPrv *crypto.Prv
if idPrv, e = crypto.GeneratePrvKey(); fails(e) {
return
}
id := nonce.NewID()
introducerHops := clients[0].Manager.GetSessionsAtHop(2)
returnHops := clients[0].Manager.GetSessionsAtHop(5)
var introducer *sessions.Data
if len(introducerHops) > 1 {
cryptorand.Shuffle(len(introducerHops), func(i, j int) {
introducerHops[i], introducerHops[j] = introducerHops[j],
introducerHops[i]
})
}
var returner *sessions.Data
if len(introducerHops) > 1 {
cryptorand.Shuffle(len(returnHops), func(i, j int) {
returnHops[i], returnHops[j] = returnHops[j],
returnHops[i]
})
}
// There must be at least one, and if there was more than one the first
// index of introducerHops will be a randomly selected one.
introducer = introducerHops[0]
returner = returnHops[0]
wg.Add(1)
counter.Inc()
svc := &services.Service{
Port: 2345,
RelayRate: 43523,
Transport: transport.NewByteChan(64),
}
clients[0].SendHiddenService(id, idPrv, 0, 0,
time.Now().Add(time.Hour), returner, introducer, svc,
func(id nonce.ID, ifc interface{}, b slice.Bytes) (e error) {
log.W.S("received intro", reflect.TypeOf(ifc), b.ToBytes())
// This happens when the gossip gets back to us.
wg.Done()
counter.Dec()
return
})
wg.Wait()
quit.Q()
cancel()
}

View File

@@ -2,6 +2,7 @@ package engine
import (
"context"
"reflect"
"runtime"
"sync"
"testing"
@@ -593,3 +594,97 @@ func TestEngine_Route(t *testing.T) {
cancel()
log.W.Ln("fin")
}
func TestEngine_SendHiddenService(t *testing.T) {
log2.SetLogLevel(log2.Debug)
log2.App.Store("")
var clients []*Engine
var e error
const nCircuits = 10
ctx, cancel := context.WithCancel(context.Background())
if clients, e = CreateNMockCircuits(nCircuits, nCircuits,
ctx); fails(e) {
t.Error(e)
t.FailNow()
}
// Start up the clients.
for _, v := range clients {
go v.Start()
}
var wg sync.WaitGroup
var counter atomic.Int32
quit := qu.T()
go func() {
select {
case <-time.After(time.Second * 6):
quit.Q()
t.Error("MakeHiddenService test failed")
case <-quit:
for _, v := range clients {
v.Shutdown()
}
for i := 0; i < int(counter.Load()); i++ {
wg.Done()
}
return
}
}()
for i := 0; i < nCircuits*nCircuits/2; i++ {
wg.Add(1)
counter.Inc()
e = clients[0].BuyNewSessions(1000000, func() {
wg.Done()
counter.Dec()
})
if fails(e) {
wg.Done()
counter.Dec()
}
wg.Wait()
}
log2.SetLogLevel(log2.Debug)
var idPrv *crypto.Prv
if idPrv, e = crypto.GeneratePrvKey(); fails(e) {
return
}
id := nonce.NewID()
introducerHops := clients[0].Manager.GetSessionsAtHop(2)
returnHops := clients[0].Manager.GetSessionsAtHop(5)
var introducer *sessions.Data
if len(introducerHops) > 1 {
cryptorand.Shuffle(len(introducerHops), func(i, j int) {
introducerHops[i], introducerHops[j] = introducerHops[j],
introducerHops[i]
})
}
var returner *sessions.Data
if len(introducerHops) > 1 {
cryptorand.Shuffle(len(returnHops), func(i, j int) {
returnHops[i], returnHops[j] = returnHops[j],
returnHops[i]
})
}
// There must be at least one, and if there was more than one the first
// index of introducerHops will be a randomly selected one.
introducer = introducerHops[0]
returner = returnHops[0]
wg.Add(1)
counter.Inc()
svc := &services.Service{
Port: 2345,
RelayRate: 43523,
Transport: transport.NewByteChan(64),
}
clients[0].SendHiddenService(id, idPrv, 0, 0,
time.Now().Add(time.Hour), returner, introducer, svc,
func(id nonce.ID, ifc interface{}, b slice.Bytes) (e error) {
log.W.S("received intro", reflect.TypeOf(ifc), b.ToBytes())
// This happens when the gossip gets back to us.
wg.Done()
counter.Dec()
return
})
wg.Wait()
quit.Q()
cancel()
}