Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void receive(ServiceInfoModuleState state, ServiceInfoKeyValuePair kvPair
break;
case FdoSimCommandOwnerModule.ARGS:
if (state.isActive()) {
commandArgs = Mapper.INSTANCE.readValue(kvPair.getValue(), String[].class);
commandArgs = Mapper.INSTANCE.readFromCborByteString(kvPair.getValue(), String[].class);
logger.info("with args: " + Arrays.asList(commandArgs));
}
break;
Expand Down
34 changes: 34 additions & 0 deletions protocol/src/main/java/org/fidoalliance/fdo/protocol/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ public byte[] writeValue(Object value) throws IOException {
}
}

/**
* Writes the given object to CBOR encoded Bytes and wraps the result inside a CBOR byte string.
* This results in a CBOR byte string whose content is itself a CBOR encoded object.
*
* @param value The object to encode.
* @return A CBOR byte string containing the CBOR encoded object.
* @throws IOException An error occurred when writing the value.
*/
public byte[] writeAsCborByteString(Object value) throws IOException {
return cborMapper.writeValueAsBytes(writeValue(value));
}

/**
* Returns a CBOR Null value.
*
* @return A CBOR Null value.
*/
public byte[] cborNull() {
return new byte[]{(byte) 0xf6};
}

/**
* Writes an Object as a yaml encoded string.
*
Expand Down Expand Up @@ -191,6 +212,19 @@ public <T> T readValue(byte[] bytes, Class<T> t) throws IOException {
return reader.readValue(bytes, t);
}

/**
* Reads an object from a CBOR byte string (bstr) containing CBOR encoded data.
*
* @param bytes The cbor byte string.
* @param t The target class.
* @param <T> The target Class type.
* @return The converted result.
* @throws IOException An error occurred when reading the content.
*/
public <T> T readFromCborByteString(byte[] bytes, Class<T> t) throws IOException {
return readValue(readValue(bytes, byte[].class), t);
}

/**
* Reads a Json encoded value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import org.fidoalliance.fdo.protocol.message.ServiceInfoDocument;
import org.fidoalliance.fdo.protocol.message.ServiceInfoKeyValuePair;
import org.fidoalliance.fdo.protocol.message.ServiceInfoModuleState;
import org.fidoalliance.fdo.protocol.message.ServiceInfoQueue;
import org.fidoalliance.fdo.protocol.serviceinfo.DevMod;
import org.fidoalliance.fdo.protocol.serviceinfo.FdoSys;

public class FdoSimCommandOwnerModule implements ServiceInfoModule {

Expand Down Expand Up @@ -219,7 +217,7 @@ protected void load(ServiceInfoModuleState state, FdoSysModuleExtra extra)

kv = new ServiceInfoKeyValuePair();
kv.setKeyName(ARGS);
kv.setValue(Mapper.INSTANCE.writeValue(commandArgs));
kv.setValue(Mapper.INSTANCE.writeAsCborByteString(commandArgs));
state.getGlobalState().getQueue().add(kv);

if (instructions[i].getMayFail() != null) {
Expand Down Expand Up @@ -272,7 +270,7 @@ protected void load(ServiceInfoModuleState state, FdoSysModuleExtra extra)

kv = new ServiceInfoKeyValuePair();
kv.setKeyName(EXECUTE);
kv.setValue(Mapper.INSTANCE.writeValue(commandArgs));
kv.setValue(Mapper.INSTANCE.cborNull());
state.getGlobalState().getQueue().add(kv);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Arrays;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.fidoalliance.fdo.protocol.message.AnyType;
import org.fidoalliance.fdo.protocol.message.Hash;
import org.fidoalliance.fdo.protocol.message.HashType;
Expand All @@ -13,6 +12,10 @@
import org.fidoalliance.fdo.protocol.message.PublicKeyType;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.dataformat.cbor.CBORConstants;

import static org.junit.jupiter.api.Assertions.*;

public class SerializationTest {

@Test
Expand Down Expand Up @@ -52,4 +55,27 @@ public void Test() throws DecoderException, IOException {
byte[] expectedData = new byte[]{1,2,3};
assert (Arrays.equals(expectedData, data));
}

@Test
public void wrapCborTest() throws IOException {
String[] input = new String[]{"date", "+%s"};

byte[] outerBstr = Mapper.INSTANCE.writeAsCborByteString(input);

byte[] innerCbor = Mapper.INSTANCE.readValue(outerBstr, byte[].class);

assertTrue(CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_BYTES, outerBstr[0]));
assertTrue(CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_ARRAY, innerCbor[0]));

assertArrayEquals(input, Mapper.INSTANCE.readValue(innerCbor, String[].class));
assertArrayEquals(input, Mapper.INSTANCE.readFromCborByteString(outerBstr, String[].class));
}

@Test
public void cborNullTest() {
byte[] cbor = Mapper.INSTANCE.cborNull();

assertTrue(CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_MISC, cbor[0]));
assertEquals(CBORConstants.BYTE_NULL, cbor[0]);
}
}