HTTPVersionParserTests.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  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. @testable import GRPC
  17. import NIOCore
  18. import XCTest
  19. class HTTPVersionParserTests: GRPCTestCase {
  20. private let preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  21. func testHTTP2ExactlyTheRightBytes() {
  22. let buffer = ByteBuffer(string: self.preface)
  23. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer), .accepted)
  24. }
  25. func testHTTP2TheRightBytesAndMore() {
  26. var buffer = ByteBuffer(string: self.preface)
  27. buffer.writeRepeatingByte(42, count: 1024)
  28. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer), .accepted)
  29. }
  30. func testHTTP2NoBytes() {
  31. let empty = ByteBuffer()
  32. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(empty), .notEnoughBytes)
  33. }
  34. func testHTTP2NotEnoughBytes() {
  35. var buffer = ByteBuffer(string: self.preface)
  36. buffer.moveWriterIndex(to: buffer.writerIndex - 1)
  37. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer), .notEnoughBytes)
  38. }
  39. func testHTTP2EnoughOfTheWrongBytes() {
  40. let buffer = ByteBuffer(string: String(self.preface.reversed()))
  41. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer), .rejected)
  42. }
  43. func testHTTP1RequestLine() {
  44. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1\r\n")
  45. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .accepted)
  46. }
  47. func testHTTP1RequestLineAndMore() {
  48. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1\r\nMore")
  49. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .accepted)
  50. }
  51. func testHTTP1RequestLineWithoutCRLF() {
  52. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html HTTP/1.1")
  53. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .notEnoughBytes)
  54. }
  55. func testHTTP1NoBytes() {
  56. let empty = ByteBuffer()
  57. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(empty), .notEnoughBytes)
  58. }
  59. func testHTTP1IncompleteRequestLine() {
  60. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html")
  61. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .notEnoughBytes)
  62. }
  63. func testHTTP1MalformedVersion() {
  64. let buffer = ByteBuffer(staticString: "GET https://grpc.io/index.html ptth/1.1\r\n")
  65. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .rejected)
  66. }
  67. func testTooManyIncorrectBytes() {
  68. let buffer = ByteBuffer(repeating: UInt8(ascii: "\r"), count: 2048)
  69. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP2ConnectionPreface(buffer), .rejected)
  70. XCTAssertEqual(HTTPVersionParser.prefixedWithHTTP1RequestLine(buffer), .rejected)
  71. }
  72. }