Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>

Expand Down
774 changes: 391 additions & 383 deletions compiler/src/test/java/org/capnproto/test/EncodingTest.java

Large diffs are not rendered by default.

376 changes: 191 additions & 185 deletions compiler/src/test/java/org/capnproto/test/TestUtil.java

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
12 changes: 7 additions & 5 deletions runtime/src/test/java/org/capnproto/ArrayInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

package org.capnproto;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ArrayInputStreamTest {
@Test
public void testEmptyArray() throws java.io.IOException {
Expand All @@ -34,15 +36,15 @@ public void testEmptyArray() throws java.io.IOException {

// read() should return -1 at the end of the stream
// https://docs.oracle.com/javase/7/docs/api/java/nio/channels/ReadableByteChannel.html
Assert.assertEquals(stream.read(dst), -1);
assertEquals(-1, stream.read(dst));
}

@Test
public void testRequestMoreBytesThanArePresent() throws java.io.IOException {
byte[] oneByte = new byte[]{42};
ArrayInputStream stream = new ArrayInputStream(ByteBuffer.wrap(oneByte));
ByteBuffer dst = ByteBuffer.allocate(10);
Assert.assertEquals(stream.read(dst), 1);
Assert.assertEquals(stream.read(dst), -1); // end of stream
assertEquals(1, stream.read(dst));
assertEquals(-1, stream.read(dst)); // end of stream
}
}
15 changes: 8 additions & 7 deletions runtime/src/test/java/org/capnproto/DefaultAllocatorTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package org.capnproto;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultAllocatorTest {
@Test
public void maxSegmentBytes() {
DefaultAllocator allocator = new DefaultAllocator();
Assert.assertEquals(allocator.allocationStrategy,
BuilderArena.AllocationStrategy.GROW_HEURISTICALLY);
assertEquals(BuilderArena.AllocationStrategy.GROW_HEURISTICALLY, allocator.allocationStrategy);
allocator.maxSegmentBytes = (1 << 25) - 1;

int allocationSize = 1 << 24;
allocator.setNextAllocationSizeBytes(allocationSize);

Assert.assertEquals(allocationSize,
assertEquals(allocationSize,
allocator.allocateSegment(allocationSize).capacity());

Assert.assertEquals(allocator.maxSegmentBytes,
assertEquals(allocator.maxSegmentBytes,
allocator.allocateSegment(allocationSize).capacity());

Assert.assertEquals(allocator.maxSegmentBytes,
assertEquals(allocator.maxSegmentBytes,
allocator.allocateSegment(allocationSize).capacity());
}
}
107 changes: 55 additions & 52 deletions runtime/src/test/java/org/capnproto/LayoutTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.capnproto;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class LayoutTest {

private static final int MAX_NESTING_LIMIT = 0x7fffffff;
Expand All @@ -30,46 +33,46 @@ public void testSimpleRawDataStruct() {

StructReader reader = WireHelpers.readStructPointer(new BareStructReader(), arena.tryGetSegment(0), 0, null, 0, MAX_NESTING_LIMIT);

Assert.assertEquals(reader._getLongField(0), 0xefcdab8967452301L);
Assert.assertEquals(reader._getLongField(1), 0L);

Assert.assertEquals(reader._getIntField(0), 0x67452301);
Assert.assertEquals(reader._getIntField(1), 0xefcdab89);
Assert.assertEquals(reader._getIntField(2), 0);

Assert.assertEquals(reader._getShortField(0), (short)0x2301);
Assert.assertEquals(reader._getShortField(1), (short)0x6745);
Assert.assertEquals(reader._getShortField(2), (short)0xab89);
Assert.assertEquals(reader._getShortField(3), (short)0xefcd);
Assert.assertEquals(reader._getShortField(4), (short)0);

Assert.assertEquals(reader._getBooleanField(0), true);
Assert.assertEquals(reader._getBooleanField(1), false);
Assert.assertEquals(reader._getBooleanField(2), false);

Assert.assertEquals(reader._getBooleanField(3), false);
Assert.assertEquals(reader._getBooleanField(4), false);
Assert.assertEquals(reader._getBooleanField(5), false);
Assert.assertEquals(reader._getBooleanField(6), false);
Assert.assertEquals(reader._getBooleanField(7), false);

Assert.assertEquals(reader._getBooleanField(8), true);
Assert.assertEquals(reader._getBooleanField(9), true);
Assert.assertEquals(reader._getBooleanField(10), false);
Assert.assertEquals(reader._getBooleanField(11), false);
Assert.assertEquals(reader._getBooleanField(12), false);
Assert.assertEquals(reader._getBooleanField(13), true);
Assert.assertEquals(reader._getBooleanField(14), false);
Assert.assertEquals(reader._getBooleanField(15), false);

Assert.assertEquals(reader._getBooleanField(63), true);
Assert.assertEquals(reader._getBooleanField(64), false);
assertEquals(0xefcdab8967452301L, reader._getLongField(0));
assertEquals(0L, reader._getLongField(1));

assertEquals(0x67452301, reader._getIntField(0));
assertEquals(0xefcdab89, reader._getIntField(1));
assertEquals(0, reader._getIntField(2));

assertEquals((short)0x2301, reader._getShortField(0));
assertEquals((short)0x6745, reader._getShortField(1));
assertEquals((short)0xab89, reader._getShortField(2));
assertEquals((short)0xefcd, reader._getShortField(3));
assertEquals((short)0, reader._getShortField(4));

assertEquals(true, reader._getBooleanField(0));
assertEquals(false, reader._getBooleanField(1));
assertEquals(false, reader._getBooleanField(2));

assertEquals(false, reader._getBooleanField(3));
assertEquals(false, reader._getBooleanField(4));
assertEquals(false, reader._getBooleanField(5));
assertEquals(false, reader._getBooleanField(6));
assertEquals(false, reader._getBooleanField(7));

assertEquals(true, reader._getBooleanField(8));
assertEquals(true, reader._getBooleanField(9));
assertEquals(false, reader._getBooleanField(10));
assertEquals(false, reader._getBooleanField(11));
assertEquals(false, reader._getBooleanField(12));
assertEquals(true, reader._getBooleanField(13));
assertEquals(false, reader._getBooleanField(14));
assertEquals(false, reader._getBooleanField(15));

assertEquals(true, reader._getBooleanField(63));
assertEquals(false, reader._getBooleanField(64));
}

/**
* @see <a href="https://github.com/capnproto/capnproto-java/issues/122">#122</a>
*/
@Test(expected = DecodeException.class)
@Test
public void readStructPointerShouldThrowDecodeExceptionOnOutOfBoundsStructPointer() {
byte[] brokenMSG = new byte[]{
0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, //declare word size of 7, with payload of only 6 words
Expand All @@ -86,7 +89,7 @@ public void readStructPointerShouldThrowDecodeExceptionOnOutOfBoundsStructPointe

ReaderArena arena = new ReaderArena(new ByteBuffer[]{ buffer }, 0x7fffffffffffffffL);

StructReader reader = WireHelpers.readStructPointer(new BareStructReader(), arena.tryGetSegment(0), 0, null, 0, MAX_NESTING_LIMIT);
assertThrows(DecodeException.class, () -> WireHelpers.readStructPointer(new BareStructReader(), arena.tryGetSegment(0), 0, null, 0, MAX_NESTING_LIMIT));
}


Expand All @@ -100,7 +103,7 @@ public ListReader constructReader(SegmentReader segment, int ptr, int elementCou
}
}

@Test(expected = DecodeException.class)
@Test
public void readListPointerShouldThrowDecodeExceptionOnOutOfBoundsCompositeListPointer() {
byte[] brokenMSG = {
// set list pointer bits to 1, elementSize to 7 to indicate composite list and number of words in the list (minus tag) to 0x1FFFFFFF (max value possible in 29b limit)
Expand All @@ -114,7 +117,7 @@ public void readListPointerShouldThrowDecodeExceptionOnOutOfBoundsCompositeListP

ReaderArena arena = new ReaderArena(new ByteBuffer[]{buffer}, 0x7fffffffffffffffL);

ListReader reader = WireHelpers.readListPointer(new BareListReader(), arena.tryGetSegment(0), 0, null, 0, (byte) 0, MAX_NESTING_LIMIT);
assertThrows(DecodeException.class, () -> WireHelpers.readListPointer(new BareListReader(), arena.tryGetSegment(0), 0, null, 0, (byte) 0, MAX_NESTING_LIMIT));
}

private class BareStructBuilder implements StructBuilder.Factory<StructBuilder> {
Expand Down Expand Up @@ -165,17 +168,17 @@ private void setUpStruct(StructBuilder builder) {
}

private void checkStruct(StructBuilder builder) {
Assert.assertEquals(0x1011121314151617L, builder._getLongField(0));
Assert.assertEquals(0x20212223, builder._getIntField(2));
Assert.assertEquals(0x3031, builder._getShortField(6));
Assert.assertEquals(0x40, builder._getByteField(14));
Assert.assertEquals(false, builder._getBooleanField(120));
Assert.assertEquals(false, builder._getBooleanField(121));
Assert.assertEquals(true, builder._getBooleanField(122));
Assert.assertEquals(false, builder._getBooleanField(123));
Assert.assertEquals(true, builder._getBooleanField(124));
Assert.assertEquals(true, builder._getBooleanField(125));
Assert.assertEquals(true, builder._getBooleanField(126));
Assert.assertEquals(false, builder._getBooleanField(127));
assertEquals(0x1011121314151617L, builder._getLongField(0));
assertEquals(0x20212223, builder._getIntField(2));
assertEquals(0x3031, builder._getShortField(6));
assertEquals(0x40, builder._getByteField(14));
assertEquals(false, builder._getBooleanField(120));
assertEquals(false, builder._getBooleanField(121));
assertEquals(true, builder._getBooleanField(122));
assertEquals(false, builder._getBooleanField(123));
assertEquals(true, builder._getBooleanField(124));
assertEquals(true, builder._getBooleanField(125));
assertEquals(true, builder._getBooleanField(126));
assertEquals(false, builder._getBooleanField(127));
}
}
9 changes: 6 additions & 3 deletions runtime/src/test/java/org/capnproto/ListBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package org.capnproto;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class ListBuilderTest {

@Test(expected = IndexOutOfBoundsException.class)
@Test
public void _setBooleanElementShouldNotOverflowDuringPositionOffsetCalculation() {
ByteBuffer buffer = ByteBuffer.allocate(10);
BuilderArena builderArena = new BuilderArena(new DefaultAllocator());
SegmentBuilder segmentBuilder = new SegmentBuilder(buffer, builderArena);
ListBuilder listBuilder = new ListBuilder(segmentBuilder, 0, 0, 2, 0, (short) 0);

listBuilder._setBooleanElement(Integer.MAX_VALUE, true);
assertThrows(IndexOutOfBoundsException.class, () -> listBuilder._setBooleanElement(Integer.MAX_VALUE, true));
}
}
16 changes: 8 additions & 8 deletions runtime/src/test/java/org/capnproto/SegmentReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package org.capnproto;

import org.capnproto.WireHelpers.FollowFarsResult;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SegmentReaderTest {

@Test
public void in_boundsCalculationShouldNotOverflow() {
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
SegmentReader segmentReader = new SegmentReader(byteBuffer, null);
MatcherAssert.assertThat(segmentReader.isInBounds(0, Integer.MAX_VALUE), is(false));
assertEquals(false, segmentReader.isInBounds(0, Integer.MAX_VALUE));
}

@Test
public void oneWordAtLastWordShouldBeInBounds() {
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
SegmentReader segmentReader = new SegmentReader(byteBuffer, null);
MatcherAssert.assertThat(segmentReader.isInBounds(7, 1), is(true));
assertEquals(true, segmentReader.isInBounds(7, 1));
}

@Test
public void twoWordsAtLastWordShouldNotBeInBounds() {
ByteBuffer byteBuffer = ByteBuffer.allocate(64);
SegmentReader segmentReader = new SegmentReader(byteBuffer, null);
MatcherAssert.assertThat(segmentReader.isInBounds(7, 2), is(false));
assertEquals(false, segmentReader.isInBounds(7, 2));
}

@Test
Expand Down Expand Up @@ -78,15 +78,15 @@ public void validSegmentWithNegativeOffsetShouldBeInBounds() {
refTarget = WirePointer.target(refOffset, ref);
dataSizeWords = StructPointer.dataSize(ref);
wordSize = dataSizeWords + StructPointer.ptrCount(ref);
MatcherAssert.assertThat(segment.isInBounds(refTarget, wordSize), is(true));
assertEquals(true, segment.isInBounds(refTarget, wordSize));

/* Read inner Struct: ComObject. */
refOffset = refTarget + dataSizeWords; /* At the inner STRUCT POINTER */
ref = segment.get(refOffset);
refTarget = WirePointer.target(refOffset, ref);
dataSizeWords = StructPointer.dataSize(ref);
wordSize = dataSizeWords + StructPointer.ptrCount(ref);
MatcherAssert.assertThat(segment.isInBounds(refTarget, wordSize), is(true));
assertEquals(true, segment.isInBounds(refTarget, wordSize));
}

}
Loading
Loading