diff --git a/cmd/ppf/main.go b/cmd/ppf/main.go index 776b5a8..244e85c 100644 --- a/cmd/ppf/main.go +++ b/cmd/ppf/main.go @@ -1,9 +1,9 @@ package main import ( - "gitea.mmo.to/ProtocolPacketForger/ppf" + "gitea.mmo.to/ProtocolPacketForger/ppf/internal/app" ) func main() { - ppf.Appmain() + app.Appmain() } diff --git a/controller.go b/controller.go deleted file mode 100644 index 218ff97..0000000 --- a/controller.go +++ /dev/null @@ -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) { - -} diff --git a/app.go b/internal/app/app.go similarity index 94% rename from app.go rename to internal/app/app.go index 35e05c0..e0a6e63 100644 --- a/app.go +++ b/internal/app/app.go @@ -1,4 +1,4 @@ -package ppf +package app import ( "fyne.io/fyne/v2" @@ -7,6 +7,8 @@ import ( "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" + + "gitea.mmo.to/ProtocolPacketForger/ppf/internal/ui" ) var fyneApp fyne.App @@ -35,8 +37,8 @@ type ppfApp struct { Settings map[string]string ProtocolForging bool // Once initialized instances of the editor views - EditorPacket *PacketEditor - EditorProtocol *ProtocolEditor + EditorPacket *ui.PacketEditor + EditorProtocol *ui.ProtocolEditor } var ppf ppfApp @@ -85,7 +87,7 @@ func CreateToolbar() fyne.CanvasObject { } func CreateWorkarea() *fyne.Container { - return GetProtocolEditor().Representation + return ui.GetProtocolEditor().Representation } func CreateMetadataProtocol() *fyne.Container { diff --git a/fieldadd.go b/internal/ui/fieldadd.go similarity index 95% rename from fieldadd.go rename to internal/ui/fieldadd.go index f515cff..b24bb80 100644 --- a/fieldadd.go +++ b/internal/ui/fieldadd.go @@ -1,4 +1,4 @@ -package ppf +package ui import ( "fyne.io/fyne/v2/widget" diff --git a/fieldcreator.go b/internal/ui/fieldcreator.go similarity index 99% rename from fieldcreator.go rename to internal/ui/fieldcreator.go index 7cc4554..b8add78 100644 --- a/fieldcreator.go +++ b/internal/ui/fieldcreator.go @@ -1,4 +1,4 @@ -package ppf +package ui import ( "fyne.io/fyne/v2" diff --git a/fieldview.go b/internal/ui/fieldview.go similarity index 93% rename from fieldview.go rename to internal/ui/fieldview.go index ca0f5b2..6fd0b98 100644 --- a/fieldview.go +++ b/internal/ui/fieldview.go @@ -1,4 +1,4 @@ -package ppf +package ui import ( "fyne.io/fyne/v2/widget" diff --git a/flowlayout.go b/internal/ui/flowlayout.go similarity index 99% rename from flowlayout.go rename to internal/ui/flowlayout.go index d1cef17..51fd75a 100644 --- a/flowlayout.go +++ b/internal/ui/flowlayout.go @@ -1,4 +1,4 @@ -package ppf +package ui import ( "fyne.io/fyne/v2" diff --git a/packeteditor.go b/internal/ui/packeteditor.go similarity index 88% rename from packeteditor.go rename to internal/ui/packeteditor.go index 433c20c..206adc3 100644 --- a/packeteditor.go +++ b/internal/ui/packeteditor.go @@ -1,4 +1,4 @@ -package ppf +package ui import "fyne.io/fyne/v2" diff --git a/protocoleditor.go b/internal/ui/protocoleditor.go similarity index 98% rename from protocoleditor.go rename to internal/ui/protocoleditor.go index edc3bab..5add203 100644 --- a/protocoleditor.go +++ b/internal/ui/protocoleditor.go @@ -1,4 +1,4 @@ -package ppf +package ui import ( "fyne.io/fyne/v2" diff --git a/packet/fieldvalue.go b/packet/fieldvalue.go index e69de29..9c40969 100644 --- a/packet/fieldvalue.go +++ b/packet/fieldvalue.go @@ -0,0 +1 @@ +package packet diff --git a/packet/packet.go b/packet/packet.go index e69de29..9c40969 100644 --- a/packet/packet.go +++ b/packet/packet.go @@ -0,0 +1 @@ +package packet diff --git a/packetctl/packetctl.go b/packetctl/packetctl.go index e69de29..53325c8 100644 --- a/packetctl/packetctl.go +++ b/packetctl/packetctl.go @@ -0,0 +1 @@ +package packetctl diff --git a/protocol/field.go b/protocol/field.go index 75e1714..70efb1d 100644 --- a/protocol/field.go +++ b/protocol/field.go @@ -1,5 +1,7 @@ package protocol +import "encoding/json" + type Field struct { Name string // Name of the Field Desc string // Lengthy description @@ -9,3 +11,11 @@ type Field struct { Optional bool // Is this field required? 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) +} diff --git a/protocolctl/protocolctl.go b/protocolctl/protocolctl.go index 6135a84..ed966f3 100644 --- a/protocolctl/protocolctl.go +++ b/protocolctl/protocolctl.go @@ -59,36 +59,26 @@ func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) { prot.Structure = append(prot.Structure, *field) } -func UpdateFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { - for _, f := range prot.Structure { - if f.Name == field.Name { - 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 UpdateFieldByName(prot *protocol.ProtocolStructure, name string, field *protocol.Field) { + var fnd int = -1 + for i, f := range prot.Structure { + if f.Name == name { + fnd = i } } + if fnd != -1 { + prot.Structure[fnd] = *field + } } func UpdateFieldByElement(prot *protocol.ProtocolStructure, element int, field *protocol.Field) { - for i, f := range prot.Structure { - 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 - } - } + prot.Structure[element] = *field } -func RemoveFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { +func RemoveFieldByName(prot *protocol.ProtocolStructure, name string) { element := -1 for i, f := range prot.Structure { - if f.Name == field.Name { + if f.Name == name { element = i } } @@ -120,6 +110,14 @@ func LoadNew(path string) (*protocol.ProtocolStructure, error) { 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 { data, err := json.Marshal(*prot) if err != nil { diff --git a/protocolctl/protocolctl_test.go b/protocolctl/protocolctl_test.go new file mode 100644 index 0000000..4a14788 --- /dev/null +++ b/protocolctl/protocolctl_test.go @@ -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)) +}