Add a test method, fix nil pointer panic

This commit is contained in:
AudriusButkevicius 2015-09-06 18:35:38 +01:00
parent 541d05df1b
commit 11b2815b88
2 changed files with 29 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/url" "net/url"
"strconv" "strconv"
"strings"
"time" "time"
syncthingprotocol "github.com/syncthing/protocol" syncthingprotocol "github.com/syncthing/protocol"
@ -20,10 +21,10 @@ func GetInvitationFromRelay(uri *url.URL, id syncthingprotocol.DeviceID, certs [
} }
conn, err := tls.Dial("tcp", uri.Host, configForCerts(certs)) conn, err := tls.Dial("tcp", uri.Host, configForCerts(certs))
conn.SetDeadline(time.Now().Add(10 * time.Second))
if err != nil { if err != nil {
return protocol.SessionInvitation{}, err return protocol.SessionInvitation{}, err
} }
conn.SetDeadline(time.Now().Add(10 * time.Second))
if err := performHandshakeAndValidation(conn, uri); err != nil { if err := performHandshakeAndValidation(conn, uri); err != nil {
return protocol.SessionInvitation{}, err return protocol.SessionInvitation{}, err
@ -97,6 +98,25 @@ func JoinSession(invitation protocol.SessionInvitation) (net.Conn, error) {
} }
} }
func TestRelay(uri *url.URL, certs []tls.Certificate) bool {
id := syncthingprotocol.NewDeviceID(certs[0].Certificate[0])
c := NewProtocolClient(uri, certs, nil)
go c.Serve()
defer c.Stop()
for i := 0; i < 5; i++ {
_, err := GetInvitationFromRelay(uri, id, certs)
if err == nil {
return true
}
if !strings.Contains(err.Error(), "Incorrect response code") {
return false
}
time.Sleep(time.Second)
}
return false
}
func configForCerts(certs []tls.Certificate) *tls.Config { func configForCerts(certs []tls.Certificate) *tls.Config {
return &tls.Config{ return &tls.Config{
Certificates: certs, Certificates: certs,

View File

@ -23,10 +23,11 @@ func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
var connect, relay, dir string var connect, relay, dir string
var join bool var join, test bool
flag.StringVar(&connect, "connect", "", "Device ID to which to connect to") flag.StringVar(&connect, "connect", "", "Device ID to which to connect to")
flag.BoolVar(&join, "join", false, "Join relay") flag.BoolVar(&join, "join", false, "Join relay")
flag.BoolVar(&test, "test", false, "Generic relay test")
flag.StringVar(&relay, "relay", "relay://127.0.0.1:22067", "Relay address") flag.StringVar(&relay, "relay", "relay://127.0.0.1:22067", "Relay address")
flag.StringVar(&dir, "keys", ".", "Directory where cert.pem and key.pem is stored") flag.StringVar(&dir, "keys", ".", "Directory where cert.pem and key.pem is stored")
@ -99,6 +100,12 @@ func main() {
log.Println("Joined", conn.RemoteAddr(), conn.LocalAddr()) log.Println("Joined", conn.RemoteAddr(), conn.LocalAddr())
connectToStdio(stdin, conn) connectToStdio(stdin, conn)
log.Println("Finished", conn.RemoteAddr(), conn.LocalAddr()) log.Println("Finished", conn.RemoteAddr(), conn.LocalAddr())
} else if test {
if client.TestRelay(uri, []tls.Certificate{cert}) {
log.Println("OK")
} else {
log.Println("FAIL")
}
} else { } else {
log.Fatal("Requires either join or connect") log.Fatal("Requires either join or connect")
} }