2024-04-05 11:31:40 +02:00
|
|
|
package pass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2024-04-09 18:20:44 +02:00
|
|
|
func msgOfSize(size int, c byte) string {
|
|
|
|
msg := make([]byte, size)
|
|
|
|
|
|
|
|
for i := range msg {
|
|
|
|
msg[i] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(msg)
|
|
|
|
}
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
func TestPairHappyFlow(t *testing.T) {
|
|
|
|
resp, err := configureBrowserExtension()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to configure browser extension: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-18 09:02:32 +02:00
|
|
|
deviceID := getDeviceID()
|
|
|
|
testPairing(t, deviceID, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPairMultipleTimes(t *testing.T) {
|
|
|
|
resp, err := configureBrowserExtension()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to configure browser extension: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceID := getDeviceID()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
testPairing(t, deviceID, resp)
|
|
|
|
if t.Failed() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPairing(t *testing.T, deviceID string, resp ConfigureBrowserExtensionResponse) {
|
|
|
|
t.Helper()
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
browserExtensionDone := make(chan struct{})
|
|
|
|
mobileDone := make(chan struct{})
|
|
|
|
|
2024-04-09 18:20:44 +02:00
|
|
|
const messageSize = 1024 * 1024
|
|
|
|
|
2024-04-05 11:31:40 +02:00
|
|
|
go func() {
|
|
|
|
defer close(browserExtensionDone)
|
|
|
|
|
|
|
|
extProxyToken, _, err := browserExtensionWaitForConfirm(resp.BrowserExtensionPairingToken)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error when Browser Extension waited for confirm: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proxyWebSocket(
|
|
|
|
getWsURL()+"/browser_extension/proxy_to_mobile",
|
|
|
|
extProxyToken,
|
2024-04-09 18:20:44 +02:00
|
|
|
msgOfSize(messageSize, 'b'),
|
|
|
|
msgOfSize(messageSize, 'm'),
|
|
|
|
)
|
2024-04-05 11:31:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Browser Extension: proxy failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer close(mobileDone)
|
|
|
|
|
2024-04-18 09:02:32 +02:00
|
|
|
mobileProxyToken, err := confirmMobile(resp.ConnectionToken, deviceID, uuid.NewString())
|
2024-04-05 11:31:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Mobile: confirm failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proxyWebSocket(
|
|
|
|
getWsURL()+"/mobile/proxy_to_browser_extension",
|
|
|
|
mobileProxyToken,
|
2024-04-09 18:20:44 +02:00
|
|
|
msgOfSize(messageSize, 'm'),
|
|
|
|
msgOfSize(messageSize, 'b'),
|
2024-04-05 11:31:40 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Mobile: proxy failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-browserExtensionDone
|
|
|
|
<-mobileDone
|
|
|
|
}
|