status-go/server/certs_test.go

55 lines
1.3 KiB
Go
Raw Permalink Normal View History

package server
import (
"net"
"testing"
"time"
"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/server/servertest"
)
func TestCerts(t *testing.T) {
suite.Run(t, new(CertsSuite))
}
type CertsSuite struct {
suite.Suite
servertest.TestKeyComponents
servertest.TestCertComponents
}
func (s *CertsSuite) SetupSuite() {
s.SetupKeyComponents(s.T())
s.SetupCertComponents(s.T())
}
func (s *CertsSuite) TestToECDSA() {
k := ToECDSA(base58.Decode(servertest.DB58))
s.Require().NotNil(k.PublicKey.X)
s.Require().NotNil(k.PublicKey.Y)
s.Require().Zero(k.PublicKey.X.Cmp(s.X))
s.Require().Zero(k.PublicKey.Y.Cmp(s.Y))
s.Require().Zero(k.D.Cmp(s.D))
b58 := base58.Encode(s.D.Bytes())
s.Require().Equal(servertest.DB58, b58)
}
func (s *CertsSuite) TestGenerateX509Cert() {
notBefore := time.Now()
notAfter := notBefore.Add(time.Hour)
c1 := GenerateX509Cert(s.SN, notBefore, notAfter, []net.IP{}, []string{Localhost})
s.Require().Exactly([]string{Localhost}, c1.DNSNames)
s.Require().Empty(c1.IPAddresses)
c2 := GenerateX509Cert(s.SN, notBefore, notAfter, []net.IP{LocalHostIP}, []string{})
s.Require().Len(c2.IPAddresses, 1)
s.Require().Equal(LocalHostIP.String(), c2.IPAddresses[0].String())
s.Require().Empty(c2.DNSNames)
}