2022-06-15 14:49:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-03-21 12:22:46 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/server/servertest"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCerts(t *testing.T) {
|
|
|
|
suite.Run(t, new(CertsSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type CertsSuite struct {
|
|
|
|
suite.Suite
|
2023-03-21 12:22:46 +00:00
|
|
|
servertest.TestKeyComponents
|
|
|
|
servertest.TestCertComponents
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertsSuite) SetupSuite() {
|
2022-06-15 15:36:17 +00:00
|
|
|
s.SetupKeyComponents(s.T())
|
|
|
|
s.SetupCertComponents(s.T())
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertsSuite) TestToECDSA() {
|
2023-03-21 12:22:46 +00:00
|
|
|
k := ToECDSA(base58.Decode(servertest.DB58))
|
2022-06-15 14:49:31 +00:00
|
|
|
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())
|
2023-03-21 12:22:46 +00:00
|
|
|
s.Require().Equal(servertest.DB58, b58)
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertsSuite) TestGenerateX509Cert() {
|
|
|
|
notBefore := time.Now()
|
|
|
|
notAfter := notBefore.Add(time.Hour)
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
c1 := GenerateX509Cert(s.SN, notBefore, notAfter, Localhost)
|
|
|
|
s.Require().Exactly([]string{Localhost}, c1.DNSNames)
|
2022-06-15 14:49:31 +00:00
|
|
|
s.Require().Nil(c1.IPAddresses)
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
c2 := GenerateX509Cert(s.SN, notBefore, notAfter, DefaultIP.String())
|
2022-06-15 14:49:31 +00:00
|
|
|
s.Require().Len(c2.IPAddresses, 1)
|
2023-01-06 12:21:14 +00:00
|
|
|
s.Require().Equal(DefaultIP.String(), c2.IPAddresses[0].String())
|
2022-06-15 14:49:31 +00:00
|
|
|
s.Require().Nil(c2.DNSNames)
|
|
|
|
}
|