Skip to content
File

Blob: tun/server/resolve_test.go

go146 lines
1package server
2 
3import (
4 "context"
5 "errors"
6 "testing"
7 "testing/synctest"
8 "time"
9 
10 "go.miragespace.co/specter/spec/protocol"
11 "go.miragespace.co/specter/spec/tun"
12 
13 "github.com/stretchr/testify/mock"
14 "github.com/stretchr/testify/require"
15)
16 
17func TestPublishDestinationsRetriesFailedKey(t *testing.T) {
18 as := require.New(t)
19 _, node, clientT, chordT, serv := getFixture(t, as)
20 defer serv.routeCache.Close()
21 defer serv.keylessCache.Close()
22 _, cht, tn := getIdentities()
23 chordT.On("Identity").Return(cht)
24 clientT.On("Identity").Return(tn)
25 
26 payload, err := (&protocol.TunnelDestination{Chord: cht, Tunnel: tn}).MarshalVT()
27 as.NoError(err)
28 chordKey := []byte(tun.DestinationByChordKey(cht))
29 tunnelKey := []byte(tun.DestinationByTunnelKey(tn))
30 node.On("Put", mock.Anything, chordKey, payload).Return(nil).Once()
31 node.On("Put", mock.Anything, tunnelKey, payload).
32 Return(errors.New("transient publication failure")).Once()
33 node.On("Put", mock.Anything, tunnelKey, payload).Return(nil).Once()
34 
35 as.NoError(serv.publishDestinations(t.Context()))
36 node.AssertExpectations(t)
37}
38 
39func TestPublishDestinationsExhaustsRetries(t *testing.T) {
40 as := require.New(t)
41 _, node, clientT, chordT, serv := getFixture(t, as)
42 defer serv.routeCache.Close()
43 defer serv.keylessCache.Close()
44 _, cht, tn := getIdentities()
45 chordT.On("Identity").Return(cht)
46 clientT.On("Identity").Return(tn)
47 
48 failure := errors.New("permanent publication failure")
49 node.On("Put", mock.Anything, []byte(tun.DestinationByChordKey(cht)), mock.Anything).
50 Return(failure).Times(3)
51 
52 as.Same(failure, serv.publishDestinations(t.Context()))
53 node.AssertExpectations(t)
54}
55 
56func TestMustRegisterCancellationStopsBackoff(t *testing.T) {
57 synctest.Test(t, func(t *testing.T) {
58 as := require.New(t)
59 _, node, clientT, chordT, serv := getFixture(t, as)
60 defer serv.routeCache.Close()
61 defer serv.keylessCache.Close()
62 _, cht, tn := getIdentities()
63 chordT.On("Identity").Return(cht)
64 clientT.On("Identity").Return(tn)
65 
66 ctx, cancel := context.WithCancel(t.Context())
67 defer cancel()
68 attempted := make(chan struct{})
69 var publishCtx context.Context
70 node.On("Put", mock.Anything, []byte(tun.DestinationByChordKey(cht)), mock.Anything).
71 Run(func(args mock.Arguments) {
72 publishCtx = args.Get(0).(context.Context)
73 close(attempted)
74 }).Return(errors.New("transient publication failure")).Once()
75 
76 finished := make(chan any, 1)
77 go func() {
78 defer func() { finished <- recover() }()
79 serv.MustRegister(ctx)
80 }()
81 <-attempted
82 synctest.Wait()
83 as.NoError(publishCtx.Err())
84 cancel()
85 
86 as.Equal("Error publishing destinations", <-finished)
87 as.ErrorIs(publishCtx.Err(), context.Canceled)
88 node.AssertExpectations(t)
89 })
90}
91 
92func TestIdentitiesRoutine(t *testing.T) {
93 as := require.New(t)
94 
95 _, node, clientT, chordT, serv := getFixture(t, as)
96 _, cht, tn := getIdentities()
97 
98 ctx := t.Context()
99 
100 chordT.On("Identity").Return(cht)
101 clientT.On("Identity").Return(tn)
102 
103 // On start up -> publish
104 node.On("Put", mock.Anything, mock.MatchedBy(func(k []byte) bool {
105 exp := [][]byte{
106 []byte(tun.DestinationByChordKey(cht)),
107 []byte(tun.DestinationByTunnelKey(tn)),
108 }
109 return assertBytes(k, exp...)
110 }), mock.MatchedBy(func(v []byte) bool {
111 pair := &protocol.TunnelDestination{}
112 err := pair.UnmarshalVT(v)
113 if err != nil {
114 return false
115 }
116 if pair.GetChord().GetId() != cht.GetId() {
117 return false
118 }
119 if pair.GetTunnel().GetId() != tn.GetId() {
120 return false
121 }
122 return true
123 })).Return(nil)
124 
125 // On stop -> unpublish
126 node.On("Delete", mock.Anything, mock.MatchedBy(func(k []byte) bool {
127 exp := [][]byte{
128 []byte(tun.DestinationByChordKey(cht)),
129 []byte(tun.DestinationByTunnelKey(tn)),
130 }
131 return assertBytes(k, exp...)
132 })).Return(nil)
133 
134 serv.MustRegister(ctx)
135 
136 <-time.After(time.Millisecond * 100)
137 
138 serv.Stop()
139 
140 <-time.After(time.Millisecond * 100)
141 
142 node.AssertExpectations(t)
143 clientT.AssertExpectations(t)
144 chordT.AssertExpectations(t)
145}