Skip to content

Commit 7347bdf

Browse files
committed
feat:Add tests to mproxy
Signed-off-by: felix.gateru <[email protected]>
1 parent 695f58d commit 7347bdf

File tree

12 files changed

+791
-3
lines changed

12 files changed

+791
-3
lines changed

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,11 @@ require (
1111
golang.org/x/sync v0.6.0
1212
)
1313

14-
require golang.org/x/net v0.20.0 // indirect
14+
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/stretchr/objx v0.5.0 // indirect
18+
github.com/stretchr/testify v1.8.4 // indirect
19+
golang.org/x/net v0.20.0 // indirect
20+
gopkg.in/yaml.v3 v3.0.1 // indirect
21+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
25
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
36
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
47
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
58
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
69
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
14+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
15+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
16+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
18+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
19+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
720
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
821
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
922
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
1023
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/http/http_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package http_test
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/absmach/mproxy/pkg/session/mocks"
11+
12+
mhttp "github.com/absmach/mproxy/pkg/http"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/mock"
15+
)
16+
17+
const (
18+
validUrl = "http://example.com"
19+
validAddess = "localhost:8080"
20+
valid = "valid"
21+
invalid = "invalid"
22+
)
23+
24+
var (
25+
han *mocks.Handler
26+
log *slog.Logger
27+
)
28+
29+
func newProxy(address, url string) (mhttp.Proxy, error) {
30+
han = new(mocks.Handler)
31+
log = new(slog.Logger)
32+
return mhttp.NewProxy(address, url, han, log)
33+
}
34+
35+
func TestNewProxy(t *testing.T) {
36+
cases := []struct {
37+
desc string
38+
address string
39+
url string
40+
err error
41+
}{
42+
{
43+
desc: "create proxy with valid",
44+
address: validAddess,
45+
url: validUrl,
46+
err: nil,
47+
},
48+
{
49+
desc: "create proxy with invalid url",
50+
address: validAddess,
51+
url: "0000",
52+
err: nil,
53+
},
54+
}
55+
for _, c := range cases {
56+
_, err := newProxy(c.address, c.url)
57+
assert.Equal(t, c.err, err, fmt.Sprintf("%s: expected %s got %s\n", c.desc, c.err, err))
58+
59+
}
60+
}
61+
62+
func TestHandler(t *testing.T) {
63+
proxy, err := newProxy(validAddess, validUrl)
64+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
65+
request := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
66+
67+
cases := []struct {
68+
desc string
69+
auth func()
70+
authConnectErr error
71+
authPublishErr error
72+
code int
73+
}{
74+
{
75+
desc: "successful request with username and password and basic auth",
76+
auth: func() {
77+
request.SetBasicAuth("username", "password")
78+
},
79+
code: http.StatusOK,
80+
},
81+
{
82+
desc: "successful request with token",
83+
auth: func() {
84+
request.Header.Set("Authorization", valid)
85+
},
86+
code: http.StatusOK,
87+
},
88+
{
89+
desc: "request without authorization token",
90+
auth: func() {
91+
request.Header.Set("Authorization", "")
92+
},
93+
code: http.StatusBadGateway,
94+
},
95+
}
96+
for _, tc := range cases {
97+
tc.auth()
98+
responseRecorder := httptest.NewRecorder()
99+
sessionCall := han.On("AuthConnect", mock.Anything).Return(tc.authConnectErr)
100+
sessionCall1 := han.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(tc.authPublishErr)
101+
proxy.Handler(responseRecorder, request)
102+
assert.Equal(t, tc.code, responseRecorder.Code, fmt.Sprintf("%s: expected %d got %d\n", tc.desc, tc.code, responseRecorder.Code))
103+
sessionCall.Unset()
104+
sessionCall1.Unset()
105+
106+
}
107+
}
108+
109+
func TestListen(t *testing.T) {
110+
proxy, err := newProxy(validAddess, validUrl)
111+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
112+
113+
t.Run("Listen", func(t *testing.T) {
114+
go func() {
115+
err := proxy.Listen()
116+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
117+
}()
118+
})
119+
}
120+
121+
func TestListenTLS(t *testing.T) {
122+
proxy, err := newProxy(validAddess, validUrl)
123+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
124+
125+
t.Run("ListenTLS", func(t *testing.T) {
126+
go func() {
127+
err := proxy.ListenTLS("cert", "key")
128+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
129+
}()
130+
})
131+
}

pkg/mqtt/mqtt_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package mqtt_test
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/absmach/mproxy/pkg/mqtt"
10+
"github.com/absmach/mproxy/pkg/session/mocks"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func newProxy(address, target string) *mqtt.Proxy {
15+
handler := new(mocks.Handler)
16+
interceptor := new(mocks.Interceptor)
17+
return mqtt.New(address, target, handler, interceptor, nil)
18+
}
19+
20+
var tlsConfig = &tls.Config{}
21+
22+
func TestListen(t *testing.T) {
23+
cases := []struct {
24+
desc string
25+
address string
26+
target string
27+
err error
28+
}{
29+
{
30+
desc: "listen with valid address",
31+
address: "localhost:8080",
32+
target: "localhost:8080",
33+
err: nil,
34+
},
35+
// {
36+
// desc: "listen with invalid address",
37+
// address: "0000",
38+
// target: "localhost:8080",
39+
// err: nil,
40+
// },
41+
}
42+
for _, c := range cases {
43+
proxy := newProxy(c.address, c.target)
44+
go func() {
45+
err := proxy.Listen(context.Background())
46+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
47+
}()
48+
}
49+
}
50+
51+
func TestListenTLS(t *testing.T) {
52+
cases := []struct {
53+
desc string
54+
address string
55+
target string
56+
err error
57+
}{
58+
{
59+
desc: "listen with valid address",
60+
address: "localhost:8080",
61+
target: "localhost:8080",
62+
err: nil,
63+
},
64+
// {
65+
// desc: "listen with invalid address",
66+
// address: "0000",
67+
// target: "localhost:8080",
68+
// err: nil,
69+
// },
70+
}
71+
for _, c := range cases {
72+
73+
proxy := newProxy(c.address, c.target)
74+
go func() {
75+
err := proxy.ListenTLS(context.Background(), tlsConfig)
76+
assert.Nil(t, err, fmt.Sprintf("expected nil got %s\n", err))
77+
}()
78+
}
79+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package websocket_test
2+
3+
import (
4+
"log/slog"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/absmach/mproxy/pkg/mqtt/websocket"
9+
"github.com/absmach/mproxy/pkg/session/mocks"
10+
)
11+
12+
func newProxy(target, path, scheme string) *websocket.Proxy {
13+
handler := new(mocks.Handler)
14+
interceptor := new(mocks.Interceptor)
15+
logger := new(slog.Logger)
16+
return websocket.New(target, path, scheme, handler, interceptor, logger)
17+
}
18+
19+
func TestHandler(t *testing.T) {
20+
cases := []struct {
21+
desc string
22+
target string
23+
path string
24+
scheme string
25+
}{
26+
{
27+
desc: "handler with valid target",
28+
target: "localhost:8080",
29+
path: "/",
30+
scheme: "ws",
31+
},
32+
}
33+
for _, c := range cases {
34+
proxy := newProxy(c.target, c.path, c.scheme)
35+
responseRecorder := httptest.NewRecorder()
36+
request := httptest.NewRequest("GET", "http://example.com", nil)
37+
proxy.Handler().ServeHTTP(responseRecorder, request)
38+
}
39+
}

pkg/session/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package session
33
import "context"
44

55
// Handler is an interface for mProxy hooks
6+
7+
//go:generate mockery --name Handler --filename handler.go --quiet --note "Copyright (c) Abstract Machines"
68
type Handler interface {
79
// Authorization on client `CONNECT`
810
// Each of the params are passed by reference, so that it can be changed

pkg/session/interceptor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
)
88

99
// Interceptor is an interface for mProxy intercept hook.
10+
11+
//go:generate mockery --name Interceptor --filename interceptor.go --quiet --note "Copyright (c) Abstract Machines"
1012
type Interceptor interface {
1113
// Intercept is called on every packet flowing through the Proxy.
1214
// Packets can be modified before being sent to the broker or the client.

0 commit comments

Comments
 (0)