ZlibTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ZlibTests: GRPCTestCase {
  20. var allocator = ByteBufferAllocator()
  21. var inputSize = 4_096
  22. func makeBytes(count: Int) -> [UInt8] {
  23. return (0..<count).map { _ in
  24. UInt8.random(in: UInt8(ascii: "a")...UInt8(ascii: "z"))
  25. }
  26. }
  27. @discardableResult
  28. func doCompressAndDecompress(of bytes: [UInt8], format: Zlib.CompressionFormat) throws -> Int {
  29. var data = self.allocator.buffer(capacity: 0)
  30. data.writeBytes(bytes)
  31. // Compress it.
  32. let deflate = Zlib.Deflate(format: format)
  33. var compressed = self.allocator.buffer(capacity: 0)
  34. let compressedBytesWritten = try deflate.deflate(&data, into: &compressed)
  35. // Did we write the right number of bytes?
  36. XCTAssertEqual(compressedBytesWritten, compressed.readableBytes)
  37. // Decompress it.
  38. let inflate = Zlib.Inflate(format: format)
  39. var decompressed = self.allocator.buffer(capacity: self.inputSize)
  40. let decompressedBytesWritten = try inflate.inflate(&compressed, into: &decompressed)
  41. // Did we write the right number of bytes?
  42. XCTAssertEqual(decompressedBytesWritten, decompressed.readableBytes)
  43. // Did we get back to where we started?
  44. XCTAssertEqual(decompressed.readBytes(length: decompressed.readableBytes), bytes)
  45. return compressedBytesWritten
  46. }
  47. func testCompressionAndDecompressionOfASCIIBytes() throws {
  48. let bytes = self.makeBytes(count: self.inputSize)
  49. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  50. try self.doCompressAndDecompress(of: bytes, format: format)
  51. }
  52. }
  53. func testCompressionAndDecompressionOfZeros() throws {
  54. // This test makes sure the decompressor is capable of increasing the output buffer size a
  55. // number of times.
  56. let bytes: [UInt8] = Array(repeating: 0, count: self.inputSize)
  57. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  58. let compressedSize = try self.doCompressAndDecompress(of: bytes, format: format)
  59. // Is the compressed size significantly smaller than the input size?
  60. XCTAssertLessThan(compressedSize, bytes.count / 4)
  61. }
  62. }
  63. func testCompressionAndDecompressionOfHardToCompressData() throws {
  64. let bytes: [UInt8] = (0..<self.inputSize).map { _ in
  65. return UInt8.random(in: UInt8.min...UInt8.max)
  66. }
  67. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  68. // Is the compressed size larger than the input size?
  69. let compressedSize = try self.doCompressAndDecompress(of: bytes, format: format)
  70. XCTAssertGreaterThan(compressedSize, bytes.count)
  71. }
  72. }
  73. func testCompressionAndDecompressionWithResets() throws {
  74. // Generate some input.
  75. let byteArrays = (0..<5).map { _ in
  76. self.makeBytes(count: self.inputSize)
  77. }
  78. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  79. let deflate = Zlib.Deflate(format: format)
  80. let inflate = Zlib.Inflate(format: format)
  81. for bytes in byteArrays {
  82. var data = self.allocator.buffer(capacity: 0)
  83. data.writeBytes(bytes)
  84. // Compress it.
  85. var compressed = self.allocator.buffer(capacity: 0)
  86. let compressedBytesWritten = try deflate.deflate(&data, into: &compressed)
  87. deflate.reset()
  88. // Did we write the right number of bytes?
  89. XCTAssertEqual(compressedBytesWritten, compressed.readableBytes)
  90. // Decompress it.
  91. var decompressed = self.allocator.buffer(capacity: self.inputSize)
  92. let decompressedBytesWritten = try inflate.inflate(&compressed, into: &decompressed)
  93. inflate.reset()
  94. // Did we write the right number of bytes?
  95. XCTAssertEqual(decompressedBytesWritten, decompressed.readableBytes)
  96. // Did we get back to where we started?
  97. XCTAssertEqual(decompressed.readBytes(length: decompressed.readableBytes), bytes)
  98. }
  99. }
  100. }
  101. func testDecompressThrowsOnGibberish() throws {
  102. let bytes = self.makeBytes(count: self.inputSize)
  103. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  104. var buffer = self.allocator.buffer(capacity: bytes.count)
  105. buffer.writeBytes(bytes)
  106. let inflate = Zlib.Inflate(format: format)
  107. var output = self.allocator.buffer(capacity: 0)
  108. XCTAssertThrowsError(try inflate.inflate(&buffer, into: &output)) { error in
  109. let withContext = error as? GRPCError.WithContext
  110. XCTAssert(withContext?.error is GRPCError.ZlibCompressionFailure)
  111. }
  112. }
  113. }
  114. }