File size: 10,853 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.nio.channels.FileChannel;
public class ReadOnlyDirectByteBufferBufTest {
protected ByteBuf buffer(ByteBuffer buffer) {
return new ReadOnlyByteBufferBuf(UnpooledByteBufAllocator.DEFAULT, buffer);
}
protected ByteBuffer allocate(int size) {
return ByteBuffer.allocateDirect(size);
}
@Test
public void testIsContiguous() {
ByteBuf buf = buffer(allocate(4).asReadOnlyBuffer());
Assert.assertTrue(buf.isContiguous());
buf.release();
}
@Test(expected = IllegalArgumentException.class)
public void testConstructWithWritable() {
buffer(allocate(1));
}
@Test
public void shouldIndicateNotWritable() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
Assert.assertFalse(buf.isWritable());
} finally {
buf.release();
}
}
@Test
public void shouldIndicateNotWritableAnyNumber() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
Assert.assertFalse(buf.isWritable(1));
} finally {
buf.release();
}
}
@Test
public void ensureWritableIntStatusShouldFailButNotThrow() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
int result = buf.ensureWritable(1, false);
Assert.assertEquals(1, result);
} finally {
buf.release();
}
}
@Test
public void ensureWritableForceIntStatusShouldFailButNotThrow() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
int result = buf.ensureWritable(1, true);
Assert.assertEquals(1, result);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void ensureWritableShouldThrow() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
buf.ensureWritable(1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetByte() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setByte(0, 1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetInt() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setInt(0, 1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetShort() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setShort(0, 1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetMedium() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setMedium(0, 1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetLong() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setLong(0, 1);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaArray() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setBytes(0, "test".getBytes());
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaBuffer() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
ByteBuf copy = Unpooled.copyInt(1);
try {
buf.setBytes(0, copy);
} finally {
buf.release();
copy.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaStream() throws IOException {
ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
try {
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
} finally {
buf.release();
}
}
@Test
public void testGetReadByte() {
ByteBuf buf = buffer(
((ByteBuffer) allocate(2).put(new byte[] { (byte) 1, (byte) 2 }).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getByte(0));
Assert.assertEquals(2, buf.getByte(1));
Assert.assertEquals(1, buf.readByte());
Assert.assertEquals(2, buf.readByte());
Assert.assertFalse(buf.isReadable());
buf.release();
}
@Test
public void testGetReadInt() {
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getInt(0));
Assert.assertEquals(2, buf.getInt(4));
Assert.assertEquals(1, buf.readInt());
Assert.assertEquals(2, buf.readInt());
Assert.assertFalse(buf.isReadable());
buf.release();
}
@Test
public void testGetReadShort() {
ByteBuf buf = buffer(((ByteBuffer) allocate(8)
.putShort((short) 1).putShort((short) 2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getShort(0));
Assert.assertEquals(2, buf.getShort(2));
Assert.assertEquals(1, buf.readShort());
Assert.assertEquals(2, buf.readShort());
Assert.assertFalse(buf.isReadable());
buf.release();
}
@Test
public void testGetReadLong() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16)
.putLong(1).putLong(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getLong(0));
Assert.assertEquals(2, buf.getLong(8));
Assert.assertEquals(1, buf.readLong());
Assert.assertEquals(2, buf.readLong());
Assert.assertFalse(buf.isReadable());
buf.release();
}
@Test(expected = IndexOutOfBoundsException.class)
public void testGetBytesByteBuffer() {
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
// Ensure destination buffer is bigger then what is in the ByteBuf.
ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
ByteBuf buffer = buffer(((ByteBuffer) allocate(bytes.length)
.put(bytes).flip()).asReadOnlyBuffer());
try {
buffer.getBytes(buffer.readerIndex(), nioBuffer);
} finally {
buffer.release();
}
}
@Test
public void testCopy() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
ByteBuf copy = buf.copy();
Assert.assertEquals(buf, copy);
buf.release();
copy.release();
}
@Test
public void testCopyWithOffset() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
ByteBuf copy = buf.copy(1, 9);
Assert.assertEquals(buf.slice(1, 9), copy);
buf.release();
copy.release();
}
// Test for https://github.com/netty/netty/issues/1708
@Test
public void testWrapBufferWithNonZeroPosition() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16)
.putLong(1).flip().position(1)).asReadOnlyBuffer());
ByteBuf slice = buf.slice();
Assert.assertEquals(buf, slice);
buf.release();
}
@Test
public void testWrapBufferRoundTrip() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.readInt());
ByteBuffer nioBuffer = buf.nioBuffer();
// Ensure this can be accessed without throwing a BufferUnderflowException
Assert.assertEquals(2, nioBuffer.getInt());
buf.release();
}
@Test
public void testWrapMemoryMapped() throws Exception {
// BUG: CWE-378 Creation of Temporary File With Insecure Permissions
// File file = File.createTempFile("netty-test", "tmp");
// FIXED:
File file = PlatformDependent.createTempFile("netty-test", "tmp", null);
FileChannel output = null;
FileChannel input = null;
ByteBuf b1 = null;
ByteBuf b2 = null;
try {
output = new RandomAccessFile(file, "rw").getChannel();
byte[] bytes = new byte[1024];
PlatformDependent.threadLocalRandom().nextBytes(bytes);
output.write(ByteBuffer.wrap(bytes));
input = new RandomAccessFile(file, "r").getChannel();
ByteBuffer m = input.map(FileChannel.MapMode.READ_ONLY, 0, input.size());
b1 = buffer(m);
ByteBuffer dup = m.duplicate();
dup.position(2);
dup.limit(4);
b2 = buffer(dup);
Assert.assertEquals(b2, b1.slice(2, 2));
} finally {
if (b1 != null) {
b1.release();
}
if (b2 != null) {
b2.release();
}
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
file.delete();
}
}
@Test
public void testMemoryAddress() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
Assert.assertFalse(buf.hasMemoryAddress());
try {
buf.memoryAddress();
Assert.fail();
} catch (UnsupportedOperationException expected) {
// expected
}
} finally {
buf.release();
}
}
}
|