ppforge/protocolctl/protocolctl_test.go

143 lines
2.9 KiB
Go

package protocolctl
import (
"fmt"
"testing"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
)
func TestUpdateMetaData(t *testing.T) {
var (
name string = "test"
version string = "1.0.0"
osilayer uint = 3
tcpiplayer uint = 2
extensionTo string = ""
desc string = "description"
)
prot := NewProtocolStructure()
UpdateMetaData(
prot,
name,
version,
tcpiplayer,
osilayer,
extensionTo,
desc,
)
if prot.Metadata.Name != name ||
prot.Metadata.Description != desc ||
prot.Metadata.ExtensionTo != extensionTo ||
prot.Metadata.OSILayer != osilayer ||
prot.Metadata.TCPIPLayer != tcpiplayer ||
prot.Metadata.Version != version ||
prot.Metadata.LowerLayerIdentification != nil ||
prot.Metadata.RequiredJSFunctions != nil {
t.Log("Data hasn't been set correctly!")
t.Fail()
}
}
func GenerateFields() []protocol.Field {
slc := make([]protocol.Field, 0)
for i := 0; i < 10; i++ {
slc = append(slc, *NewField(
fmt.Sprintf("testfield%d", i),
fmt.Sprintf("Description %d", i),
"",
int(i*8),
nil,
false,
false,
))
}
return slc
}
func GenerateStructure() *protocol.ProtocolStructure {
p := NewProtocolStructure()
slc := GenerateFields()
for i, e := range slc {
AppendField(p, &e)
fmt.Printf("%d %s\n", i, e.ToJson())
}
return p
}
func TestFieldAppend(t *testing.T) {
f := NewField(
"testfield",
"Description",
"",
8,
nil,
false,
false,
)
p := NewProtocolStructure()
AppendField(p, f)
if p.Structure[0].Name != "testfield" {
t.Log("Append failed.")
t.Fail()
}
}
func TestUpdateFieldByName(t *testing.T) {
p := GenerateStructure()
f := NewField(
"UpdatedField", "", "", 42, nil, false, true,
)
UpdateFieldByName(p, "testfield3", f)
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 {
t.Log("Update field by Ref failed!")
t.Fail()
}
}
func TestUpdateFieldByElement(t *testing.T) {
p := GenerateStructure()
f := NewField(
"UpdatedField", "", "", 42, nil, false, true,
)
UpdateFieldByElement(p, 5, f)
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
if p.Structure[5].Desc != "" || p.Structure[5].Size != 42 {
t.Log("Update field by Ref failed!")
t.Fail()
}
}
func TestRemoveFieldByName(t *testing.T) {
p := GenerateStructure()
RemoveFieldByName(p, "testfield3")
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
if p.Structure[3].Name != "testfield4" {
t.Log("Remove by name failed!")
t.Fail()
}
}
func TestRemoveFieldByElement(t *testing.T) {
p := GenerateStructure()
RemoveFieldByElement(p, 3)
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
if p.Structure[3].Name != "testfield4" {
t.Log("Remove by name failed!")
t.Fail()
}
}
func TestToJson(t *testing.T) {
p := GenerateStructure()
fmt.Print(ToJson(p))
}