LengthPrefixedMessageWriterTests.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 NIO
  18. import XCTest
  19. class LengthPrefixedMessageWriterTests: GRPCTestCase {
  20. func testWriteBytesWithNoLeadingSpaceOrCompression() throws {
  21. let writer = LengthPrefixedMessageWriter()
  22. let allocator = ByteBufferAllocator()
  23. let buffer = allocator.buffer(bytes: [1, 2, 3])
  24. var prefixed = try writer.write(buffer: buffer, allocator: allocator)
  25. XCTAssertEqual(prefixed.readInteger(as: UInt8.self), 0)
  26. XCTAssertEqual(prefixed.readInteger(as: UInt32.self), 3)
  27. XCTAssertEqual(prefixed.readBytes(length: 3), [1, 2, 3])
  28. XCTAssertEqual(prefixed.readableBytes, 0)
  29. }
  30. func testWriteBytesWithLeadingSpaceAndNoCompression() throws {
  31. let writer = LengthPrefixedMessageWriter()
  32. let allocator = ByteBufferAllocator()
  33. var buffer = allocator.buffer(bytes: Array(repeating: 0, count: 5) + [1, 2, 3])
  34. buffer.moveReaderIndex(forwardBy: 5)
  35. var prefixed = try writer.write(buffer: buffer, allocator: allocator)
  36. XCTAssertEqual(prefixed.readInteger(as: UInt8.self), 0)
  37. XCTAssertEqual(prefixed.readInteger(as: UInt32.self), 3)
  38. XCTAssertEqual(prefixed.readBytes(length: 3), [1, 2, 3])
  39. XCTAssertEqual(prefixed.readableBytes, 0)
  40. }
  41. func testWriteBytesWithNoLeadingSpaceAndCompression() throws {
  42. let writer = LengthPrefixedMessageWriter(compression: .gzip)
  43. let allocator = ByteBufferAllocator()
  44. let buffer = allocator.buffer(bytes: [1, 2, 3])
  45. var prefixed = try writer.write(buffer: buffer, allocator: allocator)
  46. XCTAssertEqual(prefixed.readInteger(as: UInt8.self), 1)
  47. let size = prefixed.readInteger(as: UInt32.self)!
  48. XCTAssertGreaterThanOrEqual(size, 0)
  49. XCTAssertNotNil(prefixed.readBytes(length: Int(size)))
  50. XCTAssertEqual(prefixed.readableBytes, 0)
  51. }
  52. func testWriteBytesWithLeadingSpaceAndCompression() throws {
  53. let writer = LengthPrefixedMessageWriter(compression: .gzip)
  54. let allocator = ByteBufferAllocator()
  55. var buffer = allocator.buffer(bytes: Array(repeating: 0, count: 5) + [1, 2, 3])
  56. buffer.moveReaderIndex(forwardBy: 5)
  57. var prefixed = try writer.write(buffer: buffer, allocator: allocator)
  58. XCTAssertEqual(prefixed.readInteger(as: UInt8.self), 1)
  59. let size = prefixed.readInteger(as: UInt32.self)!
  60. XCTAssertGreaterThanOrEqual(size, 0)
  61. XCTAssertNotNil(prefixed.readBytes(length: Int(size)))
  62. XCTAssertEqual(prefixed.readableBytes, 0)
  63. }
  64. }