| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import com.google.common.primitives.Ints; |
| 17 |
import org.testng.annotations.Test; |
| 18 |
import sun.nio.ch.DirectBuffer; |
| 19 |
|
| 20 |
import java.nio.ByteBuffer; |
| 21 |
import java.security.Permission; |
| 22 |
|
| 23 |
import static io.airlift.slice.JvmUtils.unsafe; |
| 24 |
import static org.testng.Assert.assertEquals; |
| 25 |
import static org.testng.Assert.assertTrue; |
| 26 |
import static org.testng.Assert.fail; |
| 27 |
|
| 28 |
public class TestUnsafeSliceFactory |
| 29 |
{ |
| 30 |
@Test |
| 31 |
public void testRawAddress() |
| 32 |
throws Exception |
| 33 |
{ |
| 34 |
UnsafeSliceFactory factory = UnsafeSliceFactory.getInstance(); |
| 35 |
|
| 36 |
int size = 100; |
| 37 |
long address = unsafe.allocateMemory(size); |
| 38 |
try { |
| 39 |
Slice slice = factory.newSlice(address, size); |
| 40 |
for (int i = 0; i < size; i += Ints.BYTES) { |
| 41 |
slice.setInt(i, i); |
| 42 |
} |
| 43 |
for (int i = 0; i < size; i += Ints.BYTES) { |
| 44 |
assertEquals(slice.getInt(i), i); |
| 45 |
} |
| 46 |
} |
| 47 |
finally { |
| 48 |
unsafe.freeMemory(address); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void testRawAddressWithReference() |
| 54 |
throws Exception |
| 55 |
{ |
| 56 |
ByteBuffer buffer = ByteBuffer.allocateDirect(100); |
| 57 |
assertTrue(buffer instanceof DirectBuffer); |
| 58 |
long address = ((DirectBuffer) buffer).address(); |
| 59 |
|
| 60 |
UnsafeSliceFactory factory = UnsafeSliceFactory.getInstance(); |
| 61 |
|
| 62 |
Slice slice = factory.newSlice(address, buffer.capacity(), buffer); |
| 63 |
|
| 64 |
slice.setInt(32, 0xDEADBEEF); |
| 65 |
assertEquals(slice.getInt(32), 0xDEADBEEF); |
| 66 |
} |
| 67 |
|
| 68 |
@Test(expectedExceptions = SecurityException.class) |
| 69 |
public void testSecurity() |
| 70 |
throws Exception |
| 71 |
{ |
| 72 |
SecurityManager saved = System.getSecurityManager(); |
| 73 |
System.setSecurityManager(new SecurityManager() |
| 74 |
{ |
| 75 |
@Override |
| 76 |
public void checkPermission(Permission perm) |
| 77 |
{ |
| 78 |
if (perm.getName().equals("suppressAccessChecks")) { |
| 79 |
throw new SecurityException(); |
| 80 |
} |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
try { |
| 85 |
UnsafeSliceFactory.getInstance(); |
| 86 |
fail("expected SecurityException"); |
| 87 |
} |
| 88 |
finally { |
| 89 |
System.setSecurityManager(saved); |
| 90 |
} |
| 91 |
} |
| 92 |
} |