extension_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package apiv1
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestCreateCertificateAuthorityExtension(t *testing.T) {
  9. type args struct {
  10. typ Type
  11. certificateID string
  12. keyValuePairs []string
  13. }
  14. tests := []struct {
  15. name string
  16. args args
  17. want pkix.Extension
  18. wantErr bool
  19. }{
  20. {"ok", args{Type(CloudCAS), "1ac75689-cd3f-482e-a695-8a13daf39dc4", nil}, pkix.Extension{
  21. Id: oidStepCertificateAuthority,
  22. Critical: false,
  23. Value: []byte{
  24. 0x30, 0x30, 0x13, 0x08, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x63, 0x61, 0x73, 0x13, 0x24, 0x31, 0x61,
  25. 0x63, 0x37, 0x35, 0x36, 0x38, 0x39, 0x2d, 0x63, 0x64, 0x33, 0x66, 0x2d, 0x34, 0x38, 0x32, 0x65,
  26. 0x2d, 0x61, 0x36, 0x39, 0x35, 0x2d, 0x38, 0x61, 0x31, 0x33, 0x64, 0x61, 0x66, 0x33, 0x39, 0x64,
  27. 0x63, 0x34,
  28. },
  29. }, false},
  30. {"ok", args{Type(CloudCAS), "1ac75689-cd3f-482e-a695-8a13daf39dc4", []string{"foo", "bar"}}, pkix.Extension{
  31. Id: oidStepCertificateAuthority,
  32. Critical: false,
  33. Value: []byte{
  34. 0x30, 0x3c, 0x13, 0x08, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x63, 0x61, 0x73, 0x13, 0x24, 0x31, 0x61,
  35. 0x63, 0x37, 0x35, 0x36, 0x38, 0x39, 0x2d, 0x63, 0x64, 0x33, 0x66, 0x2d, 0x34, 0x38, 0x32, 0x65,
  36. 0x2d, 0x61, 0x36, 0x39, 0x35, 0x2d, 0x38, 0x61, 0x31, 0x33, 0x64, 0x61, 0x66, 0x33, 0x39, 0x64,
  37. 0x63, 0x34, 0x30, 0x0a, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x13, 0x03, 0x62, 0x61, 0x72,
  38. },
  39. }, false},
  40. }
  41. for _, tt := range tests {
  42. t.Run(tt.name, func(t *testing.T) {
  43. got, err := CreateCertificateAuthorityExtension(tt.args.typ, tt.args.certificateID, tt.args.keyValuePairs...)
  44. if (err != nil) != tt.wantErr {
  45. t.Errorf("CreateCertificateAuthorityExtension() error = %v, wantErr %v", err, tt.wantErr)
  46. return
  47. }
  48. if !reflect.DeepEqual(got, tt.want) {
  49. t.Errorf("CreateCertificateAuthorityExtension() = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }
  54. func TestFindCertificateAuthorityExtension(t *testing.T) {
  55. expected := pkix.Extension{
  56. Id: oidStepCertificateAuthority,
  57. Value: []byte("fake data"),
  58. }
  59. type args struct {
  60. cert *x509.Certificate
  61. }
  62. tests := []struct {
  63. name string
  64. args args
  65. want pkix.Extension
  66. want1 bool
  67. }{
  68. {"first", args{&x509.Certificate{Extensions: []pkix.Extension{
  69. expected,
  70. {Id: []int{1, 2, 3, 4}},
  71. }}}, expected, true},
  72. {"last", args{&x509.Certificate{Extensions: []pkix.Extension{
  73. {Id: []int{1, 2, 3, 4}},
  74. {Id: []int{2, 3, 4, 5}},
  75. expected,
  76. }}}, expected, true},
  77. {"fail", args{&x509.Certificate{Extensions: []pkix.Extension{
  78. {Id: []int{1, 2, 3, 4}},
  79. }}}, pkix.Extension{}, false},
  80. {"fail ExtraExtensions", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
  81. expected,
  82. {Id: []int{1, 2, 3, 4}},
  83. }}}, pkix.Extension{}, false},
  84. }
  85. for _, tt := range tests {
  86. t.Run(tt.name, func(t *testing.T) {
  87. got, got1 := FindCertificateAuthorityExtension(tt.args.cert)
  88. if !reflect.DeepEqual(got, tt.want) {
  89. t.Errorf("FindCertificateAuthorityExtension() got = %v, want %v", got, tt.want)
  90. }
  91. if got1 != tt.want1 {
  92. t.Errorf("FindCertificateAuthorityExtension() got1 = %v, want %v", got1, tt.want1)
  93. }
  94. })
  95. }
  96. }
  97. func TestRemoveCertificateAuthorityExtension(t *testing.T) {
  98. caExt := pkix.Extension{
  99. Id: oidStepCertificateAuthority,
  100. Value: []byte("fake data"),
  101. }
  102. type args struct {
  103. cert *x509.Certificate
  104. }
  105. tests := []struct {
  106. name string
  107. args args
  108. want *x509.Certificate
  109. }{
  110. {"first", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
  111. caExt,
  112. {Id: []int{1, 2, 3, 4}},
  113. }}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
  114. {Id: []int{1, 2, 3, 4}},
  115. }}},
  116. {"last", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
  117. {Id: []int{1, 2, 3, 4}},
  118. caExt,
  119. }}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
  120. {Id: []int{1, 2, 3, 4}},
  121. }}},
  122. {"missing", args{&x509.Certificate{ExtraExtensions: []pkix.Extension{
  123. {Id: []int{1, 2, 3, 4}},
  124. }}}, &x509.Certificate{ExtraExtensions: []pkix.Extension{
  125. {Id: []int{1, 2, 3, 4}},
  126. }}},
  127. {"extensions", args{&x509.Certificate{Extensions: []pkix.Extension{
  128. caExt,
  129. {Id: []int{1, 2, 3, 4}},
  130. }}}, &x509.Certificate{Extensions: []pkix.Extension{
  131. caExt,
  132. {Id: []int{1, 2, 3, 4}},
  133. }}},
  134. }
  135. for _, tt := range tests {
  136. t.Run(tt.name, func(t *testing.T) {
  137. RemoveCertificateAuthorityExtension(tt.args.cert)
  138. if !reflect.DeepEqual(tt.args.cert, tt.want) {
  139. t.Errorf("RemoveCertificateAuthorityExtension() cert = %v, want %v", tt.args.cert, tt.want)
  140. }
  141. })
  142. }
  143. }