mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-10-04 19:42:02 +08:00
135 lines
3.6 KiB
Go
135 lines
3.6 KiB
Go
// Package lsa
|
|
// @Author: Wang Feng
|
|
// @Description:
|
|
// @Version: 0.1.0
|
|
// @Date: 2022/4/6 20:57
|
|
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
//
|
|
|
|
package lsa
|
|
|
|
import (
|
|
"fmt"
|
|
"minlib/component"
|
|
"minlib/encoding"
|
|
"testing"
|
|
)
|
|
|
|
//
|
|
// TestNameLsaNamePrefixList_Insert
|
|
// @Description: 测试构造:插入;
|
|
// @param t
|
|
//
|
|
func TestNameLsaNamePrefixList_Insert(t *testing.T) {
|
|
// 构造一个info
|
|
base := new(NameLsaNamePrefixInfo)
|
|
base.Sources = append(base.Sources, "/wechat")
|
|
base.Sources = append(base.Sources, "/qq")
|
|
base.NamePrefix,_ = component.CreateIdentifierByString("/min/pku")
|
|
|
|
// 插入该info
|
|
prefixList := new(NameLsaNamePrefixList)
|
|
prefixList.InsertNamePrefixInfo(base)
|
|
|
|
// 插入另一个Info
|
|
ident, _ := component.CreateIdentifierByString("/min/thu")
|
|
prefixList.Insert(ident,"/browser1")
|
|
prefixList.Insert(ident,"/browser2")
|
|
|
|
fmt.Println(prefixList.ToString())
|
|
|
|
// 删除source
|
|
fmt.Println(prefixList.Remove(ident,"/browser"))
|
|
fmt.Println(prefixList.ToString())
|
|
fmt.Println(prefixList.Remove(ident,"/browser2"))
|
|
fmt.Println(prefixList.ToString())
|
|
|
|
// 删除prefix
|
|
fmt.Println(prefixList.RemoveByNamePrefix(ident))
|
|
fmt.Println(prefixList.ToString())
|
|
|
|
// 两次删除source
|
|
fmt.Println("------------两次删除-------------")
|
|
prefixList.Insert(ident,"/browser1")
|
|
prefixList.Insert(ident,"/browser2")
|
|
fmt.Println(prefixList.ToString())
|
|
fmt.Println(prefixList.Remove(ident,"/browser2"))
|
|
fmt.Println(prefixList.ToString())
|
|
fmt.Println(prefixList.Remove(ident,"/browser1"))
|
|
fmt.Println(prefixList.ToString())
|
|
}
|
|
|
|
//
|
|
// TestNameLsaNamePrefixList_WireEncode
|
|
// @Description: 测试编码
|
|
// @param t
|
|
//
|
|
func TestNameLsaNamePrefixList_WireEncode(t *testing.T) {
|
|
// 构造结构体
|
|
// 构造一个info
|
|
base := new(NameLsaNamePrefixInfo)
|
|
base.Sources = append(base.Sources, "/wechat")
|
|
base.Sources = append(base.Sources, "/qq")
|
|
base.NamePrefix,_ = component.CreateIdentifierByString("/min/pku")
|
|
|
|
// 插入该info
|
|
prefixList := new(NameLsaNamePrefixList)
|
|
prefixList.InsertNamePrefixInfo(base)
|
|
|
|
// 插入另一个Info
|
|
ident, _ := component.CreateIdentifierByString("/min/thu")
|
|
prefixList.Insert(ident,"/browser1")
|
|
prefixList.Insert(ident,"/browser2")
|
|
|
|
// 构造编码器
|
|
encoder := encoding.Encoder{}
|
|
err := encoder.EncoderReset(encoding.MaxPacketSize, 0)
|
|
if err != nil {
|
|
fmt.Println("encoder reset失败 ", err.Error())
|
|
return
|
|
}
|
|
|
|
// 编码并打印
|
|
cnt, err := prefixList.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()
|
|
}
|
|
|
|
//
|
|
// TestNameLsaNamePrefixList_WireDecode
|
|
// @Description: 测试解码。注意,source并未被编码进TLV数组
|
|
// @param t
|
|
//
|
|
func TestNameLsaNamePrefixList_WireDecode(t *testing.T) {
|
|
// 由数组构建block
|
|
var buf = [48]byte{241, 1, 173, 44, 241, 1, 172, 18, 101, 16, 100, 2, 0, 47,
|
|
100, 4, 0, 109, 105, 110, 100, 4, 0, 112, 107, 117, 241, 1, 172, 18, 101,
|
|
16, 100, 2, 0, 47, 100, 4, 0, 109, 105, 110, 100, 4, 0, 116, 104, 117}
|
|
block, err := encoding.CreateBlockByBuffer(buf[:], true)
|
|
if err != nil {
|
|
fmt.Println("创建block失败 ", err.Error())
|
|
return
|
|
}
|
|
|
|
// 由Block构建结构体,并打印变量
|
|
base := new(NameLsaNamePrefixList)
|
|
err = base.WireDecode(block)
|
|
if err != nil {
|
|
fmt.Println("线性解码失败 ", err.Error())
|
|
return
|
|
}
|
|
fmt.Println(base.ToString())
|
|
}
|