aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Scull <ascull@google.com>2023-09-16 21:49:28 +0000
committerAndrew Scull <ascull@google.com>2023-09-17 15:43:10 +0000
commit09ccb20563eec641376f3f7d1fbe88db721b504b (patch)
tree371f604e82323ba1cace9e0a5f4b0aba9bc26c34
parent2c94515719ae4a30709098c33a4acecf3908819b (diff)
downloadcbor-java-09ccb20563eec641376f3f7d1fbe88db721b504b.tar.gz
Removes reference to encoder implementation details
Use the CborEncoder interface rather than other interfaces from the encoder package. This will allow for changes to the encoder implementation.
-rw-r--r--src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java8
-rw-r--r--src/test/java/co/nstant/in/cbor/CborEncoderTest.java49
-rw-r--r--src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java29
-rw-r--r--src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java15
-rw-r--r--src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java79
5 files changed, 53 insertions, 127 deletions
diff --git a/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java b/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java
index 2b227b5..1a24c09 100644
--- a/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java
+++ b/src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java
@@ -6,9 +6,9 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
+import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.decoder.HalfPrecisionFloatDecoder;
-import co.nstant.in.cbor.encoder.HalfPrecisionFloatEncoder;
import co.nstant.in.cbor.model.ByteString;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.DoublePrecisionFloat;
@@ -87,7 +87,7 @@ public abstract class AbstractBuilder<T> {
private boolean isHalfPrecisionEnough(float value) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- HalfPrecisionFloatEncoder encoder = getHalfPrecisionFloatEncoder(outputStream);
+ CborEncoder encoder = new CborEncoder(outputStream);
encoder.encode(new HalfPrecisionFloat(value));
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
@@ -102,10 +102,6 @@ public abstract class AbstractBuilder<T> {
}
}
- protected HalfPrecisionFloatEncoder getHalfPrecisionFloatEncoder(OutputStream outputStream) {
- return new HalfPrecisionFloatEncoder(null, outputStream);
- }
-
protected HalfPrecisionFloatDecoder getHalfPrecisionFloatDecoder(InputStream inputStream) {
return new HalfPrecisionFloatDecoder(null, inputStream);
}
diff --git a/src/test/java/co/nstant/in/cbor/CborEncoderTest.java b/src/test/java/co/nstant/in/cbor/CborEncoderTest.java
index fb37cf8..4a2169f 100644
--- a/src/test/java/co/nstant/in/cbor/CborEncoderTest.java
+++ b/src/test/java/co/nstant/in/cbor/CborEncoderTest.java
@@ -1,6 +1,10 @@
package co.nstant.in.cbor;
+import static org.junit.Assert.assertEquals;
+
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
import org.junit.Before;
import org.junit.Test;
@@ -65,4 +69,49 @@ public class CborEncoderTest {
encoder.encode(new Mock(MajorType.SPECIAL));
}
+ @Test(expected = CborException.class)
+ public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException {
+ new CborEncoder(new OutputStream() {
+
+ private int counter = 0;
+
+ @Override
+ public void write(int b) throws IOException {
+ if (++counter == 3) {
+ throw new IOException();
+ }
+ }
+
+ }).encode(new CborBuilder().startString().add("string").end().build());
+ }
+
+ @Test(expected = CborException.class)
+ public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException {
+ new CborEncoder(new OutputStream() {
+
+ @Override
+ public void write(int b) throws IOException {
+ throw new IOException();
+ }
+
+ }).encode(new CborBuilder().startArray().add(1).end().build());
+ }
+
+ @Test
+ public void shallEncode32bit() throws CborException {
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ new CborEncoder(outputStream).encode(new CborBuilder().addTag(4294967296L).build());
+ byte[] bytes = outputStream.toByteArray();
+ assertEquals(9, bytes.length);
+ assertEquals((byte) 0xdB, bytes[0]);
+ assertEquals((byte) 0x00, bytes[1]);
+ assertEquals((byte) 0x00, bytes[2]);
+ assertEquals((byte) 0x00, bytes[3]);
+ assertEquals((byte) 0x01, bytes[4]);
+ assertEquals((byte) 0x00, bytes[5]);
+ assertEquals((byte) 0x00, bytes[6]);
+ assertEquals((byte) 0x00, bytes[7]);
+ assertEquals((byte) 0x00, bytes[8]);
+ }
+
}
diff --git a/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java b/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java
index 01a4194..1ef20ec 100644
--- a/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java
+++ b/src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java
@@ -7,7 +7,6 @@ import java.io.OutputStream;
import org.junit.Test;
import co.nstant.in.cbor.CborException;
-import co.nstant.in.cbor.encoder.HalfPrecisionFloatEncoder;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.HalfPrecisionFloat;
import co.nstant.in.cbor.model.SinglePrecisionFloat;
@@ -16,8 +15,6 @@ public class AbstractBuilderTest {
private class TestBuilder extends AbstractBuilder<Object> {
- private boolean encoderThrowsException = false;
-
public TestBuilder() {
super(null);
}
@@ -30,24 +27,6 @@ public class AbstractBuilderTest {
addChunk(null);
}
- public void setEncoderThrowsException() {
- encoderThrowsException = true;
- }
-
- @Override
- protected HalfPrecisionFloatEncoder getHalfPrecisionFloatEncoder(OutputStream outputStream) {
- if (encoderThrowsException) {
- return new HalfPrecisionFloatEncoder(null, outputStream) {
- @Override
- public void encode(HalfPrecisionFloat dataItem) throws CborException {
- throw new CborException("test");
- }
- };
- } else {
- return super.getHalfPrecisionFloatEncoder(outputStream);
- }
- }
-
}
@Test
@@ -64,12 +43,4 @@ public class AbstractBuilderTest {
new TestBuilder().testAddChunk();
}
- @Test
- public void IsHalfPrecisionEnoughShallReturnFalseOnException() {
- TestBuilder builder = new TestBuilder();
- builder.setEncoderThrowsException();
- assertTrue(builder.testConvert(0.0f) instanceof SinglePrecisionFloat);
- assertTrue(0.0f == ((SinglePrecisionFloat) builder.testConvert(0.0f)).getValue());
- }
-
}
diff --git a/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java b/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java
index a4eaa1d..cb004f4 100644
--- a/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java
+++ b/src/test/java/co/nstant/in/cbor/decoder/CborDecoderTest.java
@@ -19,7 +19,6 @@ import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
-import co.nstant.in.cbor.encoder.AbstractEncoder;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.MajorType;
import co.nstant.in.cbor.model.RationalNumber;
@@ -165,15 +164,7 @@ public class CborDecoderTest {
@Test
public void shouldThrowOnItemWithForgedLength() throws CborException {
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- AbstractEncoder<Long> maliciousEncoder = new AbstractEncoder<Long>(null, buffer) {
- @Override
- public void encode(Long length) throws CborException {
- encodeTypeAndLength(MajorType.UNICODE_STRING, length.longValue());
- }
- };
- maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE + 1L));
- byte[] maliciousString = buffer.toByteArray();
+ byte[] maliciousString = new byte[] { 0x7a, (byte) 0x80, 0x00, 0x00, 0x00 };
try {
CborDecoder.decode(maliciousString);
fail("Should have failed the huge allocation");
@@ -181,9 +172,7 @@ public class CborDecoderTest {
assertThat("Exception message", e.getMessage(), containsString("limited to INTMAX"));
}
- buffer.reset();
- maliciousEncoder.encode(Long.valueOf(Integer.MAX_VALUE - 1));
- maliciousString = buffer.toByteArray();
+ maliciousString = new byte[] { 0x7a, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xfe };
try {
CborDecoder.decode(maliciousString);
fail("Should have failed the huge allocation");
diff --git a/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java b/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java
deleted file mode 100644
index 7d73883..0000000
--- a/src/test/java/co/nstant/in/cbor/encoder/AbstractEncoderTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package co.nstant.in.cbor.encoder;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-
-import org.junit.Test;
-
-import co.nstant.in.cbor.CborBuilder;
-import co.nstant.in.cbor.CborEncoder;
-import co.nstant.in.cbor.CborException;
-import co.nstant.in.cbor.model.MajorType;
-
-public class AbstractEncoderTest {
-
- private class TestEncoder extends AbstractEncoder<Object> {
-
- public TestEncoder(OutputStream outputStream) {
- super(null, outputStream);
- }
-
- @Override
- public void encode(Object dataItem) throws CborException {
- }
-
- public void doEncodeTypeAndLength(long length) throws CborException {
- encodeTypeAndLength(MajorType.ARRAY, length);
- }
-
- }
-
- @Test(expected = CborException.class)
- public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException {
- final int[] counter = new int[1];
- new CborEncoder(new OutputStream() {
-
- @Override
- public synchronized void write(int b) throws IOException {
- if (++counter[0] == 3) {
- throw new IOException();
- }
- }
-
- }).encode(new CborBuilder().startString().add("string").end().build());
- }
-
- @Test(expected = CborException.class)
- public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException {
- new CborEncoder(new OutputStream() {
-
- @Override
- public synchronized void write(int b) throws IOException {
- throw new IOException();
- }
-
- }).encode(new CborBuilder().startArray().add(1).end().build());
- }
-
- @Test
- public void shallEncode32bit() throws CborException {
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- TestEncoder encoder = new TestEncoder(outputStream);
- encoder.doEncodeTypeAndLength(4294967296L);
- byte[] bytes = outputStream.toByteArray();
- assertEquals(9, bytes.length);
- assertEquals((byte) 0x9B, bytes[0]);
- assertEquals((byte) 0x00, bytes[1]);
- assertEquals((byte) 0x00, bytes[2]);
- assertEquals((byte) 0x00, bytes[3]);
- assertEquals((byte) 0x01, bytes[4]);
- assertEquals((byte) 0x00, bytes[5]);
- assertEquals((byte) 0x00, bytes[6]);
- assertEquals((byte) 0x00, bytes[7]);
- assertEquals((byte) 0x00, bytes[8]);
- }
-
-}