Repository navigation
Expand file tree
/
Copy pathengine_group_relay_test.go
More file actions
125 lines (119 loc) · 4.42 KB
/
Copy pathengine_group_relay_test.go
File metadata and controls
125 lines (119 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package meowcaller
import (
"bytes"
"errors"
"testing"
"github.com/purpshell/meowcaller/rtp"
"github.com/purpshell/meowcaller/stun"
"github.com/polymorfa/hypermeow/types"
)
func TestGroupRelayDataSelectsCaptureAddressAndCredentials(t *testing.T) {
update := groupCallUpdate{Relay: &groupCallRelay{
Key: bytes.Repeat([]byte{0x21}, 16),
Tokens: [][]byte{bytes.Repeat([]byte{0x42}, 174)},
Endpoints: []groupCallRelayEndpoint{{
RelayID: 7, RelayName: "zrh1c01", TokenID: 0,
IPv4: "157.240.17.62", Port: 3478,
}},
}}
rd, err := groupRelayData(update, false)
if err != nil {
t.Fatalf("groupRelayData: %v", err)
}
endpoint := getMediaRelayEndpoint(rd, false)
if endpoint == nil || endpoint.relayName != "zrh1c01" ||
endpoint.addresses[0].ipv4 != "157.240.17.62" ||
endpoint.addresses[0].port != 3478 {
t.Fatalf("selected endpoint = %+v", endpoint)
}
if !bytes.Equal(rd.relayKeyASCII, update.Relay.Key) ||
!bytes.Equal(rd.relayTokens[0], update.Relay.Tokens[0]) {
t.Fatal("group relay credentials were not copied")
}
rd.relayKeyASCII[0] ^= 0xff
if bytes.Equal(rd.relayKeyASCII, update.Relay.Key) {
t.Fatal("relay key aliases the signaling snapshot")
}
}
func TestGroupRelayAllocateStateCarriesParticipantSubscriptions(t *testing.T) {
initial := []byte{0x00, 0x01, 0x02}
key := bytes.Repeat([]byte{0x24}, 16)
token := bytes.Repeat([]byte{0x42}, 174)
hbhFEC := [2]uint32{0x11223344, 0x55667788}
state := newGroupRelayAllocateStateWithHBHFEC(initial, key, hbhFEC)
endpoint := &relayEndpoint{
relayName: "zrh1c01",
addresses: []relayAddress{{ipv4: "157.240.17.62", port: 3478}},
}
relayUpdate := &groupCallRelay{
TransactionID: 4, Key: key, Tokens: [][]byte{token},
Endpoints: []groupCallRelayEndpoint{{RelayName: "zrh1c01", TokenID: 0}},
}
streamSSRCs := [9]uint32{1, 2, 3, 4, 5, 6, 7, 8, 9}
appDataSSRC := uint32(10)
pids := []uint32{31, 47}
transactionID := [12]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
var sent []byte
changed, err := state.ApplyWithSubscriptions(
endpoint,
relayUpdate,
streamSSRCs,
appDataSSRC,
pids,
transactionID,
func(packet []byte) error {
sent = bytes.Clone(packet)
return nil
},
)
if err != nil || !changed {
t.Fatalf("ApplyWithSubscriptions = (%v, %v), want (true, nil)", changed, err)
}
endpointXOR, _ := stun.EncodeXorRelayEndpoint("157.240.17.62", 3478)
want := stun.BuildWasmStunAllocateRequestWithGroupSubscriptionsAndHBHFEC(
transactionID, token, endpointXOR, streamSSRCs, appDataSSRC, hbhFEC, pids, key,
)
if !bytes.Equal(sent, want) {
t.Fatalf("group allocate = %x, want %x", sent, want)
}
}
func TestGroupRelayAllocateSendFailureDoesNotCommitRotation(t *testing.T) {
initial := []byte{0x00, 0x01, 0x02}
initialKey := bytes.Repeat([]byte{0x12}, 16)
state := newGroupRelayAllocateStateWithHBHFEC(initial, initialKey, [2]uint32{})
endpoint := &relayEndpoint{
relayName: "zrh1c01",
addresses: []relayAddress{{ipv4: "157.240.17.62", port: 3478}},
}
relayUpdate := &groupCallRelay{
TransactionID: 1,
Key: bytes.Repeat([]byte{0x24}, 16),
Tokens: [][]byte{bytes.Repeat([]byte{0x42}, 174)},
Endpoints: []groupCallRelayEndpoint{{RelayName: "zrh1c01", TokenID: 0}},
}
sendErr := errors.New("relay unavailable")
changed, err := state.ApplyWithSubscriptions(
endpoint, relayUpdate, [9]uint32{}, 0, nil, [12]byte{},
func([]byte) error { return sendErr },
)
if !errors.Is(err, sendErr) || changed {
t.Fatalf("failed ApplyWithSubscriptions = (%v, %v)", changed, err)
}
if got := state.Current(); !bytes.Equal(got, initial) {
t.Fatalf("current allocate after failure = %x, want %x", got, initial)
}
}
func TestConnectedRemoteParticipantPIDsExcludesSelfAndInactive(t *testing.T) {
self := types.JID{User: "100", Server: types.HiddenUserServer, Device: 1}
remote := types.JID{User: "200", Server: types.HiddenUserServer, Device: 2}
inactive := types.JID{User: "300", Server: types.HiddenUserServer, Device: 3}
update := groupCallUpdate{Participants: []groupCallParticipant{
{State: "connected", Devices: []groupCallDevice{{JID: self, PID: 11, HasPID: true}}},
{State: "connected", Devices: []groupCallDevice{{JID: remote, PID: 22, HasPID: true}}},
{State: "ringing", Devices: []groupCallDevice{{JID: inactive, PID: 33, HasPID: true}}},
}}
got := connectedRemoteParticipantPIDs(update, rtp.FormatE2ESrtpParticipantID(self.String()))
if len(got) != 1 || got[0] != 22 {
t.Fatalf("connected remote PIDs = %v, want [22]", got)
}
}