github.com/emmansun/gmsm@v0.29.1/smx509/cert_pool_test.go (about) 1 package smx509 2 3 import "testing" 4 5 func TestCertPoolEqual(t *testing.T) { 6 tc := &Certificate{Raw: []byte{1, 2, 3}, RawSubject: []byte{2}} 7 otherTC := &Certificate{Raw: []byte{9, 8, 7}, RawSubject: []byte{8}} 8 9 emptyPool := NewCertPool() 10 nonSystemPopulated := NewCertPool() 11 nonSystemPopulated.AddCert(tc) 12 nonSystemPopulatedAlt := NewCertPool() 13 nonSystemPopulatedAlt.AddCert(otherTC) 14 emptySystem, err := SystemCertPool() 15 if err != nil { 16 t.Fatal(err) 17 } 18 populatedSystem, err := SystemCertPool() 19 if err != nil { 20 t.Fatal(err) 21 } 22 populatedSystem.AddCert(tc) 23 populatedSystemAlt, err := SystemCertPool() 24 if err != nil { 25 t.Fatal(err) 26 } 27 populatedSystemAlt.AddCert(otherTC) 28 tests := []struct { 29 name string 30 a *CertPool 31 b *CertPool 32 equal bool 33 }{ 34 { 35 name: "two empty pools", 36 a: emptyPool, 37 b: emptyPool, 38 equal: true, 39 }, 40 { 41 name: "one empty pool, one populated pool", 42 a: emptyPool, 43 b: nonSystemPopulated, 44 equal: false, 45 }, 46 { 47 name: "two populated pools", 48 a: nonSystemPopulated, 49 b: nonSystemPopulated, 50 equal: true, 51 }, 52 { 53 name: "two populated pools, different content", 54 a: nonSystemPopulated, 55 b: nonSystemPopulatedAlt, 56 equal: false, 57 }, 58 { 59 name: "two empty system pools", 60 a: emptySystem, 61 b: emptySystem, 62 equal: true, 63 }, 64 { 65 name: "one empty system pool, one populated system pool", 66 a: emptySystem, 67 b: populatedSystem, 68 equal: false, 69 }, 70 { 71 name: "two populated system pools", 72 a: populatedSystem, 73 b: populatedSystem, 74 equal: true, 75 }, 76 { 77 name: "two populated pools, different content", 78 a: populatedSystem, 79 b: populatedSystemAlt, 80 equal: false, 81 }, 82 { 83 name: "two nil pools", 84 a: nil, 85 b: nil, 86 equal: true, 87 }, 88 { 89 name: "one nil pool, one empty pool", 90 a: nil, 91 b: emptyPool, 92 equal: false, 93 }, 94 } 95 96 for _, tc := range tests { 97 t.Run(tc.name, func(t *testing.T) { 98 equal := tc.a.Equal(tc.b) 99 if equal != tc.equal { 100 t.Errorf("Unexpected Equal result: got %t, want %t", equal, tc.equal) 101 } 102 }) 103 } 104 }