More project structural changes and controller tests

This commit is contained in:
Marcel M. Otte 2023-01-03 13:34:32 +01:00
parent 54ea40d3f1
commit 66b4cac859
15 changed files with 190 additions and 86 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"gitea.mmo.to/ProtocolPacketForger/ppf" "gitea.mmo.to/ProtocolPacketForger/ppf/internal/app"
) )
func main() { func main() {
ppf.Appmain() app.Appmain()
} }

View File

@ -1,53 +0,0 @@
package ppf
func NewDataModel(name string) *DataModel {
return &DataModel{
Name: name,
}
}
func (dm *DataModel) AddProtocol(name string) {
}
func (dm *DataModel) RemoveProtocolByName(name string) {
}
func (dm *DataModel) RemoveProtocolElement(id uint) {
}
func (dm *DataModel) NewProtocol(name string) {
}
func (dm *DataModel) SaveNewProtocol(editorContent ProtocolEditor) {
}
func (dm *DataModel) GetPacketEditor() *PacketEditor {
return nil
}
func (dm *DataModel) SwitchToProtocolEditor() {
}
func (dm *DataModel) SyncPacketEditor(pkgedt *PacketEditor) {
}
func (dm *DataModel) GetPackageBytes() []byte {
return []byte{}
}
func (dm *DataModel) FromSaved(path string) {
}
func (dm *DataModel) SaveAsPcap(path string) {
}

View File

@ -1,4 +1,4 @@
package ppf package app
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
@ -7,6 +7,8 @@ import (
"fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"
"gitea.mmo.to/ProtocolPacketForger/ppf/internal/ui"
) )
var fyneApp fyne.App var fyneApp fyne.App
@ -35,8 +37,8 @@ type ppfApp struct {
Settings map[string]string Settings map[string]string
ProtocolForging bool ProtocolForging bool
// Once initialized instances of the editor views // Once initialized instances of the editor views
EditorPacket *PacketEditor EditorPacket *ui.PacketEditor
EditorProtocol *ProtocolEditor EditorProtocol *ui.ProtocolEditor
} }
var ppf ppfApp var ppf ppfApp
@ -85,7 +87,7 @@ func CreateToolbar() fyne.CanvasObject {
} }
func CreateWorkarea() *fyne.Container { func CreateWorkarea() *fyne.Container {
return GetProtocolEditor().Representation return ui.GetProtocolEditor().Representation
} }
func CreateMetadataProtocol() *fyne.Container { func CreateMetadataProtocol() *fyne.Container {

View File

@ -1,4 +1,4 @@
package ppf package ui
import ( import (
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"

View File

@ -1,4 +1,4 @@
package ppf package ui
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"

View File

@ -1,4 +1,4 @@
package ppf package ui
import ( import (
"fyne.io/fyne/v2/widget" "fyne.io/fyne/v2/widget"

View File

@ -1,4 +1,4 @@
package ppf package ui
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"

View File

@ -1,4 +1,4 @@
package ppf package ui
import "fyne.io/fyne/v2" import "fyne.io/fyne/v2"

View File

@ -1,4 +1,4 @@
package ppf package ui
import ( import (
"fyne.io/fyne/v2" "fyne.io/fyne/v2"

View File

@ -0,0 +1 @@
package packet

View File

@ -0,0 +1 @@
package packet

View File

@ -0,0 +1 @@
package packetctl

View File

@ -1,5 +1,7 @@
package protocol package protocol
import "encoding/json"
type Field struct { type Field struct {
Name string // Name of the Field Name string // Name of the Field
Desc string // Lengthy description Desc string // Lengthy description
@ -9,3 +11,11 @@ type Field struct {
Optional bool // Is this field required? Optional bool // Is this field required?
Payload bool // Is this field the payload or next protocol level? Payload bool // Is this field the payload or next protocol level?
} }
func (f *Field) ToJson() string {
b, err := json.Marshal(*f)
if err != nil {
return ""
}
return string(b)
}

View File

@ -59,36 +59,26 @@ func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) {
prot.Structure = append(prot.Structure, *field) prot.Structure = append(prot.Structure, *field)
} }
func UpdateFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { func UpdateFieldByName(prot *protocol.ProtocolStructure, name string, field *protocol.Field) {
for _, f := range prot.Structure { var fnd int = -1
if f.Name == field.Name { for i, f := range prot.Structure {
f.Desc = field.Desc if f.Name == name {
f.Optional = field.Optional fnd = i
f.Payload = field.Payload
f.Regex = field.Regex
f.Size = field.Size
f.SubFields = field.SubFields
} }
} }
if fnd != -1 {
prot.Structure[fnd] = *field
}
} }
func UpdateFieldByElement(prot *protocol.ProtocolStructure, element int, field *protocol.Field) { func UpdateFieldByElement(prot *protocol.ProtocolStructure, element int, field *protocol.Field) {
for i, f := range prot.Structure { prot.Structure[element] = *field
if i == element {
f.Desc = field.Desc
f.Optional = field.Optional
f.Payload = field.Payload
f.Regex = field.Regex
f.Size = field.Size
f.SubFields = field.SubFields
}
}
} }
func RemoveFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { func RemoveFieldByName(prot *protocol.ProtocolStructure, name string) {
element := -1 element := -1
for i, f := range prot.Structure { for i, f := range prot.Structure {
if f.Name == field.Name { if f.Name == name {
element = i element = i
} }
} }
@ -120,6 +110,14 @@ func LoadNew(path string) (*protocol.ProtocolStructure, error) {
return prot, err return prot, err
} }
func ToJson(prot *protocol.ProtocolStructure) string {
data, err := json.Marshal(*prot)
if err != nil {
return ""
}
return string(data)
}
func Save(prot *protocol.ProtocolStructure, path string) error { func Save(prot *protocol.ProtocolStructure, path string) error {
data, err := json.Marshal(*prot) data, err := json.Marshal(*prot)
if err != nil { if err != nil {

View File

@ -0,0 +1,144 @@
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,
nil,
nil,
)
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),
"",
uint(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))
}