Repository navigation
Expand file tree
/
Copy pathreaction_test.go
More file actions
174 lines (159 loc) ยท 6.31 KB
/
Copy pathreaction_test.go
File metadata and controls
174 lines (159 loc) ยท 6.31 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package meowcaller
import (
"bytes"
"errors"
"testing"
"github.com/purpshell/meowcaller/rtp"
"github.com/polymorfa/hypermeow/types"
)
func TestAppDataReactionMatchesCapturedWirePayload(t *testing.T) {
want := []byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0xf0, 0x9f, 0x91, 0x8d}
if got := encodeAppDataReaction(1, "๐"); !bytes.Equal(got, want) {
t.Fatalf("encoded reaction = %x, want %x", got, want)
}
}
func TestAppDataReactionDecodesCapturedWirePayloads(t *testing.T) {
tests := []struct {
payload []byte
id uint64
emoji string
}{
{[]byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x01, 0x12, 0x04, 0xf0, 0x9f, 0x91, 0x8d}, 1, "๐"},
{[]byte{0x0a, 0x0c, 0x0a, 0x0a, 0x08, 0x02, 0x12, 0x06, 0xe2, 0x9d, 0xa4, 0xef, 0xb8, 0x8f}, 2, "โค๏ธ"},
{[]byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x03, 0x12, 0x04, 0xf0, 0x9f, 0x98, 0x82}, 3, "๐"},
{[]byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x04, 0x12, 0x04, 0xf0, 0x9f, 0x98, 0xae}, 4, "๐ฎ"},
{[]byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x05, 0x12, 0x04, 0xf0, 0x9f, 0x98, 0xa2}, 5, "๐ข"},
{[]byte{0x0a, 0x0a, 0x0a, 0x08, 0x08, 0x06, 0x12, 0x04, 0xf0, 0x9f, 0x99, 0x8f}, 6, "๐"},
}
for _, tt := range tests {
reactions, err := decodeAppDataReactions(tt.payload)
if err != nil {
t.Fatalf("decode %x: %v", tt.payload, err)
}
if len(reactions) != 1 || reactions[0].transactionID != tt.id || reactions[0].emoji != tt.emoji {
t.Fatalf("decode %x = %+v, want id=%d emoji=%q", tt.payload, reactions, tt.id, tt.emoji)
}
}
}
func TestAppDataReceiverDeduplicatesRetransmissions(t *testing.T) {
var receiver appDataReceiver
payload := encodeAppDataReaction(9, "๐")
first, ok, err := receiver.receive(payload)
if err != nil || !ok || first.emoji != "๐" {
t.Fatalf("first receive = (%+v, %v, %v)", first, ok, err)
}
if _, ok, err = receiver.receive(payload); err != nil || ok {
t.Fatalf("duplicate receive = (ok=%v, err=%v), want ignored", ok, err)
}
if _, ok, err = receiver.receive(encodeAppDataReaction(8, "๐")); err != nil || ok {
t.Fatalf("older receive = (ok=%v, err=%v), want ignored", ok, err)
}
}
func TestIncomingAppDataDispatchesOneTransientCallReaction(t *testing.T) {
_, call := testEngineWithOutgoingCall()
var receiver appDataReceiver
var got []CallReaction
call.OnReaction(func(reaction CallReaction) { got = append(got, reaction) })
payload := encodeAppDataReaction(1, "๐")
if handled, err := handleAppDataReaction(call, &receiver, payload); err != nil || !handled {
t.Fatalf("first app-data reaction = (handled=%v, err=%v)", handled, err)
}
if handled, err := handleAppDataReaction(call, &receiver, payload); err != nil || handled {
t.Fatalf("duplicate app-data reaction = (handled=%v, err=%v), want ignored", handled, err)
}
if len(got) != 1 || got[0].Emoji != "๐" || got[0].Sender != call.Peer() || got[0].Removed {
t.Fatalf("dispatched reactions = %+v", got)
}
}
func TestIncomingGroupAppDataAttributesSelectedParticipant(t *testing.T) {
_, call := testEngineWithOutgoingCall()
var receiver appDataReceiver
var got CallReaction
call.OnReaction(func(reaction CallReaction) { got = reaction })
sender := types.JID{User: "333333333333333", Device: 43, Server: types.HiddenUserServer}
if handled, err := handleAppDataReactionFrom(
call,
&receiver,
sender.ToNonAD(),
encodeAppDataReaction(1, "๐"),
); err != nil || !handled {
t.Fatalf("group app-data reaction = (handled=%v, err=%v)", handled, err)
}
if got.Sender != sender.ToNonAD() || got.Emoji != "๐" {
t.Fatalf("group reaction = %+v, want sender %s", got, sender.ToNonAD())
}
}
func TestAppDataSenderUsesCapturedRTPShape(t *testing.T) {
callKey := iota32()
const ssrc = 0x7f4d310b
pipe, err := NewMediaPipeline(callKey, "111111111111111:0@lid", "222222222222222:0@lid", ssrc, FrameSamples)
if err != nil {
t.Fatalf("sender pipe: %v", err)
}
var packets [][]byte
sender := newAppDataSender(pipe, ssrc, func(packet []byte) (int, error) {
packets = append(packets, bytes.Clone(packet))
return len(packet), nil
})
sender.retransmitInterval = 0
if err = sender.sendReaction("๐"); err != nil {
t.Fatalf("send reaction: %v", err)
}
if len(packets) != appDataRetransmitCount {
t.Fatalf("packet count = %d, want %d", len(packets), appDataRetransmitCount)
}
for i, packet := range packets {
header, ok := rtp.ParseRtpHeader(packet)
if !ok {
t.Fatalf("packet %d has no RTP header", i)
}
if header.PayloadType != rtp.RtpPayloadTypeAppData || header.Ssrc != ssrc || header.SequenceNumber != uint16(i+1) || header.Timestamp != uint32((i+1)*appDataTimestampStep) {
t.Fatalf("packet %d header = %+v", i, header)
}
}
receiverPipe, err := NewMediaPipeline(callKey, "222222222222222:0@lid", "111111111111111:0@lid", ssrc, FrameSamples)
if err != nil {
t.Fatalf("receiver pipe: %v", err)
}
_, payload, ok := receiverPipe.UnprotectAudio(packets[0])
if !ok {
t.Fatal("receiver could not unprotect app-data packet")
}
if !bytes.Equal(payload, encodeAppDataReaction(1, "๐")) {
t.Fatalf("decrypted payload = %x", payload)
}
}
func TestMediaPayloadClassificationDoesNotTreatAppDataAsAudio(t *testing.T) {
if got := classifyMediaPayload(rtp.RtpHeader{PayloadType: rtp.RtpPayloadTypeAppData}); got != mediaPayloadAppData {
t.Fatalf("PT119 classified as %v, want app-data", got)
}
if got := classifyMediaPayload(rtp.RtpHeader{PayloadType: 118}); got != mediaPayloadUnknown {
t.Fatalf("unknown PT classified as %v, want unknown", got)
}
}
func TestCallSendsReactionThroughActiveAppDataStream(t *testing.T) {
eng, call := testEngineWithOutgoingCall()
pipe, err := NewMediaPipeline(iota32(), "111111111111111:0@lid", "222222222222222:0@lid", 1234, FrameSamples)
if err != nil {
t.Fatalf("sender pipe: %v", err)
}
var sent int
sender := newAppDataSender(pipe, 1234, func(packet []byte) (int, error) {
sent++
return len(packet), nil
})
sender.retransmitInterval = 0
eng.calls[call.ID()].appDataTx = sender
if err = call.SendReaction("๐"); err != nil {
t.Fatalf("SendReaction: %v", err)
}
if sent != appDataRetransmitCount {
t.Fatalf("sent %d packets, want %d", sent, appDataRetransmitCount)
}
}
func TestCallSendReactionRequiresActiveAppDataStream(t *testing.T) {
_, call := testEngineWithOutgoingCall()
if err := call.SendReaction("๐"); !errors.Is(err, errAppDataUnavailable) {
t.Fatalf("SendReaction error = %v, want %v", err, errAppDataUnavailable)
}
}