From c8f0e35f1b76b4737221554b83a791b129dfa84d Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 31 Mar 2021 20:27:01 +0800 Subject: [PATCH] add test: KeyLocatorTest --- .../java/component/IdentifierComponent.java | 3 +- src/main/java/component/KeyLocator.java | 74 ++++++------- src/main/java/encoding/Block.java | 1 - src/test/java/component/KeyLocatorTest.java | 100 ++++++++++++++++++ 4 files changed, 136 insertions(+), 42 deletions(-) create mode 100644 src/test/java/component/KeyLocatorTest.java diff --git a/src/main/java/component/IdentifierComponent.java b/src/main/java/component/IdentifierComponent.java index 5f969dc..839c130 100644 --- a/src/main/java/component/IdentifierComponent.java +++ b/src/main/java/component/IdentifierComponent.java @@ -353,9 +353,8 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble { } else if(isByteArray()){ return StringHelper.escape(new String(this.byteArrayValue)); }else{ -// return StringHelper.FormatUint(this.intValue,10); // todo: 需要对long类型按照指定编码规则编码成字符串 - return ""; + return Long.valueOf(this.intValue).toString(); } } } diff --git a/src/main/java/component/KeyLocator.java b/src/main/java/component/KeyLocator.java index 2365b27..8270ebe 100644 --- a/src/main/java/component/KeyLocator.java +++ b/src/main/java/component/KeyLocator.java @@ -13,10 +13,9 @@ import encoding.*; * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ public class KeyLocator implements TlvComponentBase, IEncodingAble { - private Identifier identifier; + private Identifier identifier=new Identifier(); public KeyLocator(){ - identifier=new Identifier(); } /** @@ -24,15 +23,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { * @param block */ public KeyLocator(Block block) throws ComponentException { - this.buildKeyLocatorByBlock(block); + if(!this.buildKeyLocatorByBlock(block)){ + throw new ComponentException("KeyLocator(Block): block invalid."); + } } /** * 根据标识字符串 创建一个 KeyLocator * @param identifierString */ - public KeyLocator(String identifierString){ - this.buildKeyLocatorByString(identifierString); + public KeyLocator(String identifierString) throws ComponentException { + if(!this.buildKeyLocatorByString(identifierString)){ + throw new ComponentException("KeyLocator(String): string invalid."); + } } /** @@ -110,42 +113,33 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = this.identifier.wireEncode(encoder); - } catch (ComponentException e) { + int totalLength = 0; + // 编码 TLV-VALUE + int tmplen = this.identifier.wireEncode(encoder); + 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(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; + } catch (ComponentException | EncoderException e) { throw new ComponentException("KeyLocator.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("KeyLocator.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - // 编码 TLV-TYPE - try { - tmplen=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - return totalLength; } /** @@ -157,17 +151,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble { public boolean wireDecode(Block block) throws ComponentException { try { // 检查Type是否正确 - if(!TLV.isValidIdentifierType(block.getType())){ + if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvKeyLocator))){ return false; } // 解析子 TLV if(!block.parseSubElements()){ return false; } + // 判断是否有且仅有一个子TLV if(block.getSubElements().length()!=1){ return false; } + // 解析子TLV为一个Identifier return this.identifier.wireDecode(block.getSubElements().getBlock(0)); } catch (BlockException e) { diff --git a/src/main/java/encoding/Block.java b/src/main/java/encoding/Block.java index 856c12d..0f74ba3 100644 --- a/src/main/java/encoding/Block.java +++ b/src/main/java/encoding/Block.java @@ -247,7 +247,6 @@ public class Block { if ((this.value.length == 0) || this.elements.length() != 0) { return true; } - // 循环解析所有的一级子TLV for (int start = 0; start < this.value.length; ) { // 读取 TLV-TYPE diff --git a/src/test/java/component/KeyLocatorTest.java b/src/test/java/component/KeyLocatorTest.java new file mode 100644 index 0000000..a1a226b --- /dev/null +++ b/src/test/java/component/KeyLocatorTest.java @@ -0,0 +1,100 @@ +package component; + +import encoding.*; +import org.checkerframework.checker.units.qual.K; +import org.junit.Test; +import packet.PacketException; + +import java.util.Arrays; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 19:08 2021/3/31 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class KeyLocatorTest { + @Test + public void TestCreateKeyLocatorByString() throws ComponentException { + String str="/install/hello/王"; + KeyLocator got=new KeyLocator(str); + System.out.println("keyLocator: "+got.getIdentifier().toUri()); + } + + @Test + public void TestCreateKeyLocatorByBlock() throws EncoderException, ComponentException, BlockException { + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + String str="/min/王/123/哈哈哈哈"; + 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)); +// rawBuf[4]=101; + byte[] buf2=new byte[rawBuf.length-2]; + System.arraycopy(rawBuf,2,buf2,0,rawBuf.length-2); + System.out.println("buf2: "+ Arrays.toString(buf2)); + Block block=new Block(new VlInt(203),new VlInt(rawBuf.length-2),buf2,true); + block.parseSubElements(); + System.out.println(block.getSubElements().getBlock(0).getType()); +// System.out.println(block.getSubElements().getBlock(1).getType()); + KeyLocator gotKey=new KeyLocator(block); + System.out.println(gotKey.getIdentifier().toUri()); + System.out.println(gotKey.isValid()); + } + + @Test + public void TestCreateKeyLocatorByComponents() throws ComponentException { + IdentifierComponent component=new IdentifierComponent("min"); + IdentifierComponent component1=new IdentifierComponent("pku"); + IdentifierComponent component2=new IdentifierComponent("王"); + IdentifierComponentContainer gotComponent=new IdentifierComponentContainer(); + gotComponent.addElement(component); + gotComponent.addElement(component1); + gotComponent.addElement(component2); + KeyLocator gotKey=new KeyLocator(gotComponent); + System.out.println(gotKey.getIdentifier().toUri()); + } + + @Test + public void TestCreateKeyLocatorByIdentifier() throws ComponentException, EncoderException { + Identifier gotIdentifier=new Identifier("/min/pku/111"); + KeyLocator gotKey=new KeyLocator(gotIdentifier); + System.out.println(gotKey.getIdentifier().toUri()); + + IdentifierComponent component=new IdentifierComponent("min"); + IdentifierComponent component1=new IdentifierComponent("pku"); + IdentifierComponent component2=new IdentifierComponent(111); + IdentifierComponentContainer gotComponent=new IdentifierComponentContainer(); + gotComponent.addElement(component); + gotComponent.addElement(component1); + gotComponent.addElement(component2); + KeyLocator gotKey1=new KeyLocator(gotComponent); + System.out.println(gotKey1.getIdentifier().toUri()); + + Encoder encoder=new Encoder(); + encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotKey.wireEncode(encoder); + System.out.println(Arrays.toString(encoder.getBuffer())); + + Encoder encoder1=new Encoder(); + encoder1.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0)); + gotKey1.wireEncode(encoder1); + System.out.println(Arrays.toString(encoder1.getBuffer())); + } + + @Test + public void TestKeyLocator_WireDecode() throws ComponentException, BlockException, EncoderException, PacketException { + Identifier gotIdentifier=new Identifier("/min/pku/111"); + KeyLocator gotKey=new KeyLocator(gotIdentifier); + System.out.println(gotKey.getIdentifier().toUri()); + + Block block=new SelfEncodingBase().selfWireEncode(gotKey); + + KeyLocator newK=new KeyLocator(); + newK.wireDecode(block); + System.out.println(newK.getIdentifier().toUri()); + } +}