ZlibTests.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. print(compressedSize)
  71. XCTAssertGreaterThan(compressedSize, bytes.count)
  72. }
  73. }
  74. func testCompressionAndDecompressionWithResets() throws {
  75. // Generate some input.
  76. let byteArrays = (0..<5).map { _ in
  77. self.makeBytes(count: self.inputSize)
  78. }
  79. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  80. let deflate = Zlib.Deflate(format: format)
  81. let inflate = Zlib.Inflate(format: format)
  82. for bytes in byteArrays {
  83. var data = self.allocator.buffer(capacity: 0)
  84. data.writeBytes(bytes)
  85. // Compress it.
  86. var compressed = self.allocator.buffer(capacity: 0)
  87. let compressedBytesWritten = try deflate.deflate(&data, into: &compressed)
  88. deflate.reset()
  89. // Did we write the right number of bytes?
  90. XCTAssertEqual(compressedBytesWritten, compressed.readableBytes)
  91. // Decompress it.
  92. var decompressed = self.allocator.buffer(capacity: self.inputSize)
  93. let decompressedBytesWritten = try inflate.inflate(&compressed, into: &decompressed)
  94. inflate.reset()
  95. // Did we write the right number of bytes?
  96. XCTAssertEqual(decompressedBytesWritten, decompressed.readableBytes)
  97. // Did we get back to where we started?
  98. XCTAssertEqual(decompressed.readBytes(length: decompressed.readableBytes), bytes)
  99. }
  100. }
  101. }
  102. func testDecompressThrowsOnGibberish() throws {
  103. let bytes = self.makeBytes(count: self.inputSize)
  104. for format in [Zlib.CompressionFormat.deflate, .gzip] {
  105. var buffer = self.allocator.buffer(capacity: bytes.count)
  106. buffer.writeBytes(bytes)
  107. let inflate = Zlib.Inflate(format: format)
  108. var output = self.allocator.buffer(capacity: 0)
  109. XCTAssertThrowsError(try inflate.inflate(&buffer, into: &output)) { error in
  110. let withContext = error as? GRPCError.WithContext
  111. XCTAssert(withContext?.error is GRPCError.ZlibCompressionFailure)
  112. }
  113. }
  114. }
  115. }