add test: KeyLocatorTest

This commit is contained in:
free will committed 2021-03-31 20:27:01 +08:00
1 parent 025d2f1c62
commit c8f0e35f1b
4 files changed
+136 -42

No files matched your search

@@ -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();
}
}
}
+35 -39
View File
@@ -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) {
-1
View File
@@ -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
+100
View File
@@ -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());
}
}