Files
mlsr/lsa/AdjLsa_test.go
T
2022-04-27 17:24:41 +08:00

146 lines
4.1 KiB
Go

// Package lsa
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/4/22 11:01
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package lsa
import (
"fmt"
"minlib/component"
"minlib/encoding"
"testing"
)
//
// TestAdjLsa_New
// @Description: 构造
// @param t
//
func TestAdjLsa_New(t *testing.T) {
// 构造基本部分
base := new(AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
base.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
AdjLsaLogicFaceId: adjInfo.AdjLsaLogicFaceId,
lsaNeighborRouterIdentifier: iden2,
InterestTimedOutNo: 111,
Status: 0,
}
base.Insert(&adjInfo2)
// 打印该adjlsa
fmt.Println(base.ToString())
}
//
// TestAdjLsa_WireEncode
// @Description: 测试线速编码
// @param t
//
func TestAdjLsa_WireEncode(t *testing.T) {
// 构造LSA
base := new(AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
base.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
AdjLsaLogicFaceId: adjInfo.AdjLsaLogicFaceId,
lsaNeighborRouterIdentifier: iden2,
InterestTimedOutNo: 111,
Status: 0,
}
base.Insert(&adjInfo2)
// 构造编码器
encoder := encoding.Encoder{}
err := encoder.EncoderReset(encoding.MaxPacketSize, 0)
if err != nil {
fmt.Println("encoder reset失败 ", err.Error())
return
}
// 编码并打印
cnt, err := base.WireEncode(&encoder)
if err != nil {
fmt.Println("LsaBase 线速编码失败 ", err.Error())
return
}
res, err := encoder.GetBuffer()
if err != nil {
fmt.Println("获取编码结果失败 ", err.Error())
return
}
fmt.Println(cnt)
for i:=0;i < len(res);i++{
fmt.Print(res[i],", ")
}
fmt.Println()
}
//
// TestAdjLsa_WireDecode
// @Description: 测试线速解码
// @param t
//
func TestAdjLsa_WireDecode(t *testing.T) {
// 由数组构建block
var buf = [118]byte{241, 1, 145, 114, 241, 1, 154, 30, 101, 16, 100,
2, 0, 47, 100, 4, 0, 109, 105, 110, 100, 4, 0, 112, 107, 117,
241, 1, 165, 2, 4, 210, 241, 1, 164, 2, 7, 208, 241, 1, 170,
76, 241, 1, 169, 34, 101, 20, 100, 2, 0, 47, 100, 4, 0, 112,
107, 117, 100, 8, 0, 114, 111, 117, 116, 101, 114, 49, 241,
1, 168, 3, 117, 114, 105, 241, 1, 167, 1, 100, 241, 1, 169,
34, 101, 20, 100, 2, 0, 47, 100, 4, 0, 112, 107, 117, 100,
8, 0, 114, 111, 117, 116, 101, 114, 50,
241, 1, 168, 3, 117, 114, 105, 241, 1, 167, 1, 100}
block, err := encoding.CreateBlockByBuffer(buf[:], true)
if err != nil {
fmt.Println("创建block失败 ", err.Error())
return
}
// 由Block构建结构体,并打印变量
base := new(AdjLsa)
err = base.WireDecode(block)
if err != nil {
fmt.Println("线性解码失败 ", err.Error())
return
}
fmt.Println(base.ToString())
}