mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
205 lines
8.6 KiB
Go
205 lines
8.6 KiB
Go
|
package keymanager_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
||
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
||
|
)
|
||
|
|
||
|
var validClientCert = `-----BEGIN CERTIFICATE-----
|
||
|
MIIEITCCAgmgAwIBAgIQXUJWQZgVO4IX+zlWGI1/mTANBgkqhkiG9w0BAQsFADAU
|
||
|
MRIwEAYDVQQDEwlBdHRlc3RhbnQwHhcNMjAwMzE3MDgwNjU3WhcNMjEwOTE3MDc1
|
||
|
OTUyWjASMRAwDgYDVQQDEwdjbGllbnQxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
|
||
|
MIIBCgKCAQEAsc977g16Tan2j7YuA+zQOlDntb4Bkfs4sDOznOEvnozHwRZOgfcP
|
||
|
jVcA9AS5eZOGIRrsTssptrgVNDPoIHWoKk7LAKyyLM3dGp5PWeyMBoQA5cq+yPAT
|
||
|
4JkJpDnBFfwxXB99osJH0z3jSTRa62CSVvPRBisK4B9AlLQfcleEQlKJugy9tOAj
|
||
|
G7zodwEi+J4AYQHmOiwL38ZsKq9We5y4HMQ0E7de0FoU5QHrtuPNrTuwVwrq825l
|
||
|
cEAAFey6Btngx+sziysPHWHYOq4xOZ1UPBApeaAFLguzusc/4VwM7kzRNr4VOD8a
|
||
|
eC3CtKLhBBVVxHI5ZlaHS+YylNGYD4+FxQIDAQABo3EwbzAOBgNVHQ8BAf8EBAMC
|
||
|
A7gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB0GA1UdDgQWBBQDGCE0
|
||
|
3k4rHzB+Ycf3pt1MzeDPgzAfBgNVHSMEGDAWgBScIYZa4dQBIW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|
||
|
MVH80sXFC9siv5xW76FzvxKv/RZ6fKm8T+uizt8U+jwhL1flS4Ahj7zWUV6cwdWH
|
||
|
57O6FnN+VVNYbV4Ze8SzHS09eS1gBtityJVJttUJk70J/LtMPkaun/+VuMvAha7T
|
||
|
0tPn7P3RbGj8QYVUm+c8Z3arWaJ4K20n3v3rSYtLwV1PpI2T8nL8is7P1AUI4da+
|
||
|
JW5Xg09Yct1izRb64SylduQC9a1bbjoMU0iABaDzCl7AHzK0RlkjALQ4sIt24nKL
|
||
|
Geq0WUbSP2OuDkAf
|
||
|
-----END CERTIFICATE-----`
|
||
|
|
||
|
func TestNewRemoteWallet(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
opts string
|
||
|
clientCert string
|
||
|
clientKey string
|
||
|
caCert string
|
||
|
err string
|
||
|
}{
|
||
|
{
|
||
|
name: "Empty",
|
||
|
opts: ``,
|
||
|
err: "unexpected end of JSON input",
|
||
|
},
|
||
|
{
|
||
|
name: "NoAccounts",
|
||
|
opts: `{}`,
|
||
|
err: "at least one account specifier is required",
|
||
|
},
|
||
|
{
|
||
|
name: "NoCertificates",
|
||
|
opts: `{"accounts":["foo"]}`,
|
||
|
err: "certificates are required",
|
||
|
},
|
||
|
{
|
||
|
name: "NoClientCertificate",
|
||
|
opts: `{"accounts":["foo"],"certificates":{}}`,
|
||
|
err: "client certificate is required",
|
||
|
},
|
||
|
{
|
||
|
name: "NoClientKey",
|
||
|
opts: `{"accounts":["foo"],"certificates":{"client_cert":"foo"}}`,
|
||
|
err: "client key is required",
|
||
|
},
|
||
|
{
|
||
|
name: "MissingClientKey",
|
||
|
opts: `{"accounts":["foo"],"certificates":{"client_cert":"foo","client_key":"bar"}}`,
|
||
|
err: "failed to obtain client's certificate and/or key: open foo: no such file or directory",
|
||
|
},
|
||
|
{
|
||
|
name: "BadClientCert",
|
||
|
clientCert: `bad`,
|
||
|
clientKey: validClientKey,
|
||
|
opts: `{"accounts":["foo"],"certificates":{"client_cert":"<<clientcert>>","client_key":"<<clientkey>>"}}`,
|
||
|
err: "failed to obtain client's certificate and/or key: tls: failed to find any PEM data in certificate input",
|
||
|
},
|
||
|
{
|
||
|
name: "BadClientKey",
|
||
|
clientCert: validClientCert,
|
||
|
clientKey: `bad`,
|
||
|
opts: `{"accounts":["foo"],"certificates":{"client_cert":"<<clientcert>>","client_key":"<<clientkey>>"}}`,
|
||
|
err: "failed to obtain client's certificate and/or key: tls: failed to find any PEM data in key input",
|
||
|
},
|
||
|
{
|
||
|
name: "MissingCACert",
|
||
|
clientCert: validClientCert,
|
||
|
clientKey: validClientKey,
|
||
|
opts: `{"accounts":["foo"],"certificates":{"client_cert":"<<clientcert>>","client_key":"<<clientkey>>","ca_cert":"bad"}}`,
|
||
|
err: "failed to obtain server's CA certificate: open bad: no such file or directory",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, test := range tests {
|
||
|
t.Run(test.name, func(t *testing.T) {
|
||
|
|
||
|
if test.caCert != "" || test.clientCert != "" || test.clientKey != "" {
|
||
|
dir := fmt.Sprintf("%s/%s", testutil.TempDir(), test.name)
|
||
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
||
|
t.Fatalf(err.Error())
|
||
|
}
|
||
|
if test.caCert != "" {
|
||
|
caCertPath := fmt.Sprintf("%s/ca.crt", dir)
|
||
|
if err := ioutil.WriteFile(caCertPath, []byte(test.caCert), 0666); err != nil {
|
||
|
t.Fatalf("Failed to write CA certificate: %v", err)
|
||
|
}
|
||
|
test.opts = strings.ReplaceAll(test.opts, "<<cacert>>", caCertPath)
|
||
|
}
|
||
|
if test.clientCert != "" {
|
||
|
clientCertPath := fmt.Sprintf("%s/client.crt", dir)
|
||
|
if err := ioutil.WriteFile(clientCertPath, []byte(test.clientCert), 0666); err != nil {
|
||
|
t.Fatalf("Failed to write client certificate: %v", err)
|
||
|
}
|
||
|
test.opts = strings.ReplaceAll(test.opts, "<<clientcert>>", clientCertPath)
|
||
|
}
|
||
|
if test.clientKey != "" {
|
||
|
clientKeyPath := fmt.Sprintf("%s/client.key", dir)
|
||
|
if err := ioutil.WriteFile(clientKeyPath, []byte(test.clientKey), 0666); err != nil {
|
||
|
t.Fatalf("Failed to write client key: %v", err)
|
||
|
}
|
||
|
test.opts = strings.ReplaceAll(test.opts, "<<clientkey>>", clientKeyPath)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_, _, err := keymanager.NewRemoteWallet(test.opts)
|
||
|
if test.err == "" {
|
||
|
if err != nil {
|
||
|
t.Fatalf("Received unexpected error: %v", err.Error())
|
||
|
}
|
||
|
} else {
|
||
|
if err == nil {
|
||
|
t.Fatal("Did not received an error")
|
||
|
}
|
||
|
if err.Error() != test.err {
|
||
|
t.Fatalf("Did not received expected error: expected %v, received %v", test.err, err.Error())
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|