2017-10-19 18:49:18 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2018-06-27 04:03:15 +00:00
|
|
|
"context"
|
2017-10-19 18:49:18 +00:00
|
|
|
"encoding/json"
|
2018-11-09 03:20:25 +00:00
|
|
|
"os"
|
2017-10-19 18:49:18 +00:00
|
|
|
"testing"
|
2018-03-16 13:22:11 +00:00
|
|
|
"time"
|
2018-03-14 20:59:28 +00:00
|
|
|
|
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2017-10-19 18:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cfgJSON = []byte(`
|
|
|
|
{
|
2019-01-11 11:36:25 +00:00
|
|
|
"listen_multiaddress": "/ip4/127.0.0.1/tcp/12122",
|
2017-10-19 18:49:18 +00:00
|
|
|
"ssl_cert_file": "test/server.crt",
|
|
|
|
"ssl_key_file": "test/server.key",
|
|
|
|
"read_timeout": "30s",
|
|
|
|
"read_header_timeout": "5s",
|
|
|
|
"write_timeout": "1m0s",
|
2019-03-11 05:25:26 +00:00
|
|
|
"idle_timeout": "2m0s",
|
|
|
|
"max_header_bytes": 16384,
|
2019-01-11 11:36:25 +00:00
|
|
|
"basic_auth_credentials": null,
|
|
|
|
"cors_allowed_origins": ["myorigin"],
|
|
|
|
"cors_allowed_methods": ["GET"],
|
|
|
|
"cors_allowed_headers": ["X-Custom"],
|
|
|
|
"cors_exposed_headers": ["X-Chunked-Output"],
|
|
|
|
"cors_allow_credentials": false,
|
|
|
|
"cors_max_age": "1s"
|
2017-10-19 18:49:18 +00:00
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2019-03-07 05:28:06 +00:00
|
|
|
func TestLoadEmptyJSON(t *testing.T) {
|
|
|
|
cfg := &Config{}
|
|
|
|
err := cfg.LoadJSON([]byte(`{}`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:49:18 +00:00
|
|
|
func TestLoadJSON(t *testing.T) {
|
|
|
|
cfg := &Config{}
|
|
|
|
err := cfg.LoadJSON(cfgJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-16 13:22:11 +00:00
|
|
|
if cfg.ReadTimeout != 30*time.Second ||
|
|
|
|
cfg.WriteTimeout != time.Minute ||
|
|
|
|
cfg.ReadHeaderTimeout != 5*time.Second ||
|
|
|
|
cfg.IdleTimeout != 2*time.Minute {
|
|
|
|
t.Error("error parsing timeouts")
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:49:18 +00:00
|
|
|
j := &jsonConfig{}
|
|
|
|
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
2018-03-14 20:59:28 +00:00
|
|
|
j.HTTPListenMultiaddress = "abc"
|
2017-10-19 18:49:18 +00:00
|
|
|
tst, _ := json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error decoding listen multiaddress")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
2018-03-14 20:59:28 +00:00
|
|
|
j.ReadTimeout = "-1"
|
2017-10-19 18:49:18 +00:00
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error in read_timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.BasicAuthCreds = make(map[string]string)
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with empty basic auth map")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.SSLCertFile = "abc"
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with TLS configuration")
|
|
|
|
}
|
2018-03-14 20:59:28 +00:00
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.ID = "abc"
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.Libp2pListenMultiaddress = "abc"
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with libp2p address")
|
|
|
|
}
|
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.PrivateKey = "abc"
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with private key")
|
|
|
|
}
|
2019-03-07 05:28:06 +00:00
|
|
|
|
|
|
|
j = &jsonConfig{}
|
|
|
|
json.Unmarshal(cfgJSON, j)
|
|
|
|
j.MaxHeaderBytes = minMaxHeaderBytes - 1
|
|
|
|
tst, _ = json.Marshal(j)
|
|
|
|
err = cfg.LoadJSON(tst)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error with MaxHeaderBytes")
|
|
|
|
}
|
2018-03-14 20:59:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:36:51 +00:00
|
|
|
func TestApplyEnvVars(t *testing.T) {
|
2018-11-09 03:20:25 +00:00
|
|
|
username := "admin"
|
|
|
|
password := "thisaintmypassword"
|
|
|
|
user1 := "user1"
|
|
|
|
user1pass := "user1passwd"
|
|
|
|
os.Setenv("CLUSTER_RESTAPI_BASICAUTHCREDS", username+":"+password+","+user1+":"+user1pass)
|
|
|
|
cfg := &Config{}
|
2019-02-18 11:36:51 +00:00
|
|
|
cfg.Default()
|
|
|
|
err := cfg.ApplyEnvVars()
|
2018-11-09 03:20:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := cfg.BasicAuthCreds[username]; !ok {
|
|
|
|
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", username, cfg.BasicAuthCreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := cfg.BasicAuthCreds[user1]; !ok {
|
|
|
|
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", user1, cfg.BasicAuthCreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotpasswd := cfg.BasicAuthCreds[username]; gotpasswd != password {
|
|
|
|
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotpasswd := cfg.BasicAuthCreds[user1]; gotpasswd != user1pass {
|
|
|
|
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, user1pass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:59:28 +00:00
|
|
|
func TestLibp2pConfig(t *testing.T) {
|
2018-06-27 04:03:15 +00:00
|
|
|
ctx := context.Background()
|
2018-03-14 20:59:28 +00:00
|
|
|
cfg := &Config{}
|
|
|
|
err := cfg.Default()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
priv, pub, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
pid, err := peer.IDFromPublicKey(pub)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cfg.ID = pid
|
|
|
|
cfg.PrivateKey = priv
|
2018-03-14 22:52:49 +00:00
|
|
|
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
|
2018-03-14 20:59:28 +00:00
|
|
|
cfg.Libp2pListenAddr = addr
|
|
|
|
|
|
|
|
err = cfg.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:52:49 +00:00
|
|
|
cfgJSON, err := cfg.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.LoadJSON(cfgJSON)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test creating a new API with a libp2p config
|
2018-06-27 04:03:15 +00:00
|
|
|
rest, err := NewAPI(ctx, cfg)
|
2018-03-14 22:52:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-06-27 04:03:15 +00:00
|
|
|
defer rest.Shutdown(ctx)
|
2018-03-14 22:52:49 +00:00
|
|
|
|
|
|
|
badPid, _ := peer.IDB58Decode("QmTQ6oKHDwFjzr4ihirVCLJe8CxanxD3ZjGRYzubFuNDjE")
|
|
|
|
cfg.ID = badPid
|
2018-03-14 20:59:28 +00:00
|
|
|
err = cfg.Validate()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected id-privkey mismatch")
|
|
|
|
}
|
2018-03-14 22:52:49 +00:00
|
|
|
cfg.ID = pid
|
2018-03-14 20:59:28 +00:00
|
|
|
|
|
|
|
cfg.PrivateKey = nil
|
|
|
|
err = cfg.Validate()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected missing private key error")
|
|
|
|
}
|
2017-10-19 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToJSON(t *testing.T) {
|
|
|
|
cfg := &Config{}
|
|
|
|
cfg.LoadJSON(cfgJSON)
|
2017-10-20 08:42:41 +00:00
|
|
|
newjson, err := cfg.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cfg = &Config{}
|
|
|
|
err = cfg.LoadJSON(newjson)
|
2017-10-19 18:49:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
cfg := &Config{}
|
|
|
|
cfg.Default()
|
|
|
|
if cfg.Validate() != nil {
|
|
|
|
t.Fatal("error validating")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Default()
|
2018-03-14 20:59:28 +00:00
|
|
|
cfg.IdleTimeout = -1
|
2017-10-19 18:49:18 +00:00
|
|
|
if cfg.Validate() == nil {
|
|
|
|
t.Fatal("expected error validating")
|
|
|
|
}
|
|
|
|
}
|