|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Square, Inc. |
| 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 | +package okio |
| 17 | + |
| 18 | +import kotlin.test.Test |
| 19 | +import kotlin.test.assertEquals |
| 20 | +import kotlin.test.assertFailsWith |
| 21 | +import kotlin.test.assertFalse |
| 22 | +import kotlin.test.assertTrue |
| 23 | +import okio.ByteString.Companion.decodeBase64 |
| 24 | +import okio.ByteString.Companion.decodeHex |
| 25 | +import okio.ByteString.Companion.toByteString |
| 26 | + |
| 27 | +class InflaterTest { |
| 28 | + @Test |
| 29 | + fun happyPath() { |
| 30 | + val inflater = Inflater().apply { |
| 31 | + source = "c89PUchIzSlQKC3WUShPVS9KVcjMUyjJSFXISMxLKVbIT1NIzUvPzEtNLSrWAwA=" |
| 32 | + .decodeBase64()!!.toByteArray() |
| 33 | + sourcePos = 0 |
| 34 | + sourceLimit = source.size |
| 35 | + |
| 36 | + target = ByteArray(256) |
| 37 | + targetPos = 0 |
| 38 | + targetLimit = target.size |
| 39 | + } |
| 40 | + |
| 41 | + assertTrue(inflater.inflate()) |
| 42 | + assertEquals(inflater.sourceLimit, inflater.sourcePos) |
| 43 | + |
| 44 | + val inflated = inflater.target.toByteString(0, inflater.targetPos) |
| 45 | + assertEquals( |
| 46 | + "God help us, we're in the hands of engineers.", |
| 47 | + inflated.utf8(), |
| 48 | + ) |
| 49 | + |
| 50 | + inflater.close() |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun inflateInParts() { |
| 55 | + val inflater = Inflater().apply { |
| 56 | + target = ByteArray(256) |
| 57 | + targetPos = 0 |
| 58 | + targetLimit = target.size |
| 59 | + } |
| 60 | + |
| 61 | + inflater.source = "c89PUchIzSlQKC3WUShPVS9KVcjMUyjJ".decodeBase64()!!.toByteArray() |
| 62 | + inflater.sourcePos = 0 |
| 63 | + inflater.sourceLimit = inflater.source.size |
| 64 | + assertFalse(inflater.inflate()) |
| 65 | + assertEquals(inflater.sourceLimit, inflater.sourcePos) |
| 66 | + |
| 67 | + inflater.source = "SFXISMxLKVbIT1NIzUvPzEtNLSrWAwA=".decodeBase64()!!.toByteArray() |
| 68 | + inflater.sourcePos = 0 |
| 69 | + inflater.sourceLimit = inflater.source.size |
| 70 | + assertTrue(inflater.inflate()) |
| 71 | + assertEquals(inflater.sourceLimit, inflater.sourcePos) |
| 72 | + |
| 73 | + val inflated = inflater.target.toByteString(0, inflater.targetPos) |
| 74 | + assertEquals( |
| 75 | + "God help us, we're in the hands of engineers.", |
| 76 | + inflated.utf8(), |
| 77 | + ) |
| 78 | + |
| 79 | + inflater.close() |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun inflateInsufficientSpaceInTarget() { |
| 84 | + val targetBuffer = Buffer() |
| 85 | + |
| 86 | + val inflater = Inflater().apply { |
| 87 | + source = "c89PUchIzSlQKC3WUShPVS9KVcjMUyjJSFXISMxLKVbIT1NIzUvPzEtNLSrWAwA=" |
| 88 | + .decodeBase64()!!.toByteArray() |
| 89 | + sourcePos = 0 |
| 90 | + sourceLimit = source.size |
| 91 | + } |
| 92 | + |
| 93 | + inflater.target = ByteArray(31) |
| 94 | + inflater.targetPos = 0 |
| 95 | + inflater.targetLimit = inflater.target.size |
| 96 | + assertFalse(inflater.inflate()) |
| 97 | + assertEquals(inflater.targetLimit, inflater.targetPos) |
| 98 | + targetBuffer.write(inflater.target) |
| 99 | + |
| 100 | + inflater.target = ByteArray(256) |
| 101 | + inflater.targetPos = 0 |
| 102 | + inflater.targetLimit = inflater.target.size |
| 103 | + assertTrue(inflater.inflate()) |
| 104 | + assertEquals(inflater.sourcePos, inflater.sourceLimit) |
| 105 | + targetBuffer.write(inflater.target, 0, inflater.targetPos) |
| 106 | + |
| 107 | + assertEquals( |
| 108 | + "God help us, we're in the hands of engineers.", |
| 109 | + targetBuffer.readUtf8(), |
| 110 | + ) |
| 111 | + |
| 112 | + inflater.close() |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun inflateEmptyContent() { |
| 117 | + val inflater = Inflater().apply { |
| 118 | + source = "AwA=".decodeBase64()!!.toByteArray() |
| 119 | + sourcePos = 0 |
| 120 | + sourceLimit = source.size |
| 121 | + |
| 122 | + target = ByteArray(256) |
| 123 | + targetPos = 0 |
| 124 | + targetLimit = target.size |
| 125 | + } |
| 126 | + |
| 127 | + assertTrue(inflater.inflate()) |
| 128 | + |
| 129 | + val inflated = inflater.target.toByteString(0, inflater.targetPos) |
| 130 | + assertEquals( |
| 131 | + "", |
| 132 | + inflated.utf8(), |
| 133 | + ) |
| 134 | + |
| 135 | + inflater.close() |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun inflateInPartsStartingWithEmptySource() { |
| 140 | + val inflater = Inflater().apply { |
| 141 | + target = ByteArray(256) |
| 142 | + targetPos = 0 |
| 143 | + targetLimit = target.size |
| 144 | + } |
| 145 | + |
| 146 | + inflater.source = ByteArray(256) |
| 147 | + inflater.sourcePos = 0 |
| 148 | + inflater.sourceLimit = 0 |
| 149 | + assertFalse(inflater.inflate()) |
| 150 | + |
| 151 | + inflater.source = "c89PUchIzSlQKC3WUShPVS9KVcjMUyjJSFXISMxLKVbIT1NIzUvPzEtNLSrWAwA=" |
| 152 | + .decodeBase64()!!.toByteArray() |
| 153 | + inflater.sourcePos = 0 |
| 154 | + inflater.sourceLimit = inflater.source.size |
| 155 | + assertTrue(inflater.inflate()) |
| 156 | + |
| 157 | + val inflated = inflater.target.toByteString(0, inflater.targetPos) |
| 158 | + assertEquals( |
| 159 | + "God help us, we're in the hands of engineers.", |
| 160 | + inflated.utf8(), |
| 161 | + ) |
| 162 | + |
| 163 | + inflater.close() |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun inflateInvalidData() { |
| 168 | + val inflater = Inflater().apply { |
| 169 | + target = ByteArray(256) |
| 170 | + targetPos = 0 |
| 171 | + targetLimit = target.size |
| 172 | + } |
| 173 | + |
| 174 | + inflater.source = "ffffffffffffffff".decodeHex().toByteArray() |
| 175 | + inflater.sourcePos = 0 |
| 176 | + inflater.sourceLimit = inflater.source.size |
| 177 | + val exception = assertFailsWith<ProtocolException> { |
| 178 | + inflater.inflate() |
| 179 | + } |
| 180 | + assertEquals("Z_DATA_ERROR", exception.message) |
| 181 | + |
| 182 | + inflater.close() |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + fun cannotInflateAfterClose() { |
| 187 | + val inflater = Inflater() |
| 188 | + inflater.close() |
| 189 | + |
| 190 | + assertFailsWith<IllegalStateException> { |
| 191 | + inflater.inflate() |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + fun closeIsIdemptent() { |
| 197 | + val inflater = Inflater() |
| 198 | + inflater.close() |
| 199 | + inflater.close() |
| 200 | + } |
| 201 | +} |
0 commit comments