2023-09-17 12:49:41 +02:00
|
|
|
package protocol
|
2023-01-03 13:34:32 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 12:49:41 +02:00
|
|
|
func GenerateFields() []Field {
|
|
|
|
slc := make([]Field, 0)
|
2023-01-03 13:34:32 +01:00
|
|
|
for i := 0; i < 10; i++ {
|
2023-05-12 17:18:24 +02:00
|
|
|
field := NewField(
|
2023-01-03 13:34:32 +01:00
|
|
|
fmt.Sprintf("testfield%d", i),
|
|
|
|
fmt.Sprintf("Description %d", i),
|
|
|
|
"",
|
2023-01-04 13:38:16 +01:00
|
|
|
int(i*8),
|
2023-01-03 13:34:32 +01:00
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
false,
|
2023-05-12 17:18:24 +02:00
|
|
|
)
|
|
|
|
slc = append(slc, *field)
|
2023-01-03 13:34:32 +01:00
|
|
|
}
|
|
|
|
return slc
|
|
|
|
}
|
|
|
|
|
2023-09-17 12:49:41 +02:00
|
|
|
func GenerateStructure() *Protocol {
|
2023-01-03 13:34:32 +01:00
|
|
|
p := NewProtocolStructure()
|
|
|
|
slc := GenerateFields()
|
|
|
|
for i, e := range slc {
|
2023-09-17 12:49:41 +02:00
|
|
|
p.AppendField(e)
|
2023-05-12 17:18:24 +02:00
|
|
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
|
|
|
}
|
|
|
|
for i, e := range p.Structure {
|
2023-01-03 13:34:32 +01:00
|
|
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
|
|
|
}
|
2023-05-12 17:18:24 +02:00
|
|
|
fmt.Println("Generated Structure.")
|
2023-01-03 13:34:32 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldAppend(t *testing.T) {
|
|
|
|
f := NewField(
|
|
|
|
"testfield",
|
|
|
|
"Description",
|
|
|
|
"",
|
|
|
|
8,
|
|
|
|
nil,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
p := NewProtocolStructure()
|
2023-09-17 12:49:41 +02:00
|
|
|
p.AppendField(*f)
|
2023-01-03 13:34:32 +01:00
|
|
|
if p.Structure[0].Name != "testfield" {
|
|
|
|
t.Log("Append failed.")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:30:32 +02:00
|
|
|
/*
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
} //
|
|
|
|
*/
|
2023-01-03 13:34:32 +01:00
|
|
|
func TestUpdateFieldByElement(t *testing.T) {
|
|
|
|
p := GenerateStructure()
|
|
|
|
f := NewField(
|
|
|
|
"UpdatedField", "", "", 42, nil, false, true,
|
|
|
|
)
|
2023-09-17 12:49:41 +02:00
|
|
|
p.UpdateFieldByElement(5, f)
|
2023-01-03 13:34:32 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:30:32 +02:00
|
|
|
/*
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}//
|
|
|
|
*/
|
2023-01-03 13:34:32 +01:00
|
|
|
func TestRemoveFieldByElement(t *testing.T) {
|
|
|
|
p := GenerateStructure()
|
2023-09-17 12:49:41 +02:00
|
|
|
p.RemoveFieldByElement(3)
|
2023-01-03 13:34:32 +01:00
|
|
|
for i, e := range p.Structure {
|
|
|
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
|
|
|
}
|
|
|
|
if p.Structure[3].Name != "testfield4" {
|
2023-05-12 16:30:32 +02:00
|
|
|
t.Log("Remove by element failed!")
|
2023-01-03 13:34:32 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToJson(t *testing.T) {
|
|
|
|
p := GenerateStructure()
|
2023-10-30 22:51:13 +01:00
|
|
|
fmt.Print(p.ToJSON())
|
2023-01-03 13:34:32 +01:00
|
|
|
}
|