okhttp

Форк
0
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
 */
16
package okhttp3.internal.idn
17

18
import assertk.assertThat
19
import assertk.assertions.containsExactly
20
import assertk.assertions.isEqualTo
21
import okhttp3.internal.idn.MappedRange.InlineDelta
22
import okio.Buffer
23
import okio.ByteString
24
import okio.ByteString.Companion.encodeUtf8
25
import org.junit.jupiter.api.Test
26

27
class MappingTablesTest {
28
  @Test fun simplifyCombinesMultipleMappings() {
29
    assertThat(
30
      mergeAdjacentRanges(
31
        listOf(
32
          Mapping(0x0232, 0x0232, TYPE_MAPPED, "a".encodeUtf8()),
33
          Mapping(0x0233, 0x0233, TYPE_VALID, ByteString.EMPTY),
34
          Mapping(0x0234, 0x0236, TYPE_VALID, ByteString.EMPTY),
35
          Mapping(0x0237, 0x0239, TYPE_VALID, ByteString.EMPTY),
36
          Mapping(0x023a, 0x023a, TYPE_MAPPED, "b".encodeUtf8()),
37
        ),
38
      ),
39
    ).containsExactly(
40
      Mapping(0x0232, 0x0232, TYPE_MAPPED, "a".encodeUtf8()),
41
      Mapping(0x0233, 0x0239, TYPE_VALID, ByteString.EMPTY),
42
      Mapping(0x023a, 0x023a, TYPE_MAPPED, "b".encodeUtf8()),
43
    )
44
  }
45

46
  @Test fun simplifyDoesNotCombineWhenMappedTargetsAreDifferent() {
47
    assertThat(
48
      mergeAdjacentRanges(
49
        listOf(
50
          Mapping(0x0041, 0x0041, TYPE_MAPPED, "a".encodeUtf8()),
51
          Mapping(0x0042, 0x0042, TYPE_MAPPED, "b".encodeUtf8()),
52
        ),
53
      ),
54
    ).containsExactly(
55
      Mapping(0x0041, 0x0041, TYPE_MAPPED, "a".encodeUtf8()),
56
      Mapping(0x0042, 0x0042, TYPE_MAPPED, "b".encodeUtf8()),
57
    )
58
  }
59

60
  @Test fun simplifyCanonicalizesType() {
61
    assertThat(
62
      mergeAdjacentRanges(
63
        listOf(
64
          Mapping(0x0000, 0x002c, TYPE_DISALLOWED_STD3_VALID, ByteString.EMPTY),
65
        ),
66
      ),
67
    ).containsExactly(
68
      Mapping(0x0000, 0x002c, TYPE_VALID, ByteString.EMPTY),
69
    )
70
  }
71

72
  @Test fun simplifyCombinesCanonicalEquivalent() {
73
    assertThat(
74
      mergeAdjacentRanges(
75
        listOf(
76
          Mapping(0x0000, 0x002c, TYPE_DISALLOWED_STD3_VALID, ByteString.EMPTY),
77
          Mapping(0x002d, 0x002e, TYPE_VALID, ByteString.EMPTY),
78
        ),
79
      ),
80
    ).containsExactly(
81
      Mapping(0x0000, 0x002e, TYPE_VALID, ByteString.EMPTY),
82
    )
83
  }
84

85
  @Test fun withSectionStartsSplits() {
86
    assertThat(
87
      withoutSectionSpans(
88
        listOf(
89
          Mapping(0x40000, 0x40180, TYPE_DISALLOWED, ByteString.EMPTY),
90
        ),
91
      ),
92
    ).containsExactly(
93
      Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
94
      Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
95
      Mapping(0x40100, 0x4017f, TYPE_DISALLOWED, ByteString.EMPTY),
96
      Mapping(0x40180, 0x40180, TYPE_DISALLOWED, ByteString.EMPTY),
97
    )
98
  }
99

100
  @Test fun withSectionStartAlreadySplit() {
101
    assertThat(
102
      withoutSectionSpans(
103
        listOf(
104
          Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
105
          Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
106
        ),
107
      ),
108
    ).containsExactly(
109
      Mapping(0x40000, 0x4007f, TYPE_DISALLOWED, ByteString.EMPTY),
110
      Mapping(0x40080, 0x400ff, TYPE_DISALLOWED, ByteString.EMPTY),
111
    )
112
  }
113

114
  @Test fun mergeAdjacentDeltaMappedRangesWithMultipleDeltas() {
115
    assertThat(
116
      mergeAdjacentDeltaMappedRanges(
117
        mutableListOf(
118
          InlineDelta(1, 5),
119
          InlineDelta(2, 5),
120
          InlineDelta(3, 5),
121
          MappedRange.External(4, "a".encodeUtf8()),
122
        ),
123
      ),
124
    ).containsExactly(
125
      InlineDelta(1, 5),
126
      MappedRange.External(4, "a".encodeUtf8()),
127
    )
128
  }
129

130
  @Test fun mergeAdjacentDeltaMappedRangesWithDifferentSizedDeltas() {
131
    assertThat(
132
      mergeAdjacentDeltaMappedRanges(
133
        mutableListOf(
134
          InlineDelta(1, 5),
135
          InlineDelta(2, 5),
136
          InlineDelta(3, 1),
137
        ),
138
      ),
139
    ).containsExactly(
140
      InlineDelta(1, 5),
141
      InlineDelta(3, 1),
142
    )
143
  }
144

145
  @Test fun inlineDeltaOrNullValid() {
146
    assertThat(
147
      inlineDeltaOrNull(
148
        mappingOf(
149
          sourceCodePoint0 = 1,
150
          sourceCodePoint1 = 1,
151
          mappedToCodePoints = listOf(2),
152
        ),
153
      ),
154
    ).isEqualTo(InlineDelta(1, 1))
155

156
    assertThat(
157
      inlineDeltaOrNull(
158
        mappingOf(
159
          sourceCodePoint0 = 2,
160
          sourceCodePoint1 = 2,
161
          mappedToCodePoints = listOf(1),
162
        ),
163
      ),
164
    ).isEqualTo(InlineDelta(2, -1))
165
  }
166

167
  @Test fun inlineDeltaOrNullMultipleSourceCodePoints() {
168
    assertThat(
169
      inlineDeltaOrNull(
170
        mappingOf(
171
          sourceCodePoint0 = 2,
172
          sourceCodePoint1 = 3,
173
          mappedToCodePoints = listOf(2),
174
        ),
175
      ),
176
    ).isEqualTo(null)
177
  }
178

179
  @Test fun inlineDeltaOrNullMultipleMappedToCodePoints() {
180
    assertThat(
181
      inlineDeltaOrNull(
182
        mappingOf(
183
          sourceCodePoint0 = 1,
184
          sourceCodePoint1 = 1,
185
          mappedToCodePoints = listOf(2, 3),
186
        ),
187
      ),
188
    ).isEqualTo(null)
189
  }
190

191
  @Test fun inlineDeltaOrNullMaxCodepointDelta() {
192
    assertThat(
193
      inlineDeltaOrNull(
194
        mappingOf(
195
          sourceCodePoint0 = 0,
196
          sourceCodePoint1 = 0,
197
          mappedToCodePoints = listOf((1 shl 18) - 1),
198
        ),
199
      ),
200
    ).isEqualTo(
201
      InlineDelta(
202
        rangeStart = 0,
203
        codepointDelta = InlineDelta.MAX_VALUE,
204
      ),
205
    )
206

207
    assertThat(
208
      inlineDeltaOrNull(
209
        mappingOf(
210
          sourceCodePoint0 = 0,
211
          sourceCodePoint1 = 0,
212
          mappedToCodePoints = listOf(1 shl 18),
213
        ),
214
      ),
215
    ).isEqualTo(null)
216
  }
217

218
  private fun mappingOf(
219
    sourceCodePoint0: Int,
220
    sourceCodePoint1: Int,
221
    mappedToCodePoints: List<Int>,
222
  ): Mapping =
223
    Mapping(
224
      sourceCodePoint0 = sourceCodePoint0,
225
      sourceCodePoint1 = sourceCodePoint1,
226
      type = TYPE_MAPPED,
227
      mappedTo =
228
        Buffer().also {
229
          for (cp in mappedToCodePoints) {
230
            it.writeUtf8CodePoint(cp)
231
          }
232
        }.readByteString(),
233
    )
234
}
235

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.