github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/pki/leaf_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  	"net"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/pki"
    13  	pkitest "github.com/juju/juju/pki/test"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type LeafSuite struct {
    18  }
    19  
    20  var _ = gc.Suite(&LeafSuite{})
    21  
    22  func (l *LeafSuite) TestLeafHasDNSNames(c *gc.C) {
    23  	authority, err := pkitest.NewTestAuthority()
    24  	c.Assert(err, jc.ErrorIsNil)
    25  
    26  	tests := []struct {
    27  		CertDNSNames  []string
    28  		CheckDNSNames []string
    29  		Result        bool
    30  	}{
    31  		{
    32  			CertDNSNames:  []string{"juju-apiserver", "mongodb"},
    33  			CheckDNSNames: []string{"juju-apiserver"},
    34  			Result:        true,
    35  		},
    36  		{
    37  			CertDNSNames:  []string{"juju-apiserver", "mongodb"},
    38  			CheckDNSNames: []string{"juju-apiserver", "mongodb"},
    39  			Result:        true,
    40  		},
    41  		{
    42  			CertDNSNames:  []string{"juju-apiserver", "mongodb"},
    43  			CheckDNSNames: []string{"juju-apiserver1", "mongodb"},
    44  			Result:        false,
    45  		},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		leaf, err := authority.LeafRequestForGroup(pki.DefaultLeafGroup).
    50  			AddDNSNames(test.CertDNSNames...).
    51  			Commit()
    52  		c.Assert(err, jc.ErrorIsNil)
    53  		c.Assert(pki.LeafHasDNSNames(leaf, test.CheckDNSNames), gc.Equals,
    54  			test.Result)
    55  	}
    56  }
    57  
    58  func (l *LeafSuite) TestLeafIPAddresses(c *gc.C) {
    59  	authority, err := pkitest.NewTestAuthority()
    60  	c.Assert(err, jc.ErrorIsNil)
    61  
    62  	tests := []struct {
    63  		CertIPAddresses  []net.IP
    64  		CheckIPAddresses []net.IP
    65  		Result           bool
    66  	}{
    67  		{
    68  			CertIPAddresses:  []net.IP{net.ParseIP("fe80::abcd:1")},
    69  			CheckIPAddresses: []net.IP{net.ParseIP("fe80::abcd:1")},
    70  			Result:           true,
    71  		},
    72  	}
    73  
    74  	for _, test := range tests {
    75  		leaf, err := authority.LeafRequestForGroup(pki.DefaultLeafGroup).
    76  			AddIPAddresses(test.CertIPAddresses...).
    77  			Commit()
    78  		c.Assert(err, jc.ErrorIsNil)
    79  		c.Assert(leaf.Certificate().IPAddresses, testing.IPsEqual, test.CheckIPAddresses)
    80  	}
    81  }