mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: SignatureInfoTest
This commit is contained in:
1 parent
c8f0e35f1b
commit
c45667cc8d
6 files changed
+182
-96
No files matched your search
@@ -130,7 +130,7 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvKeyLocator));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -14,22 +14,20 @@ import encoding.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SignatureInfo implements TlvComponentBase, IEncodingAble {
|
||||
private SignatureType signatureType;
|
||||
private KeyLocator keyLocator;
|
||||
private SignatureType signatureType=new SignatureType();
|
||||
private KeyLocator keyLocator=new KeyLocator();
|
||||
|
||||
public SignatureInfo(){
|
||||
signatureType=new SignatureType();
|
||||
keyLocator=new KeyLocator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据签名值类型创建一个 SignatureInfo
|
||||
* @param value
|
||||
*/
|
||||
public SignatureInfo(SignatureAlgorithm value){
|
||||
signatureType=new SignatureType();
|
||||
keyLocator=new KeyLocator();
|
||||
this.buildSignatureInfoBySignatureTypeValue(value);
|
||||
public SignatureInfo(SignatureAlgorithm value) throws ComponentException {
|
||||
if(!this.buildSignatureInfoBySignatureTypeValue(value)){
|
||||
throw new ComponentException("SignatureInfo(SignatureAlgorithm value): value invalid");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +36,10 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
|
||||
* @param locator
|
||||
*/
|
||||
public SignatureInfo(SignatureAlgorithm value,KeyLocator locator) throws ComponentException {
|
||||
this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator);
|
||||
if(!this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator)){
|
||||
throw new ComponentException("SignatureInfo(SignatureAlgorithm value,KeyLocator locator):" +
|
||||
" value or locator invalid");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +70,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!this.keyLocator.buildKeyLocatorByIdentifier(keyLocator.getIdentifier())){
|
||||
if(!this.keyLocator.buildKeyLocatorByIdentifier(locator.getIdentifier())){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -108,51 +109,41 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws ComponentException {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
if(!this.keyLocator.isValid()){
|
||||
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
|
||||
int tmplen= 0;
|
||||
try {
|
||||
tmplen = this.keyLocator.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
|
||||
try {
|
||||
int totalLength = 0;
|
||||
// 编码 TLV-VALUE
|
||||
if (this.keyLocator.isValid()) {
|
||||
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
|
||||
int tmplen = this.keyLocator.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
}
|
||||
if(tmplen<=0){
|
||||
int tmplen = this.signatureType.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
}
|
||||
int tmplen= 0;
|
||||
tmplen = this.signatureType.wireEncode(encoder);
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
return totalLength;
|
||||
} catch (EncoderException e) {
|
||||
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
|
||||
} catch (EncoderException e) {
|
||||
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,57 +153,47 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws ComponentException {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
try {
|
||||
if(!block.parseSubElements()){
|
||||
// 检查 Type 是否正确
|
||||
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureInfo))) {
|
||||
return false;
|
||||
}
|
||||
} catch (BlockException e) {
|
||||
|
||||
// 解析子 TLV
|
||||
if (!block.parseSubElements()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean findSignatureType = false;
|
||||
int elementSize = block.getSubElements().length();
|
||||
for (int i = 0; i < elementSize; i++) {
|
||||
Block cacheBlock = block.getSubElements().getBlock(i);
|
||||
int type = cacheBlock.getType().getVlIntValue2Int();
|
||||
switch (type) {
|
||||
case TLV.TlvSignatureType:
|
||||
if (!this.signatureType.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
findSignatureType = true;
|
||||
break;
|
||||
case TLV.TlvKeyLocator:
|
||||
if (!this.keyLocator.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
|
||||
}
|
||||
}
|
||||
|
||||
// 缺失必要的 SignatureType 子TLV
|
||||
if (!findSignatureType) {
|
||||
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (BlockException | VlIntException e) {
|
||||
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
|
||||
}
|
||||
|
||||
boolean findSignatureType=false;
|
||||
int elementSize=block.getSubElements().length();
|
||||
for (int i=0;i<elementSize;i++) {
|
||||
Block cacheBlock= null;
|
||||
try {
|
||||
cacheBlock = block.getSubElements().getBlock(i);
|
||||
} catch (BlockException e) {
|
||||
throw new ComponentException("SignatureInfo.wireDecode:"+e.getMessage());
|
||||
}
|
||||
int type= 0;
|
||||
try {
|
||||
type = cacheBlock.getType().getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
switch (type){
|
||||
case TLV.TlvSignatureType:
|
||||
if(!this.signatureType.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
findSignatureType=true;
|
||||
case TLV.TlvKeyLocator:
|
||||
if(!this.keyLocator.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
|
||||
// return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 缺失必要的 SignatureType 子TLV
|
||||
if(!findSignatureType){
|
||||
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -212,10 +212,12 @@ public class Data implements IEncodingAble, InteractWithField {
|
||||
if (!this.freshnessPeriod.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvPayload:
|
||||
if (!this.payload.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
|
||||
|
||||
|
||||
@@ -258,26 +258,32 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
if (!this.canBePrefix.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvMustBeRefresh:
|
||||
if (!this.mustBeRefresh.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvInterestLifeTime:
|
||||
if (!this.interestLifeTime.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvNonce:
|
||||
if (!this.nonce.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvHopLimit:
|
||||
if (!this.hopLimit.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvPayload:
|
||||
if (!this.payload.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
|
||||
|
||||
|
||||
@@ -147,21 +147,25 @@ public class MINPacket implements IEncodingAble {
|
||||
|
||||
// 标示解析到了至少一个以上的合法标识
|
||||
existIdentifier = true;
|
||||
break;
|
||||
case TLV.TlvSignatureField: // 签名区
|
||||
// 解析签名区,保证如果存在签名,则签名均符合格式
|
||||
if (!this.signatureField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvReadOnlyField: // 只读区
|
||||
// 解析只读区
|
||||
if (!this.readOnlyField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvMutableField: // 可变区
|
||||
// 解析可变区
|
||||
if (!this.mutableField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:27 2021/3/31
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SignatureInfoTest {
|
||||
@Test
|
||||
public void TestCrateSignatureInfoBySignatureTypeValue() throws ComponentException {
|
||||
SignatureAlgorithm value=new SignatureAlgorithm(0);
|
||||
SignatureInfo get=new SignatureInfo(value);
|
||||
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestCreateSignatureInfoByBlock() throws ComponentException, EncoderException, BlockException {
|
||||
SignatureAlgorithm value=new SignatureAlgorithm(0);
|
||||
SignatureInfo get=new SignatureInfo(value);
|
||||
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
Encoder encoder=new Encoder();
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
System.out.println("get buf size: "+get.wireEncode(encoder));
|
||||
byte[] buf=encoder.getBuffer();
|
||||
System.out.println(Arrays.toString(buf));
|
||||
Block block=new Block(buf,false);
|
||||
System.out.println(block.parseSubElements());
|
||||
System.out.println("subElem: "+block.getSubElements().length());
|
||||
SignatureInfo got=new SignatureInfo(block);
|
||||
got.wireDecode(block);
|
||||
System.out.println("sigAlgo: "+got.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
System.out.println("uri: "+got.getKeyLocator().getIdentifier().toUri());
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
System.out.println("got buf size: "+got.wireEncode(encoder));
|
||||
System.out.println(Arrays.toString(encoder.getBuffer()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestSignatureInfo_WireDecodeWithValue() throws ComponentException, EncoderException, BlockException {
|
||||
SignatureAlgorithm value=new SignatureAlgorithm(0);
|
||||
SignatureInfo get=new SignatureInfo(value);
|
||||
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
Encoder encoder=new Encoder();
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
System.out.println("get buf size: "+get.wireEncode(encoder));
|
||||
byte[] buf=encoder.getBuffer();
|
||||
System.out.println(Arrays.toString(buf));
|
||||
Block block=new Block(buf,false);
|
||||
System.out.println(block.parseSubElements());
|
||||
System.out.println("subElem: "+block.getSubElements().length());
|
||||
for (int i = 0; i < block.getSubElements().length(); i++) {
|
||||
System.out.println(block.getSubElements().getBlock(i).getType());
|
||||
System.out.println(Arrays.toString(block.getSubElements().getBlock(i).getValue()));
|
||||
}
|
||||
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestSignatureInfo_WireDecodeWithLocator() throws ComponentException, EncoderException, BlockException {
|
||||
KeyLocator keyLocator=new KeyLocator("/mnfdinstall");
|
||||
System.out.println(keyLocator.getIdentifier().toUri());
|
||||
SignatureAlgorithm value=new SignatureAlgorithm(0);
|
||||
SignatureInfo get=new SignatureInfo(value,keyLocator);
|
||||
System.out.println("algo: "+get.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
System.out.println(get.getKeyLocator().getIdentifier().toUri());
|
||||
|
||||
Encoder encoder=new Encoder();
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
System.out.println("get buf size: "+get.wireEncode(encoder));
|
||||
byte[] buf=encoder.getBuffer();
|
||||
System.out.println("buf: "+Arrays.toString(buf));
|
||||
|
||||
Block block=new Block(buf,false);
|
||||
System.out.println(block.parseSubElements());
|
||||
System.out.println("subElem: "+block.getSubElements().length());
|
||||
|
||||
SignatureInfo newSig=new SignatureInfo();
|
||||
newSig.wireDecode(block);
|
||||
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
|
||||
newSig.wireEncode(encoder);
|
||||
System.out.println("new buf: "+ Arrays.toString(encoder.getBuffer()));
|
||||
System.out.println("new algo: "+newSig.getSignatureType().getValue().getSignatureAlgorithm());
|
||||
System.out.println("new uri: "+newSig.getKeyLocator().getIdentifier().toUri());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user