-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathsni_reporter_test.go
209 lines (190 loc) · 5.56 KB
/
sni_reporter_test.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2023 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package intra
import (
"bytes"
"errors"
"strings"
"testing"
"golang.org/x/net/dns/dnsmessage"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/doh"
"github.com/Jigsaw-Code/Intra/Android/app/src/go/intra/split"
)
type qfunc func(q []byte) ([]byte, error)
type fakeTransport struct {
doh.Transport
query qfunc
}
func (t *fakeTransport) Query(q []byte) ([]byte, error) {
return t.query(q)
}
func newFakeTransport(query qfunc) *fakeTransport {
return &fakeTransport{query: query}
}
func sendReport(t *testing.T, r *tcpSNIReporter, summary TCPSocketSummary, response []byte, responseErr error) string {
// This function blocks for the burst duration (10 seconds), so it's important that
// all tests that use it run in parallel to avoid extreme test delays.
t.Parallel()
c := make(chan string)
dns := newFakeTransport(func(q []byte) ([]byte, error) {
var msg dnsmessage.Message
err := msg.Unpack(q)
if err != nil {
t.Fatal(err)
}
name := msg.Questions[0].Name.String()
c <- name
return response, responseErr
})
r.SetDNS(dns)
r.Report(summary)
return <-c
}
const suffix = "mydomain.example"
const country = "zz"
func runSuccessTest(t *testing.T, summary TCPSocketSummary) string {
r := tcpSNIReporter{}
var stubFile bytes.Buffer
r.Configure(&stubFile, suffix, country)
return sendReport(t, &r, summary, make([]byte, 100), nil)
}
func TestSuccessClosed(t *testing.T) {
summary := TCPSocketSummary{
DownloadBytes: 10000, // >0 indicates success
UploadBytes: 5000,
Retry: &split.RetryStats{
Timeout: false, // Socket was explicitly closed
Split: 48, // >0 indicates a split was attempted
SNI: "user.domain.test", // SNI of the socket
},
}
name := runSuccessTest(t, summary)
labels := strings.Split(name, ".")
if labels[0] != "success" {
t.Errorf("Bad name %s, %s != success", name, labels[0])
}
if labels[1] != "closed" {
t.Errorf("Bad name %s, %s != closed", name, labels[1])
}
// labels[2] is the bin, which is random.
if labels[3] != "zz" {
t.Errorf("Bad name %s, %s != zz", name, labels[1])
}
// labels[4] is the date, which is not controlled by the code under test.
remainder := strings.Join(labels[5:], ".")
expected := summary.Retry.SNI + "." + suffix + "."
if remainder != expected {
t.Errorf("Bad name %s, %s != %s", name, remainder, expected)
}
}
func TestTimeout(t *testing.T) {
summary := TCPSocketSummary{
DownloadBytes: 10000, // >0 indicates success
UploadBytes: 5000,
Retry: &split.RetryStats{
Timeout: true, // Socket timed out
Split: 54, // >0 indicates a split was attempted
SNI: "user.domain.test", // SNI of the socket
},
}
name := runSuccessTest(t, summary)
labels := strings.Split(name, ".")
if labels[1] != "timeout" {
t.Errorf("Bad name %s, %s != timeout", name, labels[1])
}
}
func TestFail(t *testing.T) {
summary := TCPSocketSummary{
DownloadBytes: 0, // 0 indicates failure
UploadBytes: 500,
Retry: &split.RetryStats{
Timeout: true, // Socket timed out
Split: 36, // >0 indicates a split was attempted
SNI: "user.domain.test", // SNI of the socket
},
}
name := runSuccessTest(t, summary)
labels := strings.Split(name, ".")
if labels[0] != "failed" {
t.Errorf("Bad name %s, %s != failed", name, labels[0])
}
}
func TestError(t *testing.T) {
r := tcpSNIReporter{}
var stubFile bytes.Buffer
r.Configure(&stubFile, suffix, country)
summary := TCPSocketSummary{
DownloadBytes: 5000,
UploadBytes: 500,
Retry: &split.RetryStats{
Timeout: true,
Split: 36,
SNI: "user.domain.test",
},
}
// Verify that I/O errors don't cause a panic.
sendReport(t, &r, summary, nil, errors.New("DNS send failed"))
}
func TestNoSplit(t *testing.T) {
r := tcpSNIReporter{}
var stubFile bytes.Buffer
r.Configure(&stubFile, suffix, country)
summary := TCPSocketSummary{
DownloadBytes: 5000,
UploadBytes: 500,
Retry: &split.RetryStats{
Timeout: true,
Split: 0,
SNI: "user.domain.test",
},
}
dns := newFakeTransport(func(q []byte) ([]byte, error) {
t.Error("DNS query function should not be called because no split was performed")
return nil, errors.New("Unreachable")
})
r.SetDNS(dns)
r.Report(summary)
}
func TestUnconfigured(t *testing.T) {
r := tcpSNIReporter{}
summary := TCPSocketSummary{
DownloadBytes: 5000,
UploadBytes: 500,
Retry: &split.RetryStats{
Timeout: true,
Split: 45,
SNI: "user.domain.test",
},
}
dns := newFakeTransport(func(q []byte) ([]byte, error) {
t.Error("DNS query function should not be called because the reporter is not configured")
return nil, errors.New("Unreachable")
})
r.SetDNS(dns)
r.Report(summary)
}
func TestNoDNS(t *testing.T) {
r := tcpSNIReporter{}
summary := TCPSocketSummary{
DownloadBytes: 5000,
UploadBytes: 500,
Retry: &split.RetryStats{
Timeout: true,
Split: 45,
SNI: "user.domain.test",
},
}
// Verify that this doesn't panic.
r.Report(summary)
}