Files
mlsr/lsa/NameLsa_test.go
T

197 lines
5.4 KiB
Go

// Package lsa
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/4/7 10:31
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package lsa
import (
"encoding/json"
"fmt"
"log"
"minlib/component"
"minlib/encoding"
"testing"
)
//
// TestNameLsa_GetType
// @Description: 测试获取type
// @param t
//
func TestNameLsa_GetType(t *testing.T) {
nameLsa := new(NameLsa)
fmt.Println(nameLsa.GetType())
if nameLsa.GetType()!=LsaNAMEType{
log.Fatal("get type error")
}
}
//
// TestNameLsa_AddNamePrefixInfo
// @Description: 测试插入
// @param t
//
func TestNameLsa_AddNamePrefixInfo(t *testing.T) {
// 构造info
namePrefixInfo := new(NameLsaNamePrefixInfo)
namePrefixInfo.NamePrefix, _ =component.CreateIdentifierByString("/min/pku")
fmt.Println(namePrefixInfo.Sources)
namePrefixInfo.InsertSource("/wechat")
namePrefixInfo.InsertSource("/qq")
fmt.Println(namePrefixInfo.Sources)
// 构造nameLSA
fmt.Println("------------------------------------")
nameLsa := new(NameLsa)
nameLsa.LsaBase = LsaBase{
SelfEncodingBase: encoding.SelfEncodingBase{},
LsaExpirationTime: LsaExpirationTime{},
LsaSequenceNumber: LsaSequenceNumber{},
LsaOriginRouterIdentifier: nil,
}
nameLsa.SetLsaSequenceNumber(111)
nameLsa.SetLsaExpirationTime(2000)
routerIdent,_ :=component.CreateIdentifierByString("/pku/router")
nameLsa.SetOriginRouter(routerIdent)
fmt.Println("namelsa string: ",nameLsa.ToString())
nameLsa.InsertNamePrefixInfo(namePrefixInfo)
fmt.Println("namelsa string: ",nameLsa.ToString())
// 重复插入
nameLsa.InsertNamePrefixInfo(namePrefixInfo)
fmt.Println("namelsa string: ",nameLsa.ToString())
// 再次插入,但插入内容不一样
namePrefixInfo.InsertSource("/summer")
nameLsa.InsertNamePrefixInfo(namePrefixInfo)
fmt.Println("namelsa string: ",nameLsa.ToString())
// 插入新的内容前缀
ident,_ := component.CreateIdentifierByString("/pku/routerfucker")
nameLsa.Insert(ident,"/bind")
fmt.Println("namelsa string: ",nameLsa.ToString())
}
type TestJsonStruct struct {
Id int `json:"id"`
Name string `json:"name"`
}
//
// TestNameLsa_JsonConv
// @Description: NameLsa转json
// @param t
//
func TestNameLsa_JsonConv(t *testing.T) {
// 测试自建结构体转json
ss:=new(TestJsonStruct)
ss.Id = 123
ss.Name="name"
ssBytes,_ := json.Marshal(ss)
fmt.Println(string(ssBytes))
// 构造一个namelsa
base := new(NameLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
ident,_ := component.CreateIdentifierByString("/pku/routerfucker")
base.Insert(ident,"/bind")
base.Insert(ident,"/wechat")
ident,_ = component.CreateIdentifierByString("/pku/router")
base.Insert(ident,"/qq")
fmt.Println("ToString: "+base.ToString())
// 转byte[]类型的json
jsonBytes,err := json.Marshal(&base)
if err!=nil {
log.Fatal("nameLSA json 转换失败")
}
fmt.Println("直接将NameLSA为json: "+string(jsonBytes))
jsonBytes,err = base.GetJsonString()
fmt.Println("通过中间结构体将NameLSA转为json: "+string(jsonBytes))
// 从Json转回来
newNameLsa := new(NameLsa)
err = newNameLsa.LoadFromJsonString(jsonBytes)
if err!=nil {
log.Fatal("nameLSA json 转换失败")
}
fmt.Println("通过中间结构体将json转为NameLSA: "+newNameLsa.ToString())
}
//
// TestNameLsa_WireEncode
// @Description: 测试编码
// @param t
//
func TestNameLsa_WireEncode(t *testing.T) {
// 构造结构体
base := new(NameLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
ident,_ := component.CreateIdentifierByString("/pku/routerfucker")
base.Insert(ident,"/bind")
base.Insert(ident,"/wechat")
ident,_ = component.CreateIdentifierByString("/pku/router")
base.Insert(ident,"/qq")
fmt.Println(base.ToString())
// 构造编码器
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()
}
//
// TestNameLsa_WireDecode
// @Description: 测试解码
// @param t
//
func TestNameLsa_WireDecode(t *testing.T) {
// 由数组构建block
var buf = [98]byte{241, 1, 146, 94, 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, 173, 56, 241, 1, 172, 27, 101, 25, 100, 2, 0, 47, 100, 4, 0,
112, 107, 117, 100, 13, 0, 114, 111, 117, 116, 101, 114, 102, 117, 99, 107, 101,
114, 241, 1, 172, 21, 101, 19, 100, 2, 0, 47, 100, 4, 0, 112, 107, 117, 100, 7,
0, 114, 111, 117, 116, 101, 114}
block, err := encoding.CreateBlockByBuffer(buf[:], true)
if err != nil {
fmt.Println("创建block失败 ", err.Error())
return
}
// 由Block构建结构体,并打印变量
base := new(NameLsa)
err = base.WireDecode(block)
if err != nil {
fmt.Println("线性解码失败 ", err.Error())
return
}
fmt.Println(base.ToString())
}