Merge branch 'master' of gitee.com:willfree/min-dev-java

This commit is contained in:
free will committed 2021-03-15 17:28:09 +08:00
commit 92408238aa
8 files changed
+327 -33

No files matched your search

-1
View File
@@ -1,7 +1,6 @@
.idea/
.git/
out/
pom.xml
min-dev-java.iml
target
log/
+89
View File
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>min</groupId>
<artifactId>min-dev-java</artifactId>
<version>1.0</version>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- bc库, 国密算法-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.65</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.65</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 摘要运算-->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<!-- json -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<!-- mapDB kv存储 -->
<!-- https://mvnrepository.com/artifact/org.mapdb/mapdb -->
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>3.0.8</version>
</dependency>
</dependencies>
</project>
+2 -1
View File
@@ -277,7 +277,8 @@ public class Block {
* @return
*/
public boolean isValid(){
return this.tlvType.isEqual(TLV.TlvInvalid);
// return this.tlvType.isEqual(TLV.TlvInvalid);
return !this.tlvType.isEqual(TLV.TlvInvalid);
}
/**
+7 -6
View File
@@ -8,12 +8,13 @@ package encoding;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Encoder {
private byte[] buffer;
private int left; // 左指针 => 指向 Prepend* 操作下一个可以插入的位置
private int right; // 右指针 => 指向 Append* 操作下一个可以插入的位置
private boolean isInitial; // 是否初始化
private boolean isEstimator;// 是否仅测量编码后的长度,而不实际进行编码
private SizeT totalReserve; // 编码缓存数组的大小
//TODO 便于测试暂时先将private属性改成public了
public byte[] buffer;
public int left; // 左指针 => 指向 Prepend* 操作下一个可以插入的位置
public int right; // 右指针 => 指向 Append* 操作下一个可以插入的位置
public boolean isInitial; // 是否初始化
public boolean isEstimator;// 是否仅测量编码后的长度,而不实际进行编码
public SizeT totalReserve; // 编码缓存数组的大小
/**
* 编码测量初始化
+25 -2
View File
@@ -49,13 +49,15 @@ public class ByteHelper {
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
int startInt=start.getVlIntValue2Int();
int lenInt=len.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
byte[] lenbytes=new byte[lenInt];
// byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
return lenbytes;
}
public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
int startInt=start.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
byte[] lenbytes=new byte[len];
// byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,len);
return lenbytes;
}
@@ -283,4 +285,25 @@ public class ByteHelper {
System.out.println(b&0xff);
}
// 便于测试,新添加两个public方法:
// 无符号byte[]转int
public static int new_byteArrayToInt(byte[]bytes){
int value= 0;
//由高位到低位
for (int i = 0; i < bytes.length; i++) {
int offset= (bytes.length - 1 - i) * 8;
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
}
return value;
}
// 无符号byte[]转long
public static long new_byteArrayToLong(byte[]bytes){
int value= 0;
//由高位到低位
for (int i = 0; i < 8; i++) {
int offset= (bytes.length - 1 - i) * 8;
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
}
return value;
}
}
+149
View File
@@ -0,0 +1,149 @@
package encoding;
import org.junit.Test;
import util.ByteHelper;
import static org.junit.Assert.*;
/*
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 14:52 2021/3/14
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BlockTest {
@Test
public void TestCreateBlockByTypeLengthBuffer() {
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
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
//测试rawBuf数据正确性
//rawBuf[0] == 241
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
// rawBuf[1:3] == 241
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
//rawBuf[3] == 241
assertEquals(rawBuf[3],(byte) 241);
//rawBuf[4:6] == 8000
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
//rawBuf.length == 8006
assertEquals(rawBuf.length,8006);
}
//error:block.Size().size()错误。
@Test
public void TestCreateBlockByBuffer() {
byte[] buf = new byte[10000];
buf[0] = (byte)212;
buf[1] = (byte)241;
byte[] tmp = ByteHelper.uint16ToByteArray(8000);
System.arraycopy(tmp,0,buf,2,2);
Block block = Block.createBlockByBuffer(ByteHelper.getLenBytes(buf,new VlInt(0),8004),true);
assertEquals(block.getType().getVlIntValue2Int(),212);
System.out.println("测试block.Size().size()");
assertEquals(block.getLength().getVlIntValue2Int(),8000);
//block.Size()返回一个SizeT,再调用SizeT.size()
assertEquals(block.Size().size(),8004);
assertTrue(block.hasValue());
}
@Test
public void TestBuildByBlock(){
//Block构造方法测试
byte[] buf = new byte[10000];
Block block1 = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
Block block = new Block(block1);
//测试block数据正确性
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
/*测试rawBuf数据正确性
rawBuf[0] == 241
rawBuf[1:3] == 241
rawBuf[3] == 241
rawBuf[4:6] == 8000
rawBuf.length == 8006
*/
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
assertEquals(rawBuf[3],(byte) 241);
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
assertEquals(rawBuf.length,8006);
}
@Test
public void TestBuildByTypeLengthBuffer(){
//Block构造方法测试
byte[] buf = new byte[10000];
Block block = new Block(new VlInt(241),new VlInt(8000),ByteHelper.getLenBytes(buf,new VlInt(0),8000),true);
//测试block数据正确性
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
/*测试rawBuf数据正确性
rawBuf[0] == 241
rawBuf[1:3] == 241
rawBuf[3] == 241
rawBuf[4:6] == 8000
rawBuf.length == 8006
*/
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
assertEquals(rawBuf[3],(byte) 241);
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
assertEquals(rawBuf.length,8006);
}
//error
//TODO 错误处理
@Test
public void TestParseSubElements(){
byte[] buf = new byte[10000];
for (int i = 0; i < 240; 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.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));
}
}
//error
//TODO 没测完
@Test
public void TestSubElements(){
byte[] buf = new byte[10000];
Block block = new Block();
block.setType(new VlInt(7));
for (int i = 0; i < 240; i++) {
Block b = Block.createBlockByTypeLengthBuffer(new VlInt(i+1),new VlInt(6),ByteHelper.getLenBytes(buf,new VlInt(0),6),true);
block.addElement(b);
}
// System.out.println("block.hasValue() = "+block.hasValue());
// System.out.println("block.hasSubElement() = "+block.hasSubElement());
ElementContainer elements = block.getSubElements();
}
}
+36 -16
View File
@@ -44,51 +44,71 @@ public class VlIntTest {
public void getVlIntValue2Int() {
//vlint(int)的size小于4时,测试是否能准确转为int输出
int a = 222;
int b = 22222222;
int a2 = 22222222;
byte[] b1 = new byte[]{0x00,0x7f, (byte) 0xff}; //32767
byte[] b2 = new byte[]{0x00,(byte) 0x80, 0x00}; //32768
byte[] b3 = new byte[]{0x00,(byte) 0xff, (byte) 0xff};//65535
VlInt vlInt_int1 = new VlInt(a);
VlInt vlInt_int2 = new VlInt(b);
VlInt vlInt_int2 = new VlInt(a2);
VlInt vlInt_b1 = new VlInt(b1);
VlInt vlInt_b2 = new VlInt(b2);
VlInt vlInt_b3 = new VlInt(b3);
//测试size大小
assertTrue(vlInt_int1.size()<4);
assertFalse(vlInt_int2.size()<4);
//测试VlIntValue是否转为int
assertEquals(vlInt_int1.getVlIntValue2Int(),a);
assertEquals(vlInt_b1.getVlIntValue2Int(),32767);
assertEquals(vlInt_b2.getVlIntValue2Int(),32768);
assertEquals(vlInt_b3.getVlIntValue2Int(),65535);
}
//error:以bytes[]形式构造的VlInt,2^31-1 到 2^32-1之间的数会出错
@Test
public void getVlIntValue2Long() {
long l1 = new Long(0);
long l2 = new Long(1);
long l3 = new Long(4294967295L); //2^32-1
long l4 = new Long(4294967296L); //2^32
byte[] b1 = new byte[]{0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2^31-1
byte[] b2= new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
VlInt vlInt_long1 = new VlInt(l1); // size = 1
VlInt vlInt_long2 = new VlInt(l2); // size = 1
VlInt vlInt_long3 = new VlInt(l3); // size = 5
VlInt vlInt_long4 = new VlInt(l4); // size = 9
assertTrue(vlInt_long3.size()<8);
assertFalse(vlInt_long4.size()<8);
VlInt vlInt_long4 = new VlInt(b1);
VlInt vlInt_long5 = new VlInt(b2);
//以VlInt(long)形式构造的VlInt
assertEquals(vlInt_long1.getVlIntValue2Long(),l1);
assertEquals(vlInt_long2.getVlIntValue2Long(),l2);
assertEquals(vlInt_long3.getVlIntValue2Long(),l3);
///以VlInt(bytes[])形式构造的VlInt
assertEquals(vlInt_long4.getVlIntValue2Long(),2147483647L);//2^31-1
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
}
//error:以bytes[]形式构造的VlInt,在大于2^31-1的数会出错
@Test
public void getVlIntValue() {
long l1 = new Long(4294967295L); //2^32-1
long l2 = new Long(4294967296L); //2^32
BigInteger b_0 = BigInteger.valueOf(2147483647L);//2^31-1
BigInteger b_1 = BigInteger.valueOf(2147483648L);//2^31
BigInteger b = BigInteger.valueOf(4294967296L);//2^32
//2^31-1
byte[] b0 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00};
//2^32-1
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//2^32
VlInt vlInt_long = new VlInt(l1);
VlInt vlInt_long2 = new VlInt(l2);
VlInt vlInt_bigInteger = new VlInt(b);
//判断size
assertFalse(vlInt_long.size()>=9);
assertTrue(vlInt_long2.size()>=9);
assertTrue(vlInt_bigInteger.size()>=9);
//判断值是否相等
VlInt vlInt_b0 = new VlInt(b0);
VlInt vlInt_b1 = new VlInt(b1);
//以VlInt(BigInteger)形式构造的VlInt
assertEquals(vlInt_bigInteger.getVlIntValue(),b);
///以VlInt(bytes[])形式构造的VlInt
assertEquals(vlInt_b0.getVlIntValue(),b_0);
assertEquals(vlInt_b1.getVlIntValue(),b_1);
}
@Test
+19 -7
View File
@@ -29,7 +29,7 @@ import static org.junit.Assert.*;
assertArrayEquals(b2,test2);
}
//error
//todo:对start值进行限定,应当小于Byte数组的length
@Test
public void getFirstByte() {
byte[] b0 = new byte[]{0x00,0x00,0x01};
@@ -39,9 +39,17 @@ import static org.junit.Assert.*;
assertEquals(firstbyte,test[0]);
}
//error
@Test
public void getLenBytes() {
byte[] buffer = new byte[]{0x00,0x00,0x01, (byte) 0xff}; //255
byte[] test = new byte[]{0x00,0x00,0x01};
VlInt start = new VlInt(0);
VlInt len = new VlInt(3);
byte[]b1 = ByteHelper.getLenBytes(buffer,start,len);
byte[] b2= ByteHelper.getLenBytes(buffer,start,3);
assertArrayEquals(test,b1);
assertArrayEquals(test,b2);
}
// @Test
@@ -298,7 +306,7 @@ import static org.junit.Assert.*;
assertEquals(a9,65535);
}
//error
@Test
public void uint16ToByteArray() {
ByteHelper byteHelper = new ByteHelper();
@@ -368,7 +376,7 @@ import static org.junit.Assert.*;
assertEquals(a9,65535);
}
//error
@Test
public void uint32ToLong() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
@@ -415,7 +423,7 @@ import static org.junit.Assert.*;
}
//error
@Test
public void uint32ToByteArray() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
@@ -464,7 +472,7 @@ import static org.junit.Assert.*;
assertArrayEquals(a13,b13);
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToLong() {
@@ -485,7 +493,11 @@ import static org.junit.Assert.*;
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
byte[] b = new byte[]{(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)1};//1
ByteHelper byteHelper = new ByteHelper();
long a = byteHelper.uint64ToLong(b);
assertEquals(a,1);
// byte[] b_test = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x01};//2147483649
// long a_test = byteHelper.uint64ToLong(b_test);
@@ -530,7 +542,7 @@ import static org.junit.Assert.*;
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToByteArray() {