Skip to content

Commit

Permalink
Merge pull request #59 from telekom-mms/feature/socket-api-updates
Browse files Browse the repository at this point in the history
Feature/socket api updates
  • Loading branch information
hwipl authored Mar 14, 2024
2 parents ae9f423 + 084b177 commit f89221d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const (
// MaxPayloadLength is the maximum allowed length of a message payload.
MaxPayloadLength = 32768
MaxPayloadLength = 2097152
)

// Message types.
Expand All @@ -23,7 +23,7 @@ const (
// Header is a message header.
type Header struct {
Type uint16
Length uint16
Length uint32
}

// Message is an API message.
Expand All @@ -40,7 +40,7 @@ func NewMessage(t uint16, p []byte) *Message {
return &Message{
Header: Header{
Type: t,
Length: uint16(len(p)),
Length: uint32(len(p)),
},
Value: p,
}
Expand Down
3 changes: 3 additions & 0 deletions internal/vpncscript/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func runClient(socketFile string, configUpdate *daemon.VPNConfigUpdate) error {
return fmt.Errorf("VPNCScript could not convert config update to JSON: %w", err)
}
msg := api.NewMessage(api.TypeVPNConfigUpdate, b)
if msg == nil {
return fmt.Errorf("VPNCScript could not create message: invalid message")
}
err = api.WriteMessage(conn, msg)
if err != nil {
return fmt.Errorf("VPNCScript could not send message to Daemon: %w", err)
Expand Down
54 changes: 54 additions & 0 deletions internal/vpncscript/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package vpncscript

import (
"path/filepath"
"strings"
"testing"

"github.com/telekom-mms/oc-daemon/internal/api"
"github.com/telekom-mms/oc-daemon/internal/daemon"
"github.com/telekom-mms/oc-daemon/pkg/vpnconfig"
)

// TestRunClient tests runClient.
Expand Down Expand Up @@ -44,4 +46,56 @@ func TestRunClient(t *testing.T) {
t.Fatal(err)
}
server.Stop()

// helper for config update creation
getConfUpdate := func(length int) *daemon.VPNConfigUpdate {
exclude := "a.too.long.example.com"
conf := vpnconfig.New()
conf.Split.ExcludeDNS = []string{exclude}
confUpdate := daemon.NewVPNConfigUpdate()
confUpdate.Config = conf

// check length
b, err := confUpdate.JSON()
if err != nil {
t.Fatal(err)
}
n := length - len(b)

// increase length to maximum
exclude = strings.Repeat("a", n) + exclude
conf.Split.ExcludeDNS = []string{exclude}

return confUpdate
}

// test with maximum payload length
server = api.NewServer(config)
go func() {
for r := range server.Requests() {
r.Close()
}
}()
if err := server.Start(); err != nil {
t.Fatal(err)
}
if err := runClient(sockfile, getConfUpdate(api.MaxPayloadLength)); err != nil {
t.Fatal(err)
}
server.Stop()

// test with more than maximum payload length
server = api.NewServer(config)
go func() {
for r := range server.Requests() {
r.Close()
}
}()
if err := server.Start(); err != nil {
t.Fatal(err)
}
if err := runClient(sockfile, getConfUpdate(api.MaxPayloadLength+1)); err == nil {
t.Fatal("too long message should return error")
}
server.Stop()
}

0 comments on commit f89221d

Please sign in to comment.