From fafde2b0158e0003851632c57cf95a812e22b346 Mon Sep 17 00:00:00 2001 From: free will <2647778488@qq.com> Date: Wed, 31 Mar 2021 10:44:47 +0800 Subject: [PATCH] refresh test: ttl,etc... --- src/main/java/component/CanBePrefix.java | 50 ++++----- src/main/java/component/CongestionMark.java | 104 +++++++++--------- src/main/java/component/FreshnessPeriod.java | 96 ++++++++-------- src/main/java/component/HopLimit.java | 84 +++++++------- src/main/java/component/MustBeRefresh.java | 64 ++++++----- src/main/java/component/Nonce.java | 100 ++++++++--------- src/main/java/component/TTL.java | 95 ++++++++-------- src/test/java/component/CanBePrefixTest.java | 16 +-- .../java/component/CongestionMarkTest.java | 6 +- .../java/component/FreshnessPeriodTest.java | 6 +- src/test/java/component/HopLimitTest.java | 12 +- .../java/component/MustBeRefreshTest.java | 6 +- src/test/java/component/NonceTest.java | 5 +- src/test/java/component/TTLTest.java | 10 +- 14 files changed, 307 insertions(+), 347 deletions(-) diff --git a/src/main/java/component/CanBePrefix.java b/src/main/java/component/CanBePrefix.java index 94cabd3..35149ef 100644 --- a/src/main/java/component/CanBePrefix.java +++ b/src/main/java/component/CanBePrefix.java @@ -44,37 +44,32 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - // 这是一个存在性组件,所以如果值为false,则无需编码 - if(!this.canBePrefix){ - return -1; - } - - int totalLength=0; - - // 编码 TLV-LENGTH - int tmplen= 0; try { - tmplen = encoder.prependVarNumber(new VlInt(totalLength)); + // 这是一个存在性组件,所以如果值为false,则无需编码 + if (!this.canBePrefix) { + return -1; + } + + int totalLength = 0; + + // 编码 TLV-LENGTH + int tmplen = encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + // 编码 TLV-TYPE + tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; } catch (EncoderException e) { throw new ComponentException("CanBePrefix.wireEncode: "+e.getMessage()); } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - // 编码 TLV-TYPE - try { - tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix)); - } catch (EncoderException e) { - throw new ComponentException("CanBePrefix.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - return totalLength; } /** @@ -91,6 +86,7 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble // 这是一个存在性组件,所以存在则为 true this.canBePrefix=true; + this.doInitial(); return true; } } diff --git a/src/main/java/component/CongestionMark.java b/src/main/java/component/CongestionMark.java index 18e3852..6a1a89a 100644 --- a/src/main/java/component/CongestionMark.java +++ b/src/main/java/component/CongestionMark.java @@ -9,27 +9,27 @@ import encoding.*; * @Date: 15:54 2021/3/9 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ -public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAble { +public class CongestionMark implements TlvComponentBase, InitialAble, IEncodingAble { // 成员变量 todo: 越界问题 private long congestionMark; // 函数 - public long getCongestionLevel(){ + public long getCongestionLevel() { return this.congestionMark; } - public void setCongestionLevel(long congestionMark){ - this.congestionMark=congestionMark; + public void setCongestionLevel(long congestionMark) { + this.congestionMark = congestionMark; this.doInitial(); } // 接口实现-变量区 - private boolean initial=false; + private boolean initial = false; // 接口实现-方法区 @Override public void doInitial() { - initial=true; + initial = true; } @Override @@ -39,74 +39,68 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb /** * 将 CongestionMark 线速编码为一个 TLV + * * @param encoder * @return */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = encoder.prependNonNegativeInteger(this.congestionMark); - } catch (EncoderException e) { - throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + int totalLength = 0; - // 编码 TLV-LENGTH - try { - tmplen+=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-VALUE + int tmplen = encoder.prependNonNegativeInteger(this.congestionMark); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - // 编码 TLV-TYPE - try { - tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark)); - } catch (EncoderException e) { - throw new ComponentException("CongestionMark.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-LENGTH + tmplen += encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - return totalLength; + // 编码 TLV-TYPE + tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; + } catch (EncoderException e) { + throw new ComponentException("CongestionMark.wireEncode: " + e.getMessage()); + } } /** * 从 TLV Block 中解码出一个 CongestionMark + * * @param block * @return */ @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCongestionMark))){ - return false; - } - - // 读取 CongestionMark - long value = 0; try { - value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); - } catch (TLVException | VlIntException e) { - throw new ComponentException("CongestionMark.wireDecode: "+e.getMessage()); - } - if(value<0){ - return false; - } - this.setCongestionLevel(value); + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvCongestionMark))) { + return false; + } - return true; + // 读取 CongestionMark + long value = 0; + value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + + if (value < 0) { + return false; + } + this.setCongestionLevel(value); + + return true; + } catch (TLVException | VlIntException e) { + throw new ComponentException("CongestionMark.wireDecode: " + e.getMessage()); + } } } diff --git a/src/main/java/component/FreshnessPeriod.java b/src/main/java/component/FreshnessPeriod.java index 28966dc..e9476f1 100644 --- a/src/main/java/component/FreshnessPeriod.java +++ b/src/main/java/component/FreshnessPeriod.java @@ -9,7 +9,7 @@ import encoding.*; * @Date: 16:10 2021/3/9 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ -public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingAble { +public class FreshnessPeriod implements TlvComponentBase, InitialAble, IEncodingAble { // 成员变量 private long freshnessPeriod; @@ -24,12 +24,12 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA } // 接口实现-变量区 - private boolean initial=false; + private boolean initial = false; // 接口实现-方法区 @Override public void doInitial() { - initial=true; + initial = true; } @Override @@ -39,74 +39,66 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA /** * 将 FreshnessPeriod 线速编码为一个 TLV + * * @param encoder * @return */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = encoder.prependNonNegativeInteger(this.freshnessPeriod); - } catch (EncoderException e) { - throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + int totalLength = 0; - // 编码 TLV-LENGTH - try { - tmplen+=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-VALUE + int tmplen = encoder.prependNonNegativeInteger(this.freshnessPeriod); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - // 编码 TLV-TYPE - try { - tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod)); - } catch (EncoderException e) { - throw new ComponentException("FreshnessPeriod.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-LENGTH + tmplen += encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - return totalLength; + // 编码 TLV-TYPE + tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; + } catch (EncoderException e) { + throw new ComponentException("FreshnessPeriod.wireEncode: " + e.getMessage()); + } } /** * 从 TLV Block 中解码出一个 FreshnessPeriod + * * @param block * @return */ @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvFreshnessPeriod))){ - return false; - } - - // 读取 value - long value= 0; try { - value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); - } catch (TLVException | VlIntException e) { - throw new ComponentException("FreshnessPeriod.wireDecode: "+e.getMessage()); - } - if(value<0){ - return false; - } - this.setFreshnessPeriod(value); + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvFreshnessPeriod))) { + return false; + } - return true; + // 读取 value + long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + if (value < 0) { + return false; + } + this.setFreshnessPeriod(value); + + return true; + } catch (TLVException | VlIntException e) { + throw new ComponentException("FreshnessPeriod.wireDecode: " + e.getMessage()); + } } } diff --git a/src/main/java/component/HopLimit.java b/src/main/java/component/HopLimit.java index be407e9..ca12f9d 100644 --- a/src/main/java/component/HopLimit.java +++ b/src/main/java/component/HopLimit.java @@ -44,64 +44,54 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble { */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = encoder.prependNonNegativeInteger(this.hopLimit); + int totalLength = 0; + + // 编码 TLV-VALUE + int tmplen = encoder.prependNonNegativeInteger(this.hopLimit); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + // 编码 TLV-LENGTH + tmplen += encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + // 编码 TLV-TYPE + tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; } catch (EncoderException e) { throw new ComponentException("HopLimit.wireEncode: "+e.getMessage()); } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - // 编码 TLV-LENGTH - try { - tmplen+=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("HopLimit.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - // 编码 TLV-TYPE - try { - tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit)); - } catch (EncoderException e) { - throw new ComponentException("HopLimit.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; - - return totalLength; } @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){ - return false; - } - - // 读取 HopLimit - long value= 0; try { - value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvHopLimit))) { + return false; + } + + // 读取 HopLimit + long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + if (value < 0) { + return false; + } + this.setHopLimit(value); + + return true; } catch (TLVException | VlIntException e) { throw new ComponentException("HopLimit.wireDecode: "+e.getMessage()); } - if(value<0){ - return false; - } - this.setHopLimit(value); - - return true; } } diff --git a/src/main/java/component/MustBeRefresh.java b/src/main/java/component/MustBeRefresh.java index dd78d50..c556a0c 100644 --- a/src/main/java/component/MustBeRefresh.java +++ b/src/main/java/component/MustBeRefresh.java @@ -9,7 +9,7 @@ import encoding.*; * @Date: 16:30 2021/3/9 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ -public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAble { +public class MustBeRefresh implements TlvComponentBase, InitialAble, IEncodingAble { // 成员变量 private boolean mustBeRefresh; @@ -24,12 +24,12 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl } // 接口实现-变量区 - private boolean initial=false; + private boolean initial = false; // 接口实现-方法区 @Override public void doInitial() { - initial=true; + initial = true; } @Override @@ -39,58 +39,56 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl /** * 将 MustBeRefresh 线速编码为一个 TLV + * * @param encoder * @return */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - // 这是一个存在性组件,所以如果值为false,则无需编码 - if(!this.mustBeRefresh){ - return -1; - } - - int totalLength=0; - - // 编码 TLV-LENGTH - int tmplen= 0; try { - tmplen = encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("MustBeRefresh.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 这是一个存在性组件,所以如果值为false,则无需编码 + if (!this.mustBeRefresh) { + return -1; + } - // 编码 TLV-TYPE - try { - tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh)); - } catch (EncoderException e) { - throw new ComponentException("MustBeRefresh.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + int totalLength = 0; - return totalLength; + // 编码 TLV-LENGTH + int tmplen = encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + // 编码 TLV-TYPE + tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; + } catch (EncoderException e) { + throw new ComponentException("MustBeRefresh.wireEncode: " + e.getMessage()); + } } /** * 从 TLV Block 中解码出一个 MustBeRefresh + * * @param block * @return */ @Override public boolean wireDecode(Block block) { // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMustBeRefresh))){ + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvMustBeRefresh))) { return false; } // 这是一个存在性组件,所以存在则为 true - this.mustBeRefresh=true; + this.mustBeRefresh = true; + this.doInitial(); return true; } } diff --git a/src/main/java/component/Nonce.java b/src/main/java/component/Nonce.java index 1a5962b..ea32876 100644 --- a/src/main/java/component/Nonce.java +++ b/src/main/java/component/Nonce.java @@ -11,7 +11,7 @@ import java.util.Random; * @Date: 17:13 2021/3/9 * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 */ -public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble { +public class Nonce implements TlvComponentBase, InitialAble, IEncodingAble { // 成员变量 private long nonce; @@ -28,18 +28,18 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble { /** * 刷新Nonce,随机生成一个数值传递给Nonce */ - public void refreshNonce(){ - long randomLong=Math.abs(new Random().nextLong()); + public void refreshNonce() { + long randomLong = Math.abs(new Random().nextLong()); this.setNonce(randomLong); } // 接口实现-变量区 - private boolean initial=false; + private boolean initial = false; // 接口实现-方法区 @Override public void doInitial() { - initial=true; + initial = true; } @Override @@ -49,74 +49,66 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble { /** * 将 Nonce 线速编码为一个 TLV + * * @param encoder * @return */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - - // 编码 TLV-VALUE - int tmplen= 0; try { - tmplen = encoder.prependNonNegativeInteger(this.nonce); - } catch (EncoderException e) { - throw new ComponentException("Nonce.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + int totalLength = 0; - // 编码 TLV-LENGTH - try { - tmplen+=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("Nonce.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-VALUE + int tmplen = encoder.prependNonNegativeInteger(this.nonce); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - // 编码 TLV-TYPE - try { - tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNonce)); - } catch (EncoderException e) { - throw new ComponentException("Nonce.wireEncode: "+e.getMessage()); - } - if(tmplen<=0){ - return -1; - } - totalLength+=tmplen; + // 编码 TLV-LENGTH + tmplen += encoder.prependVarNumber(new VlInt(totalLength)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; - return totalLength; + // 编码 TLV-TYPE + tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvNonce)); + if (tmplen <= 0) { + return -1; + } + totalLength += tmplen; + + return totalLength; + } catch (EncoderException e) { + throw new ComponentException("Nonce.wireEncode: " + e.getMessage()); + } } /** * 从 TLV Block 中解码出一个 Nonce + * * @param block * @return */ @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查 Type 是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNonce))){ - return false; - } - - // 读取 value - long value= 0; try { - value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); - } catch (TLVException | VlIntException e) { - throw new ComponentException("Nonce.wireDecode: "+e.getMessage()); - } - if(value<0){ - return false; - } - this.setNonce(value); + // 检查 Type 是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvNonce))) { + return false; + } - return true; + // 读取 value + long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + if (value < 0) { + return false; + } + this.setNonce(value); + + return true; + } catch (TLVException | VlIntException e) { + throw new ComponentException("Nonce.wireDecode: " + e.getMessage()); + } } } diff --git a/src/main/java/component/TTL.java b/src/main/java/component/TTL.java index 59b9429..7e55215 100644 --- a/src/main/java/component/TTL.java +++ b/src/main/java/component/TTL.java @@ -15,11 +15,11 @@ public class TTL implements IEncodingAble { * todo: 后台使用的uint64,故这里用long表示,则理论上可能越界 * // @Description: * 1. 格式如下: - * TTL = TTL-TYPE TLV-LENGTH NonNegativeInteger - */ + * TTL = TTL-TYPE TLV-LENGTH NonNegativeInteger + */ private long ttl; - public TTL(){ + public TTL() { } public TTL(Block block) throws ComponentException { @@ -28,6 +28,7 @@ public class TTL implements IEncodingAble { /** * 根据 TLV Block 初始化一个 TTL + * * @param block * @return */ @@ -37,86 +38,80 @@ public class TTL implements IEncodingAble { /** * 获取 TTL 值 + * * @return */ - public long ttl(){ + public long ttl() { return this.ttl; } /** * 设置 TTL 值 + * * @param ttl */ - public void setTtl(long ttl){ - this.ttl=ttl; + public void setTtl(long ttl) { + this.ttl = ttl; } /** * 将 TTL 线速编码为一个 TLV + * * @param encoder * @return */ @Override public int wireEncode(Encoder encoder) throws ComponentException { - int totalLength=0; - // 编码 TLV-VALUE - int tmpLen= 0; try { - tmpLen = encoder.prependNonNegativeInteger(this.ttl); - } catch (EncoderException e) { - throw new ComponentException("TTL.wireEncode: "+e.getMessage()); - } - if(tmpLen<=0){ - return -1; - } - totalLength+=tmpLen; + int totalLength = 0; + // 编码 TLV-VALUE + int tmpLen = encoder.prependNonNegativeInteger(this.ttl); + if (tmpLen <= 0) { + return -1; + } + totalLength += tmpLen; - // 编码 TLV-LENGTH - try { - tmpLen=encoder.prependVarNumber(new VlInt(totalLength)); - } catch (EncoderException e) { - throw new ComponentException("TTL.wireEncode: "+e.getMessage()); - } - if(tmpLen<=0){ - return -1; - } - totalLength+=tmpLen; + // 编码 TLV-LENGTH + tmpLen = encoder.prependVarNumber(new VlInt(totalLength)); + if (tmpLen <= 0) { + return -1; + } + totalLength += tmpLen; - // 编码 TLV-TYPE - try { - tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvTTL)); - } catch (EncoderException e) { - throw new ComponentException("TTL.wireEncode: "+e.getMessage()); - } - if(tmpLen<=0){ - return -1; - } - totalLength+=tmpLen; + // 编码 TLV-TYPE + tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvTTL)); + if (tmpLen <= 0) { + return -1; + } + totalLength += tmpLen; - return totalLength; + return totalLength; + } catch (EncoderException e) { + throw new ComponentException("TTL.wireEncode: " + e.getMessage()); + } } /** * 从 TLV Block 中解码出一个 TTL + * * @param block * @return */ @Override public boolean wireDecode(Block block) throws ComponentException { - // 检查Type是否正确 - if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){ - return false; - } - long value= 0; try { - value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); + // 检查Type是否正确 + if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvTTL))) { + return false; + } + long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int()); + if (value < 0) { + return false; + } + this.ttl = value; + return true; } catch (TLVException | VlIntException e) { - throw new ComponentException("TTL.wireDecode: "+e.getMessage()); + throw new ComponentException("TTL.wireDecode: " + e.getMessage()); } - if(value<0){ - return false; - } - this.ttl=value; - return true; } } diff --git a/src/test/java/component/CanBePrefixTest.java b/src/test/java/component/CanBePrefixTest.java index fd54c1d..3727d24 100644 --- a/src/test/java/component/CanBePrefixTest.java +++ b/src/test/java/component/CanBePrefixTest.java @@ -6,6 +6,8 @@ import org.junit.Test; import packet.Interest; import packet.PacketException; +import java.util.Arrays; + import static org.junit.Assert.*; /* @@ -22,11 +24,11 @@ public class CanBePrefixTest { @Test public void TestCanBePrefix_GetCanBePrefix() { CanBePrefix can = new CanBePrefix(); -// System.out.println(can.getCanBePrefix()); + System.out.println(can.getCanBePrefix()); assertFalse(can.getCanBePrefix()); can.setCanBePrefix(true); -// System.out.println(can.getCanBePrefix()); -// System.out.println(can.isInitial()); + System.out.println(can.getCanBePrefix()); + System.out.println(can.isInitial()); assertEquals(can.getCanBePrefix(),can.isInitial()); } @@ -41,9 +43,9 @@ public class CanBePrefixTest { encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0)); can.wireEncode(encoder); byte[] buf = encoder.getBuffer(); - System.out.println(buf); + System.out.println(Arrays.toString(buf)); Block block = Block.createBlockByBuffer(buf, false); - System.out.println(block); + System.out.println(" block: "+block.getType()+","+block.getLength()+","+Arrays.toString(block.getValue())); } @Test @@ -57,9 +59,9 @@ public class CanBePrefixTest { encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0)); can.wireEncode(encoder); byte[] buf = encoder.getBuffer(); - System.out.println("interest buf:"+buf); + System.out.println(Arrays.toString(buf)); Block block = Block.createBlockByBuffer(buf, false); - System.out.println("interest block:"+block); + System.out.println(" block: "+block.getType()+","+block.getLength()+","+Arrays.toString(block.getValue())); InterestLifeTime interest1 = new InterestLifeTime(); interest1.wireDecode(block); diff --git a/src/test/java/component/CongestionMarkTest.java b/src/test/java/component/CongestionMarkTest.java index 3573f27..5ec7c01 100644 --- a/src/test/java/component/CongestionMarkTest.java +++ b/src/test/java/component/CongestionMarkTest.java @@ -23,7 +23,7 @@ public class CongestionMarkTest { public void TestCongestionMark_GetCongestionLevel() { CongestionMark congestionMark = new CongestionMark(); System.out.println("interest:"+congestionMark.getCongestionLevel()); - congestionMark.setCongestionLevel(1248355635356896986L); + congestionMark.setCongestionLevel(Long.MAX_VALUE); System.out.println("interest:"+congestionMark.getCongestionLevel()); System.out.println("interest:"+congestionMark.isInitial()); } @@ -32,7 +32,7 @@ public class CongestionMarkTest { public void TestCongestionMark_WireEncode() throws EncoderException, ComponentException, BlockException { CongestionMark congestionMark = new CongestionMark(); System.out.println("interest:"+congestionMark.getCongestionLevel()); - congestionMark.setCongestionLevel(1248355635356896986L); + congestionMark.setCongestionLevel(Long.MAX_VALUE); System.out.println("interest:"+congestionMark.getCongestionLevel()); System.out.println("interest:"+congestionMark.isInitial()); @@ -49,7 +49,7 @@ public class CongestionMarkTest { public void TestCongestionMark_WireDecode() throws EncoderException, ComponentException, BlockException, PacketException { CongestionMark congestionMark = new CongestionMark(); System.out.println("interest:"+congestionMark.getCongestionLevel()); - congestionMark.setCongestionLevel(1248355635356896986L); + congestionMark.setCongestionLevel(Long.MAX_VALUE); System.out.println("interest:"+congestionMark.getCongestionLevel()); System.out.println("interest:"+congestionMark.isInitial()); diff --git a/src/test/java/component/FreshnessPeriodTest.java b/src/test/java/component/FreshnessPeriodTest.java index 9adabb3..8c3f20f 100644 --- a/src/test/java/component/FreshnessPeriodTest.java +++ b/src/test/java/component/FreshnessPeriodTest.java @@ -21,7 +21,7 @@ public class FreshnessPeriodTest { public void getFreshnessPeriod() { FreshnessPeriod fresh = new FreshnessPeriod(); System.out.println("interest:"+fresh.getFreshnessPeriod()); - fresh.setFreshnessPeriod(1234848876468746468L); + fresh.setFreshnessPeriod(Long.MAX_VALUE); System.out.println("interest:"+fresh.getFreshnessPeriod()); System.out.println("interest:"+fresh.isInitial()); } @@ -30,7 +30,7 @@ public class FreshnessPeriodTest { public void TestFreshnessPeriod_WireEncode() throws EncoderException, ComponentException, BlockException { FreshnessPeriod fresh = new FreshnessPeriod(); System.out.println("interest:"+fresh.getFreshnessPeriod()); - fresh.setFreshnessPeriod(1234848876468746468L); + fresh.setFreshnessPeriod(Long.MAX_VALUE); System.out.println("interest:"+fresh.getFreshnessPeriod()); System.out.println("interest:"+fresh.isInitial()); @@ -47,7 +47,7 @@ public class FreshnessPeriodTest { public void TestFreshnessPeriod_WireDecode() throws EncoderException, ComponentException, BlockException { FreshnessPeriod fresh = new FreshnessPeriod(); System.out.println("interest:"+fresh.getFreshnessPeriod()); - fresh.setFreshnessPeriod(1234848876468746468L); + fresh.setFreshnessPeriod(Long.MAX_VALUE); System.out.println("interest:"+fresh.getFreshnessPeriod()); System.out.println("interest:"+fresh.isInitial()); diff --git a/src/test/java/component/HopLimitTest.java b/src/test/java/component/HopLimitTest.java index 5e8f277..dde23ae 100644 --- a/src/test/java/component/HopLimitTest.java +++ b/src/test/java/component/HopLimitTest.java @@ -21,8 +21,8 @@ public class HopLimitTest { public void TestHopLimit_GetHopLimit() { HopLimit hop = new HopLimit(); System.out.println("interest;"+hop.getHopLimit()); - hop.setHopLimit(1234848876468746468L); - assertEquals(hop.getHopLimit(),1234848876468746468L); + hop.setHopLimit(Long.MAX_VALUE); + assertEquals(hop.getHopLimit(),Long.MAX_VALUE); assertTrue(hop.isInitial()); } @@ -30,8 +30,8 @@ public class HopLimitTest { public void TestHopLimit_WireEncode() throws ComponentException, EncoderException, BlockException { HopLimit hop = new HopLimit(); System.out.println("interest;"+hop.getHopLimit()); - hop.setHopLimit(1234848876468746468L); - assertEquals(hop.getHopLimit(),1234848876468746468L); + hop.setHopLimit(Long.MAX_VALUE); + assertEquals(hop.getHopLimit(),Long.MAX_VALUE); assertTrue(hop.isInitial()); Encoder encoder = new Encoder(); @@ -47,8 +47,8 @@ public class HopLimitTest { public void TestHopLimit_WireDecode() throws EncoderException, ComponentException, BlockException { HopLimit hop = new HopLimit(); System.out.println("interest;"+hop.getHopLimit()); - hop.setHopLimit(1234848876468746468L); - assertEquals(hop.getHopLimit(),1234848876468746468L); + hop.setHopLimit(Long.MAX_VALUE); + assertEquals(hop.getHopLimit(),Long.MAX_VALUE); assertTrue(hop.isInitial()); Encoder encoder = new Encoder(); diff --git a/src/test/java/component/MustBeRefreshTest.java b/src/test/java/component/MustBeRefreshTest.java index 4b5d05e..6d7ed6f 100644 --- a/src/test/java/component/MustBeRefreshTest.java +++ b/src/test/java/component/MustBeRefreshTest.java @@ -65,17 +65,17 @@ public class MustBeRefreshTest { must.setMustBeRefresh(true); must.wireEncode(encoder); byte[] buf = encoder.getBuffer(); - System.out.println("buf"+buf); + System.out.println("buf"+Arrays.toString(buf)); must.setMustBeRefresh(false); must.wireEncode(encoder); buf = encoder.getBuffer(); - System.out.println("buf"+buf); + System.out.println("buf"+Arrays.toString(buf)); Block block = Block.createBlockByBuffer(buf, false); MustBeRefresh must1 = new MustBeRefresh(); must1.wireDecode(block); - System.out.println("must1:"+must1.getMustBeRefresh()+must1.isInitial()); + System.out.println("must1:"+must1.getMustBeRefresh()+" "+must1.isInitial()); } } diff --git a/src/test/java/component/NonceTest.java b/src/test/java/component/NonceTest.java index 935b0f1..9f1e2db 100644 --- a/src/test/java/component/NonceTest.java +++ b/src/test/java/component/NonceTest.java @@ -26,18 +26,19 @@ public class NonceTest { // nonce.setNonce(18446744073709551615L); nonce.setNonce(0); System.out.println("nonce:"+nonce.getNonce()); + System.out.println(nonce.isInitial()); } @Test public void TestNonce_RefreshNonce() { Nonce nonce = new Nonce(); - nonce.setNonce(1844674407370955161L); + nonce.setNonce(Long.MAX_VALUE); nonce.setNonce(0); System.out.println("nonce:"+nonce.getNonce()); nonce.refreshNonce();; for (int i = 0; i < 5; i++) { nonce.refreshNonce();; - System.out.println("nonce"+nonce.getNonce()); + System.out.println("nonce: "+nonce.getNonce()); } } diff --git a/src/test/java/component/TTLTest.java b/src/test/java/component/TTLTest.java index ea09803..fbfc776 100644 --- a/src/test/java/component/TTLTest.java +++ b/src/test/java/component/TTLTest.java @@ -24,13 +24,13 @@ public class TTLTest { Encoder encoder = new Encoder(); encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0)); TTL ttl = new TTL(); - ttl.setTtl(15346343413545L); + ttl.setTtl(Long.MAX_VALUE); ttl.wireEncode(encoder); byte[] buf = encoder.getBuffer(); Block block = Block.createBlockByBuffer(buf, false); TTL newttl = new TTL(); newttl.buildTTLByBlock(block); - assertEquals(newttl.ttl(),15346343413545L); + assertEquals(newttl.ttl(),Long.MAX_VALUE); } @Test @@ -38,7 +38,7 @@ public class TTLTest { Encoder encoder = new Encoder(); encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0)); TTL ttl = new TTL(); - ttl.setTtl(15346343413545L); + ttl.setTtl(Long.MAX_VALUE); ttl.wireEncode(encoder); byte[] buf = encoder.getBuffer(); Block block = Block.createBlockByBuffer(buf, false); @@ -51,13 +51,13 @@ public class TTLTest { Encoder encoder = new Encoder(); encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0)); TTL ttl = new TTL(); - ttl.setTtl(15346343413545L); + ttl.setTtl(Long.MAX_VALUE); ttl.wireEncode(encoder); byte[] buf = encoder.getBuffer(); System.out.println("TTL buf:"+ Arrays.toString(buf)); Block block = Block.createBlockByBuffer(buf, false); TTL newttl1 = new TTL(); newttl1.wireDecode(block); - assertEquals(newttl1.ttl(),15346343413545L); + assertEquals(newttl1.ttl(),Long.MAX_VALUE); } }