acme_118_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //go:build go1.18
  2. package provisioner
  3. import (
  4. "bytes"
  5. "crypto/x509"
  6. "os"
  7. "testing"
  8. )
  9. func TestACME_GetAttestationRoots(t *testing.T) {
  10. appleCA, err := os.ReadFile("testdata/certs/apple-att-ca.crt")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. yubicoCA, err := os.ReadFile("testdata/certs/yubico-piv-ca.crt")
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. pool := x509.NewCertPool()
  19. pool.AppendCertsFromPEM(appleCA)
  20. pool.AppendCertsFromPEM(yubicoCA)
  21. type fields struct {
  22. Type string
  23. Name string
  24. AttestationRoots []byte
  25. }
  26. tests := []struct {
  27. name string
  28. fields fields
  29. want *x509.CertPool
  30. want1 bool
  31. }{
  32. {"ok", fields{"ACME", "acme", bytes.Join([][]byte{appleCA, yubicoCA}, []byte("\n"))}, pool, true},
  33. {"nil", fields{"ACME", "acme", nil}, nil, false},
  34. {"empty", fields{"ACME", "acme", []byte{}}, nil, false},
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. p := &ACME{
  39. Type: tt.fields.Type,
  40. Name: tt.fields.Name,
  41. AttestationRoots: tt.fields.AttestationRoots,
  42. }
  43. if err := p.Init(Config{
  44. Claims: globalProvisionerClaims,
  45. Audiences: testAudiences,
  46. }); err != nil {
  47. t.Fatal(err)
  48. }
  49. got, got1 := p.GetAttestationRoots()
  50. switch {
  51. case tt.want == nil && got == nil:
  52. break
  53. case tt.want == nil && got != nil, tt.want != nil && got == nil:
  54. t.Errorf("ACME.GetAttestationRoots() got = %v, want %v", got, tt.want)
  55. default:
  56. //nolint:staticcheck // this file only runs in go1.18
  57. gotSubjects := got.Subjects()
  58. //nolint:staticcheck // this file only runs in go1.18
  59. wantSubjects := tt.want.Subjects()
  60. if len(gotSubjects) != len(wantSubjects) {
  61. t.Errorf("ACME.GetAttestationRoots() got = %v, want %v", got, tt.want)
  62. } else {
  63. for i, gotSub := range gotSubjects {
  64. if !bytes.Equal(gotSub, wantSubjects[i]) {
  65. t.Errorf("ACME.GetAttestationRoots() got = %v, want %v", got, tt.want)
  66. break
  67. }
  68. }
  69. }
  70. }
  71. if got1 != tt.want1 {
  72. t.Errorf("ACME.GetAttestationRoots() got1 = %v, want %v", got1, tt.want1)
  73. }
  74. })
  75. }
  76. }