diff --git a/src/main/java/component/SignatureType.java b/src/main/java/component/SignatureType.java index c141ca3..dfc68c3 100644 --- a/src/main/java/component/SignatureType.java +++ b/src/main/java/component/SignatureType.java @@ -108,43 +108,35 @@ public class SignatureType implements TlvComponentBase, IEncodingAble { */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm()); + int totalLength = 0; + + // 编码 TLV-VALUE + int tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm()); + 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.TlvSignatureType)); + + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; } catch (EncoderException e) { throw new ComponentException("SignatureType.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("SignatureType.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - // 编码 TLV-TYPE - try { - tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType)); - } catch (EncoderException e) { - throw new ComponentException("SignatureType.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - return totalLength; } /** @@ -154,23 +146,22 @@ public class SignatureType implements TlvComponentBase, IEncodingAble { */ @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureType))){ - return false; - } - - // 尝试从 TLE-VALUE 中读取出一个非负整数 - long result= 0; try { - result = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureType))) { + return false; + } + + // 尝试从 TLE-VALUE 中读取出一个非负整数 + long result = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + if (result < 0) { + return false; + } + this.value = new SignatureAlgorithm((int) result); + + return true; } catch (TLVException | VlIntException e) { throw new ComponentException("SignatureType.wireDecode: " + e.getMessage()); } - if(result<0){ - return false; - } - this.value=new SignatureAlgorithm((int)result); - - return true; } } diff --git a/src/test/java/component/SignatureTypeTest.java b/src/test/java/component/SignatureTypeTest.java new file mode 100644 index 0000000..f835bc0 --- /dev/null +++ b/src/test/java/component/SignatureTypeTest.java @@ -0,0 +1,76 @@ +package component; + +import encoding.*; +import org.junit.Test; +import packet.PacketException; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 10:48 2021/3/31 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class SignatureTypeTest { + @Test + public void TestCreateSignatureTypeByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/pkusz"; + IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str); + System.out.println("type: "+got.getTlvType().toString()); + got.wireEncode(encoder); + byte[] rawBuf=encoder.getBuffer(); + System.out.println("buf: "+ Arrays.toString(rawBuf)); + byte[] buf2=new byte[4]; + System.arraycopy(rawBuf,14,buf2,0,4); + Block block=new Block(new VlInt(202),new VlInt(4),buf2,true); + System.out.println("block length: "+block.getLength()); + SignatureType gotContent=new SignatureType(block); + System.out.println("sigtype: "+gotContent.getValue().getSignatureAlgorithm()); + } + + @Test + public void TestCreateSignatureTypeByValue() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + } + + @Test + public void TestSignatureType_WireEncode() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + got.wireEncode(encoder); + byte[] gotBuf=encoder.getBuffer(); + System.out.println(Arrays.toString(gotBuf)); + } + + @Test + public void TestCreateSignatureTypeByValue2() throws PacketException, EncoderException, BlockException, ComponentException { + SignatureAlgorithm value=new SignatureAlgorithm(0); + SignatureType got=new SignatureType(value); + System.out.println(got.getValue().getSignatureAlgorithm()); + Block rawBlock=new SelfEncodingBase().selfWireEncode(got); + System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue())); + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + got.wireEncode(encoder); + byte[] gotBuf=encoder.getBuffer(); + System.out.println(Arrays.toString(gotBuf)); + SignatureType newSig=new SignatureType(); + newSig.wireDecode(rawBlock); + System.out.println(newSig.getValue().getSignatureAlgorithm()); + } +}