├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── co │ └── nstant │ └── in │ └── cbor │ ├── CborBuilder.java │ ├── CborDecoder.java │ ├── CborEncoder.java │ ├── CborException.java │ ├── CborOutputStream.java │ ├── DataItemListener.java │ ├── builder │ ├── AbstractBuilder.java │ ├── ArrayBuilder.java │ ├── ByteStringBuilder.java │ ├── MapBuilder.java │ ├── MapEntryBuilder.java │ └── UnicodeStringBuilder.java │ ├── decoder │ ├── AbstractDecoder.java │ ├── ArrayDecoder.java │ ├── ByteStringDecoder.java │ ├── DoublePrecisionFloatDecoder.java │ ├── HalfPrecisionFloatDecoder.java │ ├── MapDecoder.java │ ├── NegativeIntegerDecoder.java │ ├── SinglePrecisionFloatDecoder.java │ ├── SpecialDecoder.java │ ├── TagDecoder.java │ ├── UnicodeStringDecoder.java │ └── UnsignedIntegerDecoder.java │ └── model │ ├── AbstractFloat.java │ ├── AdditionalInformation.java │ ├── Array.java │ ├── ByteString.java │ ├── ChunkableDataItem.java │ ├── DataItem.java │ ├── DoublePrecisionFloat.java │ ├── HalfPrecisionFloat.java │ ├── LanguageTaggedString.java │ ├── MajorType.java │ ├── Map.java │ ├── NegativeInteger.java │ ├── Number.java │ ├── RationalNumber.java │ ├── SimpleValue.java │ ├── SimpleValueType.java │ ├── SinglePrecisionFloat.java │ ├── Special.java │ ├── SpecialType.java │ ├── Tag.java │ ├── UnicodeString.java │ └── UnsignedInteger.java └── test └── java └── co └── nstant └── in └── cbor ├── CborBuilderTest.java ├── CborEncoderTest.java ├── CborExceptionTest.java ├── DatemItemListenerTest.java ├── EncoderDecoderTest.java ├── builder ├── AbstractBuilderTest.java ├── ArrayBuilderTest.java └── MapBuilderTest.java ├── decoder ├── AbstractDecoderTest.java ├── ArrayDecoderTest.java ├── ByteStringDecoderTest.java ├── CborDecoderTest.java ├── LanguageTaggedStringDecoderTest.java ├── MapDecoderTest.java ├── SimpleValueDecoderTest.java ├── SpecialDecoderTest.java ├── UnicodeStringDecoderTest.java └── UnsignedIntegerDecoderTest.java ├── encoder ├── ByteStringEncoderTest.java ├── MapEncoderTest.java ├── RationalNumberEncoderTest.java ├── SpecialEncoderTest.java └── UnicodeStringEncoderTest.java ├── examples ├── Example01Test.java ├── Example02Test.java ├── Example03Test.java ├── Example04Test.java ├── Example05Test.java ├── Example06Test.java ├── Example07Test.java ├── Example08Test.java ├── Example09Test.java ├── Example10Test.java ├── Example11Test.java ├── Example12Test.java ├── Example13Test.java ├── Example14Test.java ├── Example15Test.java ├── Example16Test.java ├── Example17Test.java ├── Example18Test.java ├── Example19Test.java ├── Example20Test.java ├── Example21Test.java ├── Example22Test.java ├── Example23Test.java ├── Example24Test.java ├── Example25Test.java ├── Example26Test.java ├── Example27Test.java ├── Example28Test.java ├── Example29Test.java ├── Example30Test.java ├── Example31Test.java ├── Example32Test.java ├── Example33Test.java ├── Example34Test.java ├── Example35Test.java ├── Example36Test.java ├── Example37Test.java ├── Example38Test.java ├── Example39Test.java ├── Example40Test.java ├── Example41Test.java ├── Example42Test.java ├── Example43Test.java ├── Example44Test.java ├── Example45Test.java ├── Example46Test.java ├── Example47Test.java ├── Example48Test.java ├── Example49Test.java ├── Example50Test.java ├── Example51Test.java ├── Example52Test.java ├── Example53Test.java ├── Example54Test.java ├── Example55Test.java ├── Example56Test.java ├── Example57Test.java ├── Example58Test.java ├── Example59Test.java ├── Example60Test.java ├── Example61Test.java ├── Example62Test.java ├── Example63Test.java ├── Example64Test.java ├── Example65Test.java ├── Example66Test.java ├── Example67Test.java ├── Example68Test.java ├── Example69Test.java ├── Example70Test.java ├── Example71Test.java ├── Example72Test.java ├── Example73Test.java ├── Example74Test.java ├── Example75Test.java ├── Example76Test.java ├── Example77Test.java ├── Example78Test.java ├── Example79Test.java ├── Example80Test.java ├── Example81Test.java ├── Example82Test.java └── Example83Test.java └── model ├── AbstractByteStringTest.java ├── AbstractDataItemTest.java ├── AbstractDoublePrecisionFloatTest.java ├── AbstractFloatTest.java ├── AbstractHalfPrecisionFloatTest.java ├── AbstractNumberTest.java ├── AbstractSinglePrecisionFloatTest.java ├── AbstractStringTest.java ├── AdditionalInformationTest.java ├── ArrayTest.java ├── ByteStringTest.java ├── ChunkableDataItemTest.java ├── DataItemTest.java ├── DoublePrecisionFloatTest.java ├── LanguageTaggedStringTest.java ├── MajorTypeTest.java ├── MapTest.java ├── NegativeIntegerTest.java ├── RationalNumberTest.java ├── SimpleValueTest.java ├── SpecialTest.java ├── SpecialTypeTest.java ├── TagTest.java ├── UnicodeStringTest.java └── UnsignedIntegerTest.java /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | time: "04:00" 8 | open-pull-requests-limit: 10 9 | 10 | - package-ecosystem: github-actions 11 | directory: "/" 12 | schedule: 13 | interval: "daily" -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI Build 2 | 3 | on: [ push ] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | java: [ 8, 11, 17 ] 11 | fail-fast: false 12 | name: JDK ${{ matrix.java }} 13 | steps: 14 | - uses: actions/checkout@v4 15 | - uses: actions/setup-java@v4 16 | with: 17 | distribution: temurin 18 | java-version: ${{ matrix.java }} 19 | cache: maven 20 | - name: Run Integration Tests 21 | run: mvn test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.jar 3 | /target 4 | .classpath 5 | .project 6 | .settings 7 | .idea 8 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/CborBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import java.math.BigInteger; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | 7 | import co.nstant.in.cbor.builder.AbstractBuilder; 8 | import co.nstant.in.cbor.builder.ArrayBuilder; 9 | import co.nstant.in.cbor.builder.ByteStringBuilder; 10 | import co.nstant.in.cbor.builder.MapBuilder; 11 | import co.nstant.in.cbor.builder.UnicodeStringBuilder; 12 | import co.nstant.in.cbor.model.Array; 13 | import co.nstant.in.cbor.model.ByteString; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.Map; 16 | import co.nstant.in.cbor.model.UnicodeString; 17 | 18 | public class CborBuilder extends AbstractBuilder { 19 | 20 | private final LinkedList dataItems = new LinkedList<>(); 21 | 22 | public CborBuilder() { 23 | super(null); 24 | } 25 | 26 | public CborBuilder reset() { 27 | dataItems.clear(); 28 | return this; 29 | } 30 | 31 | public List build() { 32 | return dataItems; 33 | } 34 | 35 | public CborBuilder add(DataItem dataItem) { 36 | dataItems.add(dataItem); 37 | return this; 38 | } 39 | 40 | public CborBuilder add(long value) { 41 | add(convert(value)); 42 | return this; 43 | } 44 | 45 | public CborBuilder add(BigInteger value) { 46 | add(convert(value)); 47 | return this; 48 | } 49 | 50 | public CborBuilder add(boolean value) { 51 | add(convert(value)); 52 | return this; 53 | } 54 | 55 | public CborBuilder add(float value) { 56 | add(convert(value)); 57 | return this; 58 | } 59 | 60 | public CborBuilder add(double value) { 61 | add(convert(value)); 62 | return this; 63 | } 64 | 65 | public CborBuilder add(byte[] bytes) { 66 | add(convert(bytes)); 67 | return this; 68 | } 69 | 70 | public ByteStringBuilder startByteString() { 71 | return startByteString(null); 72 | } 73 | 74 | public ByteStringBuilder startByteString(byte[] bytes) { 75 | add(new ByteString(bytes).setChunked(true)); 76 | return new ByteStringBuilder(this); 77 | } 78 | 79 | public CborBuilder add(String string) { 80 | add(convert(string)); 81 | return this; 82 | } 83 | 84 | public UnicodeStringBuilder startString() { 85 | return startString(null); 86 | } 87 | 88 | public UnicodeStringBuilder startString(String string) { 89 | add(new UnicodeString(string).setChunked(true)); 90 | return new UnicodeStringBuilder(this); 91 | } 92 | 93 | public CborBuilder addTag(long value) { 94 | add(tag(value)); 95 | return this; 96 | } 97 | 98 | public CborBuilder tagged(long value) { 99 | DataItem item = dataItems.peekLast(); 100 | if (item == null) { 101 | throw new IllegalStateException("Can't add a tag before adding an item"); 102 | } 103 | item.getOuterTaggable().setTag(value); 104 | return this; 105 | } 106 | 107 | public ArrayBuilder startArray() { 108 | Array array = new Array(); 109 | array.setChunked(true); 110 | add(array); 111 | return new ArrayBuilder(this, array); 112 | } 113 | 114 | public ArrayBuilder addArray() { 115 | Array array = new Array(); 116 | add(array); 117 | return new ArrayBuilder(this, array); 118 | } 119 | 120 | public MapBuilder addMap() { 121 | Map map = new Map(); 122 | add(map); 123 | return new MapBuilder(this, map); 124 | } 125 | 126 | public MapBuilder startMap() { 127 | Map map = new Map(); 128 | map.setChunked(true); 129 | add(map); 130 | return new MapBuilder(this, map); 131 | } 132 | 133 | @Override 134 | protected void addChunk(DataItem dataItem) { 135 | add(dataItem); 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/CborEncoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.OutputStream; 6 | import java.util.List; 7 | import java.util.Objects; 8 | 9 | import co.nstant.in.cbor.model.Array; 10 | import co.nstant.in.cbor.model.ByteString; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.Map; 13 | import co.nstant.in.cbor.model.NegativeInteger; 14 | import co.nstant.in.cbor.model.SimpleValue; 15 | import co.nstant.in.cbor.model.Special; 16 | import co.nstant.in.cbor.model.Tag; 17 | import co.nstant.in.cbor.model.UnicodeString; 18 | import co.nstant.in.cbor.model.UnsignedInteger; 19 | 20 | /** 21 | * Encoder for the CBOR format based. 22 | */ 23 | public class CborEncoder { 24 | 25 | private CborOutputStream cborOutputStream; 26 | 27 | /** 28 | * Initialize a new encoder which writes the binary encoded data to an 29 | * {@link OutputStream}. 30 | * 31 | * @param outputStream the {@link OutputStream} to write the encoded data to 32 | */ 33 | public CborEncoder(OutputStream outputStream) { 34 | Objects.requireNonNull(outputStream); 35 | cborOutputStream = new CborOutputStream(outputStream); 36 | } 37 | 38 | /** 39 | * Encode a list of {@link DataItem}s, also known as a stream. 40 | * 41 | * @param dataItems a list of {@link DataItem}s 42 | * @throws CborException if the {@link DataItem}s could not be encoded or there 43 | * was an problem with the {@link OutputStream}. 44 | */ 45 | public void encode(List dataItems) throws CborException { 46 | for (DataItem dataItem : dataItems) { 47 | encode(dataItem); 48 | } 49 | } 50 | 51 | /** 52 | * Encode a single {@link DataItem}. 53 | * 54 | * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a 55 | * {@link SimpleValue} NULL value. 56 | * @throws CborException if {@link DataItem} could not be encoded or there was 57 | * an problem with the {@link OutputStream}. 58 | */ 59 | public void encode(DataItem dataItem) throws CborException { 60 | try { 61 | cborOutputStream.writeDataItem(dataItem); 62 | } catch (IOException ioException) { 63 | throw new CborException(ioException); 64 | } 65 | } 66 | 67 | public boolean isCanonical() { 68 | return cborOutputStream.isCanonical(); 69 | } 70 | 71 | public CborEncoder nonCanonical() { 72 | cborOutputStream.setCanonical(false); 73 | return this; 74 | } 75 | 76 | /** 77 | * Encode a list of {@link DataItem}s, also known as a stream, to a byte array. 78 | * 79 | * @param dataItems a list of {@link DataItem}s 80 | */ 81 | public static byte[] encodeToBytes(List dataItems) { 82 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 83 | CborOutputStream cborOutputStream = new CborOutputStream(byteArrayOutputStream); 84 | try { 85 | for (DataItem dataItem : dataItems) { 86 | cborOutputStream.writeDataItem(dataItem); 87 | } 88 | } catch (IOException ioException) { 89 | // A ByteArrayOutputStream does not actually throw an IOException. 90 | throw new AssertionError(ioException); 91 | } 92 | return byteArrayOutputStream.toByteArray(); 93 | } 94 | 95 | /** 96 | * Encode a single {@link DataItem} to a byte array. 97 | * 98 | * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a 99 | * {@link SimpleValue} NULL value. 100 | */ 101 | public static byte[] encodeToBytes(DataItem dataItem) { 102 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 103 | CborOutputStream cborOutputStream = new CborOutputStream(byteArrayOutputStream); 104 | try { 105 | cborOutputStream.writeDataItem(dataItem); 106 | } catch (IOException ioException) { 107 | // A ByteArrayOutputStream does not actually throw an IOException. 108 | throw new AssertionError(ioException); 109 | } 110 | return byteArrayOutputStream.toByteArray(); 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/CborException.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | public class CborException extends Exception { 4 | 5 | private static final long serialVersionUID = 8839905301881841410L; 6 | 7 | public CborException(String message) { 8 | super(message); 9 | } 10 | 11 | public CborException(Throwable cause) { 12 | super(cause); 13 | } 14 | 15 | public CborException(String message, Throwable cause) { 16 | super(message, cause); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/DataItemListener.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import co.nstant.in.cbor.model.DataItem; 4 | 5 | /** 6 | * Callback interface for a streaming {@link CborDecoder}. 7 | */ 8 | public interface DataItemListener { 9 | 10 | /** 11 | * Gets called on every decoded {@link DataItem}. 12 | * 13 | * @param dataItem the {@link DataItem} 14 | */ 15 | void onDataItem(DataItem dataItem); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/builder/AbstractBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.math.BigInteger; 7 | 8 | import co.nstant.in.cbor.CborEncoder; 9 | import co.nstant.in.cbor.CborException; 10 | import co.nstant.in.cbor.decoder.HalfPrecisionFloatDecoder; 11 | import co.nstant.in.cbor.model.ByteString; 12 | import co.nstant.in.cbor.model.DataItem; 13 | import co.nstant.in.cbor.model.DoublePrecisionFloat; 14 | import co.nstant.in.cbor.model.HalfPrecisionFloat; 15 | import co.nstant.in.cbor.model.NegativeInteger; 16 | import co.nstant.in.cbor.model.SimpleValue; 17 | import co.nstant.in.cbor.model.SinglePrecisionFloat; 18 | import co.nstant.in.cbor.model.Tag; 19 | import co.nstant.in.cbor.model.UnicodeString; 20 | import co.nstant.in.cbor.model.UnsignedInteger; 21 | 22 | public abstract class AbstractBuilder { 23 | 24 | private final T parent; 25 | 26 | public AbstractBuilder(T parent) { 27 | this.parent = parent; 28 | } 29 | 30 | protected T getParent() { 31 | return parent; 32 | } 33 | 34 | protected void addChunk(DataItem dataItem) { 35 | throw new IllegalStateException(); 36 | } 37 | 38 | protected DataItem convert(long value) { 39 | if (value >= 0) { 40 | return new UnsignedInteger(value); 41 | } else { 42 | return new NegativeInteger(value); 43 | } 44 | } 45 | 46 | protected DataItem convert(BigInteger value) { 47 | if (value.signum() == -1) { 48 | return new NegativeInteger(value); 49 | } else { 50 | return new UnsignedInteger(value); 51 | } 52 | } 53 | 54 | protected DataItem convert(boolean value) { 55 | if (value) { 56 | return SimpleValue.TRUE; 57 | } else { 58 | return SimpleValue.FALSE; 59 | } 60 | } 61 | 62 | protected DataItem convert(byte[] bytes) { 63 | return new ByteString(bytes); 64 | } 65 | 66 | protected DataItem convert(String string) { 67 | return new UnicodeString(string); 68 | } 69 | 70 | protected DataItem convert(float value) { 71 | if (isHalfPrecisionEnough(value)) { 72 | return new HalfPrecisionFloat(value); 73 | } else { 74 | return new SinglePrecisionFloat(value); 75 | } 76 | } 77 | 78 | protected DataItem convert(double value) { 79 | return new DoublePrecisionFloat(value); 80 | } 81 | 82 | protected Tag tag(long value) { 83 | return new Tag(value); 84 | } 85 | 86 | private boolean isHalfPrecisionEnough(float value) { 87 | try { 88 | byte[] bytes = new HalfPrecisionFloat(value).encodeToBytes(); 89 | ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 90 | HalfPrecisionFloatDecoder decoder = getHalfPrecisionFloatDecoder(inputStream); 91 | if (inputStream.read() == -1) { // to skip type byte 92 | throw new CborException("unexpected end of stream"); 93 | } 94 | HalfPrecisionFloat halfPrecisionFloat = decoder.decode(0); 95 | return value == halfPrecisionFloat.getValue(); 96 | } catch (CborException cborException) { 97 | return false; 98 | } 99 | } 100 | 101 | protected HalfPrecisionFloatDecoder getHalfPrecisionFloatDecoder(InputStream inputStream) { 102 | return new HalfPrecisionFloatDecoder(null, inputStream); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/builder/ArrayBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import co.nstant.in.cbor.model.Array; 4 | import co.nstant.in.cbor.model.DataItem; 5 | import co.nstant.in.cbor.model.Map; 6 | import co.nstant.in.cbor.model.SimpleValue; 7 | 8 | public class ArrayBuilder> extends AbstractBuilder { 9 | 10 | private final Array array; 11 | 12 | public ArrayBuilder(T parent, Array array) { 13 | super(parent); 14 | this.array = array; 15 | } 16 | 17 | public ArrayBuilder add(DataItem dataItem) { 18 | array.add(dataItem); 19 | return this; 20 | } 21 | 22 | public ArrayBuilder add(long value) { 23 | add(convert(value)); 24 | return this; 25 | } 26 | 27 | public ArrayBuilder add(boolean value) { 28 | add(convert(value)); 29 | return this; 30 | } 31 | 32 | public ArrayBuilder add(float value) { 33 | add(convert(value)); 34 | return this; 35 | } 36 | 37 | public ArrayBuilder add(double value) { 38 | add(convert(value)); 39 | return this; 40 | } 41 | 42 | public ArrayBuilder add(byte[] bytes) { 43 | add(convert(bytes)); 44 | return this; 45 | } 46 | 47 | public ArrayBuilder add(String string) { 48 | add(convert(string)); 49 | return this; 50 | } 51 | 52 | public ArrayBuilder tagged(long tag) { 53 | DataItem item = array.peekLast(); 54 | if (item == null) { 55 | throw new IllegalStateException("Can't add a tag before adding an item"); 56 | } 57 | item.getOuterTaggable().setTag(tag); 58 | return this; 59 | } 60 | 61 | public ArrayBuilder> addArray() { 62 | Array nestedArray = new Array(); 63 | add(nestedArray); 64 | return new ArrayBuilder>(this, nestedArray); 65 | } 66 | 67 | public ArrayBuilder> startArray() { 68 | Array nestedArray = new Array(); 69 | nestedArray.setChunked(true); 70 | add(nestedArray); 71 | return new ArrayBuilder>(this, nestedArray); 72 | } 73 | 74 | public MapBuilder> addMap() { 75 | Map nestedMap = new Map(); 76 | add(nestedMap); 77 | return new MapBuilder>(this, nestedMap); 78 | } 79 | 80 | public MapBuilder> startMap() { 81 | Map nestedMap = new Map(); 82 | nestedMap.setChunked(true); 83 | add(nestedMap); 84 | return new MapBuilder>(this, nestedMap); 85 | } 86 | 87 | public T end() { 88 | if (array.isChunked()) { 89 | add(SimpleValue.BREAK); 90 | } 91 | return getParent(); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/builder/ByteStringBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import co.nstant.in.cbor.model.SimpleValue; 4 | 5 | public class ByteStringBuilder> extends AbstractBuilder { 6 | 7 | public ByteStringBuilder(T parent) { 8 | super(parent); 9 | } 10 | 11 | public ByteStringBuilder add(byte[] bytes) { 12 | getParent().addChunk(convert(bytes)); 13 | return this; 14 | } 15 | 16 | public T end() { 17 | getParent().addChunk(SimpleValue.BREAK); 18 | return getParent(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/builder/MapEntryBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import co.nstant.in.cbor.model.DataItem; 4 | 5 | public class MapEntryBuilder> extends AbstractBuilder { 6 | private final DataItem key; 7 | 8 | public MapEntryBuilder(T parent, DataItem key) { 9 | super(parent); 10 | this.key = key; 11 | } 12 | 13 | public T value(boolean value) { 14 | return put(key, convert(value)); 15 | } 16 | 17 | public T value(byte[] value) { 18 | return put(key, convert(value)); 19 | } 20 | 21 | public T value(double value) { 22 | return put(key, convert(value)); 23 | } 24 | 25 | public T value(String value) { 26 | return put(key, convert(value)); 27 | } 28 | 29 | private T put(DataItem key, DataItem value) { 30 | getParent().put(key, value); 31 | return getParent(); 32 | } 33 | 34 | public MapEntryBuilder tagged(long tag) { 35 | DataItem item = key.getOuterTaggable(); 36 | item.setTag(tag); 37 | return this; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/builder/UnicodeStringBuilder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import co.nstant.in.cbor.model.SimpleValue; 4 | 5 | public class UnicodeStringBuilder> extends AbstractBuilder { 6 | 7 | public UnicodeStringBuilder(T parent) { 8 | super(parent); 9 | } 10 | 11 | public UnicodeStringBuilder add(String string) { 12 | getParent().addChunk(convert(string)); 13 | return this; 14 | } 15 | 16 | public T end() { 17 | getParent().addChunk(SimpleValue.BREAK); 18 | return getParent(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/ArrayDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.Array; 8 | import co.nstant.in.cbor.model.DataItem; 9 | import co.nstant.in.cbor.model.Special; 10 | 11 | public class ArrayDecoder extends AbstractDecoder { 12 | 13 | public ArrayDecoder(CborDecoder decoder, InputStream inputStream) { 14 | super(decoder, inputStream); 15 | } 16 | 17 | @Override 18 | public Array decode(int initialByte) throws CborException { 19 | long length = getLength(initialByte); 20 | if (length == INFINITY) { 21 | return decodeInfinitiveLength(); 22 | } else { 23 | return decodeFixedLength(length); 24 | } 25 | } 26 | 27 | private Array decodeInfinitiveLength() throws CborException { 28 | Array array = new Array(); 29 | array.setChunked(true); 30 | if (decoder.isAutoDecodeInfinitiveArrays()) { 31 | DataItem dataItem; 32 | for (;;) { 33 | dataItem = decoder.decodeNext(); 34 | if (dataItem == null) { 35 | throw new CborException("Unexpected end of stream"); 36 | } 37 | if (Special.BREAK.equals(dataItem)) { 38 | array.add(Special.BREAK); 39 | break; 40 | } 41 | array.add(dataItem); 42 | } 43 | } 44 | return array; 45 | } 46 | 47 | private Array decodeFixedLength(long length) throws CborException { 48 | Array array = new Array(getPreallocationSize(length)); 49 | for (long i = 0; i < length; i++) { 50 | DataItem dataItem = decoder.decodeNext(); 51 | if (dataItem == null) { 52 | throw new CborException("Unexpected end of stream"); 53 | } 54 | array.add(dataItem); 55 | } 56 | return array; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/ByteStringDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.InputStream; 5 | 6 | import co.nstant.in.cbor.CborDecoder; 7 | import co.nstant.in.cbor.CborException; 8 | import co.nstant.in.cbor.model.ByteString; 9 | import co.nstant.in.cbor.model.DataItem; 10 | import co.nstant.in.cbor.model.MajorType; 11 | import co.nstant.in.cbor.model.Special; 12 | 13 | public class ByteStringDecoder extends AbstractDecoder { 14 | 15 | public ByteStringDecoder(CborDecoder decoder, InputStream inputStream) { 16 | super(decoder, inputStream); 17 | } 18 | 19 | @Override 20 | public ByteString decode(int initialByte) throws CborException { 21 | long length = getLength(initialByte); 22 | if (length == INFINITY) { 23 | if (decoder.isAutoDecodeInfinitiveByteStrings()) { 24 | return decodeInfinitiveLength(); 25 | } else { 26 | ByteString byteString = new ByteString(null); 27 | byteString.setChunked(true); 28 | return byteString; 29 | } 30 | } else { 31 | return decodeFixedLength(length); 32 | } 33 | } 34 | 35 | private ByteString decodeInfinitiveLength() throws CborException { 36 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 37 | for (;;) { 38 | DataItem dataItem = decoder.decodeNext(); 39 | if (dataItem == null) { 40 | throw new CborException("Unexpected end of stream"); 41 | } 42 | MajorType majorType = dataItem.getMajorType(); 43 | if (Special.BREAK.equals(dataItem)) { 44 | break; 45 | } else if (majorType == MajorType.BYTE_STRING) { 46 | ByteString byteString = (ByteString) dataItem; 47 | byte[] byteArray = byteString.getBytes(); 48 | if (byteArray != null) { 49 | bytes.write(byteArray, 0, byteArray.length); 50 | } 51 | } else { 52 | throw new CborException("Unexpected major type " + majorType); 53 | } 54 | } 55 | return new ByteString(bytes.toByteArray()); 56 | } 57 | 58 | private ByteString decodeFixedLength(long length) throws CborException { 59 | return new ByteString(decodeBytes(length)); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/DoublePrecisionFloatDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.DoublePrecisionFloat; 8 | 9 | public class DoublePrecisionFloatDecoder extends AbstractDecoder { 10 | 11 | public DoublePrecisionFloatDecoder(CborDecoder decoder, InputStream inputStream) { 12 | super(decoder, inputStream); 13 | } 14 | 15 | @Override 16 | public DoublePrecisionFloat decode(int initialByte) throws CborException { 17 | long bits = 0; 18 | byte[] symbols = nextSymbols(8); 19 | bits |= symbols[0] & 0xFF; 20 | bits <<= 8; 21 | bits |= symbols[1] & 0xFF; 22 | bits <<= 8; 23 | bits |= symbols[2] & 0xFF; 24 | bits <<= 8; 25 | bits |= symbols[3] & 0xFF; 26 | bits <<= 8; 27 | bits |= symbols[4] & 0xFF; 28 | bits <<= 8; 29 | bits |= symbols[5] & 0xFF; 30 | bits <<= 8; 31 | bits |= symbols[6] & 0xFF; 32 | bits <<= 8; 33 | bits |= symbols[7] & 0xFF; 34 | return new DoublePrecisionFloat(Double.longBitsToDouble(bits)); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/HalfPrecisionFloatDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.HalfPrecisionFloat; 8 | 9 | public class HalfPrecisionFloatDecoder extends AbstractDecoder { 10 | 11 | public HalfPrecisionFloatDecoder(CborDecoder decoder, InputStream inputStream) { 12 | super(decoder, inputStream); 13 | } 14 | 15 | @Override 16 | public HalfPrecisionFloat decode(int initialByte) throws CborException { 17 | byte[] symbols = nextSymbols(2); 18 | int bits = (symbols[0] & 0xFF) << 8 | (symbols[1] & 0xFF); 19 | return new HalfPrecisionFloat(toFloat(bits)); 20 | } 21 | 22 | /** 23 | * @see http://stackoverflow.com/a/5684578 24 | */ 25 | private static float toFloat(int bits) { 26 | int s = (bits & 0x8000) >> 15; 27 | int e = (bits & 0x7C00) >> 10; 28 | int f = bits & 0x03FF; 29 | 30 | if (e == 0) { 31 | return (float) ((s != 0 ? -1 : 1) * Math.pow(2, -14) * (f / Math.pow(2, 10))); 32 | } else if (e == 0x1F) { 33 | return f != 0 ? Float.NaN : (s != 0 ? -1 : 1) * Float.POSITIVE_INFINITY; 34 | } 35 | 36 | return (float) ((s != 0 ? -1 : 1) * Math.pow(2, e - 15) * (1 + f / Math.pow(2, 10))); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/MapDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.DataItem; 8 | import co.nstant.in.cbor.model.Map; 9 | import co.nstant.in.cbor.model.Special; 10 | 11 | public class MapDecoder extends AbstractDecoder { 12 | 13 | public MapDecoder(CborDecoder decoder, InputStream inputStream) { 14 | super(decoder, inputStream); 15 | } 16 | 17 | @Override 18 | public Map decode(int initialByte) throws CborException { 19 | long length = getLength(initialByte); 20 | if (length == INFINITY) { 21 | return decodeInfinitiveLength(); 22 | } else { 23 | return decodeFixedLength(length); 24 | } 25 | } 26 | 27 | private Map decodeInfinitiveLength() throws CborException { 28 | Map map = new Map(); 29 | map.setChunked(true); 30 | if (decoder.isAutoDecodeInfinitiveMaps()) { 31 | for (;;) { 32 | DataItem key = decoder.decodeNext(); 33 | if (Special.BREAK.equals(key)) { 34 | break; 35 | } 36 | DataItem value = decoder.decodeNext(); 37 | if (key == null || value == null) { 38 | throw new CborException("Unexpected end of stream"); 39 | } 40 | if (decoder.isRejectDuplicateKeys() && map.get(key) != null) { 41 | throw new CborException("Duplicate key found in map"); 42 | } 43 | map.put(key, value); 44 | } 45 | } 46 | return map; 47 | } 48 | 49 | private Map decodeFixedLength(long length) throws CborException { 50 | Map map = new Map(getPreallocationSize(length)); 51 | for (long i = 0; i < length; i++) { 52 | DataItem key = decoder.decodeNext(); 53 | DataItem value = decoder.decodeNext(); 54 | if (key == null || value == null) { 55 | throw new CborException("Unexpected end of stream"); 56 | } 57 | if (decoder.isRejectDuplicateKeys() && map.get(key) != null) { 58 | throw new CborException("Duplicate key found in map"); 59 | } 60 | map.put(key, value); 61 | } 62 | return map; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/NegativeIntegerDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | import java.math.BigInteger; 5 | 6 | import co.nstant.in.cbor.CborDecoder; 7 | import co.nstant.in.cbor.CborException; 8 | import co.nstant.in.cbor.model.NegativeInteger; 9 | 10 | public class NegativeIntegerDecoder extends AbstractDecoder { 11 | 12 | private static final BigInteger MINUS_ONE = BigInteger.valueOf(-1); 13 | 14 | public NegativeIntegerDecoder(CborDecoder decoder, InputStream inputStream) { 15 | super(decoder, inputStream); 16 | } 17 | 18 | @Override 19 | public NegativeInteger decode(int initialByte) throws CborException { 20 | return new NegativeInteger(MINUS_ONE.subtract(getLengthAsBigInteger(initialByte))); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/SinglePrecisionFloatDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.SinglePrecisionFloat; 8 | 9 | public class SinglePrecisionFloatDecoder extends AbstractDecoder { 10 | 11 | public SinglePrecisionFloatDecoder(CborDecoder decoder, InputStream inputStream) { 12 | super(decoder, inputStream); 13 | } 14 | 15 | @Override 16 | public SinglePrecisionFloat decode(int initialByte) throws CborException { 17 | int bits = 0; 18 | byte[] symbols = nextSymbols(4); 19 | bits |= symbols[0] & 0xFF; 20 | bits <<= 8; 21 | bits |= symbols[1] & 0xFF; 22 | bits <<= 8; 23 | bits |= symbols[2] & 0xFF; 24 | bits <<= 8; 25 | bits |= symbols[3] & 0xFF; 26 | return new SinglePrecisionFloat(Float.intBitsToFloat(bits)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/SpecialDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.SimpleValue; 8 | import co.nstant.in.cbor.model.SimpleValueType; 9 | import co.nstant.in.cbor.model.Special; 10 | import co.nstant.in.cbor.model.SpecialType; 11 | 12 | public class SpecialDecoder extends AbstractDecoder { 13 | 14 | private final HalfPrecisionFloatDecoder halfPrecisionFloatDecoder; 15 | private final SinglePrecisionFloatDecoder singlePrecisionFloatDecoder; 16 | private final DoublePrecisionFloatDecoder doublePrecisionFloatDecoder; 17 | 18 | public SpecialDecoder(CborDecoder decoder, InputStream inputStream) { 19 | super(decoder, inputStream); 20 | this.halfPrecisionFloatDecoder = new HalfPrecisionFloatDecoder(decoder, inputStream); 21 | this.singlePrecisionFloatDecoder = new SinglePrecisionFloatDecoder(decoder, inputStream); 22 | this.doublePrecisionFloatDecoder = new DoublePrecisionFloatDecoder(decoder, inputStream); 23 | } 24 | 25 | @Override 26 | public Special decode(int initialByte) throws CborException { 27 | switch (SpecialType.ofByte(initialByte)) { 28 | case BREAK: 29 | return Special.BREAK; 30 | case SIMPLE_VALUE: 31 | switch (SimpleValueType.ofByte(initialByte)) { 32 | case FALSE: 33 | return SimpleValue.FALSE; 34 | case TRUE: 35 | return SimpleValue.TRUE; 36 | case NULL: 37 | return SimpleValue.NULL; 38 | case UNDEFINED: 39 | return SimpleValue.UNDEFINED; 40 | case UNALLOCATED: 41 | return new SimpleValue(initialByte & 31); 42 | case RESERVED: 43 | default: 44 | throw new CborException("Not implemented"); 45 | } 46 | case IEEE_754_HALF_PRECISION_FLOAT: 47 | return halfPrecisionFloatDecoder.decode(initialByte); 48 | case IEEE_754_SINGLE_PRECISION_FLOAT: 49 | return singlePrecisionFloatDecoder.decode(initialByte); 50 | case IEEE_754_DOUBLE_PRECISION_FLOAT: 51 | return doublePrecisionFloatDecoder.decode(initialByte); 52 | case SIMPLE_VALUE_NEXT_BYTE: 53 | return new SimpleValue(nextSymbol()); 54 | default: 55 | throw new CborException("Not implemented"); 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/TagDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.Tag; 8 | 9 | public class TagDecoder extends AbstractDecoder { 10 | 11 | public TagDecoder(CborDecoder decoder, InputStream inputStream) { 12 | super(decoder, inputStream); 13 | } 14 | 15 | @Override 16 | public Tag decode(int initialByte) throws CborException { 17 | return new Tag(getLength(initialByte)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/UnicodeStringDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.InputStream; 5 | import java.nio.charset.StandardCharsets; 6 | 7 | import co.nstant.in.cbor.CborDecoder; 8 | import co.nstant.in.cbor.CborException; 9 | import co.nstant.in.cbor.model.DataItem; 10 | import co.nstant.in.cbor.model.MajorType; 11 | import co.nstant.in.cbor.model.Special; 12 | import co.nstant.in.cbor.model.UnicodeString; 13 | 14 | public class UnicodeStringDecoder extends AbstractDecoder { 15 | 16 | public UnicodeStringDecoder(CborDecoder decoder, InputStream inputStream) { 17 | super(decoder, inputStream); 18 | } 19 | 20 | @Override 21 | public UnicodeString decode(int initialByte) throws CborException { 22 | long length = getLength(initialByte); 23 | if (length == INFINITY) { 24 | if (decoder.isAutoDecodeInfinitiveUnicodeStrings()) { 25 | return decodeInfinitiveLength(); 26 | } else { 27 | UnicodeString unicodeString = new UnicodeString(null); 28 | unicodeString.setChunked(true); 29 | return unicodeString; 30 | } 31 | } else { 32 | return decodeFixedLength(length); 33 | } 34 | } 35 | 36 | private UnicodeString decodeInfinitiveLength() throws CborException { 37 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 38 | for (;;) { 39 | DataItem dataItem = decoder.decodeNext(); 40 | if (dataItem == null) { 41 | throw new CborException("Unexpected end of stream"); 42 | } 43 | MajorType majorType = dataItem.getMajorType(); 44 | if (Special.BREAK.equals(dataItem)) { 45 | break; 46 | } else if (majorType == MajorType.UNICODE_STRING) { 47 | UnicodeString unicodeString = (UnicodeString) dataItem; 48 | byte[] byteArray = unicodeString.toString().getBytes(StandardCharsets.UTF_8); 49 | bytes.write(byteArray, 0, byteArray.length); 50 | } else { 51 | throw new CborException("Unexpected major type " + majorType); 52 | } 53 | } 54 | return new UnicodeString(new String(bytes.toByteArray(), StandardCharsets.UTF_8)); 55 | } 56 | 57 | private UnicodeString decodeFixedLength(long length) throws CborException { 58 | return new UnicodeString(new String(decodeBytes(length), StandardCharsets.UTF_8)); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoder.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import java.io.InputStream; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.UnsignedInteger; 8 | 9 | public class UnsignedIntegerDecoder extends AbstractDecoder { 10 | 11 | public UnsignedIntegerDecoder(CborDecoder decoder, InputStream inputStream) { 12 | super(decoder, inputStream); 13 | } 14 | 15 | @Override 16 | public UnsignedInteger decode(int initialByte) throws CborException { 17 | return new UnsignedInteger(getLengthAsBigInteger(initialByte)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/AbstractFloat.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class AbstractFloat extends Special { 6 | 7 | private final float value; 8 | 9 | public AbstractFloat(SpecialType specialType, float value) { 10 | super(specialType); 11 | this.value = value; 12 | } 13 | 14 | public float getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public boolean equals(Object object) { 20 | if (object instanceof AbstractFloat) { 21 | AbstractFloat other = (AbstractFloat) object; 22 | return super.equals(object) && value == other.value; 23 | } 24 | return false; 25 | } 26 | 27 | @Override 28 | public int hashCode() { 29 | return super.hashCode() ^ Objects.hashCode(value); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/AdditionalInformation.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | /** 4 | * The initial byte of each data item contains both information about the major 5 | * type (the high-order 3 bits) and additional information (the low-order 5 6 | * bits). When the value of the additional information is less than 24, it is 7 | * directly used as a small unsigned integer. When it is 24 to 27, the 8 | * additional bytes for a variable-length integer immediately follow; the values 9 | * 24 to 27 of the additional information specify that its length is a 1-, 2-, 10 | * 4- or 8-byte unsigned integer, respectively. Additional information value 31 11 | * is used for indefinite length items, described in Section 2.2. Additional 12 | * information values 28 to 30 are reserved for future expansion. 13 | */ 14 | public enum AdditionalInformation { 15 | 16 | DIRECT(0), // 0-23 17 | ONE_BYTE(24), // 24 18 | TWO_BYTES(25), // 25 19 | FOUR_BYTES(26), // 26 20 | EIGHT_BYTES(27), // 27 21 | RESERVED(28), // 28-30 22 | INDEFINITE(31); // 31 23 | 24 | private final int value; 25 | 26 | private AdditionalInformation(int value) { 27 | this.value = value; 28 | } 29 | 30 | public int getValue() { 31 | return value; 32 | } 33 | 34 | public static AdditionalInformation ofByte(int b) { 35 | switch (b & 31) { 36 | case 24: 37 | return ONE_BYTE; 38 | case 25: 39 | return TWO_BYTES; 40 | case 26: 41 | return FOUR_BYTES; 42 | case 27: 43 | return EIGHT_BYTES; 44 | case 28: 45 | case 29: 46 | case 30: 47 | return RESERVED; 48 | case 31: 49 | return INDEFINITE; 50 | default: 51 | return DIRECT; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/Array.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class Array extends ChunkableDataItem { 8 | 9 | private final ArrayList objects; 10 | 11 | public Array() { 12 | super(MajorType.ARRAY); 13 | objects = new ArrayList<>(); 14 | } 15 | 16 | public Array(int initialCapacity) { 17 | super(MajorType.ARRAY); 18 | objects = new ArrayList<>(initialCapacity); 19 | } 20 | 21 | public Array add(DataItem object) { 22 | objects.add(object); 23 | return this; 24 | } 25 | 26 | public List getDataItems() { 27 | return objects; 28 | } 29 | 30 | @Override 31 | public boolean equals(Object object) { 32 | if (object instanceof Array) { 33 | Array other = (Array) object; 34 | return super.equals(object) && objects.equals(other.objects); 35 | } 36 | return false; 37 | } 38 | 39 | @Override 40 | public int hashCode() { 41 | return super.hashCode() ^ objects.hashCode(); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | StringBuilder stringBuilder = new StringBuilder("["); 47 | if (isChunked()) { 48 | stringBuilder.append("_ "); 49 | } 50 | stringBuilder.append(Arrays.toString(objects.toArray()).substring(1)); 51 | return stringBuilder.toString(); 52 | } 53 | 54 | public DataItem peekLast() { 55 | if (objects.isEmpty()) { 56 | return null; 57 | } 58 | return objects.get(objects.size() - 1); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/ByteString.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Arrays; 4 | 5 | public class ByteString extends ChunkableDataItem { 6 | 7 | private final byte[] bytes; 8 | 9 | public ByteString(byte[] bytes) { 10 | super(MajorType.BYTE_STRING); 11 | if (bytes == null) { 12 | this.bytes = null; 13 | } else { 14 | this.bytes = bytes; 15 | } 16 | } 17 | 18 | public byte[] getBytes() { 19 | if (bytes == null) { 20 | return null; 21 | } else { 22 | return bytes; 23 | } 24 | } 25 | 26 | @Override 27 | public boolean equals(Object object) { 28 | if (object instanceof ByteString) { 29 | ByteString other = (ByteString) object; 30 | return super.equals(object) && Arrays.equals(bytes, other.bytes); 31 | } 32 | return false; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | return super.hashCode() ^ Arrays.hashCode(bytes); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/ChunkableDataItem.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | class ChunkableDataItem extends DataItem { 6 | 7 | private boolean chunked = false; 8 | 9 | protected ChunkableDataItem(MajorType majorType) { 10 | super(majorType); 11 | } 12 | 13 | public boolean isChunked() { 14 | return chunked; 15 | } 16 | 17 | public ChunkableDataItem setChunked(boolean chunked) { 18 | this.chunked = chunked; 19 | return this; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object object) { 24 | if (object instanceof ChunkableDataItem) { 25 | ChunkableDataItem other = (ChunkableDataItem) object; 26 | return super.equals(object) && chunked == other.chunked; 27 | } 28 | return false; 29 | } 30 | 31 | @Override 32 | public int hashCode() { 33 | return super.hashCode() ^ Objects.hashCode(chunked); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/DataItem.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | import co.nstant.in.cbor.CborEncoder; 6 | 7 | public class DataItem { 8 | 9 | private final MajorType majorType; 10 | private Tag tag; 11 | 12 | protected DataItem(MajorType majorType) { 13 | this.majorType = majorType; 14 | Objects.requireNonNull(majorType, "majorType is null"); 15 | } 16 | 17 | public MajorType getMajorType() { 18 | return majorType; 19 | } 20 | 21 | public void setTag(long tag) { 22 | if (tag < 0) { 23 | throw new IllegalArgumentException("tag number must be 0 or greater"); 24 | } 25 | 26 | this.tag = new Tag(tag); 27 | } 28 | 29 | public void setTag(Tag tag) { 30 | Objects.requireNonNull(tag, "tag is null"); 31 | this.tag = tag; 32 | } 33 | 34 | public void removeTag() { 35 | tag = null; 36 | } 37 | 38 | public Tag getTag() { 39 | return tag; 40 | } 41 | 42 | public boolean hasTag() { 43 | return tag != null; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object object) { 48 | if (object instanceof DataItem) { 49 | DataItem other = (DataItem) object; 50 | if (tag != null) { 51 | return tag.equals(other.tag) && majorType == other.majorType; 52 | } else { 53 | return other.tag == null && majorType == other.majorType; 54 | } 55 | } 56 | 57 | return false; 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | return Objects.hash(majorType, tag); 63 | } 64 | 65 | protected void assertTrue(boolean condition, String message) { 66 | if (!condition) { 67 | throw new IllegalArgumentException(message); 68 | } 69 | } 70 | 71 | public DataItem getOuterTaggable() { 72 | DataItem item = this; 73 | while (item.getTag() != null) { 74 | item = item.getTag(); 75 | } 76 | return item; 77 | } 78 | 79 | public byte[] encodeToBytes() { 80 | return CborEncoder.encodeToBytes(this); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/DoublePrecisionFloat.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class DoublePrecisionFloat extends Special { 6 | 7 | private final double value; 8 | 9 | public DoublePrecisionFloat(double value) { 10 | super(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT); 11 | this.value = value; 12 | } 13 | 14 | public double getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public boolean equals(Object object) { 20 | if (object instanceof DoublePrecisionFloat) { 21 | DoublePrecisionFloat other = (DoublePrecisionFloat) object; 22 | return super.equals(object) && value == other.value; 23 | } 24 | return false; 25 | } 26 | 27 | @Override 28 | public int hashCode() { 29 | return super.hashCode() ^ Objects.hashCode(value); 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return String.valueOf(value); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/HalfPrecisionFloat.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | public class HalfPrecisionFloat extends AbstractFloat { 4 | 5 | public HalfPrecisionFloat(float value) { 6 | super(SpecialType.IEEE_754_HALF_PRECISION_FLOAT, value); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/LanguageTaggedString.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * See https://peteroupc.github.io/CBOR/langtags.html 7 | */ 8 | public class LanguageTaggedString extends Array { 9 | 10 | public LanguageTaggedString(String language, String string) { 11 | this(new UnicodeString(language), new UnicodeString(string)); 12 | } 13 | 14 | public LanguageTaggedString(UnicodeString language, UnicodeString string) { 15 | setTag(38); 16 | add(Objects.requireNonNull(language)); 17 | add(Objects.requireNonNull(string)); 18 | } 19 | 20 | public UnicodeString getLanguage() { 21 | return (UnicodeString) getDataItems().get(0); 22 | } 23 | 24 | public UnicodeString getString() { 25 | return (UnicodeString) getDataItems().get(1); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/Map.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Collection; 4 | import java.util.LinkedHashMap; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | public class Map extends ChunkableDataItem { 9 | 10 | private final LinkedHashMap map; 11 | private final List keys = new LinkedList<>(); 12 | 13 | public Map() { 14 | super(MajorType.MAP); 15 | map = new LinkedHashMap<>(); 16 | } 17 | 18 | public Map(int initialCapacity) { 19 | super(MajorType.MAP); 20 | map = new LinkedHashMap<>(initialCapacity); 21 | } 22 | 23 | public Map put(DataItem key, DataItem value) { 24 | if (map.put(key, value) == null) { 25 | keys.add(key); 26 | } 27 | return this; 28 | } 29 | 30 | public DataItem get(DataItem key) { 31 | return map.get(key); 32 | } 33 | 34 | public DataItem remove(DataItem key) { 35 | keys.remove(key); 36 | return map.remove(key); 37 | } 38 | 39 | public Collection getKeys() { 40 | return keys; 41 | } 42 | 43 | public Collection getValues() { 44 | return map.values(); 45 | } 46 | 47 | @Override 48 | public boolean equals(Object object) { 49 | if (object instanceof Map) { 50 | Map other = (Map) object; 51 | return super.equals(object) && map.equals(other.map); 52 | } 53 | return false; 54 | } 55 | 56 | @Override 57 | public int hashCode() { 58 | return super.hashCode() ^ map.hashCode(); 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | StringBuilder stringBuilder = new StringBuilder(); 64 | if (isChunked()) { 65 | stringBuilder.append("{_ "); 66 | } else { 67 | stringBuilder.append("{ "); 68 | } 69 | for (DataItem key : keys) { 70 | stringBuilder.append(key).append(": ").append(map.get(key)).append(", "); 71 | } 72 | if (stringBuilder.toString().endsWith(", ")) { 73 | stringBuilder.setLength(stringBuilder.length() - 2); 74 | } 75 | stringBuilder.append(" }"); 76 | return stringBuilder.toString(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/NegativeInteger.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.math.BigInteger; 4 | 5 | public class NegativeInteger extends Number { 6 | 7 | public NegativeInteger(long value) { 8 | this(BigInteger.valueOf(value)); 9 | assertTrue(value < 0L, "value " + value + " is not < 0"); 10 | } 11 | 12 | public NegativeInteger(BigInteger value) { 13 | super(MajorType.NEGATIVE_INTEGER, value); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/Number.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.math.BigInteger; 4 | import java.util.Objects; 5 | 6 | public abstract class Number extends DataItem { 7 | 8 | private final BigInteger value; 9 | 10 | protected Number(MajorType majorType, BigInteger value) { 11 | super(majorType); 12 | this.value = Objects.requireNonNull(value); 13 | } 14 | 15 | public BigInteger getValue() { 16 | return value; 17 | } 18 | 19 | @Override 20 | public boolean equals(Object object) { 21 | if (object instanceof Number) { 22 | Number other = (Number) object; 23 | return super.equals(object) && value.equals(other.value); 24 | } 25 | return false; 26 | } 27 | 28 | @Override 29 | public int hashCode() { 30 | return super.hashCode() ^ value.hashCode(); 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return value.toString(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/RationalNumber.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.math.BigInteger; 4 | 5 | import co.nstant.in.cbor.CborException; 6 | 7 | /** 8 | * Rational Numbers: http://peteroupc.github.io/CBOR/rational.html 9 | */ 10 | 11 | public class RationalNumber extends Array { 12 | 13 | public RationalNumber(Number numerator, Number denominator) throws CborException { 14 | setTag(30); 15 | if (numerator == null) { 16 | throw new CborException("Numerator is null"); 17 | } 18 | if (denominator == null) { 19 | throw new CborException("Denominator is null"); 20 | } 21 | if (denominator.getValue().equals(BigInteger.ZERO)) { 22 | throw new CborException("Denominator is zero"); 23 | } 24 | add(numerator); 25 | add(denominator); 26 | } 27 | 28 | public Number getNumerator() { 29 | return (Number) getDataItems().get(0); 30 | } 31 | 32 | public Number getDenominator() { 33 | return (Number) getDataItems().get(1); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/SimpleValue.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class SimpleValue extends Special { 6 | 7 | private final SimpleValueType simpleValueType; 8 | 9 | public static final SimpleValue FALSE = new SimpleValue(SimpleValueType.FALSE); 10 | public static final SimpleValue TRUE = new SimpleValue(SimpleValueType.TRUE); 11 | public static final SimpleValue NULL = new SimpleValue(SimpleValueType.NULL); 12 | public static final SimpleValue UNDEFINED = new SimpleValue(SimpleValueType.UNDEFINED); 13 | 14 | private final int value; 15 | 16 | public SimpleValue(SimpleValueType simpleValueType) { 17 | super(SpecialType.SIMPLE_VALUE); 18 | this.value = simpleValueType.getValue(); 19 | this.simpleValueType = simpleValueType; 20 | } 21 | 22 | public SimpleValue(int value) { 23 | super(value <= 23 ? SpecialType.SIMPLE_VALUE : SpecialType.SIMPLE_VALUE_NEXT_BYTE); 24 | this.value = value; 25 | this.simpleValueType = SimpleValueType.ofByte(value); 26 | } 27 | 28 | public SimpleValueType getSimpleValueType() { 29 | return simpleValueType; 30 | } 31 | 32 | public int getValue() { 33 | return value; 34 | } 35 | 36 | @Override 37 | public boolean equals(Object object) { 38 | if (object instanceof SimpleValue) { 39 | SimpleValue other = (SimpleValue) object; 40 | return super.equals(object) && value == other.value; 41 | } 42 | return false; 43 | } 44 | 45 | @Override 46 | public int hashCode() { 47 | return super.hashCode() ^ Objects.hashCode(value); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return simpleValueType.toString(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/SimpleValueType.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | public enum SimpleValueType { 4 | 5 | FALSE(20), TRUE(21), NULL(22), UNDEFINED(23), RESERVED(0), UNALLOCATED(0); 6 | 7 | private final int value; 8 | 9 | private SimpleValueType(int value) { 10 | this.value = value; 11 | } 12 | 13 | public int getValue() { 14 | return value; 15 | } 16 | 17 | public static SimpleValueType ofByte(int b) { 18 | switch (b & 31) { 19 | case 20: 20 | return FALSE; 21 | case 21: 22 | return TRUE; 23 | case 22: 24 | return NULL; 25 | case 23: 26 | return UNDEFINED; 27 | case 24: 28 | case 25: 29 | case 26: 30 | case 27: 31 | case 28: 32 | case 29: 33 | case 30: 34 | case 31: 35 | return RESERVED; 36 | default: 37 | return UNALLOCATED; 38 | } 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/SinglePrecisionFloat.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | public class SinglePrecisionFloat extends AbstractFloat { 4 | 5 | public SinglePrecisionFloat(float value) { 6 | super(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, value); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/Special.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class Special extends DataItem { 6 | 7 | public static final Special BREAK = new Special(SpecialType.BREAK); 8 | 9 | private final SpecialType specialType; 10 | 11 | protected Special(SpecialType specialType) { 12 | super(MajorType.SPECIAL); 13 | this.specialType = Objects.requireNonNull(specialType); 14 | } 15 | 16 | public SpecialType getSpecialType() { 17 | return specialType; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object object) { 22 | if (object instanceof Special) { 23 | Special other = (Special) object; 24 | return super.equals(object) && specialType == other.specialType; 25 | } 26 | return false; 27 | } 28 | 29 | @Override 30 | public int hashCode() { 31 | return super.hashCode() ^ Objects.hashCode(specialType); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return specialType.name(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/SpecialType.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import co.nstant.in.cbor.CborException; 4 | 5 | public enum SpecialType { 6 | 7 | SIMPLE_VALUE, SIMPLE_VALUE_NEXT_BYTE, IEEE_754_HALF_PRECISION_FLOAT, IEEE_754_SINGLE_PRECISION_FLOAT, 8 | IEEE_754_DOUBLE_PRECISION_FLOAT, BREAK; 9 | 10 | public static SpecialType ofByte(int b) throws CborException { 11 | switch (b & 31) { 12 | case 24: 13 | return SIMPLE_VALUE_NEXT_BYTE; 14 | case 25: 15 | return IEEE_754_HALF_PRECISION_FLOAT; 16 | case 26: 17 | return IEEE_754_SINGLE_PRECISION_FLOAT; 18 | case 27: 19 | return IEEE_754_DOUBLE_PRECISION_FLOAT; 20 | case 28: 21 | case 29: 22 | case 30: 23 | throw new CborException("Not implemented special type " + b); 24 | case 31: 25 | return BREAK; 26 | default: 27 | return SIMPLE_VALUE; 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/Tag.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.util.Objects; 4 | 5 | public class Tag extends DataItem { 6 | 7 | private final long value; 8 | 9 | public Tag(long value) { 10 | super(MajorType.TAG); 11 | this.value = value; 12 | } 13 | 14 | public long getValue() { 15 | return value; 16 | } 17 | 18 | @Override 19 | public boolean equals(Object object) { 20 | if (object instanceof Tag) { 21 | Tag other = (Tag) object; 22 | return super.equals(object) && value == other.value; 23 | } 24 | return false; 25 | } 26 | 27 | @Override 28 | public int hashCode() { 29 | return super.hashCode() ^ Objects.hashCode(value); 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Tag(" + value + ")"; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/UnicodeString.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | public class UnicodeString extends ChunkableDataItem { 4 | 5 | private final String string; 6 | 7 | public UnicodeString(String string) { 8 | super(MajorType.UNICODE_STRING); 9 | this.string = string; 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | if (string == null) { 15 | return "null"; 16 | } else { 17 | return string; 18 | } 19 | } 20 | 21 | public String getString() { 22 | return string; 23 | } 24 | 25 | @Override 26 | public boolean equals(Object object) { 27 | if (object instanceof UnicodeString && super.equals(object)) { 28 | UnicodeString other = (UnicodeString) object; 29 | if (string == null) { 30 | return other.string == null; 31 | } else { 32 | return string.equals(other.string); 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | int hash = 0; 41 | 42 | if (string != null) { 43 | hash = super.hashCode(); 44 | hash += string.hashCode(); 45 | } 46 | 47 | return hash; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/co/nstant/in/cbor/model/UnsignedInteger.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.math.BigInteger; 4 | 5 | public class UnsignedInteger extends Number { 6 | 7 | public UnsignedInteger(long value) { 8 | this(BigInteger.valueOf(value)); 9 | assertTrue(value >= 0L, "value " + value + " is not >= 0"); 10 | } 11 | 12 | public UnsignedInteger(BigInteger value) { 13 | super(MajorType.UNSIGNED_INTEGER, value); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/CborBuilderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.model.DataItem; 11 | import co.nstant.in.cbor.model.Tag; 12 | 13 | public class CborBuilderTest { 14 | 15 | @Test 16 | public void shouldResetDataItems() { 17 | CborBuilder builder = new CborBuilder(); 18 | builder.add(true); 19 | builder.add(1.0f); 20 | assertEquals(2, builder.build().size()); 21 | builder.reset(); 22 | assertEquals(0, builder.build().size()); 23 | } 24 | 25 | @Test 26 | public void shouldAddTag() { 27 | CborBuilder builder = new CborBuilder(); 28 | List dataItems = builder.addTag(1234).build(); 29 | assertEquals(1, dataItems.size()); 30 | assertTrue(dataItems.get(0) instanceof Tag); 31 | assertEquals(1234, ((Tag) dataItems.get(0)).getValue()); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/CborEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.IOException; 6 | import java.io.OutputStream; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.model.DataItem; 11 | import co.nstant.in.cbor.model.MajorType; 12 | 13 | public class CborEncoderTest { 14 | 15 | private class Mock extends DataItem { 16 | 17 | public Mock(MajorType majorType) { 18 | super(majorType); 19 | } 20 | 21 | } 22 | 23 | @Test(expected = ClassCastException.class) 24 | public void shouldExpectUnsignedIntegerImplementation() { 25 | new Mock(MajorType.UNSIGNED_INTEGER).encodeToBytes(); 26 | } 27 | 28 | @Test(expected = ClassCastException.class) 29 | public void shouldExpectNegativeIntegerImplementation() { 30 | new Mock(MajorType.NEGATIVE_INTEGER).encodeToBytes(); 31 | } 32 | 33 | @Test(expected = ClassCastException.class) 34 | public void shouldExpectByteStringImplementation() { 35 | new Mock(MajorType.BYTE_STRING).encodeToBytes(); 36 | } 37 | 38 | @Test(expected = ClassCastException.class) 39 | public void shouldExpectUnicodeStringImplementation() { 40 | new Mock(MajorType.UNICODE_STRING).encodeToBytes(); 41 | } 42 | 43 | @Test(expected = ClassCastException.class) 44 | public void shouldExpectArrayImplementation() { 45 | new Mock(MajorType.ARRAY).encodeToBytes(); 46 | } 47 | 48 | @Test(expected = ClassCastException.class) 49 | public void shouldExpectMapImplementation() { 50 | new Mock(MajorType.MAP).encodeToBytes(); 51 | } 52 | 53 | @Test(expected = ClassCastException.class) 54 | public void shouldExpectTagImplementation() { 55 | new Mock(MajorType.TAG).encodeToBytes(); 56 | } 57 | 58 | @Test(expected = ClassCastException.class) 59 | public void shouldExpectSpecialImplementation() { 60 | new Mock(MajorType.SPECIAL).encodeToBytes(); 61 | } 62 | 63 | @Test(expected = CborException.class) 64 | public void shouldThrowCborExceptionOnUnderlyingIoException() throws CborException { 65 | new CborEncoder(new OutputStream() { 66 | 67 | private int counter = 0; 68 | 69 | @Override 70 | public void write(int b) throws IOException { 71 | if (++counter == 3) { 72 | throw new IOException(); 73 | } 74 | } 75 | 76 | }).encode(new CborBuilder().startString().add("string").end().build()); 77 | } 78 | 79 | @Test(expected = CborException.class) 80 | public void shouldThrowCborExceptionOnUnderlyingIoException2() throws CborException { 81 | new CborEncoder(new OutputStream() { 82 | 83 | @Override 84 | public void write(int b) throws IOException { 85 | throw new IOException(); 86 | } 87 | 88 | }).encode(new CborBuilder().startArray().add(1).end().build()); 89 | } 90 | 91 | @Test 92 | public void shallEncode32bit() { 93 | byte[] bytes = CborEncoder.encodeToBytes(new CborBuilder().addTag(4294967296L).build()); 94 | assertEquals(9, bytes.length); 95 | assertEquals((byte) 0xdB, bytes[0]); 96 | assertEquals((byte) 0x00, bytes[1]); 97 | assertEquals((byte) 0x00, bytes[2]); 98 | assertEquals((byte) 0x00, bytes[3]); 99 | assertEquals((byte) 0x01, bytes[4]); 100 | assertEquals((byte) 0x00, bytes[5]); 101 | assertEquals((byte) 0x00, bytes[6]); 102 | assertEquals((byte) 0x00, bytes[7]); 103 | assertEquals((byte) 0x00, bytes[8]); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/CborExceptionTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class CborExceptionTest { 7 | 8 | @Test 9 | public void shouldHaveMessage() { 10 | CborException cborException = new CborException("message"); 11 | Assert.assertEquals("message", cborException.getMessage()); 12 | } 13 | 14 | @Test 15 | public void shouldHaveThrowable() { 16 | Throwable throwable = new Throwable(); 17 | CborException cborException = new CborException(throwable); 18 | Assert.assertEquals(throwable, cborException.getCause()); 19 | } 20 | 21 | @Test 22 | public void shouldHaveMessageAndThrowable() { 23 | Throwable throwable = new Throwable(); 24 | CborException cborException = new CborException("message", throwable); 25 | Assert.assertEquals("message", cborException.getMessage()); 26 | Assert.assertEquals(throwable, cborException.getCause()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/DatemItemListenerTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.io.ByteArrayInputStream; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.model.DataItem; 11 | import co.nstant.in.cbor.model.UnsignedInteger; 12 | 13 | public class DatemItemListenerTest { 14 | 15 | @Test 16 | public void shouldDecodeZero() throws CborException { 17 | final int[] dataItems = new int[1]; 18 | byte[] bytes = CborEncoder.encodeToBytes(new CborBuilder().add(1234).build()); 19 | new CborDecoder(new ByteArrayInputStream(bytes)).decode(new DataItemListener() { 20 | 21 | @Override 22 | public void onDataItem(DataItem dataItem) { 23 | synchronized (dataItems) { 24 | ++dataItems[0]; 25 | } 26 | assertTrue(dataItem instanceof UnsignedInteger); 27 | } 28 | 29 | }); 30 | assertEquals(1, dataItems[0]); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/EncoderDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import co.nstant.in.cbor.model.DataItem; 9 | import co.nstant.in.cbor.model.NegativeInteger; 10 | import co.nstant.in.cbor.model.UnsignedInteger; 11 | 12 | public class EncoderDecoderTest { 13 | 14 | @Test 15 | public void test() throws CborException { 16 | UnsignedInteger a = new UnsignedInteger(1); 17 | NegativeInteger x = new NegativeInteger(-2); 18 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 19 | CborEncoder encoder = new CborEncoder(byteArrayOutputStream); 20 | encoder.encode(a); 21 | encoder.encode(x); 22 | byte[] bytes = byteArrayOutputStream.toByteArray(); 23 | DataItem object = CborDecoder.decode(bytes).get(0); 24 | Assert.assertEquals(a, object); 25 | } 26 | 27 | @Test 28 | public void testTagging() throws CborException { 29 | UnsignedInteger a = new UnsignedInteger(1); 30 | NegativeInteger x = new NegativeInteger(-2); 31 | 32 | a.setTag(1); 33 | 34 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 35 | CborEncoder encoder = new CborEncoder(byteArrayOutputStream); 36 | encoder.encode(a); 37 | encoder.encode(x); 38 | byte[] bytes = byteArrayOutputStream.toByteArray(); 39 | DataItem object = CborDecoder.decode(bytes).get(0); 40 | Assert.assertEquals(a, object); 41 | Assert.assertTrue(object.hasTag()); 42 | Assert.assertEquals(1L, object.getTag().getValue()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/builder/AbstractBuilderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import java.io.OutputStream; 6 | 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborException; 10 | import co.nstant.in.cbor.model.DataItem; 11 | import co.nstant.in.cbor.model.HalfPrecisionFloat; 12 | import co.nstant.in.cbor.model.SinglePrecisionFloat; 13 | 14 | public class AbstractBuilderTest { 15 | 16 | private class TestBuilder extends AbstractBuilder { 17 | 18 | public TestBuilder() { 19 | super(null); 20 | } 21 | 22 | public DataItem testConvert(float value) { 23 | return convert(value); 24 | } 25 | 26 | public void testAddChunk() { 27 | addChunk(null); 28 | } 29 | 30 | } 31 | 32 | @Test 33 | public void shouldCallIsHalfPrecisionEnough() { 34 | TestBuilder builder = new TestBuilder(); 35 | assertTrue(builder.testConvert(0.0f) instanceof HalfPrecisionFloat); 36 | assertTrue(0.0f == ((HalfPrecisionFloat) builder.testConvert(0.0f)).getValue()); 37 | assertTrue(builder.testConvert(0.3f) instanceof SinglePrecisionFloat); 38 | assertTrue(0.3f == ((SinglePrecisionFloat) builder.testConvert(0.3f)).getValue()); 39 | } 40 | 41 | @Test(expected = IllegalStateException.class) 42 | public void shouldThrowExceptionOnAddChunk() { 43 | new TestBuilder().testAddChunk(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/builder/ArrayBuilderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.model.Array; 12 | import co.nstant.in.cbor.model.ByteString; 13 | import co.nstant.in.cbor.model.DataItem; 14 | import co.nstant.in.cbor.model.DoublePrecisionFloat; 15 | import co.nstant.in.cbor.model.HalfPrecisionFloat; 16 | import co.nstant.in.cbor.model.SimpleValue; 17 | 18 | public class ArrayBuilderTest { 19 | 20 | @Test 21 | public void shouldAddBoolean() { 22 | CborBuilder builder = new CborBuilder(); 23 | List dataItems = builder.addArray().add(true).add(false).end().build(); 24 | assertEquals(1, dataItems.size()); 25 | assertTrue(dataItems.get(0) instanceof Array); 26 | Array array = (Array) dataItems.get(0); 27 | assertEquals(2, array.getDataItems().size()); 28 | assertTrue(array.getDataItems().get(0) instanceof SimpleValue); 29 | assertTrue(array.getDataItems().get(1) instanceof SimpleValue); 30 | } 31 | 32 | @Test 33 | public void shouldAddFloat() { 34 | CborBuilder builder = new CborBuilder(); 35 | List dataItems = builder.addArray().add(1.0f).end().build(); 36 | assertEquals(1, dataItems.size()); 37 | assertTrue(dataItems.get(0) instanceof Array); 38 | Array array = (Array) dataItems.get(0); 39 | assertEquals(1, array.getDataItems().size()); 40 | assertTrue(array.getDataItems().get(0) instanceof HalfPrecisionFloat); 41 | } 42 | 43 | @Test 44 | public void shouldAddDouble() { 45 | CborBuilder builder = new CborBuilder(); 46 | List dataItems = builder.addArray().add(1.0d).end().build(); 47 | assertEquals(1, dataItems.size()); 48 | assertTrue(dataItems.get(0) instanceof Array); 49 | Array array = (Array) dataItems.get(0); 50 | assertEquals(1, array.getDataItems().size()); 51 | assertTrue(array.getDataItems().get(0) instanceof DoublePrecisionFloat); 52 | } 53 | 54 | @Test 55 | public void shouldAddByteArray() { 56 | CborBuilder builder = new CborBuilder(); 57 | List dataItems = builder.addArray().add(new byte[] { 0x0 }).end().build(); 58 | assertEquals(1, dataItems.size()); 59 | assertTrue(dataItems.get(0) instanceof Array); 60 | Array array = (Array) dataItems.get(0); 61 | assertEquals(1, array.getDataItems().size()); 62 | assertTrue(array.getDataItems().get(0) instanceof ByteString); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/builder/MapBuilderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.builder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.model.ByteString; 12 | import co.nstant.in.cbor.model.DataItem; 13 | import co.nstant.in.cbor.model.Map; 14 | import co.nstant.in.cbor.model.UnicodeString; 15 | import co.nstant.in.cbor.model.UnsignedInteger; 16 | 17 | public class MapBuilderTest { 18 | 19 | @Test 20 | public void testMapBuilder() { 21 | List dataItems = new CborBuilder() 22 | .addMap() 23 | .put(new UnicodeString("key"), new UnicodeString("value")) 24 | .put(1, true) 25 | .put(2, "value".getBytes()) 26 | .put(3, 1.0d) 27 | .put(4, 1.0f) 28 | .put(5, 1L) 29 | .put(6, "value") 30 | .put("7", true) 31 | .put("8", "value".getBytes()) 32 | .put("9", 1.0d) 33 | .put("10", 1.0f) 34 | .put("11", 1L) 35 | .put("12", "value") 36 | .putMap(13) 37 | .end() 38 | .putMap("14").end() 39 | .putMap(new UnsignedInteger(15)).end() 40 | .putArray(16).end() 41 | .putArray("17").end() 42 | .putArray(new UnsignedInteger(18)).end() 43 | .addKey(19).value(true) 44 | .addKey(20).value("value".getBytes()) 45 | .addKey(21).value(1.0d) 46 | .addKey(22).value(1.0f) 47 | .addKey(23).value(1L) 48 | .addKey(24).value("value") 49 | .addKey("25").value(true) 50 | .addKey("26").value("value".getBytes()) 51 | .addKey("27").value(1.0d) 52 | .addKey("28").value(1.0f) 53 | .addKey("29").value(1L) 54 | .addKey("30").value("value") 55 | .end() 56 | .startMap() 57 | .startArray(1).end() 58 | .startArray(new UnsignedInteger(2)).end() 59 | .end() 60 | .build(); 61 | assertEquals(2, dataItems.size()); 62 | assertTrue(dataItems.get(0) instanceof Map); 63 | Map map = (Map) dataItems.get(0); 64 | assertEquals(31, map.getKeys().size()); 65 | } 66 | 67 | @Test 68 | public void startMapInMap() { 69 | CborBuilder builder = new CborBuilder(); 70 | List dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end() 71 | .startMap(1).end().startMap("asdf").end().end().build(); 72 | Map rootMap = (Map) dataItems.get(0); 73 | DataItem keys[] = new DataItem[3]; 74 | rootMap.getKeys().toArray(keys); 75 | assertEquals(keys[0], new ByteString(new byte[] { 0x01 })); 76 | assertEquals(keys[1], new UnsignedInteger(1)); 77 | assertEquals(keys[2], new UnicodeString("asdf")); 78 | 79 | assertTrue(rootMap.get(keys[0]) instanceof Map); 80 | assertTrue(rootMap.get(keys[1]) instanceof Map); 81 | assertTrue(rootMap.get(keys[2]) instanceof Map); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/ArrayDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import org.junit.Test; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | 8 | public class ArrayDecoderTest { 9 | @Test(expected = CborException.class) 10 | public void shouldThrowOnIncompleteArray() throws CborException { 11 | byte[] bytes = new byte[] { (byte) 0x82, 0x01 }; 12 | CborDecoder.decode(bytes); 13 | } 14 | 15 | @Test(expected = CborException.class) 16 | public void shouldThrowInIncompleteIndefiniteLengthArray() throws CborException { 17 | byte[] bytes = new byte[] { (byte) 0x9f, 0x01, 0x02 }; 18 | CborDecoder.decode(bytes); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/ByteStringDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import java.io.ByteArrayInputStream; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborBuilder; 12 | import co.nstant.in.cbor.CborDecoder; 13 | import co.nstant.in.cbor.CborEncoder; 14 | import co.nstant.in.cbor.CborException; 15 | import co.nstant.in.cbor.model.ByteString; 16 | import co.nstant.in.cbor.model.DataItem; 17 | import co.nstant.in.cbor.model.MajorType; 18 | 19 | public class ByteStringDecoderTest { 20 | 21 | @Test 22 | public void shouldDecodeChunkedByteString() throws CborException { 23 | byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().startByteString() 24 | .add(new byte[] { '\0' }).add(new byte[] { 0x10 }).add(new byte[] { 0x13 }).end().build()); 25 | ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); 26 | CborDecoder decoder = new CborDecoder(bais); 27 | List dataItems = decoder.decode(); 28 | assertNotNull(dataItems); 29 | assertEquals(1, dataItems.size()); 30 | } 31 | 32 | @Test 33 | public void shouldDecodeByteString1K() throws CborException { 34 | byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(new byte[1024]).build()); 35 | ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); 36 | CborDecoder decoder = new CborDecoder(bais); 37 | List dataItems = decoder.decode(); 38 | assertNotNull(dataItems); 39 | assertEquals(1, dataItems.size()); 40 | } 41 | 42 | @Test 43 | public void shouldDecodeByteString1M() throws CborException { 44 | byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(new byte[1024 * 1024]).build()); 45 | ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); 46 | CborDecoder decoder = new CborDecoder(bais); 47 | List dataItems = decoder.decode(); 48 | assertNotNull(dataItems); 49 | assertEquals(1, dataItems.size()); 50 | } 51 | 52 | @Test(expected = CborException.class) 53 | public void shouldThrowOnIncompleteByteString() throws CborException { 54 | byte[] bytes = new byte[] { 0x42, 0x20 }; 55 | CborDecoder.decode(bytes); 56 | } 57 | 58 | @Test(expected = CborException.class) 59 | public void shouldTrowOnMissingBreak() throws CborException { 60 | byte[] bytes = new byte[] { 0x5f, 0x41, 0x20 }; 61 | CborDecoder.decode(bytes); 62 | } 63 | 64 | public void decodingExample() throws CborException { 65 | byte bytes[] = { 0, 1, 2, 3 }; 66 | // Encode 67 | byte[] encodedBytes = new ByteString(bytes).encodeToBytes(); 68 | // Decode 69 | ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes); 70 | CborDecoder decoder = new CborDecoder(inputStream); 71 | DataItem dataItem = decoder.decodeNext(); 72 | assertEquals(MajorType.BYTE_STRING, dataItem.getMajorType()); 73 | ByteString byteString = (ByteString) dataItem; 74 | byte[] decodedBytes = byteString.getBytes(); 75 | // Verify 76 | assertEquals(bytes.length, decodedBytes.length); 77 | assertEquals(bytes[0], decodedBytes[0]); 78 | assertEquals(bytes[1], decodedBytes[1]); 79 | assertEquals(bytes[2], decodedBytes[2]); 80 | assertEquals(bytes[3], decodedBytes[3]); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/LanguageTaggedStringDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.LanguageTaggedString; 16 | import co.nstant.in.cbor.model.UnicodeString; 17 | 18 | public class LanguageTaggedStringDecoderTest { 19 | 20 | // Unexpected end of stream, tag without data item 21 | @Test(expected = CborException.class) 22 | public void shouldThrowException() throws CborException { 23 | List items = new CborBuilder().addTag(38).build(); 24 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 25 | CborDecoder decoder = new CborDecoder(bais); 26 | decoder.decode(); 27 | } 28 | 29 | @Test(expected = CborException.class) 30 | public void testExceptionOnNotAnArray() throws CborException { 31 | List items = new CborBuilder().addTag(38).add(true).build(); 32 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 33 | CborDecoder decoder = new CborDecoder(bais); 34 | decoder.decode(); 35 | } 36 | 37 | @Test(expected = CborException.class) 38 | public void testExceptionOnNot2ElementArray() throws CborException { 39 | List items = new CborBuilder().addTag(38).addArray().add(true).end().build(); 40 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 41 | CborDecoder decoder = new CborDecoder(bais); 42 | decoder.decode(); 43 | } 44 | 45 | @Test(expected = CborException.class) 46 | public void testExceptionOnNotFirstElementIsString() throws CborException { 47 | List items = new CborBuilder().addTag(38).addArray().add(true).add(true).end().build(); 48 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 49 | CborDecoder decoder = new CborDecoder(bais); 50 | decoder.decode(); 51 | } 52 | 53 | @Test(expected = CborException.class) 54 | public void testExceptionOnNotSecondElementIsString() throws CborException { 55 | List items = new CborBuilder().addTag(38).addArray().add("en").add(true).end().build(); 56 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 57 | CborDecoder decoder = new CborDecoder(bais); 58 | decoder.decode(); 59 | } 60 | 61 | @Test 62 | public void testDecoding() throws CborException { 63 | List items = new CborBuilder().addTag(38).addArray().add("en").add("string").end().build(); 64 | ByteArrayInputStream bais = new ByteArrayInputStream(CborEncoder.encodeToBytes(items)); 65 | CborDecoder decoder = new CborDecoder(bais); 66 | DataItem item = decoder.decodeNext(); 67 | assertEquals(new LanguageTaggedString(new UnicodeString("en"), new UnicodeString("string")), item); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/MapDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborDecoder; 11 | import co.nstant.in.cbor.CborException; 12 | import co.nstant.in.cbor.model.DataItem; 13 | import co.nstant.in.cbor.model.Map; 14 | import co.nstant.in.cbor.model.UnsignedInteger; 15 | 16 | public class MapDecoderTest { 17 | @Test(expected = CborException.class) 18 | public void shouldThrowOnMissingKeyInMap() throws CborException { 19 | byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x02 }; 20 | CborDecoder.decode(bytes); 21 | } 22 | 23 | @Test(expected = CborException.class) 24 | public void shouldThrowOnMissingValueInMap() throws CborException { 25 | byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x02, 0x03 }; 26 | CborDecoder.decode(bytes); 27 | } 28 | 29 | @Test(expected = CborException.class) 30 | public void shouldThrowOnIncompleteIndefiniteLengthMap() throws CborException { 31 | byte[] bytes = new byte[] { (byte) 0xbf, 0x61, 0x01 }; 32 | CborDecoder.decode(bytes); 33 | } 34 | 35 | @Test 36 | public void shouldUseLastOfDuplicateKeysByDefault() throws CborException { 37 | byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 }; 38 | List decoded = CborDecoder.decode(bytes); 39 | Map map = (Map) decoded.get(0); 40 | assertEquals(map.getKeys().size(), 1); 41 | assertEquals(map.get(new UnsignedInteger(1)), new UnsignedInteger(2)); 42 | } 43 | 44 | @Test(expected = CborException.class) 45 | public void shouldThrowOnDuplicateKeyIfEnabled() throws CborException { 46 | byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 }; 47 | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 48 | CborDecoder decoder = new CborDecoder(bais); 49 | decoder.setRejectDuplicateKeys(true); 50 | decoder.decode(); 51 | } 52 | 53 | @Test(expected = CborException.class) 54 | public void shouldThrowInDuplicateKeyInIndefiniteLengthMapIfEnabled() throws CborException { 55 | byte[] bytes = new byte[] { (byte) 0xbf, 0x01, 0x01, 0x01, 0x02, (byte) 0xff }; 56 | ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 57 | CborDecoder decoder = new CborDecoder(bais); 58 | decoder.setRejectDuplicateKeys(true); 59 | decoder.decode(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/SimpleValueDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.io.ByteArrayInputStream; 6 | import java.io.ByteArrayOutputStream; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.SimpleValue; 16 | import co.nstant.in.cbor.model.Special; 17 | 18 | public class SimpleValueDecoderTest { 19 | 20 | @Test 21 | public void shouldDecodeBoolean() throws CborException { 22 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 23 | CborEncoder encoder = new CborEncoder(byteArrayOutputStream); 24 | encoder.encode(SimpleValue.TRUE); 25 | encoder.encode(SimpleValue.FALSE); 26 | byte[] encodedBytes = byteArrayOutputStream.toByteArray(); 27 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedBytes); 28 | List dataItems = new CborDecoder(byteArrayInputStream).decode(); 29 | int result = 0; 30 | int position = 1; 31 | for (DataItem dataItem : dataItems) { 32 | position++; 33 | switch (dataItem.getMajorType()) { 34 | case SPECIAL: 35 | Special special = (Special) dataItem; 36 | switch (special.getSpecialType()) { 37 | case SIMPLE_VALUE: 38 | SimpleValue simpleValue = (SimpleValue) special; 39 | switch (simpleValue.getSimpleValueType()) { 40 | case FALSE: 41 | result += position * 2; 42 | break; 43 | case TRUE: 44 | result += position * 3; 45 | break; 46 | default: 47 | break; 48 | } 49 | break; 50 | default: 51 | break; 52 | } 53 | break; 54 | default: 55 | break; 56 | } 57 | } 58 | assertEquals(12, result); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/SpecialDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import org.junit.Test; 4 | 5 | import co.nstant.in.cbor.CborException; 6 | 7 | public class SpecialDecoderTest { 8 | 9 | @Test(expected = CborException.class) 10 | public void shouldThrowExceptionOnUnallocated() throws CborException { 11 | SpecialDecoder decoder = new SpecialDecoder(null, null); 12 | decoder.decode(28); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/UnicodeStringDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | 6 | import java.io.ByteArrayInputStream; 7 | import java.util.List; 8 | 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborBuilder; 12 | import co.nstant.in.cbor.CborDecoder; 13 | import co.nstant.in.cbor.CborEncoder; 14 | import co.nstant.in.cbor.CborException; 15 | import co.nstant.in.cbor.model.DataItem; 16 | 17 | public class UnicodeStringDecoderTest { 18 | 19 | @Test 20 | public void shouldDecodeChunkedUnicodeString() throws CborException { 21 | byte[] encodedBytes = CborEncoder.encodeToBytes( 22 | new CborBuilder().startString().add("foo").add("bar").end().build()); 23 | ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); 24 | CborDecoder decoder = new CborDecoder(bais); 25 | List dataItems = decoder.decode(); 26 | assertNotNull(dataItems); 27 | assertEquals(1, dataItems.size()); 28 | } 29 | 30 | @Test(expected = CborException.class) 31 | public void shouldThrowOnIncompleteString() throws CborException { 32 | byte[] bytes = new byte[] { 0x62, 0x61 }; 33 | CborDecoder.decode(bytes); 34 | } 35 | 36 | @Test(expected = CborException.class) 37 | public void shouldThrowOnMissingBreak() throws CborException { 38 | byte[] bytes = new byte[] { 0x7f, 0x61, 0x61 }; 39 | CborDecoder.decode(bytes); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/decoder/UnsignedIntegerDecoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.decoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotNull; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.io.ByteArrayInputStream; 8 | import java.math.BigInteger; 9 | import java.util.List; 10 | 11 | import org.junit.Test; 12 | 13 | import co.nstant.in.cbor.CborBuilder; 14 | import co.nstant.in.cbor.CborDecoder; 15 | import co.nstant.in.cbor.CborEncoder; 16 | import co.nstant.in.cbor.CborException; 17 | import co.nstant.in.cbor.model.DataItem; 18 | import co.nstant.in.cbor.model.UnsignedInteger; 19 | 20 | public class UnsignedIntegerDecoderTest { 21 | 22 | @Test 23 | public void shouldDecodeBigNumbers() throws CborException { 24 | BigInteger value = BigInteger.ONE; 25 | for (int i = 1; i < 64; i++) { 26 | value = value.shiftLeft(1); 27 | byte[] encodedBytes = CborEncoder.encodeToBytes(new CborBuilder().add(value).build()); 28 | ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes); 29 | CborDecoder decoder = new CborDecoder(bais); 30 | List dataItems = decoder.decode(); 31 | assertNotNull(dataItems); 32 | assertEquals(1, dataItems.size()); 33 | DataItem dataItem = dataItems.get(0); 34 | assertTrue(dataItem instanceof UnsignedInteger); 35 | assertEquals(value, ((UnsignedInteger) dataItem).getValue()); 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/encoder/ByteStringEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.encoder; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | 7 | import co.nstant.in.cbor.CborBuilder; 8 | import co.nstant.in.cbor.CborEncoder; 9 | import co.nstant.in.cbor.model.ByteString; 10 | import co.nstant.in.cbor.model.DataItem; 11 | 12 | public class ByteStringEncoderTest { 13 | 14 | @Test 15 | public void shouldEncodeNullString() { 16 | List dataItems = new CborBuilder().add((ByteString) null).build(); 17 | CborEncoder.encodeToBytes(dataItems); 18 | } 19 | 20 | @Test 21 | public void shouldEncodeChunkedString() { 22 | List dataItems = new CborBuilder().add(new ByteString(null)).add(new ByteString("test".getBytes())) 23 | .startByteString("test".getBytes()).end().build(); 24 | CborEncoder.encodeToBytes(dataItems); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/encoder/MapEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.encoder; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | 7 | import co.nstant.in.cbor.CborBuilder; 8 | import co.nstant.in.cbor.CborEncoder; 9 | import co.nstant.in.cbor.model.DataItem; 10 | 11 | public class MapEncoderTest { 12 | 13 | @Test 14 | public void shouldEncodeMap() { 15 | List dataItems = new CborBuilder().addMap().put(1, true).put(".", true).put(3, true).put("..", true) 16 | .put(2, true).put("...", true).end().build(); 17 | CborEncoder.encodeToBytes(dataItems); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/encoder/RationalNumberEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.encoder; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertTrue; 5 | 6 | import java.io.ByteArrayInputStream; 7 | 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborDecoder; 11 | import co.nstant.in.cbor.CborException; 12 | import co.nstant.in.cbor.model.Array; 13 | import co.nstant.in.cbor.model.RationalNumber; 14 | import co.nstant.in.cbor.model.Tag; 15 | import co.nstant.in.cbor.model.UnsignedInteger; 16 | 17 | public class RationalNumberEncoderTest { 18 | 19 | private static final UnsignedInteger ONE = new UnsignedInteger(1); 20 | private static final UnsignedInteger TWO = new UnsignedInteger(2); 21 | 22 | @Test 23 | public void shouldEncode() throws CborException { 24 | byte[] bytes = new RationalNumber(ONE, TWO).encodeToBytes(); 25 | ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 26 | CborDecoder decoder = new CborDecoder(inputStream); 27 | 28 | Array expected = new Array(); 29 | expected.setTag(11); 30 | expected.add(ONE); 31 | expected.add(TWO); 32 | 33 | Object object = decoder.decodeNext(); 34 | assertTrue(object instanceof Array); 35 | Array decoded = (Array) object; 36 | 37 | assertEquals(new Tag(30), decoded.getTag()); 38 | assertEquals(2, decoded.getDataItems().size()); 39 | assertEquals(ONE, decoded.getDataItems().get(0)); 40 | assertEquals(TWO, decoded.getDataItems().get(1)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/encoder/SpecialEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.encoder; 2 | 3 | import org.junit.Test; 4 | 5 | import co.nstant.in.cbor.model.Special; 6 | import co.nstant.in.cbor.model.SpecialType; 7 | 8 | public class SpecialEncoderTest { 9 | 10 | private class Mock extends Special { 11 | 12 | public Mock(SpecialType specialType) { 13 | super(specialType); 14 | } 15 | 16 | } 17 | 18 | @Test(expected = ClassCastException.class) 19 | public void shouldExpectDoublePrecisionFloatImplementation() { 20 | new Mock(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT).encodeToBytes(); 21 | } 22 | 23 | @Test(expected = ClassCastException.class) 24 | public void shouldExpectHalfPrecisionFloatImplementation() { 25 | new Mock(SpecialType.IEEE_754_HALF_PRECISION_FLOAT).encodeToBytes(); 26 | } 27 | 28 | @Test(expected = ClassCastException.class) 29 | public void shouldExpectSinglePrecisionFloatImplementation() { 30 | new Mock(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT).encodeToBytes(); 31 | } 32 | 33 | @Test(expected = ClassCastException.class) 34 | public void shouldExpectSimpleValueImplementation() { 35 | new Mock(SpecialType.SIMPLE_VALUE_NEXT_BYTE).encodeToBytes(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/encoder/UnicodeStringEncoderTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.encoder; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | 7 | import co.nstant.in.cbor.CborBuilder; 8 | import co.nstant.in.cbor.CborEncoder; 9 | import co.nstant.in.cbor.model.DataItem; 10 | 11 | public class UnicodeStringEncoderTest { 12 | 13 | @Test 14 | public void shouldEncodeNullString() { 15 | List dataItems = new CborBuilder().add((String) null).build(); 16 | CborEncoder.encodeToBytes(dataItems); 17 | } 18 | 19 | @Test 20 | public void shouldEncodeChunkedString() { 21 | List dataItems = new CborBuilder().startString("test").end().build(); 22 | CborEncoder.encodeToBytes(dataItems); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example01Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 0 -> 0x00 7 | */ 8 | public class Example01Test extends AbstractNumberTest { 9 | 10 | public Example01Test() { 11 | super(0, new byte[] { 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example02Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 0 -> 0x00 7 | */ 8 | public class Example02Test extends AbstractNumberTest { 9 | 10 | public Example02Test() { 11 | super(1, new byte[] { 0x01 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example03Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 10 -> 0x0a 7 | */ 8 | public class Example03Test extends AbstractNumberTest { 9 | 10 | public Example03Test() { 11 | super(10, new byte[] { 0x0a }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example04Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 23 -> 0x17 7 | */ 8 | public class Example04Test extends AbstractNumberTest { 9 | 10 | public Example04Test() { 11 | super(23, new byte[] { 0x17 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example05Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 24 -> 0x1818 7 | */ 8 | public class Example05Test extends AbstractNumberTest { 9 | 10 | public Example05Test() { 11 | super(24, new byte[] { 0x18, 0x18 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example06Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 25 -> 0x1819 7 | */ 8 | public class Example06Test extends AbstractNumberTest { 9 | 10 | public Example06Test() { 11 | super(25, new byte[] { 0x18, 0x19 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example07Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 100 -> 0x1864 7 | */ 8 | public class Example07Test extends AbstractNumberTest { 9 | 10 | public Example07Test() { 11 | super(100, new byte[] { 0x18, 0x64 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example08Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 1000 -> 0x1903e8 7 | */ 8 | public class Example08Test extends AbstractNumberTest { 9 | 10 | public Example08Test() { 11 | super(1000, new byte[] { 0x19, 0x03, (byte) 0xe8 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example09Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 1000000 -> 0x1a000f4240 7 | */ 8 | public class Example09Test extends AbstractNumberTest { 9 | 10 | public Example09Test() { 11 | super(1000000, new byte[] { 0x1a, 0x00, 0x0f, 0x42, 0x40 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example10Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * 1000000000000 -> 0x1b 00 00 00 e8 d4 a5 10 00 7 | */ 8 | public class Example10Test extends AbstractNumberTest { 9 | 10 | public Example10Test() { 11 | super(1000000000000L, new byte[] { 0x1b, 0x00, 0x00, 0x00, (byte) 0xe8, (byte) 0xd4, (byte) 0xa5, 0x10, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example11Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.math.BigInteger; 4 | 5 | import co.nstant.in.cbor.model.AbstractNumberTest; 6 | 7 | /** 8 | * 18446744073709551615 -> 0x1bffffffffffffffff 9 | */ 10 | public class Example11Test extends AbstractNumberTest { 11 | 12 | public Example11Test() { 13 | super(new BigInteger("18446744073709551615"), new byte[] { 0x1b, (byte) 0xff, (byte) 0xff, (byte) 0xff, 14 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example12Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.math.BigInteger; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.ByteString; 12 | import co.nstant.in.cbor.model.DataItem; 13 | import co.nstant.in.cbor.model.Tag; 14 | import co.nstant.in.cbor.model.UnsignedInteger; 15 | 16 | /** 17 | * 18446744073709551616 -> 0xc249010000000000000000 18 | */ 19 | public class Example12Test { 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 24 | new UnsignedInteger(new BigInteger("18446744073709551616")).encodeToBytes()); 25 | } 26 | 27 | @Test 28 | public void shouldDecode() throws CborException { 29 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( 30 | new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); 31 | CborDecoder decoder = new CborDecoder(byteArrayInputStream); 32 | DataItem b = decoder.decodeNext(); 33 | 34 | Assert.assertTrue(b.hasTag()); 35 | Tag tag = b.getTag(); 36 | Assert.assertEquals(2, tag.getValue()); 37 | 38 | Assert.assertTrue(b instanceof ByteString); 39 | ByteString byteString = (ByteString) b; 40 | Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 41 | byteString.getBytes()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example13Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.math.BigInteger; 4 | 5 | import co.nstant.in.cbor.model.AbstractNumberTest; 6 | 7 | /** 8 | * -18446744073709551616 -> 0x3bffffffffffffffff 9 | */ 10 | public class Example13Test extends AbstractNumberTest { 11 | 12 | public Example13Test() { 13 | super(new BigInteger("-18446744073709551616"), new byte[] { (byte) 0x3b, (byte) 0xff, (byte) 0xff, (byte) 0xff, 14 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example14Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.math.BigInteger; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.ByteString; 12 | import co.nstant.in.cbor.model.DataItem; 13 | import co.nstant.in.cbor.model.NegativeInteger; 14 | import co.nstant.in.cbor.model.Tag; 15 | 16 | /** 17 | * -18446744073709551617 -> 0xc349010000000000000000 18 | */ 19 | public class Example14Test { 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 24 | new NegativeInteger(new BigInteger("-18446744073709551617")).encodeToBytes()); 25 | } 26 | 27 | @Test 28 | public void shouldDecode() throws CborException { 29 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( 30 | new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); 31 | CborDecoder decoder = new CborDecoder(byteArrayInputStream); 32 | DataItem b = decoder.decodeNext(); 33 | 34 | Assert.assertTrue(b.hasTag()); 35 | Tag tag = b.getTag(); 36 | Assert.assertEquals(3, tag.getValue()); 37 | 38 | Assert.assertTrue(b instanceof ByteString); 39 | ByteString byteString = (ByteString) b; 40 | Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 41 | byteString.getBytes()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example15Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * -1 -> 0x20 7 | */ 8 | public class Example15Test extends AbstractNumberTest { 9 | 10 | public Example15Test() { 11 | super(-1, new byte[] { 0x20 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example16Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * -10 -> 0x29 7 | */ 8 | public class Example16Test extends AbstractNumberTest { 9 | 10 | public Example16Test() { 11 | super(-10, new byte[] { 0x29 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example17Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * -100 -> 0x3863 7 | */ 8 | public class Example17Test extends AbstractNumberTest { 9 | 10 | public Example17Test() { 11 | super(-100, new byte[] { 0x38, 0x63 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example18Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractNumberTest; 4 | 5 | /** 6 | * -1000 -> 0x3903e7 7 | */ 8 | public class Example18Test extends AbstractNumberTest { 9 | 10 | public Example18Test() { 11 | super(-1000, new byte[] { 0x39, 0x03, (byte) 0xe7 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example19Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * 0.0 -> 0xf90000 7 | */ 8 | public class Example19Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example19Test() { 11 | super(0.0f, new byte[] { (byte) 0xf9, 0x00, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example20Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * Example 20: -0.0 -> 0xf98000 7 | */ 8 | public class Example20Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example20Test() { 11 | super(-0.0f, new byte[] { (byte) 0xf9, (byte) 0x80, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example21Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * Example 21: 1.0 -> 0xf93c00 7 | */ 8 | public class Example21Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example21Test() { 11 | super(1.0f, new byte[] { (byte) 0xf9, 0x3c, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example22Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * Example 22: 1.1 -> 0xfb 3f f1 99 99 99 99 99 9a 7 | */ 8 | public class Example22Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example22Test() { 11 | super(1.1d, new byte[] { (byte) 0xfb, 0x3f, (byte) 0xf1, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, 12 | (byte) 0x99, (byte) 0x9a }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example23Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * 1.5 -> 0xf93e00 7 | */ 8 | public class Example23Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example23Test() { 11 | super(1.5f, new byte[] { (byte) 0xf9, 0x3e, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example24Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * 65504.0 -> 0xf97bff 7 | */ 8 | public class Example24Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example24Test() { 11 | super(65504.0f, new byte[] { (byte) 0xf9, 0x7b, (byte) 0xff }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example25Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractSinglePrecisionFloatTest; 4 | 5 | /** 6 | * 100000.0 -> 0xfa47c35000 7 | */ 8 | public class Example25Test extends AbstractSinglePrecisionFloatTest { 9 | 10 | public Example25Test() { 11 | super(100000.0f, new byte[] { (byte) 0xfa, 0x47, (byte) 0xc3, 0x50, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example26Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractSinglePrecisionFloatTest; 4 | 5 | /** 6 | * 3.4028234663852886e+38 -> 0xfa 7f 7f ff ff 7 | */ 8 | public class Example26Test extends AbstractSinglePrecisionFloatTest { 9 | 10 | public Example26Test() { 11 | super(Float.parseFloat("3.4028234663852886e+38"), 12 | new byte[] { (byte) 0xfa, 0x7f, (byte) 0x7f, (byte) 0xff, (byte) 0xff }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example27Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * 1.0e+300 -> 0xfb 7e 37 e4 3c 88 00 75 9c 7 | */ 8 | public class Example27Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example27Test() { 11 | super(Double.parseDouble("1.0e+300"), 12 | new byte[] { (byte) 0xfb, 0x7e, 0x37, (byte) 0xe4, 0x3c, (byte) 0x88, 0x00, 0x75, (byte) 0x9c }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example28Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * 5.960464477539063e-08 -> 0xf90001 7 | */ 8 | public class Example28Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example28Test() { 11 | super(Float.parseFloat("5.960464477539063e-08"), new byte[] { (byte) 0xf9, 0x00, 0x01 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example29Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * 6.103515625e-05 -> 0xf90400 7 | */ 8 | public class Example29Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example29Test() { 11 | super(Float.parseFloat("6.103515625e-05"), new byte[] { (byte) 0xf9, 0x04, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example30Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * -4,0 -> 0xf9c400 7 | */ 8 | public class Example30Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example30Test() { 11 | super(Float.parseFloat("-4.0"), new byte[] { (byte) 0xf9, (byte) 0xc4, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example31Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * -4.1 -> 0xfb c0 10 66 66 66 66 66 66 7 | */ 8 | public class Example31Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example31Test() { 11 | super(Double.parseDouble("-4.1"), 12 | new byte[] { (byte) 0xfb, (byte) 0xc0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example32Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * Infinity -> 0xf97c00 7 | */ 8 | public class Example32Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example32Test() { 11 | super(Float.POSITIVE_INFINITY, new byte[] { (byte) 0xf9, 0x7c, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example33Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * NaN -> 0xf97e00 7 | */ 8 | public class Example33Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example33Test() { 11 | super(Float.NaN, new byte[] { (byte) 0xf9, 0x7e, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example34Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractHalfPrecisionFloatTest; 4 | 5 | /** 6 | * -Infinity -> 0xf9fc00 7 | */ 8 | public class Example34Test extends AbstractHalfPrecisionFloatTest { 9 | 10 | public Example34Test() { 11 | super(Float.NEGATIVE_INFINITY, new byte[] { (byte) 0xf9, (byte) 0xfc, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example35Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractSinglePrecisionFloatTest; 4 | 5 | /** 6 | * Infinity -> 0xfa 7f 80 00 00 7 | */ 8 | public class Example35Test extends AbstractSinglePrecisionFloatTest { 9 | 10 | public Example35Test() { 11 | super(Float.POSITIVE_INFINITY, new byte[] { (byte) 0xfa, 0x7f, (byte) 0x80, 0x00, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example36Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractSinglePrecisionFloatTest; 4 | 5 | /** 6 | * NaN -> 0xfa7fc00000 7 | */ 8 | public class Example36Test extends AbstractSinglePrecisionFloatTest { 9 | 10 | public Example36Test() { 11 | super(Float.NaN, new byte[] { (byte) 0xfa, 0x7f, (byte) 0xc0, 0x00, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example37Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractSinglePrecisionFloatTest; 4 | 5 | /** 6 | * -Infinity -> 0xfa ff 80 00 00 7 | */ 8 | public class Example37Test extends AbstractSinglePrecisionFloatTest { 9 | 10 | public Example37Test() { 11 | super(Float.NEGATIVE_INFINITY, new byte[] { (byte) 0xfa, (byte) 0xff, (byte) 0x80, 0x00, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example38Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * Infinity -> 0xfb 7f f0 00 00 00 00 00 00 7 | */ 8 | public class Example38Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example38Test() { 11 | super(Double.POSITIVE_INFINITY, 12 | new byte[] { (byte) 0xfb, 0x7f, (byte) 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example39Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * NaN -> 0xfb 7f f8 00 00 00 00 00 00 7 | */ 8 | public class Example39Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example39Test() { 11 | super(Double.NaN, new byte[] { (byte) 0xfb, 0x7f, (byte) 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example40Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractDoublePrecisionFloatTest; 4 | 5 | /** 6 | * -Infinity -> 0xfb ff f0 00 00 00 00 00 00 7 | */ 8 | public class Example40Test extends AbstractDoublePrecisionFloatTest { 9 | 10 | public Example40Test() { 11 | super(Double.NEGATIVE_INFINITY, 12 | new byte[] { (byte) 0xfb, (byte) 0xff, (byte) 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example41Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * false -> 0xf4 16 | */ 17 | public class Example41Test { 18 | 19 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf4 }; 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.FALSE.encodeToBytes()); 24 | } 25 | 26 | @Test 27 | public void shouldDecode() throws CborException { 28 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 29 | CborDecoder decoder = new CborDecoder(inputStream); 30 | DataItem dataItem = decoder.decodeNext(); 31 | Assert.assertTrue(dataItem instanceof SimpleValue); 32 | SimpleValue simpleValue = (SimpleValue) dataItem; 33 | Assert.assertEquals(SimpleValue.FALSE, simpleValue); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example42Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * true -> 0xf5 16 | */ 17 | public class Example42Test { 18 | 19 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf5 }; 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.TRUE.encodeToBytes()); 24 | } 25 | 26 | @Test 27 | public void shouldDecode() throws CborException { 28 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 29 | CborDecoder decoder = new CborDecoder(inputStream); 30 | DataItem dataItem = decoder.decodeNext(); 31 | Assert.assertTrue(dataItem instanceof SimpleValue); 32 | SimpleValue simpleValue = (SimpleValue) dataItem; 33 | Assert.assertEquals(SimpleValue.TRUE, simpleValue); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example43Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * nil -> 0xf6 16 | */ 17 | public class Example43Test { 18 | 19 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf6 }; 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.NULL.encodeToBytes()); 24 | } 25 | 26 | @Test 27 | public void shouldDecode() throws CborException { 28 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 29 | CborDecoder decoder = new CborDecoder(inputStream); 30 | DataItem dataItem = decoder.decodeNext(); 31 | Assert.assertTrue(dataItem instanceof SimpleValue); 32 | SimpleValue simpleValue = (SimpleValue) dataItem; 33 | Assert.assertEquals(SimpleValue.NULL, simpleValue); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example44Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * undefined -> 0xf7 16 | */ 17 | public class Example44Test { 18 | 19 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf7 }; 20 | 21 | @Test 22 | public void shouldEncode() { 23 | Assert.assertArrayEquals(ENCODED_VALUE, SimpleValue.UNDEFINED.encodeToBytes()); 24 | } 25 | 26 | @Test 27 | public void shouldDecode() throws CborException { 28 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 29 | CborDecoder decoder = new CborDecoder(inputStream); 30 | DataItem dataItem = decoder.decodeNext(); 31 | Assert.assertTrue(dataItem instanceof SimpleValue); 32 | SimpleValue simpleValue = (SimpleValue) dataItem; 33 | Assert.assertEquals(SimpleValue.UNDEFINED, simpleValue); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example45Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * simple(16) -> 0xf0 16 | */ 17 | public class Example45Test { 18 | 19 | private static final SimpleValue VALUE = new SimpleValue(16); 20 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf0 }; 21 | 22 | @Test 23 | public void shouldEncode() { 24 | Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); 25 | } 26 | 27 | @Test 28 | public void shouldDecode() throws CborException { 29 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 30 | CborDecoder decoder = new CborDecoder(inputStream); 31 | DataItem dataItem = decoder.decodeNext(); 32 | Assert.assertTrue(dataItem instanceof SimpleValue); 33 | SimpleValue simpleValue = (SimpleValue) dataItem; 34 | Assert.assertEquals(VALUE, simpleValue); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example46Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * simple(24) -> 0xf818 16 | */ 17 | public class Example46Test { 18 | 19 | private static final SimpleValue VALUE = new SimpleValue(24); 20 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf8, 0x18 }; 21 | 22 | @Test 23 | public void shouldEncode() { 24 | Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); 25 | } 26 | 27 | @Test 28 | public void shouldDecode() throws CborException { 29 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 30 | CborDecoder decoder = new CborDecoder(inputStream); 31 | DataItem dataItem = decoder.decodeNext(); 32 | Assert.assertTrue(dataItem instanceof SimpleValue); 33 | SimpleValue simpleValue = (SimpleValue) dataItem; 34 | Assert.assertEquals(VALUE, simpleValue); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example47Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SimpleValue; 13 | 14 | /** 15 | * simple(255) -> 0xf8ff 16 | */ 17 | public class Example47Test { 18 | 19 | private static final SimpleValue VALUE = new SimpleValue(255); 20 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf8, (byte) 0xff }; 21 | 22 | @Test 23 | public void shouldEncode() { 24 | Assert.assertArrayEquals(ENCODED_VALUE, VALUE.encodeToBytes()); 25 | } 26 | 27 | @Test 28 | public void shouldDecode() throws CborException { 29 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 30 | CborDecoder decoder = new CborDecoder(inputStream); 31 | DataItem dataItem = decoder.decodeNext(); 32 | Assert.assertTrue(dataItem instanceof SimpleValue); 33 | SimpleValue simpleValue = (SimpleValue) dataItem; 34 | Assert.assertEquals(VALUE, simpleValue); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example48Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.UnicodeString; 16 | 17 | /** 18 | * 0("2013-03-21T20:04:00Z") -> 0xc074323031332d30332d32315432303a30343a30305a 19 | */ 20 | public class Example48Test { 21 | 22 | private final List VALUE; 23 | 24 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 25 | 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30, 0x5a }; 26 | 27 | public Example48Test() { 28 | DataItem di = new UnicodeString("2013-03-21T20:04:00Z"); 29 | di.setTag(0); 30 | 31 | VALUE = new CborBuilder().add(di).build(); 32 | } 33 | 34 | @Test 35 | public void shouldEncode() { 36 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 37 | } 38 | 39 | @Test 40 | public void shouldDecode() throws CborException { 41 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 42 | CborDecoder decoder = new CborDecoder(inputStream); 43 | List dataItems = decoder.decode(); 44 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example49Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.UnsignedInteger; 16 | 17 | /** 18 | * 1(1363896240) -> 0xc11a514b67b0 19 | */ 20 | public class Example49Test { 21 | 22 | private final List VALUE; 23 | 24 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xc1, 0x1a, 0x51, 0x4b, 0x67, (byte) 0xb0 }; 25 | 26 | public Example49Test() { 27 | DataItem di = new UnsignedInteger(1363896240); 28 | di.setTag(1); 29 | 30 | VALUE = new CborBuilder().add(di).build(); 31 | } 32 | 33 | @Test 34 | public void shouldEncode() { 35 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 36 | } 37 | 38 | @Test 39 | public void shouldDecode() throws CborException { 40 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 41 | CborDecoder decoder = new CborDecoder(inputStream); 42 | List dataItems = decoder.decode(); 43 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example50Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.DoublePrecisionFloat; 16 | 17 | /** 18 | * 1(1363896240.5) -> 0xc1fb41d452d9ec200000 19 | */ 20 | public class Example50Test { 21 | 22 | private final List VALUE; 23 | 24 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xc1, (byte) 0xfb, 0x41, (byte) 0xd4, 0x52, (byte) 0xd9, 25 | (byte) 0xec, 0x20, 0x00, 0x00 }; 26 | 27 | public Example50Test() { 28 | DataItem di = new DoublePrecisionFloat(1363896240.5); 29 | di.setTag(1); 30 | 31 | VALUE = new CborBuilder().add(di).build(); 32 | } 33 | 34 | @Test 35 | public void shouldEncode() { 36 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 37 | } 38 | 39 | @Test 40 | public void shouldDecode() throws CborException { 41 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 42 | CborDecoder decoder = new CborDecoder(inputStream); 43 | List dataItems = decoder.decode(); 44 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example51Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.ByteString; 15 | import co.nstant.in.cbor.model.DataItem; 16 | 17 | /** 18 | * 23(h'01020304') -> 0xd74401020304 19 | */ 20 | public class Example51Test { 21 | 22 | private final List VALUE; 23 | 24 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xd7, 0x44, 0x01, 0x02, 0x03, 0x04 }; 25 | 26 | public Example51Test() { 27 | DataItem di = new ByteString(new byte[] { 0x01, 0x02, 0x03, 0x04 }); 28 | di.setTag(23); 29 | 30 | VALUE = new CborBuilder().add(di).build(); 31 | } 32 | 33 | @Test 34 | public void shouldEncode() { 35 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 36 | } 37 | 38 | @Test 39 | public void shouldDecode() throws CborException { 40 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 41 | CborDecoder decoder = new CborDecoder(inputStream); 42 | List dataItems = decoder.decode(); 43 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example52Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.ByteString; 15 | import co.nstant.in.cbor.model.DataItem; 16 | 17 | /** 18 | * 24(h'6449455446') -> 0xd818456449455446 19 | */ 20 | public class Example52Test { 21 | 22 | private final List VALUE; 23 | 24 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xd8, 0x18, 0x45, 0x64, 0x49, 0x45, 0x54, 0x46 }; 25 | 26 | public Example52Test() { 27 | DataItem di = new ByteString(new byte[] { 0x64, 0x49, 0x45, 0x54, 0x46 }); 28 | di.setTag(24); 29 | 30 | VALUE = new CborBuilder().add(di).build(); 31 | } 32 | 33 | @Test 34 | public void shouldEncode() { 35 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 36 | } 37 | 38 | @Test 39 | public void shouldDecode() throws CborException { 40 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 41 | CborDecoder decoder = new CborDecoder(inputStream); 42 | List dataItems = decoder.decode(); 43 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example53Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.UnicodeString; 16 | 17 | /** 18 | * 32("http://www.example.com") -> 19 | * 0xd82076687474703a2f2f7777772e6578616d706c652e636f6d 20 | */ 21 | public class Example53Test { 22 | 23 | private final List VALUE; 24 | 25 | private final byte[] ENCODED_VALUE = new byte[] { (byte) 0xd8, 0x20, 0x76, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 26 | 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d }; 27 | 28 | public Example53Test() { 29 | DataItem di = new UnicodeString("http://www.example.com"); 30 | di.setTag(32); 31 | 32 | VALUE = new CborBuilder().add(di).build(); 33 | } 34 | 35 | @Test 36 | public void shouldEncode() { 37 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 38 | } 39 | 40 | @Test 41 | public void shouldDecode() throws CborException { 42 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 43 | CborDecoder decoder = new CborDecoder(inputStream); 44 | List dataItems = decoder.decode(); 45 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example54Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractByteStringTest; 4 | 5 | /** 6 | * h'' -> 0x40 7 | */ 8 | public class Example54Test extends AbstractByteStringTest { 9 | 10 | public Example54Test() { 11 | super(new byte[] {}, new byte[] { 0x40 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example55Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractByteStringTest; 4 | 5 | /** 6 | * h'01020304' -> 0x4401020304 7 | */ 8 | public class Example55Test extends AbstractByteStringTest { 9 | 10 | public Example55Test() { 11 | super(new byte[] { 0x01, 0x02, 0x03, 0x04 }, new byte[] { 0x44, 0x01, 0x02, 0x03, 0x04 }); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example56Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "" -> 0x60 7 | */ 8 | public class Example56Test extends AbstractStringTest { 9 | 10 | public Example56Test() { 11 | super("", new byte[] { 0x60 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example57Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "a" -> 0x6161 7 | */ 8 | public class Example57Test extends AbstractStringTest { 9 | 10 | public Example57Test() { 11 | super("a", new byte[] { 0x61, 0x61 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example58Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "IETF" -> 0x64 49 45 54 46 7 | */ 8 | public class Example58Test extends AbstractStringTest { 9 | 10 | public Example58Test() { 11 | super("IETF", new byte[] { 0x64, 0x49, 0x45, 0x54, 0x46 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example59Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "\"\\" -> 0x62225c 7 | */ 8 | public class Example59Test extends AbstractStringTest { 9 | 10 | public Example59Test() { 11 | super("\"\\", new byte[] { 0x62, 0x22, 0x5c }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example60Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "\u00fc" -> 0x62c3bc 7 | */ 8 | public class Example60Test extends AbstractStringTest { 9 | 10 | public Example60Test() { 11 | super("\u00fc", new byte[] { 0x62, (byte) 0xc3, (byte) 0xbc }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example61Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "\u6c34" -> 0x63e6b0b4 7 | */ 8 | public class Example61Test extends AbstractStringTest { 9 | 10 | public Example61Test() { 11 | super("\u6c34", new byte[] { 0x63, (byte) 0xe6, (byte) 0xb0, (byte) 0xb4 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example62Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.model.AbstractStringTest; 4 | 5 | /** 6 | * "\ud800\udd51" -> 0x64f0908591 7 | */ 8 | public class Example62Test extends AbstractStringTest { 9 | 10 | public Example62Test() { 11 | super("\ud800\udd51", new byte[] { 0x64, (byte) 0xf0, (byte) 0x90, (byte) 0x85, (byte) 0x91 }); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example63Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborBuilder; 10 | import co.nstant.in.cbor.CborDecoder; 11 | import co.nstant.in.cbor.CborEncoder; 12 | import co.nstant.in.cbor.CborException; 13 | import co.nstant.in.cbor.model.Array; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * [] -> 0x80 18 | */ 19 | public class Example63Test { 20 | 21 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x80 }; 22 | 23 | @Test 24 | public void shouldEncode() { 25 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes( 26 | new CborBuilder().addArray().end().build())); 27 | } 28 | 29 | @Test 30 | public void shouldDecode() throws CborException { 31 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 32 | CborDecoder decoder = new CborDecoder(inputStream); 33 | DataItem dataItem = decoder.decodeNext(); 34 | Assert.assertTrue(dataItem instanceof Array); 35 | Array array = (Array) dataItem; 36 | Assert.assertTrue(array.getDataItems().isEmpty()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example64Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborBuilder; 10 | import co.nstant.in.cbor.CborDecoder; 11 | import co.nstant.in.cbor.CborEncoder; 12 | import co.nstant.in.cbor.CborException; 13 | import co.nstant.in.cbor.model.Array; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.Number; 16 | 17 | /** 18 | * [1, 2, 3] -> 0x83010203 19 | */ 20 | public class Example64Test { 21 | 22 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x83, 0x01, 0x02, 0x03 }; 23 | 24 | @Test 25 | public void shouldEncode() { 26 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes( 27 | new CborBuilder().addArray().add(1).add(2).add(3).end().build())); 28 | } 29 | 30 | @Test 31 | public void shouldDecode() throws CborException { 32 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 33 | CborDecoder decoder = new CborDecoder(inputStream); 34 | DataItem dataItem = decoder.decodeNext(); 35 | Assert.assertTrue(dataItem instanceof Array); 36 | Array array = (Array) dataItem; 37 | Assert.assertEquals(3, array.getDataItems().size()); 38 | Assert.assertEquals(1, ((Number) array.getDataItems().get(0)).getValue().intValue()); 39 | Assert.assertEquals(2, ((Number) array.getDataItems().get(1)).getValue().intValue()); 40 | Assert.assertEquals(3, ((Number) array.getDataItems().get(2)).getValue().intValue()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example65Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.Array; 15 | import co.nstant.in.cbor.model.DataItem; 16 | import co.nstant.in.cbor.model.Number; 17 | 18 | /** 19 | * [1, [2, 3], [4, 5]] -> 0x83 01 82 02 03 82 04 05 20 | */ 21 | public class Example65Test { 22 | 23 | private static final List VALUE = new CborBuilder().addArray().add(1).addArray().add(2).add(3).end() 24 | .addArray().add(4).add(5).end().end().build(); 25 | 26 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x83, 0x01, (byte) 0x82, 0x02, 0x03, (byte) 0x82, 27 | 0x04, 0x05 }; 28 | 29 | @Test 30 | public void shouldEncode() { 31 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 32 | } 33 | 34 | @Test 35 | public void shouldDecode() throws CborException { 36 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 37 | CborDecoder decoder = new CborDecoder(inputStream); 38 | DataItem dataItem = decoder.decodeNext(); 39 | Assert.assertTrue(dataItem instanceof Array); 40 | Array array = (Array) dataItem; 41 | Assert.assertEquals(3, array.getDataItems().size()); 42 | 43 | DataItem dataItem1 = array.getDataItems().get(0); 44 | DataItem dataItem2 = array.getDataItems().get(1); 45 | DataItem dataItem3 = array.getDataItems().get(2); 46 | 47 | Assert.assertTrue(dataItem1 instanceof Number); 48 | Assert.assertTrue(dataItem2 instanceof Array); 49 | Assert.assertTrue(dataItem3 instanceof Array); 50 | 51 | Number number = (Number) dataItem1; 52 | Array array1 = (Array) dataItem2; 53 | Array array2 = (Array) dataItem3; 54 | 55 | Assert.assertEquals(1, number.getValue().intValue()); 56 | Assert.assertEquals(2, array1.getDataItems().size()); 57 | Assert.assertEquals(2, array2.getDataItems().size()); 58 | 59 | DataItem array1item1 = array1.getDataItems().get(0); 60 | DataItem array1item2 = array1.getDataItems().get(1); 61 | 62 | Assert.assertTrue(array1item1 instanceof Number); 63 | Assert.assertTrue(array1item2 instanceof Number); 64 | 65 | Number array1number1 = (Number) array1item1; 66 | Number array1number2 = (Number) array1item2; 67 | 68 | Assert.assertEquals(2, array1number1.getValue().intValue()); 69 | Assert.assertEquals(3, array1number2.getValue().intValue()); 70 | 71 | DataItem array2item1 = array2.getDataItems().get(0); 72 | DataItem array2item2 = array2.getDataItems().get(1); 73 | 74 | Assert.assertTrue(array2item1 instanceof Number); 75 | Assert.assertTrue(array2item2 instanceof Number); 76 | 77 | Number array2number1 = (Number) array2item1; 78 | Number array2number2 = (Number) array2item2; 79 | 80 | Assert.assertEquals(4, array2number1.getValue().intValue()); 81 | Assert.assertEquals(5, array2number2.getValue().intValue()); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example66Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * [1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17,18, 19, 20, 21, 22, 18 | * 23, 24, 25] -> 0x98 190102030405060708090a0b0c0d0e0f101112131415161718181819 19 | */ 20 | public class Example66Test { 21 | 22 | private static final List VALUE = new CborBuilder().addArray().add(1).add(2).add(3).add(4).add(5).add(6) 23 | .add(7).add(8).add(9).add(10).add(11).add(12).add(13).add(14).add(15).add(16).add(17).add(18).add(19).add(20) 24 | .add(21).add(22).add(23).add(24).add(25).end().build(); 25 | 26 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x98, 0x19, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 27 | 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 28 | 0x18, 0x18, 0x19 }; 29 | 30 | @Test 31 | public void shouldEncode() { 32 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 33 | } 34 | 35 | @Test 36 | public void shouldDecode() throws CborException { 37 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 38 | CborDecoder decoder = new CborDecoder(inputStream); 39 | List dataItems = decoder.decode(); 40 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example67Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {} -> 0xa0 18 | */ 19 | public class Example67Test { 20 | 21 | private static final List VALUE = new CborBuilder().addMap().end().build(); 22 | 23 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa0 }; 24 | 25 | @Test 26 | public void shouldEncode() { 27 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 28 | } 29 | 30 | @Test 31 | public void shouldDecode() throws CborException { 32 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 33 | CborDecoder decoder = new CborDecoder(inputStream); 34 | List dataItems = decoder.decode(); 35 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example68Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {1: 2, 3: 4} -> 0xa201020304 18 | */ 19 | public class Example68Test { 20 | 21 | private static final List VALUE = new CborBuilder().addMap().put(1, 2).put(3, 4).end().build(); 22 | 23 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa2, 0x01, 0x02, 0x03, 0x04 }; 24 | 25 | @Test 26 | public void shouldEncode() { 27 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 28 | } 29 | 30 | @Test 31 | public void shouldDecode() throws CborException { 32 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 33 | CborDecoder decoder = new CborDecoder(inputStream); 34 | List dataItems = decoder.decode(); 35 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example69Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {"a": 1, "b": [2,3]} -> 0xa26161016162820203 18 | */ 19 | public class Example69Test { 20 | 21 | private static final List VALUE = new CborBuilder().addMap().put("a", 1).putArray("b").add(2).add(3).end() 22 | .end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, (byte) 0x82, 25 | 0x02, 0x03 }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example70Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * ["a", {"b": "c"}] -> 0x826161a161626163 18 | */ 19 | public class Example70Test { 20 | 21 | private static final List VALUE = new CborBuilder().addArray().add("a").addMap().put("b", "c").end().end() 22 | .build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x82, 0x61, 0x61, (byte) 0xa1, 0x61, 0x62, 0x61, 25 | 0x63 }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example71Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {"a": "A", "b": "B", "c": "C", "d": "D","e": "E"} -> 18 | * 0xa56161614161626142616361436164614461656145 19 | */ 20 | public class Example71Test { 21 | 22 | private static final List VALUE = new CborBuilder().addMap().put("a", "A").put("b", "B").put("c", "C") 23 | .put("d", "D").put("e", "E").end().build(); 24 | 25 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, 26 | 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, 0x44, 0x61, 0x65, 0x61, 0x45 }; 27 | 28 | @Test 29 | public void shouldEncode() { 30 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 31 | } 32 | 33 | @Test 34 | public void shouldDecode() throws CborException { 35 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 36 | CborDecoder decoder = new CborDecoder(inputStream); 37 | List dataItems = decoder.decode(); 38 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example72Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * (_ h'0102', h'030405') -> 0x5f42010243030405ff 18 | */ 19 | public class Example72Test { 20 | 21 | private static final List VALUE = new CborBuilder().startByteString().add(new byte[] { 0x01, 0x02 }) 22 | .add(new byte[] { 0x03, 0x04, 0x05 }).end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 25 | (byte) 0xff }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | decoder.setAutoDecodeInfinitiveByteStrings(false); 37 | List dataItems = decoder.decode(); 38 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example73Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * (_ "strea", "ming") -> 0x7f657374726561646d696e67ff 18 | */ 19 | public class Example73Test { 20 | 21 | private static final List VALUE = new CborBuilder().startString().add("strea").add("ming").end().build(); 22 | 23 | private static final byte[] ENCODED_VALUE = new byte[] { 0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 24 | 0x6e, 0x67, (byte) 0xff }; 25 | 26 | @Test 27 | public void shouldEncode() { 28 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 29 | } 30 | 31 | @Test 32 | public void shouldDecode() throws CborException { 33 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 34 | CborDecoder decoder = new CborDecoder(inputStream); 35 | decoder.setAutoDecodeInfinitiveUnicodeStrings(false); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example74Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.Array; 15 | import co.nstant.in.cbor.model.DataItem; 16 | import co.nstant.in.cbor.model.SimpleValue; 17 | 18 | /** 19 | * [_ ] -> 0x9fff 20 | */ 21 | public class Example74Test { 22 | 23 | private static final List VALUE = new CborBuilder().add(new Array().setChunked(true)) 24 | .add(SimpleValue.BREAK).build(); 25 | 26 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x9f, (byte) 0xff }; 27 | 28 | @Test 29 | public void shouldEncode() { 30 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 31 | } 32 | 33 | @Test 34 | public void shouldDecode() throws CborException { 35 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 36 | CborDecoder decoder = new CborDecoder(inputStream); 37 | decoder.setAutoDecodeInfinitiveArrays(false); 38 | List dataItems = decoder.decode(); 39 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example75Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.Array; 15 | import co.nstant.in.cbor.model.DataItem; 16 | import co.nstant.in.cbor.model.SimpleValue; 17 | 18 | /** 19 | * [_ 1, [2, 3], [_ 4, 5]] -> 0x9f018202039f0405ffff 20 | */ 21 | public class Example75Test { 22 | 23 | private static final List VALUE = new CborBuilder().add(new Array().setChunked(true)).add(1).addArray() 24 | .add(2).add(3).end().add(new Array().setChunked(true)).add(4).add(5).add(SimpleValue.BREAK) 25 | .add(SimpleValue.BREAK).build(); 26 | 27 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x9f, 0x01, (byte) 0x82, 0x02, 0x03, (byte) 0x9f, 28 | 0x04, 0x05, (byte) 0xff, (byte) 0xff }; 29 | 30 | @Test 31 | public void shouldEncode() { 32 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 33 | } 34 | 35 | @Test 36 | public void shouldDecode() throws CborException { 37 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 38 | CborDecoder decoder = new CborDecoder(inputStream); 39 | decoder.setAutoDecodeInfinitiveArrays(false); 40 | List dataItems = decoder.decode(); 41 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example76Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.Array; 15 | import co.nstant.in.cbor.model.DataItem; 16 | import co.nstant.in.cbor.model.SimpleValue; 17 | 18 | /** 19 | * [_ 1, [2, 3], [4,5]] -> 0x9f01820203820405ff 20 | */ 21 | public class Example76Test { 22 | 23 | private static final List VALUE = new CborBuilder().add(new Array().setChunked(true)).add(1).addArray() 24 | .add(2).add(3).end().addArray().add(4).add(5).end().add(SimpleValue.BREAK).build(); 25 | 26 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x9f, 0x01, (byte) 0x82, 0x02, 0x03, (byte) 0x82, 27 | 0x04, 0x05, (byte) 0xff }; 28 | 29 | @Test 30 | public void shouldEncode() { 31 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 32 | } 33 | 34 | @Test 35 | public void shouldDecode() throws CborException { 36 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 37 | CborDecoder decoder = new CborDecoder(inputStream); 38 | decoder.setAutoDecodeInfinitiveArrays(false); 39 | List dataItems = decoder.decode(); 40 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example77Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * [1, [2, 3], [_ 4,5]] -> 0x83 01 82 02 03 9f 04 05 ff 18 | */ 19 | public class Example77Test { 20 | 21 | private static final List VALUE = new CborBuilder().addArray().add(1).addArray().add(2).add(3).end() 22 | .startArray().add(4).add(5).end().end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x83, 0x01, (byte) 0x82, 0x02, 0x03, (byte) 0x9f, 25 | 0x04, 0x05, (byte) 0xff }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example78Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * [1, [_ 2, 3], [4,5]] -> 0x83019f0203ff820405 18 | */ 19 | public class Example78Test { 20 | 21 | private static final List VALUE = new CborBuilder().addArray().add(1).startArray().add(2).add(3).end() 22 | .addArray().add(4).add(5).end().end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x83, 0x01, (byte) 0x9f, 0x02, 0x03, (byte) 0xff, 25 | (byte) 0x82, 0x04, 0x05 }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example79Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * [_ 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12,13, 14, 15, 16, 17,18, 19, 20, 21, 18 | * 22,23, 24, 25] -> 19 | * 0x9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff 20 | */ 21 | public class Example79Test { 22 | 23 | private static final List VALUE = new CborBuilder().startArray().add(1).add(2).add(3).add(4).add(5).add(6) 24 | .add(7).add(8).add(9).add(10).add(11).add(12).add(13).add(14).add(15).add(16).add(17).add(18).add(19).add(20) 25 | .add(21).add(22).add(23).add(24).add(25).end().build(); 26 | 27 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x9f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 28 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x18, 29 | 0x18, 0x19, (byte) 0xff }; 30 | 31 | @Test 32 | public void shouldEncode() { 33 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 34 | } 35 | 36 | @Test 37 | public void shouldDecode() throws CborException { 38 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 39 | CborDecoder decoder = new CborDecoder(inputStream); 40 | List dataItems = decoder.decode(); 41 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example80Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {_ "a": 1, "b": [_ 2, 3]} -> 0xbf61610161629f0203ffff 18 | */ 19 | public class Example80Test { 20 | 21 | private static final List VALUE = new CborBuilder().startMap().put("a", 1).put("b", 2).startArray("b") 22 | .add(2).add(3).end().end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xbf, 0x61, 0x61, 0x01, 0x61, 0x62, (byte) 0x9f, 25 | 0x02, 0x03, (byte) 0xff, (byte) 0xff }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example81Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * ["a", {_ "b": "c"}] -> 0x826161bf61626163ff 18 | */ 19 | public class Example81Test { 20 | 21 | private static final List VALUE = new CborBuilder().addArray().add("a").startMap().put("b", "c").end() 22 | .end().build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0x82, 0x61, 0x61, (byte) 0xbf, 0x61, 0x62, 0x61, 25 | 0x63, (byte) 0xff }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example82Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborEncoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | 16 | /** 17 | * {_ "Fun": true, "Amt": -2} -> 0xbf6346756ef563416d7421ff 18 | */ 19 | public class Example82Test { 20 | 21 | private static final List VALUE = new CborBuilder().startMap().put("Fun", true).put("Amt", -2).end() 22 | .build(); 23 | 24 | private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xbf, 0x63, 0x46, 0x75, 0x6e, (byte) 0xf5, 0x63, 25 | 0x41, 0x6d, 0x74, 0x21, (byte) 0xff }; 26 | 27 | @Test 28 | public void shouldEncode() { 29 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | List dataItems = decoder.decode(); 37 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/examples/Example83Test.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.examples; 2 | 3 | import co.nstant.in.cbor.CborBuilder; 4 | import co.nstant.in.cbor.CborDecoder; 5 | import co.nstant.in.cbor.CborEncoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.DataItem; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | import java.io.ByteArrayInputStream; 12 | import java.io.InputStream; 13 | import java.util.List; 14 | 15 | public class Example83Test { 16 | 17 | private final List VALUE = 18 | new CborBuilder().addArray() 19 | .add("foo").tagged(300).tagged(301).tagged(302) 20 | .add("bar").tagged(400) 21 | .addMap() 22 | .put("a", "b") 23 | .addKey("c").tagged(401).value("d").tagged(402) 24 | .end().tagged(403) 25 | .addArray() 26 | .add("c").tagged(503) 27 | .end().tagged(504) 28 | .end() 29 | .build(); 30 | 31 | private final byte[] ENCODED_VALUE = 32 | new byte[]{(byte) 0x84, (byte) 0xD9, 0x01, 0x2E, (byte) 0xD9, 0x01, 0x2D, (byte) 0xD9, 33 | 0x01, 0x2C, 0x63, 0x66, 0x6F, 0x6F, (byte) 0xD9, 0x01, (byte) 0x90, 0x63, 0x62, 34 | 0x61, 0x72, (byte) 0xD9, 0x01, (byte) 0x93, (byte) 0xA2, 0x61, 0x61, 0x61, 0x62, 35 | (byte) 0xD9, 0x01, (byte) 0x91, 0x61, 0x63, (byte) 0xD9, 0x01, (byte) 0x92, 36 | 0x61, 0x64, (byte) 0xD9, 0x01, (byte) 0xF8, (byte) 0x81, (byte) 0xD9, 0x01, 37 | (byte) 0xF7, 0x61, 0x63}; 38 | 39 | @Test 40 | public void shouldEncode() { 41 | Assert.assertArrayEquals(ENCODED_VALUE, CborEncoder.encodeToBytes(VALUE)); 42 | } 43 | 44 | @Test 45 | public void shouldDecode() throws CborException { 46 | InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); 47 | CborDecoder decoder = new CborDecoder(inputStream); 48 | List dataItems = decoder.decode(); 49 | Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractByteStringTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.ByteString; 12 | import co.nstant.in.cbor.model.DataItem; 13 | 14 | public abstract class AbstractByteStringTest { 15 | 16 | private final byte[] value; 17 | private final byte[] encodedValue; 18 | 19 | public AbstractByteStringTest(byte[] value, byte[] encodedValue) { 20 | this.value = value; 21 | this.encodedValue = encodedValue; 22 | } 23 | 24 | @Test 25 | public void shouldEncode() { 26 | Assert.assertArrayEquals(encodedValue, new ByteString(value).encodeToBytes()); 27 | } 28 | 29 | @Test 30 | public void shouldDecode() throws CborException { 31 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 32 | CborDecoder decoder = new CborDecoder(inputStream); 33 | DataItem dataItem = decoder.decodeNext(); 34 | Assert.assertTrue(dataItem instanceof ByteString); 35 | ByteString byteString = (ByteString) dataItem; 36 | Assert.assertArrayEquals(value, byteString.getBytes()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractDataItemTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Assert; 4 | 5 | import co.nstant.in.cbor.CborDecoder; 6 | import co.nstant.in.cbor.CborException; 7 | import co.nstant.in.cbor.model.DataItem; 8 | 9 | public abstract class AbstractDataItemTest { 10 | 11 | protected void shouldEncodeAndDecode(String message, DataItem dataItem) throws CborException { 12 | byte[] bytes = dataItem.encodeToBytes(); 13 | DataItem object = CborDecoder.decode(bytes).get(0); 14 | Assert.assertEquals(message, dataItem, object); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractDoublePrecisionFloatTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.List; 6 | 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import co.nstant.in.cbor.CborBuilder; 11 | import co.nstant.in.cbor.CborDecoder; 12 | import co.nstant.in.cbor.CborException; 13 | import co.nstant.in.cbor.model.DataItem; 14 | import co.nstant.in.cbor.model.DoublePrecisionFloat; 15 | 16 | public abstract class AbstractDoublePrecisionFloatTest { 17 | 18 | private final double value; 19 | private final byte[] encodedValue; 20 | 21 | public AbstractDoublePrecisionFloatTest(double value, byte[] encodedValue) { 22 | this.value = value; 23 | this.encodedValue = encodedValue; 24 | } 25 | 26 | @Test 27 | public void shouldEncode() { 28 | List dataItems = new CborBuilder().add(value).build(); 29 | Assert.assertArrayEquals(encodedValue, dataItems.get(0).encodeToBytes()); 30 | } 31 | 32 | @Test 33 | public void shouldDecode() throws CborException { 34 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 35 | CborDecoder decoder = new CborDecoder(inputStream); 36 | DataItem dataItem = decoder.decodeNext(); 37 | Assert.assertTrue(dataItem instanceof DoublePrecisionFloat); 38 | DoublePrecisionFloat doublePrecisionFloat = (DoublePrecisionFloat) dataItem; 39 | Assert.assertEquals(value, doublePrecisionFloat.getValue(), 0); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractFloatTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | 6 | import java.util.Objects; 7 | 8 | import org.junit.Test; 9 | 10 | public class AbstractFloatTest { 11 | 12 | @Test 13 | public void testEquals() { 14 | AbstractFloat a = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.0f); 15 | AbstractFloat b = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.3f); 16 | assertEquals(a, a); 17 | assertEquals(b, b); 18 | assertFalse(a.equals(b)); 19 | assertFalse(a.equals(null)); 20 | assertFalse(a.equals("test")); 21 | } 22 | 23 | @Test 24 | public void testHashcode() { 25 | Special superClass = new Special(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT); 26 | AbstractFloat f = new AbstractFloat(SpecialType.IEEE_754_SINGLE_PRECISION_FLOAT, 0.0f); 27 | assertEquals(superClass.hashCode() ^ Objects.hashCode(0.0f), f.hashCode()); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractHalfPrecisionFloatTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.HalfPrecisionFloat; 13 | 14 | public abstract class AbstractHalfPrecisionFloatTest { 15 | 16 | private final float value; 17 | private final byte[] encodedValue; 18 | 19 | public AbstractHalfPrecisionFloatTest(float value, byte[] encodedValue) { 20 | this.value = value; 21 | this.encodedValue = encodedValue; 22 | } 23 | 24 | @Test 25 | public void shouldEncode() throws CborException { 26 | Assert.assertArrayEquals(encodedValue, new HalfPrecisionFloat(value).encodeToBytes()); 27 | } 28 | 29 | @Test 30 | public void shouldDecode() throws CborException { 31 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 32 | CborDecoder decoder = new CborDecoder(inputStream); 33 | DataItem dataItem = decoder.decodeNext(); 34 | Assert.assertTrue(dataItem instanceof HalfPrecisionFloat); 35 | HalfPrecisionFloat halfPrecisionFloat = (HalfPrecisionFloat) dataItem; 36 | Assert.assertEquals(value, halfPrecisionFloat.getValue(), 0); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractNumberTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.math.BigInteger; 6 | import java.util.List; 7 | 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborBuilder; 12 | import co.nstant.in.cbor.CborDecoder; 13 | import co.nstant.in.cbor.CborException; 14 | import co.nstant.in.cbor.model.DataItem; 15 | import co.nstant.in.cbor.model.Number; 16 | 17 | public abstract class AbstractNumberTest { 18 | 19 | private final BigInteger value; 20 | private final byte[] encodedValue; 21 | 22 | public AbstractNumberTest(long value, byte[] encodedValue) { 23 | this.value = BigInteger.valueOf(value); 24 | this.encodedValue = encodedValue; 25 | } 26 | 27 | public AbstractNumberTest(BigInteger value, byte[] encodedValue) { 28 | this.value = value; 29 | this.encodedValue = encodedValue; 30 | } 31 | 32 | @Test 33 | public void shouldEncode() { 34 | List dataItems = new CborBuilder().add(value).build(); 35 | Assert.assertArrayEquals(encodedValue, dataItems.get(0).encodeToBytes()); 36 | } 37 | 38 | @Test 39 | public void shouldDecode() throws CborException { 40 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 41 | CborDecoder decoder = new CborDecoder(inputStream); 42 | DataItem dataItem = decoder.decodeNext(); 43 | Assert.assertTrue(dataItem instanceof Number); 44 | Number number = (Number) dataItem; 45 | Assert.assertEquals(value, number.getValue()); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractSinglePrecisionFloatTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.SinglePrecisionFloat; 13 | 14 | public abstract class AbstractSinglePrecisionFloatTest { 15 | 16 | private final float value; 17 | private final byte[] encodedValue; 18 | 19 | public AbstractSinglePrecisionFloatTest(float value, byte[] encodedValue) { 20 | this.value = value; 21 | this.encodedValue = encodedValue; 22 | } 23 | 24 | @Test 25 | public void shouldEncode() { 26 | Assert.assertArrayEquals(encodedValue, new SinglePrecisionFloat(value).encodeToBytes()); 27 | } 28 | 29 | @Test 30 | public void shouldDecode() throws CborException { 31 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 32 | CborDecoder decoder = new CborDecoder(inputStream); 33 | DataItem dataItem = decoder.decodeNext(); 34 | Assert.assertTrue(dataItem instanceof SinglePrecisionFloat); 35 | SinglePrecisionFloat singlePrecisionFloat = (SinglePrecisionFloat) dataItem; 36 | Assert.assertEquals(value, singlePrecisionFloat.getValue(), 0); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AbstractStringTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import co.nstant.in.cbor.CborDecoder; 10 | import co.nstant.in.cbor.CborException; 11 | import co.nstant.in.cbor.model.DataItem; 12 | import co.nstant.in.cbor.model.UnicodeString; 13 | 14 | public abstract class AbstractStringTest { 15 | 16 | private final String value; 17 | private final byte[] encodedValue; 18 | 19 | public AbstractStringTest(String value, byte[] encodedValue) { 20 | this.value = value; 21 | this.encodedValue = encodedValue; 22 | } 23 | 24 | @Test 25 | public void shouldEncode() { 26 | Assert.assertArrayEquals(encodedValue, new UnicodeString(value).encodeToBytes()); 27 | } 28 | 29 | @Test 30 | public void shouldDecode() throws CborException { 31 | InputStream inputStream = new ByteArrayInputStream(encodedValue); 32 | CborDecoder decoder = new CborDecoder(inputStream); 33 | DataItem dataItem = decoder.decodeNext(); 34 | Assert.assertTrue(dataItem instanceof UnicodeString); 35 | UnicodeString unicodeString = (UnicodeString) dataItem; 36 | Assert.assertEquals(value, unicodeString.toString()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/AdditionalInformationTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class AdditionalInformationTest { 7 | 8 | /** 9 | * Additional information values 28 to 30 are reserved for future expansion. 10 | */ 11 | @Test 12 | public void shouldHandleReserved28() { 13 | Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(28)); 14 | Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(29)); 15 | Assert.assertEquals(AdditionalInformation.RESERVED, AdditionalInformation.ofByte(30)); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/ArrayTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotEquals; 5 | 6 | import org.junit.Test; 7 | 8 | public class ArrayTest { 9 | 10 | @Test 11 | public void testEquals() { 12 | Array array = new Array(); 13 | assertEquals(array, array); 14 | assertNotEquals(array, null); 15 | } 16 | 17 | @Test 18 | public void testHashcode() { 19 | Array array1 = new Array().add(new UnicodeString("string")); 20 | Array array2 = new Array().add(new UnicodeString("string")); 21 | assertEquals(array1.hashCode(), array2.hashCode()); 22 | } 23 | 24 | @Test 25 | public void testToString() { 26 | Array array = new Array().add(new UnicodeString("a")); 27 | assertEquals("[a]", array.toString()); 28 | array.setChunked(true); 29 | assertEquals("[_ a]", array.toString()); 30 | array.add(new UnicodeString("b")); 31 | assertEquals("[_ a, b]", array.toString()); 32 | array.setChunked(false); 33 | assertEquals("[a, b]", array.toString()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/ByteStringTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.util.Arrays; 8 | 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborException; 12 | 13 | public class ByteStringTest extends AbstractDataItemTest { 14 | 15 | @Test 16 | public void testByteString() throws CborException { 17 | shouldEncodeAndDecode("1-byte array", new ByteString(new byte[] { (byte) 0x00 })); 18 | } 19 | 20 | @Test 21 | public void testUnicodeString() throws CborException { 22 | shouldEncodeAndDecode("string", new UnicodeString("hello world")); 23 | } 24 | 25 | @Test 26 | public void shouldEquals() { 27 | byte[] bytes = "string".getBytes(); 28 | ByteString byteString = new ByteString(bytes); 29 | assertTrue(byteString.equals(byteString)); 30 | } 31 | 32 | @Test 33 | public void shouldNotEquals() { 34 | byte[] bytes = "string".getBytes(); 35 | ByteString byteString = new ByteString(bytes); 36 | assertFalse(byteString.equals(new Object())); 37 | } 38 | 39 | @Test 40 | public void shouldHashcode() { 41 | ChunkableDataItem superClass = new ChunkableDataItem(MajorType.BYTE_STRING); 42 | byte[] bytes = "string".getBytes(); 43 | ByteString byteString = new ByteString(bytes); 44 | assertEquals(byteString.hashCode(), superClass.hashCode() ^ Arrays.hashCode(bytes)); 45 | } 46 | 47 | @Test 48 | public void shouldNotClone() { 49 | byte[] bytes = "see issue #18".getBytes(); 50 | ByteString byteString = new ByteString(bytes); 51 | assertEquals(byteString.getBytes(), bytes); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/ChunkableDataItemTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | 6 | import org.junit.Test; 7 | 8 | public class ChunkableDataItemTest { 9 | 10 | private class TestDataItem extends ChunkableDataItem { 11 | 12 | protected TestDataItem() { 13 | super(MajorType.SPECIAL); 14 | } 15 | 16 | } 17 | 18 | @Test 19 | public void testEquals() { 20 | TestDataItem item1 = new TestDataItem(); 21 | TestDataItem item2 = new TestDataItem(); 22 | assertEquals(item1, item2); 23 | item1.setChunked(true); 24 | item2.setChunked(false); 25 | assertFalse(item1.equals(item2)); 26 | assertFalse(item1.equals(null)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/DataItemTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertNotNull; 6 | import static org.junit.Assert.assertNull; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | import org.junit.Test; 10 | 11 | public class DataItemTest { 12 | 13 | private class TestDataItem extends DataItem { 14 | 15 | protected TestDataItem() { 16 | super(MajorType.SPECIAL); 17 | } 18 | 19 | } 20 | 21 | @Test(expected = NullPointerException.class) 22 | public void shouldThrowNullPointerException() { 23 | new DataItem(null); 24 | } 25 | 26 | @Test 27 | public void testSetTag_Long() { 28 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 29 | di.setTag(1); 30 | assertNotNull(di.getTag()); 31 | } 32 | 33 | @Test(expected = IllegalArgumentException.class) 34 | public void testSetTag_Long_negative() { 35 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 36 | di.setTag(-1); 37 | } 38 | 39 | @Test 40 | public void testSetTag_Tag() { 41 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 42 | di.setTag(new Tag(1)); 43 | assertNotNull(di.getTag()); 44 | } 45 | 46 | @Test(expected = NullPointerException.class) 47 | public void testSetTag_Tag_null() { 48 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 49 | di.setTag(null); 50 | } 51 | 52 | @Test 53 | public void testGetTag() { 54 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 55 | di.setTag(new Tag(1)); 56 | 57 | Tag t = di.getTag(); 58 | assertEquals(1L, t.getValue()); 59 | } 60 | 61 | @Test 62 | public void testHasTag() { 63 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 64 | assertFalse(di.hasTag()); 65 | 66 | di.setTag(new Tag(1)); 67 | assertTrue(di.hasTag()); 68 | } 69 | 70 | @Test 71 | public void testRemoveTag() { 72 | DataItem di = new DataItem(MajorType.UNSIGNED_INTEGER); 73 | di.setTag(new Tag(1)); 74 | assertNotNull(di.getTag()); 75 | 76 | di.removeTag(); 77 | assertNull(di.getTag()); 78 | } 79 | 80 | @Test 81 | public void testEquals() { 82 | DataItem a = new TestDataItem(); 83 | DataItem b = new TestDataItem(); 84 | assertEquals(a, b); 85 | a.setTag(1); 86 | assertFalse(a.equals(b)); 87 | assertFalse(a.equals(null)); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/DoublePrecisionFloatTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | 6 | import java.util.Objects; 7 | 8 | import org.junit.Test; 9 | 10 | public class DoublePrecisionFloatTest { 11 | 12 | @Test 13 | public void testEquals() { 14 | DoublePrecisionFloat a = new DoublePrecisionFloat(1.234); 15 | DoublePrecisionFloat b = new DoublePrecisionFloat(0.333); 16 | assertEquals(a, a); 17 | assertEquals(b, b); 18 | } 19 | 20 | @Test 21 | public void testNotEquals() { 22 | DoublePrecisionFloat doublePrecisionFloat = new DoublePrecisionFloat(1.234); 23 | assertFalse(doublePrecisionFloat.equals(null)); 24 | assertFalse(doublePrecisionFloat.equals(new DoublePrecisionFloat(1.2345))); 25 | } 26 | 27 | @Test 28 | public void testHashcode() { 29 | Special superClass = new Special(SpecialType.IEEE_754_DOUBLE_PRECISION_FLOAT); 30 | double value = 1.234; 31 | DoublePrecisionFloat doublePrecisionFloat = new DoublePrecisionFloat(value); 32 | assertEquals(superClass.hashCode() ^ Objects.hashCode(value), doublePrecisionFloat.hashCode()); 33 | } 34 | 35 | @Test 36 | public void testToString() { 37 | DoublePrecisionFloat doublePrecisionFloat = new DoublePrecisionFloat(1.234); 38 | assertEquals("1.234", doublePrecisionFloat.toString()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/LanguageTaggedStringTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class LanguageTaggedStringTest { 8 | 9 | @Test 10 | public void shouldInitializeWithStrings() { 11 | LanguageTaggedString lts = new LanguageTaggedString("en", "Hello"); 12 | assertEquals(38, lts.getTag().getValue()); 13 | assertEquals("en", lts.getLanguage().getString()); 14 | assertEquals("Hello", lts.getString().getString()); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/MajorTypeTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import co.nstant.in.cbor.CborException; 7 | 8 | public class MajorTypeTest { 9 | 10 | @Test 11 | public void shouldParseUnsignedInteger() throws CborException { 12 | Assert.assertEquals(MajorType.UNSIGNED_INTEGER, MajorType.ofByte(0b000_00000)); 13 | } 14 | 15 | @Test 16 | public void shouldParseNegativeInteger() throws CborException { 17 | Assert.assertEquals(MajorType.NEGATIVE_INTEGER, MajorType.ofByte(0b001_00000)); 18 | } 19 | 20 | @Test 21 | public void shouldParseByteString() throws CborException { 22 | Assert.assertEquals(MajorType.BYTE_STRING, MajorType.ofByte(0b010_00000)); 23 | } 24 | 25 | @Test 26 | public void shouldParseUnicodeString() throws CborException { 27 | Assert.assertEquals(MajorType.UNICODE_STRING, MajorType.ofByte(0b011_00000)); 28 | } 29 | 30 | @Test 31 | public void shouldParseArray() throws CborException { 32 | Assert.assertEquals(MajorType.ARRAY, MajorType.ofByte(0b100_00000)); 33 | } 34 | 35 | @Test 36 | public void shouldParseMap() throws CborException { 37 | Assert.assertEquals(MajorType.MAP, MajorType.ofByte(0b101_00000)); 38 | } 39 | 40 | @Test 41 | public void shouldParseTag() throws CborException { 42 | Assert.assertEquals(MajorType.TAG, MajorType.ofByte(0b110_00000)); 43 | } 44 | 45 | @Test 46 | public void shouldParseSpecial() throws CborException { 47 | Assert.assertEquals(MajorType.SPECIAL, MajorType.ofByte(0b111_00000)); 48 | } 49 | 50 | @Test(expected = CborException.class) 51 | public void shouldReturnThrowOnInvalidByteValue() throws CborException { 52 | MajorType.ofByte(0xffffffff); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/MapTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNotEquals; 5 | 6 | import java.io.ByteArrayInputStream; 7 | import java.io.ByteArrayOutputStream; 8 | import java.util.Collection; 9 | import java.util.Iterator; 10 | import java.util.List; 11 | 12 | import org.junit.Test; 13 | 14 | import co.nstant.in.cbor.CborBuilder; 15 | import co.nstant.in.cbor.CborDecoder; 16 | import co.nstant.in.cbor.CborEncoder; 17 | import co.nstant.in.cbor.CborException; 18 | 19 | public class MapTest { 20 | 21 | @Test 22 | public void testRemove() { 23 | UnicodeString key = new UnicodeString("key"); 24 | UnicodeString value = new UnicodeString("value"); 25 | Map map = new Map(); 26 | map.put(key, value); 27 | assertEquals(1, map.getValues().size()); 28 | map.remove(key); 29 | assertEquals(0, map.getValues().size()); 30 | } 31 | 32 | @Test 33 | public void testEquals() { 34 | Map map1 = new Map(); 35 | assertEquals(map1, map1); 36 | assertNotEquals(map1, new Object()); 37 | } 38 | 39 | @Test 40 | public void testHashcode() { 41 | Map map1 = new Map(); 42 | Map map2 = new Map(); 43 | assertEquals(map1.hashCode(), map2.hashCode()); 44 | map1.put(new UnicodeString("key"), new UnicodeString("value")); 45 | assertNotEquals(map1.hashCode(), map2.hashCode()); 46 | } 47 | 48 | @Test 49 | public void testToString() { 50 | Map map = new Map(); 51 | assertEquals("{ }", map.toString()); 52 | map.put(new UnicodeString("key1"), new UnicodeString("value1")); 53 | assertEquals("{ key1: value1 }", map.toString()); 54 | map.put(new UnicodeString("key2"), new UnicodeString("value2")); 55 | assertEquals("{ key1: value1, key2: value2 }", map.toString()); 56 | map.setChunked(true); 57 | assertEquals("{_ key1: value1, key2: value2 }", map.toString()); 58 | } 59 | 60 | @Test 61 | public void testItemOrderIsPreserved() throws CborException { 62 | List input = new CborBuilder().addMap().put(1, "v1").put(0, "v2").end().build(); 63 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 64 | CborEncoder encoder = new CborEncoder(byteArrayOutputStream); 65 | encoder.nonCanonical().encode(input); 66 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); 67 | CborDecoder decoder = new CborDecoder(byteArrayInputStream); 68 | List output = decoder.decode(); 69 | assertEquals(input, output); 70 | DataItem dataItem = output.get(0); 71 | assertEquals(MajorType.MAP, dataItem.getMajorType()); 72 | Map map = (Map) dataItem; 73 | Collection values = map.getValues(); 74 | Iterator iterator = values.iterator(); 75 | assertEquals(new UnicodeString("v1"), iterator.next()); 76 | assertEquals(new UnicodeString("v2"), iterator.next()); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/NegativeIntegerTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Test; 4 | 5 | import co.nstant.in.cbor.CborException; 6 | 7 | public class NegativeIntegerTest extends AbstractDataItemTest { 8 | 9 | @Test 10 | public void shouldEncodeAndDecode() throws CborException { 11 | long maxInteger = -4_294_967_295L; 12 | for (long i = -1L; i >= maxInteger; i += (maxInteger / 100_000L)) { 13 | shouldEncodeAndDecode(String.valueOf(i), new NegativeInteger(i)); 14 | } 15 | shouldEncodeAndDecode(String.valueOf(maxInteger), new NegativeInteger(maxInteger)); 16 | 17 | // Test for issue #1: Creation of 64-bit NegativeInteger >= -4294967296L fails 18 | shouldEncodeAndDecode("Long.MIN_VALUE", new NegativeInteger(Long.MIN_VALUE)); 19 | } 20 | 21 | @Test(expected = IllegalArgumentException.class) 22 | public void shouldNotAcceptPositiveValues() { 23 | new NegativeInteger(0); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/RationalNumberTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | import co.nstant.in.cbor.CborException; 8 | 9 | public class RationalNumberTest { 10 | 11 | @Test(expected = CborException.class) 12 | public void shouldThrowIfNumeratorIsNull() throws CborException { 13 | new RationalNumber(null, new UnsignedInteger(1)); 14 | } 15 | 16 | @Test(expected = CborException.class) 17 | public void shouldThrowIfDenominatorIsNull() throws CborException { 18 | new RationalNumber(new UnsignedInteger(1), null); 19 | } 20 | 21 | @Test(expected = CborException.class) 22 | public void shouldThrowIfDenominatorIsZero() throws CborException { 23 | new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(0)); 24 | } 25 | 26 | @Test 27 | public void shouldSetNumeratorAndDenominator() throws CborException { 28 | UnsignedInteger one = new UnsignedInteger(1); 29 | UnsignedInteger two = new UnsignedInteger(2); 30 | RationalNumber rationalNumber = new RationalNumber(one, two); 31 | assertEquals(one, rationalNumber.getNumerator()); 32 | assertEquals(two, rationalNumber.getDenominator()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/SimpleValueTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.util.Objects; 8 | 9 | import org.junit.Test; 10 | 11 | public class SimpleValueTest { 12 | 13 | @Test 14 | public void testHashcode() { 15 | Special superClass1 = new Special(SpecialType.SIMPLE_VALUE); 16 | Special superClass2 = new Special(SpecialType.SIMPLE_VALUE_NEXT_BYTE); 17 | for (int i = 1; i < 256; i++) { 18 | SimpleValue simpleValue = new SimpleValue(i); 19 | if (i <= 23) { 20 | assertEquals(simpleValue.hashCode(), superClass1.hashCode() ^ Objects.hashCode(i)); 21 | } else { 22 | assertEquals(simpleValue.hashCode(), superClass2.hashCode() ^ Objects.hashCode(i)); 23 | } 24 | } 25 | } 26 | 27 | @Test 28 | public void testEquals1() { 29 | for (int i = 0; i < 256; i++) { 30 | SimpleValue simpleValue1 = new SimpleValue(i); 31 | SimpleValue simpleValue2 = new SimpleValue(i); 32 | assertTrue(simpleValue1.equals(simpleValue2)); 33 | } 34 | } 35 | 36 | @Test 37 | public void testEquals2() { 38 | SimpleValue simpleValue = new SimpleValue(0); 39 | assertFalse(simpleValue.equals(new SimpleValue(1))); 40 | assertFalse(simpleValue.equals(null)); 41 | assertFalse(simpleValue.equals(false)); 42 | assertFalse(simpleValue.equals("")); 43 | assertFalse(simpleValue.equals(1)); 44 | assertFalse(simpleValue.equals(1.1)); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/SpecialTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class SpecialTest { 8 | 9 | @Test 10 | public void testToString() { 11 | Special special = Special.BREAK; 12 | assertEquals("BREAK", special.toString()); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/SpecialTypeTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import co.nstant.in.cbor.CborException; 7 | 8 | public class SpecialTypeTest { 9 | 10 | @Test(expected = CborException.class) 11 | public void shouldDetectUnallocated28() throws CborException { 12 | SpecialType.ofByte(28); 13 | } 14 | 15 | @Test(expected = CborException.class) 16 | public void shouldDetectUnallocated29() throws CborException { 17 | SpecialType.ofByte(29); 18 | } 19 | 20 | @Test(expected = CborException.class) 21 | public void shouldDetectUnallocated30() throws CborException { 22 | SpecialType.ofByte(30); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/TagTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.util.Objects; 8 | 9 | import org.junit.Test; 10 | 11 | public class TagTest { 12 | 13 | @Test 14 | public void testHashcode() { 15 | DataItem superClass = new DataItem(MajorType.TAG); 16 | for (long i = 0; i < 256; i++) { 17 | Tag tag = new Tag(i); 18 | assertEquals(tag.hashCode(), superClass.hashCode() ^ Objects.hashCode(i)); 19 | } 20 | } 21 | 22 | @Test 23 | public void testEquals1() { 24 | for (int i = 0; i < 256; i++) { 25 | Tag tag1 = new Tag(i); 26 | Tag tag2 = new Tag(i); 27 | assertTrue(tag1.equals(tag2)); 28 | } 29 | } 30 | 31 | @Test 32 | public void testEquals2() { 33 | Tag tag = new Tag(0); 34 | assertTrue(tag.equals(tag)); 35 | } 36 | 37 | @Test 38 | public void testNotEquals() { 39 | Tag tag = new Tag(0); 40 | assertFalse(tag.equals(new Tag(1))); 41 | assertFalse(tag.equals(null)); 42 | assertFalse(tag.equals(false)); 43 | assertFalse(tag.equals("")); 44 | assertFalse(tag.equals(1)); 45 | assertFalse(tag.equals(1.1)); 46 | } 47 | 48 | @Test 49 | public void testToString() { 50 | assertEquals("Tag(0)", new Tag(0).toString()); 51 | assertEquals("Tag(123)", new Tag(123).toString()); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/UnicodeStringTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class UnicodeStringTest { 7 | 8 | @Test 9 | public void toStringShouldPrintNull() { 10 | Assert.assertEquals("null", new UnicodeString(null).toString()); 11 | } 12 | 13 | @Test 14 | public void shouldEqualsSameObject() { 15 | UnicodeString unicodeString = new UnicodeString("string"); 16 | Assert.assertTrue(unicodeString.equals(unicodeString)); 17 | } 18 | 19 | @Test 20 | public void shouldEqualsBothNull() { 21 | UnicodeString unicodeString1 = new UnicodeString(null); 22 | UnicodeString unicodeString2 = new UnicodeString(null); 23 | Assert.assertTrue(unicodeString1.equals(unicodeString2)); 24 | Assert.assertTrue(unicodeString2.equals(unicodeString1)); 25 | } 26 | 27 | @Test 28 | public void shouldEqualsSameValue() { 29 | UnicodeString unicodeString1 = new UnicodeString("string"); 30 | UnicodeString unicodeString2 = new UnicodeString("string"); 31 | Assert.assertTrue(unicodeString1.equals(unicodeString2)); 32 | Assert.assertTrue(unicodeString2.equals(unicodeString1)); 33 | } 34 | 35 | @Test 36 | public void shouldHashNull() { 37 | Assert.assertEquals(0, new UnicodeString(null).hashCode()); 38 | } 39 | 40 | @Test 41 | public void shouldNotEqualOtherObjects() { 42 | UnicodeString unicodeString = new UnicodeString("string"); 43 | Assert.assertFalse(unicodeString.equals(null)); 44 | Assert.assertFalse(unicodeString.equals(1)); 45 | Assert.assertFalse(unicodeString.equals("string")); 46 | } 47 | 48 | @Test 49 | public void shouldNotEquals() { 50 | UnicodeString unicodeString1 = new UnicodeString(null); 51 | UnicodeString unicodeString2 = new UnicodeString(""); 52 | Assert.assertFalse(unicodeString1.equals(unicodeString2)); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/co/nstant/in/cbor/model/UnsignedIntegerTest.java: -------------------------------------------------------------------------------- 1 | package co.nstant.in.cbor.model; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | 7 | import java.math.BigInteger; 8 | 9 | import org.junit.Test; 10 | 11 | import co.nstant.in.cbor.CborException; 12 | 13 | public class UnsignedIntegerTest extends AbstractDataItemTest { 14 | 15 | @Test 16 | public void shouldEncodeAndDecode() throws CborException { 17 | long maxInteger = 4_294_967_295L; 18 | for (long i = 0L; i < maxInteger; i += (maxInteger / 100_000L)) { 19 | shouldEncodeAndDecode(String.valueOf(i), new UnsignedInteger(i)); 20 | } 21 | shouldEncodeAndDecode(String.valueOf(maxInteger), new UnsignedInteger(maxInteger)); 22 | } 23 | 24 | @Test(expected = IllegalArgumentException.class) 25 | public void shouldNotAcceptNegativeValues() { 26 | new UnsignedInteger(-1); 27 | } 28 | 29 | @Test 30 | public void testToString() { 31 | UnsignedInteger unsignedInteger = new UnsignedInteger(BigInteger.ZERO); 32 | assertEquals("0", unsignedInteger.toString()); 33 | } 34 | 35 | @Test 36 | public void testEquals1() { 37 | UnsignedInteger unsignedInteger = new UnsignedInteger(BigInteger.ZERO); 38 | assertTrue(unsignedInteger.equals(unsignedInteger)); 39 | } 40 | 41 | @Test 42 | public void testEquals2() { 43 | for (int i = 0; i < 256; i++) { 44 | UnsignedInteger unsignedInteger1 = new UnsignedInteger(BigInteger.valueOf(i)); 45 | UnsignedInteger unsignedInteger2 = new UnsignedInteger(BigInteger.valueOf(i)); 46 | assertTrue(unsignedInteger1.equals(unsignedInteger2)); 47 | } 48 | } 49 | 50 | @Test 51 | public void testEquals3() { 52 | UnsignedInteger unsignedInteger = new UnsignedInteger(BigInteger.ZERO); 53 | assertFalse(unsignedInteger.equals(new Object())); 54 | } 55 | 56 | @Test 57 | public void testHashcode() { 58 | UnsignedInteger unsignedInteger = new UnsignedInteger(BigInteger.ZERO); 59 | assertEquals(BigInteger.ZERO.hashCode(), unsignedInteger.getValue().hashCode()); 60 | } 61 | 62 | } 63 | --------------------------------------------------------------------------------