okhttp
234 строки · 6.3 Кб
1/*
2* Copyright (C) 2023 Square, Inc.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16package okhttp3.internal.idn
17
18import assertk.assertThat
19import assertk.assertions.containsExactly
20import assertk.assertions.isEqualTo
21import okhttp3.internal.idn.MappedRange.InlineDelta
22import okio.Buffer
23import okio.ByteString
24import okio.ByteString.Companion.encodeUtf8
25import org.junit.jupiter.api.Test
26
27class MappingTablesTest {
28@Test fun simplifyCombinesMultipleMappings() {
29assertThat(
30mergeAdjacentRanges(
31listOf(
32Mapping(0x0232, 0x0232, TYPE_MAPPED, "a".encodeUtf8()),
33Mapping(0x0233, 0x0233, TYPE_VALID, ByteString.EMPTY),
34Mapping(0x0234, 0x0236, TYPE_VALID, ByteString.EMPTY),
35Mapping(0x0237, 0x0239, TYPE_VALID, ByteString.EMPTY),
36Mapping(0x023a, 0x023a, TYPE_MAPPED, "b".encodeUtf8()),
37),
38),
39).containsExactly(
40Mapping(0x0232, 0x0232, TYPE_MAPPED, "a".encodeUtf8()),
41Mapping(0x0233, 0x0239, TYPE_VALID, ByteString.EMPTY),
42Mapping(0x023a, 0x023a, TYPE_MAPPED, "b".encodeUtf8()),
43)
44}
45
46@Test fun simplifyDoesNotCombineWhenMappedTargetsAreDifferent() {
47assertThat(
48mergeAdjacentRanges(
49listOf(
50Mapping(0x0041, 0x0041, TYPE_MAPPED, "a".encodeUtf8()),
51Mapping(0x0042, 0x0042, TYPE_MAPPED, "b".encodeUtf8()),
52),
53),
54).containsExactly(
55Mapping(0x0041, 0x0041, TYPE_MAPPED, "a".encodeUtf8()),
56Mapping(0x0042, 0x0042, TYPE_MAPPED, "b".encodeUtf8()),
57)
58}
59
60@Test fun simplifyCanonicalizesType() {
61assertThat(
62mergeAdjacentRanges(
63listOf(
64Mapping(0x0000, 0x002c, TYPE_DISALLOWED_STD3_VALID, ByteString.EMPTY),
65),
66),
67).containsExactly(
68Mapping(0x0000, 0x002c, TYPE_VALID, ByteString.EMPTY),
69)
70}
71
72@Test fun simplifyCombinesCanonicalEquivalent() {
73assertThat(
74mergeAdjacentRanges(
75listOf(
76Mapping(0x0000, 0x002c, TYPE_DISALLOWED_STD3_VALID, ByteString.EMPTY),
77Mapping(0x002d, 0x002e, TYPE_VALID, ByteString.EMPTY),
78),
79),
80).containsExactly(
81Mapping(0x0000, 0x002e, TYPE_VALID, ByteString.EMPTY),
82)
83}
84
85@Test fun withSectionStartsSplits() {
86assertThat(
87withoutSectionSpans(
88listOf(
89Mapping(0x40000, 0x40180, TYPE_DISALLOWED, ByteString.EMPTY),
90),
91),
92).containsExactly(
93Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
94Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
95Mapping(0x40100, 0x4017f, TYPE_DISALLOWED, ByteString.EMPTY),
96Mapping(0x40180, 0x40180, TYPE_DISALLOWED, ByteString.EMPTY),
97)
98}
99
100@Test fun withSectionStartAlreadySplit() {
101assertThat(
102withoutSectionSpans(
103listOf(
104Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
105Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
106),
107),
108).containsExactly(
109Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
110Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
111)
112}
113
114@Test fun mergeAdjacentDeltaMappedRangesWithMultipleDeltas() {
115assertThat(
116mergeAdjacentDeltaMappedRanges(
117mutableListOf(
118InlineDelta(1, 5),
119InlineDelta(2, 5),
120InlineDelta(3, 5),
121MappedRange.External(4, "a".encodeUtf8()),
122),
123),
124).containsExactly(
125InlineDelta(1, 5),
126MappedRange.External(4, "a".encodeUtf8()),
127)
128}
129
130@Test fun mergeAdjacentDeltaMappedRangesWithDifferentSizedDeltas() {
131assertThat(
132mergeAdjacentDeltaMappedRanges(
133mutableListOf(
134InlineDelta(1, 5),
135InlineDelta(2, 5),
136InlineDelta(3, 1),
137),
138),
139).containsExactly(
140InlineDelta(1, 5),
141InlineDelta(3, 1),
142)
143}
144
145@Test fun inlineDeltaOrNullValid() {
146assertThat(
147inlineDeltaOrNull(
148mappingOf(
149sourceCodePoint0 = 1,
150sourceCodePoint1 = 1,
151mappedToCodePoints = listOf(2),
152),
153),
154).isEqualTo(InlineDelta(1, 1))
155
156assertThat(
157inlineDeltaOrNull(
158mappingOf(
159sourceCodePoint0 = 2,
160sourceCodePoint1 = 2,
161mappedToCodePoints = listOf(1),
162),
163),
164).isEqualTo(InlineDelta(2, -1))
165}
166
167@Test fun inlineDeltaOrNullMultipleSourceCodePoints() {
168assertThat(
169inlineDeltaOrNull(
170mappingOf(
171sourceCodePoint0 = 2,
172sourceCodePoint1 = 3,
173mappedToCodePoints = listOf(2),
174),
175),
176).isEqualTo(null)
177}
178
179@Test fun inlineDeltaOrNullMultipleMappedToCodePoints() {
180assertThat(
181inlineDeltaOrNull(
182mappingOf(
183sourceCodePoint0 = 1,
184sourceCodePoint1 = 1,
185mappedToCodePoints = listOf(2, 3),
186),
187),
188).isEqualTo(null)
189}
190
191@Test fun inlineDeltaOrNullMaxCodepointDelta() {
192assertThat(
193inlineDeltaOrNull(
194mappingOf(
195sourceCodePoint0 = 0,
196sourceCodePoint1 = 0,
197mappedToCodePoints = listOf((1 shl 18) - 1),
198),
199),
200).isEqualTo(
201InlineDelta(
202rangeStart = 0,
203codepointDelta = InlineDelta.MAX_VALUE,
204),
205)
206
207assertThat(
208inlineDeltaOrNull(
209mappingOf(
210sourceCodePoint0 = 0,
211sourceCodePoint1 = 0,
212mappedToCodePoints = listOf(1 shl 18),
213),
214),
215).isEqualTo(null)
216}
217
218private fun mappingOf(
219sourceCodePoint0: Int,
220sourceCodePoint1: Int,
221mappedToCodePoints: List<Int>,
222): Mapping =
223Mapping(
224sourceCodePoint0 = sourceCodePoint0,
225sourceCodePoint1 = sourceCodePoint1,
226type = TYPE_MAPPED,
227mappedTo =
228Buffer().also {
229for (cp in mappedToCodePoints) {
230it.writeUtf8CodePoint(cp)
231}
232}.readByteString(),
233)
234}
235