co.nstant.in.cbor.CborDecoder Java Examples

The following examples show how to use co.nstant.in.cbor.CborDecoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: cbor-java   Author: c-rack   File: RationalNumberEncoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    encoder.encode(new RationalNumber(ONE, TWO));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(inputStream);

    Array expected = new Array();
    expected.setTag(11);
    expected.add(ONE);
    expected.add(TWO);

    Object object = decoder.decodeNext();
    assertTrue(object instanceof Array);
    Array decoded = (Array) object;

    assertEquals(new Tag(30), decoded.getTag());
    assertEquals(2, decoded.getDataItems().size());
    assertEquals(ONE, decoded.getDataItems().get(0));
    assertEquals(TWO, decoded.getDataItems().get(1));
}
 
Example #2
Source Project: cbor-java   Author: c-rack   File: Example12Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(2, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example #3
Source Project: cbor-java   Author: c-rack   File: UnsignedIntegerDecoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeBigNumbers() throws CborException {
    BigInteger value = BigInteger.ONE;
    for (int i = 1; i < 64; i++) {
        value = value.shiftLeft(1);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder().add(value).build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
        DataItem dataItem = dataItems.get(0);
        assertTrue(dataItem instanceof UnsignedInteger);
        assertEquals(value, ((UnsignedInteger) dataItem).getValue());
    }
}
 
Example #4
Source Project: cbor-java   Author: c-rack   File: Example14Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
        new byte[] { (byte) 0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    DataItem b = decoder.decodeNext();

    Assert.assertTrue(b.hasTag());
    Tag tag = b.getTag();
    Assert.assertEquals(3, tag.getValue());

    Assert.assertTrue(b instanceof ByteString);
    ByteString byteString = (ByteString) b;
    Assert.assertArrayEquals(new byte[] { (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteString.getBytes());
}
 
Example #5
Source Project: cbor-java   Author: c-rack   File: MapTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testItemOrderIsPreserved() throws CborException {
    List<DataItem> input = new CborBuilder().addMap().put(1, "v1").put(0, "v2").end().build();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.nonCanonical().encode(input);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    CborDecoder decoder = new CborDecoder(byteArrayInputStream);
    List<DataItem> output = decoder.decode();
    assertEquals(input, output);
    DataItem dataItem = output.get(0);
    assertEquals(MajorType.MAP, dataItem.getMajorType());
    Map map = (Map) dataItem;
    Collection<DataItem> values = map.getValues();
    Iterator<DataItem> iterator = values.iterator();
    assertEquals(new UnicodeString("v1"), iterator.next());
    assertEquals(new UnicodeString("v2"), iterator.next());
}
 
Example #6
Source Project: cbor-java   Author: c-rack   File: ByteStringDecoderTest.java    License: Apache License 2.0 6 votes vote down vote up
public void decodingExample() throws CborException {
    byte bytes[] = { 0, 1, 2, 3 };
    // Encode
    ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();
    new CborEncoder(encodedStream).encode(new ByteString(bytes));
    byte encodedBytes[] = encodedStream.toByteArray();
    // Decode
    ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    assertEquals(MajorType.BYTE_STRING, dataItem.getMajorType());
    ByteString byteString = (ByteString) dataItem;
    byte[] decodedBytes = byteString.getBytes();
    // Verify
    assertEquals(bytes.length, decodedBytes.length);
    assertEquals(bytes[0], decodedBytes[0]);
    assertEquals(bytes[1], decodedBytes[1]);
    assertEquals(bytes[2], decodedBytes[2]);
    assertEquals(bytes[3], decodedBytes[3]);
}
 
Example #7
Source Project: cbor-java   Author: c-rack   File: LanguageTaggedStringDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void testExceptionOnNot2ElementArray() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).addArray().add(true).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #8
Source Project: sawtooth-sdk-java   Author: hyperledger   File: IntegerKeyHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to decode the Payload of a transaction.
 * Convert the co.nstant.in.cbor.model.Map to a HashMap.
 */
public Map<String, String> decodePayload(byte[] bytes) throws CborException {
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  co.nstant.in.cbor.model.Map data =
      (co.nstant.in.cbor.model.Map) new CborDecoder(bais).decodeNext();
  DataItem[] keys = data.getKeys().toArray(new DataItem[0]);
  Map<String, String> result = new HashMap();
  for (int i = 0; i < keys.length; i++) {
    result.put(
        keys[i].toString(),
        data.get(keys[i]).toString());
  }
  return result;
}
 
Example #9
Source Project: sawtooth-sdk-java   Author: hyperledger   File: IntegerKeyHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to decode State retrieved from the address of the name.
 * Convert the co.nstant.in.cbor.model.Map to a HashMap.
 */
public Map<String, Long> decodeState(byte[] bytes) throws CborException {
  ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  co.nstant.in.cbor.model.Map data =
      (co.nstant.in.cbor.model.Map) new CborDecoder(bais).decodeNext();
  DataItem[] keys = data.getKeys().toArray(new DataItem[0]);
  Map<String, Long> result = new HashMap();
  for (int i = 0; i < keys.length; i++) {
    result.put(
        keys[i].toString(),
        Long.decode(data.get(keys[i]).toString()));
  }
  return result;
}
 
Example #10
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnRationalNumberDecode1() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).add(true).build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #11
Source Project: webauthndemo   Author: google   File: AttestationObject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param attestationObject
 * @return AttestationObject created from the provided byte array
 * @throws CborException
 * @throws ResponseException
 */
public static AttestationObject decode(byte[] attestationObject)
    throws CborException, ResponseException {
  AttestationObject result = new AttestationObject();
  List<DataItem> dataItems = CborDecoder.decode(attestationObject);

  if (dataItems.size() == 1 && dataItems.get(0) instanceof Map) {
    DataItem attStmt = null;
    Map attObjMap = (Map) dataItems.get(0);
    for (DataItem key : attObjMap.getKeys()) {
      if (key instanceof UnicodeString) {
        switch(((UnicodeString) key).getString()) {
          case "fmt":
            UnicodeString value = (UnicodeString) attObjMap.get(key);
            result.fmt = value.getString();
            break;
          case "authData":
            byte[] authData = ((ByteString) attObjMap.get(key)).getBytes();
            result.authData = AuthenticatorData.decode(authData);
            break;
          case "attStmt":
            attStmt = attObjMap.get(key);
            break;
            
        }
      }
    }

    if (attStmt != null) {
      result.attStmt = AttestationStatement.decode(result.fmt, attStmt);
    }

  }
  return result;
}
 
Example #12
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnRationalNumberDecode4() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).addArray().add(1).add(true).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    decoder.decode();
}
 
Example #13
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeTaggedRationalNumber() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(1).addTag(30).addArray().add(1).add(2).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);

    RationalNumber expected = new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2));
    expected.getTag().setTag(new Tag(1));
    assertEquals(expected, decoder.decodeNext());
}
 
Example #14
Source Project: cbor-java   Author: c-rack   File: Example78Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #15
Source Project: cbor-java   Author: c-rack   File: Example41Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(SimpleValue.FALSE, simpleValue);
}
 
Example #16
Source Project: cbor-java   Author: c-rack   File: Example45Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(VALUE, simpleValue);
}
 
Example #17
Source Project: cbor-java   Author: c-rack   File: Example73Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    decoder.setAutoDecodeInfinitiveUnicodeStrings(false);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #18
Source Project: cbor-java   Author: c-rack   File: Example48Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #19
Source Project: cbor-java   Author: c-rack   File: Example49Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #20
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeRationalNumber() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).addArray().add(1).add(2).end().build();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(items);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    CborDecoder decoder = new CborDecoder(bais);
    assertEquals(new RationalNumber(new UnsignedInteger(1), new UnsignedInteger(2)), decoder.decodeNext());
}
 
Example #21
Source Project: cbor-java   Author: c-rack   File: Example52Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #22
Source Project: cbor-java   Author: c-rack   File: Example51Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());

}
 
Example #23
Source Project: cbor-java   Author: c-rack   File: Example71Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #24
Source Project: cbor-java   Author: c-rack   File: Example44Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof SimpleValue);
    SimpleValue simpleValue = (SimpleValue) dataItem;
    Assert.assertEquals(SimpleValue.UNDEFINED, simpleValue);
}
 
Example #25
Source Project: cbor-java   Author: c-rack   File: Example67Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #26
Source Project: cbor-java   Author: c-rack   File: Example82Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #27
Source Project: cbor-java   Author: c-rack   File: Example63Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    DataItem dataItem = decoder.decodeNext();
    Assert.assertTrue(dataItem instanceof Array);
    Array array = (Array) dataItem;
    Assert.assertTrue(array.getDataItems().isEmpty());
}
 
Example #28
Source Project: cbor-java   Author: c-rack   File: Example77Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #29
Source Project: cbor-java   Author: c-rack   File: Example66Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecode() throws CborException {
    InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE);
    CborDecoder decoder = new CborDecoder(inputStream);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #30
Source Project: cbor-java   Author: c-rack   File: CborDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSetAutoDecodeInfinitiveMaps() {
    InputStream inputStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
    CborDecoder cborDecoder = new CborDecoder(inputStream);
    assertTrue(cborDecoder.isAutoDecodeInfinitiveMaps());
    cborDecoder.setAutoDecodeInfinitiveMaps(false);
    assertFalse(cborDecoder.isAutoDecodeInfinitiveMaps());
}