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

This commit is contained in:
free will
2021-04-22 20:52:45 +08:00
13 changed files with 447 additions and 109 deletions
+22 -13
View File
@@ -19,15 +19,24 @@
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>8</source>-->
<!-- <target>8</target>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
</build>
<dependencies>
@@ -97,11 +106,11 @@
<!-- Sqlite Driver compatible with sqlcipher-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<!-- <dependency>-->
<!-- <groupId>org.xerial</groupId>-->
<!-- <artifactId>sqlite-jdbc</artifactId>-->
<!-- <version>3.28.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.28.0</version>
</dependency>
<dependency>
<groupId>io.github.willena</groupId>
<artifactId>sqlite-jdbc</artifactId>
@@ -130,11 +139,11 @@
<!-- Unix Socket -->
<!-- https://mvnrepository.com/artifact/com.github.jnr/jnr-unixsocket -->
<!-- <dependency>-->
<!-- <groupId>com.github.jnr</groupId>-->
<!-- <artifactId>jnr-unixsocket</artifactId>-->
<!-- <version>0.18</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.cloudbees.util/jnr-unixsocket-nodep -->
<dependency>
<groupId>com.cloudbees.util</groupId>
+6
View File
@@ -254,6 +254,8 @@ public class Block {
try {
tlvType = TLV.readType(this.value, new VlInt(start));
} catch (TLVException e) {
// go 版本 先清空再抛出异常 这里是否需要清空?
this.clearElements();
throw new BlockException("parseSubElements" + e.getMessage());
}
if (tlvType == null) {
@@ -267,6 +269,8 @@ public class Block {
try {
tlvLength = TLV.readVarNumber(this.value, new VlInt(start));
} catch (TLVException e) {
// go 版本 先清空再抛出异常 这里是否需要清空?
this.clearElements();
throw new BlockException("parseSubElements" + e.getMessage());
}
if (tlvType == null) {
@@ -290,6 +294,8 @@ public class Block {
try {
block = createBlockByTypeLengthBuffer(tlvType, tlvLength, subBlockValue, true);
} catch (BlockException e) {
// go 版本 先清空再抛出异常 这里是否需要清空?
this.clearElements();
return false;
}
if (block == null) {
+14 -14
View File
@@ -8,18 +8,18 @@ package logicface;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class LogicFaceCounters {
public static long InCPacketN; // 从本接口流入的普通推式包的个数
public static long OutCPacketN; // 从本接口流出的普通推式包的个数
public static long DropCPacketN; // 从本接口流入后被丢弃的普通推式包的个数
public static long InInterestN; // 从本接口流入的兴趣包的个数
public static long OutInterestN; // 从本接口流出的兴趣包的个数
public static long DropInterestN; // 从本接口流入后被丢弃的兴趣包的个数
public static long InDataN; // 从本接口流入的数据包的个数
public static long OutDataN; // 从本接口流出的数据包的个数
public static long DropDataN; // 从本接口流入后被丢弃的数据包的个数
public static long InNackN; // 从本接口流入的Nack包的个数
public static long OutNackN; // 从本接口流出的Nack包的个数
public static long DropNackN; // 从本接口流入后被丢弃的Nack包的个数
public static long InBytesN; // 从本接口流入的数据字节数
public static long OutBytesN; // 从本接口流出的数据字节数
public long InCPacketN; // 从本接口流入的普通推式包的个数
public long OutCPacketN; // 从本接口流出的普通推式包的个数
public long DropCPacketN; // 从本接口流入后被丢弃的普通推式包的个数
public long InInterestN; // 从本接口流入的兴趣包的个数
public long OutInterestN; // 从本接口流出的兴趣包的个数
public long DropInterestN; // 从本接口流入后被丢弃的兴趣包的个数
public long InDataN; // 从本接口流入的数据包的个数
public long OutDataN; // 从本接口流出的数据包的个数
public long DropDataN; // 从本接口流入后被丢弃的数据包的个数
public long InNackN; // 从本接口流入的Nack包的个数
public long OutNackN; // 从本接口流出的Nack包的个数
public long DropNackN; // 从本接口流入后被丢弃的Nack包的个数
public long InBytesN; // 从本接口流入的数据字节数
public long OutBytesN; // 从本接口流出的数据字节数
}
+43 -49
View File
@@ -10,6 +10,7 @@ import javafx.concurrent.Task;
import logicface.CallbackInterface.*;
import packet.*;
import util.ConcurrentHelper;
import util.ReschedulableTimer;
import util.TimeHelper;
import java.util.*;
@@ -26,20 +27,27 @@ import java.util.concurrent.locks.ReentrantLock;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class LogicFaceICN extends LogicFace {
public Timer timer; // 定时器,在processEvent函数中就被设置成很大的值
public ReschedulableTimer timer; // 定时器,在processEvent函数中就被设置成很大的值
public long recentExpireTime; // 最近将要超时的时间戳
public Map<String, PITEntry> mPit; // PIT表。不初始化,可以根据它来判断本face是否初始化
public Lock timeoutEventHeapLock=new ReentrantLock(); // 锁, mpit 、timeoutEventHeap、recentExpireTime
public Lock timeoutEventHeapLock; // 锁, mpit 、timeoutEventHeap、recentExpireTime
// 超时事件堆, 以超时时间排序的最小堆,初始化大小为1000
public PriorityBlockingQueue<TimeoutEvent> timeoutEventHeap
=new PriorityBlockingQueue<TimeoutEvent>(1000,new Comparator<TimeoutEvent>(){
@Override
public int compare(TimeoutEvent o1, TimeoutEvent o2) {
return Long.compare(o1.timeoutTime,o2.timeoutTime);
}
});
public Map<String, OnInterestInterface> mFib=new ConcurrentHashMap<>();
public List<PendingPacket> pendingPacketList=new LinkedList<>();
public PriorityBlockingQueue<TimeoutEvent> timeoutEventHeap;
public Map<String, OnInterestInterface> mFib;
public List<PendingPacket> pendingPacketList;
public LogicFaceICN() {
// PIT表。不初始化,可以根据它来判断本face是否初始化
timeoutEventHeapLock = new ReentrantLock();
timeoutEventHeap = new PriorityBlockingQueue<>(1000,new Comparator<TimeoutEvent>(){
@Override
public int compare(TimeoutEvent o1, TimeoutEvent o2) {
return Long.compare(o1.timeoutTime,o2.timeoutTime);
}
});
mFib = new ConcurrentHashMap<>();
pendingPacketList = new ArrayList<>();
}
/**
* 通过最长匹配原则查找FIB表,找到合适的兴趣包处理函数
@@ -78,6 +86,9 @@ public class LogicFaceICN extends LogicFace {
}
IdentifierWrapper identifierWrapper=interest.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry=this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry == null) {
LoggerHelper.logger.info("no match pit entry");
}
OnInterestInterface oI=this.lookUpFib(identifierWrapper.getIdentifier());
if(oI!=null){
oI.onInterest(interest);
@@ -119,6 +130,7 @@ public class LogicFaceICN extends LogicFace {
IdentifierWrapper identifierWrapper=data.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry = this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry==null){
LoggerHelper.logger.info("no match pit entry");
return;
}
pitEntry.onDataInterface.onData(pitEntry.interest,data);
@@ -141,6 +153,7 @@ public class LogicFaceICN extends LogicFace {
IdentifierWrapper identifierWrapper=nack.interest.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry = this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry==null){
LoggerHelper.logger.info("no match pit entry");
return;
}
pitEntry.onNackInterface.onNack(pitEntry.interest,nack);
@@ -182,20 +195,10 @@ public class LogicFaceICN extends LogicFace {
}
}
if(this.timeoutEventHeap.size()>0){
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},this.timeoutEventHeap.peek().timeoutTime-currentTime);
this.timer.reset(this.timeoutEventHeap.peek().timeoutTime-currentTime);
this.recentExpireTime=this.timeoutEventHeap.peek().timeoutTime;
}else{
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},500000);
this.timer.reset(500000);
this.recentExpireTime=currentTime+500000;
}
this.timeoutEventHeapLock.unlock();
@@ -204,27 +207,21 @@ public class LogicFaceICN extends LogicFace {
/**
* 等待 timer超时,触发调用dealWithTimeout函数
*/
// private void detectTimeout(){
// while (true){
// // this.timer.C
// this.dealWithTimeout();
// }
// }
// private void detectTimeout(){
// while (true){
// // this.timer.C
// this.dealWithTimeout();
// }
// }
/**
* 在 processEvent之前先初始化一些东西主要 包括初始化PIT表,和将定时器启动起来
*/
public void initBeforeProcessEvent(){
this.mPit=new ConcurrentHashMap<>();
this.timer=new Timer();
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},500000);
this.timer.schedule(this::dealWithTimeout,500000);
this.recentExpireTime=TimeHelper.getTimestampMS()+500000;
// ConcurrentHelper.doConcurrent(() -> detectTimeout());
// ConcurrentHelper.doConcurrent(() -> detectTimeout());
}
/**
@@ -236,6 +233,7 @@ public class LogicFaceICN extends LogicFace {
MINPacket minPacket = this.receivePacket();
if(minPacket==null){
LoggerHelper.logger.debug("LogicFaceICN.doReceivePacket: receivePacket null");
return;
}
IdentifierWrapper identifier=minPacket.identifierField.getIdentifier(0);
if(identifier==null){
@@ -259,12 +257,13 @@ public class LogicFaceICN extends LogicFace {
public void processEvent() throws LogicFaceException {
this.initBeforeProcessEvent();
for (int i = 0; i < this.pendingPacketList.size(); i++) {
if(this.pendingPacketList.get(i).data!=null){
this.putData(this.pendingPacketList.get(i).data);
PendingPacket pendingPacket = this.pendingPacketList.get(i);
if(pendingPacket.data!=null){
this.putData(pendingPacket.data);
continue;
}
this.expressInterest(this.pendingPacketList.get(i).interest,this.pendingPacketList.get(i).onDataInterface,
this.pendingPacketList.get(i).onTimeoutInterface,this.pendingPacketList.get(i).onNackInterface);
this.expressInterest(pendingPacket.interest,pendingPacket.onDataInterface,
pendingPacket.onTimeoutInterface, pendingPacket.onNackInterface);
}
this.pendingPacketList=null; // release the memory of pendingPacketLt
this.doReceivePacket();
@@ -287,12 +286,7 @@ public class LogicFaceICN extends LogicFace {
if(this.timeoutEventHeap.peek().timeoutTime<this.recentExpireTime){
long durationMs=this.timeoutEventHeap.peek().timeoutTime-TimeHelper.getTimestampMS();
this.recentExpireTime=this.timeoutEventHeap.peek().timeoutTime;
timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
}, durationMs);
timer.reset(durationMs);
}
this.timeoutEventHeapLock.unlock();
}
@@ -347,7 +341,7 @@ public class LogicFaceICN extends LogicFace {
return;
}
IdentifierWrapper interestName=interest.minPacket.identifierField.getIdentifier(0);
Identifier interestName=interest.getName();
if(interestName==null){
LoggerHelper.logger.debug("LogicFaceICN.expressInterest: minPacket getIdentifier null");
return;
@@ -388,7 +382,7 @@ public class LogicFaceICN extends LogicFace {
}
}
// TODO:似乎更新了
public void registerPrefix(Identifier identifier,OnInterestInterface onInterestInterface,
OnRegisterFailInterface onRegisterFailInterface,
OnRegisterSuccessInterface onRegisterSuccessInterface) throws LogicFaceException {
+13 -5
View File
@@ -13,9 +13,17 @@ import packet.Interest;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PITEntry{
public Interest interest = new Interest(); // 兴趣包
public long expireTime = 0; // 本PIT表项的超时时间
OnDataInterface onDataInterface = null; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface = null; // 兴趣包超时的处理函数
OnNackInterface onNackInterface = null; // 兴趣包无路由的处理函数
public Interest interest; // 兴趣包
public long expireTime; // 本PIT表项的超时时间
OnDataInterface onDataInterface; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface; // 兴趣包超时的处理函数
OnNackInterface onNackInterface; // 兴趣包无路由的处理函数
public PITEntry() {
interest = new Interest();
expireTime = 0;
onDataInterface = null;
onTimeoutInterface = null;
onNackInterface = null;
}
}
+13 -5
View File
@@ -14,9 +14,17 @@ import packet.Interest;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PendingPacket {
public Interest interest=new Interest();
public Data data=new Data();
OnDataInterface onDataInterface = null; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface = null; // 兴趣包超时的处理函数
OnNackInterface onNackInterface = null; // 兴趣包无路由的处理函数
public Interest interest;
public Data data;
OnDataInterface onDataInterface; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface; // 兴趣包超时的处理函数
OnNackInterface onNackInterface; // 兴趣包无路由的处理函数
public PendingPacket() {
interest = new Interest();
data = new Data();
onDataInterface = null;
onTimeoutInterface = null;
onNackInterface = null;
}
}
+4 -1
View File
@@ -1,5 +1,6 @@
package logicface;
import common.LoggerHelper;
import encoding.*;
import packet.LpPacket;
import packet.PacketException;
@@ -33,11 +34,13 @@ public class Transport {
public LpPacket parseByteArray2LpPacket(byte[] buf) throws LogicFaceException {
try {
Block block=new Block(buf,true);
if(block.isValid()){
if(!block.isValid()){
LoggerHelper.logger.error("recv packet from face invalid");
return null;
}
LpPacket lpPacket=new LpPacket();
if(!lpPacket.wireDecode(block)){
LoggerHelper.logger.error("parse to lpPacket error");
return null;
}
return lpPacket;
@@ -0,0 +1,44 @@
package util;
import java.util.Timer;
import java.util.TimerTask;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 21:35 2021/04/21
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ReschedulableTimer extends Timer {
private Runnable task;
private TimerTask timerTask;
public void schedule(Runnable runnable, long delay)
{
task = runnable;
timerTask = new TimerTask()
{
@Override
public void run()
{
task.run();
}
};
this.schedule(timerTask, delay);
}
public void reset(long delay)
{
timerTask.cancel();
timerTask = new TimerTask()
{
@Override
public void run()
{
task.run();
}
};
this.schedule(timerTask, delay);
}
}
@@ -21,7 +21,7 @@ import static minsecurity.crypto.TestSM4.SRC_DATA_16B;
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
@Fork(4)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
+73 -9
View File
@@ -1,19 +1,16 @@
package packet;
import component.ComponentException;
import component.Identifier;
import component.IdentifierWrapper;
import component.TTL;
import component.*;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 14:08 2021/4/7
* @Date: 14:08 2021/4/14
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class CPacketTest {
@@ -21,10 +18,20 @@ public class CPacketTest {
public void TestCPacket_GetIdentifier() throws ComponentException {
MINPacket packet=new MINPacket();
Identifier wrapper=new Identifier("/min/pku/sz");
Identifier wrapper1=new Identifier("/net/王");
Identifier wrapper1=new Identifier("/pkusz/zf");
// byte[] buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
// SignatureValue got = new SignatureValue(buf);
// byte[] buf1 = new byte[]{1, 2, 3, 4, 5, 6, 7, 9};
// SignatureValue got1 = new SignatureValue(buf1);
// SignatureAlgorithm value = new SignatureAlgorithm(0);
// SignatureInfo info = new SignatureInfo(value);
// Signature signature = new Signature(info,got);
// Signature signature1 = new Signature(info,got1);
// packet.signatureField.addSignature(signature);
// packet.signatureField.addSignature(signature1);
packet.packetType=new VlInt(3);
CPacket cPacket=new CPacket();
cPacket.setSrcIdentifier(wrapper);
cPacket.setDstIdentifier(wrapper1);
@@ -55,7 +62,18 @@ public class CPacketTest {
public void TestCPacket_WireEncode() throws EncoderException, ComponentException, PacketException, BlockException {
MINPacket packet=new MINPacket();
Identifier wrapper=new Identifier("/min/pku/sz");
Identifier wrapper1=new Identifier("/net/王");
Identifier wrapper1=new Identifier("/pkusz/zf");
// byte[] buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
// SignatureValue got = new SignatureValue(buf);
// byte[] buf1 = new byte[]{1, 2, 3, 4, 5, 6, 7, 9};
// SignatureValue got1 = new SignatureValue(buf1);
// SignatureAlgorithm value = new SignatureAlgorithm(0);
// SignatureInfo info = new SignatureInfo(value);
// Signature signature = new Signature(info,got);
// Signature signature1 = new Signature(info,got1);
// packet.signatureField.addSignature(signature);
// packet.signatureField.addSignature(signature1);
packet.packetType=new VlInt(3);
@@ -86,4 +104,50 @@ public class CPacketTest {
System.out.println("ttl: "+newPacket.getTtl().ttl());
System.out.println("value: "+ Arrays.toString(newPacket.getValue()));
}
@Test
public void TestCPacket_WireDecode() throws ComponentException, PacketException, BlockException, EncoderException {
MINPacket packet = new MINPacket();
Identifier wrapper = new Identifier("/min/pku/sz");
Identifier wrapper1 = new Identifier("/install");
// byte[] buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
// SignatureValue got = new SignatureValue(buf);
// byte[] buf1 = new byte[]{1, 2, 3, 4, 5, 6, 7, 9};
// SignatureValue got1 = new SignatureValue(buf1);
// SignatureAlgorithm value = new SignatureAlgorithm(0);
// SignatureInfo info = new SignatureInfo(value);
// Signature signature = new Signature(info,got);
// Signature signature1 = new Signature(info,got1);
// packet.signatureField.addSignature(signature);
// packet.signatureField.addSignature(signature1);
packet.packetType = new VlInt(3);
CPacket cPacket = new CPacket();
cPacket.setSrcIdentifier(wrapper);
cPacket.setDstIdentifier(wrapper1);
TTL ttl = new TTL();
ttl.setTtl(16546418374324163L);
cPacket.setTtl(ttl);
byte[] bytes={(byte)1,(byte)2,(byte)3,(byte)4,(byte)6};
cPacket.payload.setValue(bytes);
System.out.println("cPacket:"+cPacket.srcIdentifier.toUri()+","+cPacket.dstIdentifier.toUri()
+","+cPacket.getTtl().ttl()+","+Arrays.toString(cPacket.payload.getValue()));
Encoder encoder = new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize), new SizeT(0));
cPacket.wireEncode(encoder);
byte[] buf = encoder.getBuffer();
Block block = Block.createBlockByBuffer(buf,false);
// System.out.println(" block : "+block.getType()+","+block.getLength()+","+Arrays.toString(block.getValue()));
CPacket newPacket = new CPacket();
newPacket.wireDecode(block);
System.out.println("new cPacket srcUri:"+newPacket.srcIdentifier.toUri()+",dstUri:"+newPacket.dstIdentifier.toUri() +",ttl:"+newPacket.getTtl().ttl()+",block:"
+newPacket.minPacket.readOnlyField.getBlocks().getBlock(0).getValue()+","
+newPacket.minPacket.identifierField.getIdentifier(0).toUri()+","
+newPacket.minPacket.identifierField.getIdentifier(1).toUri()
);
}
}
+95 -6
View File
@@ -3,22 +3,29 @@ package packet;
import component.ComponentException;
import component.Identifier;
import encoding.*;
import minsecurity.Common;
import minsecurity.certificate.cert.CertUtils;
import minsecurity.certificate.cert.Certificate;
import minsecurity.crypto.sm2.SM2KeyPair;
import minsecurity.identity.Identity;
import minsecurity.identity.KeyParam;
import org.junit.Test;
import security.KeyChain;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 21:19 2021/4/7
* @Date: 21:19 2021/4/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class DataTest {
@Test
public void TestData_ToUri() throws ComponentException {
Data data=new Data();
Identifier name=new Identifier("/wefree/王");
Identifier name=new Identifier("/pkusz/zf");
data.setName(name);
System.out.println(data.getName().toUri());
}
@@ -40,13 +47,14 @@ public class DataTest {
* @throws BlockException
*/
@Test
public void TestData_WireEncode() throws ComponentException, EncoderException, PacketException, BlockException {
public void TestData_WireEncode_Decode() throws ComponentException, EncoderException, PacketException, BlockException {
/////////////// Encode ///////////////
Data data=new Data();
data.freshnessPeriod.setFreshnessPeriod(Long.MAX_VALUE);
byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
data.payload.setValue(value);
data.congestionMark.setCongestionLevel(Long.MAX_VALUE);
Identifier name=new Identifier("/wefree/王");
Identifier name=new Identifier("/pkusz/zf");
data.setName(name);
Encoder encoder=new Encoder();
@@ -57,6 +65,8 @@ public class DataTest {
Block block=new Block(buf,false);
System.out.println("block buf: "+Arrays.toString(block.getValue()));
/////////////// Decode ///////////////
Data newData=new Data();
System.out.println("decode res: "+newData.wireDecode(block));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
@@ -64,6 +74,85 @@ public class DataTest {
byte[] newbuf=encoder.getBuffer();
System.out.println("newbuf: "+ Arrays.toString(newbuf));
System.out.println("new uri: "+newData.toUri()
+" , freshness "+newData.freshnessPeriod.getFreshnessPeriod());
+" , freshness "+newData.freshnessPeriod.getFreshnessPeriod()
+" , payload: "+newData.payload.getValue() );
}
/**
* 随机生成身份数据
* @return Identity
*/
private Identity createRandomIdentity() throws Exception{
// 测试PersistIdentity
SM2KeyPair pair = SM2KeyPair.generateKeyPair();
Identity identity = new Identity();
identity.setName("/zf"+Math.random()); // 身份名称必须以/开头 Identifier规定
KeyParam keyParam = new KeyParam();
keyParam.PublicKeyAlgorithm = 0;
keyParam.SignatureAlgorithm = 0;
identity.setKeyParam(keyParam);
identity.setPrikey(pair.getSm2PrivateKey());
identity.setPubkey(pair.getSm2PublicKey());
identity.setPasswd("2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99");
// identity.lock("0123456789abcdef", Common.SM4ECB);
Certificate cert = new Certificate();
cert.setVersion(1);
cert.setSerialNumber(1);
cert.setPublicKey(pair.getSm2PublicKey());
cert.setSignatureAlgorithm(Common.SM3withSM2); // TODO 名字有误? SM2withSM3?
cert.setPublicKeyAlgorithm(Common.SM2);
cert.setIssueTo("root");
cert.setIssuer("root");
long timestamp = System.currentTimeMillis() / 1000;
cert.setTimestamp(timestamp); // 10bit timestamp
cert.setNotAfter(timestamp); // 10bit timestamp
cert.setNotBefore(timestamp + 1000); // 10bit timestamp
cert.setKeyUsage(Common.CertSign);
cert.setCA(true);
CertUtils.signCert(cert, pair.getSm2PrivateKey());
identity.setCert(cert);
return identity;
}
// @Test
// public void WireEncode2() throws Exception {
// //////////////////// 构建数据包 ////////////////////////
// Identifier name = new Identifier("/min/pkusz");
// Data data = new Data();
// data.setName(name);
// data.freshnessPeriod.setFreshnessPeriod(1234);
// byte[] str = "hello world".getBytes();
// data.congestionMark.setCongestionLevel(1234);
////// data.setttl(1234);
// /////////////////////////////////////////////////////
//
// //////////////////// 数据包签名 ////////////////////////
// //测试设置新身份为当前身份
// KeyChain keyChain = new KeyChain();
// Identity id = createRandomIdentity();
// keyChain.getIdentifyManager().setDefaultIdentity(id,true);
// // 输入命名解锁身份
// keyChain.setCurrentIdentity(keyChain.getIdentifyManager().getDefaultIdentity(),"0123456789abcdef");
// keyChain.signData(data);
//
//// //////////////////// 数据包签名 ////////////////////////
//// // 如果要进行包签名的话,则需要加入KeyChain
//// KeyChain keyChain = new KeyChain();
//// // 获取身份
//// Identity i = keyChain.getIdentifyManager().getIdentityByName("/localhost/operator");
//// // 将身份对应的加密秘钥作为第二个参数传入
//// keyChain.setCurrentIdentity(i,"pkusz123pkusz123");
//// keyChain.signData(data);
//// /////////////////////////////////////////////////////
////
// // 获取编码结果
// Encoder encoder = new Encoder();
// encoder.encoderReset(new SizeT(Encoder.MaxPacketSize), new SizeT(0));
// data.wireEncode(encoder);
// byte[] buf = encoder.getBuffer();
// System.out.println(Arrays.toString(buf));
// }
}
+40 -4
View File
@@ -4,22 +4,24 @@ import component.ComponentException;
import component.Identifier;
import component.TTL;
import encoding.*;
import minsecurity.identity.Identity;
import org.junit.Test;
import security.KeyChain;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 10:32 2021/4/8
* @Date: 10:32 2021/4/14
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class InterestTest {
@Test
public void TestInterest_ToUri() throws ComponentException {
Interest interest=new Interest();
Identifier name=new Identifier("/wefree/王");
Identifier name=new Identifier("/pkusz/zf");
interest.setName(name);
System.out.println(interest.getName().toUri());
}
@@ -48,7 +50,7 @@ public class InterestTest {
byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
interest.payload.setValue(value);
interest.congestionMark.setCongestionLevel(Long.MAX_VALUE);
Identifier name=new Identifier("/wefree/王");
Identifier name=new Identifier("/pkusz/zf");
interest.setName(name);
Encoder encoder=new Encoder();
@@ -68,4 +70,38 @@ public class InterestTest {
System.out.println("new uri: "+newInterest.toUri()
+" , ttl "+newInterest.ttl.ttl());
}
// @Test
// public void TestInterest_WireEncode2() throws Exception {
// Identifier name = new Identifier("/min/pkusz");
// //////////////////// 构建兴趣包 ////////////////////////
// Interest interest = new Interest();
// interest.setName(name);
// interest.canBePrefix.setCanBePrefix(true);
// interest.mustBeRefresh.setMustBeRefresh(true);
// interest.nonce.setNonce(1234);
// interest.hopLimit.setHopLimit(1234);
// interest.congestionMark.setCongestionLevel(1234);
// interest.ttl.setTtl(1234);
// byte[] str = "hello world".getBytes();
// interest.payload.setValue(str);
// //////////////////////////////////////////////////////
//
// //////////////////// 兴趣包签名 ////////////////////////
// // 如果要进行包签名的话,则需要加入KeyChain
// KeyChain keyChain = new KeyChain();
//// Identity i = keyChain.getIdentifyManager().getIdentityByName("/min123");
// Identity i = keyChain.getIdentifyManager().getIdentityByName("/localhost/operator");
// keyChain.setCurrentIdentity(i,"pkusz123pkusz123");
// keyChain.signInterest(interest);
// /////////////////////////////////////////////////////
//
// // 获取编码结果
// Encoder encoder = new Encoder();
// encoder.encoderReset(new SizeT(Encoder.MaxPacketSize), new SizeT(0));
// interest.wireEncode(encoder);
// byte[] buf = encoder.getBuffer();
// System.out.println(Arrays.toString(buf));
//
// }
}
+79 -2
View File
@@ -1,12 +1,89 @@
package packet;
import component.*;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 11:17 2021/4/2
* @Date: 11:17 2021/4/16
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MINPacketTest {
@Test
public void TestMINPacket_IsPacketValid() throws ComponentException, EncoderException, PacketException, BlockException {
MINPacket packet = new MINPacket();
IdentifierWrapper wrapper = new IdentifierWrapper();
wrapper.createCommonIdentifierByString("/min/pku/sz");
IdentifierWrapper wrapper1 = new IdentifierWrapper(new VlInt(105), "/install");
packet.identifierField.setIdentifier(0,wrapper);
packet.identifierField.setIdentifier(1,wrapper1);
byte[] buf = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
SignatureValue got = new SignatureValue(buf);
byte[] buf1 = new byte[]{1, 2, 3, 4, 5, 6, 7, 9};
SignatureValue got1 = new SignatureValue(buf1);
SignatureAlgorithm value = new SignatureAlgorithm(0);
SignatureInfo info = new SignatureInfo(value);
Signature signature = new Signature(info,got);
Signature signature1 = new Signature(info,got1);
packet.signatureField.addSignature(signature);
packet.signatureField.addSignature(signature1);
packet.packetType = new VlInt(3);
System.out.println("packet:"+packet.isPacketValid());
Encoder encoder = new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize), new SizeT(0));
packet.wireEncode(encoder);
byte[] packetBuf = encoder.getBuffer();
System.out.println("packetBuf:"+ Arrays.toString(packetBuf));
System.out.println("encode res: "+packet.wireEncode(encoder));
// /////////////// Decode /////////////////
// Block block = new Block(packetBuf,false);
// MINPacket newPacket = new MINPacket();
// System.out.println("decode res: "+ newPacket.wireDecode(block));
// newPacket.wireDecode(block);
// System.out.println("IdentifiersContainer size: "+newPacket.identifierField.getIdentifiers().length());
// System.out.println("new v:"+newPacket.identifierField.getIdentifiers().getElement(0).toUri());
}
@Test
public void TestMINPacket_WireDecode() throws ComponentException, EncoderException, PacketException, BlockException {
MINPacket packet = new MINPacket();
IdentifierWrapper wrapper = new IdentifierWrapper();
wrapper.createCommonIdentifierByString("/min/pku/sz");
IdentifierWrapper wrapper1 = new IdentifierWrapper(new VlInt(105), "/install");
packet.identifierField.setIdentifier(0,wrapper);
packet.identifierField.setIdentifier(1,wrapper1);
packet.packetType = new VlInt(3);
// System.out.println("v:"+packet.identifierField.getIdentifiers().getElement(0).toUri());
System.out.println("packet:"+packet.isPacketValid());
Encoder encoder = new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize), new SizeT(0));
packet.wireEncode(encoder);
byte[] packetBuf = encoder.getBuffer();
System.out.println("packetBuf:"+ Arrays.toString(packetBuf));
Block block = new Block(packetBuf,false);
MINPacket newPacket = new MINPacket();
System.out.println("decode res1:"+ newPacket.wireDecode(block));
newPacket.wireDecode(block);
System.out.println("decode res2:"+ newPacket.wireDecode(block));
System.out.println("new v:"+newPacket.identifierField.getIdentifiers().getElement(0).toUri());
}
}