File size: 2,716 Bytes
4bcc2be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | // Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package x509
import (
"testing"
)
func TestFallbackPanic(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("Multiple calls to SetFallbackRoots should panic")
}
}()
SetFallbackRoots(nil)
SetFallbackRoots(nil)
}
func TestFallback(t *testing.T) {
// call systemRootsPool so that the sync.Once is triggered, and we can
// manipulate systemRoots without worrying about our working being overwritten
systemRootsPool()
if systemRoots != nil {
originalSystemRoots := *systemRoots
defer func() { systemRoots = &originalSystemRoots }()
}
tests := []struct {
name string
systemRoots *CertPool
systemPool bool
poolContent []*Certificate
forceFallback bool
returnsFallback bool
}{
{
name: "nil systemRoots",
returnsFallback: true,
},
{
name: "empty systemRoots",
systemRoots: NewCertPool(),
returnsFallback: true,
},
{
name: "empty systemRoots system pool",
systemRoots: NewCertPool(),
systemPool: true,
},
{
name: "filled systemRoots system pool",
systemRoots: NewCertPool(),
poolContent: []*Certificate{{}},
systemPool: true,
},
{
name: "filled systemRoots",
systemRoots: NewCertPool(),
poolContent: []*Certificate{{}},
},
{
name: "filled systemRoots, force fallback",
systemRoots: NewCertPool(),
poolContent: []*Certificate{{}},
forceFallback: true,
returnsFallback: true,
},
{
name: "filled systemRoot system pool, force fallback",
systemRoots: NewCertPool(),
poolContent: []*Certificate{{}},
systemPool: true,
forceFallback: true,
returnsFallback: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
useFallbackRoots = false
fallbacksSet = false
systemRoots = tc.systemRoots
if systemRoots != nil {
systemRoots.systemPool = tc.systemPool
}
for _, c := range tc.poolContent {
systemRoots.AddCert(c)
}
if tc.forceFallback {
t.Setenv("GODEBUG", "x509usefallbackroots=1")
} else {
t.Setenv("GODEBUG", "x509usefallbackroots=0")
}
fallbackPool := NewCertPool()
SetFallbackRoots(fallbackPool)
systemPoolIsFallback := systemRoots == fallbackPool
if tc.returnsFallback && !systemPoolIsFallback {
t.Error("systemRoots was not set to fallback pool")
} else if !tc.returnsFallback && systemPoolIsFallback {
t.Error("systemRoots was set to fallback pool when it shouldn't have been")
}
})
}
}
|