Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ require (
golang.org/x/sync v0.6.0
)

require golang.org/x/net v0.20.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/net v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
131 changes: 131 additions & 0 deletions pkg/http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package http_test

import (
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"testing"

"github.com/absmach/mproxy/pkg/session/mocks"

mhttp "github.com/absmach/mproxy/pkg/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

const (
validUrl = "http://example.com"
validAddess = "localhost:8080"
valid = "valid"
invalid = "invalid"
)

var (
han *mocks.Handler
log *slog.Logger
)

func newProxy(address, url string) (mhttp.Proxy, error) {
han = new(mocks.Handler)
log = new(slog.Logger)
return mhttp.NewProxy(address, url, han, log)
}

func TestNewProxy(t *testing.T) {
cases := []struct {
desc string
address string
url string
err error
}{
{
desc: "create proxy with valid",
address: validAddess,
url: validUrl,
err: nil,
},
{
desc: "create proxy with invalid url",
address: validAddess,
url: "0000",
err: nil,
},
}
for _, c := range cases {
_, err := newProxy(c.address, c.url)
assert.Equal(t, c.err, err, fmt.Sprintf("%s: expected %s got %s\n", c.desc, c.err, err))

}
}

func TestHandler(t *testing.T) {
proxy, err := newProxy(validAddess, validUrl)
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
request := httptest.NewRequest(http.MethodGet, "http://example.com", nil)

cases := []struct {
desc string
auth func()
authConnectErr error
authPublishErr error
code int
}{
{
desc: "successful request with username and password and basic auth",
auth: func() {
request.SetBasicAuth("username", "password")
},
code: http.StatusOK,
},
{
desc: "successful request with token",
auth: func() {
request.Header.Set("Authorization", valid)
},
code: http.StatusOK,
},
{
desc: "request without authorization token",
auth: func() {
request.Header.Set("Authorization", "")
},
code: http.StatusBadGateway,
},
}
for _, tc := range cases {
tc.auth()
responseRecorder := httptest.NewRecorder()
sessionCall := han.On("AuthConnect", mock.Anything).Return(tc.authConnectErr)
sessionCall1 := han.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(tc.authPublishErr)
proxy.Handler(responseRecorder, request)
assert.Equal(t, tc.code, responseRecorder.Code, fmt.Sprintf("%s: expected %d got %d\n", tc.desc, tc.code, responseRecorder.Code))
sessionCall.Unset()
sessionCall1.Unset()

}
}

func TestListen(t *testing.T) {
proxy, err := newProxy(validAddess, validUrl)
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))

t.Run("Listen", func(t *testing.T) {
go func() {
err := proxy.Listen()
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
}()
})
}

func TestListenTLS(t *testing.T) {
proxy, err := newProxy(validAddess, validUrl)
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))

t.Run("ListenTLS", func(t *testing.T) {
go func() {
err := proxy.ListenTLS("cert", "key")
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
}()
})
}
79 changes: 79 additions & 0 deletions pkg/mqtt/mqtt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package mqtt_test

import (
"context"
"crypto/tls"
"fmt"
"testing"

"github.com/absmach/mproxy/pkg/mqtt"
"github.com/absmach/mproxy/pkg/session/mocks"
"github.com/stretchr/testify/assert"
)

func newProxy(address, target string) *mqtt.Proxy {
handler := new(mocks.Handler)
interceptor := new(mocks.Interceptor)
return mqtt.New(address, target, handler, interceptor, nil)
}

var tlsConfig = &tls.Config{}

func TestListen(t *testing.T) {
cases := []struct {
desc string
address string
target string
err error
}{
{
desc: "listen with valid address",
address: "localhost:8080",
target: "localhost:8080",
err: nil,
},
// {
// desc: "listen with invalid address",
// address: "0000",
// target: "localhost:8080",
// err: nil,
// },
}
for _, c := range cases {
proxy := newProxy(c.address, c.target)
go func() {
err := proxy.Listen(context.Background())
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
}()
}
}

func TestListenTLS(t *testing.T) {
cases := []struct {
desc string
address string
target string
err error
}{
{
desc: "listen with valid address",
address: "localhost:8080",
target: "localhost:8080",
err: nil,
},
// {
// desc: "listen with invalid address",
// address: "0000",
// target: "localhost:8080",
// err: nil,
// },
}
for _, c := range cases {

proxy := newProxy(c.address, c.target)
go func() {
err := proxy.ListenTLS(context.Background(), tlsConfig)
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
}()
}
}
39 changes: 39 additions & 0 deletions pkg/mqtt/websocket/websocket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package websocket_test

import (
"log/slog"
"net/http/httptest"
"testing"

"github.com/absmach/mproxy/pkg/mqtt/websocket"
"github.com/absmach/mproxy/pkg/session/mocks"
)

func newProxy(target, path, scheme string) *websocket.Proxy {
handler := new(mocks.Handler)
interceptor := new(mocks.Interceptor)
logger := new(slog.Logger)
return websocket.New(target, path, scheme, handler, interceptor, logger)
}

func TestHandler(t *testing.T) {
cases := []struct {
desc string
target string
path string
scheme string
}{
{
desc: "handler with valid target",
target: "localhost:8080",
path: "/",
scheme: "ws",
},
}
for _, c := range cases {
proxy := newProxy(c.target, c.path, c.scheme)
responseRecorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "http://example.com", nil)
proxy.Handler().ServeHTTP(responseRecorder, request)
}
}
2 changes: 2 additions & 0 deletions pkg/session/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package session
import "context"

// Handler is an interface for mProxy hooks

//go:generate mockery --name Handler --filename handler.go --quiet --note "Copyright (c) Abstract Machines"
type Handler interface {
// Authorization on client `CONNECT`
// Each of the params are passed by reference, so that it can be changed
Expand Down
2 changes: 2 additions & 0 deletions pkg/session/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

// Interceptor is an interface for mProxy intercept hook.

//go:generate mockery --name Interceptor --filename interceptor.go --quiet --note "Copyright (c) Abstract Machines"
type Interceptor interface {
// Intercept is called on every packet flowing through the Proxy.
// Packets can be modified before being sent to the broker or the client.
Expand Down
Loading