ZlibTests.swift 4.9 KB

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