add test: SignatureFieldTest

This commit is contained in:
free will committed 2021-04-02 11:14:56 +08:00
1 parent 39999e0534
commit c36569fbca
2 files changed
+149 -19

No files matched your search

+9 -19
View File
@@ -13,10 +13,9 @@ import java.util.List;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class SignatureContainer {
private List<Signature> signatures;
private List<Signature> signatures= new LinkedList<>();;
public SignatureContainer() {
signatures = new LinkedList<>();
}
/**
@@ -74,10 +73,9 @@ class SignatureContainer {
}
public class SignatureField implements TlvComponentBase, IEncodingAble {
private SignatureContainer signatures;
private SignatureContainer signatures= new SignatureContainer();
public SignatureField() {
signatures = new SignatureContainer();
}
public SignatureField(SignatureContainer container) {
@@ -120,6 +118,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try{
// 如果签名区没有签名,则可以直接去掉签名区 TLV,因为它不是必须的
if (signatures.length() == 0) {
return -1;
@@ -129,8 +128,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for (int i = signatures.length() - 1; i >= 0; i--) {
Signature newSignature = signatures.getElement(i);
int tmplen = 0;
tmplen = newSignature.getSigValue().wireEncode(encoder);
int tmplen = newSignature.getSigValue().wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
@@ -143,29 +141,23 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
}
// 编码签名区 TLV-LENGTH
int tmplen = 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: " + e.getMessage());
}
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码签名区 TLV-TYPE
try {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: " + e.getMessage());
}
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: " + e.getMessage());
}
}
/**
@@ -186,11 +178,9 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
if (!block.parseSubElements()) {
return false;
}
ElementContainer subElements = block.getSubElements();
int subElementNum = subElements.length();
if (subElementNum % 2 != 0) {
// throw new Error("SignatureField.wireDecode: Invalid SignatureField TLV, require even num sub element");
return false;
}