From deafbc757420ae9eb3039a35407f687514ee0ece Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Thu, 1 Apr 2021 20:50:54 +0800 Subject: [PATCH] add test: SignatureValueTest --- src/main/java/component/SignatureValue.java | 84 +++++++++---------- .../java/component/SignatureValueTest.java | 69 +++++++++++++++ 2 files changed, 108 insertions(+), 45 deletions(-) create mode 100644 src/test/java/component/SignatureValueTest.java diff --git a/src/main/java/component/SignatureValue.java b/src/main/java/component/SignatureValue.java index 05d5c13..ddf81c6 100644 --- a/src/main/java/component/SignatureValue.java +++ b/src/main/java/component/SignatureValue.java @@ -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; } } diff --git a/src/test/java/component/SignatureValueTest.java b/src/test/java/component/SignatureValueTest.java new file mode 100644 index 0000000..976cb24 --- /dev/null +++ b/src/test/java/component/SignatureValueTest.java @@ -0,0 +1,69 @@ +package component; + +import encoding.*; +import org.junit.Test; + +import java.util.Arrays; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 20:21 2021/4/1 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class SignatureValueTest { + @Test + public void TestCreateSignatureValueByByteArray() 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); + System.out.println(Arrays.toString(got.getValue())); + } + + @Test + public void TestCreateSignatureValueByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pku"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+Arrays.toString(rawBuf)); + + byte[] a=new byte[4]; + System.arraycopy(rawBuf,12,a,0,4); + Block block=new Block(new VlInt(200),new VlInt(4),a,true); + SignatureValue gotValue=new SignatureValue(block); + System.out.println("sv value: "+Arrays.toString(gotValue.getValue())); + } + + @Test + public void TestSignatureValue_WireEncode() throws BlockException, ComponentException, EncoderException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pku"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+Arrays.toString(rawBuf)); + + byte[] a=new byte[4]; + System.arraycopy(rawBuf,12,a,0,4); + Block block=new Block(new VlInt(200),new VlInt(4),a,true); + SignatureValue gotValue=new SignatureValue(block); + System.out.println("sv value: "+Arrays.toString(gotValue.getValue())); + + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotValue.wireEncode(encoder); + System.out.println("all: "+Arrays.toString(encoder.getBuffer())); + byte[] bytes=encoder.getBuffer(); + Block block1=new Block(bytes,false); + SignatureValue newSig=new SignatureValue(block1); + System.out.println("new value: "+Arrays.toString(newSig.getValue())); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + newSig.wireEncode(encoder); + System.out.println("new all: "+Arrays.toString(encoder.getBuffer())); + } +}