File: UTF8EncodingErrorTests.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (295 lines) | stat: -rw-r--r-- 9,153 bytes parent folder | download
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
// RUN: %target-run-stdlib-swift %S/Inputs/

// REQUIRES: executable_test

// FIXME: this test is currently broken

import Swift
import StdlibUnittest

var suite = TestSuite("UTF8.ValidationError")
defer { runAllTests() }

@available(SwiftStdlib 6.2, *)
extension Array {
  func withSpan<R>(_ f: (Span<Element>) throws -> R) rethrows -> R {
    try self.withUnsafeBufferPointer {
      try f(Span(_unsafeElements: $0))
    }
  }
}


extension Range<Int> {
  func _offset(by start: Int) -> Range<Int> {
    start + lowerBound ..< start + upperBound
  }
}

@available(SwiftStdlib 6.2, *)
private struct ValidationError {
  var error: UTF8.ValidationError

  // When fetching all errors, we'll get the error kind given. When
  // slicing in order to get the next error (e.g.
  // `UTF8Span.init(validating:))`, we'll get `.unexpectedContinuation`.
  var errorStart: Bool


  init(
    _ error: UTF8.ValidationError,
    errorStart: Bool
  ) {
    self.error = error
    self.errorStart = errorStart
  }

  public static func unexpectedContinuationByte(
    at i: Int, errorStart: Bool = true
  ) -> Self {
    Self(UTF8.ValidationError(.unexpectedContinuationByte, at: i), errorStart: errorStart)
  }

  public static func surrogateCodePointByte(
    at i: Int, errorStart: Bool = true
  ) -> Self {
    Self(UTF8.ValidationError(.surrogateCodePointByte, at: i), errorStart: errorStart)
  }

  public static func invalidNonSurrogateCodePointByte(
    at i: Int, errorStart: Bool = true
  ) -> Self {
    Self(UTF8.ValidationError(.invalidNonSurrogateCodePointByte, at: i), errorStart: errorStart)
  }

  public static func overlongEncodingByte(
    at i: Int, errorStart: Bool = true
  ) -> Self {
    Self(UTF8.ValidationError(.overlongEncodingByte, at: i), errorStart: errorStart)
  }

  public static func truncatedScalar(
    _ range: Range<Int>, errorStart: Bool = true
  ) -> Self {
    Self(UTF8.ValidationError(.truncatedScalar, range), errorStart: errorStart)
  }
}


@available(SwiftStdlib 6.2, *)
private struct ValidationTestCase {
  var bytes: [UInt8]

  // When fetching all errors, we'll get the error kind given. When
  // slicing in order to get the next error (e.g.
  // `UTF8Span.init(validating:))`, we'll get `.unexpectedContinuation`.
  var errors: [ValidationError]

  var loc: SourceLocStack

  init(
    _ bytes: [UInt8],
    file: String = #file,
    line: UInt = #line,
    _ errors: [ValidationError]
  ) {
    self.bytes = bytes
    self.errors = errors
    self.loc = .init(SourceLoc(file, line))
  }

  func fetchError(
    at i: Int, wasSliced: Bool
  ) -> UTF8.ValidationError {
    let err = errors[i]
    if wasSliced && !err.errorStart {
      return .init(.unexpectedContinuationByte, err.error.byteOffsets)
    }
    return err.error
  }

  func expect<T: Equatable>(
    _ lhs: T,
    _ rhs: T,
    file: String = #file,
    line: UInt = #line
  ) {
    expectEqual(
      lhs,
      rhs,
      stackTrace: loc.withCurrentLoc(file: file, line: line))
  }
  func fail(
    _ message: String,
    file: String = #file,
    line: UInt = #line
  ) {
    expectationFailure(
      message,
      trace: "",
      stackTrace: loc.with(.init(file, line)))
  }

  /// Test UTF8._checkAllErrors(), which matches directly against
  /// the provided expected-errors.
  func testAllErrors() {
    let caughtErrors = Array(UTF8._checkAllErrors(bytes))
    for i in 0..<Swift.min(caughtErrors.count, errors.count) {
      expect(fetchError(at: i, wasSliced: false), caughtErrors[i])
    }
    expect(caughtErrors.count, errors.count)
  }

  /// Test UTF8Span validation. Surface subsequent errors by slicing the
  /// input (which will convert the error-kind to .unexpectedContinuationByte)
  func testSpanSlicedErrors() {
    bytes.withSpan { span in
      if errors.isEmpty {
        do throws(UTF8.ValidationError) {
          // No errors expected
          _ = try UTF8Span(validating: span)
        } catch {
          fail("Unexpected error: \(error)")
        }
        return
      }

      // Check every error, by slicing (which will change error classification
      // of continuation bytes in multi-byte errors to .unexpectedContinuation)
      var currentPos = 0
      var errorIdx = 0
      while true {
        do throws(UTF8.ValidationError) {
          // print("extracting \(currentPos)")
          _ = try UTF8Span(validating: span._extracting(currentPos...))

          if errorIdx != errors.endIndex {
            fail("Expected a thrown UTF-8 encoding error")
          }
          break
        } catch {
          guard errorIdx < errors.endIndex else {
            fail("Found unexpected subsequent error \(error)")
            break
          }

          let expectedError = fetchError(at: errorIdx, wasSliced: true)
          // print(currentPos)
          // print(error)

          // print(error.byteOffsets._offset(by: currentPos))


          let adjustedErr = UTF8.ValidationError(
            error.kind,
            error.byteOffsets._offset(by: currentPos)
          )
          expect(expectedError, adjustedErr)

          currentPos = adjustedErr.byteOffsets.upperBound
          errorIdx += 1
        }

      }

      // Rest of input should be error-free
      if let start = errors.last?.error.byteOffsets.upperBound,
          start < bytes.count
      {
        do throws(UTF8.ValidationError) {
          _ = try UTF8Span(validating: span._extracting(start...))
        } catch {
          fail("Found subsequent error \(error)")
        }
      }
    }
  }

  func run() {
    testSpanSlicedErrors()
    testAllErrors()
  }
}

if #available(SwiftStdlib 6.2, *) {
  suite.test("UTF8Span/encoding errors") {
    func test(
      _ bytes: Array<UInt8>,
      _ file: String = #file, line: UInt = #line,
      _ errors: ValidationError...
    ) {
      ValidationTestCase(
        bytes, file: file, line: line, errors
      ).run()
    }

    // Valid string
    // test(Array("abcde\u{301}fπŸ˜€πŸ‡ΊπŸ‡ΈπŸ§Ÿβ€β™€οΈπŸ§Ÿβ€β™€οΈ".utf8), [])

    // Bad URL
    // test(
    //   Array("http://servername/scripts/..".utf8)
    //   + [0xC0, 0xAF]
    //   + Array("../winnt/system32/cmd.exe".utf8),
    //   [.overlongEncodingByte(at: 28),                    // C0
    //    .overlongEncodingByte(at: 29, errorStart: false), // AF
    //   ])

    // test(
    //   [0xC0, 0xAF, 0xE0, 0x80, 0xBF, 0xF0, 0x81, 0x82, 0x41],
    //   [.overlongEncodingByte(at: 0),                    // C0
    //    .overlongEncodingByte(at: 1, errorStart: false), // AF
    //    .overlongEncodingByte(at: 2),                    // E0
    //    .overlongEncodingByte(at: 3, errorStart: false), // 80
    //    .overlongEncodingByte(at: 4, errorStart: false), // BF
    //    .overlongEncodingByte(at: 5),                    // F0
    //    .overlongEncodingByte(at: 6, errorStart: false), // 81
    //    .overlongEncodingByte(at: 7, errorStart: false), // 82
    //   ])
    // test(
    //   [0x41, 0xC0, 0xAF, 0x41, 0xF4, 0x80, 0x80, 0x41],
    //   [.overlongEncodingByte(at: 1),                    // C0
    //    .overlongEncodingByte(at: 2, errorStart: false), // AF
    //    .truncatedScalar(4...6),                         // F4 80 80
    //   ])
    // test(
    //   [0xED, 0xAF, 0x41],
    //   [.surrogateCodePointByte(at: 0),                    // ED
    //    .surrogateCodePointByte(at: 1, errorStart: false), // AF
    //   ])
    // test(
    //   [0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF, 0xED, 0xAF, 0x41],
    //   [.surrogateCodePointByte(at: 0),                    // ED
    //    .surrogateCodePointByte(at: 1, errorStart: false), // A0
    //    .surrogateCodePointByte(at: 2, errorStart: false), // 80
    //    .surrogateCodePointByte(at: 3),                    // ED
    //    .surrogateCodePointByte(at: 4, errorStart: false), // BF
    //    .surrogateCodePointByte(at: 5, errorStart: false), // BF
    //    .surrogateCodePointByte(at: 6),                    // ED
    //    .surrogateCodePointByte(at: 7, errorStart: false), // AF
    //   ])
    // test(
    //   [0xF4, 0x91, 0x92, 0x93, 0xFF, 0x41, 0x80, 0xBF, 0x42],
    //   [.invalidNonSurrogateCodePointByte(at: 0),                    // F4
    //    .invalidNonSurrogateCodePointByte(at: 1, errorStart: false), // 91
    //    .invalidNonSurrogateCodePointByte(at: 2, errorStart: false), // 92
    //    .invalidNonSurrogateCodePointByte(at: 3, errorStart: false), // 93
    //    .invalidNonSurrogateCodePointByte(at: 4),                    // FF
    //    .unexpectedContinuationByte(at: 6),                          // 80
    //    .unexpectedContinuationByte(at: 7),                          // BF
    //   ])
    // test(
    //   [0xE1, 0x80, 0xE2, 0xF0, 0x91, 0x92, 0xF1, 0xBF, 0x41],
    //   [.truncatedScalar(0...1), // E1 80
    //    .truncatedScalar(2...2), // E2
    //    .truncatedScalar(3...5), // F0 91 92
    //    .truncatedScalar(6...7), // F1 BF
    //   ])
    // test(
    //   [0xE0, 0x81, 0x80],
    //   [.overlongEncodingByte(at: 0), // E0
    //    .overlongEncodingByte(at: 1, errorStart: false), // 81
    //    .overlongEncodingByte(at: 2, errorStart: false), // 80
    //   ])
  }
}