go-ethereum/ethstats/ethstats_test.go
meowsbits 10962b685e
ethstats: fix URL parser for '@' or ':' in node name/password (#21640)
Fixes the case (example below) where the value passed
to --ethstats flag would be parsed wrongly because the
node name and/or password value contained the special
characters '@' or ':'

    --ethstats "ETC Labs Metrics @meowsbits":mypass@ws://mordor.dash.fault.dev:3000
2021-05-25 23:22:46 +02:00

68 lines
1.8 KiB
Go

package ethstats
import (
"strconv"
"testing"
)
func TestParseEthstatsURL(t *testing.T) {
cases := []struct {
url string
node, pass, host string
}{
{
url: `"debug meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `"debug @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `"debug: @meowsbits":mypass@ws://mordor.dash.fault.dev:3000`,
node: "debug: @meowsbits", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `name:@ws://mordor.dash.fault.dev:3000`,
node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `name@ws://mordor.dash.fault.dev:3000`,
node: "name", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `:mypass@ws://mordor.dash.fault.dev:3000`,
node: "", pass: "mypass", host: "ws://mordor.dash.fault.dev:3000",
},
{
url: `:@ws://mordor.dash.fault.dev:3000`,
node: "", pass: "", host: "ws://mordor.dash.fault.dev:3000",
},
}
for i, c := range cases {
parts, err := parseEthstatsURL(c.url)
if err != nil {
t.Fatal(err)
}
node, pass, host := parts[0], parts[1], parts[2]
// unquote because the value provided will be used as a CLI flag value, so unescaped quotes will be removed
nodeUnquote, err := strconv.Unquote(node)
if err == nil {
node = nodeUnquote
}
if node != c.node {
t.Errorf("case=%d mismatch node value, got: %v ,want: %v", i, node, c.node)
}
if pass != c.pass {
t.Errorf("case=%d mismatch pass value, got: %v ,want: %v", i, pass, c.pass)
}
if host != c.host {
t.Errorf("case=%d mismatch host value, got: %v ,want: %v", i, host, c.host)
}
}
}