co.nstant.in.cbor.CborException Java Examples

The following examples show how to use co.nstant.in.cbor.CborException. 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 File: UnicodeStringDecoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
private UnicodeString decodeInfinitiveLength() throws CborException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (;;) {
        DataItem dataItem = decoder.decodeNext();
        if (dataItem == null) {
            throw new CborException("Unexpected end of stream");
        }
        MajorType majorType = dataItem.getMajorType();
        if (Special.BREAK.equals(dataItem)) {
            break;
        } else if (majorType == MajorType.UNICODE_STRING) {
            UnicodeString unicodeString = (UnicodeString) dataItem;
            byte[] byteArray = unicodeString.toString().getBytes(StandardCharsets.UTF_8);
            bytes.write(byteArray, 0, byteArray.length);
        } else {
            throw new CborException("Unexpected major type " + majorType);
        }
    }
    return new UnicodeString(new String(bytes.toByteArray(), StandardCharsets.UTF_8));
}
 
Example #2
Source File: Connack.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] connackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(STATUS_KEY, status)
            .end()
            .build());
        connackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return connackBytes;
}
 
Example #3
Source File: AuthenticatorDataTest.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link com.google.webauthn.gaedemo.objects.AuthenticatorData#decode(byte[])}.
 * 
 * @throws CborException
 * @throws ResponseException 
 */
@Test
public void testDecodeWithAttestation() throws CborException, ResponseException {
  byte[] randomRpIdHash = new byte[32];
  random.nextBytes(randomRpIdHash);
  byte[] flags = {1 << 6};
  AttestationData attData = new AttestationData();
  EccKey eccKey = new EccKey(Base64.decodeBase64("NNxD3LBXs6iF1jiBGZ4Qqhd997NKcmDLJyyILL49V90"),
      Base64.decodeBase64("MJtVZlRRfTscLy06DSHLBfA8O03pZJ1K01DbCILr0rA"));
  random.nextBytes(attData.aaguid);
  eccKey.alg = Algorithm.ES256;
  attData.publicKey = eccKey;
  int countUnsignedInt = Integer.parseUnsignedInt("" + (random.nextLong() & 0xffffffffL));
  byte[] count = ByteBuffer.allocate(4).putInt(countUnsignedInt).array();
  byte[] data = null;
  data = Bytes.concat(randomRpIdHash, flags, count, attData.encode());
  AuthenticatorData result = AuthenticatorData.decode(data);
  assertTrue(result.getAttData().getPublicKey().equals(eccKey));
  assertArrayEquals(randomRpIdHash, result.getRpIdHash());
  assertTrue(Integer.compareUnsigned(countUnsignedInt, result.getSignCount()) == 0);
}
 
Example #4
Source File: Unsubscribe.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] unsubscribeBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ArrayBuilder<MapBuilder<CborBuilder>> topicsArray = new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .putArray(TOPICS_KEY);

        for (String topic : topics) {
            topicsArray = topicsArray.add(topic);
        }

        List<DataItem> cbordata = topicsArray.end().end().build();
        new CborEncoder(baos).encode(cbordata);
        unsubscribeBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return unsubscribeBytes;
}
 
Example #5
Source File: AuthenticatorAttestationResponse.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * @param data
 * @throws ResponseException
 */
public AuthenticatorAttestationResponse(JsonElement data) throws ResponseException {
  Gson gson = new Gson();
  AttestationResponseJson parsedObject = gson.fromJson(data, AttestationResponseJson.class);

  clientDataBytes = BaseEncoding.base64().decode(parsedObject.clientDataJSON);
  byte[] attestationObject = BaseEncoding.base64().decode(parsedObject.attestationObject);

  try {
    decodedObject = AttestationObject.decode(attestationObject);
  } catch (CborException e) {
    throw new ResponseException("Cannot decode attestation object");
  }

  clientData = gson.fromJson(new String(clientDataBytes, StandardCharsets.UTF_8),
      CollectedClientData.class);
  transports = parsedObject.transports;
}
 
Example #6
Source File: DeleteNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] DeleteNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, DELETE_NETWORK_REQ)
                .put(INDEX_KEY, index)
                .end()
                .build());
        DeleteNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return DeleteNetworkRequestBytes;
}
 
Example #7
Source File: AbstractEncoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shallEncode32bit() throws CborException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    TestEncoder encoder = new TestEncoder(outputStream);
    encoder.doEncodeTypeAndLength(4294967296L);
    byte[] bytes = outputStream.toByteArray();
    assertEquals(9, bytes.length);
    assertEquals((byte) 0x9B, bytes[0]);
    assertEquals((byte) 0x00, bytes[1]);
    assertEquals((byte) 0x00, bytes[2]);
    assertEquals((byte) 0x00, bytes[3]);
    assertEquals((byte) 0x01, bytes[4]);
    assertEquals((byte) 0x00, bytes[5]);
    assertEquals((byte) 0x00, bytes[6]);
    assertEquals((byte) 0x00, bytes[7]);
    assertEquals((byte) 0x00, bytes[8]);
}
 
Example #8
Source File: ListNetworkReq.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] ListNetworkRequestBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, LIST_NETWORK_REQ)
                .put(MAXNETWORKS_KEY, maxNetworks)
                .put(TIMEOUT_KEY, timeout)
                .end()
                .build());
        ListNetworkRequestBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return ListNetworkRequestBytes;
}
 
Example #9
Source File: Publish.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] publishBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
            .addMap()
            .put(TYPE_KEY, type)
            .put(TOPIC_KEY, topic)
            .put(MSGID_KEY, msgID)
            .put(QOS_KEY, qoS)
            .put(PAYLOAD_KEY, payloadBytes)
            .end()
            .build());
        publishBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return publishBytes;
}
 
Example #10
Source File: AuthenticatorData.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
/**
 * Parse Attestation extensions
 * 
 * @return extension map
 */
private HashMap<String, AttestationExtension> parseExtensions(byte[] extensions) {
  HashMap<String, AttestationExtension> extensionMap = new HashMap<>();

  try {
    List<DataItem> dataItems = CborDecoder.decode(extensions);

    if (dataItems.size() < 1 || !(dataItems.get(0) instanceof Map)) {
      return extensionMap;
    }
    Map map = (Map) dataItems.get(0);

    for (DataItem data : map.getKeys()) {
      if (data instanceof UnicodeString) {
        if (((UnicodeString) data).getString().equals(CableRegistrationData.KEY)) {
          CableRegistrationData decodedCableData = CableRegistrationData.parseFromCbor(map.get(data));
          extensionMap.put(CableRegistrationData.KEY, decodedCableData);
        }
      }
    }
  } catch (CborException e) {
    e.printStackTrace();
  }
  return extensionMap;
}
 
Example #11
Source File: Unsuback.java    From amazon-freertos-ble-android-sdk with Apache License 2.0 6 votes vote down vote up
public byte[] encode() {
    byte[] unsubackBytes = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new CborEncoder(baos).encode(new CborBuilder()
                .addMap()
                .put(TYPE_KEY, type)
                .put(MSGID_KEY, msgID)
                .end()
                .build());
        unsubackBytes = baos.toByteArray();
    } catch (CborException e) {
        Log.e(TAG, "Failed to encode.", e);
    }
    return unsubackBytes;
}
 
Example #12
Source File: MapEncoder.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Override
public void encode(Map map) throws CborException {
    Collection<DataItem> keys = map.getKeys();

    if (map.isChunked()) {
        encodeTypeChunked(MajorType.MAP);
    } else {
        encodeTypeAndLength(MajorType.MAP, keys.size());
    }

    if (keys.isEmpty()) {
        return;
    }

    if (map.isChunked()) {
        encodeNonCanonical(map);
        encoder.encode(SimpleValue.BREAK);
    } else {
        if (encoder.isCanonical()) {
            encodeCanonical(map);
        } else {
            encodeNonCanonical(map);
        }
    }
}
 
Example #13
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void shouldThrowOnRationalNumberDecode3() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(30).addArray().add(true).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 #14
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeChunkedByteString() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().startByteString().add(new byte[] { '\0' }).add(new byte[] { 0x10 })
        .add(new byte[] { 0x13 }).end().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());
}
 
Example #15
Source File: AbstractDataItemTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected void shouldEncodeAndDecode(String message, DataItem dataItem) throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(dataItem);
    byte[] bytes = byteArrayOutputStream.toByteArray();
    DataItem object = CborDecoder.decode(bytes).get(0);
    Assert.assertEquals(message, dataItem, object);
}
 
Example #16
Source File: Example52Test.java    From cbor-java with 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 #17
Source File: CborDecoderTest.java    From cbor-java with 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 #18
Source File: ByteStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeByteString1K() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().add(new byte[1024]).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());
}
 
Example #19
Source File: AbstractDecoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected int nextSymbol() throws CborException {
    try {
        int symbol = inputStream.read();
        if (symbol == -1) {
            throw new IOException("Unexpected end of stream");
        }
        return symbol;
    } catch (IOException ioException) {
        throw new CborException(ioException);
    }
}
 
Example #20
Source File: UnicodeStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeChunkedUnicodeString() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(baos);
    encoder.encode(new CborBuilder().startString().add("foo").add("bar").end().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());
}
 
Example #21
Source File: MapDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseLastOfDuplicateKeysByDefault() throws CborException {
    byte[] bytes = new byte[] { (byte) 0xa2, 0x01, 0x01, 0x01, 0x02 };
    List<DataItem> decoded = CborDecoder.decode(bytes);
    Map map = (Map) decoded.get(0);
    assertEquals(map.getKeys().size(), 1);
    assertEquals(map.get(new UnsignedInteger(1)), new UnsignedInteger(2));
}
 
Example #22
Source File: Example74Test.java    From cbor-java with 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.setAutoDecodeInfinitiveArrays(false);
    List<DataItem> dataItems = decoder.decode();
    Assert.assertArrayEquals(VALUE.toArray(), dataItems.toArray());
}
 
Example #23
Source File: Example48Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #24
Source File: ByteStringEncoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(ByteString byteString) throws CborException {
    byte[] bytes = byteString.getBytes();
    if (byteString.isChunked()) {
        encodeTypeChunked(MajorType.BYTE_STRING);
        if (bytes != null) {
            encode(new ByteString(bytes));
        }
    } else if (bytes == null) {
        encoder.encode(SimpleValue.NULL);
    } else {
        encodeTypeAndLength(MajorType.BYTE_STRING, bytes.length);
        write(bytes);
    }
}
 
Example #25
Source File: ArrayEncoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(Array array) throws CborException {
    List<DataItem> dataItems = array.getDataItems();
    if (array.isChunked()) {
        encodeTypeChunked(MajorType.ARRAY);
    } else {
        encodeTypeAndLength(MajorType.ARRAY, dataItems.size());
    }
    for (DataItem dataItem : dataItems) {
        encoder.encode(dataItem);
    }
}
 
Example #26
Source File: CborDecoderTest.java    From cbor-java with 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 #27
Source File: Example48Test.java    From cbor-java with 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 #28
Source File: Example82Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(VALUE);
    Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray());
}
 
Example #29
Source File: AbstractHalfPrecisionFloatTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteOutputStream);
    encoder.encode(new HalfPrecisionFloat(value));
    Assert.assertArrayEquals(encodedValue, byteOutputStream.toByteArray());
}
 
Example #30
Source File: LanguageTaggedStringDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test(expected = CborException.class)
public void testExceptionOnNotAnArray() throws CborException {
    List<DataItem> items = new CborBuilder().addTag(38).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();
}