github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/crypto/x509/root_darwin_test.go (about) 1 // Copyright 2013 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 ( 8 "runtime" 9 "testing" 10 "time" 11 ) 12 13 func TestSystemRoots(t *testing.T) { 14 switch runtime.GOARCH { 15 case "arm", "arm64": 16 t.Skipf("skipping on %s/%s, no system root", runtime.GOOS, runtime.GOARCH) 17 } 18 19 t0 := time.Now() 20 sysRoots := systemRootsPool() // actual system roots 21 sysRootsDuration := time.Since(t0) 22 23 t1 := time.Now() 24 execRoots, err := execSecurityRoots() // non-cgo roots 25 execSysRootsDuration := time.Since(t1) 26 27 if err != nil { 28 t.Fatalf("failed to read system roots: %v", err) 29 } 30 31 t.Logf(" cgo sys roots: %v", sysRootsDuration) 32 t.Logf("non-cgo sys roots: %v", execSysRootsDuration) 33 34 for _, tt := range []*CertPool{sysRoots, execRoots} { 35 if tt == nil { 36 t.Fatal("no system roots") 37 } 38 // On Mavericks, there are 212 bundled certs; require only 39 // 150 here, since this is just a sanity check, and the 40 // exact number will vary over time. 41 t.Logf("got %d roots", len(tt.certs)) 42 if want, have := 150, len(tt.certs); have < want { 43 t.Fatalf("want at least %d system roots, have %d", want, have) 44 } 45 } 46 47 // Check that the two cert pools are roughly the same; 48 // |A∩B| > max(|A|, |B|) / 2 should be a reasonably robust check. 49 50 isect := make(map[string]bool, len(sysRoots.certs)) 51 for _, c := range sysRoots.certs { 52 isect[string(c.Raw)] = true 53 } 54 55 have := 0 56 for _, c := range execRoots.certs { 57 if isect[string(c.Raw)] { 58 have++ 59 } 60 } 61 62 var want int 63 if nsys, nexec := len(sysRoots.certs), len(execRoots.certs); nsys > nexec { 64 want = nsys / 2 65 } else { 66 want = nexec / 2 67 } 68 69 if have < want { 70 t.Errorf("insufficient overlap between cgo and non-cgo roots; want at least %d, have %d", want, have) 71 } 72 }