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;
}
@@ -0,0 +1,140 @@
package component;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:00 2021/4/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureFieldTest {
@Test
public void TestSignatureField_AddSignature() throws ComponentException {
byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)8};
SignatureValue got=new SignatureValue(buf);
byte[] buf1={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)9};
SignatureValue got1=new SignatureValue(buf1);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
System.out.println("signature num: "+field.getSignatures().length());
field.addSignature(signature1);
System.out.println("signature num: "+field.getSignatures().length());
}
@Test
public void TestSignatureField_GetSignature() throws ComponentException {
byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)8};
SignatureValue got=new SignatureValue(buf);
byte[] buf1={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)9};
SignatureValue got1=new SignatureValue(buf1);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
System.out.println("signature num: "+field.getSignatures().length());
Signature get=field.getSignature(0);
System.out.println("type: "+get.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("keylocator: "+get.getSigInfo().getKeyLocator().getIdentifier().toUri());
System.out.println("value: "+ Arrays.toString(get.getSigValue().getValue()));
}
@Test
public void TestSignatureField_GetSignatures() throws ComponentException {
byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)8};
SignatureValue got=new SignatureValue(buf);
byte[] buf1={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)9};
SignatureValue got1=new SignatureValue(buf1);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
System.out.println("signature num: "+field.getSignatures().length());
Signature get=field.getSignatures().getElement(1);
System.out.println("type: "+get.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("keylocator: "+get.getSigInfo().getKeyLocator().getIdentifier().toUri());
System.out.println("value: "+ Arrays.toString(get.getSigValue().getValue()));
}
@Test
public void TestSignatureField_WireDecode() throws EncoderException, ComponentException, BlockException {
byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)8};
SignatureValue got=new SignatureValue(buf);
byte[] buf1={(byte)1, (byte)2, (byte)3, (byte)4,
(byte)5, (byte)6, (byte)7, (byte)9};
SignatureValue got1=new SignatureValue(buf1);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
System.out.println("signature num: "+field.getSignatures().length());
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buff=encoder.getBuffer();
Signature get=field.getSignatures().getElement(1);
System.out.println("sf buf: "+Arrays.toString(buff));
System.out.println("type: "+get.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("keylocator: "+get.getSigInfo().getKeyLocator().getIdentifier().toUri());
System.out.println("value: "+ Arrays.toString(get.getSigValue().getValue()));
Block block=new Block(buff,false);
System.out.println(block.parseSubElements());
SignatureField field1=new SignatureField();
field1.wireDecode(block);
get=field1.getSignatures().getElement(1);
System.out.println("type: "+get.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("keylocator: "+get.getSigInfo().getKeyLocator().getIdentifier().toUri());
System.out.println("value: "+ Arrays.toString(get.getSigValue().getValue()));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field1.wireEncode(encoder);
byte[] buff1=encoder.getBuffer();
System.out.println("new buff: "+Arrays.toString(buff1));
}
}