fixed some test error

This commit is contained in:
free will committed 2021-03-22 21:51:38 +08:00
1 parent fc3671ea00
commit 5785f29ec3
12 files changed
+118 -59

No files matched your search

+48 -19
View File
@@ -23,6 +23,8 @@ public class Block {
public Block(){
this.tlvType=new VlInt(TLV.TlvInvalid);
this.length=new VlInt(0);
this.elements=new ElementContainer();
}
/**
@@ -32,15 +34,15 @@ public class Block {
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws EncoderException {
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws BlockException {
if(buffer.length==0){
return null;
throw new BlockException("createBlockByBuffer: buffer.length==0");
}
// 读取type
VlInt tlvType=TLV.readType(buffer,new VlInt(0));
if(tlvType==null){
return null;
throw new BlockException("createBlockByBuffer: tlvType read error");
}
// 读取length
@@ -48,10 +50,10 @@ public class Block {
try {
tlvLength = TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
} catch (ByteHelperException e) {
e.printStackTrace();
throw new BlockException("createBlockByBuffer: tlvLength read error");
}
if(tlvLength==null){
return null;
throw new BlockException("createBlockByBuffer: tlvLength read error");
}
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
@@ -67,7 +69,7 @@ public class Block {
* @param verifyLength
* @return
*/
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException {
return new Block(tlvType,tlvLength,buffer,verifyLength);
}
@@ -78,33 +80,55 @@ public class Block {
* @param buffer
* @param verifyLength
*/
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException{
this.tlvType=tlvType;
this.length=tlvLength;
this.elements=new ElementContainer();
// 检查长度是否正确
if(verifyLength && (!this.length.isEqual(buffer.length))){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): invalid length.");
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): invalid length.");
}
// 设置 Raw
Encoder encoder=new Encoder();
boolean resetBool=encoder.encoderReset(new SizeT(tlvType.size()+tlvLength.size()+buffer.length),new SizeT(0));
boolean resetBool= false;
try {
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());
}
if(!resetBool){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
}
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))==0){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error");
try {
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());
}
if(encoder.prependVarNumber(tlvLength)==0){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error");
try {
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());
}
if(encoder.prependVarNumber(tlvType)==0){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error");
try {
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());
}
byte[] encodedBuffer=encoder.getBuffer();
if(encodedBuffer==null){
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoder.getBuffer error");
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoder.getBuffer error");
}
this.raw=encodedBuffer;
@@ -242,13 +266,18 @@ public class Block {
try {
subBlockValue = new byte[tlvLength.getVlIntValue2Int()];
System.arraycopy(this.value,start,subBlockValue,0,tlvLength.getVlIntValue2Int());
Block block=createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
Block block= null;
try {
block = createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
} catch (BlockException e) {
return false;
}
if(block==null){
return false;
}
this.addElement(block);
start+=tlvLength.getVlIntValue2Int();
} catch (VlIntException | EncoderException e) {
} catch (VlIntException e) {
return false;
}
}
@@ -0,0 +1,14 @@
package encoding;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:53 2021/3/22
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BlockException extends Exception {
public BlockException(String msg){
super("Block."+msg);
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ public interface IEncodingAble {
* @param encoder
* @return
*/
public int wireEncode(Encoder encoder) throws EncoderException;
public int wireEncode(Encoder encoder) throws EncoderException, BlockException;
/**
* 线速解码,将一个TLV解码成一个网络包
+2 -2
View File
@@ -44,7 +44,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException {
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException, BlockException {
// this.setiEncodingAble(iEncodingAble);
if((this.rawBlock!=null)&&this.rawBlock.isValid()){
return this.rawBlock;
@@ -64,7 +64,7 @@ public class SelfEncodingBase {
* @param iEncodingAble
* @return
*/
private Block easyEncoder(IEncodingAble iEncodingAble) throws EncoderException {
private Block easyEncoder(IEncodingAble iEncodingAble) throws EncoderException, BlockException {
Encoder encoder=new Encoder();
// 首先测量目标 TLV 的大小
+5 -3
View File
@@ -59,7 +59,8 @@ public class VlInt {
new Double(pow(2, 32)).longValue())) < 0){
return 5;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 64)).longValue())) < 0){
new Double(pow(2, 64)).longValue())) <= 0){
// 这里其实是与long的最大值做对比,所以可以等
return 9;
}else{
//MIN1.0中,VlInt的size最大为9
@@ -103,7 +104,8 @@ public class VlInt {
return false;
}
if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 64)).longValue())) >= 0){
new Double(pow(2, 64)).longValue())) > 0){
// 这里其实是与long的最大值做对比,所以不能等于,暂时用大于
return false;
}
return true;
@@ -176,7 +178,7 @@ public class VlInt {
public VlInt(long l){
this.VlIntValue=BigInteger.valueOf(l);
if(!isValidVlIntValue(this.VlIntValue)){
throw new Error("VlInt(long l): The l value is invalid, it is out of the bound.");
throw new Error("VlInt(long l): out of the bound, the l value is invalid.");
}
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
this.size=SizeOfVarNumber(this.VlIntValue);
+2 -2
View File
@@ -51,7 +51,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException{
public boolean fillDataToFields() throws EncoderException, BlockException {
// 可变区 => 受保护区(无)
// 填充可变区 => 非受保护区
@@ -128,7 +128,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
// 首先将属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
+2 -2
View File
@@ -68,7 +68,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException {
public boolean fillDataToFields() throws EncoderException, BlockException {
/////////////////////////////////////////////////////////////
//// 填充可变区
// { => 可变区
@@ -216,7 +216,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
// 首先将各项属性填充到分区当中
if(this.fillDataToFields()){
return 0;
+2 -1
View File
@@ -1,5 +1,6 @@
package packet;
import encoding.BlockException;
import encoding.EncoderException;
import encoding.VlIntException;
@@ -15,7 +16,7 @@ public interface InteractWithField {
* 将包的属性填充到网络包的四个分区当中
* @return
*/
boolean fillDataToFields() throws EncoderException;
boolean fillDataToFields() throws EncoderException, BlockException;
/**
* 从网络包的四个分区当中提取出特定网络包的相关属性
+2 -2
View File
@@ -104,7 +104,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException {
public boolean fillDataToFields() throws EncoderException, BlockException {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
@@ -292,7 +292,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
// 首先将各项属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
+24 -12
View File
@@ -16,7 +16,7 @@ import static org.junit.Assert.*;
public class BlockTest {
@Test
public void TestCreateBlockByTypeLengthBuffer() throws VlIntException, EncoderException {
public void TestCreateBlockByTypeLengthBuffer() throws VlIntException, EncoderException, BlockException {
byte[] buf = new byte[10000];
Block block = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
//测试type与size
@@ -42,7 +42,7 @@ public class BlockTest {
//error:block.Size().size()错误。
@Test
public void TestCreateBlockByBuffer() throws VlIntException, EncoderException {
public void TestCreateBlockByBuffer() throws VlIntException, EncoderException, BlockException {
byte[] buf = new byte[10000];
buf[0] = (byte)212;
buf[1] = (byte)241;
@@ -54,12 +54,12 @@ public class BlockTest {
System.out.println("测试block.Size().size()");
assertEquals(block.getLength().getVlIntValue2Int(),8000);
//block.Size()返回一个SizeT,再调用SizeT.size()
assertEquals(block.Size().size(),8004);
assertEquals(block.Size().getVlIntValue2Int(),8004);
assertTrue(block.hasValue());
}
@Test
public void TestBuildByBlock() throws VlIntException, EncoderException {
public void TestBuildByBlock() throws VlIntException, EncoderException, BlockException {
//Block构造方法测试
byte[] buf = new byte[10000];
Block block1 = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
@@ -85,7 +85,7 @@ public class BlockTest {
}
@Test
public void TestBuildByTypeLengthBuffer() throws VlIntException, EncoderException {
public void TestBuildByTypeLengthBuffer() throws VlIntException, EncoderException, BlockException {
//Block构造方法测试
byte[] buf = new byte[10000];
Block block = new Block(new VlInt(241),new VlInt(8000),ByteHelper.getLenBytes(buf,new VlInt(0),8000),true);
@@ -109,31 +109,44 @@ public class BlockTest {
assertEquals(rawBuf.length,8006);
}
public static void main(String[] args){
byte a=(byte)254;
System.out.println(a);
}
//error
//TODO 错误处理
@Test
public void TestParseSubElements() throws EncoderException {
public void TestParseSubElements() throws EncoderException, BlockException, VlIntException {
byte[] buf = new byte[10000];
for (int i = 0; i < 240; i++) {
for (int i = 0; i < 24; i++) {
buf[i*8] = (byte)(i+1);
buf[i*8+1] = 6;
}
Block block = new Block(new VlInt(241),new VlInt(240*8),ByteHelper.getLenBytes(buf,new VlInt(0),240*8),true);
Block block = new Block(new VlInt(241),new VlInt(24*8),ByteHelper.getLenBytes(buf,new VlInt(0),24*8),true);
if(block.isValid()){
System.out.println("true");
}
block.parseSubElements();
//TODO 错误处理
ElementContainer elementContainer = block.getSubElements();
//测试解析数据正确性
for (int i = 0; i < elementContainer.length(); i++) {
assertEquals(elementContainer.getBlock(i).getType(),new VlInt(i+1));
assertEquals(elementContainer.getBlock(i).getLength(),new VlInt(6));
// System.out.println(elementContainer.getBlock(i).getType().equals(new VlInt(i+1)));
// System.out.println(elementContainer.getBlock(i).getLength().equals(new VlInt(6)));
assertArrayEquals(elementContainer.getBlock(i).getType().getVlIntBytes(),new VlInt(i+1).getVlIntBytes());
assertArrayEquals(elementContainer.getBlock(i).getLength().getVlIntBytes(),new VlInt(6).getVlIntBytes());
assertEquals(elementContainer.getBlock(i).getType().getVlIntValue2Int(),new VlInt(i+1).getVlIntValue2Int());
assertEquals(elementContainer.getBlock(i).getLength().getVlIntValue2Int(),new VlInt(6).getVlIntValue2Int());
assertEquals(elementContainer.getBlock(i).getType().size(),new VlInt(i+1).size());
assertEquals(elementContainer.getBlock(i).getLength().size(),new VlInt(6).size());
}
}
//error
//TODO 没测完
@Test
public void TestSubElements() throws EncoderException {
public void TestSubElements() throws EncoderException, BlockException {
byte[] buf = new byte[10000];
Block block = new Block();
block.setType(new VlInt(7));
@@ -144,6 +157,5 @@ public class BlockTest {
// System.out.println("block.hasValue() = "+block.hasValue());
// System.out.println("block.hasSubElement() = "+block.hasSubElement());
ElementContainer elements = block.getSubElements();
}
}
+14 -14
View File
@@ -19,7 +19,7 @@ import static org.junit.Assert.*;
public class EncoderTest {
int MaxPacketSize = 8800;
@Test
public void TestEmpty() {
public void TestEmpty() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
@@ -38,7 +38,7 @@ public class EncoderTest {
}
@Test
public void TestEncoder_PrependByte(){
public void TestEncoder_PrependByte() throws EncoderException, VlIntException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
@@ -78,7 +78,7 @@ public class EncoderTest {
}
@Test
public void TestEncoder_AppendByte(){
public void TestEncoder_AppendByte() throws EncoderException, VlIntException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
@@ -109,7 +109,7 @@ public class EncoderTest {
}
@Test
public void TestEncoder_PrependByteArray(){
public void TestEncoder_PrependByteArray() throws EncoderException, VlIntException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
@@ -142,7 +142,7 @@ public class EncoderTest {
}
@Test
public void TestEncoder_AppendByteArray(){
public void TestEncoder_AppendByteArray() throws EncoderException, VlIntException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
@@ -179,7 +179,7 @@ public class EncoderTest {
//error
@Test
public void TestPrependVarNumber(){
public void TestPrependVarNumber() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
@@ -228,7 +228,7 @@ public class EncoderTest {
assertEquals(num3,4294967296L);
// TODO 测试添加64位整型所能表示的最大值
// uint64最大值在java中无法表示
// uint64最大值在java中无法表示=>用Long.MaxValue
}
@@ -236,7 +236,7 @@ public class EncoderTest {
@Test
public void TestAppendVarNumber(){
public void TestAppendVarNumber() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
@@ -285,7 +285,7 @@ public class EncoderTest {
}
@Test
public void TestPrependNonNegativeInteger(){
public void TestPrependNonNegativeInteger() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0));
@@ -326,7 +326,7 @@ public class EncoderTest {
}
@Test
public void TestAppendNonNegativeInteger(){
public void TestAppendNonNegativeInteger() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
@@ -373,7 +373,7 @@ public class EncoderTest {
}
@Test
public void TestPrependByteArrayBlock(){
public void TestPrependByteArrayBlock() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0));
@@ -410,7 +410,7 @@ public class EncoderTest {
}
@Test
public void TestAppendByteArrayBlock(){
public void TestAppendByteArrayBlock() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
@@ -447,7 +447,7 @@ public class EncoderTest {
}
@Test
public void TestPrependBlock(){
public void TestPrependBlock() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0));
@@ -478,7 +478,7 @@ public class EncoderTest {
}
@Test
public void TestAppendBlock(){
public void TestAppendBlock() throws EncoderException {
Encoder encoder = new Encoder();
Encoder estimator = new Encoder();
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
+2 -1
View File
@@ -2,6 +2,7 @@ package others;
import component.CanBePrefix;
import encoding.Block;
import encoding.BlockException;
import encoding.EncoderException;
import encoding.SelfEncodingBase;
@@ -13,7 +14,7 @@ import encoding.SelfEncodingBase;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestCallBack {
public static void main(String[] args) throws EncoderException {
public static void main(String[] args) throws EncoderException, BlockException {
CanBePrefix canBePrefix=new CanBePrefix();
Block block=new SelfEncodingBase().selfWireEncode(canBePrefix);
}