add test: MutableDangerousFieldTest

This commit is contained in:
free will committed 2021-03-25 21:06:35 +08:00
1 parent 28f963c28a
commit 83992bed6c
4 files changed
+227 -70

No files matched your search

@@ -12,129 +12,135 @@ import encoding.*;
public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
public MutableDangerousField(){
blocks=new ElementContainer();
public MutableDangerousField() {
blocks = new ElementContainer();
}
/**
* 往可变非受保护区添加一个Block
*
* @param block
*/
public void addBlock(Block block){
public void addBlock(Block block) {
blocks.addElement(block);
}
/**
* 获取可变非受保护区指定位置的 Block
*
* @return
*/
public Block getBlock(int index) throws ComponentException {
try {
return this.blocks.getBlock(index);
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.getBlock: "+e.getMessage());
throw new ComponentException("MutableDangerousField.getBlock: " + e.getMessage());
}
}
/**
* 清空可变非受保护区中,所有的子 Block
*/
public void clearBlocks(){
public void clearBlocks() {
this.blocks.clear();
}
/**
* 获取可变非受保护区所有的 Block
*
* @return
*/
public ElementContainer getBlocks(){
public ElementContainer getBlocks() {
return this.blocks;
}
/**
* 获取可变非受保护区中,第一个指定TLV-TYPE的子 TLV Block
*
* @return
*/
public Block getBlockByType(VlInt tlvType) throws ComponentException {
try {
return this.blocks.getBlockByType(tlvType);
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.getBlockByType: "+e.getMessage());
throw new ComponentException("MutableDangerousField.getBlockByType: " + e.getMessage());
}
}
/**
* 将可变非受保护区线速编码为一个 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try{
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if(this.blocks.length()==0){
return -1;
}
int totalLength=0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen= 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if(tmplen<=0){
try {
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if (this.blocks.length() == 0) {
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen= 0;
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableDangerousField));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
return totalLength;
} catch (EncoderException | BlockException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
throw new ComponentException("MutableDangerousField.wireEncode: " + e.getMessage());
}
}
/**
* 从 TLV Block 中解码出一个 ReadOnlyField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvMutableDangerousField))) {
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
if (!block.parseSubElements()) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.wireDecode: "+e.getMessage());
throw new ComponentException("MutableDangerousField.wireDecode: " + e.getMessage());
}
this.blocks=block.getSubElements();
this.blocks = block.getSubElements();
return true;
}
}
+33 -25
View File
@@ -24,14 +24,7 @@ public class Block {
this.elements=new ElementContainer();
}
/**
* 从一个包含TLV的字节数组中构造一个 Block 对象
* @param buffer
* @param verifyLength 是否要验证TLV的长度是否匹配,如果true,
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws BlockException {
public Block(byte[] buffer,boolean verifyLength) throws BlockException {
if(buffer.length==0){
throw new BlockException("createBlockByBuffer: buffer.length==0");
}
@@ -58,11 +51,22 @@ public class Block {
throw new BlockException("createBlockByBuffer: tlvLength read error");
}
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
this.buildBlockByTypeLengthBuffer(tlvType,tlvLength,
ByteHelper.getLenBytes(buffer,new VlInt(tlvType.size()+tlvLength.size()),
buffer.length-(tlvType.size()+tlvLength.size())),verifyLength);
}
/**
* 从一个包含TLV的字节数组中构造一个 Block 对象
* @param buffer
* @param verifyLength 是否要验证TLV的长度是否匹配,如果true,
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws BlockException {
return new Block(buffer,verifyLength);
}
/**
* 传入 T、L、V 构造一个 TLV Block
* @param tlvType
@@ -83,6 +87,18 @@ public class Block {
* @param verifyLength
*/
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException{
this.buildBlockByTypeLengthBuffer(tlvType,tlvLength,buffer,verifyLength);
}
public Block(Block block){
this.tlvType=block.tlvType;
this.length=block.length;
this.value=block.value;
this.elements=block.elements;
this.raw=block.raw;
}
public void buildBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException {
this.tlvType=tlvType;
this.length=tlvLength;
this.elements=new ElementContainer();
@@ -99,34 +115,34 @@ public class Block {
resetBool = encoder.encoderReset(new SizeT(tlvType.size()+tlvLength.size()+buffer.length),new SizeT(0));
} catch (EncoderException e) {
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error"
+e.getMessage());
+e.getMessage());
}
if(!resetBool){
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
}
try {
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))==0){
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))<0){
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error");
}
} catch (EncoderException e) {
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error"
+e.getMessage());
+e.getMessage());
}
try {
if(encoder.prependVarNumber(tlvLength)==0){
if(encoder.prependVarNumber(tlvLength)<=0){
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error");
}
} catch (EncoderException e) {
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error"
+e.getMessage());
+e.getMessage());
}
try {
if(encoder.prependVarNumber(tlvType)==0){
if(encoder.prependVarNumber(tlvType)<=0){
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error");
}
} catch (EncoderException e) {
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error"
+e.getMessage());
+e.getMessage());
}
byte[] encodedBuffer=encoder.getBuffer();
if(encodedBuffer==null){
@@ -138,14 +154,6 @@ public class Block {
this.value=buffer;
}
public Block(Block block){
this.tlvType=block.tlvType;
this.length=block.length;
this.value=block.value;
this.elements=block.elements;
this.raw=block.raw;
}
public VlInt getType() {
return tlvType;
}
@@ -235,7 +243,7 @@ public class Block {
}
// 循环解析所有的一级子TLV
for (int start = 0; start < this.value.length; start++) {
for (int start = 0; start < this.value.length; ) {
// 读取 TLV-TYPE
VlInt tlvType= null;
try {
+2 -1
View File
@@ -4,6 +4,8 @@ package encoding;
import util.ByteHelper;
import util.ByteHelperException;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
@@ -116,7 +118,6 @@ public class TLV {
*/
public static VlInt readType(byte[] buffer,VlInt start) throws TLVException {
VlInt tlvType = readVarNumber(buffer,start);
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
throw new TLVException("readType: tlvType invalid");
}
@@ -0,0 +1,142 @@
package component;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:05 2021/3/25
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MutableDangerousFieldTest {
@Test
public void TestMutableDangerousField_AddBlock() throws BlockException, ComponentException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)55, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
mutableDangerousField.addBlock(block);
System.out.println("block: "+mutableDangerousField.getBlock(0).getType()+", "
+mutableDangerousField.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(0).getValue()));
}
@Test
public void TestMutableDangerousField_GetBlock() throws BlockException, ComponentException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)55, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
System.out.println("block: "+mutableDangerousField.getBlock(0).getType()+", "
+mutableDangerousField.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(0).getValue()));
System.out.println("block1: "+mutableDangerousField.getBlock(1).getType()+", "
+mutableDangerousField.getBlock(1).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(1).getValue()));
}
@Test
public void TestMutableDangerousField_ClearBlocks() throws ComponentException, BlockException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)55, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
System.out.println("block: "+mutableDangerousField.getBlock(0).getType()+", "
+mutableDangerousField.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(0).getValue()));
System.out.println("block1: "+mutableDangerousField.getBlock(1).getType()+", "
+mutableDangerousField.getBlock(1).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(1).getValue()));
mutableDangerousField.clearBlocks();
System.out.println(mutableDangerousField.getBlocks().length());
// System.out.println("block: "+mutableDangerousField.getBlock(0).getType()+", "
// +mutableDangerousField.getBlock(0).getLength()+", "
// + Arrays.toString(mutableDangerousField.getBlock(0).getValue()));
}
@Test
public void TestMutableDangerousField_getBlocks() throws ComponentException, BlockException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)55, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
System.out.println("block: "+mutableDangerousField.getBlock(0).getType()+", "
+mutableDangerousField.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(0).getValue()));
System.out.println("block1: "+mutableDangerousField.getBlock(1).getType()+", "
+mutableDangerousField.getBlock(1).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlock(1).getValue()));
System.out.println(mutableDangerousField.getBlocks().length());
System.out.println("block: "+mutableDangerousField.getBlocks().getBlock(0).getType()+", "
+mutableDangerousField.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlocks().getBlock(0).getValue()));
}
@Test
public void TestMutableDangerousField_getBlockByType() throws ComponentException, BlockException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)56, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
System.out.println("block: "+mutableDangerousField.getBlockByType(new VlInt(55)).getType()+", "
+mutableDangerousField.getBlockByType(new VlInt(55)).getLength()+", "
+ Arrays.toString(mutableDangerousField.getBlockByType(new VlInt(55)).getValue()));
}
@Test
public void TestMutableDangerousField_WireEncode() throws EncoderException, ComponentException, BlockException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)56, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
mutableDangerousField.wireEncode(encoder);
byte[] bufget=encoder.getBuffer();
System.out.println(Arrays.toString(bufget));
}
@Test
public void TestMutableDangerousField_WireDecode()throws EncoderException, ComponentException, BlockException {
MutableDangerousField mutableDangerousField=new MutableDangerousField();
byte[] buf={(byte)56, (byte)6, (byte)0, (byte)0, (byte)110, (byte)50, (byte)24, (byte)66};
Block block=new Block(buf,false);
byte[] buf1={(byte)55,(byte)0};
Block block1=new Block(buf1,false);
mutableDangerousField.addBlock(block);
mutableDangerousField.addBlock(block1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
mutableDangerousField.wireEncode(encoder);
byte[] bufget=encoder.getBuffer();
Block newBlock=new Block(bufget,false);
System.out.println(Arrays.toString(bufget));
MutableDangerousField mutableDangerousField1=new MutableDangerousField();
mutableDangerousField1.wireDecode(newBlock);
System.out.println(mutableDangerousField1.getBlocks().length());
System.out.println("block: "+mutableDangerousField1.getBlock(0).getType()+", "
+mutableDangerousField1.getBlock(0).getLength()+", "
+ Arrays.toString(mutableDangerousField1.getBlock(0).getValue()));
System.out.println("block1: "+mutableDangerousField1.getBlock(1).getType()+", "
+mutableDangerousField1.getBlock(1).getLength()+", "
+ Arrays.toString(mutableDangerousField1.getBlock(1).getValue()));
}
}