|
| 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 x509fork |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/x509" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCertPoolEqual(t *testing.T) { |
| 13 | + tc := &x509.Certificate{Raw: []byte{1, 2, 3}, RawSubject: []byte{2}} |
| 14 | + otherTC := &x509.Certificate{Raw: []byte{9, 8, 7}, RawSubject: []byte{8}} |
| 15 | + |
| 16 | + emptyPool := NewCertPool() |
| 17 | + nonSystemPopulated := NewCertPool() |
| 18 | + nonSystemPopulated.AddCert(tc) |
| 19 | + nonSystemPopulatedAlt := NewCertPool() |
| 20 | + nonSystemPopulatedAlt.AddCert(otherTC) |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + a *CertPool |
| 24 | + b *CertPool |
| 25 | + equal bool |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "two empty pools", |
| 29 | + a: emptyPool, |
| 30 | + b: emptyPool, |
| 31 | + equal: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "one empty pool, one populated pool", |
| 35 | + a: emptyPool, |
| 36 | + b: nonSystemPopulated, |
| 37 | + equal: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "two populated pools", |
| 41 | + a: nonSystemPopulated, |
| 42 | + b: nonSystemPopulated, |
| 43 | + equal: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "two populated pools, different content", |
| 47 | + a: nonSystemPopulated, |
| 48 | + b: nonSystemPopulatedAlt, |
| 49 | + equal: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "two nil pools", |
| 53 | + a: nil, |
| 54 | + b: nil, |
| 55 | + equal: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "one nil pool, one empty pool", |
| 59 | + a: nil, |
| 60 | + b: emptyPool, |
| 61 | + equal: false, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + equal := tc.a.Equal(tc.b) |
| 68 | + if equal != tc.equal { |
| 69 | + t.Errorf("Unexpected Equal result: got %t, want %t", equal, tc.equal) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments