Files
Reticulum-Go/pkg/destination/destination_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

153 lines
3.5 KiB
Go

package destination
import (
"bytes"
"path/filepath"
"testing"
"git.quad4.io/Networks/Reticulum-Go/pkg/common"
"git.quad4.io/Networks/Reticulum-Go/pkg/identity"
)
type mockTransport struct {
config *common.ReticulumConfig
interfaces map[string]common.NetworkInterface
}
func (m *mockTransport) GetConfig() *common.ReticulumConfig {
return m.config
}
func (m *mockTransport) GetInterfaces() map[string]common.NetworkInterface {
return m.interfaces
}
func (m *mockTransport) RegisterDestination(hash []byte, dest interface{}) {
}
type mockInterface struct {
common.BaseInterface
}
func (m *mockInterface) Send(data []byte, address string) error {
return nil
}
func TestNewDestination(t *testing.T) {
id, _ := identity.New()
transport := &mockTransport{config: &common.ReticulumConfig{}}
dest, err := New(id, IN|OUT, SINGLE, "testapp", transport, "testaspect")
if err != nil {
t.Fatalf("New failed: %v", err)
}
if dest == nil {
t.Fatal("New returned nil")
}
if dest.ExpandName() != "testapp.testaspect" {
t.Errorf("Expected name testapp.testaspect, got %s", dest.ExpandName())
}
hash := dest.GetHash()
if len(hash) != 16 {
t.Errorf("Expected hash length 16, got %d", len(hash))
}
}
func TestFromHash(t *testing.T) {
id, _ := identity.New()
transport := &mockTransport{}
hash := make([]byte, 16)
dest, err := FromHash(hash, id, SINGLE, transport)
if err != nil {
t.Fatalf("FromHash failed: %v", err)
}
if !bytes.Equal(dest.GetHash(), hash) {
t.Error("Hashes don't match")
}
}
func TestRequestHandlers(t *testing.T) {
id, _ := identity.New()
dest, _ := New(id, IN, SINGLE, "test", &mockTransport{})
path := "test/path"
response := []byte("hello")
err := dest.RegisterRequestHandler(path, func(p string, d []byte, rid []byte, lid []byte, ri *identity.Identity, ra int64) []byte {
return response
}, ALLOW_ALL, nil)
if err != nil {
t.Fatalf("RegisterRequestHandler failed: %v", err)
}
result := dest.HandleRequest(path, nil, nil, nil, nil, 0)
if !bytes.Equal(result, response) {
t.Errorf("Expected response %q, got %q", response, result)
}
if !dest.DeregisterRequestHandler(path) {
t.Error("DeregisterRequestHandler failed")
}
}
func TestEncryptDecrypt(t *testing.T) {
id, _ := identity.New()
dest, _ := New(id, IN|OUT, SINGLE, "test", &mockTransport{})
plaintext := []byte("hello world")
ciphertext, err := dest.Encrypt(plaintext)
if err != nil {
t.Fatalf("Encrypt failed: %v", err)
}
decrypted, err := dest.Decrypt(ciphertext)
if err != nil {
t.Fatalf("Decrypt failed: %v", err)
}
if !bytes.Equal(plaintext, decrypted) {
t.Errorf("Decrypted data doesn't match: %q vs %q", decrypted, plaintext)
}
}
func TestRatchets(t *testing.T) {
tmpDir := t.TempDir()
ratchetPath := filepath.Join(tmpDir, "ratchets")
id, _ := identity.New()
dest, _ := New(id, IN|OUT, SINGLE, "test", &mockTransport{})
if !dest.EnableRatchets(ratchetPath) {
t.Fatal("EnableRatchets failed")
}
err := dest.RotateRatchets()
if err != nil {
t.Fatalf("RotateRatchets failed: %v", err)
}
ratchets := dest.GetRatchets()
if len(ratchets) != 1 {
t.Errorf("Expected 1 ratchet, got %d", len(ratchets))
}
}
func TestPlainDestination(t *testing.T) {
id, _ := identity.New()
dest, _ := New(id, IN|OUT, PLAIN, "test", &mockTransport{})
plaintext := []byte("plain text")
ciphertext, _ := dest.Encrypt(plaintext)
if !bytes.Equal(plaintext, ciphertext) {
t.Error("Plain destination should not encrypt")
}
decrypted, _ := dest.Decrypt(ciphertext)
if !bytes.Equal(plaintext, decrypted) {
t.Error("Plain destination should not decrypt")
}
}