acme_118_test.go 2.1 KB

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