github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/pki/certificate_test.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package pki_test
     5  
     6  import (
     7  	"crypto/x509"
     8  	"crypto/x509/pkix"
     9  	"net"
    10  	"net/url"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/pki"
    16  )
    17  
    18  type CertificateSuite struct {
    19  }
    20  
    21  var _ = gc.Suite(&CertificateSuite{})
    22  
    23  func (cs *CertificateSuite) VerifyCSRToCertificate(c *gc.C) {
    24  	jujuURL, err := url.Parse("https://discourse.juju.is")
    25  	c.Assert(err, jc.ErrorIsNil)
    26  
    27  	subject := pkix.Name{
    28  		CommonName:         "juju test",
    29  		Country:            []string{"Australia"},
    30  		Organization:       []string{"Canonical"},
    31  		OrganizationalUnit: []string{"Juju"},
    32  	}
    33  	csr := x509.CertificateRequest{
    34  		Subject:        subject,
    35  		DNSNames:       []string{"juju.is"},
    36  		EmailAddresses: []string{"juju@juju.is"},
    37  		IPAddresses:    []net.IP{net.ParseIP("fe80:abcd:12")},
    38  		URIs:           []*url.URL{jujuURL},
    39  	}
    40  
    41  	expectedCert := x509.Certificate{
    42  		Subject:        csr.Subject,
    43  		DNSNames:       csr.DNSNames,
    44  		EmailAddresses: csr.EmailAddresses,
    45  		IPAddresses:    csr.IPAddresses,
    46  		URIs:           csr.URIs,
    47  	}
    48  
    49  	rCert := pki.CSRToCertificate(&csr)
    50  	c.Assert(rCert, jc.DeepEquals, &expectedCert)
    51  }
    52  
    53  func (cs *CertificateSuite) CheckPkixNameFromDefaults(c *gc.C) {
    54  	tests := []struct {
    55  		Template pkix.Name
    56  		Request  pkix.Name
    57  		RVal     pkix.Name
    58  	}{
    59  		{
    60  			Template: pkix.Name{
    61  				Country:      []string{"Australia"},
    62  				Organization: []string{"Canonical"},
    63  			},
    64  			Request: pkix.Name{
    65  				Country:    []string{"New Zealand"},
    66  				PostalCode: []string{"4000"},
    67  				CommonName: "Juju Testing",
    68  			},
    69  			RVal: pkix.Name{
    70  				Country:      []string{"New Zealand"},
    71  				Organization: []string{"Canonical"},
    72  				PostalCode:   []string{"4000"},
    73  				CommonName:   "Juju Testing",
    74  			},
    75  		},
    76  	}
    77  
    78  	for _, test := range tests {
    79  		rval := pki.MakeX509NameFromDefaults(&test.Template, &test.Request)
    80  		c.Assert(rval, jc.DeepEquals, test.RVal)
    81  	}
    82  }