ppforge/protocol/protocol_test.go

149 lines
2.9 KiB
Go

package protocol
import (
"fmt"
"testing"
)
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.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() []Field {
slc := make([]Field, 0)
for i := 0; i < 10; i++ {
field := NewField(
fmt.Sprintf("testfield%d", i),
fmt.Sprintf("Description %d", i),
"",
int(i*8),
nil,
false,
false,
)
slc = append(slc, *field)
}
return slc
}
func GenerateStructure() *Protocol {
p := NewProtocolStructure()
slc := GenerateFields()
for i, e := range slc {
p.AppendField(e)
fmt.Printf("%d %s\n", i, e.ToJson())
}
for i, e := range p.Structure {
fmt.Printf("%d %s\n", i, e.ToJson())
}
fmt.Println("Generated Structure.")
return p
}
func TestFieldAppend(t *testing.T) {
f := NewField(
"testfield",
"Description",
"",
8,
nil,
false,
false,
)
p := NewProtocolStructure()
p.AppendField(*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,
)
p.UpdateFieldByElement(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()
p.RemoveFieldByElement(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 element failed!")
t.Fail()
}
}
func TestToJson(t *testing.T) {
p := GenerateStructure()
fmt.Print(p.ToJson())
}