-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Expand file tree
/
Copy pathDnsOverHttpsTest.kt
More file actions
316 lines (293 loc) · 11 KB
/
Copy pathDnsOverHttpsTest.kt
File metadata and controls
316 lines (293 loc) · 11 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (C) 2018 Square, Inc.
*
* 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 okhttp3.dnsoverhttps
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.containsExactly
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import assertk.assertions.isNull
import java.io.EOFException
import java.io.File
import java.io.IOException
import java.net.InetAddress
import java.net.UnknownHostException
import java.util.concurrent.TimeUnit
import mockwebserver3.MockResponse
import mockwebserver3.MockWebServer
import okhttp3.Cache
import okhttp3.Dns
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.testing.PlatformRule
import okio.Buffer
import okio.ByteString.Companion.decodeHex
import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
@Tag("Slowish")
class DnsOverHttpsTest {
@RegisterExtension
val platform = PlatformRule()
private lateinit var server: MockWebServer
private lateinit var dns: Dns
private val cacheFs = FakeFileSystem()
private val bootstrapClient =
OkHttpClient.Builder()
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
.build()
@BeforeEach
fun setUp(server: MockWebServer) {
this.server = server
server.protocols = bootstrapClient.protocols
dns = buildLocalhost(bootstrapClient, false)
}
@Test
fun getOne() {
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
"0003b00049df00112",
),
)
val result = dns.lookup("google.com")
assertThat(result).isEqualTo(listOf(address("157.240.1.18")))
val recordedRequest = server.takeRequest()
assertThat(recordedRequest.method).isEqualTo("GET")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ")
}
@Test
fun getIpv6() {
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c0005000" +
"100000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c0420001000" +
"10000003b00049df00112",
),
)
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d00001c0001c00c0005000" +
"100000a1b000603617069c012c0300005000100000b1f000c04737461720463313072c012c042001c000" +
"10000003b00102a032880f0290011faceb00c00000002",
),
)
dns = buildLocalhost(bootstrapClient, true)
val result = dns.lookup("google.com")
assertThat(result.size).isEqualTo(2)
assertThat(result).contains(address("157.240.1.18"))
assertThat(result).contains(address("2a03:2880:f029:11:face:b00c:0:2"))
val request1 = server.takeRequest()
assertThat(request1.method).isEqualTo("GET")
val request2 = server.takeRequest()
assertThat(request2.method).isEqualTo("GET")
assertThat(listOf(request1.path, request2.path))
.containsExactlyInAnyOrder(
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AABwAAQ",
)
}
@Test
fun failure() {
server.enqueue(
dnsResponse(
"0000818300010000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000" +
"007070038026e7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb1" +
"2c100000e10000003840012750000000e10",
),
)
try {
dns.lookup("google.com")
fail<Any>()
} catch (uhe: UnknownHostException) {
assertThat(uhe.message).isEqualTo("google.com: NXDOMAIN")
}
val recordedRequest = server.takeRequest()
assertThat(recordedRequest.method).isEqualTo("GET")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ")
}
@Test
fun failOnExcessiveResponse() {
val array = CharArray(128 * 1024 + 2) { '0' }
server.enqueue(dnsResponse(String(array)))
try {
dns.lookup("google.com")
fail<Any>()
} catch (ioe: IOException) {
assertThat(ioe.message).isEqualTo("google.com")
val cause = ioe.cause!!
assertThat(cause).isInstanceOf<IOException>()
assertThat(cause).hasMessage("response size exceeds limit (65536 bytes): 65537 bytes")
}
}
@Test
fun failOnBadResponse() {
server.enqueue(dnsResponse("00"))
try {
dns.lookup("google.com")
fail<Any>()
} catch (ioe: IOException) {
assertThat(ioe).hasMessage("google.com")
assertThat(ioe.cause!!).isInstanceOf<EOFException>()
}
}
// TODO GET preferred order - with tests to confirm this
// 1. successful fresh cached GET response
// 2. unsuccessful (404, 500) fresh cached GET response
// 3. successful network response
// 4. successful stale cached GET response
// 5. unsuccessful response
@Test
fun usesCache() {
val cache = Cache("cache-usesCache".toPath(), (100 * 1024).toLong(), cacheFs)
val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
val cachedDns = buildLocalhost(cachedClient, false)
repeat(2) {
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
"0003b00049df00112",
)
.newBuilder()
.setHeader("cache-control", "private, max-age=298")
.build(),
)
}
var result = cachedDns.lookup("google.com")
assertThat(result).containsExactly(address("157.240.1.18"))
var recordedRequest = server.takeRequest()
assertThat(recordedRequest.method).isEqualTo("GET")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ")
result = cachedDns.lookup("google.com")
assertThat(server.takeRequest(1, TimeUnit.MILLISECONDS)).isNull()
assertThat(result).isEqualTo(listOf(address("157.240.1.18")))
result = cachedDns.lookup("www.google.com")
assertThat(result).containsExactly(address("157.240.1.18"))
recordedRequest = server.takeRequest()
assertThat(recordedRequest.method).isEqualTo("GET")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAAA3d3dwZnb29nbGUDY29tAAABAAE")
}
@Test
fun usesCacheEvenForPost() {
val cache = Cache("cache-usesCacheEvenForPost".toPath(), (100 * 1024).toLong(), cacheFs)
val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
val cachedDns = buildLocalhost(cachedClient, false, post = true)
repeat(2) {
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
"0003b00049df00112",
)
.newBuilder()
.setHeader("cache-control", "private, max-age=298")
.build(),
)
}
var result = cachedDns.lookup("google.com")
assertThat(result).containsExactly(address("157.240.1.18"))
var recordedRequest = server.takeRequest()
assertThat(recordedRequest.method).isEqualTo("POST")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct")
result = cachedDns.lookup("google.com")
assertThat(server.takeRequest(0, TimeUnit.MILLISECONDS)).isNull()
assertThat(result).isEqualTo(listOf(address("157.240.1.18")))
result = cachedDns.lookup("www.google.com")
assertThat(result).containsExactly(address("157.240.1.18"))
recordedRequest = server.takeRequest(0, TimeUnit.MILLISECONDS)!!
assertThat(recordedRequest.method).isEqualTo("POST")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct")
cache.close()
}
@Test
fun usesCacheOnlyIfFresh() {
val cache = Cache(File("./target/DnsOverHttpsTest.cache"), 100 * 1024L)
val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
val cachedDns = buildLocalhost(cachedClient, false)
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
"0003b00049df00112",
)
.newBuilder()
.setHeader("cache-control", "max-age=1")
.build(),
)
var result = cachedDns.lookup("google.com")
assertThat(result).containsExactly(address("157.240.1.18"))
var recordedRequest = server.takeRequest(0, TimeUnit.SECONDS)
assertThat(recordedRequest!!.method).isEqualTo("GET")
assertThat(recordedRequest.path).isEqualTo(
"/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ",
)
Thread.sleep(2000)
server.enqueue(
dnsResponse(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
"0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
"0003b00049df00112",
)
.newBuilder()
.setHeader("cache-control", "max-age=1")
.build(),
)
result = cachedDns.lookup("google.com")
assertThat(result).isEqualTo(listOf(address("157.240.1.18")))
recordedRequest = server.takeRequest(0, TimeUnit.SECONDS)
assertThat(recordedRequest!!.method).isEqualTo("GET")
assertThat(recordedRequest.path)
.isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ")
cache.close()
}
private fun dnsResponse(s: String): MockResponse {
return MockResponse.Builder()
.body(Buffer().write(s.decodeHex()))
.addHeader("content-type", "application/dns-message")
.addHeader("content-length", s.length / 2)
.build()
}
private fun buildLocalhost(
bootstrapClient: OkHttpClient,
includeIPv6: Boolean,
post: Boolean = false,
): DnsOverHttps {
val url = server.url("/lookup?ct")
return DnsOverHttps.Builder().client(bootstrapClient)
.includeIPv6(includeIPv6)
.resolvePrivateAddresses(true)
.url(url)
.post(post)
.build()
}
companion object {
private fun address(host: String) = InetAddress.getByName(host)
}
}