From 1454dafcc2b2b19381e0e7f40d74b314a4e689a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D1=85=D0=B5=D1=80=D0=B5=D1=82=D0=B8=D0=BA?= <херетик@indra.org> Date: Mon, 29 May 2023 16:33:01 +0100 Subject: [PATCH] removing unused and irrelevant things --- pkg/engine/dispatcher/dispatcher_test.go | 102 ++++++++++++++++++++ pkg/engine/dispatcher_test.go | 92 ------------------ pkg/engine/eng_common.go | 21 ----- pkg/engine/hiddenservice_test.go | 115 ----------------------- 4 files changed, 102 insertions(+), 228 deletions(-) delete mode 100644 pkg/engine/dispatcher_test.go delete mode 100644 pkg/engine/eng_common.go delete mode 100644 pkg/engine/hiddenservice_test.go diff --git a/pkg/engine/dispatcher/dispatcher_test.go b/pkg/engine/dispatcher/dispatcher_test.go index d0341169..e269cd8d 100644 --- a/pkg/engine/dispatcher/dispatcher_test.go +++ b/pkg/engine/dispatcher/dispatcher_test.go @@ -2,7 +2,15 @@ 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" @@ -245,3 +253,97 @@ func TestDispatcher_Rekey(t *testing.T) { t.Fatal("did not receive all messages correctly", succ, countTo*3) } } + +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() +} diff --git a/pkg/engine/dispatcher_test.go b/pkg/engine/dispatcher_test.go deleted file mode 100644 index 6a6ab1c3..00000000 --- a/pkg/engine/dispatcher_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package engine - -import ( - "context" - "net/netip" - "os" - "testing" - "time" - - "github.com/multiformats/go-multiaddr" - - "git-indra.lan/indra-labs/indra/pkg/crypto" - "git-indra.lan/indra-labs/indra/pkg/engine/node" - "git-indra.lan/indra-labs/indra/pkg/engine/transport" - log2 "git-indra.lan/indra-labs/indra/pkg/proc/log" -) - -func TestEngine_Dispatcher(t *testing.T) { - log2.SetLogLevel(log2.Trace) - var e error - const nTotal = 26 - ctx, cancel := context.WithCancel(context.Background()) - var listeners []*transport.Listener - var keys []*crypto.Keys - var nodes []*node.Node - var engines []*Engine - var seed string - for i := 0; i < nTotal; i++ { - var k *crypto.Keys - if k, e = crypto.GenerateKeys(); fails(e) { - t.FailNow() - } - keys = append(keys, k) - var l *transport.Listener - dataPath, err := os.MkdirTemp(os.TempDir(), "badger") - if err != nil { - t.FailNow() - } - if l, e = transport.NewListener(seed, transport.LocalhostZeroIPv4QUIC, - dataPath, k, ctx, transport.DefaultMTU); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - sa := transport.GetHostAddress(l.Host) - if i == 0 { - seed = sa - } - listeners = append(listeners, l) - var addr netip.AddrPort - var ma multiaddr.Multiaddr - if ma, e = multiaddr.NewMultiaddr(sa); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - - var ip, port string - if ip, e = ma.ValueForProtocol(multiaddr.P_IP4); fails(e) { - // we specified ipv4 previously. - os.RemoveAll(dataPath) - t.FailNow() - } - //if port, e = ma.ValueForProtocol(multiaddr.P_TCP); fails(e) { - if port, e = ma.ValueForProtocol(multiaddr.P_UDP); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - //} - if addr, e = netip.ParseAddrPort(ip + ":" + port); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - var nod *node.Node - if nod, _ = node.NewNode(&addr, k, nil, 50000); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - nodes = append(nodes, nod) - var eng *Engine - if eng, e = NewEngine(Params{ - Listener: l, - Keys: k, - Node: nod, - }); fails(e) { - os.RemoveAll(dataPath) - t.FailNow() - } - engines = append(engines, eng) - defer os.RemoveAll(dataPath) - } - time.Sleep(time.Second * 2) - cancel() -} diff --git a/pkg/engine/eng_common.go b/pkg/engine/eng_common.go deleted file mode 100644 index 3b2750d5..00000000 --- a/pkg/engine/eng_common.go +++ /dev/null @@ -1,21 +0,0 @@ -package engine - -import ( - "net" - "net/netip" -) - -func GetNetworkFromAddrPort(addr string) (nw string, u *net.UDPAddr, - e error) { - - nw = "udp" - var ap netip.AddrPort - if ap, e = netip.ParseAddrPort(addr); fails(e) { - return - } - u = &net.UDPAddr{IP: net.ParseIP(ap.Addr().String()), Port: int(ap.Port())} - if u.IP.To4() != nil { - nw = "udp4" - } - return -} diff --git a/pkg/engine/hiddenservice_test.go b/pkg/engine/hiddenservice_test.go deleted file mode 100644 index 829e9a90..00000000 --- a/pkg/engine/hiddenservice_test.go +++ /dev/null @@ -1,115 +0,0 @@ -package engine - -import ( - "context" - "reflect" - "sync" - "testing" - "time" - - "go.uber.org/atomic" - - "git-indra.lan/indra-labs/indra/pkg/crypto" - "git-indra.lan/indra-labs/indra/pkg/crypto/nonce" - "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/engine/transport" - log2 "git-indra.lan/indra-labs/indra/pkg/proc/log" - "git-indra.lan/indra-labs/indra/pkg/util/cryptorand" - "git-indra.lan/indra-labs/indra/pkg/util/qu" - "git-indra.lan/indra-labs/indra/pkg/util/slice" -) - -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() -}