Search in sources :

Example 1 with CborEncoder

use of co.nstant.in.cbor.CborEncoder in project identity-credential by google.

the class UtilTests method cborCalendarForeign.

@Test
public void cborCalendarForeign() throws CborException {
    ByteArrayOutputStream baos;
    byte[] data;
    // milliseconds, non-standard format
    baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().addTag(0).add("2019-07-08T11:51:42.25Z").build());
    data = baos.toByteArray();
    assertEquals("tag 0 '2019-07-08T11:51:42.250Z'", Util.cborPrettyPrint(Util.cborEncodeDateTime(Util.cborDecodeDateTime(data))));
    // milliseconds set to 0
    baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().addTag(0).add("2019-07-08T11:51:42.0Z").build());
    data = baos.toByteArray();
    assertEquals("tag 0 '2019-07-08T11:51:42Z'", Util.cborPrettyPrint(Util.cborEncodeDateTime(Util.cborDecodeDateTime(data))));
    // we only support millisecond-precision
    baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().addTag(0).add("2019-07-08T11:51:42.9876Z").build());
    data = baos.toByteArray();
    assertEquals("tag 0 '2019-07-08T11:51:42.987Z'", Util.cborPrettyPrint(Util.cborEncodeDateTime(Util.cborDecodeDateTime(data))));
    // milliseconds and timezone
    baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().addTag(0).add("2019-07-08T11:51:42.26-11:30").build());
    data = baos.toByteArray();
    assertEquals("tag 0 '2019-07-08T11:51:42.260-11:30'", Util.cborPrettyPrint(Util.cborEncodeDateTime(Util.cborDecodeDateTime(data))));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CborEncoder(co.nstant.in.cbor.CborEncoder) CborBuilder(co.nstant.in.cbor.CborBuilder) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with CborEncoder

use of co.nstant.in.cbor.CborEncoder in project identity-credential by google.

the class UtilTests method prettyPrintTag.

@Test
public void prettyPrintTag() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().addTag(0).add("ABC").build());
    byte[] data = baos.toByteArray();
    assertEquals("tag 0 'ABC'", Util.cborPrettyPrint(data));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CborEncoder(co.nstant.in.cbor.CborEncoder) CborBuilder(co.nstant.in.cbor.CborBuilder) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with CborEncoder

use of co.nstant.in.cbor.CborEncoder in project identity-credential by google.

the class UtilTests method prettyPrintNull.

@Test
public void prettyPrintNull() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new SimpleValue(SimpleValueType.NULL));
    assertEquals("null", Util.cborPrettyPrint(baos.toByteArray()));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CborEncoder(co.nstant.in.cbor.CborEncoder) SimpleValue(co.nstant.in.cbor.model.SimpleValue) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with CborEncoder

use of co.nstant.in.cbor.CborEncoder in project identity-credential by google.

the class UtilTests method prettyPrintString.

@Test
public void prettyPrintString() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new UnicodeString("foobar"));
    assertEquals("'foobar'", Util.cborPrettyPrint(baos.toByteArray()));
}
Also used : UnicodeString(co.nstant.in.cbor.model.UnicodeString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CborEncoder(co.nstant.in.cbor.CborEncoder) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 5 with CborEncoder

use of co.nstant.in.cbor.CborEncoder in project identity-credential by google.

the class UtilTests method prettyPrintMultipleCompleteTypes.

@Test
public void prettyPrintMultipleCompleteTypes() throws CborException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new CborEncoder(baos).encode(new CborBuilder().add(// add string
    "text").add(// add integer
    1234).add(// add byte array
    new byte[] { 0x10 }).addArray().add(1).add("text").end().build());
    assertEquals("'text',\n" + "1234,\n" + "[0x10],\n" + "[1, 'text']", Util.cborPrettyPrint(baos.toByteArray()));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CborEncoder(co.nstant.in.cbor.CborEncoder) CborBuilder(co.nstant.in.cbor.CborBuilder) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

CborEncoder (co.nstant.in.cbor.CborEncoder)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 Test (org.junit.Test)19 MediumTest (androidx.test.filters.MediumTest)17 CborBuilder (co.nstant.in.cbor.CborBuilder)15 CborException (co.nstant.in.cbor.CborException)9 ByteString (co.nstant.in.cbor.model.ByteString)4 DataItem (co.nstant.in.cbor.model.DataItem)4 SimpleValue (co.nstant.in.cbor.model.SimpleValue)4 UnicodeString (co.nstant.in.cbor.model.UnicodeString)4 NonNull (androidx.annotation.NonNull)3 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509Certificate (java.security.cert.X509Certificate)2 ECPublicKey (java.security.interfaces.ECPublicKey)2 Collection (java.util.Collection)2 Context (android.content.Context)1 LargeTest (androidx.test.filters.LargeTest)1 MapBuilder (co.nstant.in.cbor.builder.MapBuilder)1 DoublePrecisionFloat (co.nstant.in.cbor.model.DoublePrecisionFloat)1