root_darwin_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. package system
  5. import (
  6. "crypto/x509"
  7. "runtime"
  8. "testing"
  9. )
  10. func TestSystemRoots(t *testing.T) {
  11. switch runtime.GOARCH {
  12. case "arm", "arm64":
  13. t.Skipf("skipping on %s/%s, no system root", runtime.GOOS, runtime.GOARCH)
  14. }
  15. sysRoots := initSystemRoots() // actual system roots
  16. execRoots, err := execSecurityRoots() // non-cgo roots
  17. if err != nil {
  18. t.Fatalf("failed to read system roots: %v", err)
  19. }
  20. for _, tt := range [][]*x509.Certificate{sysRoots, execRoots} {
  21. if tt == nil {
  22. t.Fatal("no system roots")
  23. }
  24. // On Mavericks, there are 212 bundled certs; require only
  25. // 150 here, since this is just a sanity check, and the
  26. // exact number will vary over time.
  27. if want, have := 150, len(tt); have < want {
  28. t.Fatalf("want at least %d system roots, have %d", want, have)
  29. }
  30. }
  31. // Check that the two cert pools are roughly the same;
  32. // |A∩B| > max(|A|, |B|) / 2 should be a reasonably robust check.
  33. isect := make(map[string]bool, len(sysRoots))
  34. for _, c := range sysRoots {
  35. isect[string(c.Raw)] = true
  36. }
  37. have := 0
  38. for _, c := range execRoots {
  39. if isect[string(c.Raw)] {
  40. have++
  41. }
  42. }
  43. var want int
  44. if nsys, nexec := len(sysRoots), len(execRoots); nsys > nexec {
  45. want = nsys / 2
  46. } else {
  47. want = nexec / 2
  48. }
  49. if have < want {
  50. t.Errorf("insufficient overlap between cgo and non-cgo roots; want at least %d, have %d", want, have)
  51. }
  52. }