add test: SignatureValueTest

This commit is contained in:
free will committed 2021-04-01 20:50:54 +08:00
1 parent c45667cc8d
commit deafbc7574
2 files changed
+108 -45

No files matched your search

+39 -45
View File
@@ -19,8 +19,10 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* 根据签名值字节数组创建一个 SignatureValue
* @param value
*/
public SignatureValue(byte[] value){
this.buildSignatureValueByByteArray(value);
public SignatureValue(byte[] value) throws ComponentException {
if(!this.buildSignatureValueByByteArray(value)){
throw new ComponentException("Signature(byte[] value): value invalid");
}
}
/**
@@ -28,7 +30,9 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @param block
*/
public SignatureValue(Block block) throws ComponentException {
this.buildSignatureValueByBlock(block);
if(!this.buildSignatureValueByBlock(block)){
throw new ComponentException("Signature(Block block): block invalid");
}
}
/**
@@ -66,42 +70,33 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -111,19 +106,18 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureValue))){
return false;
}
int len= 0;
try {
len = block.getLength().getVlIntValue2Int();
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureValue))) {
return false;
}
int len = block.getLength().getVlIntValue2Int();
this.value = new byte[len];
System.arraycopy(block.getValue(), 0, this.value, 0, len);
return true;
} catch (VlIntException e) {
throw new ComponentException("SignatureValue.wireDecode: "+e.getMessage());
}
this.value=new byte[len];
System.arraycopy(block.getValue(),0,this.value,0,len);
return true;
}
}