...

Source file src/crypto/x509/cert_pool_test.go

Documentation: crypto/x509

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package x509
     6  
     7  import "testing"
     8  
     9  func TestCertPoolEqual(t *testing.T) {
    10  	tc := &Certificate{Raw: []byte{1, 2, 3}, RawSubject: []byte{2}}
    11  	otherTC := &Certificate{Raw: []byte{9, 8, 7}, RawSubject: []byte{8}}
    12  
    13  	emptyPool := NewCertPool()
    14  	nonSystemPopulated := NewCertPool()
    15  	nonSystemPopulated.AddCert(tc)
    16  	nonSystemPopulatedAlt := NewCertPool()
    17  	nonSystemPopulatedAlt.AddCert(otherTC)
    18  	emptySystem, err := SystemCertPool()
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	populatedSystem, err := SystemCertPool()
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	populatedSystem.AddCert(tc)
    27  	populatedSystemAlt, err := SystemCertPool()
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	populatedSystemAlt.AddCert(otherTC)
    32  	tests := []struct {
    33  		name  string
    34  		a     *CertPool
    35  		b     *CertPool
    36  		equal bool
    37  	}{
    38  		{
    39  			name:  "two empty pools",
    40  			a:     emptyPool,
    41  			b:     emptyPool,
    42  			equal: true,
    43  		},
    44  		{
    45  			name:  "one empty pool, one populated pool",
    46  			a:     emptyPool,
    47  			b:     nonSystemPopulated,
    48  			equal: false,
    49  		},
    50  		{
    51  			name:  "two populated pools",
    52  			a:     nonSystemPopulated,
    53  			b:     nonSystemPopulated,
    54  			equal: true,
    55  		},
    56  		{
    57  			name:  "two populated pools, different content",
    58  			a:     nonSystemPopulated,
    59  			b:     nonSystemPopulatedAlt,
    60  			equal: false,
    61  		},
    62  		{
    63  			name:  "two empty system pools",
    64  			a:     emptySystem,
    65  			b:     emptySystem,
    66  			equal: true,
    67  		},
    68  		{
    69  			name:  "one empty system pool, one populated system pool",
    70  			a:     emptySystem,
    71  			b:     populatedSystem,
    72  			equal: false,
    73  		},
    74  		{
    75  			name:  "two populated system pools",
    76  			a:     populatedSystem,
    77  			b:     populatedSystem,
    78  			equal: true,
    79  		},
    80  		{
    81  			name:  "two populated pools, different content",
    82  			a:     populatedSystem,
    83  			b:     populatedSystemAlt,
    84  			equal: false,
    85  		},
    86  		{
    87  			name:  "two nil pools",
    88  			a:     nil,
    89  			b:     nil,
    90  			equal: true,
    91  		},
    92  		{
    93  			name:  "one nil pool, one empty pool",
    94  			a:     nil,
    95  			b:     emptyPool,
    96  			equal: false,
    97  		},
    98  	}
    99  
   100  	for _, tc := range tests {
   101  		t.Run(tc.name, func(t *testing.T) {
   102  			equal := tc.a.Equal(tc.b)
   103  			if equal != tc.equal {
   104  				t.Errorf("Unexpected Equal result: got %t, want %t", equal, tc.equal)
   105  			}
   106  		})
   107  	}
   108  }
   109  

View as plain text