Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update rawtrafficcontroller kind switch to support mqttproxy #694

Merged
merged 4 commits into from
Jul 10, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
integration test
  • Loading branch information
suchen-sci committed Jul 8, 2022
commit a700f340a37f32054140ff9cd6253094f7021d89
37 changes: 5 additions & 32 deletions build/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ package test

import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"

paho "github.com/eclipse/paho.mqtt.golang"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -241,13 +240,8 @@ filters:
assert.Equal("hello from backend", string(data))
}

func sha256Sum(data []byte) string {
sha256Bytes := sha256.Sum256(data)
return hex.EncodeToString(sha256Bytes[:])
}

func getMQTTClient(port int, clientID, userName, password string) (paho.Client, error) {
opts := paho.NewClientOptions().AddBroker(fmt.Sprintf("tcp:https://0.0.0.0:%d", port)).SetClientID(clientID).SetUsername(userName).SetPassword(password)
func getMQTTClient(clientID, userName, password string) (paho.Client, error) {
opts := paho.NewClientOptions().AddBroker("tcp:https://0.0.0.0:1883").SetClientID(clientID).SetUsername(userName).SetPassword(password)
c := paho.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
return nil, token.Error()
Expand All @@ -258,38 +252,17 @@ func getMQTTClient(port int, clientID, userName, password string) (paho.Client,
func TestMQTTProxy(t *testing.T) {
assert := assert.New(t)

// create httpserver
yamlStr := `
kind: MQTTProxy
name: mqttproxy-test
port: 1883
rules:
- when:
packetType: Connect
pipeline: pipeline-mqtt-auth
`
ok, msg := createObject(t, yamlStr)
assert.True(ok, msg)
defer deleteObject(t, "mqttproxy-test")

yamlStr = `
kind: Pipeline
name: pipeline-mqtt-auth
filters:
- name: mqtt-auth
kind: MQTTClientAuth
auth:
- username: test
saltedSha256Pass: %v
`
yamlStr = fmt.Sprintf(yamlStr, sha256Sum([]byte("test")))
ok, msg = createObject(t, yamlStr)
assert.True(ok, msg)
defer deleteObject(t, "pipeline-mqtt-auth")
time.Sleep(time.Second * 2)

_, err := getMQTTClient(1883, "client1", "test", "test")
_, err := getMQTTClient("client1", "test", "test")
assert.Nil(err)

_, err = getMQTTClient(1883, "client2", "test", "wrongPassword")
assert.NotNil(err, "wrong password should be rejected")
}