co.nstant.in.cbor.model.UnsignedInteger Java Examples

The following examples show how to use co.nstant.in.cbor.model.UnsignedInteger. 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: DatemItemListenerTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeZero() throws CborException {
    final int[] dataItems = new int[1];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    new CborEncoder(outputStream).encode(new CborBuilder().add(1234).build());
    new CborDecoder(new ByteArrayInputStream(outputStream.toByteArray())).decode(new DataItemListener() {

        @Override
        public void onDataItem(DataItem dataItem) {
            synchronized (dataItems) {
                ++dataItems[0];
            }
            assertTrue(dataItem instanceof UnsignedInteger);
        }

    });
    assertEquals(1, dataItems[0]);
}
 
Example #2
Source File: MapBuilderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void startMapInMap() {
    CborBuilder builder = new CborBuilder();
    List<DataItem> dataItems = builder.addMap().startMap(new ByteString(new byte[] { 0x01 })).put(1, 2).end()
        .startMap(1).end().startMap("asdf").end().end().build();
    Map rootMap = (Map) dataItems.get(0);
    DataItem keys[] = new DataItem[3];
    rootMap.getKeys().toArray(keys);
    assertEquals(keys[0], new ByteString(new byte[] { 0x01 }));
    assertEquals(keys[1], new UnsignedInteger(1));
    assertEquals(keys[2], new UnicodeString("asdf"));

    assertTrue(rootMap.get(keys[0]) instanceof Map);
    assertTrue(rootMap.get(keys[1]) instanceof Map);
    assertTrue(rootMap.get(keys[2]) instanceof Map);
}
 
Example #3
Source File: UnsignedIntegerDecoderTest.java    From cbor-java with 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 File: EncoderDecoderTest.java    From cbor-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testTagging() throws CborException {
    UnsignedInteger a = new UnsignedInteger(1);
    NegativeInteger x = new NegativeInteger(-2);

    a.setTag(1);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(a);
    encoder.encode(x);
    byte[] bytes = byteArrayOutputStream.toByteArray();
    DataItem object = CborDecoder.decode(bytes).get(0);
    Assert.assertEquals(a, object);
    Assert.assertTrue(object.hasTag());
    Assert.assertEquals(1L, object.getTag().getValue());
}
 
Example #5
Source File: CableRegistrationData.java    From webauthndemo with Apache License 2.0 6 votes vote down vote up
public static CableRegistrationData parseFromCbor(DataItem cborCableData) {
  CableRegistrationData cableData = new CableRegistrationData();

  Map cborMap = (Map) cborCableData;

  for (DataItem data : cborMap.getKeys()) {
    if (data instanceof UnicodeString) {
      switch (((UnicodeString) data).getString()) {
        case "version":
          cableData.versions = new ArrayList<>();
          cableData.versions.add(((UnsignedInteger) cborMap.get(data)).getValue().intValue());
          break;
        case "maxVersion":
          cableData.maxVersion = ((UnsignedInteger) cborMap.get(data)).getValue().intValue();
          break;
        case "authenticatorPublicKey":
          cableData.publicKey = ((ByteString) cborMap.get(data)).getBytes();
          break;
      }
    }
  }
  return cableData;
}
 
Example #6
Source File: CborEncoder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
/**
 * Encode a single {@link DataItem}.
 *
 * @param dataItem the {@link DataItem} to encode. If null, encoder encodes a
 *                 {@link SimpleValue} NULL value.
 * @throws CborException if {@link DataItem} could not be encoded or there was
 *                       an problem with the {@link OutputStream}.
 */
public void encode(DataItem dataItem) throws CborException {
    if (dataItem == null) {
        dataItem = SimpleValue.NULL;
    }

    if (dataItem.hasTag()) {
        Tag tagDi = dataItem.getTag();
        encode(tagDi);
    }

    switch (dataItem.getMajorType()) {
    case UNSIGNED_INTEGER:
        unsignedIntegerEncoder.encode((UnsignedInteger) dataItem);
        break;
    case NEGATIVE_INTEGER:
        negativeIntegerEncoder.encode((NegativeInteger) dataItem);
        break;
    case BYTE_STRING:
        byteStringEncoder.encode((ByteString) dataItem);
        break;
    case UNICODE_STRING:
        unicodeStringEncoder.encode((UnicodeString) dataItem);
        break;
    case ARRAY:
        arrayEncoder.encode((Array) dataItem);
        break;
    case MAP:
        mapEncoder.encode((Map) dataItem);
        break;
    case SPECIAL:
        specialEncoder.encode((Special) dataItem);
        break;
    case TAG:
        tagEncoder.encode((Tag) dataItem);
        break;
    default:
        throw new CborException("Unknown major type");
    }
}
 
Example #7
Source File: AbstractBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected DataItem convert(long value) {
    if (value >= 0) {
        return new UnsignedInteger(value);
    } else {
        return new NegativeInteger(value);
    }
}
 
Example #8
Source File: AbstractBuilder.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
protected DataItem convert(BigInteger value) {
    if (value.signum() == -1) {
        return new NegativeInteger(value);
    } else {
        return new UnsignedInteger(value);
    }
}
 
Example #9
Source File: Example12Test.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEncode() throws CborException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(new UnsignedInteger(new BigInteger("18446744073709551616")));
    Assert.assertArrayEquals(new byte[] { (byte) 0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
        byteArrayOutputStream.toByteArray());
}
 
Example #10
Source File: RsaKey.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(N_LABEL), new ByteString(n))
          .put(new NegativeInteger(E_LABEL), new ByteString(e)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example #11
Source File: EncoderDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws CborException {
    UnsignedInteger a = new UnsignedInteger(1);
    NegativeInteger x = new NegativeInteger(-2);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CborEncoder encoder = new CborEncoder(byteArrayOutputStream);
    encoder.encode(a);
    encoder.encode(x);
    byte[] bytes = byteArrayOutputStream.toByteArray();
    DataItem object = CborDecoder.decode(bytes).get(0);
    Assert.assertEquals(a, object);
}
 
Example #12
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 #13
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 #14
Source File: CborDecoderTest.java    From cbor-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDecodeTaggedTags() throws CborException {
    DataItem decoded = CborDecoder.decode(new byte[] { (byte) 0xC1, (byte) 0xC2, 0x02 }).get(0);

    Tag outer = new Tag(1);
    Tag inner = new Tag(2);
    UnsignedInteger expected = new UnsignedInteger(2);
    inner.setTag(outer);
    expected.setTag(inner);

    assertEquals(expected, decoded);
}
 
Example #15
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 #16
Source File: EccKey.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode() throws CborException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  List<DataItem> dataItems =
      new CborBuilder().addMap().put(new UnsignedInteger(KTY_LABEL), new UnsignedInteger(kty))
          .put(new UnsignedInteger(ALG_LABEL), new NegativeInteger(alg.encodeToInt()))
          .put(new NegativeInteger(CRV_LABEL), new UnsignedInteger(crv))
          .put(new NegativeInteger(X_LABEL), new ByteString(x))
          .put(new NegativeInteger(Y_LABEL), new ByteString(y)).end().build();
  new CborEncoder(output).encode(dataItems);
  return output.toByteArray();
}
 
Example #17
Source File: UnsignedIntegerDecoder.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
@Override
public UnsignedInteger decode(int initialByte) throws CborException {
    return new UnsignedInteger(getLengthAsBigInteger(initialByte));
}
 
Example #18
Source File: Example49Test.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
public Example49Test() {
    DataItem di = new UnsignedInteger(1363896240);
    di.setTag(1);

    VALUE = new CborBuilder().add(di).build();
}
 
Example #19
Source File: UnsignedIntegerEncoder.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(UnsignedInteger dataItem) throws CborException {
    encodeTypeAndLength(MajorType.UNSIGNED_INTEGER, dataItem.getValue());
}
 
Example #20
Source File: MapBuilderTest.java    From cbor-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testMapBuilder() {
    List<DataItem> dataItems = new CborBuilder()
            .addMap()
                .put(new UnicodeString("key"), new UnicodeString("value"))
                .put(1, true)
                .put(2, "value".getBytes())
                .put(3, 1.0d)
                .put(4, 1.0f)
                .put(5, 1L)
                .put(6, "value")
                .put("7", true)
                .put("8", "value".getBytes())
                .put("9", 1.0d)
                .put("10", 1.0f)
                .put("11", 1L)
                .put("12", "value")
                .putMap(13)
                .end()
                .putMap("14").end()
                .putMap(new UnsignedInteger(15)).end()
                .putArray(16).end()
                .putArray("17").end()
                .putArray(new UnsignedInteger(18)).end()
                .addKey(19).value(true)
                .addKey(20).value("value".getBytes())
                .addKey(21).value(1.0d)
                .addKey(22).value(1.0f)
                .addKey(23).value(1L)
                .addKey(24).value("value")
                .addKey("25").value(true)
                .addKey("26").value("value".getBytes())
                .addKey("27").value(1.0d)
                .addKey("28").value(1.0f)
                .addKey("29").value(1L)
                .addKey("30").value("value")
            .end()
            .startMap()
                .startArray(1).end()
                .startArray(new UnsignedInteger(2)).end()
            .end()
            .build();
    assertEquals(2, dataItems.size());
    assertTrue(dataItems.get(0) instanceof Map);
    Map map = (Map) dataItems.get(0);
    assertEquals(31, map.getKeys().size());
}