Files
Reticulum-Go/pkg/transport/transport_test.go
Sudo-Ivan 53e98c73af
Some checks failed
Go Build Multi-Platform / build (amd64, freebsd) (push) Successful in 51s
Go Build Multi-Platform / build (amd64, windows) (push) Successful in 49s
Go Build Multi-Platform / build (arm, linux) (push) Successful in 58s
Go Build Multi-Platform / build (arm64, darwin) (push) Successful in 57s
Go Build Multi-Platform / build (arm64, windows) (push) Failing after 49s
Go Test Multi-Platform / Test (ubuntu-latest, arm64) (push) Successful in 53s
Run Gosec / tests (push) Successful in 1m25s
Go Revive Lint / lint (push) Successful in 48s
Go Test Multi-Platform / Test (ubuntu-latest, amd64) (push) Successful in 1m39s
Go Build Multi-Platform / build (arm64, linux) (push) Failing after 4m59s
Go Build Multi-Platform / build (amd64, linux) (push) Successful in 9m39s
Go Build Multi-Platform / build (arm64, freebsd) (push) Successful in 9m33s
Go Build Multi-Platform / build (arm, windows) (push) Successful in 9m35s
Go Build Multi-Platform / build (arm, freebsd) (push) Successful in 9m37s
Go Build Multi-Platform / build (amd64, darwin) (push) Successful in 9m42s
Go Build Multi-Platform / Create Release (push) Has been skipped
Add unit tests for Reticulum-Go packages including reticulum, storage, announce, channel, destination, identity, resource, and transport, ensuring comprehensive coverage of functionality.
2025-12-29 00:15:08 -06:00

121 lines
2.5 KiB
Go

package transport
import (
"bytes"
"testing"
"git.quad4.io/Networks/Reticulum-Go/pkg/common"
)
type mockInterface struct {
common.BaseInterface
sent [][]byte
}
func (m *mockInterface) Send(data []byte, address string) error {
m.sent = append(m.sent, data)
return nil
}
func (m *mockInterface) GetName() string {
return m.Name
}
func (m *mockInterface) IsEnabled() bool {
return m.Enabled
}
func TestNewTransport(t *testing.T) {
config := &common.ReticulumConfig{}
tr := NewTransport(config)
if tr == nil {
t.Fatal("NewTransport returned nil")
}
defer tr.Close()
}
func TestRegisterInterface(t *testing.T) {
tr := NewTransport(&common.ReticulumConfig{})
defer tr.Close()
iface := &mockInterface{}
iface.Name = "test"
err := tr.RegisterInterface("test", iface)
if err != nil {
t.Fatalf("RegisterInterface failed: %v", err)
}
retrieved, err := tr.GetInterface("test")
if err != nil {
t.Fatalf("GetInterface failed: %v", err)
}
if retrieved != iface {
t.Error("Retrieved interface doesn't match")
}
}
func TestPathManagement(t *testing.T) {
tr := NewTransport(&common.ReticulumConfig{})
defer tr.Close()
destHash := []byte("test-destination-hash")
nextHop := []byte("next-hop")
iface := &mockInterface{}
iface.Name = "iface1"
_ = tr.RegisterInterface("iface1", iface)
tr.UpdatePath(destHash, nextHop, "iface1", 2)
if !tr.HasPath(destHash) {
t.Error("Path not found after update")
}
if tr.HopsTo(destHash) != 2 {
t.Errorf("Expected 2 hops, got %d", tr.HopsTo(destHash))
}
if !bytes.Equal(tr.NextHop(destHash), nextHop) {
t.Error("Next hop mismatch")
}
if tr.NextHopInterface(destHash) != "iface1" {
t.Errorf("Expected iface1, got %s", tr.NextHopInterface(destHash))
}
}
func TestDestinationRegistration(t *testing.T) {
tr := NewTransport(&common.ReticulumConfig{})
defer tr.Close()
destHash := []byte("dest")
tr.RegisterDestination(destHash, "test-dest")
tr.mutex.RLock()
dest, ok := tr.destinations[string(destHash)]
tr.mutex.RUnlock()
if !ok || dest != "test-dest" {
t.Error("Destination not registered correctly")
}
}
func TestTransportStatus(t *testing.T) {
tr := NewTransport(&common.ReticulumConfig{})
defer tr.Close()
destHash := []byte("dest")
if tr.PathIsUnresponsive(destHash) {
t.Error("Path should not be unresponsive initially")
}
tr.MarkPathUnresponsive(destHash)
if !tr.PathIsUnresponsive(destHash) {
t.Error("Path should be unresponsive")
}
tr.MarkPathResponsive(destHash)
if tr.PathIsUnresponsive(destHash) {
t.Error("Path should be responsive again")
}
}